-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMonitor.py
More file actions
81 lines (69 loc) · 3.88 KB
/
Copy pathMonitor.py
File metadata and controls
81 lines (69 loc) · 3.88 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from Configurator import Configurator as cf
from MqttClient import MqttClient
from Logger import Logger, ExceptionTracker
import multiprocessing
from consts import *
class Monitor():
def __init__(self, config, globalConfig, entityManager, monitor_id=1):
self.config = config
self.globalConfig = globalConfig
self.monitor_id = monitor_id
self.entityManager = entityManager
# Some Sensors and Commands, after load, will return which sensor or command they need to run
self.requirements = []
self.loadedEntities = [] # To avoid reload for requirements, something is working
self.Setup()
def Setup(self):
# Setup logger
self.logger = Logger(self.globalConfig, self.monitor_id)
self.Log(Logger.LOG_INFO, 'Starting')
# Setup MQTT client
self.mqttClient = MqttClient(self.config, self.logger)
# HERE I TAKE THE SENSORS AND COMMANDS NAME FROM THE CONFIGURATION AND ASSIGN THE SUFFIX TO THE NAME TO LOAD THEM CORRECTLY. Add here the Custom part
if CONFIG_SENSORS_KEY in self.config:
self.LoadEntities(
self.config[CONFIG_SENSORS_KEY], SENSOR_NAME_SUFFIX)
if CONFIG_COMMANDS_KEY in self.config:
self.LoadEntities(
self.config[CONFIG_COMMANDS_KEY], COMMAND_NAME_SUFFIX)
# While because some requirements may need other requirements themselves
while(len(self.requirements)):
self.Log(Logger.LOG_INFO, "Loading dependencies...")
self.LoadRequirements()
# Some need post-initialize configuration
self.entityManager.PostInitializeEntities()
# Some need post-initialize configuration
# self.commandManager.PostInitializeCommands()
def LoadEntities(self, entitiesToAdd, name_suffix, loadingRequirements=False):
# From configs I read sensors list and I give the names to the sensors manager which will initialize them
# and will keep trace of who is the mqtt_client and the logger of the sensor
# self.entityManager.PostInitializeEntities()
if entitiesToAdd:
for entity in entitiesToAdd:
# I load the sensor and if I need some requirements, I save them to the list
# Additional check to not load double if I am loading requirements
if not (loadingRequirements and entity in self.loadedEntities):
settings = self.entityManager.LoadEntity(name_suffix,
entity, self.monitor_id, self.config, self.mqttClient, self.config['send_interval'], self.logger)
requirements = cf.GetOption(
settings, SETTINGS_REQUIREMENTS_KEY)
if requirements:
self.requirements.append(requirements)
self.loadedEntities.append(entity)
def LoadRequirements(self):
# Here I load sensors and commands
# I have a dict with {'sensors':[SENSORS],'commands':[COMMANDS]}
# SENSORS and COMMANDS have the same format as the configutaration.yaml so I
# tell to LoadSensor and LoadCommands what to load with the usual method
for requirements in self.requirements:
sensors = cf.GetOption(
requirements, SETTINGS_REQUIREMENTS_SENSOR_KEY)
commands = cf.GetOption(
requirements, SETTINGS_REQUIREMENTS_COMMAND_KEY)
if sensors:
self.LoadEntities(sensors, SENSOR_NAME_SUFFIX, loadingRequirements=True)
if commands:
self.LoadEntities(commands, COMMAND_NAME_SUFFIX, loadingRequirements=True)
self.requirements.remove(requirements)
def Log(self, messageType, message):
self.logger.Log(messageType, 'Main', message)