source: de.wigbels.ruby/hothotread/hothotpiper_power.rb@ 97e1817

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

Auf Eventmachine umgebaut

  • Property mode set to 100644
File size: 974 bytes
Line 
1require 'pi_piper'
2require 'eventmachine'
3
4include PiPiper
5
6
7mypath = File.expand_path(File.dirname(__FILE__))
8trigger_step = 1.0 / 96
9trigger_watch = 0
10counter = 0
11
12
13def last_rrd_count(concretepath)
14 val = 0.0
15 handle = IO.popen("rrdtool lastupdate #{concretepath}")
16 handle.each_line do |line|
17 m = line.match("^[0-9]*: ([0-9.]*) [0-9.]*")
18 if m
19 val = m[1].to_f
20 break
21 end
22 end
23 return val
24end
25
26watch :pin => 11, :trigger => :falling do
27 puts('updated power consumption')
28 trigger_watch = 1
29end
30
31
32rrdpath = mypath + "/power.rrd"
33counter = last_rrd_count(rrdpath)
34puts('Counter restored to: ' + counter.to_s)
35
36
37EventMachine.run do
38 EM.add_periodic_timer(1) do
39 if trigger_watch == 1 then
40 counter = counter + trigger_step
41 trigger_update = trigger_step * 3600000.0
42 system("rrdtool update #{rrdpath} N:#{counter}:#{trigger_update}")
43 puts('Counter updated to: ' + counter.to_s)
44 trigger_watch = 0
45 end
46 end
47end
Note: See TracBrowser for help on using the repository browser.