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 create(self): logging.debug('Create socket') self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.settimeout(TIMEOUT) while True: try: self.sock.connect((self._host, self._port)) except ConnectionRefusedError as ex: logging.exception(ex) time.sleep(3) else: break
def on_message(self, mosq, obj, msg): self._command_event.clear() self._stopped = True global is_lock while is_lock: time.sleep(0.3) is_lock = True host, port, data, flags = msg.payload.decode().split('#') data = bytes.fromhex(data) flags = flags.split(':') size = 0 for flag in flags: if 'RS' in flag: size = int(flag[2:]) for device in self.sock: if device._port == int(port) and device._host == host: try: pass #device.send_message(data, size) #out=data #print(data) except BaseException as ex: logging.exception(ex) self.mqttc.publish( topic=topic_dump.format(PROJECT, BUS_ID, '0') + '/error', payload=str(ex)) else: try: out = ''.join(hex(b)[2:].rjust(2, '0') for b in out) self.mqttc.publish(topic=topic_dump.format( PROJECT, BUS_ID, '0'), payload=out) logging.debug('[ <-]: {}'.format(out)) except BaseException as ex: logging.exception(ex) finally: is_lock = False #device.start_timer() self._stopped = False self._command_event.set()
def on_reply(self, variable): if variable.id in self.ids: try: if self.cur_e and variable.id == self.cur_e.id: self.stop_timer() response = json.loads(variable.value) static = base64.b64decode(response['data']) static = zlib.decompress(static).decode() static = static.split() time_from = None period = 0 while len(static) > 0: try: item = static.pop(0).split(',') time = datetime.datetime.fromisoformat(item[1].replace( '"', '')) valid = bool(int(item[2])) if valid: value = int(item[3]) if time_from: gap = int(time.timestamp() - time_from.timestamp()) period += gap if value > 0: time_from = time else: time_from = None except BaseException as ex: logging.exception(ex) except BaseException as ex: logging.exception(ex) else: logging.info(f'{variable.id}: {int(period/60/60 * 10)/10}') self.req()
def on_project(self, data): data = json.loads(data) if ENGINERIES in data: try: self.unsubscribe(TopicProject(self.id, f'{ENGINERIES}.json')) for e_data in data[ENGINERIES]: if RECIPE in e_data and TYPE in e_data[ RECIPE] and INGREDIENTS in e_data[RECIPE]: if e_data[RECIPE][TYPE] == 'Simple' and len( e_data[RECIPE][INGREDIENTS]) > 0: if e_data[ TYPE] == engineries.DimmingLight.__name__: try: _eng = generator.generate_enginery( self.id, e_data, None) self._engineries.append(_eng) self.ids.append(_eng.id) except ProjectError as ex: logging.warning(ex) except BaseException as ex: logging.exception(ex) finally: self.on_ready()
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 listen_all(self): global reg_sw global night_reg while True: time.sleep(1) self._command_event.wait() nsw = 0 dore = 0 # Опрос тумблера режима маскировки for thing in NIGHT_THINGS: data = thing['command'] size = len(data) data = bytes.fromhex(data) try: # tk=2415 out = self.sock_night.send_message(data, size) except BaseException as ex: logging.exception(ex) # self.mqttc.publish(topic=topic_dump.format(BUS_ID) + '/error', # payload=str(ex)) else: # value = self.get_from_DI(thing['id'], out) tt = out[18:22] tk = int(tt, 16) if tk == 0: if thing['di'] == 0: night_di0 = False else: night_di1 = False if tk == 1: nsw = nsw +1 if thing['di'] == 0: night_di0 = True night_reg = 1 if night_di0 != night_di0_old: pass else: night_di1 = True night_reg = 2 if nsw == 0: night_reg = 0 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: for thing in THINGS: num=0 for key, value in thing['topicValues'].items(): if (key == 'isOpenedId') and (thing['di'] != 'None'): data = thing['command'] size = len(data) data = bytes.fromhex(data) try: out = device.send_message(data, size) except BaseException as ex: logging.exception(ex) # self.mqttc.publish(topic=topic_dump.format(BUS_ID) + '/error', # payload=str(ex)) else: tt = out[18:22] tk = int(tt, 16) if tk == 1: # Дверь открыта value=True # Запомнить состояние освещения # погасить свет # if night_reg != 0: for devid, dali_data in self.msg_sub: self.saved_data[devid] = dali_data self.msg_sub[devid] = '' if night_reg == 1: self.mqttc.publish(topic=TOPIC_PUB['111427'], payload='{"address": {"id": 111427, "class": 31090132}, "key": "{00000000-0000-0000-0000-000000000000}", "action": "set", "timestamp": "2020-09-12T14:09:32.267404", "data": {"value": 0}}') dore = dore +1 ### ### if tk == 0: value=False #value = 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)) # self.write_to_bro(thing['topicId'], num, value) else: #self.write_to_bro(thing['topicId'], num, value) num = num+1 if dore != 0 and night_reg != 0: # Любая дверь открыта и есть режим светомаскировки if reg_sw == 0: # Запомнить состояние reg_sw = 1 # # # Послать команду на включение синего света if dore == 0 or night_reg == 0: if reg_sw == 1: # Восстановить сохраненные значения света reg_sw = 0 pass
def listen_all(self): while True: time.sleep(1) self._command_event.wait() 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: for thing in THINGS: num=0 for key, value in thing['topicValues'].items(): if (key == 'isOpenedId') and (thing['di'] != 'None'): data = thing['command'] size = len(data) data = bytes.fromhex(data) try: # tk=2415 out = device.send_message(data, size) except BaseException as ex: logging.exception(ex) self.mqttc.publish(topic=topic_dump.format(BUS_ID) + '/error', payload=str(ex)) else: #value = self.get_from_DI(thing['id'], out) tt = out[18:22] tk = int(tt, 16) if tk == 1: # Дверь открыта value=True # Запомнить состояние освещения # погасить свет # # if tk == 0: value=False #value = 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)) self.write_to_bro(thing['topicId'], num, value) else: self.write_to_bro(thing['topicId'], num, value) num = num+1