source: de.wigbels.ruby/hothotread/hothotpiper_gas.rb@ dc12dfc

Last change on this file since dc12dfc was 1801b32, checked in by Norbert Wigbels <njw@…>, 9 years ago

Auf Eventmachine umgebaut

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[8cc14b9]1#!/usr/bin/env ruby
2require 'pi_piper'
[1801b32]3require 'eventmachine'
4
[8cc14b9]5include PiPiper
6
[1801b32]7
[8cc14b9]8mypath = File.expand_path(File.dirname(__FILE__))
9trigger_step = 0.01
10trigger_watch = 0
11counter = 0.0
12timestamp = Time.now.to_i
13
[1801b32]14
[8cc14b9]15def last_rrd_count(concretepath)
16 val = 0.0
17 handle = IO.popen("rrdtool lastupdate #{concretepath}")
18 handle.each_line do |line|
19 m = line.match("^[0-9]*: ([0-9.]*) [0-9.]*")
20 if m
21 val = m[1].to_f
22 break
23 end
24 end
25 return val
26end
27
28watch :pin => 4, :trigger => :falling do
29 puts('triggered watch to 1')
30 trigger_watch = 1
31end
32
[1801b32]33
[8cc14b9]34rrdpath = mypath + "/gasneu.rrd"
35counter = last_rrd_count(rrdpath)
[1801b32]36puts('Counter restored to: ' + counter.to_s)
37
38
39EventMachine.run do
40 EM.add_periodic_timer(1) do
41 if trigger_watch == 1 then
42 counter = counter + trigger_step
43 system("rrdtool update #{rrdpath} N:#{counter}:#{trigger_step}")
44 puts('Counter updated to: ' + counter.to_s)
45 timestamp = Time.now.to_i
46 trigger_watch = 0
47 elsif Time.now.to_i - timestamp > 3600
48 system("rrdtool update #{rrdpath} N:#{counter}:0")
49 timestamp = Time.now.to_i
50 end
[8cc14b9]51 end
52end
53
Note: See TracBrowser for help on using the repository browser.