mtx-youless.lua /size: 5137 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['mtx-youless'] = {
2    version   = 1.002,
3    comment   = "script to fetch data from kwh meter polling device",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE",
6    license   = "see context related readme files"
7}
8
9-- This script can fetch data from a youless device (http://www.youless.nl/) where data
10-- is merged into a file. The data concerns energy consumption (current wattage as well
11-- as kwh usage). There is an accompanying module to generate graphics.
12
13require("util-you")
14
15local formatters = string.formatters
16
17-- the script
18
19local helpinfo = [[
20<?xml version="1.0"?>
21<application>
22 <metadata>
23  <entry name="name">mtx-youless</entry>
24  <entry name="detail">youless Fetcher</entry>
25  <entry name="version">1.100</entry>
26 </metadata>
27 <flags>
28  <category name="basic">
29   <subcategory>
30    <flag name="collect"><short>collect data from device</short></flag>
31    <flag name="nobackup"><short>don't backup old datafile</short></flag>
32    <flag name="nofile"><short>don't write data to file (for checking)</short></flag>
33    <flag name="electricity"><short>collected eletricity data (p)</short></flag>
34    <flag name="gas"><short>collected gas data</short></flag>
35    <flag name="pulse"><short>collected eletricity data (s)</short></flag>
36    <flag name="host"><short>ip address of device</short></flag>
37    <flag name="auto"><short>fetch (refresh) all data every hour</short></flag>
38   </subcategory>
39  </category>
40 </flags>
41 <examples>
42  <category>
43   <title>Example</title>
44   <subcategory>
45    <example><command>mtxrun --script youless --collect --host=192.168.2.50 --electricity somefile.lua</command></example>
46    <example><command>mtxrun --script youless --collect --host=192.168.2.50 --gas         somefile.lua</command></example>
47    <example><command>mtxrun --script youless --collect --host=192.168.2.50 --pulse       somefile.lua</command></example>
48    <example><command>mtxrun --script youless --collect --host=192.168.2.50 --auto        file-prefix</command></example>
49   </subcategory>
50  </category>
51 </examples>
52</application>
53]]
54
55local application = logs.application {
56    name     = "mtx-youless",
57    banner   = "YouLess Fetcher 1.10",
58    helpinfo = helpinfo,
59}
60
61local report = application.report
62
63scripts         = scripts         or { }
64scripts.youless = scripts.youless or { }
65
66local arguments = environment.arguments
67local files     = environment.files
68
69function scripts.youless.collect()
70    local host     = arguments.host
71    local nobackup = arguments.nobackup
72    local nofile   = arguments.nofile
73    local password = arguments.password
74    local filename = files[1]
75    local delay    = tonumber(arguments.delay) or 12*60*60
76
77    local function fetch(filename,variant)
78        local data = utilities.youless.collect {
79            filename = filename,
80            host     = host,
81            variant  = variant,
82            nobackup = nobackup,
83            password = password,
84        }
85        if type(data) ~= "table" then
86            report("no data collected")
87        elseif filename == "" then
88            report("data collected but not saved")
89        end
90        report("using variant %a",variant)
91        if filename ~= "" then
92            report("using file %a",filename)
93        end
94        report("current time %a",os.now())
95    end
96
97    if not host then
98        host = "192.168.2.50"
99        report("using default host %a",host)
100    else
101        report("using host %a",host)
102    end
103    if nobackup then
104        report("not backing up data file")
105    end
106
107    if arguments.auto then
108        local filename_electricity = formatters["%s-electricity.lua"](filename ~= "" and filename or "youless")
109        local filename_gas         = formatters["%s-gas.lua"  ]      (filename ~= "" and filename or "youless")
110        local filename_pulse       = formatters["%s-pulse.lua"]      (filename ~= "" and filename or "youless")
111        while true do
112            fetch(filename_electricity,"electricity")
113            fetch(filename_gas,        "gas")
114            fetch(filename_pulse,      "pulse")
115            report("sleeping for %i seconds",delay)
116            io.flush()
117            os.sleep(delay)
118        end
119    else
120        local variant = (environment.arguments.electricity  and "electricity") or
121                        (environment.arguments.watt         and "electricity") or
122                        (environment.arguments.gas          and "gas") or
123                        (environment.arguments.pulse        and "pulse")
124        if not variant then
125            report("provide variant --electricity, --gas or --pulse")
126            return
127        end
128        if nofile then
129            filename = ""
130        elseif not filename or filename == "" then
131            filename = formatters["youless-%s.lua"](variant)
132        end
133        fetch(filename,variant)
134    end
135end
136
137if environment.argument("collect") then
138    scripts.youless.collect()
139elseif environment.argument("exporthelp") then
140    application.export(environment.argument("exporthelp"),environment.files[1])
141else
142    application.help()
143end
144