sample movement fix
[svn42.git] / sensor_graph / sample_sensor.lua
index 50b2031..31cdfc3 100755 (executable)
@@ -1,66 +1,28 @@
 #!/usr/bin/lua
 require('os')
 require('string')
-
-last_temp = 0.0
-last_light = 0
-last_movement = 0
-
-function save_values()
-  os.execute(string.format("rrdtool update /home/sensordata.rrd -t temp:light:movement N:%f:%d:%d", last_temp, last_light, last_movement))
-  last_movement=0
-end
-
+require('io')
 
 function parse_value(str)
-  if string.find(str,"Sensor T: Temp C:") then
-    last_temp = tonumber(string.sub(str,18))
+  last_temp = 0.0
+  last_light = 0
+  if string.find(str,"temp0: Temp C:") then
+    last_temp = tonumber(string.sub(str,15))
+    os.execute(string.format("rrdtool update /home/sensortemp.rrd -t temp N:%f", last_temp))
     --print(string.format("t: %f Grad Celsius",last_temp))
   end
-  if string.find(str,"Sensor P: Photo:") then
-    last_light = tonumber(string.sub(str,17))
+  if string.find(str,"photo0: Photo:") then
+    last_light = tonumber(string.sub(str,15))
+    os.execute(string.format("rrdtool update /home/sensorlight.rrd -t light N:%d", last_light))
     --print(string.format("p: %d",last_light))
   end
-  if string.find(str,"movement") then
-   last_movement=1
-   --print "something moved"
-  end
 end
 
-
-local socket = require("socket")
-
 while 1 do
-  local client = socket.connect("127.0.0.1",2010)
-  --socket.unix = require("socket.unix")
-  --local socket = assert(socket.unix())
-  --local client = assert(socket:connect("/var/run/power_sensor.socket"))
-  if client then
-    client:settimeout(30)
-    while 1 do
-      local line, err = client:receive()
-      if not err then 
-        parse_value(line) 
-      elseif err ~= "timeout" then
-        break
-      end
-      client:send("T")
-      line, err = client:receive()
-      if not err then 
-        parse_value(line)
-      elseif err ~= "timeout" then
-        break
-      end
-      client:send("P")
-      line, err = client:receive()
-      if not err then 
-        parse_value(line)
-      elseif err ~= "timeout" then
-        break
-      end
-      save_values()
+   local line = io.read("*line")
+    if line then 
+      parse_value(line) 
+    else
+      break
     end
-    client:shutdown("both")
-  end
-  socket.select(nil, nil, 10)
 end