-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoboHUB.py
executable file
·1328 lines (1162 loc) · 60.6 KB
/
KoboHUB.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from cgitb import small
import configparser
from dataclasses import dataclass
from operator import ne
from typing import List
import socket
import tempfile
import time
from datetime import datetime, date, timedelta
from subprocess import call
from sys import platform
import os
from xml.etree.ElementInclude import include
from PIL import Image, ImageDraw, ImageFont
from PIL.ImageFont import FreeTypeFont
from PIL.Image import Image as PilImage
from weather import weather_current, weather_forecast, koboHUBWeather
from garbage_schedule import get_garbage_status, get_garbage_config_data
from getcomic import getComicFile
from generic_transit import next_transit
from transit import gettransitdepartures
from getquote import quoteoftheday
try:
from _fbink import ffi, lib as fbink
except ImportError:
from fbink_mock import ffi, lib as fbink
CONFIGFILE = "config.ini"
#Preset of global variable
kobo_blight = 0
os.system("fbink -q --clear --flash")
def wait_for_wifi():
while True:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip_addr = s.getsockname()[0]
return ip_addr
except Exception as e:
print("exc. ignored {}".format(e))
os.system("reboot")
time.sleep(15)
def get_screen_ref_data(file_path:str):
if os.path.exists(file_path) :
parser = configparser.RawConfigParser()
parser.read(file_path)
data = dict()
data['times_id'] = parser.get("screen-refresh-schedule", "times")
data['screen_rate_id'] = parser.get("screen-refresh-schedule", "screen_rate")
data['backlit_setting_id'] = parser.get("screen-refresh-schedule", "backlit_setting")
return data
else :
data = dict()
data['times_id'] = "0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
data['screen_rate_id'] = "60,60,60,60,65,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60"
data['backlit_setting_id'] = "0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
return data
def get_config_data(file_path:str, show_icons:int):
screen_orientation = get_screen_orientation()
"""turns the config file data into a dictionary"""
parser = configparser.RawConfigParser()
parser.read(file_path)
data = dict()
data['ow-api-key_id'] = parser.get("koboHUB", "ow-api-key")
data['ow-city_id'] = parser.get("koboHUB", "ow-city")
data['ow-units_id'] = parser.get("koboHUB", "ow-units")
data['ow-language_id'] = parser.get("koboHUB","ow-language")
data['show-comic_id'] = parser.get("koboHUB","show-comic")
data['comic_id'] = parser.get("koboHUB", "comic")
data['comic_morning_id'] = parser.get("koboHUB", "comic-morning")
data['comic_midday_id'] = parser.get("koboHUB", "comic-midday")
data['comic_afternoon_id'] = parser.get("koboHUB", "comic-afternoon")
data['comic_evening_id'] = parser.get("koboHUB", "comic-evening")
data['transit-feature_id'] = parser.get("koboHUB","transit-feature")
data['transit-icon_id'] = parser.get("koboHUB","transit-icon")
data['transit-id_id'] = parser.get("koboHUB","transit-id")
data['transit-stop-name_id'] = parser.get("koboHUB","transit-stop-name")
data['use-transit-api_id'] = parser.get("koboHUB","use-transit-api")
data['transit-apikey_id'] = parser.get("koboHUB","transit-apikey")
data['transit-stop_id'] = parser.get("koboHUB","transit-stop")
data['use-generic-transit_id'] = parser.get("koboHUB","use-generic-transit")
data['generic-transit-timeframe_id'] = parser.get("koboHUB","generic-transit-timeframe")
data['live-cutin-time-start_id'] = parser.get("koboHUB","live-cutin-time-start")
data['live-cutin-time-stop_id'] = parser.get("koboHUB","live-cutin-time-stop")
data['quote-of-the-day-show_id'] = parser.get("koboHUB","quote-of-the-day-show")
data['quote-of-the-day-max-lenght_id'] = parser.get("koboHUB","quote-of-the-day-max-lenght")
data['screen-orientation_id'] = parser.get("koboHUB","screen-orientation")
data['garbage-show_id'] = parser.get("koboHUB","garbage-show")
data['koboHUB-refresh-seconds_id'] = parser.get("koboHUB","koboHUB-refresh-seconds")
data['koboHUB-var-refresh_id'] = parser.get("beta","koboHUB-var-refresh")
data['koboHUB-backlight_id'] = parser.get("koboHUB","koboHUB-backlight")
if show_icons == 1:
if screen_orientation == "LANDSCAPE" :
config_x = 20
else :
config_x = 10
config_y = 10
image_file = 'icons/loading_icons_openweather.png'
if data['ow-units_id'] == "metric":
image_file = 'icons/loading_icons_openweather_metric.png'
if data['ow-units_id'] == "imperial":
image_file = 'icons/loading_icons_openweather_imperial.png'
os_cmd ="fbink -q --image file="+image_file+",x="+str(config_x)+",y="+str(config_y)+" > /dev/null"
os.system(os_cmd)
config_x = config_x + 84
if data['show-comic_id'] == "TRUE":
image_file = 'icons/loading_icons_gocomics.png'
os_cmd ="fbink -q --image file="+image_file+",x="+str(config_x)+",y="+str(config_y)+" > /dev/null"
os.system(os_cmd)
config_x = config_x + 84
time.sleep(1)
if data['transit-feature_id'] == "TRUE":
image_file = 'icons/loading_icons_transit.png'
os_cmd ="fbink -q --image file="+image_file+",x="+str(config_x)+",y="+str(config_y)+" > /dev/null"
os.system(os_cmd)
config_x = config_x + 84
time.sleep(1)
if data['use-transit-api_id'] == "TRUE":
image_file = 'icons/loading_icons_transit_app.png'
os_cmd ="fbink -q --image file="+image_file+",x="+str(config_x)+",y="+str(config_y)+" > /dev/null"
os.system(os_cmd)
config_x = config_x + 84
time.sleep(1)
if data['garbage-show_id'] == "TRUE" :
image_file = 'icons/loading_icons_trash.png'
os_cmd ="fbink -q --image file="+image_file+",x="+str(config_x)+",y="+str(config_y)+" > /dev/null"
os.system(os_cmd)
config_x = config_x + 84
time.sleep(1)
return data
@dataclass
class box_descriptor:
pos_x: int
pos_y: int
width: int
height: int
@dataclass
class boxes:
current: box_descriptor
today: box_descriptor
tomorrow: box_descriptor
next_days: List[box_descriptor]
@dataclass
class fonts:
micro: FreeTypeFont
tiny: FreeTypeFont
small: FreeTypeFont
medium: FreeTypeFont
quote: FreeTypeFont
weather_stats: FreeTypeFont
weather_stats_bold: FreeTypeFont
comfort_small: FreeTypeFont
comfort: FreeTypeFont
comfortB: FreeTypeFont
comfortBH: FreeTypeFont
larger : FreeTypeFont
SFCompact_tiny : FreeTypeFont
SFCompact_tinyB : FreeTypeFont
SFCompact : FreeTypeFont
SFCompact_Big : FreeTypeFont
bus_times_font : FreeTypeFont
@dataclass
class icons:
wind: PilImage
humidity: PilImage
temperature: PilImage
@dataclass
class data:
current: weather_current
forecast: List[weather_forecast]
@dataclass
class garbage_summary:
landfill: bool
recycle: bool
xmas_tree: bool
landfill_prepapre : bool
recycle_prepare : bool
dumpster : bool
dumpster_prepapre : bool
class koboHUB:
# BORDER, in pixels, so we don't draw too close to the edges
BORDER = 10
def __init__(self):
# config from the file
self.cfg_data = dict()
cfg_file_data = get_config_data(CONFIGFILE,0)
self.cfg_data['ow-api-key'] = cfg_file_data['ow-api-key_id']
self.cfg_data['ow-city'] = cfg_file_data['ow-city_id']
self.cfg_data['ow-units'] = cfg_file_data['ow-units_id']
self.cfg_data['ow-language'] = cfg_file_data['ow-language_id']
self.cfg_data['show-comic'] = cfg_file_data['show-comic_id']
self.cfg_data['comic'] = cfg_file_data['comic_id']
self.cfg_data['comic-morning'] = cfg_file_data['comic_morning_id']
self.cfg_data['comic-midday'] = cfg_file_data['comic_midday_id']
self.cfg_data['comic-afternoon'] = cfg_file_data['comic_afternoon_id']
self.cfg_data['comic-evening'] = cfg_file_data['comic_evening_id']
self.cfg_data['transit-feature'] = cfg_file_data['transit-feature_id']
self.cfg_data['transit-icon'] = cfg_file_data['transit-icon_id']
self.cfg_data['transit-id'] = cfg_file_data['transit-id_id']
self.cfg_data['transit-stop-name'] = cfg_file_data['transit-stop-name_id']
self.cfg_data['use-transit-api'] = cfg_file_data['use-transit-api_id']
self.cfg_data['transit-apikey'] = cfg_file_data['transit-apikey_id']
self.cfg_data['transit-stop'] = cfg_file_data['transit-stop_id']
self.cfg_data['use-generic-transit'] = cfg_file_data['use-generic-transit_id']
self.cfg_data['generic-transit-timeframe'] = cfg_file_data['generic-transit-timeframe_id']
self.cfg_data['live-cutin-time-start'] = cfg_file_data['live-cutin-time-start_id']
self.cfg_data['live-cutin-time-stop'] = cfg_file_data['live-cutin-time-stop_id']
self.cfg_data['quote-of-the-day-show'] = cfg_file_data['quote-of-the-day-show_id']
self.cfg_data['quote-of-the-day-max-lenght'] = cfg_file_data['quote-of-the-day-max-lenght_id']
self.cfg_data['screen-orientation'] = cfg_file_data['screen-orientation_id']
self.cfg_data['garbage_schedule'] = cfg_file_data['garbage-show_id']
self.cfg_data['koboHUB-backlight'] = cfg_file_data['koboHUB-backlight_id']
cfg_file_data = get_screen_ref_data("koboHUB_refresh_schedule.ini")
self.times_data = cfg_file_data['times_id'].split(",")
self.screen_refresh_data = cfg_file_data['screen_rate_id'].split(",")
self.backlit_settings_data = cfg_file_data['backlit_setting_id'].split(",")
# fbink configuration
self.fbink_cfg = ffi.new("FBInkConfig *")
self.fbink_cfg.is_centered = True
self.fbink_cfg.is_halfway = True
self.fbink_cfg.is_cleared = True
self.fbfd = fbink.fbink_open()
fbink.fbink_init(self.fbfd, self.fbink_cfg)
state = ffi.new("FBInkState *")
fbink.fbink_get_state(self.fbink_cfg, state)
if "linux" in platform:
self.screen_size = get_kobo_screen_size()
self.screen_size = (state.view_width, state.view_height)
else:
self.screen_size = (768, 1024)
# app configuration
self.ip_address = "1.1.1.1"
# weather class instance
try:
self.weather_fetcher = koboHUBWeather(self.cfg_data)
self.weather = data(current=self.weather_fetcher.get_weather_current(),
forecast=self.weather_fetcher.get_weather_forecast())
except Exception as e:
print("Weather: Error getting weather")
print(e)
fbink.fbink_close(self.fbfd)
# configuration for the image
# Boxes positions
# current condition
current = box_descriptor(0, 0, int(self.screen_size[0]), 350)
# today's forecast
today = box_descriptor(current.width, 0, self.screen_size[0] - current.width, current.height)
# tomorrow
tomorrow = box_descriptor(0, current.height, int( self.screen_size[0]) , 200)
# next 3 days
next_day0 = box_descriptor(0, ( current.height + tomorrow.height ), int(self.screen_size[0]), int( self.screen_size[1] - current.height - tomorrow.height))
next_day1 = box_descriptor(next_day0.width, next_day0.pos_y, next_day0.width, next_day0.height)
next_day2 = box_descriptor(next_day1.pos_x + next_day1.width, next_day1.pos_y, next_day1.width, next_day1.height)
self.boxes = boxes(current, today, tomorrow, [next_day0, next_day1, next_day2])
# fonts
# tiny: used on the weather condition for the next days and ip address
# small: used on the headers and most stuff on the current conditions
# comfort: temperatures (gets scaled according to the box)
# big: for the current temperature
self.fonts = fonts(micro=ImageFont.truetype("fonts/Fabrica.otf", 16),
tiny=ImageFont.truetype("fonts/segoe-ui.ttf", 22),
small=ImageFont.truetype("fonts/Fabrica.otf", 26),
weather_stats=ImageFont.truetype("fonts/SF-Compact-Rounded-Regular.ttf", 26),
weather_stats_bold=ImageFont.truetype("fonts/SF-Compact-Rounded-Bold.ttf", 26),
medium=ImageFont.truetype("fonts/SF-Compact-Rounded-Regular.ttf", int(self.screen_size[0] / 22)),
quote=ImageFont.truetype("fonts/SF-Compact-Text-ThinItalic.ttf",int(self.screen_size[0] / 22) ),
comfort=ImageFont.truetype("fonts/Comfortaa-Regular.ttf", int(self.screen_size[0] / 8)),
comfort_small=ImageFont.truetype("fonts/SF-Compact-Rounded-Bold.ttf", int(self.screen_size[0] / 18)),
comfortBH=ImageFont.truetype("fonts/Mont-Heavy-Bold.otf", int(self.screen_size[0] / 10)),
comfortB=ImageFont.truetype("fonts/Comfortaa-Bold.ttf", int(self.screen_size[0] /14)),
SFCompact_tiny=ImageFont.truetype("fonts/SF-Compact-Rounded-Regular.ttf", int(self.screen_size[0] / 26)),
SFCompact_tinyB=ImageFont.truetype("fonts/SF-Compact-Rounded-Bold.ttf", int(self.screen_size[0] / 26)),
SFCompact=ImageFont.truetype("fonts/SF-Compact-Rounded-Bold.ttf", int(self.screen_size[0] / 28)),
SFCompact_Big=ImageFont.truetype("fonts/SF-Compact-Rounded-Bold.ttf", int(self.screen_size[0] / 8)),
bus_times_font=ImageFont.truetype("fonts/RobotoMono-Bold.ttf", int(self.screen_size[0] / 22) ),
larger=ImageFont.truetype("fonts/segoe-ui.ttf", int(self.screen_size[0] / 20)))
self.daycount = datetime.now().strftime("%d")
self.daycount = "0"
# icons
self.icons = icons(wind=Image.open('icons/w.png'),
humidity=Image.open('icons/h.png'),
temperature=Image.open('icons/C.png'))
def _create_image(self) -> str:
global HOURGLASS, gTy, quote
screen_rotation = get_screen_orientation()
today = self.weather.forecast[0]
tomorrow = self.weather.forecast[1]
days = self.weather.forecast[2:]
currenttime = datetime.now()
X_OFFSET = 0
if screen_rotation == "LANDSCAPE" :
X_OFFSET = 10
#Pushing the screen slightly to the right as rotation has a left shift it seems
print("KoboHUB: Preparing dashboard image . . .")
# 758 x 1024
WIDTH = int(self.screen_size[0])
HEIGHT = int(self.screen_size[1])
white = 255
black = 0
gray = 132
img = Image.new('L', (WIDTH, HEIGHT), color=white)
draw = ImageDraw.Draw(img, 'L')
# Find out how many characters per line of screen for garbage text
test_t = "H"
test_w_max = int(self.screen_size[0] - 200)
text_c = 0
test_t_w = 10
while test_t_w < test_w_max :
test_t = test_t + "H"
test_t_w, test_t_h = draw.textsize(test_t, font=self.fonts.medium)
#print("Quote Feature : Max text width is "+str(test_t_w)+" number of chars "+str(len(test_t)))
self.max_chars = test_t_w
# Find out how many characters per line of screen for Quotes
test_t = "H"
test_w_max = int(self.screen_size[0] - 200)
text_c = 0
test_t_w = 0
while test_t_w < test_w_max :
test_t = test_t + "H"
test_t_w, test_t_h = draw.textsize(test_t, font=self.fonts.quote)
#print("Quote Feature : Max text width is "+str(test_t_w)+" number of chars "+str(len(test_t)))
self.max_charsQ = test_t_w
# Dividing lines
# under today/current
draw.line([(self.BORDER, self.boxes.current.height), (WIDTH - self.BORDER, self.boxes.current.height)], gray)
# between today/current | DISABLED
# draw.line([(self.boxes.current.width, self.BORDER), (self.boxes.current.width, self.boxes.current.height - self.BORDER)], gray)
# under tomorrow
# draw.line([(self.BORDER, self.boxes.next_days[0].pos_y), (WIDTH - self.BORDER, self.boxes.next_days[0].pos_y)], gray)
#Below comic line
# draw.line([(self.BORDER, self.boxes.next_days[1].pos_y), (WIDTH - self.BORDER, self.boxes.next_days[1].pos_y)], gray)
# Decide if we want to draw more lines for weather or leave it for Garbage area.
# Current conditions
# City Name, Country Code, Day, Time
header_Day = datetime.now().strftime("%A")
header_Month_Date = datetime.now().strftime("%b %-d")
header_w, header_h = draw.textsize(header_Day, font=self.fonts.comfortBH)
header_w2, header_h2 = draw.textsize(header_Month_Date, font=self.fonts.comfortB)
# Show Day big letters and Month date ontop Banner
# Draw day big letters in gray
x = 10 + X_OFFSET
y = 10
draw.text((x, y), header_Day, font=self.fonts.comfortBH, fill=gray)
# Draw Month and date Black on top
xoff = X_OFFSET + ( int(header_w2) / 2)
x = int(header_w) - xoff
y = (int(header_h) / 2) + 10
draw.text((x, y), header_Month_Date, font=self.fonts.comfortB, fill=black)
# Current condition mini icon
condition = Image.open(self.weather.current.icon)
condition = condition.resize((int(condition.size[0] /1.2), int(condition.size[1] / 1.2)))
x_icon_mini = int (x + header_w2) + 5
y_icon_mini = 15
img.paste(condition, (x_icon_mini,y_icon_mini))
#Transit schedule#
###########
#############
## BUS ##
## ##
#############
### ###
if self.cfg_data['transit-feature'] == "TRUE" :
transit_icon_file = "Icons/buss_80x80.png"
if self.cfg_data['transit-icon'] =="BUS":
transit_icon_file = "Icons/buss_80x80.png"
if self.cfg_data['transit-icon'] =="METRO":
transit_icon_file = "Icons/metro_80x80.png"
if self.cfg_data['transit-icon'] =="TRAM":
transit_icon_file = "Icons/tram_80x80.png"
transit_icon = Image.open(transit_icon_file)
bx = self.screen_size[0] - (transit_icon.size[0] + 50)
by = 70
# blx = bx + ( int(transit_icon.size[0]) - int(bus_live_icon.size[0]/2)) + 5
# bly = by - (int(bus_live_icon.size[1]/2))
img.paste(transit_icon,(bx,by))
x = bx + 20
y = by + 3
bus_t_w, bus_t_h = draw.textsize(self.cfg_data['transit-id'], font=self.fonts.larger)
x = bx + ( int(transit_icon.size[0]/2) - int(bus_t_w/2) )
draw.text((x, y), self.cfg_data['transit-id'], font=self.fonts.larger, fill=black)
stop_t_w, stop_t_h = draw.textsize(self.cfg_data['transit-stop-name'], font=self.fonts.micro)
x = (bx + int((transit_icon.size[0]/2)) ) - int( (stop_t_w/2) )
y = by + int( transit_icon.size[1] + 4 )
draw.text((x, y), self.cfg_data['transit-stop-name'], font=self.fonts.micro, fill=black)
# LIVE Bus Schedule
bus_times = []
get_live_bus_times = []
#Decide here to call the STM API - maybe only mon-fry and 6 to 11?
if self.cfg_data['use-transit-api'] == "TRUE":
print("Transit API :Feature ON")
if currenttime > currenttime.replace(hour=int(self.cfg_data['live-cutin-time-start']), minute=0) and currenttime <= currenttime.replace(hour=int(self.cfg_data['live-cutin-time-stop']), minute=0) or self.cfg_data['generic-transit-timeframe'] == "FALSE":
print("Transit API :Getting LIVE transit times...")
bus_time_status_icon = Image.open("icons/live24x24.png")
get_live_bus_times = gettransitdepartures(datetime.now(),self.cfg_data['transit-apikey'],self.cfg_data['transit-stop'])
if len(get_live_bus_times) == 0:
print("Transit API :Error getting live times...")
else:
#Convert the datetime to HH:MM array here.
for i in get_live_bus_times :
bus_times.append(datetime.fromtimestamp(i).strftime('%H:%M'))
if self.cfg_data['use-generic-transit'] == "TRUE" and len(get_live_bus_times) == 0:
#Time Table - Hard coded in generic_transit.py - returned as HH:MM arrays no conversion needed.
print("Transit (Static) :Using static transit time table data")
bus_time_status_icon = Image.open("icons/not_live24x24.png")
bus_times = next_transit()
b = len(bus_times)
if len(get_live_bus_times) :
print("Transit Feature :"+str(len(get_live_bus_times))+" Live times found")
else:
print("Transit Feature :"+str(b)+" Bus schedules found")
bc = 0
bus_w, bus_h = draw.textsize("00:00", font=self.fonts.bus_times_font)
x = (bx + int((transit_icon.size[0]/2)) ) - int( (bus_w/2) )
y = y + 10
blx = x + (int(bus_w)+ 5)
bly = y + 6
if b > 0 :
while bc < 3 :
draw.text((x, y), bus_times[bc], font=self.fonts.bus_times_font, fill=black)
img.paste(bus_time_status_icon,(blx,bly))
bc = bc +1
y = y + (bus_h)
bly = bly + bus_h
if b == 0 :
draw.text((x, y), "--:--", font=self.fonts.bus_times_font, fill=black)
else:
print("Transit Feature :OFF")
###
### ###
###
###
### ###
###
# Draw actual like temp middle of screen large
temp_w, temp_h = draw.textsize(str(round(self.weather.current.temperature,1 ))+"\N{DEGREE SIGN}", font=self.fonts.SFCompact_Big)
x = (self.screen_size[0]/2)-(temp_w/2)
y = 120
draw.text((x, y), str(round(self.weather.current.temperature, 1))+"\N{DEGREE SIGN}", font=self.fonts.SFCompact_Big, fill=black)
# condition description - under the current temp
cond_w, cond_h = draw.textsize(self.weather.current.condition.capitalize(), font=self.fonts.larger)
x = (self.screen_size[0]/2)-(cond_w/2)
y = y + (temp_h + 10)
draw.text((x, y), self.weather.current.condition.capitalize(), font=self.fonts.larger, fill=black)
# Combined H / L
today_cond_combined_temp = "H: "+str(round(today.high,1))+"\N{DEGREE SIGN}"+" L: "+str(round(today.low,1))+"\N{DEGREE SIGN}"
condition_w, condition_h = draw.textsize(today_cond_combined_temp, font=self.fonts.SFCompact)
x = (self.screen_size[0]/2)-(condition_w/2)
y = y + (cond_h + 10)
draw.text((x, y), today_cond_combined_temp, font=self.fonts.SFCompact, fill=gray)
### METRICS TO THE LEFT
#Set X Y for site weather stats
x = 20 + X_OFFSET
y = 100
# Thermometer
weather_font = self.fonts.weather_stats
thermometer_icon = Image.open("Icons/t.png")
if self.weather.current.feelslike <= -5:
thermometer_icon = Image.open("Icons/temp_low.png")
weather_font = self.fonts.weather_stats_bold
if self.weather.current.feelslike > -1 and self.weather.current.temperature < 20:
thermometer_icon = Image.open("Icons/t.png")
if self.weather.current.feelslike >= 21 :
thermometer_icon = Image.open("Icons/temp_high.png")
weather_font = self.fonts.weather_stats_bold
# Draw feels like temp next to icon
img.paste(thermometer_icon,(x,y))
xt = x + thermometer_icon.size[0] + 5
yt = y -4
draw.text((xt, yt), "Feels like", font=weather_font, fill=gray)
yt = yt + 24
draw.text((xt, yt), str(round(self.weather.current.feelslike, 1))+"\N{DEGREE SIGN}", font=weather_font, fill=black)
'''
# Draw feels like temp next to icon
img.paste(thermometer_icon,(x,y))
xt = x + thermometer_icon.size[0] + 5
yt = y + 10
draw.text((xt, yt), str(round(self.weather.current.feelslike, 1))+"\N{DEGREE SIGN}", font=weather_font, fill=black)
'''
# Step down for next values
y = y + 50
# pressure icon
weather_font = self.fonts.weather_stats
if self.weather.current.pressure <= 990 :
pressure_icon = Image.open("icons/p_low.png")
weather_font = self.fonts.weather_stats_bold
weather_pressure_text = "Low"
if self.weather.current.pressure > 990 and self.weather.current.pressure <= 1025:
pressure_icon = Image.open("icons/p.png")
weather_pressure_text = "Normal"
if self.weather.current.pressure > 1025 :
pressure_icon = Image.open("icons/p_high.png")
weather_font = self.fonts.weather_stats_bold
weather_pressure_text = "High"
# Draw icon and text
img.paste(pressure_icon, (x, y))
xt = x + pressure_icon.size[0] + 5
yt = y + 10
draw.text((xt, yt), weather_pressure_text, font=weather_font, fill=black)
# Step down for next values
y = y + 50
# humidity icon
weather_font = self.fonts.weather_stats
if int(round(self.weather.current.humidity, 0)) <= 45 :
humid_icon = Image.open("icons/h_low.png")
if int(round(self.weather.current.humidity, 0)) > 45 and int(round(self.weather.current.humidity, 0)) <= 80 :
humid_icon = Image.open("icons/h.png")
if int(round(self.weather.current.humidity, 0)) > 80 :
humid_icon = Image.open("icons/h_high.png")
weather_font = self.fonts.weather_stats
img.paste(humid_icon, (x, y))
xt = x + humid_icon.size[0] + 5
yt = y + 10
# humidity value
humidity_w, humidity_h = draw.textsize(str(int(round(self.weather.current.humidity, 0))) + "%", font=weather_font)
draw.text((xt, yt), str(int(round(self.weather.current.humidity, 0))) + "%", font=weather_font, fill=black)
# Step down for next values
y = y + 50
# Wind icon
weather_font = self.fonts.weather_stats
if round(self.weather.current.wind, 0) <= 30 :
wind_icon = Image.open("icons/w_low.png")
if round(self.weather.current.wind) > 30 and round(self.weather.current.wind) <= 50 :
wind_icon = Image.open("icons/w.png")
if round(self.weather.current.wind) > 50 :
wind_icon = Image.open("icons/w_high.png")
weather_font = self.fonts.weather_stats_bold
img.paste(wind_icon, (x, y))
# wind value
xt = x + wind_icon.size[0] + 5
yt = y + 10
wind_w, wind_h = draw.textsize(str(int(round(self.weather.current.wind, 0))) + "km/h", font=weather_font)
draw.text((xt, yt), str(int(round(self.weather.current.wind, 0))) + "km/h", font=weather_font, fill=black)
# TOMORROW's forecast MINI
# tomorrow's text
# Current conditions
Tomorrow_Days = datetime.now() + timedelta(days=1)
Tomorrow_Day = Tomorrow_Days.strftime("%A :")
header_w, header_h = draw.textsize(Tomorrow_Day, font=self.fonts.SFCompact_tinyB)
x = 20 + X_OFFSET
y = self.boxes.today.height - int(header_h +14)
draw.text((x,y),Tomorrow_Day,font=self.fonts.SFCompact_tinyB, fill=black)
# Draw the condition icon mini
condition = Image.open(tomorrow.icon)
condition = condition.resize((int(condition.size[0] /3), int(condition.size[1] / 3)))
x = x + int(header_w +4)
iy = y
img.paste(condition, (x, iy))
# Print Tomorrows forecast
xoff = x + int( condition.size[0]+4 )
condition_w, condition_h = draw.textsize(tomorrow.condition.capitalize(), font=self.fonts.SFCompact_tiny)
tomorrow_cond_trunc = tomorrow.condition.capitalize()[0:16]
tomorrow_summary = tomorrow_cond_trunc+" | H: "+str(round(tomorrow.high,1))+"\N{DEGREE SIGN}"+" L: "+str(round(tomorrow.low,1))+"\N{DEGREE SIGN}"
draw.text((xoff, y), tomorrow_summary, font=self.fonts.SFCompact_tiny, fill=black)
y_offset = 0
# COMIC Pane drawing
if self.cfg_data['show-comic'] == "TRUE" :
comicfile = "comics/404.jpg"
comictitle = "none"
if currenttime >= currenttime.replace(hour=5, minute=0) and currenttime <= currenttime.replace(hour=9, minute=0) :
comictitle = self.cfg_data['comic-morning']
#print("Morning Comic is: "+comictitle)
if currenttime > currenttime.replace(hour=9, minute=0) and currenttime <= currenttime.replace(hour=11, minute=0) :
comictitle = self.cfg_data['comic-midday']
#print("Midday Comic is: "+comictitle)
if currenttime > currenttime.replace(hour=11, minute=0) and currenttime <= currenttime.replace(hour=17, minute=0) :
comictitle = self.cfg_data['comic-afternoon']
#print("Afternoon Comic is: "+comictitle)
if currenttime > currenttime.replace(hour=17, minute=0) and currenttime <= currenttime.replace(hour=23, minute=0) :
comictitle = self.cfg_data['comic-evening']
#print("Evening Comic is: "+comictitle)
if currenttime > currenttime.replace(hour=0, minute=0) and currenttime <= currenttime.replace(hour=4, minute=0) :
comictitle = self.cfg_data['comic-evening']
#print("Evening Comic is: "+comictitle)
print("Comic Feature : Comis is "+comictitle)
get_comicdata = getComicFile(comictitle)
comicfile = get_comicdata.comic_url
comictitle = get_comicdata.comic_title
if comicfile == "Nofile" :
comicfile = "comics/404.jpg"
print("Comic is: ",comicfile)
if comicfile != "Nofile" :
comic_image = Image.open(comicfile)
print("Comic Feature : Comic Strip is "+str(comic_image.size[0])+" by "+str(comic_image.size[1]))
print("Comic Feature : Screen size is "+str(self.screen_size[0])+" by "+str(self.screen_size[1]))
if comic_image.size[0] > self.screen_size[0]:
comic_scale_factor = (self.screen_size[0] - 10) / comic_image.size[0]
print("Comic Feature : Scaling the comic down to "+str(comic_scale_factor))
comic_image = comic_image.resize((int(comic_image.size[0] * comic_scale_factor), int(comic_image.size[1] * comic_scale_factor)))
print("Comic Feature : Comic size is now "+str(comic_image.size[0])+" by "+str(comic_image.size[1]))
elif comic_image.size[0] < self.screen_size[0]:
comic_scale_factor = comic_image.size[0] / (self.screen_size[0] - 10)
print("Comic Feature : Scaling the comic up to "+str(comic_scale_factor))
comic_image = comic_image.resize((int(comic_image.size[0] / comic_scale_factor), int(comic_image.size[1] / comic_scale_factor)))
print("Comic Feature : Comic size is now "+str(comic_image.size[0])+" by "+str(comic_image.size[1]))
if comicfile != "Nofile" :
Cx = int((self.screen_size[0]/2)) - int((comic_image.size[0])/2) + X_OFFSET
Cy = int(self.boxes.tomorrow.pos_y)
img.paste(comic_image,(Cx,Cy))
y_offset = Cy + int(comic_image.size[1])
#print("Setting Y offset to "+str(y_offset))
else:
print("Comic Feature : OFF")
#########################
#Quote of the day section
#########################
if self.cfg_data['quote-of-the-day-show'] == "TRUE" :
qml = int(self.cfg_data['quote-of-the-day-max-lenght'])
qll = qml+1
qmaxtries = 0
if int( currenttime.strftime("%d") ) > int( self.daycount ):
self.daycount = datetime.now().strftime("%d")
print("Quote Feature : Getting a quote under "+str(qml)+" lenght")
while qll >= qml :
quote = quoteoftheday()
qll = len(quote.quote_text)
qmaxtries +=1
if qmaxtries > 10:
break
if qll > qml:
print("Quote Feature : Attempt: "+str(qmaxtries))
else:
print("Quote Feature : Quote lenght is "+str(qll))
if qll > qml:
print("Quote Feature : Max attempts to get a short enough quote exhausted.")
#Just in case we could not find a short enough quote in 10 attempts.
quote.quote_text = "Sorry, No short Quote found, please adjust the quote-of-the-day-max-lenght value"
quote.quote_author = "KoboHUB"
print("Quote Feature : "+quote.quote_text+" - "+quote.quote_author)
else:
next_quote_hour = datetime.now() + timedelta(days=1)
print("Quote Feature : Keeping quote, next one at "+next_quote_hour.strftime("%d")+" daycount at "+str(self.daycount))
#print("Now trying to slice the text in chunks")
test_t_w, test_t_h = draw.textsize(quote.quote_text, font=self.fonts.quote)
text_max = test_t_w
text_line_max = self.max_charsQ
text_line = []
textbuffer = ""
#Split the quote into words in an array
quote_words = quote.quote_text.split()
wl = len(quote.quote_text)
#See if the total is larger than the text_line_max value set.
if text_max > text_line_max:
l = 0
ql = len(quote_words)
while l < ql:
textbuffer = textbuffer + quote_words[l] + " "
l += 1
test_t_w, test_t_h = draw.textsize(textbuffer, font=self.fonts.quote)
#print(textbuffer)
if test_t_w > text_line_max:
text_line.append(textbuffer)
textbuffer = ""
#print(l)
if (len(textbuffer)):
text_line.append(textbuffer)
else :
text_line.append(quote.quote_text)
# Get number of arrays generated
qs = len(text_line)
qc = 0
qx = 20 + X_OFFSET
g_w = 0
q_h = 0
q_w = 0
#Getting the widest line of text
tq_w, tq_h = draw.textsize(text_line[qc], font=self.fonts.medium)
while qc < qs :
tq_w, tq_h = draw.textsize(text_line[qc], font=self.fonts.medium)
q_h = tq_h
if tq_w > q_w :
q_w = tq_w
qc +=1
#Writing the quote line by line.
qc = 0
gTx = 10 + X_OFFSET
# gTy = self.screen_size[1] - 280
if y_offset == 0 :
gTy = int(self.boxes.tomorrow.pos_y + 5) + y_offset
else:
gTy = y_offset
if self.cfg_data['show-comic'] == "TRUE" :
draw.line([(0, gTy), (WIDTH, gTy)], gray)
print("Quote Feature : Drawing a line to separate")
gTy = gTy + 5
# Show the Quote Icon
quote_icon = Image.open("icons/quote_icon.png")
img.paste(quote_icon,(gTx, gTy))
gTx = gTx + int(quote_icon.size[0]+4)
while qc < qs:
draw.text((gTx, gTy), text_line[qc], font=self.fonts.quote, fill=black)
#print(text_line[qc])
qc += 1
gTy = gTy + int(q_h + 4)
if qc == 1:
gTx = gTx + 20
q_w, q_h = draw.textsize("- "+quote.quote_author, font=self.fonts.medium)
gTx = int(self.screen_size[0]/2) - int(q_w/2)
gTy = gTy - 5
draw.text((gTx, gTy), "- "+quote.quote_author, font=self.fonts.medium, fill=gray)
gTy = gTy + int(quote_icon.size[1]+10)
###########################
# End of Quote of the day
###########################
else:
print("Quote feature :OFF")
# Preparing for Garbage screens here.
# Set Garbbage Day vaiables
######
##############
## # # # ##
## # # # ##
## # # # ##
## # # # ##
## # # # ##
##############
if self.cfg_data['garbage_schedule'] == "TRUE":
# Setting up Garabage Variables
garbage_vars = dict()
garbage_vars=get_garbage_config_data("garbage_schedules.ini")
g_data = get_garbage_status()
is_garbage_tomorrow = (g_data.landfill_prepapre, g_data.recycle_prepare, g_data.compost_prepare, g_data.dumpster_prepapre)
is_garbage_today = (g_data.landfill, g_data.recycle, g_data.compost, g_data.xmas_tree, g_data.dumpster)
# Clear the space below the comic - in case of Garbage
if 1 in is_garbage_today or 1 in is_garbage_tomorrow :
if gTy > int(self.boxes.next_days[0].pos_y + 14):
print("Clearing the bottom space for instructions...")
gCx = 0
gCy= int(self.boxes.next_days[0].pos_y +1)
comic_bottom = Image.open("icons/comic_bottom.png")
while gCx < self.screen_size[0] :
img.paste(comic_bottom,(gCx, gCy))
gCx = gCx + comic_bottom.size[0]
gTy = int(self.boxes.next_days[0].pos_y + 14)
else :
draw.line([(0, gTy), (WIDTH, gTy)], gray)
print("Garbage schedule : Drawing a line to separate today")
gTy = gTy + 5
else:
print("Garbage schedule : No garbage schedules found, skipping...")
garbage_collection_hour = int(garbage_vars['all-collection-time-over-id'])
comparetime = currenttime.replace(hour=garbage_collection_hour, minute=0)
gTx = 10 + X_OFFSET
gx = 50
gy = self.screen_size[1] - 280
# Show the Calendar Icon
cal_todo_icon = Image.open("icons/reminder_icon.png")
if 1 in is_garbage_today :
print("Garbage schedule : there is garbage today")
img.paste(cal_todo_icon,(gTx, gTy))
gTx = gTx + int( cal_todo_icon.size[0] + 8 )
gTy = gTy - 8
if currenttime <= comparetime:
g_image_end_icon = Image.open('icons/garbage_icons_garbagetruck.png')
g_string = garbage_vars['all-garbage-time-message-today-id']
g_sub_sting = garbage_vars['all-garbage-collect-message-id']
if g_data.landfill == 1:
g_string = g_string + " "+garbage_vars['landfill_title-id']
if g_data.recycle == 1:
g_string = g_string + " & "+garbage_vars['recycle_title-id']
if g_data.compost == 1:
g_string = g_string + " & "+garbage_vars['compost_title-id']
if g_data.dumpster == 1:
g_string = g_string + " & "+garbage_vars['dumpster_title-id']
if g_data.xmas_tree == 1:
g_string = g_string + " & "+garbage_vars['holiday-tree-schedule_title-id']
g_string = g_string + " " + garbage_vars['all-garbage-time-message-end-id']
else:
g_image_end_icon = Image.open('icons/garbage_icons_garage.png')
g_string = garbage_vars['all-collection-time-over-message-line1-id']
g_sub_sting = garbage_vars['all-collection-time-over-message-line2-id']
#print("Now trying to slice the text in chunks")
print("Text is "+str(len(g_string)+" max is "+str(self.max_chars)))
test_t_w, test_t_h = draw.textsize(g_string, font=self.fonts.medium)
text_max = test_t_w
text_line_max = self.max_chars
text_line = []
textbuffer = ""
#Split the quote into words in an array
schedule_words = g_string.split()
wl = len(g_string)
#See if the total is larger than the text_line_max value set.
if text_max > text_line_max:
l = 0
ql = len(schedule_words)
while l < ql:
textbuffer = textbuffer + schedule_words[l] + " "
l += 1
test_t_w, test_t_h = draw.textsize(g_string, font=self.fonts.medium)
if test_t_w > text_line_max:
text_line.append(textbuffer)
textbuffer = ""
if (len(textbuffer)):
text_line.append(textbuffer)
else :
text_line.append(g_string)
# Get number of arrays generated
qs = len(text_line)
qc = 0
tg_w, tg_h = draw.textsize(text_line[0], font=self.fonts.medium)
while qc < qs:
draw.text((gTx, gTy), text_line[qc], font=self.fonts.medium, fill=black)
qc += 1
gTy = gTy + int(tg_h)
g_all_prepare = g_sub_sting
if (len(g_sub_sting)) < self.max_chars :
draw.text((gTx, gTy), g_sub_sting, font=self.fonts.medium, fill=gray)
gTy = gTy + int(cal_todo_icon.size[1]+4)
# Now add the icons in sequence below the Schedule text
# Show the truck icon at the very right.
gTx = int(self.screen_size[0] - int(g_image_end_icon.size[0])) - 10
img.paste(g_image_end_icon, (gTx, gTy))
# Add the arrows icon poiting to the street
g_image_icon = Image.open('icons/garbage_icons_arrows.png')
gTx = gTx - (int(g_image_icon.size[0])+10)
img.paste(g_image_icon, (gTx, gTy))
gTx = gTx - (int(g_image_icon.size[0])+10)
if g_data.xmas_tree == 1:
g_image_icon = Image.open('icons/garbage_icons_holiday.png')
img.paste(g_image_icon, (gTx, gTy))
gTx = gTx - int(g_image_icon.size[1]+4)
if g_data.dumpster == 1:
g_image_icon = Image.open('icons/garbage_icons_dumpster.png')
img.paste(g_image_icon, (gTx, gTy))
gTx = gTx - int(g_image_icon.size[1]+4)
if g_data.compost == 1:
g_image_icon = Image.open('icons/garbage_icons_compost.png')
img.paste(g_image_icon, (gTx, gTy))
gTx = gTx - int(g_image_icon.size[1]+4)
if g_data.recycle == 1:
g_image_icon = Image.open('icons/garbage_icons_recycle.png')
img.paste(g_image_icon, (gTx, gTy))
gTx = gTx - int(g_image_icon.size[1]+4)
if g_data.landfill == 1:
g_image_icon = Image.open('icons/garbage_icons_landfill.png')
img.paste(g_image_icon, (gTx, gTy))
gTx = gTx - int(g_image_icon.size[1]+4)
gTy = gTy + int(cal_todo_icon.size[1]+20)
#else:
#print("Garbage schedule : there is no garbage today")
if 1 in is_garbage_tomorrow :
cal_todo_icon = Image.open("icons/reminder_tomorrow_icon.png")
gTx = 10 + X_OFFSET
print("Garbage schedule : there is garbage tomorrow")
if 1 in is_garbage_today :
draw.line([(0, gTy), (WIDTH, gTy)], gray)
print("Drawing a line to separate today and tomorrow")
gTy = gTy + 5
img.paste(cal_todo_icon,(gTx, gTy))
gTx = gTx + int( cal_todo_icon.size[0] + 8 )
gTy = gTy - 8
#Build the garbage schedule reminder string
g_string = garbage_vars['all-garbage-time-message-tomorrow-id']
if g_data.landfill_prepapre == 1:
g_string = g_string + " "+garbage_vars['landfill_title-id']
if g_data.recycle_prepare == 1:
g_string = g_string + " & "+garbage_vars['recycle_title-id']
if g_data.compost_prepare == 1:
g_string = g_string + " & "+garbage_vars['compost_title-id']
if g_data.dumpster_prepapre == 1:
g_string = g_string + " & "+garbage_vars['dumpster_title-id']
g_string = g_string + " " + garbage_vars['all-garbage-time-message-end-id']
#print("Now trying to slice the text in chunks")
test_t_w, test_t_h = draw.textsize(g_string, font=self.fonts.medium)
text_max = test_t_w
text_line_max = self.max_chars
text_line = []
textbuffer = ""
#Split the quote into words in an array
schedule_words = g_string.split()
wl = len(g_string)
#See if the total is larger than the text_line_max value set.
if text_max > text_line_max:
l = 0
ql = len(schedule_words)
while l < ql:
textbuffer = textbuffer + schedule_words[l] + " "
l += 1
test_t_w, test_t_h = draw.textsize(g_string, font=self.fonts.medium)
if test_t_w > text_line_max:
text_line.append(textbuffer)
textbuffer = ""
if (len(textbuffer)):
text_line.append(textbuffer)
else :