-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus_display.py
188 lines (162 loc) · 5.74 KB
/
status_display.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
import adafruit_ssd1306
import busio
import json
import os
from os.path import join, dirname
import requests
import subprocess
import time
from board import SCL, SDA
from datetime import datetime
from dateutil import parser
from PIL import Image, ImageDraw, ImageFont
import robin_api
import status_light
# Create the I2C interface.
i2c = busio.I2C(SCL, SDA)
# Create the SSD1306 OLED class.
# The first two parameters are the pixel width and pixel height. Change these
# to the right size for your display!
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
# flip the display 180º if needed
# display.rotation = 2
height = display.height
width = display.width
padding = -2
top = padding
bottom = height-padding
x = 0
# Make sure to create image with mode '1' for 1-bit color.
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Load default font
# font = ImageFont.load_default()
# Load TTF fonts
font = ImageFont.truetype(join(dirname(__file__), 'fonts/UbuntuMono-B.ttf'), 16)
alt_font = ImageFont.truetype(join(dirname(__file__), 'fonts/UbuntuMono-R.ttf'), 16)
small_font = ImageFont.truetype(join(dirname(__file__), 'fonts/Ubuntu-C.ttf'), 13)
def draw_reset():
draw.rectangle((0, 0, width, height), outline=0, fill=0)
display.image(image)
def update_seat():
# Draw a black-filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=0)
seat_reservation_request = robin_api.get_reservations_by_seat()
reservations = seat_reservation_request['data']
seat_name = ''
seat_zone = ''
reservee_name = ''
seat_type = ''
seat_disabled = False
seat_reservable = True
now = datetime.now().astimezone().replace(microsecond=0)
line_one = ''
line_two = ''
line_three = ''
# lazy property diving
if len(reservations):
try:
seat_name = reservations[0]['seat']['name']
except:
pass
try:
seat_zone = reservations[0]['seat']['zone']['name']
except:
pass
try:
reservee_name = reservations[0]['reservee']['user']['name']
except:
pass
try:
seat_type = reservations[0]['type']
except:
pass
try:
seat_disabled = reservations[0]['seat']['is_disabled']
except:
pass
try:
seat_reservable = reservations[0]['seat']['is_reservable']
except:
pass
line_one = ', '.join((seat_name, seat_zone))
start = parser.isoparse(reservations[0]['start']['date_time'])
active_reservation = now > start
# If the seat is not assigned, make sure the end of the reservation
# is also not in the past
if reservations[0]['end'] is not None:
end = parser.isoparse(reservations[0]['end']['date_time'])
active_reservation = now > start and now < end
if active_reservation is True:
if reservations[0]['reservee']['email'] is not None:
line_two = 'This seat is assigned to:'
line_three = reservee_name
status_light.set_status('UNAVAILABLE')
elif seat_disabled is True or seat_reservable is False:
line_two = 'This seat is:'
line_three = 'UNAVAILABLE'
status_light.set_status('UNAVAILABLE')
else:
line_two = 'This seat is:'
line_three = 'UNAVAILABLE'
status_light.set_status('AVAILABLE')
else:
line_two = 'This seat is:'
if seat_disabled is not True and seat_reservable is True:
line_three = 'AVAILABLE'
status_light.set_status('AVAILABLE')
else:
line_three = 'UNAVAILABLE'
status_light.set_status('UNAVAILABLE')
else:
seat_request = robin_api.get_seat()
if seat_request is None or len(seat_request['data']) == 0:
draw.text((x, top+16), 'ERROR', font=font, fill=255)
line_three = 'DESK NOT FOUND'
status_light.set_status('NONE')
status_light.signal_service_needed()
else:
try:
seat_name = seat_request['data']['name']
except:
pass
try:
seat_zone = seat_request['data']['zone']['name']
except:
pass
try:
seat_disabled = seat_request['data']['is_disabled']
except:
pass
try:
seat_reservable = seat_request['data']['is_reservable']
except:
pass
line_one = ', '.join((seat_name, seat_zone))
line_two = 'This seat is:'
if seat_disabled is not True and seat_reservable is True:
line_three = 'AVAILABLE'
status_light.set_status('AVAILABLE')
else:
line_three = 'UNAVAILABLE'
status_light.set_status('UNAVAILABLE')
if len(line_one):
draw.text((x, top), line_one, font=alt_font, fill=255)
if len(line_two):
draw.text((x, top+16), line_two, font=small_font, fill=255)
if len(line_three):
draw.text((x, top+16+13+2), line_three, font=font, fill=255)
timestamp_size = 12
draw.text((x, height-timestamp_size-2), now.strftime('as of %I:%M%P %d/%m/%y'), font=ImageFont.truetype('./fonts/Ubuntu-C.ttf', timestamp_size), fill=255)
# Display image.
display.image(image)
display.show()
def shutdown():
draw_reset()
draw.text((x, top+16), 'Shutting Down...', font=font, fill=255)
display.image(image)
display.show()
time.sleep(2)
draw_reset()
display.show()