summaryrefslogtreecommitdiff
path: root/scripts/context/lua/mtx-evohome.lua
blob: 43479c0720776672222d58ab94c2b66bb5f7a54a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
if not modules then modules = { } end modules ['mtx-evohome'] = {
    version   = 1.002,
    comment   = "script to fetch data from a evohome device",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE",
    license   = "see context related readme files"
}

require("util-evo")

local formatters = string.formatters

-- the script

local helpinfo = [[
<?xml version="1.0"?>
<application>
 <metadata>
  <entry name="name">mtx-evohome</entry>
  <entry name="detail">Evohome Fetcher</entry>
  <entry name="version">1.00</entry>
 </metadata>
 <flags>
  <category name="basic">
   <subcategory>
    <flag name="collect"><short>collect data from device</short></flag>
    <flag name="presets"><short>file with authenciation data</short></flag>
    <flag name="auto"><short>fetch temperature data every hour</short></flag>
   </subcategory>
  </category>
 </flags>
 <examples>
  <category>
   <title>Example</title>
   <subcategory>
    <example><command>mtxrun --script evohome --collect --presets=c:/data/develop/domotica/code/evohome-presets.lua</command></example>
   </subcategory>
  </category>
 </examples>
</application>
]]

local application = logs.application {
    name     = "mtx-evohome",
    banner   = "Evohome Fetcher 1.00",
    helpinfo = helpinfo,
}

local report = application.report

scripts         = scripts         or { }
scripts.evohome = scripts.evohome or { }

local arguments = environment.arguments
local files     = environment.files

function scripts.evohome.collect()
    local presets = arguments.presets
    local delay   = tonumber(arguments.delay) or 12*60*60

    if presets then
        presets = utilities.evohome.loadpresets(presets)
    end
    if presets then
        local function fetch()
            report("current time %a",os.now())
            utilities.evohome.updatetemperatures(presets)
        end
        if arguments.auto then
            while true do
                fetch()
                report("sleeping for %i seconds",delay)
                io.flush()
                os.sleep(delay)
            end
        else
            fetch(presets)
        end
    else
        report("invalid preset file")
    end
end

if environment.argument("collect") then
    scripts.evohome.collect()
elseif environment.argument("exporthelp") then
    application.export(environment.argument("exporthelp"),environment.files[1])
else
    application.help()
end