def on_message(self, client, userdata, msg): _topic = Topic(msg.topic) if _topic.get_prefix() != "device": LOGGER.error("Unknown message") # # 1. /device/<device_type>/<device_id>/connect # # 2. /device/<device_type>/<device_id>/disconnect # # 3. /device/<device_type>/<device_id>/set # # 4. /device/<device_type>/<device_id>/get # # 5. /device/<device_type>/<device_id>/update else: state = msg.payload LOGGER.info('the device:{}` has been `{}`, device Type: {}'.format( _topic.get_device_id(), _topic.get_action(), _topic.device_type())) id = tinydb.Query() if (self._database.search( id.device_id == str(_topic.get_device_id()))): # is exists self._database.update({'state': str(state)}, id.device_id == str( _topic.get_device_id())) else: self._database.insert({ 'device_id': str(_topic.get_device_id()), 'device_type': str(_topic.get_device_id()), 'action': str(_topic.get_action()) }) # Was state and changed to action