summaryrefslogtreecommitdiff
path: root/scripts/context/lua/mtx-youless.lua
diff options
context:
space:
mode:
authorHans Hagen <pragma@wxs.nl>2017-11-07 12:49:36 +0100
committerContext Git Mirror Bot <phg42.2a@gmail.com>2017-11-07 12:49:36 +0100
commit75fbb107b15d01179a4b772844144e0661240e77 (patch)
treec5210f4f9ade25c89a540f755912a52966404792 /scripts/context/lua/mtx-youless.lua
parent7830451577b876020de2a26bbfbf069625ab4d6f (diff)
downloadcontext-75fbb107b15d01179a4b772844144e0661240e77.tar.gz
2017-11-07 11:43:00
Diffstat (limited to 'scripts/context/lua/mtx-youless.lua')
-rw-r--r--scripts/context/lua/mtx-youless.lua68
1 files changed, 45 insertions, 23 deletions
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 = [[
<flag name="kwh"><short>summative kwh data</short></flag>
<flag name="watt"><short>collected watt data</short></flag>
<flag name="host"><short>ip address of device</short></flag>
+ <flag name="auto"><short>fetch kwh and watt data every hour</short></flag>
</subcategory>
</category>
</flags>
@@ -42,6 +43,7 @@ local helpinfo = [[
<subcategory>
<example><command>mtxrun --script youless --collect --host=192.168.2.50 --kwh</command></example>
<example><command>mtxrun --script youless --collect --host=192.168.2.50 --watt somefile.lua</command></example>
+ <example><command>mtxrun --script youless --collect --host=192.168.2.50 --auto file-prefix</command></example>
</subcategory>
</category>
</examples>
@@ -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