-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
59 lines (45 loc) · 1.78 KB
/
backend.py
File metadata and controls
59 lines (45 loc) · 1.78 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
#!/usr/bin/python
import os
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import requests
import sys
# API is unsecured right now (yeah....) so don't include it in the code
gdac_discord_bot_api_url = os.environ.get('GDAC_DISCORD_BOT_API_URL')
observe_path = './pico8-home/carts'
lab_status_filename = 'lab_status.txt'
sep = os.sep
class FileChangeHandler(FileSystemEventHandler):
def on_modified(self, event):
print(f'event type: {event.event_type} path : {event.src_path}')
filename = event.src_path.split(sep)[-1]
path = event.src_path
# If the lab status changes, update discord bot status
if(filename == lab_status_filename):
self.update_lab_status_discord_bot(path)
def update_lab_status_discord_bot(self, status_file_path):
print("lab open file has been modified")
new_state = open(status_file_path, 'r').read().strip()
print("Setting room to " + new_state)
print("Calling endpoint: " + gdac_discord_bot_api_url)
# will need to match API of the gdac discord bot
body = {'setRoomState': int(new_state)}
response = requests.post(gdac_discord_bot_api_url, json = body)
print(response.text)
if __name__ == "__main__":
if gdac_discord_bot_api_url is None:
print("Please set the GDAC_DISCORD_BOT_API_URL environment variable")
sys.exit(1)
print("Starting file watcher")
# Watchdog observer for file changes
event_handler = FileChangeHandler()
observer = Observer()
observer.schedule(event_handler, path=observe_path, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()