-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirtualDeviceHost.py
67 lines (53 loc) · 3.11 KB
/
VirtualDeviceHost.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
from VirtualDeviceRepository import VirtualDeviceRepository
from Monitoring.DeviceHealthPublisher import DeviceHealthPublisher
from ConfigurationFactory import ConfigurationFactory
from BrokerConnectionRepository import BrokerConnectionRepository
class VirtualDeviceHost:
virtual_device_repository = None
broker_connection_repository = None
device_health_publisher = None
active_configuration = None
def initialize(self, configuration_name):
# load configuration
configuration_factory = ConfigurationFactory()
self.active_configuration = configuration_factory.create_configuration(configuration_name)
# initialize repositories
self.virtual_device_repository = VirtualDeviceRepository()
self.broker_connection_repository = BrokerConnectionRepository()
# initialize broker connections
broker_connections = self.active_configuration.create_broker_connections()
for bc in broker_connections:
self.broker_connection_repository.add_broker_connection(bc)
# initialize virtual devices
virtual_devices = self.active_configuration.create_virtual_devices()
for vd in virtual_devices:
self.virtual_device_repository.add_virtual_device(vd)
# initialize monitoring (if enabled)
if self.active_configuration.is_monitoring_enabled():
device_health_publisher_broker_connection = self.broker_connection_repository.get_broker_connection(
self.active_configuration.get_monitoring_broker_connection())
self.device_health_publisher = DeviceHealthPublisher(self.virtual_device_repository,
device_health_publisher_broker_connection,
self.active_configuration.get_monitoring_output_topic()
, 5, self.active_configuration
.get_monitoring_grace_period_duration())
def start(self):
for bc in self.broker_connection_repository.get_all_broker_connections():
bc.start_receiving(self.on_handle_message)
virtual_devices = self.virtual_device_repository.get_all_virtual_devices()
for vd in virtual_devices:
vd.start(self.broker_connection_repository)
if self.active_configuration.is_monitoring_enabled():
self.device_health_publisher.start()
def stop(self):
virtual_devices = self.virtual_device_repository.get_all_virtual_devices()
for vd in virtual_devices:
vd.stop()
if self.active_configuration.is_monitoring_enabled():
self.device_health_publisher.stop()
for bc in self.broker_connection_repository.get_all_broker_connections():
bc.stop_receiving()
def on_handle_message(self, broker_connection, topic, msg):
virtual_devices = self.virtual_device_repository.get_all_virtual_devices()
for vd in virtual_devices:
vd.handle_mqtt_message(broker_connection, topic, msg)