-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportal.py
314 lines (252 loc) · 8.06 KB
/
portal.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
import time
import os
import platform
import subprocess
import json
import sys
import io
import socket
from util import timer
from util import config
from util import display
from net import Socket
from net import kerrishausapi
from commands import CommandHandler
from commands import ShutdownCommand
from commands import RebootCommand
from commands import PingCommand
if len(sys.argv) > 0:
if "-install" in sys.argv:
# TODO: make sure we have privileges to do this
subprocess.run('''
echo "Installing systemd services."
cd /etc/systemd/system/
touch portal.service
{
echo "[Unit]"
echo "Description=Portal Client Backend"
echo "Wants=network-online.target"
echo "After=network-online.target"
echo ""
echo "[Service]"
echo "User=kiosk"
echo "Type=simple"
echo "Restart=always"
echo "ExecStart=/usr/bin/python3 -u /home/kiosk/portal-rpi/portal.py"
echo ""
echo "[Install]"
echo "WantedBy=multi-user.target"
} > portal.service
touch portal-gui.service
{
echo "[Unit]"
echo "Description=Portal GUI"
echo "Requires=portal.service"
echo "After=network-online.target"
echo ""
echo "[Service]"
echo "User=kiosk"
echo "Type=simple"
echo "Restart=always"
# make sure to modify chrome config and change exited cleanly to true and export the proper display
echo "Environment=DISPLAY=:0"
echo "ExecStart=chromium-browser --noerrdialogs --disable-infobars --disable-error-bubbles --ignore-certificate-errors --check-for-update-interval=31536000 --autoplay-policy=no-user-gesture-required --kiosk /home/kiosk/portal_login.html"
echo ""
echo "[Install]"
echo "WantedBy=multi-user.target"
} > portal-gui.service
systemctl daemon-reload
systemctl enable portal.service
systemctl enable portal-gui.service
echo "Installed and enabled Portal Client Backend and Portal GUI services."
''', shell=True)
#from urllib.request import urlopen
#from zipfile import ZipFile
# gonna try downloading sounds and playing them
#zipurl = 'https://kerrishaus.com/packages/portal-rpi-sounds.zip'
# Download the file from the URL
#zipresp = urlopen(zipurl)
# Create a new file on the hard drive
#tempzip = open("./resources/sounds.zip", "wb")
# Write the contents of the downloaded file into the new file
#tempzip.write(zipresp.read())
# Close the newly-created file
#tempzip.close()
# Re-open the newly-created file with ZipFile()
#zf = ZipFile("./resources/sounds.zip")
# Extract its contents into <extraction_path>
# note that extractall will automatically create the path
#zf.extractall(path = './resources/sounds')
# close the ZipFile instance
#zf.close()
# TODO: put this in util somewhere
def isRaspberryPi():
try:
with io.open('/sys/firmware/devicetree/base/model', 'r') as m:
if 'raspberry pi' in m.read().lower():
return True
print(m.read())
except Exception as e:
print("An exception occured trying to read device model.", e)
return False
cman = CommandHandler.CommandHandler()
cman.RegisterCommand(ShutdownCommand.ShutdownCommand(), "SHUTDOWN")
cman.RegisterCommand(RebootCommand.RebootCommand(), "REBOOT")
cman.RegisterCommand(PingCommand.PingCommand(), "TELL_HIM_HES_UGLY")
if "Kiosk" in config.my_purpose:
from commands import ScreenOffCommand
from commands import ScreenOnCommand
cman.RegisterCommand(ScreenOffCommand.ScreenOffCommand(), "SCREEN_OFF")
cman.RegisterCommand(ScreenOnCommand.ScreenOnCommand(), "SCREEN_ON")
VERSION = 1
print("Starting Portal client ", VERSION)
# config is currently autoloaded
#config.load_config()
print("My name is " + config.my_name + ".")
print("I am device " + config.api_device_id)
print("My purpose is " + config.my_purpose)
if isRaspberryPi():
print("Setting up as Raspberry Pi")
from util import gpio
from util.gpio import lights
gpio.setup()
lights.setup()
Socket.setup()
kerrishausapi.status(1)
lights.send_light()
print("Portal Client is ready.")
if "Motion-PassiveIR" in config.my_purpose:
from util.gpio.motion import passiveir
passiveir.setup()
elif "Motion-Radar" in config.my_purpose:
from util.gpio.motion import radar
radar.setup()
def shutdown():
print("Portal service is stopping...")
if not kerrishausapi.status(0):
lights.fail_light()
lights.fail_light(3)
gpio.cleanup()
Socket.portalSocket.close()
print("Have a good night.")
def send_message(message):
sent = csock.send(message.encode())
if sent != 0:
lights.send_light()
else:
lights.fail_light()
return sent
api_timer = timer.Timer()
light_timer = timer.Timer()
try:
running = True
while running: # this is the socket accept loop
if api_timer.getElapsedTime() > config.api_status_interval:
print("API timer reached.")
if "Kiosk" in config.my_purpose:
if display.is_display_powered():
kerrishausapi.status(2)
else:
kerrishausapi.status(3)
else:
kerrishausapi.status(1)
api_timer.reset()
if "Motion-PassiveIR" in config.my_purpose:
passiveir.update()
elif "Motion-Radar" in config.my_purpose:
radar.update()
try:
csock, caddr = Socket.portalSocket.accept()
csock.settimeout(.1)
invalidDataCount = 0
print("/ ACCEPTED SOCKET")
try: # this is the client communication loop
while True:
data = csock.recv(1024)
if data:
lights.recv_light()
data = data.decode()
print("> " + data)
cman.runCommand(data)
if data == "DISCONNECT":
if send_message("BYE_BYE"):
csock.close()
break
elif data == "SET_NAME":
send_message("OKAY_GIVE_NAME")
data = csock.recv(1024)
if data:
new_name = data.decode()
config.updateConfig("DEFAULT", "MyName", new_name)
config.my_name = new_name
print("my new name is " + config.my_name)
send_message("NAME_SET")
else:
print("NO name given")
send_message("FAIL_INVALID_DATA")
elif data == "SET_API_INTERVAL":
send_message("OKAY_GIVE_INTERVAL")
data = csock.recv(1024)
if data:
new_interval = data.decode()
config.updateConfig("KERRISHAUS_API", "StatusInterval", new_interval)
config.api_status_interval = new_interval
print("Status interval set to " + config.api_status_interval)
send_message("INTERVAL_SET")
else:
print("Data is invalid.")
send_message("FAIL_INVALID_DATA")
elif data == "GIVE_PURPOSE":
send_message(config.my_purpose)
elif data == "SCREEN_STATUS":
send_message(display.is_display_powered())
elif data == "PLATFORM_INFO":
send_message(message = os.name + " " + platform.system() + " " + platform.release() + " " + platform.machine())
elif data == "GIVE_NAME":
send_message(config.my_name)
elif data == "GIVE_GENERAL":
if "Kiosk" in config.my_purpose:
screen = {
"powered": display.is_display_powered()
}
else:
screen = False
if "Motion-PassiveIR" in config.my_purpose:
motion_stat = passiveir.motion
elif "Motion-PassiveIR" in config.my_purpose:
motion_stat = radar.motion
else:
motion_stat = False
payload = {
"deviceid": config.api_device_id,
"name": config.my_name,
"platform": os.name + " " + platform.system() + " " + platform.release() + " " + platform.machine(),
"purpose": config.my_purpose,
"screen": screen,
"motion": motion_stat,
}
payload = json.dumps(payload)
send_message(payload)
else:
send_message("UNKNOWN_COMMAND")
else:
print("Data received, but data was invalid.")
lights.fail_light()
data = None; del data
invalidDataCount += 1
if invalidDataCount > 20:
print("| CLOSING SOCKET (too much invalid data)")
csock.close()
break
except socket.timeout:
print("socket timeout")
finally:
print("\ CLOSING SOCKET")
csock.close()
except socket.timeout:
continue
except KeyboardInterrupt:
print("STOP")
shutdown()
exit()
shutdown()