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)
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=""))
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)
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)
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)
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)
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)
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)
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)
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)
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={}))
def _fetch_digital_twin(self): msg = ObserverMessage(event="digital_twin", data={}, subject="fetch_digital_twin") self._observer_publish_queue.put(msg)
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)
def _publish_device_status_poll(self): msg = ObserverMessage(event="digital_twin", data={}, subject="poll_devices") self._observer_publish_queue.put(msg)
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=""))
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)