2009-06-20 12:57:22 +00:00
|
|
|
#!/usr/bin/env lua
|
|
|
|
|
|
|
|
--
|
|
|
|
-- flukso.lua: flukso deamon running on openwrt
|
|
|
|
-- Copyright (c) 2008-2009 jokamajo.org
|
2010-04-19 21:34:07 +00:00
|
|
|
-- 2010 flukso.net
|
2009-06-20 12:57:22 +00:00
|
|
|
--
|
|
|
|
-- This program is free software; you can redistribute it and/or
|
|
|
|
-- modify it under the terms of the GNU General Public License
|
|
|
|
-- as published by the Free Software Foundation; either version 2
|
|
|
|
-- of the License, or (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU General Public License
|
|
|
|
-- along with this program; if not, write to the Free Software
|
|
|
|
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
--
|
2009-06-22 12:54:51 +00:00
|
|
|
-- $Id$
|
2009-06-20 12:57:22 +00:00
|
|
|
--
|
|
|
|
|
|
|
|
require 'posix'
|
|
|
|
require 'xmlrpc.http'
|
|
|
|
|
|
|
|
data = require 'flukso.data'
|
|
|
|
auth = require 'flukso.auth'
|
|
|
|
dbg = require 'flukso.dbg'
|
|
|
|
|
|
|
|
local param = {xmlrpcaddress = 'http://logger.flukso.net/xmlrpc',
|
|
|
|
xmlrpcversion = '1',
|
|
|
|
xmlrpcmethod = 'logger.measurementAdd',
|
2010-05-22 09:24:24 +00:00
|
|
|
pwrenable = true,
|
2010-04-20 22:07:47 +00:00
|
|
|
pwrinterval = 0,
|
2010-04-20 13:38:53 +00:00
|
|
|
pwrdir = '/tmp/sensor',
|
2009-06-20 12:57:22 +00:00
|
|
|
device = '/dev/ttyS0',
|
2010-05-22 09:24:24 +00:00
|
|
|
interval = 300,
|
|
|
|
dbgenable = false}
|
2009-06-20 12:57:22 +00:00
|
|
|
|
2010-04-22 14:08:39 +00:00
|
|
|
function dispatch(e_child, p_child, device, pwrenable)
|
2010-04-19 21:34:07 +00:00
|
|
|
return coroutine.create(function()
|
2010-09-04 22:42:33 +00:00
|
|
|
|
|
|
|
local function flash() -- flash the power led for 50ms
|
|
|
|
os.execute('gpioctl clear 4 > /dev/null')
|
|
|
|
socket.select(nil, nil, 0.05)
|
|
|
|
os.execute('gpioctl set 4 > /dev/null')
|
|
|
|
end
|
|
|
|
|
2009-06-20 12:57:22 +00:00
|
|
|
-- open the connection to the syslog deamon, specifying our identity
|
|
|
|
posix.openlog('flukso')
|
|
|
|
posix.syslog(30, 'starting the flukso deamon')
|
|
|
|
posix.syslog(30, 'listening for pulses on '..device..'...')
|
|
|
|
|
2010-09-04 22:42:33 +00:00
|
|
|
pattern = '^(%l+)%s(%x+):(%d+):?(%d*)$'
|
|
|
|
|
2009-06-20 12:57:22 +00:00
|
|
|
for line in io.lines(device) do
|
2010-09-04 22:42:33 +00:00
|
|
|
command, meter, value, millis = line:match(pattern)
|
|
|
|
length = line:len()
|
|
|
|
|
|
|
|
if command == 'pls' and (length == 47 or length == 58) then -- user data
|
|
|
|
flash()
|
|
|
|
posix.syslog(30, 'received pulse from ' .. device .. ': ' .. line:sub(5))
|
2009-06-20 12:57:22 +00:00
|
|
|
|
2010-09-04 22:42:33 +00:00
|
|
|
coroutine.resume(e_child, meter, os.time(), tonumber(value))
|
2009-06-20 12:57:22 +00:00
|
|
|
|
2010-09-04 22:42:33 +00:00
|
|
|
-- pls includes a msec timestamp so report to p_child as well
|
|
|
|
if length == 58 then
|
|
|
|
coroutine.resume(p_child, meter, os.time(), nil, tonumber(millis))
|
|
|
|
end
|
2010-04-20 13:38:53 +00:00
|
|
|
|
2010-09-04 22:42:33 +00:00
|
|
|
elseif command == 'pwr' and length == 47 then -- user data
|
2010-04-20 13:38:53 +00:00
|
|
|
if pwrenable then coroutine.resume(p_child, meter, os.time(), value) end
|
|
|
|
|
2010-09-04 22:42:33 +00:00
|
|
|
elseif command == 'msg' then -- control data
|
|
|
|
posix.syslog(31, 'received message from ' .. device .. ': ' .. line:sub(5))
|
2010-04-20 13:38:53 +00:00
|
|
|
|
2010-09-04 22:42:33 +00:00
|
|
|
else -- error
|
|
|
|
posix.syslog(27, 'input error on ' .. device .. ': ' .. line)
|
2009-06-20 12:57:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
posix.syslog(30, 'closing down the flukso deamon')
|
2010-09-04 22:42:33 +00:00
|
|
|
os.exit(1)
|
2009-06-20 12:57:22 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-09-04 22:42:33 +00:00
|
|
|
-- TODO: adapt for millis input
|
2010-04-19 21:34:07 +00:00
|
|
|
function buffer(child, interval)
|
2010-09-04 22:42:33 +00:00
|
|
|
return coroutine.create(function(meter, timestamp, value, millis)
|
2009-06-20 12:57:22 +00:00
|
|
|
local measurements = data.new()
|
2010-04-22 14:08:39 +00:00
|
|
|
local threshold = timestamp + interval
|
2010-04-22 14:35:46 +00:00
|
|
|
local timestamp_prev = {}
|
2009-06-20 12:57:22 +00:00
|
|
|
|
|
|
|
while true do
|
2010-04-22 14:35:46 +00:00
|
|
|
if meter ~= nil and timestamp > math.max(1234567890, timestamp_prev[meter] or 0) then
|
|
|
|
measurements:add(meter, timestamp, value)
|
|
|
|
end
|
2009-06-20 12:57:22 +00:00
|
|
|
if timestamp > threshold and next(measurements) then --checking whether table is not empty
|
2010-04-19 21:34:07 +00:00
|
|
|
coroutine.resume(child, measurements)
|
2009-06-20 12:57:22 +00:00
|
|
|
threshold = timestamp + interval
|
2010-04-22 14:35:46 +00:00
|
|
|
timestamp_prev[meter] = timestamp
|
2009-06-20 12:57:22 +00:00
|
|
|
end
|
2010-04-19 21:34:07 +00:00
|
|
|
meter, timestamp, value = coroutine.yield()
|
2009-06-20 12:57:22 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-04-19 21:34:07 +00:00
|
|
|
function filter(child, span, offset)
|
|
|
|
return coroutine.create(function(measurements)
|
2009-06-20 12:57:22 +00:00
|
|
|
while true do
|
|
|
|
measurements:filter(span, offset)
|
2010-04-19 21:34:07 +00:00
|
|
|
coroutine.resume(child, measurements)
|
|
|
|
measurements = coroutine.yield()
|
2009-06-20 12:57:22 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-04-19 21:34:07 +00:00
|
|
|
function send(child, address, version, method)
|
|
|
|
return coroutine.create(function(measurements)
|
2009-06-20 12:57:22 +00:00
|
|
|
while true do
|
|
|
|
local auth = auth.new()
|
|
|
|
auth:load()
|
|
|
|
auth:hmac(measurements)
|
|
|
|
|
|
|
|
local status, ret_or_err, res = pcall(xmlrpc.http.call,
|
|
|
|
address..'/'..version,
|
|
|
|
method,
|
|
|
|
auth,
|
|
|
|
measurements)
|
|
|
|
|
|
|
|
if status then
|
|
|
|
posix.syslog(30, tostring(res))
|
|
|
|
if ret_or_err then --successful xmlrpc call
|
|
|
|
measurements:clear()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
posix.syslog(27, tostring(ret_or_err)..' '..address..' '..tostring(res))
|
|
|
|
end
|
2010-04-19 21:34:07 +00:00
|
|
|
coroutine.resume(child, measurements)
|
|
|
|
measurements = coroutine.yield()
|
2009-06-20 12:57:22 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-04-19 21:34:07 +00:00
|
|
|
function gc(child)
|
|
|
|
return coroutine.create(function(measurements)
|
2009-06-20 12:57:22 +00:00
|
|
|
while true do
|
|
|
|
posix.syslog(31, tostring(collectgarbage('count')*1024)..' bytes of memory used by Lua before garbage collection cycle')
|
|
|
|
collectgarbage() -- force a complete garbage collection cycle
|
|
|
|
posix.syslog(31, tostring(collectgarbage('count')*1024)..' bytes of memory used by Lua after garbage collection cycle')
|
2010-04-19 21:34:07 +00:00
|
|
|
coroutine.resume(child, measurements)
|
|
|
|
measurements = coroutine.yield()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-04-20 13:38:53 +00:00
|
|
|
function polish(child, cutoff)
|
|
|
|
return coroutine.create(function(measurements)
|
|
|
|
while true do
|
|
|
|
measurements:fill()
|
|
|
|
measurements:truncate(cutoff)
|
|
|
|
coroutine.resume(child, measurements)
|
|
|
|
measurements = coroutine.yield()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function publish(child, dir)
|
|
|
|
return coroutine.create(function(measurements)
|
|
|
|
os.execute('mkdir -p ' .. dir .. ' > /dev/null')
|
|
|
|
while true do
|
|
|
|
local measurements_json = measurements:json_encode()
|
2010-04-20 22:07:47 +00:00
|
|
|
for meter, json in pairs(measurements_json) do
|
2010-04-20 13:38:53 +00:00
|
|
|
io.output(dir .. '/' .. meter)
|
|
|
|
io.write(json)
|
|
|
|
io.close()
|
|
|
|
end
|
|
|
|
coroutine.resume(child, measurements)
|
|
|
|
measurements = coroutine.yield()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-05-22 09:24:24 +00:00
|
|
|
function debug(child, dbgenable)
|
2010-04-19 21:34:07 +00:00
|
|
|
return coroutine.create(function(measurements)
|
|
|
|
while true do
|
2010-05-22 09:24:24 +00:00
|
|
|
if dbgenable then dbg.vardump(measurements) end
|
2010-04-20 13:38:53 +00:00
|
|
|
if child then coroutine.resume(child, measurements) end
|
2010-04-19 21:34:07 +00:00
|
|
|
measurements = coroutine.yield()
|
2009-06-20 12:57:22 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-04-20 13:38:53 +00:00
|
|
|
-- dispatch: listen to the serial port for incoming pulses
|
2009-06-20 12:57:22 +00:00
|
|
|
-- buffer: buffer the pulses in a measurement object
|
|
|
|
-- filter: sweep recursively to filter all redundant entries
|
|
|
|
-- send: report the measurements to the server via xmlrpc
|
|
|
|
-- gc: perform a full garbage collection cycle
|
2010-04-19 21:34:07 +00:00
|
|
|
-- debug: dump measurements table to stdout
|
|
|
|
|
2010-04-20 13:38:53 +00:00
|
|
|
local e_chain = buffer(
|
2010-04-19 21:34:07 +00:00
|
|
|
filter(
|
|
|
|
filter(
|
|
|
|
filter(
|
|
|
|
send(
|
|
|
|
gc(
|
2010-05-22 09:24:24 +00:00
|
|
|
debug(nil, param.dbgenable)
|
2010-04-19 21:34:07 +00:00
|
|
|
)
|
|
|
|
, param.xmlrpcaddress, param.xmlrpcversion, param.xmlrpcmethod)
|
|
|
|
, 86400, 172800)
|
|
|
|
, 900, 7200)
|
|
|
|
, 60, 0)
|
|
|
|
, param.interval)
|
2010-04-20 13:38:53 +00:00
|
|
|
|
|
|
|
local p_chain = buffer(
|
|
|
|
polish(
|
|
|
|
publish(
|
2010-05-22 09:24:24 +00:00
|
|
|
debug(nil, param.dbgenable)
|
2010-04-20 13:38:53 +00:00
|
|
|
, param.pwrdir)
|
|
|
|
, 60)
|
|
|
|
, param.pwrinterval)
|
|
|
|
|
2010-04-22 14:08:39 +00:00
|
|
|
local chain = dispatch(e_chain, p_chain, param.device, param.pwrenable)
|
2010-04-19 21:34:07 +00:00
|
|
|
|
|
|
|
coroutine.resume(chain)
|