openwrt: use uci file for setting daemon parameters
This commit is contained in:
parent
57a0d071aa
commit
40ce61e4a6
|
@ -1,10 +1,11 @@
|
||||||
# Copyright (c) 2008 jokamajo.org
|
# Copyright (c) 2008 jokamajo.org
|
||||||
|
# 2010 flukso.net
|
||||||
# $Id$
|
# $Id$
|
||||||
|
|
||||||
include $(TOPDIR)/rules.mk
|
include $(TOPDIR)/rules.mk
|
||||||
|
|
||||||
PKG_NAME:=flukso
|
PKG_NAME:=flukso
|
||||||
PKG_VERSION:=1.0
|
PKG_VERSION:=1.1
|
||||||
PKG_RELEASE:=1
|
PKG_RELEASE:=1
|
||||||
|
|
||||||
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
|
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
|
||||||
|
@ -39,6 +40,8 @@ define Package/flukso/install
|
||||||
$(CP) $(PKG_BUILD_DIR)/heartbeat.lua $(1)/usr/share/lua/flukso/
|
$(CP) $(PKG_BUILD_DIR)/heartbeat.lua $(1)/usr/share/lua/flukso/
|
||||||
$(INSTALL_DIR) $(1)/etc/init.d/
|
$(INSTALL_DIR) $(1)/etc/init.d/
|
||||||
$(CP) $(PKG_BUILD_DIR)/flukso.init $(1)/etc/init.d/flukso
|
$(CP) $(PKG_BUILD_DIR)/flukso.init $(1)/etc/init.d/flukso
|
||||||
|
$(INSTALL_DIR) $(1)/etc/config/
|
||||||
|
$(CP) $(PKG_BUILD_DIR)/flukso.uci $(1)/etc/config/flukso
|
||||||
endef
|
endef
|
||||||
|
|
||||||
$(eval $(call BuildPackage,flukso))
|
$(eval $(call BuildPackage,flukso))
|
||||||
|
|
|
@ -25,21 +25,15 @@
|
||||||
require 'posix'
|
require 'posix'
|
||||||
require 'xmlrpc.http'
|
require 'xmlrpc.http'
|
||||||
|
|
||||||
data = require 'flukso.data'
|
local data = require 'flukso.data'
|
||||||
auth = require 'flukso.auth'
|
local auth = require 'flukso.auth'
|
||||||
dbg = require 'flukso.dbg'
|
local dbg = require 'flukso.dbg'
|
||||||
|
|
||||||
local param = {xmlrpcaddress = 'http://logger.flukso.net/xmlrpc',
|
local uci = require 'luci.model.uci'.cursor()
|
||||||
xmlrpcversion = '1',
|
local param = uci:get_all('flukso', 'main')
|
||||||
xmlrpcmethod = 'logger.measurementAdd',
|
|
||||||
pwrenable = true,
|
|
||||||
pwrinterval = 0,
|
|
||||||
pwrdir = '/tmp/sensor',
|
|
||||||
device = '/dev/ttyS0',
|
|
||||||
interval = 300,
|
|
||||||
dbgenable = false}
|
|
||||||
|
|
||||||
function dispatch(e_child, p_child, device, pwrenable)
|
|
||||||
|
function dispatch(e_child, p_child, port, homeEnable, localEnable)
|
||||||
return coroutine.create(function()
|
return coroutine.create(function()
|
||||||
|
|
||||||
local function flash() -- flash the power led for 50ms
|
local function flash() -- flash the power led for 50ms
|
||||||
|
@ -51,11 +45,11 @@ function dispatch(e_child, p_child, device, pwrenable)
|
||||||
-- 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 ' .. port .. '...')
|
||||||
|
|
||||||
local pattern = '^(%l+)%s(%x+):(%d+):?(%d*)$'
|
local pattern = '^(%l+)%s(%x+):(%d+):?(%d*)$'
|
||||||
|
|
||||||
for line in io.lines(device) do
|
for line in io.lines(port) do
|
||||||
local command, meter, value, msec = line:match(pattern)
|
local command, meter, value, msec = line:match(pattern)
|
||||||
value = tonumber(value or '0')
|
value = tonumber(value or '0')
|
||||||
msec = tonumber(msec or '0')
|
msec = tonumber(msec or '0')
|
||||||
|
@ -63,23 +57,23 @@ function dispatch(e_child, p_child, device, pwrenable)
|
||||||
|
|
||||||
if command == 'pls' and (length == 47 or length == 58) then -- user data
|
if command == 'pls' and (length == 47 or length == 58) then -- user data
|
||||||
flash()
|
flash()
|
||||||
posix.syslog(30, 'received pulse from ' .. device .. ': ' .. line:sub(5))
|
posix.syslog(30, 'received pulse from ' .. port .. ': ' .. line:sub(5))
|
||||||
|
|
||||||
coroutine.resume(e_child, meter, os.time(), value)
|
if homeEnable == 1 then coroutine.resume(e_child, meter, os.time(), value) end
|
||||||
|
|
||||||
-- pls includes a msec timestamp so report to p_child as well
|
-- pls includes a msec timestamp so report to p_child as well
|
||||||
if length == 58 then
|
if length == 58 and localEnable == 1 then
|
||||||
coroutine.resume(p_child, meter, os.time(), value, msec)
|
coroutine.resume(p_child, meter, os.time(), value, msec)
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif command == 'pwr' and length == 47 then -- user data
|
elseif command == 'pwr' and length == 47 then -- user data
|
||||||
if pwrenable then coroutine.resume(p_child, meter, os.time(), value) end
|
if localEnable == 1 then coroutine.resume(p_child, meter, os.time(), value) end
|
||||||
|
|
||||||
elseif command == 'msg' then -- control data
|
elseif command == 'msg' then -- control data
|
||||||
posix.syslog(31, 'received message from ' .. device .. ': ' .. line:sub(5))
|
posix.syslog(31, 'received message from ' .. port .. ': ' .. line:sub(5))
|
||||||
|
|
||||||
else -- error
|
else -- error
|
||||||
posix.syslog(27, 'input error on ' .. device .. ': ' .. line)
|
posix.syslog(27, 'input error on ' .. port .. ': ' .. line)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -145,7 +139,7 @@ function filter(child, span, offset)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function send(child, address, version, method)
|
function send(child, home, version, method)
|
||||||
return coroutine.create(function(measurements)
|
return coroutine.create(function(measurements)
|
||||||
while true do
|
while true do
|
||||||
local auth = auth.new()
|
local auth = auth.new()
|
||||||
|
@ -153,7 +147,7 @@ function send(child, address, version, method)
|
||||||
auth:hmac(measurements)
|
auth:hmac(measurements)
|
||||||
|
|
||||||
local status, ret_or_err, res = pcall(xmlrpc.http.call,
|
local status, ret_or_err, res = pcall(xmlrpc.http.call,
|
||||||
address..'/'..version,
|
'http://' .. home .. '/' .. version,
|
||||||
method,
|
method,
|
||||||
auth,
|
auth,
|
||||||
measurements)
|
measurements)
|
||||||
|
@ -164,7 +158,7 @@ function send(child, address, version, method)
|
||||||
measurements:clear()
|
measurements:clear()
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
posix.syslog(27, tostring(ret_or_err)..' '..address..' '..tostring(res))
|
posix.syslog(27, tostring(ret_or_err) .. ' ' .. home .. ' ' .. tostring(res))
|
||||||
end
|
end
|
||||||
coroutine.resume(child, measurements)
|
coroutine.resume(child, measurements)
|
||||||
measurements = coroutine.yield()
|
measurements = coroutine.yield()
|
||||||
|
@ -195,13 +189,13 @@ function polish(child, cutoff)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function publish(child, dir)
|
function publish(child, path)
|
||||||
return coroutine.create(function(measurements)
|
return coroutine.create(function(measurements)
|
||||||
os.execute('mkdir -p ' .. dir .. ' > /dev/null')
|
os.execute('mkdir -p ' .. path .. ' > /dev/null')
|
||||||
while true do
|
while true do
|
||||||
local measurements_json = measurements:json_encode()
|
local measurements_json = measurements:json_encode()
|
||||||
for meter, json in pairs(measurements_json) do
|
for meter, json in pairs(measurements_json) do
|
||||||
io.output(dir .. '/' .. meter)
|
io.output(path .. '/' .. meter)
|
||||||
io.write(json)
|
io.write(json)
|
||||||
io.close()
|
io.close()
|
||||||
end
|
end
|
||||||
|
@ -211,10 +205,10 @@ function publish(child, dir)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function debug(child, dbgenable)
|
function debug(child, debug)
|
||||||
return coroutine.create(function(measurements)
|
return coroutine.create(function(measurements)
|
||||||
while true do
|
while true do
|
||||||
if dbgenable then dbg.vardump(measurements) end
|
if debug == 1 then dbg.vardump(measurements) end
|
||||||
if child then coroutine.resume(child, measurements) end
|
if child then coroutine.resume(child, measurements) end
|
||||||
measurements = coroutine.yield()
|
measurements = coroutine.yield()
|
||||||
end
|
end
|
||||||
|
@ -234,22 +228,22 @@ local e_chain = buffer(
|
||||||
filter(
|
filter(
|
||||||
send(
|
send(
|
||||||
gc(
|
gc(
|
||||||
debug(nil, param.dbgenable)
|
debug(nil, tonumber(param.debug) or 0)
|
||||||
)
|
)
|
||||||
, param.xmlrpcaddress, param.xmlrpcversion, param.xmlrpcmethod)
|
, param.home, param.homeVersion, 'logger.measurementAdd')
|
||||||
, 86400, 172800)
|
, 86400, 172800)
|
||||||
, 900, 7200)
|
, 900, 7200)
|
||||||
, 60, 0)
|
, 60, 0)
|
||||||
, param.interval)
|
, tonumber(param.homeInterval) or 300)
|
||||||
|
|
||||||
local p_chain = buffer(
|
local p_chain = buffer(
|
||||||
polish(
|
polish(
|
||||||
publish(
|
publish(
|
||||||
debug(nil, param.dbgenable)
|
debug(nil, tonumber(param.debug) or 0)
|
||||||
, param.pwrdir)
|
, param.localDir or '/tmp/sensor')
|
||||||
, 60)
|
, 60)
|
||||||
, param.pwrinterval)
|
, tonumber(param.localInterval) or 0)
|
||||||
|
|
||||||
local chain = dispatch(e_chain, p_chain, param.device, param.pwrenable)
|
local chain = dispatch(e_chain, p_chain, param.port or '/dev/ttyS0', tonumber(param.homeEnable) or 1, tonumber(param.localEnable) or 1)
|
||||||
|
|
||||||
coroutine.resume(chain)
|
coroutine.resume(chain)
|
||||||
|
|
|
@ -4,7 +4,7 @@ config flukso main
|
||||||
option homeVersion 1
|
option homeVersion 1
|
||||||
option homeInterval 300
|
option homeInterval 300
|
||||||
option homeEnable 1
|
option homeEnable 1
|
||||||
option local /tmp/sensor
|
option localDir /tmp/sensor
|
||||||
option localVersion 1
|
option localVersion 1
|
||||||
option localInterval 0
|
option localInterval 0
|
||||||
option localEnable 1
|
option localEnable 1
|
||||||
|
|
|
@ -62,7 +62,7 @@ function rest_sensor(id)
|
||||||
local ltn12 = require "luci.ltn12"
|
local ltn12 = require "luci.ltn12"
|
||||||
local uci = require "luci.model.uci".cursor()
|
local uci = require "luci.model.uci".cursor()
|
||||||
|
|
||||||
local path = uci:get("flukso", "main", "local")
|
local path = uci:get("flukso", "main", "localDir")
|
||||||
local param = decode(http.getenv("QUERY_STRING"))
|
local param = decode(http.getenv("QUERY_STRING"))
|
||||||
|
|
||||||
if param.interval == "minute" and param.unit == "watt" then
|
if param.interval == "minute" and param.unit == "watt" then
|
||||||
|
|
Loading…
Reference in New Issue