-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
140 lines (111 loc) · 3.05 KB
/
Copy pathmain.py
File metadata and controls
140 lines (111 loc) · 3.05 KB
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
import atexit
import json
import requests
import threading
import time
from os import path
from papirus import PapirusComposite
def getAbsPath(file):
base = path.dirname(__file__)
return path.abspath(path.join(base, file))
URL = 'http://localhost:3000/trips'
LINE_HEIGHT = 22
FONT_REGULAR = getAbsPath('FiraMono-Regular.ttf')
FONT_BOLD = getAbsPath('FiraMono-Bold.ttf')
FONT_SIZE = 14
ICONS = {
'ferry': getAbsPath('ferry-icon.png'),
'bus': getAbsPath('bus-icon.png'),
'train': getAbsPath('train-icon.png'),
'subway': getAbsPath('train-icon.png')
}
ICON_SIZE = (17, 17)
UPDATE_INTERVAL = 30
state = {
'items': []
}
configFile = open(getAbsPath('config.json'))
queries = json.load(configFile)
# Request new items from the REST api
def requestItems():
response = requests.post(URL, json=queries)
return response.json()
# Setup all position and size of text & icon elements
def setupLayout(canvas):
layout = {
'routes': [],
'times': [],
'icons': []
}
for index in range(0, 9):
yOffset = index * LINE_HEIGHT + 2
routeId = 'route-' + str(index)
timeId = 'time-' + str(index)
iconId = 'icon-' + str(index)
layout['routes'].append(routeId)
layout['times'].append(timeId)
layout['icons'].append(iconId)
canvas.AddText(
'',
2,
yOffset,
size=FONT_SIZE,
fontPath=FONT_REGULAR,
Id=routeId
)
canvas.AddText(
'',
32,
yOffset,
size=FONT_SIZE,
fontPath=FONT_BOLD,
Id=timeId
)
canvas.AddImg(
ICONS['bus'],
77,
yOffset,
ICON_SIZE,
Id=iconId
)
return layout
# Call a function in a given interval
def set_interval(func, sec):
def func_wrapper():
set_interval(func, sec)
func()
t = threading.Timer(sec, func_wrapper)
t.start()
# Render items to the canvas
def render(items, canvas, layout):
for index, item in enumerate(items):
routeId = layout['routes'][index]
timeId = layout['times'][index]
iconId = layout['icons'][index]
canvas.UpdateText(routeId, item['route'], fontPath=FONT_REGULAR)
canvas.UpdateText(timeId, item['time'], fontPath=FONT_BOLD)
canvas.UpdateImg(iconId, ICONS[item['type']])
canvas.WriteAll()
# Trigger item update & re-rendering
def update(canvas, layout):
render(state['items'], canvas, layout)
state['items'] = requestItems()
# Show startup screen
def startup(canvas):
canvas.AddImg(getAbsPath('startup.png'), 0, 0, (96, 200))
canvas.WriteAll()
time.sleep(3)
canvas.Clear()
def main():
state['items'] = requestItems()
canvas = PapirusComposite(False, rotation=90)
startup(canvas)
layout = setupLayout(canvas)
def loop():
update(canvas, layout)
set_interval(loop, UPDATE_INTERVAL)
loop()
def clear():
canvas.Clear()
atexit.register(clear)
main()