[fsync] refactor the code and improve documentation
This commit is contained in:
parent
7a40fe3c8d
commit
609cea6f22
|
@ -28,6 +28,8 @@ local nixio = require 'nixio'
|
||||||
nixio.fs = require 'nixio.fs'
|
nixio.fs = require 'nixio.fs'
|
||||||
local uci = require 'luci.model.uci'.cursor()
|
local uci = require 'luci.model.uci'.cursor()
|
||||||
|
|
||||||
|
local HW_CHECK_OVERRIDE = (arg[1] == '-f')
|
||||||
|
|
||||||
local CTRL_PATH = '/var/run/spid/ctrl'
|
local CTRL_PATH = '/var/run/spid/ctrl'
|
||||||
local CTRL_PATH_IN = CTRL_PATH .. '/in'
|
local CTRL_PATH_IN = CTRL_PATH .. '/in'
|
||||||
local CTRL_PATH_OUT = CTRL_PATH .. '/out'
|
local CTRL_PATH_OUT = CTRL_PATH .. '/out'
|
||||||
|
@ -38,6 +40,39 @@ local POLLIN = nixio.poll_flags('in')
|
||||||
local POLL_TIMEOUT_MS = 1000
|
local POLL_TIMEOUT_MS = 1000
|
||||||
local MAX_TRIES = 5
|
local MAX_TRIES = 5
|
||||||
|
|
||||||
|
-- parse and load /etc/config/flukso
|
||||||
|
local flukso = uci:get_all('flukso')
|
||||||
|
|
||||||
|
local MAX_SENSORS = tonumber(flukso.main.max_sensors)
|
||||||
|
local MAX_ANALOG_SENSORS = tonumber(flukso.main.max_analog_sensors)
|
||||||
|
local RESET_COUNTERS = (flukso.main.reset_counters == '1')
|
||||||
|
local SYNC_TO_SERVER = (flukso.daemon.enable_wan_branch == '1')
|
||||||
|
|
||||||
|
local METERCONST_FACTOR = 0.449
|
||||||
|
|
||||||
|
local GET_HW_VERSION = 'gh'
|
||||||
|
local GET_HW_VERSION_R = '^gh%s+(%d+)%s+(%d+)$'
|
||||||
|
local SET_ENABLE = 'se %d %d'
|
||||||
|
local SET_PHY_TO_LOG = 'sp' -- with [1..MAX_SENSORS] arguments
|
||||||
|
local SET_METERCONST = 'sm %d %d'
|
||||||
|
local SET_COUNTER = 'sc %d %d'
|
||||||
|
local COMMIT = 'ct'
|
||||||
|
|
||||||
|
local API_PATH = '/www/sensor/'
|
||||||
|
local CGI_SCRIPT = '/usr/bin/restful'
|
||||||
|
local AVAHI_PATH = '/etc/avahi/services/flukso.service'
|
||||||
|
|
||||||
|
|
||||||
|
--- Convert from Lua-style to c-style index.
|
||||||
|
-- @param index Lua-style index startng at 1
|
||||||
|
-- @return C-style index starting at 0
|
||||||
|
local function toc(index)
|
||||||
|
return index - 1
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Log exit status to syslog first, then exit.
|
||||||
|
-- @param code exit status code
|
||||||
|
-- @return none
|
||||||
local function exit(code)
|
local function exit(code)
|
||||||
nixio.openlog('fsync', 'pid')
|
nixio.openlog('fsync', 'pid')
|
||||||
|
|
||||||
|
@ -49,10 +84,13 @@ local function exit(code)
|
||||||
level = 'err'
|
level = 'err'
|
||||||
end
|
end
|
||||||
|
|
||||||
nixio.syslog(level, string.format('fync exited with code: %d', code))
|
nixio.syslog(level, string.format('fsync exit status: %d', code))
|
||||||
os.exit(code)
|
os.exit(code)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Create a pair of file descriptors [fd] to the spid control fifo's.
|
||||||
|
-- @return ctrl object containing the fd's, a line-based iterator and poll flags
|
||||||
|
local function ctrl_init()
|
||||||
local ctrl = { fdin = nixio.open(CTRL_PATH_IN, O_RDWR_NONBLOCK),
|
local ctrl = { fdin = nixio.open(CTRL_PATH_IN, O_RDWR_NONBLOCK),
|
||||||
fdout = nixio.open(CTRL_PATH_OUT, O_RDWR_NONBLOCK),
|
fdout = nixio.open(CTRL_PATH_OUT, O_RDWR_NONBLOCK),
|
||||||
events = POLLIN,
|
events = POLLIN,
|
||||||
|
@ -74,7 +112,21 @@ end
|
||||||
ctrl.fd = ctrl.fdout -- need this entry for nixio.poll
|
ctrl.fd = ctrl.fdout -- need this entry for nixio.poll
|
||||||
ctrl.line = ctrl.fdout:linesource()
|
ctrl.line = ctrl.fdout:linesource()
|
||||||
|
|
||||||
|
return ctrl
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Close the spid control fifo's.
|
||||||
|
-- @param code ctrl object
|
||||||
|
-- @return none
|
||||||
|
local function ctrl_close(ctrl)
|
||||||
|
ctrl.fdin:close()
|
||||||
|
ctrl.fdout:close()
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Send a command to the control fifo.
|
||||||
|
-- @param ctrl ctrl object
|
||||||
|
-- @param cmd command to send
|
||||||
|
-- @return none
|
||||||
local function send(ctrl, cmd)
|
local function send(ctrl, cmd)
|
||||||
while ctrl.line() do end -- flush the out fifo
|
while ctrl.line() do end -- flush the out fifo
|
||||||
|
|
||||||
|
@ -112,33 +164,10 @@ local function send(ctrl, cmd)
|
||||||
exit(2)
|
exit(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function toc(num)
|
--- Check the sensor board hardware version.
|
||||||
return num - 1
|
-- @param ctrl ctrl object
|
||||||
end
|
-- @return none
|
||||||
|
local function check_hw_version(ctrl)
|
||||||
|
|
||||||
-- parse and load /etc/config/flukso
|
|
||||||
local flukso = uci:get_all('flukso')
|
|
||||||
|
|
||||||
local HW_CHECK_OVERRIDE = (arg[1] == '-f')
|
|
||||||
|
|
||||||
local MAX_SENSORS = tonumber(flukso.main.max_sensors)
|
|
||||||
local MAX_ANALOG_SENSORS = tonumber(flukso.main.max_analog_sensors)
|
|
||||||
local METERCONST_FACTOR = 0.449
|
|
||||||
|
|
||||||
local GET_HW_VERSION = 'gh'
|
|
||||||
local GET_HW_VERSION_R = '^gh%s+(%d+)%s+(%d+)$'
|
|
||||||
local SET_ENABLE = 'se %d %d'
|
|
||||||
local SET_PHY_TO_LOG = 'sp' -- with [1..MAX_SENSORS] arguments
|
|
||||||
local SET_METERCONST = 'sm %d %d'
|
|
||||||
local SET_COUNTER = 'sc %d %d'
|
|
||||||
local COMMIT = 'ct'
|
|
||||||
|
|
||||||
local API_PATH = '/www/sensor/'
|
|
||||||
local CGI_SCRIPT = '/usr/bin/restful'
|
|
||||||
local AVAHI_PATH = '/etc/avahi/services/flukso.service'
|
|
||||||
|
|
||||||
-- check hardware version
|
|
||||||
local hw_major, hw_minor = send(ctrl, GET_HW_VERSION):match(GET_HW_VERSION_R)
|
local hw_major, hw_minor = send(ctrl, GET_HW_VERSION):match(GET_HW_VERSION_R)
|
||||||
|
|
||||||
if hw_major ~= flukso.main.hw_major or hw_minor > flukso.main.hw_minor then
|
if hw_major ~= flukso.main.hw_major or hw_minor > flukso.main.hw_minor then
|
||||||
|
@ -160,14 +189,22 @@ if hw_major ~= flukso.main.hw_major or hw_minor > flukso.main.hw_minor then
|
||||||
else
|
else
|
||||||
print(string.format('Hardware check (major: %s, minor: %s) .. ok', hw_major, hw_minor))
|
print(string.format('Hardware check (major: %s, minor: %s) .. ok', hw_major, hw_minor))
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- disable all ports
|
--- Disable all sensors in the sensor board.
|
||||||
|
-- @param ctrl ctrl object
|
||||||
|
-- @return none
|
||||||
|
local function disable_all_sensors(ctrl)
|
||||||
for i = 1, MAX_SENSORS do
|
for i = 1, MAX_SENSORS do
|
||||||
local cmd = string.format(SET_ENABLE, toc(i), 0)
|
local cmd = string.format(SET_ENABLE, toc(i), 0)
|
||||||
send(ctrl, cmd)
|
send(ctrl, cmd)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- populate phy_to_log
|
--- Populate the physical (port) to logical (sensor) map on the sensor board.
|
||||||
|
-- @param ctrl ctrl object
|
||||||
|
-- @return none
|
||||||
|
local function set_phy_to_log(ctrl)
|
||||||
local phy_to_log = {}
|
local phy_to_log = {}
|
||||||
|
|
||||||
for i = 1, MAX_SENSORS do
|
for i = 1, MAX_SENSORS do
|
||||||
|
@ -200,8 +237,12 @@ end
|
||||||
|
|
||||||
local cmd = SET_PHY_TO_LOG .. ' ' .. table.concat(phy_to_log, ' ', 0)
|
local cmd = SET_PHY_TO_LOG .. ' ' .. table.concat(phy_to_log, ' ', 0)
|
||||||
send(ctrl, cmd)
|
send(ctrl, cmd)
|
||||||
|
end
|
||||||
|
|
||||||
-- populate meterconst
|
--- Populate each sensor's meterconstant on the sensor board.
|
||||||
|
-- @param ctrl ctrl object
|
||||||
|
-- @return none
|
||||||
|
local function set_meterconst(ctrl)
|
||||||
for i = 1, MAX_SENSORS do
|
for i = 1, MAX_SENSORS do
|
||||||
local cmd
|
local cmd
|
||||||
|
|
||||||
|
@ -216,15 +257,19 @@ for i = 1, MAX_SENSORS do
|
||||||
elseif flukso[tostring(i)]['class'] == 'pulse'then
|
elseif flukso[tostring(i)]['class'] == 'pulse'then
|
||||||
local meterconst = tonumber(flukso[tostring(i)].constant or "0")
|
local meterconst = tonumber(flukso[tostring(i)].constant or "0")
|
||||||
cmd = string.format(SET_METERCONST, toc(i), meterconst)
|
cmd = string.format(SET_METERCONST, toc(i), meterconst)
|
||||||
|
|
||||||
else
|
else
|
||||||
cmd = string.format(SET_METERCONST, toc(i), 0)
|
cmd = string.format(SET_METERCONST, toc(i), 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
if cmd then send(ctrl,cmd) end
|
if cmd then send(ctrl,cmd) end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- populate counter if reset_counters is set
|
--- Reset each sensor's counter on the sensor board.
|
||||||
if flukso.main.reset_counters == '1' then
|
-- @param ctrl ctrl object
|
||||||
|
-- @return none
|
||||||
|
local function reset_counters(ctrl)
|
||||||
for i = 1, MAX_SENSORS do
|
for i = 1, MAX_SENSORS do
|
||||||
local cmd = string.format(SET_COUNTER, toc(i), 0)
|
local cmd = string.format(SET_COUNTER, toc(i), 0)
|
||||||
send(ctrl, cmd)
|
send(ctrl, cmd)
|
||||||
|
@ -234,22 +279,29 @@ if flukso.main.reset_counters == '1' then
|
||||||
uci:commit('flukso')
|
uci:commit('flukso')
|
||||||
end
|
end
|
||||||
|
|
||||||
-- enable configured sensors
|
--- Activate the enabled sensors on the sensor board.
|
||||||
|
-- @param ctrl ctrl object
|
||||||
|
-- @return none
|
||||||
|
local function enable_sensors(ctrl)
|
||||||
for i = 1, MAX_SENSORS do
|
for i = 1, MAX_SENSORS do
|
||||||
if flukso[tostring(i)] ~= nil and flukso[tostring(i)].enable == '1' then
|
if flukso[tostring(i)] ~= nil and flukso[tostring(i)].enable == '1' then
|
||||||
cmd = string.format(SET_ENABLE, toc(i), 1)
|
cmd = string.format(SET_ENABLE, toc(i), 1)
|
||||||
send(ctrl, cmd)
|
send(ctrl, cmd)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- commit changes
|
--- Commit all changes on the sensor board.
|
||||||
|
-- @param ctrl ctrl object
|
||||||
|
-- @return none
|
||||||
|
local function commit(ctrl)
|
||||||
send(ctrl, COMMIT)
|
send(ctrl, COMMIT)
|
||||||
|
end
|
||||||
-- clean up ctrl port fd's
|
|
||||||
ctrl.fdin:close()
|
|
||||||
ctrl.fdout:close()
|
|
||||||
|
|
||||||
|
|
||||||
|
--- Map /sensor/xyz endpoints to the cgi script.
|
||||||
|
-- @return none
|
||||||
|
local function create_symlinks()
|
||||||
-- make sure /www/sensor exists
|
-- make sure /www/sensor exists
|
||||||
nixio.fs.mkdirr(API_PATH)
|
nixio.fs.mkdirr(API_PATH)
|
||||||
|
|
||||||
|
@ -260,7 +312,12 @@ end
|
||||||
|
|
||||||
-- generate new symlinks
|
-- generate new symlinks
|
||||||
for i = 1, MAX_SENSORS do
|
for i = 1, MAX_SENSORS do
|
||||||
if flukso[tostring(i)] ~= nil and flukso[tostring(i)].enable == '1' then
|
if flukso[tostring(i)] ~= nil
|
||||||
|
and flukso[tostring(i)].enable == '1'
|
||||||
|
and flukso[tostring(i)].id
|
||||||
|
and flukso[tostring(i)].class ~= 'uart'
|
||||||
|
then
|
||||||
|
|
||||||
local sensor_id = flukso[tostring(i)].id
|
local sensor_id = flukso[tostring(i)].id
|
||||||
|
|
||||||
if sensor_id then
|
if sensor_id then
|
||||||
|
@ -269,8 +326,11 @@ for i = 1, MAX_SENSORS do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- generate a new flukso.service for avahi
|
--- Generate a new flukso.service xml file for the avahi-daemon.
|
||||||
|
-- @return none
|
||||||
|
local function create_avahi_config()
|
||||||
avahi = { head = {}, body = {}, tail = {} }
|
avahi = { head = {}, body = {}, tail = {} }
|
||||||
|
|
||||||
avahi.head[1] = '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->'
|
avahi.head[1] = '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->'
|
||||||
|
@ -282,7 +342,12 @@ avahi.head[6] = ' <type>_flukso._tcp</type>'
|
||||||
avahi.head[7] = ' <port>8080</port>'
|
avahi.head[7] = ' <port>8080</port>'
|
||||||
|
|
||||||
for i = 1, MAX_SENSORS do
|
for i = 1, MAX_SENSORS do
|
||||||
if flukso[tostring(i)] ~= nil and flukso[tostring(i)].enable == '1' and flukso[tostring(i)].id then
|
if flukso[tostring(i)] ~= nil
|
||||||
|
and flukso[tostring(i)].enable == '1'
|
||||||
|
and flukso[tostring(i)].id
|
||||||
|
and flukso[tostring(i)].class ~= 'uart'
|
||||||
|
then
|
||||||
|
|
||||||
avahi.body[#avahi.body + 1] = string.format(' <txt-record>id%d=%s</txt-record>' , i, flukso[tostring(i)].id)
|
avahi.body[#avahi.body + 1] = string.format(' <txt-record>id%d=%s</txt-record>' , i, flukso[tostring(i)].id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -310,6 +375,37 @@ end
|
||||||
for i = 1, #avahi.tail do
|
for i = 1, #avahi.tail do
|
||||||
fd:write(avahi.tail[i] .. '\n')
|
fd:write(avahi.tail[i] .. '\n')
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- sync config to sensor board
|
||||||
|
local ctrl = ctrl_init()
|
||||||
|
|
||||||
|
check_hw_version(ctrl)
|
||||||
|
disable_all_sensors(ctrl)
|
||||||
|
set_phy_to_log(ctrl)
|
||||||
|
set_meterconst(ctrl)
|
||||||
|
|
||||||
|
if RESET_COUNTERS then
|
||||||
|
reset_counters(ctrl)
|
||||||
|
end
|
||||||
|
|
||||||
|
enable_sensors(ctrl)
|
||||||
|
commit(ctrl)
|
||||||
|
|
||||||
|
ctrl_close(ctrl)
|
||||||
|
|
||||||
|
|
||||||
|
-- sync config locally
|
||||||
|
create_symlinks()
|
||||||
|
create_avahi_config()
|
||||||
|
|
||||||
|
|
||||||
|
-- sync config to server
|
||||||
|
if SYNC_TO_SERVER then
|
||||||
|
-- TODO POST each sensor's parameters to the /sensor/xyz endpoint
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
print(arg[0] .. ' completed successfully. Bye!')
|
print(arg[0] .. ' completed successfully. Bye!')
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
Loading…
Reference in New Issue