Пример #1
0
 def test_fetch_digital_twin(self, mock_mongo, mock_get_doc_data, mock_adapter):
     mock_mongo.return_value = None
     mock_get_doc_data.return_value = None
     mock_adapter.return_value = "test"
     db_handler = DbHandler(queue=self.test_queue, thread_event=self.test_event)
     emtpy_queue(queue=self.test_queue)
     db_handler._fetch_digital_twin()
     item = self.test_queue.get()
     correct_msg = ObserverMessage(event="digital_twin", data="test", subject="retrieved_digital_twin")
     correct_msg.source = "DbHandler"
     self.assertEqual(correct_msg, item)
Пример #2
0
 def test_store_remote_digital_twin(self):
     emtpy_queue(queue=self.test_queue)
     device_manager = DeviceManager(queue=self.test_queue, thread_event=self.default_event)
     device_manager.start()
     moch_dt = ["abc"]
     notify_msg = ObserverMessage(event="digital_twin", subject="retrieved_digital_twin", data=moch_dt)
     device_manager.notify(msg=notify_msg)
     sleep(0.5)
     self.assertEqual(device_manager.remote_digital_twin, moch_dt)
     device_manager.running = False
     device_manager.notify(msg=ObserverMessage(data="", event=""))
Пример #3
0
 def test_handle_digital_twin_fetch_event(self, mock_mongo, mock_fetch_dt):
     mock_mongo.return_value = None
     mock_fetch_dt.return_value = None
     db_handler = DbHandler(queue=self.test_queue, thread_event=self.test_event)
     msg = ObserverMessage("digital_twin", [], subject="fetch_digital_twin")
     db_handler.handle_digital_twin(msg)
     self.assertTrue(mock_fetch_dt.called)
 def test_to_str(self):
     event_name = "test"
     data = "hello"
     subject = "test 1"
     result = "ObserverMessage {'event': 'test', 'data': 'hello', 'subject': 'test 1', 'destination': None, 'source': 'TestObserverMessage'}"
     msg = ObserverMessage(event=event_name, data=data, subject=subject)
     self.assertEqual(str(msg), result)
Пример #5
0
 def test_handle_digital_twin_save_event_fake(self, mock_mongo, mock_skip):
     mock_mongo.return_value = None
     mock_skip.return_value = None
     db_handler = DbHandler(queue=self.test_queue, thread_event=self.test_event)
     msg = ObserverMessage("digital_twin", [], subject="fake subject")
     db_handler.handle_digital_twin(msg)
     self.assertTrue(mock_skip.called)
Пример #6
0
 def _fetch_digital_twin(self) -> None:
     self.log.info("Fetching digital twin from DB")
     twin_data = self.get_document_data(document="digital_twin")
     digital_twin = self._outbound_adapter(data=twin_data)
     msg = ObserverMessage(event="digital_twin",
                           data=digital_twin,
                           subject="retrieved_digital_twin")
     self.observer_publish_queue.put(msg)
Пример #7
0
 def _handle_system(self, msg: IotMessage) -> None:
     self.log.debug(f"Handling system message from device {msg.device_id}")
     if msg.device_id != "framework":
         message = {'device_id': msg.device_id}
         message.update(msg.payload)
         item = ObserverMessage(event="digital_twin",
                                data=message,
                                subject="device_status")
         self._observer_publish_queue.put(item)
Пример #8
0
 def _handle_state_change(self, msg: IotMessage) -> None:
     self.log.debug("Handling state event")
     message = {
         'device_id': msg.device_id,
         'event_type': msg.event,
         'state': msg.payload.get('state')
     }
     item = ObserverMessage(event="device_state_changed", data=message)
     self._observer_publish_queue.put(item)
Пример #9
0
 def _log_mqtt_traffic(self, topic: str, payload: str) -> None:
     data = {
         'timestamp': datetime.now(),
         'source': type(self).__name__,
         'topic': topic,
         'payload': payload
     }
     msg = ObserverMessage(event="iot_traffic", data=data)
     self._observer_publish_queue.put(msg)
 def test_init(self):
     event_name = "test"
     data = "hello"
     subject = "test 1"
     msg = ObserverMessage(event=event_name, data=data, subject=subject)
     self.assertEqual(msg.event, event_name)
     self.assertEqual(msg.data, data)
     self.assertEqual(msg.subject, subject)
     self.assertEqual(msg.source, self.__class__.__name__)
     self.assertEqual(msg.destination, None)
Пример #11
0
 def test_notify(self, mock_mongo):
     mock_mongo.return_value = None
     db_handler = DbHandler(queue=self.test_queue, thread_event=self.test_event)
     data = {'test': 123}
     event = 'test'
     msg = ObserverMessage(event=event, data=data)
     db_handler.notify(msg=msg)
     queue_item = db_handler.observer_notify_queue.get()
     self.assertEqual(queue_item.event, event)
     self.assertEqual(queue_item.data, data)
Пример #12
0
    def run(self) -> None:
        self.log.info(f'Updating system information every {self.update_time_sec} seconds.')
        self._thread_ready.set()
        while self.running:
            start_time = time()

            host_data = self._fetch_host_data()
            msg = ObserverMessage(event="host_health", data=host_data)
            self.observer_publish_queue.put(msg)

            sleep_time = self.update_time_sec - ((time() - start_time) % self.update_time_sec)
            sleep(sleep_time)
Пример #13
0
 def test_store_state_data_new(self, mock_mongo):
     mock_mongo.return_value = None
     db_handler = DbHandler(queue=self.test_queue, thread_event=self.test_event)
     db_handler.mongo = MockMongoNoDeviceNameExist
     db_handler.store_state_data(msg=ObserverMessage(event="pass", data={}))
Пример #14
0
 def _fetch_digital_twin(self):
     msg = ObserverMessage(event="digital_twin",
                           data={},
                           subject="fetch_digital_twin")
     self._observer_publish_queue.put(msg)
Пример #15
0
 def _publish_digital_twin(self, twin: Union[list, dict]) -> None:
     msg = ObserverMessage(event="digital_twin",
                           data=twin,
                           subject="save_digital_twin")
     self._observer_publish_queue.put(msg)
Пример #16
0
 def _publish_device_status_poll(self):
     msg = ObserverMessage(event="digital_twin",
                           data={},
                           subject="poll_devices")
     self._observer_publish_queue.put(msg)
Пример #17
0
 def test_add_document_row(self, mock_mongo):
     mock_mongo.return_value = None
     db_handler = DbHandler(queue=self.test_queue, thread_event=self.test_event)
     db_handler.mongo = MockMongo
     db_handler.add_document_row(msg=ObserverMessage(event="pass", data=""))
Пример #18
0
 def _handle_telemetry(self, msg: IotMessage) -> None:
     self.log.debug("Handling telemetry event")
     message = {'timestamp': datetime.now(), 'device_id': msg.device_id}
     message.update(msg.payload)
     item = ObserverMessage(event="device_sensor_data", data=message)
     self._observer_publish_queue.put(item)