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'
|
|
|
|
|
2010-09-30 12:34:39 +00:00
|
|
|
local data = require 'flukso.data'
|
|
|
|
local auth = require 'flukso.auth'
|
|
|
|
local dbg = require 'flukso.dbg'
|
|
|
|
|
|
|
|
local uci = require 'luci.model.uci'.cursor()
|
|
|
|
local param = uci:get_all('flukso', 'main')
|
|
|
|
|
|
|
|
|
|
|
|
function dispatch(e_child, p_child, port, homeEnable, localEnable)
|
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')
|
2010-09-30 12:34:39 +00:00
|
|
|
posix.syslog(30, 'listening for pulses on ' .. port .. '...')
|
2009-06-20 12:57:22 +00:00
|
|
|
|
2010-09-05 14:06:57 +00:00
|
|
|
local pattern = '^(%l+)%s(%x+):(%d+):?(%d*)$'
|
2010-09-04 22:42:33 +00:00
|
|
|
|
2010-09-30 12:34:39 +00:00
|
|
|
for line in io.lines(port) do
|
2010-09-05 14:06:57 +00:00
|
|
|
local command, meter, value, msec = line:match(pattern)
|
|
|
|
value = tonumber(value or '0')
|
|
|
|
msec = tonumber(msec or '0')
|
|
|
|
local length = line:len()
|
2010-09-04 22:42:33 +00:00
|
|
|
|
|
|
|
if command == 'pls' and (length == 47 or length == 58) then -- user data
|
|
|
|
flash()
|
2010-09-30 12:34:39 +00:00
|
|
|
posix.syslog(30, 'received pulse from ' .. port .. ': ' .. line:sub(5))
|
2009-06-20 12:57:22 +00:00
|
|
|
|
2010-09-30 12:34:39 +00:00
|
|
|
if homeEnable == 1 then coroutine.resume(e_child, meter, os.time(), value) end
|
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
|
2010-09-30 12:34:39 +00:00
|
|
|
if length == 58 and localEnable == 1 then
|
2010-09-05 14:06:57 +00:00
|
|
|
coroutine.resume(p_child, meter, os.time(), value, msec)
|
2010-09-04 22:42:33 +00:00
|
|
|
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-09-30 12:34:39 +00:00
|
|
|
if localEnable == 1 then coroutine.resume(p_child, meter, os.time(), value) end
|
2010-04-20 13:38:53 +00:00
|
|
|
|
2010-09-04 22:42:33 +00:00
|
|
|
elseif command == 'msg' then -- control data
|
2010-09-30 12:34:39 +00:00
|
|
|
posix.syslog(31, 'received message from ' .. port .. ': ' .. line:sub(5))
|
2010-04-20 13:38:53 +00:00
|
|
|
|
2010-09-04 22:42:33 +00:00
|
|
|
else -- error
|
2010-09-30 12:34:39 +00:00
|
|
|
posix.syslog(27, 'input error on ' .. port .. ': ' .. 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-04-19 21:34:07 +00:00
|
|
|
function buffer(child, interval)
|
2010-09-05 14:06:57 +00:00
|
|
|
return coroutine.create(function(meter, timestamp, value, msec)
|
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-09-09 17:36:49 +00:00
|
|
|
local prev = {}
|
2009-06-20 12:57:22 +00:00
|
|
|
|
2010-09-09 17:36:49 +00:00
|
|
|
local function diff(x, y) -- calculates y - x
|
|
|
|
if y >= x then
|
|
|
|
return y - x
|
|
|
|
else -- y wrapped around 32-bit boundary
|
2010-09-10 10:01:56 +00:00
|
|
|
return 4294967296 - x + y
|
2010-09-09 17:36:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-20 12:57:22 +00:00
|
|
|
while true do
|
2010-09-09 17:36:49 +00:00
|
|
|
if meter ~= nil then
|
|
|
|
if not prev[meter] then
|
|
|
|
prev[meter] = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
if msec then -- we're dealing with a pls xxx:yyy:zzz message so calculate power
|
2010-09-09 20:04:32 +00:00
|
|
|
if prev[meter].msec then
|
|
|
|
local power = math.floor(diff(prev[meter].value, value) / diff(prev[meter].msec, msec) * 3.6 * 10^6 + 0.5)
|
|
|
|
prev[meter].value = value
|
2010-09-09 17:36:49 +00:00
|
|
|
value = power
|
|
|
|
else
|
2010-09-09 20:04:32 +00:00
|
|
|
prev[meter].value = value
|
2010-09-09 17:36:49 +00:00
|
|
|
value = nil
|
|
|
|
end
|
2010-09-09 20:04:32 +00:00
|
|
|
prev[meter].msec = msec
|
2010-09-09 17:36:49 +00:00
|
|
|
end
|
|
|
|
|
2010-09-09 20:04:32 +00:00
|
|
|
if timestamp > math.max(1234567890, prev[meter].timestamp or 0) and value then
|
2010-09-09 17:36:49 +00:00
|
|
|
measurements:add(meter, timestamp, value)
|
|
|
|
end
|
2010-04-22 14:35:46 +00:00
|
|
|
end
|
2010-09-09 17:36:49 +00:00
|
|
|
|
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-09-09 20:04:32 +00:00
|
|
|
prev[meter].timestamp = timestamp
|
2009-06-20 12:57:22 +00:00
|
|
|
end
|
2010-09-09 17:36:49 +00:00
|
|
|
meter, timestamp, value, msec = 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-09-30 12:34:39 +00:00
|
|
|
function send(child, home, version, method)
|
2010-04-19 21:34:07 +00:00
|
|
|
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,
|
2010-09-30 12:34:39 +00:00
|
|
|
'http://' .. home .. '/' .. version,
|
2009-06-20 12:57:22 +00:00
|
|
|
method,
|
|
|
|
auth,
|
|
|
|
measurements)
|
|
|
|
|
|
|
|
if status then
|
|
|
|
posix.syslog(30, tostring(res))
|
|
|
|
if ret_or_err then --successful xmlrpc call
|
|
|
|
measurements:clear()
|
|
|
|
end
|
|
|
|
else
|
2010-09-30 12:34:39 +00:00
|
|
|
posix.syslog(27, tostring(ret_or_err) .. ' ' .. home .. ' ' .. tostring(res))
|
2009-06-20 12:57:22 +00:00
|
|
|
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
|
|
|
|
|
2010-09-30 12:34:39 +00:00
|
|
|
function publish(child, path)
|
2010-04-20 13:38:53 +00:00
|
|
|
return coroutine.create(function(measurements)
|
2010-09-30 12:34:39 +00:00
|
|
|
os.execute('mkdir -p ' .. path .. ' > /dev/null')
|
2010-04-20 13:38:53 +00:00
|
|
|
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-09-30 12:34:39 +00:00
|
|
|
io.output(path .. '/' .. meter)
|
2010-04-20 13:38:53 +00:00
|
|
|
io.write(json)
|
|
|
|
io.close()
|
|
|
|
end
|
|
|
|
coroutine.resume(child, measurements)
|
|
|
|
measurements = coroutine.yield()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-09-30 12:34:39 +00:00
|
|
|
function debug(child, debug)
|
2010-04-19 21:34:07 +00:00
|
|
|
return coroutine.create(function(measurements)
|
|
|
|
while true do
|
2010-09-30 12:34:39 +00:00
|
|
|
if debug == 1 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-09-30 12:34:39 +00:00
|
|
|
debug(nil, tonumber(param.debug) or 0)
|
2010-04-19 21:34:07 +00:00
|
|
|
)
|
2010-09-30 12:34:39 +00:00
|
|
|
, param.home, param.homeVersion, 'logger.measurementAdd')
|
2010-04-19 21:34:07 +00:00
|
|
|
, 86400, 172800)
|
|
|
|
, 900, 7200)
|
|
|
|
, 60, 0)
|
2010-09-30 12:34:39 +00:00
|
|
|
, tonumber(param.homeInterval) or 300)
|
2010-04-20 13:38:53 +00:00
|
|
|
|
|
|
|
local p_chain = buffer(
|
|
|
|
polish(
|
|
|
|
publish(
|
2010-09-30 12:34:39 +00:00
|
|
|
debug(nil, tonumber(param.debug) or 0)
|
|
|
|
, param.localDir or '/tmp/sensor')
|
2010-04-20 13:38:53 +00:00
|
|
|
, 60)
|
2010-09-30 12:34:39 +00:00
|
|
|
, tonumber(param.localInterval) or 0)
|
2010-04-20 13:38:53 +00:00
|
|
|
|
2010-09-30 12:34:39 +00:00
|
|
|
local chain = dispatch(e_chain, p_chain, param.port or '/dev/ttyS0', tonumber(param.homeEnable) or 1, tonumber(param.localEnable) or 1)
|
2010-04-19 21:34:07 +00:00
|
|
|
|
|
|
|
coroutine.resume(chain)
|