-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
99 lines (76 loc) · 3.29 KB
/
config.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
import yaml
import os
from datetime import datetime, time as dttime
class Config:
SCHEDULE_START = dttime(6, 30)
SCHEDULE_END = dttime(22, 0)
ACTIVE_TIMEOUT = 10
DOWN_TIMEOUT = 5
RESPONSE_TIMEOUT = 5
ACTIVE_TEXT = "the internet connection has been restored"
DOWN_TEXT = "the internet is currently unreachable"
LATENCY_HIGH_TEXT = "the internet speed is currently slow"
LATENCY_NORMAL_TEXT = "the internet speed is back to normal"
ACTIVE_FILE = "audio/active.mp3"
DOWN_FILE = "audio/down.mp3"
SLOW_FILE = "audio/slow.mp3"
NORMAL_FILE = "audio/normal.mp3"
LATENCY_MIN = 0.200
LATENCY_COUNT = 3
NOTIFY_ACTIVE_VOICE = True
NOTIFY_ACTIVE_LED = False
NOTIFY_ACTIVE_LOG = False
NOTIFY_LATENCY_VOICE = True
NOTIFY_LATENCY_LED = False
NOTIFY_LATENCY_BEEP = True
NOTIFY_LATENCY_LOG = True
LOG_ENABLED = True
LOG_FILE = "log.csv"
LOG_ACTIVE = True
LOG_LATENCY = True
def __init__( self, file ):
self.CONFIG_FILE = file
self.config_modified = None
self.load()
def load( self ) -> None:
self.config_modified = os.path.getmtime( self.CONFIG_FILE )
try:
with open(self.CONFIG_FILE, "r") as f:
config = yaml.safe_load(f)
self.SCHEDULE_START = dttime( config["schedule"]["start_hour"], 0 )
self.SCHEDULE_END = dttime( config["schedule"]["end_hour"], 0 )
self.ACTIVE_TIMEOUT = config["timeout"]["active"]
self.DOWN_TIMEOUT = config["timeout"]["down"]
self.RESPONSE_TIMEOUT = config["timeout"]["response"]
self.ACTIVE_TEXT = config["phrase"]["active"]
self.DOWN_TEXT = config["phrase"]["down"]
self.LATENCY_HIGH_TEXT = config["phrase"]["slow"]
self.LATENCY_NORMAL_TEXT = config["phrase"]["normal"]
self.ACTIVE_FILE = config["audio"]["active"]
self.DOWN_FILE = config["audio"]["down"]
self.LATENCY_MIN = config["latency"]["min"]
self.LATENCY_COUNT = config["latency"]["count"]
self.NOTIFY_ACTIVE_VOICE = config["notify"]["active"]["voice"]
self.NOTIFY_ACTIVE_LED = config["notify"]["active"]["led"]
self.NOTIFY_LATENCY_VOICE = config["notify"]["latency"]["voice"]
self.NOTIFY_LATENCY_LED = config["notify"]["latency"]["led"]
self.NOTIFY_LATENCY_BEEP = config["notify"]["latency"]["beep"]
self.LOG_ENABLED = config["log"]["enabled"]
self.LOG_FILE = config["log"]["file"]
self.LOG_ACTIVE = config["log"]["active"]
self.LOG_LATENCY = config["log"]["latency"]
print("Loaded config file.")
except Exception as e:
print( "Error loading config file. Key: " + str(e) )
print( "Failed to load config, using defaults")
def refresh( self ) -> bool:
last_modified = os.path.getmtime( self.CONFIG_FILE )
time_difference = last_modified - self.config_modified
if time_difference != 0:
print("Config file changed, reloading...")
self.load()
return True
return False
def is_in_schedule( self ) -> bool:
in_schedule = self.SCHEDULE_START <= datetime.now().time() <= self.SCHEDULE_END
return in_schedule