summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkiv/s-evohome.mkiv
blob: 1577517e513031dbbc00b546693e7b07b6390b46 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
%D \module
%D   [      file=s-evohome,
%D        version=2017.11.12,
%D          title=\CONTEXT\ Style File,
%D       subtitle=Evohome Graphics,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\setupbodyfont[dejavu]

\starttext

\startluacode

local evohome = require("util-evo")

moduledata.evohome = { }

function moduledata.evohome.status(specification)

    local presets = evohome.helpers.loadpresets(specification.presets)
    local zones   = evohome.helpers.loadtemperatures(presets)

    -- todo : time

    if not zones then
        context("no zones")
        context.blank()
        return
    end

    local t = zones.time

    t = t and os.date("%Y-%m-%d %H:%M:%S",t) or "unknown time"

    context.starttitle { title = t }

    context.starttabulate { "|l|c|c|c|c|" }
        context.BC() context("name")
        context.BC() context("current")
        context.BC() context("min")
        context.BC() context("max")
        context.BC() context("target")
        context.NC() context.NR()
        for i=1,#zones do
            local zone = zones[i]
            context.NC() context.word(zone.name)
            context.NC() context("%0.1f",zone.current)
            context.NC() context("%0.1f",zone.min)
            context.NC() context("%0.1f",zone.max)
            context.NC() context("%0.1f",zone.target)
            context.NC() context.NR()
        end
    context.stoptabulate()

    for i=1,#zones do
        local zone     = zones[i]
-- evohome.helpers.updatezone(presets,zone.name)
        local data     = evohome.helpers.getzonestate(presets,zone.name)
        local schedule = data and data.schedule or { }
        context.startsubject { title = zone.name }
        context.starttabulate { "|l|c|c|" }
        context.BC() context("day")
        context.BC() context("time")
        context.BC() context("temperature")
        context.NR()
        local daily = schedule.dailySchedules
        if daily then
            for i=1,#daily do
                local d = daily[i]
                local s = d.switchpoints
                if s then
                    for i=1,#s do
                        context.NC()
                        if i == 1 then
                            context.word(d.dayOfWeek)
                        end
                        context.NC() context(s[i].timeOfDay)
                        context.NC() context(s[i].temperature)
                        context.NC() context.NR()
                    end
                end
            end
        end
        context.stoptabulate()
        context.stopsubject()
    end

    context.stoptitle()

end

function moduledata.evohome.history(specification)

    local presets = evohome.helpers.loadpresets(specification.presets)
    local zones   = evohome.helpers.loadtemperatures(presets)

    if not zones then
        context("no zones")
        context.blank()
        return
    end

    local data = evohome.helpers.loadhistory(presets)

    if not zones then
        context("no history")
        return
    end

    local years = data.years

    if not years then
        context("no years")
        return
    end

    local minyear  = specification.year or 2017
    local maxyear  = minyear
    local minmonth = specification.month or 1
    local maxmonth = specification.month or 12

    local scale   = 1/8 -- 20
    local mark    = 3
    local abstemp = 25

    for y=minyear,maxyear do

        local year = years[y]

        for m=minmonth,maxmonth do

            local month = year.months[m]

            if month then

                context.starttitle { title = "\\month{" .. m .. "}\\enspace" .. y } -- todo: function context.Month(m) end

                for i=1,#zones do

                    local zone    = zones[i]
                    local where   = zone.name
                    local mintemp = zone.min
                    local maxtemp = zone.max

                 -- context.startMPpage { offset = "10pt" }
                    context.startlinecorrection()
                    context.startMPcode()
                    context("linecap := butt; pickup pencircle scaled .5")

                    context("fill (%s,%s) -- (%s,%s) -- (%s,%s) -- (%s,%s) -- cycle withcolor .8white ;",
                              0, mintemp/scale,
                        31 * 24, mintemp/scale,
                        31 * 24, maxtemp/scale,
                              0, maxtemp/scale
                    )

                    for i=0,abstemp do
                        context("draw (%s,%s) -- (%s,%s) withcolor .6white ;",
                                  0, i/scale,
                            31 * 24, i/scale
                        )
                    end

                    local days = month.days
                    if days then
                        local nd = os.nofdays(y,m)
                        for d=1,nd do
                            local day = days[d]
                            local xoffset = (d-1) * 24
                            local wd = os.weekday(d,m,y)
                            local weekend = wd == 1 or wd == 7
                            if not weekend then
                                -- okay
                            elseif mark == 1 then
                                context("draw (%s,%s) -- (%s,%s) ; ",xoffset,   -17.5,xoffset,   -32.5)
                                context("draw (%s,%s) -- (%s,%s) ; ",xoffset+24,-17.5,xoffset+24,-32.5)
                            elseif mark == 2 then
                                context("draw (%s,%s) -- (%s,%s) ; ",xoffset,   -17.5,xoffset+24,-17.5)
                                context("draw (%s,%s) -- (%s,%s) ; ",xoffset,   -32.5,xoffset+24,-32.5)
                            elseif mark == 3 then
                                context("draw unitsquare xysized (%s,%s) shifted (%s,%s) ; ",24,15,xoffset,-32.5)
                            end
                            context([[draw textext("%s") shifted (%s,%s) ; ]],d,xoffset + 12,-25)
                            if day then
                                for h=0,23 do
                                    local hours = day.hours
                                    if hours then
                                        local hour = hours[h]
                                        if hour then
                                            local a = 0
                                            local n = 0
                                            local m = 0
                                            for minute, d in next, hour do
                                                local v = d[where]
                                                a = a + v
                                                n = n + 1
                                                if v > m then
                                                    m = v
                                                end
                                            end
                                            a = a / n
                                            local dx = xoffset + h
                                            local dy = a/scale
                                            local dm = m/scale
                                            context("draw (%s,%s) -- (%s,%s) withcolor %s ; ",
                                                dx, 0,dx,dy,weekend and "darkmagenta" or "darkblue")
                                            context("draw (%s,%s) -- (%s,%s) withcolor %s ; ",
                                                dx,dy,dx,dm,"darkred")
                                        end
                                    end
                                end
                            end
                        end
                        for d=0,30 do
                            local xoffset = d * 24
                            context("draw (%s,%s) -- (%s,%s) withcolor darkgray ; ",xoffset+ 0,0,xoffset+ 0,-10)
                            context("draw (%s,%s) -- (%s,%s) withcolor darkgray ; ",xoffset+ 6,0,xoffset+ 6,-2.5)
                            context("draw (%s,%s) -- (%s,%s) withcolor darkgray ; ",xoffset+12,0,xoffset+12,-5)
                            context("draw (%s,%s) -- (%s,%s) withcolor darkgray ; ",xoffset+18,0,xoffset+18,-2.5)
                        end
                        local xoffset = 31 * 24
                        context("draw (%s,%s) -- (%s,%s) withcolor darkgray ; ",xoffset,0,xoffset,-10)
                    end

                    for i=0,abstemp,5 do
                        context([[draw textext.lft("%s") shifted (%s,%s) ; ]],i,-10,i/scale)
                        context("draw (%s,%s) -- (%s,%s) withcolor .2white  ;",0,i/scale,31 * 24,i/scale)
                    end

                    context([[draw textext("\strut\month{%s}\enspace%s\enspace:\enspace%s") shifted (%s,%s) ; ]], m, y, where, 31 * 24 / 2, -50)
                    context([[draw textext.lft("\textdegree{}C") shifted (%s,%s) ; ]],-10,-25)

                    context("currentpicture := currentpicture xsized TextWidth") ;

                    context.stopMPcode()
                    context.stoplinecorrection()
                    -- context.stopMPpage()
                end

                context.stoptitle()

            end

        end

    end

end

\stopluacode

\continueifinputfile{s-evohome.mkiv}

\usemodule[art-01]

\setuplayout
  [article]
  [footer=0cm,
   header=1cm,
   topspace=5mm,
   bottomspace=5mm]

\starttext

    \startluacode

     -- os.execute([[mtxrun --script evohome --collect --presets=c:/data/develop/domotica/code/evohome-presets.lua]])

     -- local specification = {
     --     year    = 2017,
     --  -- month   = 11,
     --     presets = "c:/data/develop/domotica/evohome/evohome-presets.lua",
     -- }

     -- moduledata.evohome.status (specification)
     -- moduledata.evohome.history(specification)

        local specification = {
            year    = 2018,
         -- month   = 11,
            presets = "c:/data/develop/domotica/evohome/evohome-presets.lua",
        }

     -- moduledata.evohome.status (specification)
        moduledata.evohome.history(specification)

    \stopluacode

\stoptext