forked from maxi07/Teams-Presence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zoomPresence.py
588 lines (460 loc) · 15.2 KB
/
zoomPresence.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# !/usr/bin/env python
# Python script to show Zoom presence status on led
# Author: Jonathan Welton
# Date 09.01.2021
version = 1.0
workday_start = None
workday_end = None
workdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
width = 0
height = 0
blinkThread = None
after_work = False
globalRed = 0
globalGreen = 0
globalBlue = 0
points = []
brightness_led = 0.5
sleepValue = 30 # seconds
# Define Error Logging
def printerror(ex):
print('\033[31m' + str(ex) + '\033[0m')
def printwarning(warn):
print('\033[33m' + str(warn) + '\033[0m')
def printgreen(msg):
print('\033[32m' + str(msg) + '\033[0m')
def printyellow(msg):
print('\033[33m' + str(warn) + '\033[0m')
def printred(msg):
print('\033[31m' + str(ex) + '\033[0m')
def printblue(msg):
print('\033[34m' + str(ex) + '\033[0m')
def printblink(msg):
print('\033[5m' + str(ex) + '\033[0m')
print("Loading modules...")
try:
import requests
import socket
import os
import argparse
from random import randint
from urllib.error import HTTPError
import json
import unicornhat as unicorn
import threading
import sys
import urllib.parse
import configparser
from time import sleep
from datetime import datetime, time
from signal import signal, SIGINT
from gpiozero import CPUTemperature
except ModuleNotFoundError as ex:
printerror("The app could not be started.")
printerror("Please run 'sudo ./install.sh' first.")
printerror(ex)
exit(2)
except:
printerror("An unknown error occured while loading modules.")
exit(2)
# Check for arguments
parser = argparse.ArgumentParser()
parser.add_argument("--version", "-v",
help="Prints the version", action="store_true")
parser.add_argument("--refresh", "-r",
help="Sets the refresh value in seconds", type=int)
parser.add_argument("--brightness", "-b",
help="Sets the brightness of the LED display. Value must be between 0.1 and 1", type=int)
parser.add_argument("--afterwork", "-aw",
help="Check for presence after working hours", action="store_true")
parser.add_argument("--nopulse", "-np",
help="Disables pulsing, if after work hours", action="store_true")
args = parser.parse_args()
if args.version:
print(str(version))
exit(0)
if args.nopulse:
printwarning("Option: No pulsing")
if args.refresh:
if args.refresh < 10:
printerror("Refresh value must be greater than 10")
exit(4)
sleep = args.refresh
printwarning("Option: Sleep set to " + str(sleep))
if args.brightness:
if args.brightness < 0.1 and args.brightness > 1:
printerror("Value must be between 0.1 and 1")
exit(5)
brightness = args.brightness
printwarning("Option: Brightness set to " + str(brightness))
if args.afterwork:
printwarning("Option: Set after work to true")
# Get the width and height of the hardware
width, height = unicorn.get_shape()
workday_start = time(9)
workday_end = time(18)
# Handles Ctrl+C
def handler(signal_received, frame):
# Handle any cleanup here
print()
printwarning(
'SIGINT or CTRL-C detected. Please wait until the service has stopped.')
blinkThread.do_run = False
blinkThread.join()
switchOff()
exit(0)
# Disable Printing
def blockPrint():
sys.stdout = open(os.devnull, 'w')
# Restore Printing
def enablePrint():
sys.stdout = sys.__stdout__
# Check times
def is_time_between(begin_time, end_time, check_time=None):
# If check time is not given, default to current UTC time
try:
check_time = check_time or datetime.now().time()
if begin_time < end_time:
return check_time >= begin_time and check_time <= end_time
else: # crosses midnight
return check_time >= begin_time or check_time <= end_time
except:
printerror("Could not verify times. " + sys.exc_info()[0])
return False
# Countdown for minutes
def countdown(t):
total = t
progvalue = 0
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print("Time until next run: " + timer, end="\r")
sleep(1)
t -= 1
print(" ", end="\r")
# Check or internet connection
def is_connected():
try:
# connect to the host -- tells us if the host is actually
# reachable
socket.create_connection(("www.google.com", 80))
return True
except OSError:
pass
return False
# Checks for updates
def checkUpdate():
updateUrl = "https://raw.githubusercontent.com/maxi07/Teams-Presence/master/doc/version"
try:
f = requests.get(updateUrl, timeout=10)
latestVersion = float(f.text)
if latestVersion > version:
printwarning("There is an update available.")
printwarning(
"Head over to https://github.com/maxi07/Teams-Presence to get the latest features.")
else:
print("Application is running latest version.")
except Exception as e:
printerror("An error occured while searching for updates.")
printerror(e)
# ############################
# UNICORN SETUP
# ############################
def setColor(r, g, b, brightness, speed):
global crntColors, globalBlue, globalGreen, globalRed
globalRed = r
globalGreen = g
globalBlue = b
if brightness == '':
unicorn.brightness(brightness_led)
for y in range(height):
for x in range(width):
unicorn.set_pixel(x, y, r, g, b)
unicorn.show()
def pulse():
for b in range(0, 7):
blockPrint()
unicorn.brightness(b/10)
enablePrint()
for y in range(height):
for x in range(width):
unicorn.set_pixel(x, y, 102, 255, 255)
unicorn.show()
sleep(0.05)
sleep(1)
for b in range(6, 0, -1):
blockPrint()
unicorn.brightness(b/10)
enablePrint()
for y in range(height):
for x in range(width):
unicorn.set_pixel(x, y, 102, 255, 255)
unicorn.show()
sleep(0.05)
def switchBlue():
red = 0
green = 0
blue = 250
blinkThread = threading.Thread(
target=setColor, args=(red, green, blue, '', ''))
blinkThread.do_run = True
blinkThread.start()
def switchRed():
red = 250
green = 0
blue = 0
blinkThread = threading.Thread(
target=setColor, args=(red, green, blue, '', ''))
blinkThread.do_run = True
blinkThread.start()
def switchGreen():
red = 0
green = 250
blue = 0
blinkThread = threading.Thread(
target=setColor, args=(red, green, blue, '', ''))
blinkThread.do_run = True
blinkThread.start()
def switchPink():
red = 255
green = 108
blue = 180
blinkThread = threading.Thread(
target=setColor, args=(red, green, blue, '', ''))
blinkThread.do_run = True
blinkThread.start()
def switchYellow():
red = 255
green = 255
blue = 0
blinkThread = threading.Thread(
target=setColor, args=(red, green, blue, '', ''))
blinkThread.do_run = True
blinkThread.start()
def switchOff():
global blinkThread, globalBlue, globalGreen, globalRed
globalRed = 0
globalGreen = 0
globalBlue = 0
if blinkThread != None:
blinkThread.do_run = False
unicorn.clear()
unicorn.off()
class LightPoint:
def __init__(self):
self.direction = randint(1, 4)
if self.direction == 1:
self.x = randint(0, width - 1)
self.y = 0
elif self.direction == 2:
self.x = 0
self.y = randint(0, height - 1)
elif self.direction == 3:
self.x = randint(0, width - 1)
self.y = height - 1
else:
self.x = width - 1
self.y = randint(0, height - 1)
self.colour = []
for i in range(0, 3):
self.colour.append(randint(100, 255))
def update_positions():
for point in points:
if point.direction == 1:
point.y += 1
if point.y > height - 1:
points.remove(point)
elif point.direction == 2:
point.x += 1
if point.x > width - 1:
points.remove(point)
elif point.direction == 3:
point.y -= 1
if point.y < 0:
points.remove(point)
else:
point.x -= 1
if point.x < 0:
points.remove(point)
def plot_points():
unicorn.clear()
for point in points:
unicorn.set_pixel(point.x, point.y,
point.colour[0], point.colour[1], point.colour[2])
unicorn.show()
def blinkRandom(arg):
t = threading.currentThread()
while getattr(t, "do_run", True):
if len(points) < 10 and randint(0, 5) > 1:
points.append(LightPoint())
plot_points()
update_positions()
sleep(0.03)
##################################################
def printHeader():
# Get CPU temp
cpu = CPUTemperature()
os.system('clear')
print("============================================")
print(" Zoom Presence")
print("============================================")
print()
cpu_r = round(cpu.temperature, 2)
print("Current CPU:\t\t" + str(cpu_r) + "°C")
# Check for Weekend
def check_weekend():
now = datetime.now()
while now.strftime("%A") not in workdays:
printHeader()
now = datetime.now()
print("It's " + now.strftime("%A") +
", weekend! Grab more beer! \N{beer mug}")
print()
if args.nopulse:
switchOff()
else:
pulsethread = threading.Thread(target=pulse)
pulsethread.start()
countdown(30)
# Check for working hours
def check_workingtimes():
if args.afterwork:
return
# Get CPU temp
cpu = CPUTemperature()
while is_time_between(workday_start, workday_end) == False:
printHeader()
now = datetime.now()
print("Work is over for today, grab a beer! \N{beer mug}")
print()
if args.nopulse:
switchOff()
else:
pulsethread = threading.Thread(target=pulse)
pulsethread.start()
countdown(30)
# Main
def run():
print("Welcome to Zoom presence for Pi!")
print("Booting v" + str(version))
# Tell Python to run the handler() function when SIGINT is received
signal(SIGINT, handler)
# Check internet
if is_connected == False:
printerror(
"No network. Please connect to the internet and restart the app.")
exit(3)
# Check for updates
checkUpdate()
# Setup Unicorn light
setColor(50, 50, 50, 1, '')
unicorn.set_layout(unicorn.AUTO)
unicorn.brightness(0.5)
blinkThread = threading.Thread(target=blinkRandom, args=("task",))
blinkThread.do_run = True
blinkThread.start()
while True:
check_weekend()
check_workingtimes()
# Check network
if is_connected() == False:
printerror("No network is connected. Waiting for reconnect.")
countdown(30)
continue
print("Fetching new data")
config = configparser.ConfigParser()
config.read("config.ini")
url = config.get('presence_api', 'url')
token = config.get('presence_api', 'token')
headers = {'Authorization': token}
jsonresult = ''
trycount = 0
try:
result = requests.get(
f'{url}', headers=headers, timeout=5)
result.raise_for_status()
jsonresult = result.json()
except requests.exceptions.Timeout as timeerr:
printerror(
"The request for Zoom Presence API timed out! " + str(timeerr))
continue
except requests.exceptions.HTTPError as err:
if err.response.status_code == 404:
printerror("Zoom Presence API URL is invalid!")
exit(5)
elif err.response.status_code == 401:
blinkThread = threading.Thread(
target=blinkRandom, args=("task",))
blinkThread.do_run = True
blinkThread.start()
trycount = trycount + 1
printerror(
"Zoom Presence API is not authorized. Please reauthorize the app (401). Try count: " + str(trycount))
print()
Authorize()
continue
except:
print("Unexpected error:", sys.exc_info()[0])
print("Will try again. Try count: " + str(trycount))
print()
countdown(int(sleepValue))
continue
trycount = 0
# Check for jsonresult
if jsonresult == '':
printerror("JSON result is empty! Will try again.")
printerror(jsonresult)
countdown(5)
continue
# Stop random blinking
if blinkThread != None:
blinkThread.do_run = False
blinkThread.join()
# Get CPU temp
cpu = CPUTemperature()
# Print to display
os.system('clear')
print("============================================")
print(" Zoom Presence")
print("============================================")
print()
now = datetime.now()
print("Last API call:\t\t" + now.strftime("%Y-%m-%d %H:%M:%S"))
cpu_r = round(cpu.temperature, 2)
print("Current CPU:\t\t" + str(cpu_r) + "°C")
if args.brightness:
printwarning("Option:\t\t\t" +
"Set brightness to " + str(brightness))
if args.refresh:
printwarning("Option:\t\t\t" + "Set refresh to " + str(sleepValue))
if args.nopulse:
printwarning("Option:\t\t\t" + "Pulsing disabled")
if args.afterwork:
printwarning("Option:\t\t\t" + "Set display after work to True")
if jsonresult['activity'] == "Available":
print("Zoom presence:\t\t" + '\033[32m' + "Available" + '\033[0m')
switchGreen()
elif jsonresult['activity'] == "In_Calendar_Event":
print("Zoom presence:\t\t" +
'\033[31m' + "In Calendar Event" + '\033[0m')
switchPink()
elif jsonresult['activity'] == "Away":
print("Zoom presence:\t\t" + '\033[33m' + "Away" + '\033[0m')
switchGreen()
elif jsonresult['activity'] == "Do_Not_Disturb":
print("Zoom presence:\t\t" +
'\033[31m' + "Do Not Disturb" + '\033[0m')
switchPink()
elif jsonresult['activity'] == "In_Meeting":
print("Zoom presence:\t\t" +
'\033[31m' + "In a meeting" + '\033[0m')
switchPink()
elif jsonresult['activity'] == "Presenting":
print("Zoom presence:\t\t" +
'\033[31m' + "Presenting" + '\033[0m')
switchRed()
else:
print("Zoom presence:\t\t" + "Unknown")
switchBlue()
print()
countdown(int(sleepValue))