From 75fbb107b15d01179a4b772844144e0661240e77 Mon Sep 17 00:00:00 2001 From: Hans Hagen Date: Tue, 7 Nov 2017 12:49:36 +0100 Subject: 2017-11-07 11:43:00 --- scripts/context/lua/mtx-youless.lua | 68 ++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 23 deletions(-) (limited to 'scripts/context/lua/mtx-youless.lua') diff --git a/scripts/context/lua/mtx-youless.lua b/scripts/context/lua/mtx-youless.lua index 740fdcb65..6c600517a 100644 --- a/scripts/context/lua/mtx-youless.lua +++ b/scripts/context/lua/mtx-youless.lua @@ -33,6 +33,7 @@ local helpinfo = [[ summative kwh data collected watt data ip address of device + fetch kwh and watt data every hour @@ -42,6 +43,7 @@ local helpinfo = [[ mtxrun --script youless --collect --host=192.168.2.50 --kwh mtxrun --script youless --collect --host=192.168.2.50 --watt somefile.lua + mtxrun --script youless --collect --host=192.168.2.50 --auto file-prefix @@ -61,17 +63,32 @@ scripts.youless = scripts.youless or { } function scripts.youless.collect() local host = environment.arguments.host - local variant = environment.arguments.kwh and "kwh" or environment.arguments.watt and "watt" local nobackup = environment.arguments.nobackup local nofile = environment.arguments.nofile local password = environment.arguments.password local filename = environment.files[1] - if not variant then - report("provide variant --kwh or --watt") - return - else + local delay = tonumber(environment.delay) or 12*60*60 + + local function fetch(filename,variant) + local data = utilities.youless.collect { + filename = filename, + host = host, + variant = variant, + nobackup = nobackup, + password = password, + } + if type(data) ~= "table" then + report("no data collected") + elseif filename == "" then + report("data collected but not saved") + end report("using variant %a",variant) + if filename ~= "" then + report("using file %a",filename) + end + report("current time %a",os.now()) end + if not host then host = "192.168.2.50" report("using default host %a",host) @@ -81,25 +98,30 @@ function scripts.youless.collect() if nobackup then report("not backing up data file") end - if not filename and not nofile then - filename = formatters["youless-%s.lua"](variant) - end - if filename ~= "" then - report("using file %a",filename) - end - local data = utilities.youless.collect { - filename = filename, - host = host, - variant = variant, - nobackup = nobackup, - password = password, - } - if type(data) ~= "table" then - report("no data collected") - elseif filename == "" then - report("data collected but not saved") + + if environment.arguments.auto then + local filename_kwh = formatters["%s-kwh.lua" ](filename ~= "" and filename or "youless") + local filename_watt = formatters["%s-watt.lua"](filename ~= "" and filename or "youless") + while true do + fetch(filename_kwh,"kwh") + fetch(filename_watt,"watt") + report("sleeping for %i seconds",delay) + io.flush() + os.sleep(delay) + end + else + local variant = environment.arguments.kwh and "kwh" or environment.arguments.watt and "watt" + if not variant then + report("provide variant --kwh or --watt") + return + end + if nofile then + filename = "" + elseif not filename or filename == "" then + filename = formatters["youless-%s.lua"](variant) + end + fetch(filename,variant) end - report("current time %a",os.now()) end if environment.argument("collect") then -- cgit v1.2.3