def req(self): if len(self._engineries) > 0: self.cur_e = self._engineries.pop(0) class_id = engineries.DimmingLight._cmds.index( engineries.historyRequestId) topic = TopicCommandTros3(self.id, entity_addr=EntityAddress('/'.join([ self.key, str(self.cur_e.id), str(class_id) ]))) data = { "algorithm": "raw", "startTime": "2020-08-01T00:00:00", "endTime": "2020-09-01T00:00:00", "key": f'{self.key}' } var = VariableTRS3(id=self.cur_e.id, cl=class_id, val=json.dumps(data)) try: self.start_timer() self.publish(topic, var) except BaseException as ex: logging.exception(ex) else: self.on_exit(0, None)
def on_message(self, mosq, obj, msg): if msg.topic.startswith(f'Tros3/Reply/'): var = VariableTRS3(VariableReader(msg.payload)) self.on_reply(var) else: topic = mqtt.of(msg.topic) if isinstance(topic, mqtt.TopicProject): self.on_project(msg.payload.decode())
def empty_data_founder(self, topic, data): try: if 'Jocket' in topic: data = make_jocket(data) elif 'Equipment' in topic: data = VariableTRS3(VariableReader(data)) else: return if data.value == '': print(f'{topic}') except BaseException as ex: print(str(ex)) finally: self.restart_timer()
def listen_all(self): while True: time.sleep(1) self._command_event.wait() try: for thing in THINGS: num = 0 for key, value in thing['topicValues'].items(): if key == 'isOpenedId': #value = self.get_from_DI() self.write_to_bro(thing['topicId'], num, value) else: self.write_to_bro(thing['topicId'], num, value) num = num + 1 except BaseException as ex: logging.exception(ex) for device in self.sock: for data in device.commands(): size = len(data) data = bytes.fromhex(data) try: tk = 2415 #out = device.send_message(data, size) #print(data) except BaseException as ex: logging.exception(ex) self.mqttc.publish(topic=topic_dump.format(BUS_ID) + '/error', payload=str(ex)) else: try: #tt=out[18:22] #tk=int(tt, 16) tk = tk + 27315 out = VariableTRS3(None, int(BUS_ID), 0, tk) top_out = topic_dump.format(PROJECT, BUS_ID, '0') self.mqttc.publish(topic=topic_dump.format( PROJECT, BUS_ID, '0'), payload=out.pack()) logging.debug('[ <-]: {}'.format(out)) except BaseException as ex: logging.exception(ex)
def write_to_bro(self, topId, num, value): out = VariableTRS3(None, topId, num, value) self.mqttc.publish(topic=topic_dump.format(PROJECT, str(topId), str(num)), payload=out.pack()) logging.debug('[ <-]: {}'.format(out))
def make_tros3(data): data = VariableTRS3(VariableReader(data)) return data
def on_tros3(self, _topic: mqtt.TopicData, data): _var = VariableTRS3(VariableReader(data)) e_id = _var.id timestamp = None if _var.timeStamp: timestamp = datetime.fromisoformat(str(_var.timeStamp)) if e_id in self.engineries: e_data = self.engineries[e_id] funit_name = e_data._cmds[_var.cl] if isinstance(_topic, mqtt.TopicStateTros3): value = _var.value if funit_name == engineries.isOnId: funit_type = F_ON elif funit_name == engineries.levelId: funit_type = F_BrightnessLevel elif funit_name == engineries.powerLevelId: funit_type = F_PowerLevel else: logging.debug(f'Unsupported funit "{funit_name}"') return if _var.invalid: value = None topic = spread.topic.State( spread.address.EngineryAddress(_topic.p_id, e_data.__class__.__name__, e_id, funit_type)) retain = True elif isinstance(_topic, mqtt.TopicCommandTros3): if funit_name == engineries.onId: funit_type = F_ON value = True elif funit_name == engineries.offId: funit_type = F_ON value = False elif funit_name == engineries.setLevelId: funit_type = F_BrightnessLevel value = _var.value elif funit_name == engineries.groupOnId: funit_type = F_GROUP_LEVEL_RAW value = 254 elif funit_name == engineries.groupOffId: funit_type = F_GROUP_LEVEL_RAW value = 0 elif funit_name == engineries.groupSetLevelId: funit_type = F_GROUP_LEVEL_RAW value = _var.value else: logging.debug(f'Unsupported funit "{funit_name}"') return topic = spread.topic.Event( spread.address.EngineryAddress(_topic.p_id, e_data.__class__.__name__, e_id, funit_type)) retain = False else: return elif e_id in self.subgineries: e_data = self.subgineries[e_id] funit_name = e_data._cmds[_var.cl] if isinstance(_topic, mqtt.TopicStateTros3): value = _var.value if funit_name == subgineries.isOnId: funit_type = F_SwitchOn elif funit_name == subgineries.isOffId: funit_type = F_SwitchOff elif funit_name == subgineries.isMatchScene1Id: funit_type = F_Scene1On elif funit_name == subgineries.isMatchScene2Id: funit_type = F_Scene2On elif funit_name == subgineries.powerLevelId: funit_type = F_PowerLevel elif funit_name == subgineries.isLightSensorsOnId: funit_type = F_LightSensorsOn value = True elif funit_name == subgineries.isLightSensorsOffId: funit_type = F_LightSensorsOn value = False elif funit_name == subgineries.isPresenceSensorsOnId: funit_type = F_PresenceSensorsOn value = True elif funit_name == subgineries.isPresenceSensorsOffId: funit_type = F_PresenceSensorsOn value = False else: logging.debug(f'Unsupported funit "{funit_name}"') return if _var.invalid: value = None topic = spread.topic.State( spread.address.SubgineryAddress(_topic.p_id, e_data.location_id, e_data.__class__.__name__, funit_type)) retain = True elif isinstance(_topic, mqtt.TopicCommandTros3): if funit_name == subgineries.onId: funit_type = F_ON value = True elif funit_name == subgineries.offId: funit_type = F_ON value = False # elif funit_name == subgineries.loadScene1Id: # elif funit_name == subgineries.loadScene2Id: elif funit_name == subgineries.presenceSensorsOnId: funit_type = F_PresenceSensorsOn value = True elif funit_name == subgineries.presenceSensorsOffId: funit_type = F_PresenceSensorsOn value = False elif funit_name == subgineries.lightSensorsOnId: funit_type = F_LightSensorsOn value = True elif funit_name == subgineries.lightSensorsOffId: funit_type = F_LightSensorsOn value = False else: logging.debug(f'Unsupported funit "{funit_name}"') return topic = spread.topic.Event( spread.address.SubgineryAddress(_topic.p_id, e_data.location_id, e_data.__class__.__name__, funit_type)) retain = False else: logging.debug(f'Unsupported topic "{_topic}"') return else: logging.debug(f'Unknown entity({e_id})') return var = spread.variable.Variable(value, timestamp=timestamp) self.publish(topic=topic, data=var, retain=retain)
def on_spread(self, _topic: (spread.topic.Set, spread.topic.Renew), data): _var = spread.variable.Variable(json.loads(data.decode())) _address = _topic.entity_address if isinstance(_topic, spread.topic.Set): if isinstance(_topic.entity_address, (spread.address.EngineryAddress, spread.address.SubgineryAddress)): cl = mqtt.classifier['Lighting'][_address.type][ _address.funit_type]['id'] value = True if _address.funit_type == F_ON: if _var.value: funit_id = engineries.onId else: funit_id = engineries.offId elif _address.funit_type == F_BrightnessLevel: funit_id = engineries.powerLevelId value = _var.value elif _address.funit_type == F_GROUP_LEVEL_RAW: if _var.value == 0: funit_id = engineries.groupOffId elif _var.value == 254: funit_id = engineries.groupOnId else: funit_id = engineries.groupSetLevelId e_data = self.engineries[_address.id] if isinstance(e_data, engineries.DimmingLight): value = e_data.raw2prs(_var.value) else: logging.debug( f'Unsupported funit "{_address.funit_type} for {e_data.__class__.__name__}"' ) return elif _address.funit_type == F_PresenceSensorsOn: if _var.value: funit_id = subgineries.presenceSensorsOnId else: funit_id = subgineries.presenceSensorsOffId elif _address.funit_type == F_LightSensorsOn: if _var.value: funit_id = subgineries.lightSensorsOnId else: funit_id = subgineries.lightSensorsOffId else: logging.debug(f'Unsupported funit "{_address.funit_type}"') return var = VariableTRS3(_address.id, cl, value, _var.invalid) topic = mqtt.TopicCommandTros3( PROJECT_ID, mqtt.EngineryAddress(_address.id, funit_id)) else: try: address = spread_address_to_mqtt(self.server_id, _address) funit = get_funit(_address) except ClassifierError as ex: logging.debug(ex) return except BaseException: logging.debug( f'Unsupported address "{_address.__class__.__name__}"') return else: var = VariableJocket.create_data(_address.id, funit['id'], 'set', _var.value, invalid=_var.invalid) topic = mqtt.TopicCommand(PROJECT_ID, 'Frontier_SID', address) elif isinstance(_topic, spread.topic.Renew): try: topic = mqtt.TopicCommand( spread_address_to_mqtt(self.server_id, _address)) funit = get_funit(_address) except ClassifierError as ex: logging.debug(ex) return except BaseException: logging.debug( f'Unsupported address "{_address.__class__.__name__} of {_topic.__class__.__name__}"' ) return else: var = VariableJocket.create_data(_address.id, funit['id'], 'get', _var.value) else: logging.debug(f'Unsupported topic "{_topic.__class__.__name__}"') return self.publish(topic, var, retain=False)