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