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$
--
local os, math, table =
os, math, table
local os, math, table, string =
os, math, table, string
local getfenv, setmetatable, pairs =
getfenv, setmetatable, pairs
local getfenv, setmetatable, pairs, ipairs =
getfenv, setmetatable, pairs, ipairs
module (...)
local modenv = getfenv() -- module environment
@ -78,19 +78,19 @@ function fill(M)
end
function json_encode(M)
J = {}
local J = {}
for meter, T in pairs(M) do
J[meter] = '['
local H = timestamps(T)
for i = H[1], H[#H] do
J[meter] = J[meter] .. '[' .. T[i] .. ']'
for k, timestamp in ipairs(H) do
J[meter] = J[meter] .. '[' .. timestamp .. ',' .. T[timestamp] .. '],'
end
J[meter] = J[meter] .. ']'
J[meter] = string.sub(J[meter], 1, -2) .. ']'
end
return J
end
local function timestamps(T)
function timestamps(T)
local H = {} -- helper table, an indexed array containing all the measurement's timestamps
for timestamp in pairs(T) do H[#H+1] = timestamp end
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',
pwrport = 26488,
pwrenable = false,
pwrinterval = 1,
pwrinterval = 0,
pwrdir = '/tmp/sensor',
device = '/dev/ttyS0',
interval = 300}
@ -164,7 +164,7 @@ function publish(child, dir)
os.execute('mkdir -p ' .. dir .. ' > /dev/null')
while true do
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.write(json)
io.close()