#!/usr/bin/env ruby

require 'pi_piper'
include PiPiper

mypath = File.expand_path(File.dirname(__FILE__))
trigger_step = 0.01
trigger_watch = 0
counter = 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 => :falling do 
  puts('added 10 liter of gas to sensor-database')
  system("rrdtool update #{mypath}/gas.rrd N:10")
end

rrdpath = mypath + "/gasneu.rrd"
counter = last_rrd_count(rrdpath)
#loop do
# if trigger_watch == 1 then
#  puts('trigger 1 detected')
#  trigger_watch = 0
# else
#  puts('trigger 0 detected')
# end
# sleep(1)
#end
PiPiper.wait
