s-evohome.mkiv /size: 10 Kb    last modification: 2025-02-21 11:03
1%D \module
2%D   [      file=s-evohome,
3%D        version=2017.11.12,
4%D          title=\CONTEXT\ Style File,
5%D       subtitle=Evohome Graphics,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\setupbodyfont[dejavu]
15
16\starttext
17
18\startluacode
19
20local evohome = require("util-evo")
21
22moduledata.evohome = { }
23
24function moduledata.evohome.status(specification)
25
26    local presets = evohome.helpers.loadpresets(specification.presets)
27    local zones   = evohome.helpers.loadtemperatures(presets)
28
29    -- todo : time
30
31    if not zones then
32        context("no zones")
33        context.blank()
34        return
35    end
36
37    local t = zones.time
38
39    t = t and os.date("%Y-%m-%d %H:%M:%S",t) or "unknown time"
40
41    context.starttitle { title = t }
42
43    context.starttabulate { "|l|c|c|c|c|" }
44        context.BC() context("name")
45        context.BC() context("current")
46        context.BC() context("min")
47        context.BC() context("max")
48        context.BC() context("target")
49        context.NC() context.NR()
50        for i=1,#zones do
51            local zone = zones[i]
52            context.NC() context.word(zone.name)
53            context.NC() context("%0.1f",zone.current)
54            context.NC() context("%0.1f",zone.min)
55            context.NC() context("%0.1f",zone.max)
56            context.NC() context("%0.1f",zone.target)
57            context.NC() context.NR()
58        end
59    context.stoptabulate()
60
61    for i=1,#zones do
62        local zone     = zones[i]
63-- evohome.helpers.updatezone(presets,zone.name)
64        local data     = evohome.helpers.getzonestate(presets,zone.name)
65        local schedule = data and data.schedule or { }
66        context.startsubject { title = zone.name }
67        context.starttabulate { "|l|c|c|" }
68        context.BC() context("day")
69        context.BC() context("time")
70        context.BC() context("temperature")
71        context.NR()
72        local daily = schedule.dailySchedules
73        if daily then
74            for i=1,#daily do
75                local d = daily[i]
76                local s = d.switchpoints
77                if s then
78                    for i=1,#s do
79                        context.NC()
80                        if i == 1 then
81                            context.word(d.dayOfWeek)
82                        end
83                        context.NC() context(s[i].timeOfDay)
84                        context.NC() context(s[i].temperature)
85                        context.NC() context.NR()
86                    end
87                end
88            end
89        end
90        context.stoptabulate()
91        context.stopsubject()
92    end
93
94    context.stoptitle()
95
96end
97
98function moduledata.evohome.history(specification)
99
100    local presets = evohome.helpers.loadpresets(specification.presets)
101    local zones   = evohome.helpers.loadtemperatures(presets)
102
103    if not zones then
104        context("no zones")
105        context.blank()
106        return
107    end
108
109    local data = evohome.helpers.loadhistory(presets)
110
111    if not zones then
112        context("no history")
113        return
114    end
115
116    local years = data.years
117
118    if not years then
119        context("no years")
120        return
121    end
122
123    local minyear  = specification.year or 2017
124    local maxyear  = minyear
125    local minmonth = specification.month or 1
126    local maxmonth = specification.month or 12
127
128    local scale   = 1/8 -- 20
129    local mark    = 3
130    local abstemp = 25
131
132    for y=minyear,maxyear do
133
134        local year = years[y]
135
136        if year then
137
138            for m=minmonth,maxmonth do
139
140                local month = year.months[m]
141
142                if month then
143
144                    context.starttitle { title = "\\month{" .. m .. "}\\enspace" .. y } -- todo: function context.Month(m) end
145
146                    for i=1,#zones do
147
148                        local zone    = zones[i]
149                        local where   = zone.name
150                        local mintemp = zone.min
151                        local maxtemp = zone.max
152
153                     -- context.startMPpage { offset = "10pt" }
154                        context.startlinecorrection()
155                        context.startMPcode()
156                        context("linecap := butt; pickup pencircle scaled .5")
157
158                        context("fill (%s,%s) -- (%s,%s) -- (%s,%s) -- (%s,%s) -- cycle withcolor .8white ;",
159                                  0, mintemp/scale,
160                            31 * 24, mintemp/scale,
161                            31 * 24, maxtemp/scale,
162                                  0, maxtemp/scale
163                        )
164
165                        for i=0,abstemp do
166                            context("draw (%s,%s) -- (%s,%s) withcolor .6white ;",
167                                      0, i/scale,
168                                31 * 24, i/scale
169                            )
170                        end
171
172                        local days = month.days
173                        if days then
174                            local nd = os.nofdays(y,m)
175                            for d=1,nd do
176                                local day = days[d]
177                                local xoffset = (d-1) * 24
178                                local wd = os.weekday(d,m,y)
179                                local weekend = wd == 1 or wd == 7
180                                if not weekend then
181                                    -- okay
182                                elseif mark == 1 then
183                                    context("draw (%s,%s) -- (%s,%s) ; ",xoffset,   -17.5,xoffset,   -32.5)
184                                    context("draw (%s,%s) -- (%s,%s) ; ",xoffset+24,-17.5,xoffset+24,-32.5)
185                                elseif mark == 2 then
186                                    context("draw (%s,%s) -- (%s,%s) ; ",xoffset,   -17.5,xoffset+24,-17.5)
187                                    context("draw (%s,%s) -- (%s,%s) ; ",xoffset,   -32.5,xoffset+24,-32.5)
188                                elseif mark == 3 then
189                                    context("draw unitsquare xysized (%s,%s) shifted (%s,%s) ; ",24,15,xoffset,-32.5)
190                                end
191                                context([[draw textext("%s") shifted (%s,%s) ; ]],d,xoffset + 12,-25)
192                                if day then
193                                    for h=0,23 do
194                                        local hours = day.hours
195                                        if hours then
196                                            local hour = hours[h]
197                                            if hour then
198                                                local a = 0
199                                                local n = 0
200                                                local m = 0
201                                                for minute, d in next, hour do
202                                                    local v = d[where]
203                                                    if v then
204                                                        a = a + v
205                                                        n = n + 1
206                                                        if v > m then
207                                                            m = v
208                                                        end
209                                                    end
210                                                end
211                                                if n > 0 then
212                                                    a = a / n
213                                                else
214                                                    a = 0
215                                                end
216                                                local dx = xoffset + h
217                                                local dy = a/scale
218                                                local dm = m/scale
219                                                context("draw (%s,%s) -- (%s,%s) withcolor %s ; ",
220                                                    dx, 0,dx,dy,weekend and "darkmagenta" or "darkblue")
221                                                context("draw (%s,%s) -- (%s,%s) withcolor %s ; ",
222                                                    dx,dy,dx,dm,"darkred")
223                                            end
224                                        end
225                                    end
226                                end
227                            end
228                            for d=0,30 do
229                                local xoffset = d * 24
230                                context("draw (%s,%s) -- (%s,%s) withcolor darkgray ; ",xoffset+ 0,0,xoffset+ 0,-10)
231                                context("draw (%s,%s) -- (%s,%s) withcolor darkgray ; ",xoffset+ 6,0,xoffset+ 6,-2.5)
232                                context("draw (%s,%s) -- (%s,%s) withcolor darkgray ; ",xoffset+12,0,xoffset+12,-5)
233                                context("draw (%s,%s) -- (%s,%s) withcolor darkgray ; ",xoffset+18,0,xoffset+18,-2.5)
234                            end
235                            local xoffset = 31 * 24
236                            context("draw (%s,%s) -- (%s,%s) withcolor darkgray ; ",xoffset,0,xoffset,-10)
237                        end
238
239                        for i=0,abstemp,5 do
240                            context([[draw textext.lft("%s") shifted (%s,%s) ; ]],i,-10,i/scale)
241                            context("draw (%s,%s) -- (%s,%s) withcolor .2white  ;",0,i/scale,31 * 24,i/scale)
242                        end
243
244                        context([[draw textext("\strut\month{%s}\enspace%s\enspace:\enspace%s") shifted (%s,%s) ; ]], m, y, where, 31 * 24 / 2, -50)
245                        context([[draw textext.lft("\textdegree{}C") shifted (%s,%s) ; ]],-10,-25)
246
247                        context("currentpicture := currentpicture xsized TextWidth") ;
248
249                        context.stopMPcode()
250                        context.stoplinecorrection()
251                        -- context.stopMPpage()
252                    end
253
254                    context.stoptitle()
255
256                end
257
258            end
259
260        end
261
262    end
263
264end
265
266\stopluacode
267
268\continueifinputfile{s-evohome.mkiv}
269
270\usemodule[art-01]
271
272\setuplayout
273  [article]
274  [footer=0cm,
275   header=1cm,
276   topspace=5mm,
277   bottomspace=5mm]
278
279\starttext
280
281    \startluacode
282
283     -- os.execute([[mtxrun --script evohome --collect --presets=c:/data/develop/domotica/code/evohome-presets.lua]])
284
285     -- local specification = {
286     --     year    = 2017,
287     --  -- month   = 11,
288     --     presets = "c:/data/develop/domotica/evohome/evohome-presets.lua",
289     -- }
290
291     -- moduledata.evohome.status (specification)
292     -- moduledata.evohome.history(specification)
293
294        local specification = {
295            year    = 2018,
296         -- month   = 11,
297            presets = "c:/data/develop/domotica/evohome/evohome-presets.lua",
298        }
299
300     -- moduledata.evohome.status (specification)
301        moduledata.evohome.history(specification)
302
303    \stopluacode
304
305\stoptext
306