openwrt: add basic support for broadcasting pwr messages

This commit is contained in:
Bart Van Der Meerssche 2010-03-01 13:15:52 +00:00
parent 7b8c322628
commit 078652c277
1 changed files with 14 additions and 4 deletions

View File

@ -31,27 +31,37 @@ dbg = require 'flukso.dbg'
local param = {xmlrpcaddress = 'http://logger.flukso.net/xmlrpc', local param = {xmlrpcaddress = 'http://logger.flukso.net/xmlrpc',
xmlrpcversion = '1', xmlrpcversion = '1',
xmlrpcmethod = 'logger.measurementAdd', xmlrpcmethod = 'logger.measurementAdd',
pwraddress = '255.255.255.255',
pwrport = 26488,
pwrenable = false,
device = '/dev/ttyS0', device = '/dev/ttyS0',
interval = 300} interval = 300}
function receive(device) function receive(device, pwraddress, pwrport, pwrenable)
return coroutine.wrap(function() return coroutine.wrap(function()
-- open the connection to the syslog deamon, specifying our identity -- open the connection to the syslog deamon, specifying our identity
posix.openlog('flukso') posix.openlog('flukso')
posix.syslog(30, 'starting the flukso deamon') posix.syslog(30, 'starting the flukso deamon')
posix.syslog(30, 'listening for pulses on '..device..'...') posix.syslog(30, 'listening for pulses on '..device..'...')
-- open a UDP socket for transmitting the pwr messages
local udp = assert(socket.udp())
udp:setoption('broadcast', true)
assert(udp:setpeername(pwraddress, pwrport))
for line in io.lines(device) do for line in io.lines(device) do
if line:sub(1, 3) == 'pls' and line:len() == 47 and line:find(':') == 37 then -- user data + additional data integrity checks if line:sub(1, 3) == 'pls' and line:len() == 47 and line:find(':') == 37 then -- user data + additional data integrity checks
posix.syslog(30, 'received pulse from '..device..': '..line:sub(5)) posix.syslog(30, 'received pulse from '..device..': '..line:sub(5))
-- flash the power led for 50ms -- flash the power led for 50ms
os.execute('gpioctl clear 4') os.execute('gpioctl clear 4 > /dev/null')
socket.select(nil, nil, 0.05) socket.select(nil, nil, 0.05)
os.execute('gpioctl set 4') os.execute('gpioctl set 4 > /dev/null')
local meter, value = line:sub(5, 36), tonumber(line:sub(38)) local meter, value = line:sub(5, 36), tonumber(line:sub(38))
coroutine.yield(meter, os.time(), value) coroutine.yield(meter, os.time(), value)
elseif line:sub(1, 3) == 'pwr' and line:len() == 47 and line:find(':') == 37 and pwrenable then -- user data + additional data integrity checks
udp:send(line)
elseif line:sub(1, 3) == 'msg' then -- control data elseif line:sub(1, 3) == 'msg' then -- control data
posix.syslog(31, 'received message from '..device..': '..line:sub(5)) posix.syslog(31, 'received message from '..device..': '..line:sub(5))
else else
@ -141,7 +151,7 @@ local aggregator = gc(
filter( filter(
filter( filter(
buffer( buffer(
receive(param.device) receive(param.device, param.pwraddress, param.pwrport, param.pwrenable)
, param.interval) , param.interval)
, 60, 0) , 60, 0)
, 900, 7200) , 900, 7200)