X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=sensor_graph%2Fsample_sensor.lua;h=8f5f19316c3081d0654d9f1e9b5350d588a9a857;hb=51b8c0cb530de874de351d6a7d954c21a7644a3e;hp=9ff02a2e18729f92f49e0ba116b75dadb754ba25;hpb=fd194c6530c2d76abae71c0db2b6193018c54450;p=svn42.git diff --git a/sensor_graph/sample_sensor.lua b/sensor_graph/sample_sensor.lua old mode 100644 new mode 100755 index 9ff02a2..8f5f193 --- a/sensor_graph/sample_sensor.lua +++ b/sensor_graph/sample_sensor.lua @@ -1,8 +1,8 @@ +#!/usr/bin/lua require('os') require('string') +require('socket') -last_temp = 0.0 -last_light = 0 last_movement = 0 function save_values() @@ -12,48 +12,44 @@ end function parse_value(str) - if string.find(str,"Temp C:") then - last_temp = tonumber(string.sub(str,8)) - --print(string.format("t: %f Grad Celsius",last_temp)) + last_temp = 0.0 + last_light = 0 + if string.find(str,"Sensor T: Temp C:") then + last_temp = tonumber(string.sub(str,18)) + 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,"Photo:") then - last_light = tonumber(string.sub(str,7)) - --print(string.format("p: %d",last_light)) + if string.find(str,"Sensor P: Photo:") then + last_light = tonumber(string.sub(str,17)) + 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 client = assert(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")) -client:settimeout(30) - - +local socket_factory = require("socket.unix"); +local socket = socket_factory() while 1 do - local line, err = client:receive() - if not err then - parse_value(line) - end - client:send("T") - line, err = client:receive() - if not err then - parse_value(line) - end - client:send("P") - line, err = client:receive() - if not err then - parse_value(line) + local client = socket.connect("/var/run/powersensordaemon/cmd.sock") + if client then + client:send("listen sensor\n") + --client:settimeout(30) + while 1 do + local line, err = client:receive() + if not err then + parse_value(line) + elseif err ~= "timeout" then + break + end + end + client:shutdown("both") end - save_values() + --wait 10 seconds + socket.select(nil, nil, 10) end