-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
440 lines (375 loc) · 15.8 KB
/
main.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import os
import sys
import threading
import time
from datetime import datetime, date, timedelta
import paho.mqtt.client as mqtt
import rrdtool
from PIL import Image
import sunriseset
## We'll try to use the local caldav library, not the system-installed
sys.path.insert(0, '..')
import caldav
import icalendar
from calendarcredentials import url, username, password
def publishTime(client):
now = datetime.now()
client.publish("/inkplate/in/datetime", now.strftime("%Y %m %d %H %M %S"))
def publishTrains(client):
trains = "Abfahrten Gettorf@--:--@--:--@--:--@--:--"
try:
if os.path.exists("/tmp/station-Gettorf.txt"):
with open("/tmp/station-Gettorf.txt", "r") as f:
trains = f.readline ()
f.close()
except Exception as e:
print("error reading station file:", e)
client.publish ("/inkplate/in/station", trains, qos=1, retain=True)
def publishSunTimes(client):
sunTimes = "--:--@--:--@--:--@--:--@--:--"
try:
times=sun(lat=54.407777777778,long=9.9741666666667)
sunTimes = f"{times.sunrise()[:5]}@{times.solarnoon()[:5]}@{times.sunset()[:5]}@--:--@--:--"
client.publish("/inkplate/in/suntimes", sunTimes, qos=1, retain=True)
except:
pass
def publishLowest(client):
lowest = "--:--@???@--:--@???@???"
try:
try:
minTempTime, minTemperature, minPressTime, minPressure = get_lowest()
lowest = f"{minTempTime.strftime('%H:%M')[:5]}@{minTemperature:.1f}@{minPressTime.strftime('%H:%M')[:5]}@{minPressure:4.0f}@???"
except:
pass
client.publish("/inkplate/in/lowest", lowest, qos=1, retain=True)
except:
pass
def publishHighest(client):
lowest = "--:--@???@--:--@???@???"
try:
try:
maxTempTime, maxTemperature, maxPressTime, maxPressure = get_highest()
highest = f"{maxTempTime.strftime('%H:%M')[:5]}@{maxTemperature:.1f}@{maxPressTime.strftime('%H:%M')[:5]}@{maxPressure:4.0f}@???"
except:
pass
client.publish("/inkplate/in/lowest", lowest, qos=1, retain=True)
except:
pass
class TimeThread (threading.Thread):
# publish server time and station info (once per minute)
def run(self):
lastHour = -1
while True:
now = datetime.now()
if now.hour != lastHour:
lastHour = now.hour
fullUpdate(client)
pictureUpdate()
publishTrains(client)
publishTime(client)
time.sleep(60)
def send_config(client):
def send_udpaterates(client):
now = datetime.now()
clockupdate = 3600 # clock and data updates hourly, pad checked hourly
dataupdate = 1
padchecktime = 3600
if now.hour in range(4, 8): # clock is up to date, data max 5 mins old, pad checked every 10 secs
clockupdate = 60
dataupdate = 5
padchecktime = 10
elif now.hour in range(8, 22): # clock is up to date, data max 30 mins old, pad checked every 60 secs
clockupdate = 300 # 60
dataupdate = 30
padchecktime = 60
client.publish("/inkplate/in/clockupdate", str(clockupdate), qos=1, retain=True) # in seconds, must be >= padchecktime
client.publish("/inkplate/in/dataupdate", str(dataupdate), qos=1, retain=True) # in clockupdate-cycles
client.publish("/inkplate/in/padchecktime", "10", qos=1, retain=True) # in seconds, must be >= 2s!
send_udpaterates(client)
# WLAN times order scheme:
# 00:00:00 < wlan-on < wlan-off <= 23:59:59
client.publish("/inkplate/in/wlan-off", "22:00:00", qos=1, retain=True) # next WLAN turn off time
client.publish("/inkplate/in/wlan-on", "05:30:00", qos=1, retain=True) # next WLAN turn on time
def on_connect(client, userdata, flags, rc):
client.subscribe("/inkplate/out/#")
def on_message(client, userdata, message):
topic = message.topic.split("/")[3]
topicDict = {}
topicDict["buttons"] = handleButtons
topicDict["menulevel"] = handleMenuLevel
topicDict["battery"] = handleBattery
topicDict["temperature"] = handleTemperature
topicDict["pressure"] = handlePressure
topicDict["humidity"] = handleHumidity
topicDict["reset"] = handleReset
handler = topicDict.get(topic)
if handler:
handler(client, message.payload)
else:
print("Unknown topic coming in (else):", message.topic)
def handleButtons(aClient, aButtons):
buttons = int(aButtons)
# if buttons & 1:
# print("pad 1 touched")
# if buttons & 2:
# print("pad 2 touched")
# if buttons & 4:
# print("pad 3 touched")
def handleMenuLevel(aClient, aMenuLevel):
menuLevel = int(aMenuLevel)
print("Menu level %d" % menuLevel)
def handleBattery(aClient, aBattery):
battery = float(aBattery)
def handleTemperature(aClient, aTemperature):
temperature = float(aTemperature)
def handlePressure(aClient, aPressure):
pressure = float(aPressure)
def handleHumidity(aClient, aHumidity):
humidity = float(aHumidity)
def handleReset(aClient, aReset):
fullUpdate(aClient)
def fullUpdate(aClient):
def publishEnvSensors(which):
def appendFile(filename, digits):
f = open(filename, "r")
value = float(f.readline())
f.close()
if (digits == 1):
message = "{:.1f}".format (value) + "@"
elif (digits == 2):
message = "{:.2f}".format (value) + "@"
else:
message = str(value) + "@"
return message
message = ""
path = "/tmp/weather"
if which == "indoor":
path = "/tmp/inkplate"
elif which == "outdoor":
path = "/tmp/weather"
else:
return
if not os.path.exists(path):
message = "<Keine Daten>"
else:
message = appendFile(path + "/temperature", 1) \
+ appendFile(path + "/pressure", 1) \
+ appendFile(path + "/humidity", 1) \
+ appendFile(path + "/battery", 2)
modificationTime = time.localtime(os.path.getmtime(path + "/temperature"))
message = message.replace (" ", "")
message += time.strftime("%d.%m._%H:%M", modificationTime)
aClient.publish("/inkplate/in/"+which, message, qos=1, retain=True)
def publishPower():
try:
today = datetime.now()
database = "/srv/dev-disk-by-label-DISK1/localdata/powermeter.rrd"
max = rrdtool.fetch (database, "MAX", "--start=" + today.strftime("%Y%m%d"), "--end=" + today.strftime('%Y%m%d'))
yesterday = today - timedelta (days = 1)
min = rrdtool.fetch (database, "MAX", "--start=" + yesterday.strftime("%Y%m%d"), "--end=" + yesterday.strftime('%Y%m%d'))
day = yesterday.strftime ("%d.%m.")
consumption = max[2][0][0] - min[2][0][0]
assessmentConsumption = 0
if (consumption < 0.001):
assessmentConsumption = "2"
elif (consumption < 1):
assessmentConsumption = "1"
elif (consumption < 5.0):
assessmentConsumption = "0"
elif (consumption < 7.0):
assessmentConsumption = "1"
else:
assessmentProduction = "2"
production = max[2][0][1] - min[2][0][1]
assessmentProduction = 0
if (production < 0.001):
if production < 0.0:
production = 0.0
assessmentProduction = "-2"
elif (production < 0.1):
assessmentProduction = "-1"
elif (production < 0.5):
assessmentProduction = "0"
elif (production < 0.9):
assessmentProduction = "1"
else:
assessmentProduction = "2"
if (consumption < 10.0):
aClient.publish ("/inkplate/in/power", f"{day}@{consumption:.3f}@{assessmentConsumption}@{production:.3f}@{assessmentProduction}", qos=1, retain=True)
else:
aClient.publish ("/inkplate/in/power", f"{day}@{consumption:.2f}@{assessmentConsumption}@{production:.3f}@{assessmentProduction}", qos=1, retain=True)
except:
aClient.publish ("/inkplate/in/power", "[email protected]@[email protected]@0", qos=1, retain=True)
def publishMOTD():
def getEvents(my_principal, calname, calid, offset, duration):
calendars = my_principal.calendars()
my_calendar = my_principal.calendar(name=calname, cal_id=calid)
events_fetched = my_calendar.date_search(
start=date.today() + timedelta(days=offset), end=date.today() + timedelta(days=offset + duration), expand=True)
results = []
for e in events_fetched:
cal = icalendar.Calendar.from_ical(e._get_data())
ev = cal.walk("VEVENT")[0]
no_time = False
date_override = False
time = ""
try:
# RDATE not handled yet!
rule = ev["RRULE"]
# FREQ:YEARLY BYMONTH:<int> BYMONTHDAY:<int>
# FREQ:WEEKLY BYDAY:<char2>
try:
freq = rule["FREQ"] # req! SECONDLY, MINUTELY, HOURLY, DAILY, WEEKLY, MONTHLY, YEARLY
# INTERVAL: <digit> factor for FREQ
except KeyError:
pass
if "WEEKLY" in freq:
try:
byday = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'].index(rule["BYDAY"][0])
startDayOfWeek = (date.today() + timedelta(days=offset)).isoweekday()
if byday < startDayOfWeek:
byday += 7
calcday = date.today() + timedelta(days=offset + byday - startDayOfWeek)
time = calcday.strftime ("%d.%m.")
date_override = True
# or BYSECOND, BYMINUTE, BYHOUR (CSL, comma-separated list)
# BYDAY (SU, MO, TU, WE, TH, FR, SA) (CSL), +-<integer>: first or last of ...
# BYMONTHDAY CSL of days of month, -<integer> last of ...
# BYYEARDAY CSL of days of year, -<integer> last of ...
# BYWEEKNO CSL of weeks of year, -<integer> last of ...
# BYMONTH CSL of months (1..12)
# WKST: first work way of week (MO, TU, ... SU)
except KeyError:
pass
elif "YEARLY" in freq:
try:
print ("yearly")
bymonth = rule["BYMONTH"]
bymonthday = rule["BYMONTHDAY"]
calcday = datetime(year=date.today().year, month=bymonth[0], day=bymonthday[0]);
time = calcday.strftime ("%d.%m.")
date_override = True
no_time = True
except KeyError:
pass
else:
time = "<????>"
date_override = True
except KeyError:
pass
#vPeriod: start, end, by_duration, duration
#vRecur: ?! RRULE
#vGeo: latitude, longitude
#vUTCOffset: td
try:
if ev["DURATION"].dt >= timedelta (days=1):
no_time = True
except KeyError:
pass
try:
if not date_override:
time = ev["DTSTART"].dt.strftime ("%d.%m.")
if not no_time:
time += ev["DTSTART"].dt.strftime (" %H:%M")
except KeyError:
pass
try:
if not no_time:
time += "-" + ev["DTEND"].dt.strftime ("%H:%M")
except KeyError:
pass
summary = ""
try:
summary = ev["DESCRIPTION"].to_ical().decode("utf-8")
except KeyError:
pass
try:
summary = ev["SUMMARY"].to_ical().decode("utf-8")
except KeyError:
pass
if len(time) > 0:
results += [time + ": " + summary]
else:
results += [summary]
results.sort()
return results
client = caldav.DAVClient(url=url, username=username, password=password)
my_principal = client.principal()
results = []
results += getEvents (my_principal, "Müllabfuhr", "4f7cdade-64f0-9d35-ae78-4bd84ec8d89b", +1, 1) # tomorrow
results += getEvents (my_principal, "Geburtstage", "3b626971-7de3-f82a-1899-8fe2d85c04e1", 0, 1) # today
line1 = ""
line2 = ""
print (results)
if len(results) == 1:
line1 = results[0]
elif len(results) == 2:
line1 = results[0]
line2 = results[1]
elif len(results) == 3:
line1 = results[0] + " / " + results[1]
line2 = results[2]
elif len(results) == 4:
line1 = results[0] + " / " + results[1]
line2 = results[2] + " / " + results[3]
elif len(results) > 4:
line1 = results[0] + " / " + results[1]
line2 = results[2] + " / ... weitere ..."
aClient.publish ("/inkplate/in/motd", f"{line1}@{line2}", qos=1, retain=True)
send_config(aClient)
print ("handleReset")
publishTime(client)
print (". time")
publishEnvSensors("indoor")
print (". indoor")
publishEnvSensors("outdoor")
print (". outdoor")
publishSunTimes(client)
print (". rise/set")
publishLowest(client)
print (". lowest")
publishHighest(client)
print (". highest")
publishPower()
print (". power")
publishTrains(client)
print (". trains")
publishMOTD()
print (". motd")
print ("... done")
def pictureUpdate():
try:
image = Image.new('1', (800, 386))
humidity = Image.open("/tmp/humidity.gif").convert(mode = "1", dither=Image.NONE)
image.paste(humidity, (383, 0))
temperature = Image.open("/tmp/temperature.gif").convert(mode = "1", dither=Image.NONE)
image.paste(temperature, (-17, 0))
image.save("/sharedfolders/inkplate/content-1.bmp")
image = Image.new('1', (800, 386))
battery = Image.open("/tmp/battery.gif").convert("1", dither=Image.NONE)
image.paste(battery, (383, 0))
pressure = Image.open("/tmp/pressure.gif").convert("1", dither=Image.NONE)
image.paste(pressure, (-17, 0))
image.save("/sharedfolders/inkplate/content-11.bmp")
image = Image.new('1', (800, 386))
power = Image.open("/tmp/allday.png")
width, height = power.size
for x in range(width):
for y in range(height):
r, g, b = power[x, y]
if g > 10 and r < 245:
power[x, y] = (0, 255, 0)
image.paste(power.convert("1", dither=Image.NONE), (0, -29))
image.save("/sharedfolders/inkplate/content-3.bmp")
except:
pass
if __name__ == '__main__':
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("mediaberry")
fullUpdate(client)
pictureUpdate()
send_time = TimeThread()
send_time.start()
client.loop_forever()