This repository has been archived by the owner on Feb 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluxateams.py
executable file
·74 lines (54 loc) · 1.86 KB
/
luxateams.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
#!/usr/bin/env python3
import atexit
import signal
import sys
import threading
from typing import Dict
from busylight.lights.luxafor import Flag
# We only want this if we're on macOS
if sys.platform == "darwin":
from notifypy.notify import Notify
from aad.authentication import authenticate
from luxateams import config
from teams.presence import Activity, get_presence
def set_light(flag: Flag, status_map: Dict[str, str], status: Activity):
status_color = config.to_rgb(status_map[status.value])
flag.on(status_color)
def graceful_exit(signal, frame) -> None:
sys.exit(0)
def shutdown_light(flag: Flag) -> None:
if sys.platform == "darwin":
notification = Notify()
notification.title = "Luxateams exited"
notification.send(block=True)
flag.off()
def main() -> None:
flag = Flag.first_light()
# Handle Ctrl+C
atexit.register(shutdown_light, flag)
signal.signal(signal.SIGINT, graceful_exit)
configuration = config.load_config()
result = authenticate(configuration)
ticker = threading.Event()
attempts = 0
while not ticker.wait(configuration.check_interval):
if 'access_token' in result:
presence = None
try:
presence = get_presence(result['access_token'])
except Exception as e:
print(f"Error getting presence: {e}")
if presence is None and attempts < 5:
# We had an auth error, try authenticating again
flag.on([255, 128, 0])
result = authenticate(configuration)
attempts += 1
else:
set_light(flag, configuration.activity_map, presence)
attempts = 0
else:
print('No access token found!')
flag.on([255, 128, 0])
sys.exit(1)
if __name__ == '__main__':
main()