From: Bernhard Tittelbach Date: Tue, 16 Mar 2010 13:01:17 +0000 (+0000) Subject: untested unix socket lua sample sensor X-Git-Url: https://git.realraum.at/?p=svn42.git;a=commitdiff_plain;h=9fb50288fde0eb8919287a49e015a5f21645dd81 untested unix socket lua sample sensor --- diff --git a/sensor_graph/sample_sensor.lua b/sensor_graph/sample_sensor.lua index 50b2031..8f5f193 100755 --- a/sensor_graph/sample_sensor.lua +++ b/sensor_graph/sample_sensor.lua @@ -1,9 +1,8 @@ #!/usr/bin/lua require('os') require('string') +require('socket') -last_temp = 0.0 -last_light = 0 last_movement = 0 function save_values() @@ -13,30 +12,34 @@ end function parse_value(str) + last_temp = 0.0 + last_light = 0 if string.find(str,"Sensor T: Temp C:") then last_temp = tonumber(string.sub(str,18)) - --print(string.format("t: %f Grad Celsius",last_temp)) + os.execute(string.format("rrdtool update /home/sensordata.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)) - --print(string.format("p: %d",last_light)) + os.execute(string.format("rrdtool update /home/sensordata.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" + --last_movement=1 + os.execute(string.format("rrdtool update /home/sensordata.rrd -t movement N:%d", 1)) end end -local socket = require("socket") +local socket_factory = require("socket.unix"); +local socket = socket_factory() + 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")) + local client = socket.connect("/var/run/powersensordaemon/cmd.sock") if client then - client:settimeout(30) + client:send("listen sensor\n") + --client:settimeout(30) while 1 do local line, err = client:receive() if not err then @@ -44,23 +47,9 @@ while 1 do 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() end client:shutdown("both") end + --wait 10 seconds socket.select(nil, nil, 10) end