예제 #1
0
    def on_message(self, topic: str, payload: str) -> None:
        self.log.info(f'Received {payload!r} on topic {topic!r}')
        self._log_mqtt_traffic(topic=topic, payload=payload)

        message = IotMessage(mqtt_topic=topic, data=payload)
        if message.is_valid():
            handler = self._select_handler(event=message.event)
            handler(msg=message)
        else:
            self.log.warning('The MQTT message is not valid')
예제 #2
0
    def test_is_valid_one_attr_not_set(self):
        msg = IotMessage(mqtt_topic=self.test_topic, data=self.test_payload)

        mock_attr_event = mock.PropertyMock(return_value='data123')
        mock_attr_device_id = mock.PropertyMock(return_value=None)
        mock_attr_payload = mock.PropertyMock(return_value={'data': 'test'})
        type(msg).event = mock_attr_event
        type(msg).device_id = mock_attr_device_id
        type(msg).payload = mock_attr_payload

        self.assertFalse(msg.is_valid())
예제 #3
0
 def test__destruct_topic_bad_topics(self):
     for topic in [
             'iot/devices/', 'iot/device/d/state',
             'iot_/devices/test/state', 'devices/test/state'
     ]:
         destructed_topic = IotMessage._destruct_topic(topic=topic,
                                                       item_index=2)
         self.assertIsNone(destructed_topic)
예제 #4
0
 def test_init(self, mock_get_event, mock_get_dev_id, mock_parse_data):
     mock_get_event.return_value = 'state'
     mock_get_dev_id.return_value = 'dev001'
     mock_parse_data.return_value = {'state': True}
     msg = IotMessage(mqtt_topic=self.test_topic, data=self.test_payload)
     self.assertEqual(msg.event, 'state')
     self.assertEqual(msg.device_id, 'dev001')
     self.assertDictEqual(msg.payload, {'state': True})
예제 #5
0
 def test__destruct_topic_device_id(self):
     dev_id = IotMessage._destruct_topic(topic=self.test_topic,
                                         item_index=2)
     self.assertEqual(dev_id, 'dev_001')
예제 #6
0
 def test__destruct_topic_event(self):
     event = IotMessage._destruct_topic(topic=self.test_topic, item_index=3)
     self.assertEqual(event, 'state')