-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoffice_status_indicator.py
126 lines (86 loc) · 3.16 KB
/
office_status_indicator.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
import json
import os
import time
import pytz
from datetime import datetime, timedelta, timezone
from calendar_setup import get_calendar_service
from unicornhatmini import UnicornHATMini
unicornhatmini = UnicornHATMini()
unicornhatmini.set_brightness(0.1)
# Global variables
OFFICE_STATUS_HOUR_START = int(os.getenv("OFFICE_STATUS_HOUR_START", 8))
OFFICE_STATUS_HOUR_END = int(os.getenv("OFFICE_STATUS_HOUR_END", 18))
OFFICE_STATUS_TZ = os.getenv("OFFICE_STATUS_TZ", "America/New_York")
OFFICE_STATUS_WARNING_MINUTES = int(os.getenv("OFFICE_STATUS_WARNING_MINUTES", 10))
OFFICE_STATUS_WEEK_START = int(os.getenv("OFFICE_STATUS_WEEK_START", 0))
OFFICE_STATUS_WEEK_END = int(os.getenv("OFFICE_STATUS_WEEK_END", 4))
def get_events():
# Get the calendar service
service = get_calendar_service()
# Call the Calendar API
now = datetime.utcnow().isoformat() + 'Z'
print('Getting list of calendar events...')
events_result = service.events().list(calendarId='primary',
timeMin=now,
maxResults=10,
singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
return events
def set_red():
set_color(255, 0, 0)
def set_orange():
set_color(255, 144, 0)
def set_green():
set_color(0, 255, 0)
def set_off():
set_color(0, 0, 0)
def set_color(r, g, b):
unicornhatmini.set_all(r, g, b)
unicornhatmini.show()
def read_datetime(timestamp):
return datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S%z")
def main():
current_time = datetime.now(timezone.utc)
# Convert to the desired timezone
pytz_timezone = pytz.timezone(OFFICE_STATUS_TZ)
current_tz_time = current_time.astimezone(pytz_timezone)
print(f"Current Time: {current_tz_time}")
try:
# Refresh calendar events
events = get_events()
except Exception:
print("Failed to fetch calendar events from Google.")
return
# For each event
for event in events:
if event.get("transparency", "") == "transparent":
continue
# Get the datetime for the start/end of the event
start_time = read_datetime(event["start"]["dateTime"])
end_time = read_datetime(event["end"]["dateTime"])
print(f"Next Calendar Event: {start_time}")
if start_time < current_time < end_time:
# In a Meeting
set_red()
return
elif start_time < current_time + timedelta(minutes=OFFICE_STATUS_WARNING_MINUTES):
# Meeting Soon
set_orange()
return
else:
break
# If it's a weekday
if OFFICE_STATUS_WEEK_START <= current_tz_time.weekday() <= OFFICE_STATUS_WEEK_END:
# If we're during working hours
if OFFICE_STATUS_HOUR_START <= current_tz_time.hour < OFFICE_STATUS_HOUR_END:
# No current/upcoming meetings
set_green()
return
print(f"Not working hours...")
set_off()
return
if __name__ == '__main__':
while True:
main()
time.sleep(60)