From 3b1d42bd8a6816e8e0aa9345f62ecb3c1b1c864a Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Sat, 20 Mar 2010 09:28:57 +0000 Subject: [PATCH] Sensor Sampling Revived --- sensor_graph/sample_sensor.lua | 55 ++++++++++----------------------------- sensor_graph/sample_sensor3.lua | 35 ------------------------- sensor_graph/sample_sensors.sh | 8 +++++- sensor_graph/switch.cgi | 53 +++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 77 deletions(-) delete mode 100755 sensor_graph/sample_sensor3.lua create mode 100755 sensor_graph/switch.cgi diff --git a/sensor_graph/sample_sensor.lua b/sensor_graph/sample_sensor.lua index 8f5f193..31cdfc3 100755 --- a/sensor_graph/sample_sensor.lua +++ b/sensor_graph/sample_sensor.lua @@ -1,55 +1,28 @@ #!/usr/bin/lua require('os') require('string') -require('socket') - -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) 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,"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)) + 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,"movement") then - --last_movement=1 - os.execute(string.format("rrdtool update /home/sensordata.rrd -t movement N:%d", 1)) + 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 end - -local socket_factory = require("socket.unix"); -local socket = socket_factory() - - while 1 do - 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 + local line = io.read("*line") + if line then + parse_value(line) + else + break end - client:shutdown("both") - end - --wait 10 seconds - socket.select(nil, nil, 10) end diff --git a/sensor_graph/sample_sensor3.lua b/sensor_graph/sample_sensor3.lua deleted file mode 100755 index f2e469a..0000000 --- a/sensor_graph/sample_sensor3.lua +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/lua -require('os') -require('string') -require('io') ---require('socket') - -function parse_value(str) - 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,"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 - os.execute(string.format("rrdtool update /home/sensormovement.rrd -t movement N:%d", 1)) - end -end - - - -while 1 do - local line = io.read("*line") - if line then - parse_value(line) - else - break - end -end diff --git a/sensor_graph/sample_sensors.sh b/sensor_graph/sample_sensors.sh index 9ed260c..bc666fb 100755 --- a/sensor_graph/sample_sensors.sh +++ b/sensor_graph/sample_sensors.sh @@ -1,2 +1,8 @@ #!/bin/sh -echo -e "listen sensor\nlisten movement\n" | usocket /var/run/powersensordaemon/cmd.sock -n | ./sample_sensor3.lua &>/dev/null & +echo -e "listen sensor\n" | usocket /var/run/powersensordaemon/cmd.sock -n | ./sample_sensor.lua &>/dev/null & +echo -e "listen movement\n" | usocket /var/run/powersensordaemon/cmd.sock -n > /tmp/movement.tmp & +while sleep 30; do + L=$(wc -l /tmp/movement.tmp | cut -d' ' -f1) + echo -n > /tmp/movement.tmp + rrdtool update /home/sensormovement.rrd -t movement N:$L +done diff --git a/sensor_graph/switch.cgi b/sensor_graph/switch.cgi new file mode 100755 index 0000000..1e70cf0 --- /dev/null +++ b/sensor_graph/switch.cgi @@ -0,0 +1,53 @@ +#!/bin/sh + +for QUERY in `echo $QUERY_STRING | tr '&' ' '`; do + for VALUE in `echo $QUERY | tr '=' ' '`; do + if [ "$VALUE" == "id" ]; then + ID='?' + elif [ "$ID" == "?" ]; then + ID=$VALUE + elif [ "$VALUE" == "power" ]; then + POWER='?' + elif [ "$POWER" == "?" ]; then + POWER=$VALUE + fi + i=$i+1 + done +done + +UNIXSOCK=/var/run/powersensordaemon/cmd.sock +VALIDIDS="werkzeug stereo labor schreibtisch logo idee" + +if [ "$POWER" == "on" -o "$POWER" == "off" ]; then + for CHECKID in $VALIDIDS; do + if [ "$CHECKID" == "$ID" ]; then + echo "power $POWER $ID" | usocket $UNIXSOCK + fi + done +fi + +DESC_werkzeug="Beleuchtung Werkzeug" +DESC_stereo="Stereo Anlage" +DESC_labor="Labor Licht" +DESC_schreibtisch="Schreibtisch Licht" +DESC_logo="Logo" +DESC_idee="Idee" + +echo "Content-type: text/html" +echo "" +echo "" +echo "" +echo "Realraum rf433ctl" +echo "" +echo "" +echo "

Realraum rf433ctl

" +echo "" +echo " " +for DISPID in $VALIDIDS; do + NAME="$(eval echo \$DESC_$DISPID)" + [ -z "$NAME" ] && NAME=$DISPID + echo " " +done +echo "
DeviceONOFF
$NAMEONOFF
" +echo "" +echo "" -- 1.7.10.4