/
common.py
438 lines (364 loc) · 14.5 KB
/
common.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
from collections import namedtuple
from PIL import Image, ImageDraw, ImageFont
from enum import Enum
from files import *
import logging
import json
import time
import config
import socket
import version
import signal
import pytz
import threading
import subprocess
import requests
import uuid
import re
from dateutil.parser import *
log = logging.getLogger(__name__)
Color = namedtuple('Color', 'red green blue')
ACTIVE_SCREEN_KEY = "active_screen"
SETUP_STATE_KEY = "setup_state"
SCREENS_KEY = "screens"
RESTART_KEY = "restart"
REBOOT_MESSAGE_KEY = "reboot_message"
MATRIX_KEY = "matrix"
SCREEN_ON_KEY = "screen_on"
VERSION_KEY = "version"
MAC_ADDRESS_KEY = "mac_address"
AWS_URL = 'https://opbhrfuhq5.execute-api.us-east-2.amazonaws.com/Prod/'
# AWS_URL = 'http://127.0.0.1:1337/'
small_down_arrow_pixels = [(0, 0), (1, 0), (2, 0),
(3, 0), (4, 0), (1, -1), (2, -1), (3, -1), (2, -2)]
small_up_arrow_pixels = [(2, 0), (1, -1), (2, -1),
(3, -1), (0, -2), (1, -2), (2, -2), (3, -2), (4, -2)]
square_3x3_open = [(0, 0), (0, 1), (0, 2), (1, 0),
(1, 2), (2, 0), (2, 1), (2, 2)]
square_3x3_filled = square_3x3_open + [(1, 1)]
wifi = [(0, -6), (1, -5), (2, -4), (2, -7), (3, -4), (3, -6), (4, -3), (4, -6), (4, -8), (5, -3), (5, -5), (5, -7), (6, -3), (6, -5),
(6, -7), (6, -9), (7, -3), (7, -5), (7, -7), (8, -3), (8, -6), (8, -8), (9, -4), (9, -6), (10, -4), (10, -7), (11, -5), (12, -6)]
def send_restart_signal():
subprocess.call(["sudo", "kill", "-10", str(os.getppid())])
def send_wifi_signal():
subprocess.call(["sudo", "kill", "-12", str(os.getppid())])
def get_api_key():
try:
with open(secrets_path) as f:
lines = f.readlines()
return lines[0].strip()
except:
log.error("Failed to read secret")
return None
def get_settings():
try:
with open(settings_path) as f:
settings = json.load(f)
except:
with open(settings_template_path) as f:
settings = json.load(f)
write_settings(settings)
return settings
def write_settings(new_settings):
with open(settings_path, "w+") as f:
new_settings["version"] = version.version
json.dump(new_settings, f)
def get_ip_address():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80)) # attempt to connect, and then get hostname
return s.getsockname()[0]
except:
return ""
def get_mac_address():
return ':'.join(re.findall('..', '%012x' % uuid.getnode()))
class ActiveScreen(Enum):
NHL = 0
MLB = 1
CLOCK = 50
REBOOT = 99
REFRESH = 100
HOTSPOT = 101
WIFI_DETAILS = 102
SYNC = 103
ERROR = 999
def hexToRGB(hex):
red = hex[0:2]
green = hex[2:4]
blue = hex[4:6]
return Color(int(red, 16), int(green, 16), int(blue, 16))
class Team:
def __init__(self, common):
self.id = int(common['id'])
self.name = common['name']
self.display_name = common['display_name'].upper()
self.city = common['city']
self.abbreviation = common['abbreviation']
self.primary_color = hexToRGB(common['primary_color'])
self.secondary_color = hexToRGB(common['secondary_color'])
def __repr__(self):
return "{!r}({!r}, {!r}, {!r}, {!r}, {!r}, {!r})".format(self.__class__.__name__,
self.id, self.name,
self.city, self.abbreviation,
self.primary_color, self.secondary_color)
class GameStatus(Enum):
INVALID = 0
PREGAME = 1
ACTIVE = 2
INTERMISSION = 3
END = 4
@staticmethod
def stringToValue(string):
if string == "PREGAME":
return GameStatus.PREGAME
elif string == "ACTIVE":
return GameStatus.ACTIVE
elif string == "INTERMISSION":
return GameStatus.INTERMISSION
elif string == "END":
return GameStatus.END
else:
return GameStatus.INVALID
class Game:
def __init__(self, timezone, common):
self.id = common['id']
self.away = Team(common['away_team'])
self.home = Team(common['home_team'])
self.away_score = common['away_score']
self.home_score = common['home_score']
self.status = GameStatus.stringToValue(common['status'])
self.start_time = common['start_time']
self.ordinal = common['ordinal']
time = parse(self.start_time).astimezone(
pytz.timezone(timezone))
self.start_hour = time.hour % 12
if self.start_hour == 0:
self.start_hour = 12
self.start_afternoon = "PM" if time.hour >= 12 else "AM"
self.start_minute = time.minute
if self.status == GameStatus.PREGAME:
self.ordinal = "{}:{:02d} {}".format(
self.start_hour, self.start_minute, self.start_afternoon)
class Screen:
def __init__(self):
self.error = False
self.error_title = ""
self.error_message = ""
pass
def reset(self):
pass
def get_image(self):
pass
def refresh(self):
pass
def get_sleep_time(self):
return 10
def is_stale(self):
return False
def get_refresh_time(self):
return 60
class League(Screen):
def __init__(self, settings, api_key, timezone):
super().__init__()
self.active_index = 0
self.last_reset = 0
self.settings = settings
self.api_key = api_key
self.timezone = timezone
self.rotation_time = settings.get("rotation_time", 10)
self.focus_teams = settings.get("focus_teams", [])
self.games = []
self.league_mutex = threading.RLock()
self.is_initialized = False
def reset(self):
pass
def refresh(self):
refresh_thread = None
with self.league_mutex:
if self.is_stale():
# if it's been more than X seconds since the last refresh, refresh all games
log.info(
f"Performing refresh, my ip address is '{get_ip_address()}'")
self.last_reset = time.time()
self.error = False # First, clear any errors
refresh_thread = threading.Thread(target=self.reset)
refresh_thread.start()
if not self.is_initialized and refresh_thread is not None:
refresh_thread.join()
with self.league_mutex:
self.is_initialized = True
# Regardless, move the active game up one, unless a favorite team is playing
if len(self.games) == 0:
self.active_index = -1
elif self.favorite_teams_playing() is not None:
self.active_index = self.favorite_teams_playing()
else:
self.active_index = (self.active_index + 1) % len(self.games)
def get_sleep_time(self):
if self.error:
return 0.05
else:
return self.rotation_time
def get_image(self):
pass
def get_refresh_time(self):
if len(self.games) == 0:
return 120
else:
return 60
def team_playing(self, team_id):
for game in self.games:
if game.home.id == team_id or game.away.id == team_id:
if game.status == GameStatus.ACTIVE or game.status == GameStatus.INTERMISSION:
return self.games.index(game)
return None
def favorite_teams_playing(self):
for team_id in self.focus_teams:
game = self.team_playing(team_id)
if game is not None:
return game
return None
def handle_error(self, error_title, error_message):
self.error = True
self.error_title = error_title
self.error_message = error_message
def get_games(self, endpoint, query):
self.error = False # First, clear any errors
try:
log.info("Fetching new games")
response = requests.get(
url=AWS_URL + endpoint, json={'query': query}, headers={'x-api-key': self.api_key})
if response.status_code == 403:
log.error("403 error, failed to authenticate")
error_title = "Error"
error_message = "Authentication failed, please contact support"
self.handle_error(error_title, error_message)
return None
log.info(response.json())
return response.json()['data']
except Exception as e:
log.error("Error: " + str(e))
error_title = "Disconnected"
error_message = "Use the Scoreboard app to get reconnected"
self.handle_error(error_title, error_message)
return None
def is_stale(self):
return (time.time() - self.last_reset) > self.get_refresh_time()
class Renderer:
def __init__(self, width, height):
self.width = width
self.height = height
self.text_start = self.width
def draw_big_scoreboard(self, game):
log.info("Drawing big scoreboard, game " + game.id)
image = Image.new("RGB", (self.width, self.height))
draw = ImageDraw.Draw(image) # let's draw on this image
team_font = ImageFont.load(big_font)
# add teams
draw.rectangle(((0, 0), (64, 9)), fill=game.away.primary_color)
draw.rectangle(((0, 0), (2, 9)), fill=game.away.secondary_color)
draw.text((5, 1), game.away.display_name,
font=team_font, fill=game.away.secondary_color)
away_score_message = str(game.away_score)
w, _ = team_font.getsize(away_score_message)
draw.text((61-w, 1), str(game.away_score), font=team_font,
fill=game.away.secondary_color)
draw.rectangle(((0, 10), (64, 19)), fill=game.home.primary_color)
draw.rectangle(((0, 10), (2, 19)), fill=game.home.secondary_color)
draw.text((5, 11), game.home.display_name,
font=team_font, fill=game.home.secondary_color)
home_score_message = str(game.home_score)
w, _ = team_font.getsize(home_score_message)
draw.text((61 - w, 11), str(game.home_score),
font=team_font, fill=game.home.secondary_color)
return (image, draw)
def draw_small_scoreboard(self, game, image=None):
log.info("Drawing small scoreboard, game " + game.id)
if image is None:
image = Image.new("RGB", (self.width, self.height))
draw = ImageDraw.Draw(image) # let's draw on this image
team_font = ImageFont.load(small_font)
# add teams
draw.rectangle(((0, 0), (64, 6)), fill=game.away.primary_color)
draw.rectangle(((0, 0), (2, 6)), fill=game.away.secondary_color)
draw.text((5, 1), game.away.display_name,
font=team_font, fill=game.away.secondary_color)
away_score_message = str(game.away_score)
w, _ = team_font.getsize(away_score_message)
draw.text((61 - w, 1), str(game.away_score),
font=team_font, fill=game.away.secondary_color)
draw.rectangle(((0, 7), (64, 13)), fill=game.home.primary_color)
draw.rectangle(((0, 7), (2, 13)), fill=game.home.secondary_color)
draw.text((5, 8), game.home.display_name,
font=team_font, fill=game.home.secondary_color)
home_score_message = str(game.home_score)
w, _ = team_font.getsize(home_score_message)
draw.text((61 - w, 8), str(game.home_score),
font=team_font, fill=game.home.secondary_color)
return (image, draw)
def draw_error(self, title, scrollingText=None):
image, draw = self.draw_border(color=(255, 0, 0))
height = None
if scrollingText is not None:
height = self.height + 6
image, draw, self.text_start = self.get_scrolling_text(
self.text_start, image, scrollingText, (255, 0, 0), (0, 0, 0))
if title is not None:
image, draw = self.draw_text(
title, centered=True, image=image, height=height, color=(255, 0, 0))
return image
def draw_text(self, text, centered=False, x=None, y=None, width=None, height=None, color=None, image=None, font_filename=small_font):
if image is None:
image = Image.new("RGB", (self.width, self.height))
if width is None:
width = self.width
if height is None:
height = self.height
if x is None:
x = width/2
if y is None:
y = height/2
if color is None:
color = (255, 255, 255)
font = ImageFont.load(font_filename)
draw = ImageDraw.Draw(image)
w, h = font.getsize(text)
if centered:
x = x - w/2
y = y - h/2
draw.text((x, y), text, font=font, fill=color)
return (image, draw)
def draw_border(self, color=None, image=None):
if image is None:
image = Image.new("RGB", (self.width, self.height))
if color is None:
color = (255, 255, 255)
draw = ImageDraw.Draw(image)
draw.rectangle([(0, 0), (self.width-1, self.height-1)],
outline=color, fill=(0, 0, 0))
return (image, draw)
def draw_info(self, text):
image, draw = self.draw_border()
image, draw = self.draw_text(text, centered=True, image=image)
return (image, draw)
def draw_icon(self, icon, image=None):
pass
def draw_pixels(self, pixels, x, y):
return [(x+xi, y-yi) for xi, yi in pixels]
def get_scrolling_text(self, text_start, image=None, message="Message", background_color=(255, 255, 255), text_color=(0, 0, 0)):
if image is None:
image = Image.new("RGB", (self.width, self.height))
draw = ImageDraw.Draw(image) # let's draw on this image
draw.rectangle(((0, 0), (64, 6)), fill=background_color)
font = ImageFont.load(small_font)
w, _ = font.getsize(message)
text_end = text_start + w
secondary_start = text_end + 16
self.draw_text(message, x=text_start, y=1,
color=text_color, image=image)
self.draw_text(message, x=secondary_start, y=1,
color=text_color, image=image)
text_start -= 1
if text_end <= 0:
text_start = secondary_start - 1
return image, draw, text_start