-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
279 lines (244 loc) · 12.3 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
import os
import sys
import time
import logging
import datetime
import requests
import json
import random
import spidev as SPI
from PIL import Image, ImageDraw, ImageFont
import asyncio
from lib import LCD_2inch
from dotenv import load_dotenv
from gpiozero import Button
button = Button(2)
load_dotenv()
APIURL = os.getenv('APIURL')
selectedStation = 'CL'
rotateStations = True
async def get_credentials():
global selectedStation
url = APIURL
data = {'location': selectedStation}
response = requests.post(url, json=data)
dataJSON = response.json()
return dataJSON['x_amz_date'], dataJSON['authorization']
def get_next_train(api_url, headers, platform):
response = requests.get(api_url, headers=headers)
if response.status_code == 200:
data = response.json()
station_name = data.get('Name', 'Unknown Station')
first_train = None
second_train = None
for service in data['services']:
if service['Platform'] == platform:
if first_train is None:
first_train = service
else:
second_train = service
break
return first_train, second_train, station_name
else:
print(f"Failed to fetch data: {response.status_code}")
return None, None, None
def update_display(disp, train, matrixFont, x_position, station_name, matrixFontSmaller, secondTrain):
destination_text = train['Destinations']['Front']['Name']
destinationTerminating_text = train['Origins']['Front']['Name']
platform = train['Platform']
status_text = train['ArrStatus']
if status_text == "Exp":
status_text = train['ETA']
if secondTrain:
secondTrainTime = secondTrain['Destinations']['Front']['STA']
secondTrainDestination = secondTrain['Destinations']['Front']['Name']
# Scrolling text settings
calling_at_text = ""
if 'CallingPoints' in train:
calling_at_text = "Calling at: " + ", ".join([point['Name'] for point in train['CallingPoints']['Front']])
elif destination_text == station_name:
calling_at_text = "Terminating here"
else:
calling_at_text = "Calling at: None"
text_bbox = ImageDraw.Draw(Image.new("RGB", (0, 0))).textbbox((0, 0), calling_at_text, font=matrixFont)
text_width = text_bbox[2] - text_bbox[0]
# Create blank image for drawing
image1 = Image.new("RGB", (disp.height, disp.width), "BLACK")
draw = ImageDraw.Draw(image1)
draw.text((170, 20), "Platform " + platform, fill="ORANGE", font=matrixFont)
# Get current time
now = datetime.datetime.now()
draw.text((10, 20), f"{now.hour:02}:{now.minute:02}:{now.second:02}", fill="ORANGE", font=matrixFont)
if destination_text == station_name:
draw.text((10, 70), destinationTerminating_text, fill="ORANGE", font=matrixFont)
else:
draw.text((10, 70), destination_text, fill="ORANGE", font=matrixFont)
status_text_bbox = draw.textbbox((0, 0), status_text, font=matrixFont)
status_text_text_width = status_text_bbox[2] - status_text_bbox[0]
status_text_x = 310 - status_text_text_width
draw.text((status_text_x, 70), status_text, fill="ORANGE", font=matrixFont)
draw.text((10, 210), station_name, fill="ORANGE", font=matrixFontSmaller)
draw.text((x_position, 120), calling_at_text, fill="ORANGE", font=matrixFont)
if secondTrain:
draw.text((10, 165), secondTrainTime, fill="ORANGE", font=matrixFont)
second_train_bbox = draw.textbbox((0, 0), secondTrainDestination, font=matrixFont)
second_train_text_width = second_train_bbox[2] - second_train_bbox[0]
second_train_x = 310 - second_train_text_width
draw.text((second_train_x, 165), secondTrainDestination, fill="ORANGE", font=matrixFont)
x_position -= 10 + (text_width / 50)
if x_position < -text_width:
x_position = disp.width
image1 = image1.rotate(0)
disp.ShowImage(image1)
return x_position
async def main():
global selectedStation
api_url = "https://tiger-api.worldline.global/services/" + selectedStation
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0",
"Accept": "application/json",
"Accept-Language": "en-US,en;q=0.5",
"x-api-key": "JsfAqhmRuO88ndFxs6VG47Z5dXRB8YZB7Dy4xWXN",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type,Access-Control-Allow-Origin,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
}
consecutive_failures = 0
stations = {
"AD": {"name": "Adelaide", "totalPlatforms": 2},
"AN": {"name": "Antrim", "totalPlatforms": 3},
"BC": {"name": "Ballycarry", "totalPlatforms": 1},
"BA": {"name": "Ballymena", "totalPlatforms": 2},
"BY": {"name": "Ballymoney", "totalPlatforms": 2},
"BL": {"name": "Balmoral", "totalPlatforms": 2},
"BR": {"name": "Bangor", "totalPlatforms": 3},
"BW": {"name": "Bangor West", "totalPlatforms": 2},
"GC": {"name": "Belfast Grand Central", "totalPlatforms": 8, "showNext": True},
"BN": {"name": "Bellarena", "totalPlatforms": 2},
"BT": {"name": "Botanic", "totalPlatforms": 2},
"CA": {"name": "Carnalea", "totalPlatforms": 2},
"CS": {"name": "Carrickfergus", "totalPlatforms": 3},
"CK": {"name": "Castlerock", "totalPlatforms": 1},
"CH": {"name": "City Hospital", "totalPlatforms": 2},
"CP": {"name": "Clipperstown", "totalPlatforms": 2},
"CE": {"name": "Coleraine", "totalPlatforms": 3},
"CY": {"name": "Cullybackey", "totalPlatforms": 1},
"CT": {"name": "Cultra", "totalPlatforms": 2},
"DH": {"name": "Derriaghy", "totalPlatforms": 2},
"DV": {"name": "Dhu Varren", "totalPlatforms": 1},
"DP": {"name": "Downshire", "totalPlatforms": 2},
"DG": {"name": "Drogheda", "totalPlatforms": 3},
"DN": {"name": "Dublin Connolly", "totalPlatforms": 5},
"DK": {"name": "Dundalk", "totalPlatforms": 2},
"DM": {"name": "Dunmurry", "totalPlatforms": 2},
"FY": {"name": "Finaghy", "totalPlatforms": 2},
"GN": {"name": "Glynn", "totalPlatforms": 1},
"GD": {"name": "Greenisland", "totalPlatforms": 2},
"HB": {"name": "Helen's Bay", "totalPlatforms": 2},
"HD": {"name": "Hilden", "totalPlatforms": 2},
"HW": {"name": "Holywood", "totalPlatforms": 2},
"JN": {"name": "Jordanstown", "totalPlatforms": 2},
"LG": {"name": "Lambeg", "totalPlatforms": 2},
"CL": {"name": "Lanyon Place", "totalPlatforms": 4, "showNext": True},
"LR": {"name": "Larne Harbour", "totalPlatforms": 2},
"LN": {"name": "Larne Town", "totalPlatforms": 1},
"LB": {"name": "Lisburn", "totalPlatforms": 3},
"LY": {"name": "Londonderry", "totalPlatforms": 2},
"LU": {"name": "Lurgan", "totalPlatforms": 2},
"MM": {"name": "Magheramorne", "totalPlatforms": 1},
"MO": {"name": "Marino", "totalPlatforms": 2},
"MR": {"name": "Moira", "totalPlatforms": 2},
"MW": {"name": "Mossley West", "totalPlatforms": 1},
"NY": {"name": "Newry", "totalPlatforms": 3},
"PD": {"name": "Portadown", "totalPlatforms": 3},
"PH": {"name": "Portrush", "totalPlatforms": 3},
"PS": {"name": "Poyntzpass", "totalPlatforms": 2},
"SA": {"name": "Scarva", "totalPlatforms": 2},
"SL": {"name": "Seahill", "totalPlatforms": 2},
"SY": {"name": "Sydenham", "totalPlatforms": 2},
"BE": {"name": "Titanic Quarter", "totalPlatforms": 2},
"TE": {"name": "Trooperslane", "totalPlatforms": 2},
"UV": {"name": "University", "totalPlatforms": 1},
"WY": {"name": "Whiteabbey", "totalPlatforms": 2},
"WT": {"name": "Whitehead", "totalPlatforms": 2},
"YG": {"name": "York Street", "totalPlatforms": 2}
}
logging.basicConfig(level=logging.DEBUG)
currentPlatform = 1
try:
disp = LCD_2inch.LCD_2inch()
disp.Init()
disp.clear()
disp.bl_DutyCycle(100) # Set the backlight to 100
matrixFont = ImageFont.truetype("font/DotMatrixRegular.ttf", 32)
matrixFontSmaller = ImageFont.truetype("font/DotMatrixRegular.ttf", 24)
splashScreen = Image.new("RGB", (disp.height, disp.width), "BLACK")
draw = ImageDraw.Draw(splashScreen)
draw.text((10, 100), "Fetching train data...", fill="ORANGE", font=matrixFont)
splashScreen = splashScreen.rotate(0)
disp.ShowImage(splashScreen)
x_position = disp.width
x_amz_date, authorization = await get_credentials()
headers["x-amz-date"] = x_amz_date
headers["Authorization"] = authorization
last_credential_time = datetime.datetime.now()
last_random_station = datetime.datetime.now()
train, secondTrain, currentStation = get_next_train(api_url, headers, str(currentPlatform))
last_fetch_time = datetime.datetime.now()
while True:
current_time = datetime.datetime.now()
if (current_time - last_credential_time).seconds >= 300:
x_amz_date, authorization = await get_credentials()
headers["x-amz-date"] = x_amz_date
headers["Authorization"] = authorization
last_credential_time = current_time
if ((current_time - last_random_station).seconds >= 830 and rotateStations) or button.is_pressed:
random_station_code = random.choice(list(stations.keys()))
selectedStation = random_station_code
last_random_station = current_time
currentPlatform = 1
api_url = "https://tiger-api.worldline.global/services/" + selectedStation
x_amz_date, authorization = await get_credentials()
headers["x-amz-date"] = x_amz_date
headers["Authorization"] = authorization
last_credential_time = current_time
consecutive_failures = 0
last_fetch_time = current_time
train, secondTrain, currentStation = get_next_train(api_url, headers, str(currentPlatform))
if train:
x_position = update_display(disp, train, matrixFont, x_position, currentStation, matrixFontSmaller, secondTrain)
print("Selecting random station:", selectedStation, random_station_code)
if (current_time - last_fetch_time).seconds >= 60 or not train:
if not train:
consecutive_failures += 1
station_info = stations.get(selectedStation, {})
total_platforms = station_info.get("totalPlatforms", 0)
print(f"No trains at platform {currentPlatform} currently.")
if consecutive_failures >= total_platforms + 1:
image2 = Image.new("RGB", (disp.height, disp.width), "BLACK")
draw = ImageDraw.Draw(image2)
station_name = station_info.get("name", "Unknown Station")
draw.text((10, 100), "No trains at current station:", fill="ORANGE", font=matrixFontSmaller)
draw.text((10, 120), station_name, fill="ORANGE", font=matrixFontSmaller)
image2 = image2.rotate(0)
disp.ShowImage(image2)
time.sleep(5)
else:
consecutive_failures = 0
currentPlatform += 1
station_info = stations.get(selectedStation, {})
total_platforms = station_info.get("totalPlatforms", 0)
if currentPlatform > total_platforms:
currentPlatform = 1
train, secondTrain, currentStation = get_next_train(api_url, headers, str(currentPlatform))
last_fetch_time = current_time
if train:
x_position = update_display(disp, train, matrixFont, x_position, currentStation, matrixFontSmaller, secondTrain)
logging.info("Display updated")
except IOError as e:
logging.info(e)
except KeyboardInterrupt:
disp.module_exit()
logging.info("quit:")
exit()
if __name__ == "__main__":
asyncio.run(main())