-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonHandlers.py
104 lines (78 loc) · 3.73 KB
/
JsonHandlers.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
import os
import json
import logging
logging.basicConfig(encoding='utf-8', level=logging.ERROR)
class ConfigHandler():
#this class is used to load different configs stored in json files in Autohausmain/Configs
def __init__(self):
pass
def getAllPins(self, mode = "all"):
#load pins.json and return it as a list of dicts
pins = json.load(open(os.path.join(os.path.dirname(__file__),"../Configs/pins.json")))
if(mode == "all"):
return pins
else:
return [pin for pin in pins if pin["mode"] == mode]
def getPin(self,pinID):
#load pins.json and return it as a list of dicts
pins = json.load(open(os.path.join(os.path.dirname(__file__),"../Configs/pins.json")))
for pin in pins:
if(pin["pinID"] == pinID):
return pin
return None
def getSensors(self,onlyActive = True):
#load sensors.json and return it as a list of dicts
sensors = json.load(open(os.path.join(os.path.dirname(__file__),"../Configs/sensors.json")))
if(onlyActive):
sensors = [sensor for sensor in sensors if sensor["active"] == True]
return sensors
def addSensor(self, name: str, pinID: int, className: str, active: bool=True):
json_file = os.path.join(os.path.dirname(__file__), "../Configs/sensors.json")
with open(json_file, "r") as f:
data = json.load(f)
# check if name already exists
if any((sensor["name"] == name) for sensor in data):
logging.error(f"Name {name} already exists!")
return False
# add new actuator
new_sensor = {"active": active, "name": name, "pinID": pinID, "class": className}
data.append(new_sensor)
# write data back to file but keep file readable with \n
with open(json_file, "w") as f:
json.dump(data, f, indent=4)
f.write("\n")
return True
def getActuators(self,onlyActive = True):
actuators = json.load(open(os.path.join(os.path.dirname(__file__),"../Configs/actuators.json")))
if(onlyActive):
actuators = [actuator for actuator in actuators if actuator["active"] == True]
return actuators
def addActuator(self, name: str, type: str, collection: str, config: dict, active: bool=True,configIsUnique=True):
json_file = os.path.join(os.path.dirname(__file__), "../Configs/actuators.json")
with open(json_file, "r") as f:
data = json.load(f)
# if config is unique, check if config already exists
if configIsUnique:
for entry in data:
if entry["config"] == config:
logging.error(f"Config already exists!\n{entry}")
return False
# check if name already exists
if any((actuator["name"] == name) for actuator in data):
logging.error(f"Name {name} already exists!")
return False
# add new actuator
new_actuator = {"active": active, "name": name, "type": type, "collection": collection, "config": config}
data.append(new_actuator)
# write data back to file but keep file readable with \n
with open(json_file, "w") as f:
json.dump(data, f, indent=4)
f.write("\n")
return True
def getLogics(self,onlyActive = True):
logics = json.load(open(os.path.join(os.path.dirname(__file__),"../Configs/logics.json")))
if(onlyActive):
logics = [logic for logic in logics if logic["active"] == True]
return logics
def getMainConfig(self):
return json.load(open(os.path.join(os.path.dirname(__file__),"../Configs/mainSystem.json")))