changed temp/photo listener to sensor listener
[svn42.git] / sensor_graph / sample_sensor.lua
1 #!/usr/bin/lua
2 require('os')
3 require('string')
4
5 last_temp = 0.0
6 last_light = 0
7 last_movement = 0
8
9 function save_values()
10   os.execute(string.format("rrdtool update /home/sensordata.rrd -t temp:light:movement N:%f:%d:%d", last_temp, last_light, last_movement))
11   last_movement=0
12 end
13
14
15 function parse_value(str)
16   if string.find(str,"Temp C:") then
17     last_temp = tonumber(string.sub(str,11))
18     --print(string.format("t: %f Grad Celsius",last_temp))
19   end
20   if string.find(str,"Photo:") then
21     last_light = tonumber(string.sub(str,10))
22     --print(string.format("p: %d",last_light))
23   end
24   if string.find(str,"movement") then
25    last_movement=1
26    --print "something moved"
27   end
28 end
29
30
31 local socket = require("socket")
32
33 while 1 do
34   local client = socket.connect("127.0.0.1",2010)
35   --socket.unix = require("socket.unix")
36   --local socket = assert(socket.unix())
37   --local client = assert(socket:connect("/var/run/power_sensor.socket"))
38   if client then
39     client:settimeout(30)
40     while 1 do
41       local line, err = client:receive()
42       if not err then 
43         parse_value(line) 
44       elseif err ~= "timeout" then
45         break
46       end
47       client:send("T")
48       line, err = client:receive()
49       if not err then 
50         parse_value(line)
51       elseif err ~= "timeout" then
52         break
53       end
54       client:send("P")
55       line, err = client:receive()
56       if not err then 
57         parse_value(line)
58       elseif err ~= "timeout" then
59         break
60       end
61       save_values()
62     end
63     client:shutdown("both")
64   end
65   socket.select(nil, nil, 10)
66 end