-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_config_mgt.py
255 lines (222 loc) · 9.33 KB
/
class_config_mgt.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import json
from datetime import datetime, timedelta
import fcntl
class ConfigManager:
DEFAULT_STATUS = "00 - Raw" # Define a constant for the default status
DEFAULT_REGISTERED = False # Define a constant for the default registered status
DEFAULT_DEVICE = 0 # Define a constant for the default device
DEFAULT_LAT = 51.1 # Define a constant for the default latitude
DEFAULT_LONG = 0.1 # Define a constant for the default longitude
def __init__(self, file='config.json'):
self.config_file = file
self.config = self.load_config()
def load_config(self):
try:
with open(self.config_file, 'r+') as f:
fcntl.flock(f, fcntl.LOCK_EX) # Acquire an exclusive lock
try:
config = json.load(f)
fcntl.flock(f, fcntl.LOCK_UN) # Release the lock
except json.JSONDecodeError:
fcntl.flock(f, fcntl.LOCK_UN) # Release the lock
config = self.create_default_config()
self.save_config(config)
return config
except FileNotFoundError:
return self.create_default_config()
def save_config(self, config):
with open(self.config_file, 'w') as f:
fcntl.flock(f, fcntl.LOCK_EX) # Acquire an exclusive lock
json.dump(config, f, indent=4)
fcntl.flock(f, fcntl.LOCK_UN) # Release the lock
def create_default_config(self):
default_config = {
"device_id": self.DEFAULT_DEVICE,
"registered": self.DEFAULT_REGISTERED,
"status": self.DEFAULT_STATUS,
"lat": self.DEFAULT_LAT,
"long": self.DEFAULT_LONG,
"sensor_co2": "scd30",
"sensor_pressure": "bme280",
"last_calibration_date": 0,
"last_calibration_value": 0
}
self.save_config(default_config)
return default_config
def get_device_id(self):
# Retrieve the device_id from config
device_id = self.config.get("device_id", None)
# If device_id is 0, save the config and return 0
if device_id == 0:
self.save_config(self.config)
return device_id
def set_device_id(self, device_id):
self.config["device_id"] = device_id
self.save_config(self.config)
def get_status(self):
# Retrieve the status from config with a default value
status = self.config.get("status", self.DEFAULT_STATUS)
# If the status is the default value, save the config
if status == self.DEFAULT_STATUS:
self.save_config(self.config)
return status
def set_status(self, status):
self.config["status"] = status
self.save_config(self.config)
def is_registered(self):
# Retrieve the registered status from config with a default value
registered = self.config.get("registered", self.DEFAULT_REGISTERED)
# If registered is False, save the config
if not registered:
self.save_config(self.config)
return registered
def set_registered(self, registered=True):
self.config["registered"] = registered
self.save_config(self.config)
def get_lat(self):
return float(self.config.get("lat", self.DEFAULT_LAT))
def set_lat(self, lat):
self.config["lat"] = round(lat,4)
self.save_config(self.config)
def get_long(self):
return float(self.config.get("long", self.DEFAULT_LONG))
def set_long(self, long):
self.config["long"] = round(long,4)
self.save_config(self.config)
def get_time_interval_values(self, interval="min"):
if "time_intervals" not in self.config:
self.config["time_intervals"] = {
"month": {
"start":datetime.now().isoformat(),
"co2": 0,
"temperature": 0,
"humidity": 0,
"pressure": 0,
"count": 0
},
"min": {
"start":datetime.now().isoformat(),
"co2": [],
"temperature": [],
"humidity": [],
"pressure": [],
},
"hour": {
"start":datetime.now().isoformat(),
"co2": 0,
"temperature": 0,
"humidity": 0,
"pressure": 0,
"count": 0
},
"day": {
"start":datetime.now().isoformat(),
"co2": 0,
"temperature": 0,
"humidity": 0,
"pressure": 0,
"count": 0
}
}
self.save_config(self.config)
if interval in self.config["time_intervals"]:
return self.config["time_intervals"][interval]
else:
raise KeyError(f"Invalid interval '{interval}' for time intervals")
def remove_interval(self, interval):
del self.config["time_intervals"][interval]
if interval == "min":
self.config["time_intervals"]["min"] = {
"start":datetime.now().isoformat(),
"co2": [],
"temperature": [],
"humidity": [],
"pressure": [],
}
else:
self.config["time_intervals"][interval]= {
"start":datetime.now().isoformat(),
"co2": 0,
"temperature": 0,
"humidity": 0,
"pressure": 0,
"count":0
}
self.save_config(self.config)
return self.config
def set_time_interval_values(self, date_time, sensor_value_dict):
if "min" not in self.config["time_intervals"]:
self.config["time_intervals"]["min"] = {
"start":date_time,
"co2": [],
"temperature": [],
"humidity": [],
"pressure": [],
}
if "hour" not in self.config["time_intervals"]:
self.config["time_intervals"]["hour"]= {
"start":date_time,
"co2": 0,
"temperature": 0,
"humidity": 0,
"pressure": 0,
"count":0
}
if "day" not in self.config["time_intervals"]:
self.config["time_intervals"]["day"]= {
"start":date_time,
"co2": 0,
"temperature": 0,
"humidity": 0,
"pressure": 0,
"count":0
}
if "month" not in self.config["time_intervals"]:
self.config["time_intervals"]["month"]= {
"start":date_time,
"co2": 0,
"temperature": 0,
"humidity": 0,
"pressure": 0,
"count":0
}
for interval in self.config["time_intervals"]:
match interval:
case "min":
self.config["time_intervals"][interval]["co2"].append(sensor_value_dict["co2"])
self.config["time_intervals"][interval]["temperature"].append(sensor_value_dict["temperature"])
self.config["time_intervals"][interval]["humidity"].append(sensor_value_dict["humidity"])
self.config["time_intervals"][interval]["pressure"].append(sensor_value_dict["pressure"])
case _:
self.config["time_intervals"][interval]["co2"] += sensor_value_dict["co2"]
self.config["time_intervals"][interval]["temperature"] += sensor_value_dict["temperature"]
self.config["time_intervals"][interval]["humidity"] += sensor_value_dict["humidity"]
self.config["time_intervals"][interval]["pressure"] += sensor_value_dict["pressure"]
self.config["time_intervals"][interval]["count"] += 1
self.save_config(self.config)
return self.config
# Usage example:
if __name__ == "__main__":
config_manager = ConfigManager()
print("\nTest 10 return device ID:")
print("Device ID:", config_manager.get_device_id())
print("\nTest 20 return Latitude:")
print("Latitude:", config_manager.get_lat())
print("\nTest 30 return Longitude:")
print("Longitude:", config_manager.get_long())
config_manager = ConfigManager("sensor_values.json")
print("\nTest 40 return minute subtotals:")
# Set and get sensor values for different time intervals
config_manager.set_time_interval_values(datetime.now(), {
"co2": 420,
"temperature": 22.4,
"humidity": 47.1,
"pressure": 1024
} )
print("Minutes:", config_manager.get_time_interval_values("min"))
print("\nTest 50 return invalid time period - ERROR:")
# Try to get an invalid time interval value
try:
print("Invalid key:", config_manager.get_time_interval_values("weeks"))
except KeyError as e:
print("Error:", e)