require 'pi_piper' require 'eventmachine' include PiPiper mypath = File.expand_path(File.dirname(__FILE__)) trigger_step = 1.0 / 96 trigger_watch = 0 counter = 0 def last_rrd_count(concretepath) val = 0.0 handle = IO.popen("rrdtool lastupdate #{concretepath}") handle.each_line do |line| m = line.match("^[0-9]*: ([0-9.]*) [0-9.]*") if m val = m[1].to_f break end end return val end watch :pin => 11, :trigger => :falling do puts('updated power consumption') trigger_watch = 1 end rrdpath = mypath + "/power.rrd" counter = last_rrd_count(rrdpath) puts('Counter restored to: ' + counter.to_s) EventMachine.run do EM.add_periodic_timer(1) do if trigger_watch == 1 then counter = counter + trigger_step trigger_update = trigger_step * 3600000.0 system("rrdtool update #{rrdpath} N:#{counter}:#{trigger_update}") puts('Counter updated to: ' + counter.to_s) trigger_watch = 0 end end end