def set_data(self, auto, cooler, heater): data = (auto << 2) | (cooler << 1) | (heater << 0) try: ser = serial.Serial(port=self._port, baudrate=BAUDRATE, timeout=0.1) request = Wake.wake_transmit(cmd=Commands.CLIMATE_SET, data=struct.pack('b', data), adr=self._address) ser.write(request) ret = Wake.wake_decode(ser.read(64)) print('Request: ', _humane_bytes(request)) print('Response: ', _humane_bytes(ret)) ser.close() if Wake.wake_check_crc(ret) is False: return False if ret[2] != Commands.CLIMATE_SET.value: return False return True except serial.SerialException as err: sys.stderr.write(str(err) + '\n') return False return True
def get_info(port, address): try: request = Wake.wake_transmit(Commands.INFO, b'', address) ser = serial.Serial(port=port, baudrate=BAUDRATE, timeout=0.1) ser.write(request) ret = Wake.wake_decode(ser.read(64)) print('Request: ', _humane_bytes(request)) print('Response: ', _humane_bytes(ret)) ser.close() if not Wake.wake_check_crc(ret): return None, None, None, None flash_size = struct.unpack('<H', ret[4:6])[0] unique_id = ret[6:18] version = struct.unpack('<I', ret[18:22])[0] name = ret[22:-1].decode('utf-8') return name, flash_size, version, unique_id except serial.SerialException as err: sys.stderr.write(str(err) + '\n') return None, None, None, None except struct.error as err: sys.stderr.write('Can not parse returned data, {}\n'.format(str(err))) return None, None, None, None
def get_data(self): err_ret = False, 0.0, 0.0, 0.0, 0.0, False, False, False try: ser = serial.Serial(port=self._port, baudrate=BAUDRATE, timeout=0.1) request = Wake.wake_transmit(cmd=Commands.CLIMATE_GET, data=b'', adr=self._address) ser.write(request) ret = Wake.wake_decode(ser.read(64)) print('Request: ', _humane_bytes(request)) print('Response: ', _humane_bytes(ret)) ser.close() if Wake.wake_check_crc(ret) is False: return err_ret if ret[2] != Commands.CLIMATE_GET.value: return err_ret t_local = struct.unpack('<f', ret[4:8])[0] t_remote = struct.unpack('<f', ret[8:12])[0] t_local_alt = struct.unpack('<f', ret[12:16])[0] humidity = struct.unpack('<f', ret[16:20])[0] heater = (ret[20] & 0b001) == 0b01 cooler = (ret[20] & 0b010) == 0b10 automatic = (ret[20] & 0b100) == 0b100 return True, t_local, t_remote, t_local_alt, humidity, heater, cooler, automatic except serial.SerialException as err: sys.stderr.write(str(err) + '\n') return err_ret
def read_odr(self): try: ser = serial.Serial(port=self._port, baudrate=BAUDRATE, timeout=0.1) request = Wake.wake_transmit(cmd=Commands.RELAYS_ODDR, data=b'', adr=self._address) ser.write(request) ret = Wake.wake_decode(ser.read(64)) print('Request: ', _humane_bytes(request)) print('Response: ', _humane_bytes(ret)) ser.close() if Wake.wake_check_crc(ret) is False: return False, 0 if ret[2] != Commands.RELAYS_ODDR.value: return False, 0 return True, (ret[4] << 8) | ret[5] except serial.SerialException as err: sys.stderr.write(str(err) + '\n') return False, 0 except IndexError as err: sys.stderr.write('Response not valid: {}\n'.format(str(err))) return False, 0
def write_ones(self, data): try: ser = serial.Serial(port=self._port, baudrate=BAUDRATE, timeout=0.1) request = Wake.wake_transmit(cmd=Commands.RELAYS_SET, data=struct.pack('>H', data), adr=self._address) ser.write(request) ret = Wake.wake_decode(ser.read(64)) print('Request: ', _humane_bytes(request)) print('Response: ', _humane_bytes(ret)) ser.close() if Wake.wake_check_crc(ret) is False: return False if ret[2] != Commands.RELAYS_SET.value: return False return True except serial.SerialException as err: sys.stderr.write(str(err) + '\n') return False
def get_wiegand(port, address): err_ret = False, 0, 0, b'' try: ser = serial.Serial(port=port, baudrate=BAUDRATE, timeout=0.1) request = Wake.wake_transmit(Commands.WIEGAND, b'', address) ser.write(request) ret = Wake.wake_decode(ser.read(64)) print('Request: ', _humane_bytes(request)) print('Response: ', _humane_bytes(ret)) ser.close() if not Wake.wake_check_crc(ret): return err_ret channel = ret[4] wig_len = ret[5] return True, channel, wig_len, ret[6:-1] except serial.SerialException as err: sys.stderr.write(str(err) + '\n') return err_ret
def write_toggle(self, data_a, data_b): try: ser = serial.Serial(port=self._port, baudrate=BAUDRATE, timeout=0.1) request = Wake.wake_transmit(cmd=Commands.PORTS_TOGGLE, data=bytes([data_a, data_b]), adr=self._address) ser.write(request) ret = Wake.wake_decode(ser.read(64)) print('Request: ', _humane_bytes(request)) print('Response: ', _humane_bytes(ret)) ser.close() if Wake.wake_check_crc(ret) is False: return False if ret[2] != Commands.PORTS_TOGGLE.value: return False return True except serial.SerialException as err: sys.stderr.write(str(err) + '\n') return False
def change_to_boot(port, address): key = bytes.fromhex('64 CA 56 BA 15 57 63 39 DA 57 40 21') _, _, version, unique_id = get_info(port, address) if unique_id is None: return False if len(key) != len(unique_id): return False cipher = b'' for uid, k in zip(unique_id, key): cipher += bytes([uid ^ k]) print('UID: {}'.format(_humane_bytes(unique_id))) print('Key: {}'.format(_humane_bytes(key))) print('Out: {}'.format(_humane_bytes(cipher))) try: ser = serial.Serial(port=port, baudrate=BAUDRATE, timeout=0.1) ser.write(Wake.wake_transmit(cmd=Commands.BOOT, data=cipher, adr=0x02)) ret = Wake.wake_decode(ser.read(64)) ser.close() if Wake.wake_check_crc(ret) is False: print('Check CRC Error\n') return False print('Ret: {}'.format(_humane_bytes(ret))) return ret[ 2] == Commands.BOOT.value # Вернулась команда BOOT (0x04), Ok except serial.SerialException as err: sys.stderr.write(str(err) + '\n') return False return True