forked from richibrics/PyMonitorMQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonitor.py
57 lines (48 loc) · 2.27 KB
/
Monitor.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
import Sensors
import Commands
import Managers
from MqttClient import MqttClient
import Logger
import multiprocessing
from consts import *
class Monitor():
def __init__(self, config, globalConfig, commandManager, sensorManager, monitor_id=1):
self.config = config
self.globalConfig = globalConfig
self.monitor_id = monitor_id
self.commandManager = commandManager
self.sensorManager = sensorManager
self.Setup()
def Setup(self):
# Setip logger
self.logger = Logger.Logger(self.globalConfig, self.monitor_id)
self.Log(Logger.LOG_INFO, 'Starting')
# Setup MQTT client
self.mqttClient = MqttClient(self.config, self.logger)
self.LoadSensors()
self.LoadCommands()
def LoadSensors(self):
# 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.sensorManager.PostInitializeSensors()
if CONFIG_SENSORS_KEY in self.config:
sensorsToAdd = self.config[CONFIG_SENSORS_KEY]
for sensor in sensorsToAdd:
self.sensorManager.LoadSensor(
sensor, self.monitor_id, self.config, self.mqttClient, self.config['send_interval'], self.logger)
# Some need post-initialize configuration
self.sensorManager.PostInitializeSensors()
# All configurations must go above
def LoadCommands(self):
# From configs I read commands list and I give the names to the commands manager which will initialize them
# and will keep trace of who is the mqtt_client and the logger of the command
if CONFIG_COMMANDS_KEY in self.config:
commandsToAdd = self.config[CONFIG_COMMANDS_KEY]
for command in commandsToAdd:
self.commandManager.LoadCommand(
command, self.monitor_id, self.config, self.mqttClient, self.logger)
# Some need post-initialize configuration
self.commandManager.PostInitializeCommands()
# All configurations must go above
def Log(self, messageType, message):
self.logger.Log(messageType, 'Main', message)