-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtmlwriter.py
373 lines (347 loc) · 11.4 KB
/
htmlwriter.py
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
'''
Copyright © 2016 Daniel Müllner <http://danifold.net>
All changes from 2017-12-27 on: Copyright © Google Inc. <http://google.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import sys
if sys.hexversion < 0x03000000:
from ConfigParser import RawConfigParser
import Queue
else:
from configparser import RawConfigParser
import queue
import codecs
import datetime
import os
import shutil
import time
from ip import get_ip_address, get_wan_ip
from uptime import Uptime
from component import Component
progstart = Uptime()
DEBUG = False
config = RawConfigParser()
config.read('fancontrol.cfg')
pagefilename = config.get('webserver', 'page')
pagetempfilename = config.get('webserver', 'temppage')
datafilename = config.get('webserver', 'data')
datatempfilename = config.get('webserver', 'tempdata')
indexsource = config.get('webserver', 'indexsource')
indextarget = config.get('webserver', 'indextarget')
shutil.copyfile(indexsource, indextarget)
class Bunch:
def __init__(self, **kwds):
self.__dict__.update(kwds)
def CSSstyle(x):
c = Bunch()
c.rH = '' if x.rH==x.rH else 'color:red'
c.T = '' if x.T==x.T else 'color:red'
c.tau = '' if x.tau==x.tau else 'color:red'
return c
def prettyPrint(number):
return '{:2.1f}'.format(number).replace('-', u'−')
class PageGenerator:
def __init__(self):
self.statustxt = 'Status: Not set.'
self.statusstyle = 'color:red'
self.set_mode(None)
self.set_fanstate(None)
self.last_sync = None
self.S1 = Bunch(T=float('nan'), tau=float('nan'), rH=float('nan'))
self.S2 = Bunch(T=float('nan'), tau=float('nan'), rH=float('nan'))
def set_measurements(self, S1, S2):
self.S1 = S1
self.S2 = S2
def set_status(self, status):
self.statustxt = status[0]
self.statusstyle = status[1]
self.last_sync = status[2]
def set_fanstate(self, state):
if state == 'FanOn':
self.fanstatetxt = 'Fan: On.'
self.fanstatestyle = 'color:#FF8C00'
elif state == 'FanOff':
self.fanstatetxt = 'Fan: Off.'
self.fanstatestyle = ''
elif state == 'OpenWindow':
self.fanstatetxt = 'Window is being opened.'
self.fanstatestyle = 'color:#FF8C00'
elif state == 'CloseWindow':
self.fanstatetxt = 'Window is being closed.'
self.fanstatestyle = 'color:#FF8C00'
else:
self.fanstatetxt = 'Fan state: unknown.'
self.fanstatestyle = 'color:red'
def set_mode(self, mode):
if mode == 'manual':
self.modetxt = 'Mode: manual. '
else:
self.modetxt = ''
def write(self):
localtime = time.localtime()
with codecs.open(pagetempfilename, 'w', encoding='utf8') as f:
f.write(u'''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fan control</title>
<meta name="author" content="Daniel Müllner">
<script type="text/JavaScript">
function timedRefresh(timeoutPeriod) {{ setTimeout("location.reload(true);",timeoutPeriod); }}
window.onload = timedRefresh(10000);
</script>
</head>
<style>
body {{font-family: sans-serif;}}
h1 {{font-size: 125%;}}
table {{border:1px solid grey;
border-collapse:collapse;}}
td{{padding:4px 8px}}
tr.bg {{background-color:#ffb;}}
tr.hr {{border-bottom:1px solid black}}
td.l {{text-align:left;}}
td.c {{text-align:center;}}
td.r {{text-align:right;}}
</style>
<body>
<h1>Fan control</h1>
<table>
<tr>
<td colspan="3">
{date}<span style="float:right">{time}</span>
</td>
</tr>
<tr class="hr">
<td>
</td>
<td class="c">
Indoors
</td>
<td class="c">
Outdoors
</td>
</tr>
<tr class="bg">
<td>
Relative humidity in %
</td>
<td class="c" style="{S1Color.rH}">
{S1rH}
</td>
<td class="c" style="{S2Color.rH}">
{S2rH}
</td>
</tr>
<tr>
<td>
Temperature in °C
</td>
<td class="c" style="{S1Color.T}">
{S1T}
</td>
<td class="c" style="{S2Color.T}">
{S2T}
</td>
</tr>
<tr class="bg">
<td>
Dew point in °C
</td>
<td class="c" style="{S1Color.tau}">
{S1tau}
</td>
<td class="c" style="{S2Color.tau}">
{S2tau}
</td>
</tr>
<tr>
<td colspan="3" style="{fanstatestyle}">
{modetxt}{fanstatetxt}
</td>
</tr>
<tr class="bg hr">
<td colspan="3" style="{statusstyle}">
{statustxt}
</td>
</tr>
<tr>
<td colspan="3">
IP Ethernet: <span style="float:right">{IPeth0}</span>
</td>
</tr>
<tr class="bg">
<td colspan="3">
IP WLAN: <span style="float:right">{IPwlan0}</span>
</td>
</tr>
<tr>
<td colspan="3">
IP WAN: <span style="float:right">{IPwan}</span>
</td>
</tr>
<tr class="bg">
<td colspan="3">
OS uptime: <span style="float:right">{uptime}</span>
</td>
</tr>
<tr>
<td colspan="3">
Controller uptime: <span style="float:right">{progtime}</span>
</td>
</tr>
<tr class="bg">
<td colspan="3">
Last DCF77 signal: <span style="float:right">{lastsync}</span>
</td>
</tr>
</table>
</body>
</html>
'''.
format(date=time.strftime("%d.%m.%Y", localtime),
time=time.strftime("%H:%M:%S", localtime),
S1rH=prettyPrint(self.S1.rH),
S2rH=prettyPrint(self.S2.rH),
S1T=prettyPrint(self.S1.T),
S2T=prettyPrint(self.S2.T),
S1tau=prettyPrint(self.S1.tau),
S2tau=prettyPrint(self.S2.tau),
S1Color=CSSstyle(self.S1), S2Color=CSSstyle(self.S2),
statustxt = self.statustxt,
statusstyle = self.statusstyle,
modetxt = self.modetxt,
fanstatetxt = self.fanstatetxt,
fanstatestyle = self.fanstatestyle,
IPeth0=get_ip_address('eth0'),
IPwlan0=get_ip_address('wlan0'),
IPwan=get_wan_ip(),
uptime=str(datetime.timedelta(seconds=int(Uptime()))),
progtime=str(datetime.timedelta(seconds=int(Uptime() - progstart))),
lastsync=self.last_sync if self.last_sync else 'None')
)
os.rename(pagetempfilename, pagefilename)
def writedata(self):
localtime = time.localtime()
with codecs.open(datatempfilename, 'w', encoding='utf8') as f:
f.write(u'''\
{date}
{time}
{S1Color.rH}
{S1rH}
{S2Color.rH}
{S2rH}
{S1Color.T}
{S1T}
{S2Color.T}
{S2T}
{S1Color.tau}
{S1tau}
{S2Color.tau}
{S2tau}
{fanstatestyle}
{modetxt}{fanstatetxt}
{statusstyle}
{statustxt}
{IPeth0}
{IPwlan0}
{IPwan}
{uptime}
{progtime}
{lastsync}'''.
format(date=time.strftime("%d.%m.%Y", localtime),
time=time.strftime("%H:%M:%S", localtime),
S1rH=prettyPrint(self.S1.rH),
S2rH=prettyPrint(self.S2.rH),
S1T=prettyPrint(self.S1.T),
S2T=prettyPrint(self.S2.T),
S1tau=prettyPrint(self.S1.tau),
S2tau=prettyPrint(self.S2.tau),
S1Color=CSSstyle(self.S1), S2Color=CSSstyle(self.S2),
statustxt = self.statustxt,
statusstyle = self.statusstyle,
modetxt = self.modetxt,
fanstatetxt = self.fanstatetxt,
fanstatestyle = self.fanstatestyle,
IPeth0=get_ip_address('eth0'),
IPwlan0=get_ip_address('wlan0'),
IPwan=get_wan_ip(),
uptime=str(datetime.timedelta(seconds=int(Uptime()))),
progtime=str(datetime.timedelta(seconds=int(Uptime() - progstart))),
lastsync=self.last_sync if self.last_sync else 'None')
)
os.rename(datatempfilename, datafilename)
def writeEndPage(self):
localtime = time.localtime()
with open(pagetempfilename, 'w') as f:
f.write('''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fan control</title>
<meta name="author" content="Daniel Müllner">
<script type="text/JavaScript">
function timedRefresh(timeoutPeriod) {{ setTimeout("location.reload(true);",timeoutPeriod); }}
window.onload = timedRefresh(10000);
</script>
</head>
<style>
body {{font-family: sans-serif;}}
</style>
<body>
{date}, {time}: Program exited.
</body>
</html>
'''.
format(date=time.strftime("%d.%m.%Y", localtime),
time=time.strftime("%H:%M:%S", localtime))
)
os.rename(pagetempfilename, pagefilename)
class HtmlWriter(Component):
def __init__(self):
Component.__init__(self, 'HTML writer')
self.pageGenerator = PageGenerator()
self.oldstatus = (None, None)
def __enter__(self):
with self.lock:
self.messageboard.subscribe('Measurement', self, HtmlWriter.onMeasurement)
self.messageboard.subscribe('HTMLStatus', self, HtmlWriter.onHTMLStatus)
self.messageboard.subscribe('FanState', self, HtmlWriter.onFanState)
self.messageboard.subscribe('Mode', self, HtmlWriter.onMode)
return Component.__enter__(self)
def __exit__(self, exc_type, exc_value, traceback):
Component.__exit__(self, exc_type, exc_value, traceback)
with self.lock:
self.pageGenerator.writeEndPage()
def onMeasurement(self, message):
with self.lock:
self.pageGenerator.set_measurements(*message[1:])
#self.pageGenerator.write() # ???
self.pageGenerator.writedata()
def onHTMLStatus(self, message):
with self.lock:
self.pageGenerator.set_status(message)
#self.pageGenerator.write()
self.pageGenerator.writedata()
def onFanState(self, message):
with self.lock:
self.pageGenerator.set_fanstate(message)
#self.pageGenerator.write()
self.pageGenerator.writedata()
def onMode(self, message):
with self.lock:
self.pageGenerator.set_mode(message)
#self.pageGenerator.write()
self.pageGenerator.writedata()