A program to read a Phidget IR sensor and log pull-ups with Fitbit's API
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

unify debugging/verbose printing

+36 -34
+2 -7
lib/logger/beeper.rb
··· 10 10 11 11 def after_initialize 12 12 if @enabled = !!@parent.config["sound"] 13 - if @parent.config["verbose"] 14 - puts "#{Time.now.to_f} - playing sound file " << 15 - @parent.config["sound"] 16 - end 13 + @parent.vputs "playing sound file " << @parent.config["sound"] 17 14 18 15 if `uname -s`.strip == "OpenBSD" 19 16 @player = [ "aucat", "-i" ] ··· 27 24 def log_pullup!(time) 28 25 cmd = @player + [ @parent.config["sound"] ] 29 26 30 - if @parent.config["debug"] 31 - puts "#{Time.now.to_f} - running " << cmd.inspect 32 - end 27 + @parent.dputs "running " << cmd.inspect 33 28 34 29 system(*cmd) 35 30 end
+5 -10
lib/logger/fitbit.rb
··· 11 11 12 12 def after_initialize 13 13 if @enabled = !!@parent.config["fitbit"] 14 - if @parent.config["verbose"] 15 - puts "#{Time.now.to_f} - enabling Fitbit logging module" 16 - end 14 + @parent.vputs "enabling Fitbit logging module" 17 15 18 16 verify_keys 19 17 end ··· 35 33 begin 36 34 h = JSON.parse(json) 37 35 if (id = h["activityLog"]["activityId"].to_i) != 0 38 - if @parent.config["verbose"] 39 - puts "#{Time.now.to_f} - logged pullup on fitbit (id #{id})" 40 - end 36 + @parent.vputs "logged pullup on fitbit (id #{id})" 41 37 else 42 38 raise "no activity id" 43 39 end 44 40 45 41 rescue => e 46 - puts "#{Time.now.to_f} - error from fitbit (#{e.message}): " << 47 - json.inspect 42 + @parent.eputs "error from fitbit (#{e.message}): " << json.inspect 48 43 end 49 44 end 50 45 ··· 75 70 return res.body 76 71 end 77 72 rescue Timeout::Error => e 78 - puts "#{Time.now.to_f} - timed out talking to Fitbit: #{e.message}" 73 + @parent.eputs "timed out talking to Fitbit: #{e.message}" 79 74 rescue StandardError => e 80 - puts "#{Time.now.to_f} - error talking to Fitbit: #{e.message}" 75 + @parent.eputs "error talking to Fitbit: #{e.message}" 81 76 end 82 77 end 83 78
+2 -6
lib/logger/textfile.rb
··· 8 8 9 9 def after_initialize 10 10 if @enabled = !!@parent.config["file"] 11 - if @parent.config["verbose"] 12 - puts "#{Time.now.to_f} - enabling plaintext logging module to " << 11 + @parent.vputs "enabling plaintext logging module to " << 13 12 @parent.config["file"] 14 - end 15 13 end 16 14 end 17 15 ··· 20 18 f.puts time.strftime("%Y-%m-%d %H:%M:%S") 21 19 end 22 20 23 - if @parent.config["verbose"] 24 - puts "#{Time.now.to_f} - logged to file #{@parent.config["file"]}" 25 - end 21 + @parent.vputs "logged to file " << @parent.config["file"] 26 22 end 27 23 end
+1 -1
lib/loggerish.rb
··· 14 14 end 15 15 16 16 def log_pullup!(time) 17 - puts "#{Time.now.to_f} - logged pullup" 17 + @parent.aputs "logged pullup" 18 18 end 19 19 end
+5 -8
lib/sensor/phidget.rb
··· 20 20 def main_loop 21 21 begin 22 22 Phidgets::InterfaceKit.new do |ifkit| 23 - if @parent.config["verbose"] 24 - puts "#{Time.now.to_f} - reading from Phidget #{ifkit.serial_number}" 25 - end 23 + @parent.vputs "reading from Phidget #{ifkit.serial_number}" 26 24 27 25 ifkit.on_sensor_change do |device, input, value, obj| 28 26 @cur_time = Time.now ··· 35 33 end 36 34 37 35 rescue => e 38 - puts "#{Time.now.to_f} - exception in Phidget handler: #{e.message}" 36 + @parent.eputs "exception in Phidget handler: #{e.message}" 39 37 sleep 3 40 38 retry 41 39 end ··· 43 41 44 42 def cur_value=(value) 45 43 if @parent.config["debug"] 46 - puts "#{@cur_time.to_f},#{value}" 44 + # XXX: direct this somewhere so it doesn't get muxed with chatter 45 + STDOUT.puts "#{@cur_time.to_f},#{value}" 47 46 end 48 47 49 48 if @cur_time.to_i - @last_state_change.to_i > 10 && self.state != :idle ··· 81 80 end 82 81 83 82 @state = state 84 - if @parent.config["verbose"] 85 - puts "#{@cur_time.to_f} - state is now #{state} (#{self.cur_value})" 86 - end 83 + @parent.vputs "state is now #{state} (#{self.cur_value})" 87 84 88 85 if state == :pulled_up 89 86 @parent.log_pullup!(@cur_time)
+21 -2
pullup_counter.rb
··· 76 76 begin 77 77 logger.log_pullup!(time) 78 78 rescue => e 79 - puts "#{Time.now.to_f} - error from #{logger.class} logger: " << 80 - e.message 79 + eputs "error from #{logger.class} logger: " << e.message 81 80 end 82 81 end 83 82 end 83 + end 84 + 85 + def aputs(str) 86 + STDOUT.puts Time.now.strftime("%Y-%m-%d %H:%M:%S.%L") << " - #{str}" 87 + end 88 + 89 + def dputs(str) 90 + if @config["debug"] 91 + STDOUT.puts Time.now.strftime("%Y-%m-%d %H:%M:%S.%L") << " - #{str}" 92 + end 93 + end 94 + 95 + def vputs(str) 96 + if @config["verbose"] || @config["debug"] 97 + STDOUT.puts Time.now.strftime("%Y-%m-%d %H:%M:%S.%L") << " - #{str}" 98 + end 99 + end 100 + 101 + def eputs(str) 102 + STDERR.puts Time.now.strftime("%Y-%m-%d %H:%M:%S.%L") << " - #{str}" 84 103 end 85 104 end 86 105