53 lines
1.2 KiB
Ruby
53 lines
1.2 KiB
Ruby
#!/usr/bin/env ruby
|
|
require 'pi_piper'
|
|
require 'eventmachine'
|
|
|
|
include PiPiper
|
|
|
|
|
|
mypath = File.expand_path(File.dirname(__FILE__))
|
|
trigger_step = 0.01
|
|
trigger_watch = 0
|
|
counter = 0.0
|
|
timestamp = Time.now.to_i
|
|
|
|
|
|
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 => 4, :trigger => :rising, :pull => :up do
|
|
puts('triggered watch to 1')
|
|
trigger_watch = 1
|
|
end
|
|
|
|
|
|
rrdpath = mypath + "/gasneu.rrd"
|
|
counter = last_rrd_count(rrdpath)
|
|
puts('Counter restored to: ' + counter.to_s)
|
|
#PiPiper::Pin.new(:pin => 4, :direction => :in)
|
|
|
|
EventMachine.run do
|
|
EM.add_periodic_timer(1) do
|
|
if trigger_watch == 1 then
|
|
counter = counter + trigger_step
|
|
system("rrdtool update #{rrdpath} N:#{counter}:#{trigger_step}")
|
|
puts('Counter updated to: ' + counter.to_s)
|
|
timestamp = Time.now.to_i
|
|
trigger_watch = 0
|
|
elsif Time.now.to_i - timestamp > 3600
|
|
system("rrdtool update #{rrdpath} N:#{counter}:0")
|
|
timestamp = Time.now.to_i
|
|
end
|
|
end
|
|
end
|
|
|