fixes bugs in data module for the daemons pwr chain

This commit is contained in:
Bart Van Der Meerssche 2010-04-21 00:07:47 +02:00
parent 6667727b13
commit ed4e01ce8d
2 changed files with 11 additions and 11 deletions

View File

@ -20,11 +20,11 @@
-- $Id$ -- $Id$
-- --
local os, math, table = local os, math, table, string =
os, math, table os, math, table, string
local getfenv, setmetatable, pairs = local getfenv, setmetatable, pairs, ipairs =
getfenv, setmetatable, pairs getfenv, setmetatable, pairs, ipairs
module (...) module (...)
local modenv = getfenv() -- module environment local modenv = getfenv() -- module environment
@ -78,19 +78,19 @@ function fill(M)
end end
function json_encode(M) function json_encode(M)
J = {} local J = {}
for meter, T in pairs(M) do for meter, T in pairs(M) do
J[meter] = '[' J[meter] = '['
local H = timestamps(T) local H = timestamps(T)
for i = H[1], H[#H] do for k, timestamp in ipairs(H) do
J[meter] = J[meter] .. '[' .. T[i] .. ']' J[meter] = J[meter] .. '[' .. timestamp .. ',' .. T[timestamp] .. '],'
end end
J[meter] = J[meter] .. ']' J[meter] = string.sub(J[meter], 1, -2) .. ']'
end end
return J return J
end end
local function timestamps(T) function timestamps(T)
local H = {} -- helper table, an indexed array containing all the measurement's timestamps local H = {} -- helper table, an indexed array containing all the measurement's timestamps
for timestamp in pairs(T) do H[#H+1] = timestamp end for timestamp in pairs(T) do H[#H+1] = timestamp end
table.sort(H) -- sort in ascending order, oldest timestamps will be treated first table.sort(H) -- sort in ascending order, oldest timestamps will be treated first

View File

@ -35,7 +35,7 @@ local param = {xmlrpcaddress = 'http://logger.flukso.net/xmlrpc',
pwraddress = '255.255.255.255', pwraddress = '255.255.255.255',
pwrport = 26488, pwrport = 26488,
pwrenable = false, pwrenable = false,
pwrinterval = 1, pwrinterval = 0,
pwrdir = '/tmp/sensor', pwrdir = '/tmp/sensor',
device = '/dev/ttyS0', device = '/dev/ttyS0',
interval = 300} interval = 300}
@ -164,7 +164,7 @@ function publish(child, dir)
os.execute('mkdir -p ' .. dir .. ' > /dev/null') os.execute('mkdir -p ' .. dir .. ' > /dev/null')
while true do while true do
local measurements_json = measurements:json_encode() local measurements_json = measurements:json_encode()
for meter, json in measurements_json do for meter, json in pairs(measurements_json) do
io.output(dir .. '/' .. meter) io.output(dir .. '/' .. meter)
io.write(json) io.write(json)
io.close() io.close()