Last change
on this file since 1801b32 was
1801b32,
checked in by Norbert Wigbels <njw@…>, 8 years ago
|
Auf Eventmachine umgebaut
|
-
Property mode set to
100644
|
File size:
974 bytes
|
Rev | Line | |
---|
[8cc14b9] | 1 | require 'pi_piper' |
---|
[1801b32] | 2 | require 'eventmachine' |
---|
| 3 | |
---|
[8cc14b9] | 4 | include PiPiper |
---|
| 5 | |
---|
[1801b32] | 6 | |
---|
[8cc14b9] | 7 | mypath = File.expand_path(File.dirname(__FILE__)) |
---|
| 8 | trigger_step = 1.0 / 96 |
---|
[1801b32] | 9 | trigger_watch = 0 |
---|
[8cc14b9] | 10 | counter = 0 |
---|
| 11 | |
---|
[1801b32] | 12 | |
---|
[8cc14b9] | 13 | def 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 |
---|
| 24 | end |
---|
| 25 | |
---|
| 26 | watch :pin => 11, :trigger => :falling do |
---|
| 27 | puts('updated power consumption') |
---|
[1801b32] | 28 | trigger_watch = 1 |
---|
[8cc14b9] | 29 | end |
---|
| 30 | |
---|
[1801b32] | 31 | |
---|
[8cc14b9] | 32 | rrdpath = mypath + "/power.rrd" |
---|
| 33 | counter = last_rrd_count(rrdpath) |
---|
[1801b32] | 34 | puts('Counter restored to: ' + counter.to_s) |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | EventMachine.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 |
---|
| 47 | end |
---|
Note: See
TracBrowser
for help on using the repository browser.