-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
111 lines (95 loc) · 4.33 KB
/
main.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
import json
import os
import time
from google_interface import GoogleInterface
from monitor import Monitor
from util import *
from web_server import WebServer
# Config
SPREADSHEET_ID = ""
ENABLE_MONITOR = True
# Cache paths
DATA_FOLDER = "data"
CRED_FILE_PATH = "google_credentials.json"
CONFIG_CACHE_FILENAME = "config_cache.json"
BACKGROUND_CACHE_FOLDER = "backgrounds"
# Global variables
config_cache = {"general": {}, "people": []}
data_cache = {"devices": [], "records": []}
google_interface = None
web_server = None
monitor = None
# The status lights on the main page indicate DISCONNECTED, WARNING, or CONNECTED:
#
# "Server": DISCONNECTED = The WebSocket connection is currently closed
# CONNECTED = The WebSocket connection is currently open
#
# "Monitor": DISCONNECTED = Data is unavailable due to an error.
# WARNING = No devices are visible on the network (might be disconnected).
# CONNECTED = At least one device is visible on the network.
#
# "Google": DISCONNECTED = There was an error with the connection or authentication.
# WARNING = There was an unknown error, possibly due to unexpected data in the sheet.
# CONNECTED = There are no issues with the connection.
def update_config_cache(new_config):
"""Callback to update the config cache from Google, writing to disk and pushing to all modules."""
global config_cache
if new_config != config_cache:
log("Config cache has changed, saving to file and sending to web server")
config_cache = new_config
json.dump(config_cache, open(
get_absolute_path(DATA_FOLDER, CONFIG_CACHE_FILENAME), "w"))
web_server.new_config()
def update_data_cache(new_data):
"""Callback to update the data cache from Google, pushing to all modules."""
global data_cache
if new_data != data_cache:
log("Data cache has changed, sending to web sever")
data_cache = new_data
web_server.new_data()
if __name__ == "__main__":
# Create data and background folders
data_path = get_absolute_path(DATA_FOLDER)
if not os.path.isdir(data_path):
os.makedirs(data_path)
backgrounds_path = get_absolute_path(DATA_FOLDER, BACKGROUND_CACHE_FOLDER)
if not os.path.isdir(backgrounds_path):
os.makedirs(backgrounds_path)
# Read initial config cache
config_path = get_absolute_path(DATA_FOLDER, CONFIG_CACHE_FILENAME)
if os.path.isfile(config_path):
config_cache = json.load(open(config_path))
# Instantiate components
google_interface = GoogleInterface(DATA_FOLDER, CRED_FILE_PATH, BACKGROUND_CACHE_FOLDER, SPREADSHEET_ID,
lambda status: web_server.new_google_status(
status),
lambda new_config: update_config_cache(
new_config),
lambda new_data: update_data_cache(
new_data),
lambda: web_server.new_backgrounds())
web_server = WebServer(DATA_FOLDER, BACKGROUND_CACHE_FOLDER, lambda: config_cache,
lambda: data_cache,
lambda person: google_interface.add_sign_in(
person, True),
lambda person: google_interface.add_sign_out(
person, True),
lambda person, mac: google_interface.add_device(
person, mac),
lambda person, mac: google_interface.remove_device(person, mac))
monitor = Monitor(lambda: config_cache,
lambda: data_cache,
lambda status: web_server.new_monitor_status(status),
lambda person, event_time: google_interface.add_sign_in(
person, False, event_time),
lambda person, event_time: google_interface.add_sign_out(
person, False, event_time),
lambda person, mac: google_interface.update_device_last_seen(person, mac))
# Start components
google_interface.start()
web_server.start()
if ENABLE_MONITOR:
monitor.start()
# Loop forever
while True:
time.sleep(1)