-
Notifications
You must be signed in to change notification settings - Fork 0
/
depature_board.py
203 lines (167 loc) · 6.75 KB
/
depature_board.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
#!/usr/bin/python3
UPPER_DEPATURE = "Redbergsplatsen"
UPPER_DEPATURE_ID = "9021014005460000"
UPPER_ARRIVAL = "Östra Sjukhuset"
UPPER_ARRIVAL_ID = "9021014007880000"
LOWER_DEPATURE = "Olskrokstorget"
LOWER_DEPATURE_ID = "9021014005160000"
#LOWER_ARRIVAL = "Brunnsparken"
#LOWER_ARRIVAL_ID = "9021014001760000"
LOWER_ARRIVAL = "Svingeln"
LOWER_ARRIVAL_ID = "9021014006480000"
NBR_DEPARTURES = 4
TIME_OFFSET = 6
CLOCK_SIZE = 50
HIBERNATION_TIME = 180000
import os
import datetime
import subprocess
import socket
from tkinter import *
from tram_GUI import *
from vasttrafik_API import *
def testInternet(host="8.8.8.8", port=53, timeout=3):
"""
Host: 8.8.8.8 (google-public-dns-a.google.com)
OpenPort: 53/tcp
Service: domain (DNS/TCP)
"""
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except Exception as ex:
return False
class DEPARTURE_BOARD:
""" Class for creating a departure board
"""
def __init__(self, api, nbrOfDepartures):
self.api = api
self.nbrOfDep = nbrOfDepartures
self.root = Tk()
self.root.title("Departure Board")
# Clock Frame
self.clockTime = datetime.datetime.now()
self.clock = StringVar()
self.clock.set(self.clockTime.strftime('%H:%M'))
self.clockFrame = Frame(self.root, bg = "black")
self.clockFrame.pack(side="top", fill="both")
self.clockLabel = Label(self.clockFrame,
textvariable=self.clock,
font=("default",50),
bg = "black",
fg="white")
self.clockLabel.pack(side="top", fill="both", expand=True, pady=10)
# So that fullscreen can be toggled
self.root.bind("<F11>", self.fullscreen_toggle)
self.root.bind("<Escape>", self.fullscreen_cancel)
# Upper Title
self.upperTitleFrame = Frame(self.root, bg = "black")
self.upperTitleFrame.pack(side="top", fill="both")
self.upperTitleRow = TitleRow(self.upperTitleFrame, UPPER_DEPATURE, UPPER_ARRIVAL)
# Upper Content
self.upperContentFrame = Frame(self.root, bg = "black")
self.upperContentFrame.pack(side="top", fill="both", expand=True)
# Lower Title
self.lowerTitleFrame = Frame(self.root, bg = "black")
self.lowerTitleFrame.pack(side="top", fill="both")
self.lowerTitleRow = TitleRow(self.lowerTitleFrame, LOWER_DEPATURE, LOWER_ARRIVAL)
# Lower Content
self.lowerContentFrame = Frame(self.root, bg = "black")
self.lowerContentFrame.pack(side="top", fill="both", expand=True)
def fullscreen_toggle(self, event="none"):
self.root.focus_set()
self.root.attributes("-fullscreen", False)
self.root.attributes("-fullscreen", True)
self.root.attributes("-topmost", 1)
self.root.focus_force()
def fullscreen_set(self, event="none"):
self.root.focus_set()
self.root.attributes("-fullscreen", True)
self.root.attributes("-topmost", 1)
self.root.focus_force()
def fullscreen_cancel(self, event="none"):
self.root.attributes("-fullscreen", False)
self.root.attributes("-topmost", 0)
self.centerWindow()
def centerWindow(self):
sw = self.root.winfo_screenwidth()
sh = self.root.winfo_screenheight()
w = sw*0.7
h = sh*0.7
x = (sw-w)/2
y = (sh-h)/2
self.root.geometry("%dx%d+%d+%d" % (w, h, x, y))
def update_rotation(self):
# TODO Add check to see which OS is used and un/comment the rotation
subprocess.call(["xrandr", "--output", "LVDS1", "--rotate", "right"])
#self.root.after(500, self.update_rotation)
def update_clock(self):
self.clockTime = datetime.datetime.now()
self.clock.set(self.clockTime.strftime('%H:%M'))
# Update clock every second
self.root.after(1000, self.update_clock)
def create_board(self):
if testInternet():
self.token = self.api.retrieve_token()
self.upperTrams = self.api.retrieve_trams(self.token, UPPER_DEPATURE_ID, UPPER_ARRIVAL_ID, TIME_OFFSET, NBR_DEPARTURES)
self.lowerTrams = self.api.retrieve_trams(self.token, LOWER_DEPATURE_ID, LOWER_ARRIVAL_ID, TIME_OFFSET, NBR_DEPARTURES)
#if len(self.upperTrams) < self.nbrOfDep:
# nbrOfUpperLines = len(self.upperTrams)
#else:
# nbrOfUpperLines = self.nbrOfDep
for i in range(len(self.upperTrams)):
TramRow(self.upperContentFrame,
self.upperTrams[i]['number'],
self.upperTrams[i]['direction'],
str(self.upperTrams[i]['time']) + ' min')
#if len(self.lowerTrams) < self.nbrOfDep:
# nbrOfLowerLines = len(self.lowerTrams)
#else:
# nbrOfLowerLines = self.nbrOfDep
for i in range(len(self.upperTrams)):
TramRow(self.lowerContentFrame,
self.lowerTrams[i]['number'],
self.lowerTrams[i]['direction'],
str(self.lowerTrams[i]['time']) + ' min')
# Update trams every 30 seconds
self.root.after(30000, self.update_board)
self.fullscreen_set()
else:
TramRow(self.upperContentFrame,
'-',
'no connection',
'00:00')
TramRow(self.lowerContentFrame,
'-',
'no connection',
'00:00')
# Update trams every second
self.root.after(1000, self.update_board)
self.update_rotation()
# Make sure of fullscreen
self.fullscreen_toggle()
def update_board(self):
if len(self.upperContentFrame.winfo_children()) > 0:
# Clear frames
for child in self.upperContentFrame.winfo_children():
child.destroy()
for child in self.lowerContentFrame.winfo_children():
child.destroy()
self.create_board()
else:
self.create_board()
def hibernate_mirror(self):
os.system("systemctl suspend")
time.sleep(1)
self.root.after(HIBERNATION_TIME, self.hibernate_mirror)
# Create connection to Västtrafik API
api = API_VT()
# Create the board
board = DEPARTURE_BOARD(api,NBR_DEPARTURES)
board.update_clock()
board.update_rotation()
board.create_board()
board.root.after(HIBERNATION_TIME, board.hibernate_mirror)
# Start infinite loop
board.root.mainloop()