def read(self, mac): global result result = {} try: if mac in globals.KEEPED_CONNECTION: logging.debug('Already a connection for ' + mac + ' use it') conn = globals.KEEPED_CONNECTION[mac] else: logging.debug('Creating a new connection for ' + mac) conn = Connector(mac) globals.KEEPED_CONNECTION[mac] = conn conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return notification = Notification(conn, Smartplug) conn.writeCharacteristic('0x2b', '0f050400000005ffff') notification.subscribe(2) return except Exception as e: try: conn.disconnect() except Exception as e: pass logging.error(str(e)) return
def read(self, mac): result = {} try: conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return logging.debug('read batteryFirm') batteryFirm = bytearray(conn.readCharacteristic('0x18')) battery = batteryFirm[0] firmware = "".join(map(chr, batteryFirm[2:])) notification = Notification(conn, Mithermaff) logging.debug('write 0x10 0100 to get sensor values') conn.writeCharacteristic('0x10', '0100', response=True) notification.subscribe(2) result['battery'] = battery result['firmware'] = firmware result['id'] = mac logging.debug(str(result)) return result except Exception, e: logging.error(str(e))
def read(self, mac): result = {} #account verify : 0x29 #realtimedata : 0x30 #settings : 0x34 #settingsresult : 0x25 #historydata : 0x2c # #credentials : 0x21, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xb8, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00 # #enablerealtime : 0x0B, 0x01, 0x00, 0x00, 0x00, 0x00 # #units celcisu : 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 # defini seuil : dans 0x34 ecrire 010X48f4YYYY avec x numero de sonde (commencant a 0) et y temperature x10 inverse # #0x31 0100 activer notif temp #batterie 0x34 082400000000 try: conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return conn.writeCharacteristic('0x29', '2107060504030201b8220000000000', response=True) notification = Notification(conn, Ibbq6) conn.writeCharacteristic('0x26', '0100', response=True) conn.writeCharacteristic('0x34', '082400000000', response=True) notification.subscribe(10) return result except Exception as e: logging.error(str(e)) return result
def action(self, message): mac = message['device']['id'] handle = message['command']['handle'] value = message['command']['value'] conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return try: if 'key' in message['command']: conn.writeCharacteristic('0x19', message['command']['key'], response=True) if handle == '0x4e': conn.writeCharacteristic(handle, value, response=True) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return conn.writeCharacteristic('0x51', '04') notification = Notification(conn, Miband) notification.subscribe(20) conn.writeCharacteristic('0x4c', '0100', response=True) else: conn.writeCharacteristic(handle, value) if value == '02': time.sleep(10) conn.disconnect() except Exception as e: logging.error(str(e)) conn.disconnect() return
def getStatus(self, mac): logging.debug('STATUS BLPNR') try: conn = Connector(mac) if not conn.isconnected: conn.connect(type='random') if not conn.isconnected: return conn.writeCharacteristic('0x000f', '2902') conn.disconnect() except Exception as e: logging.error(str(e)) try: conn = Connector(mac) if not conn.isconnected: conn.connect(type='random') if not conn.isconnected: return logging.debug('READ notif BLPNR') notification = Notification(conn, Blpnr) #notification.subscribe(10,disconnect=True) notification.subscribe() conn.writeCharacteristic('0x0011', '0d00000d00') except Exception as e: logging.error(str(e))
def read(self, mac): result = {} try: conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return if (mac in globals.KNOWN_DEVICES and globals.KNOWN_DEVICES[mac]['model'] == 'xiaomiht/xiaomiht_cleargrass'): Firm = bytearray(conn.readCharacteristic('0x2a')) else: Firm = bytearray(conn.readCharacteristic('0x24')) batt = bytearray(conn.readCharacteristic('0x18')) battery = batt[0] result['battery'] = battery firmware = "".join(map(chr, Firm)) result['firmware'] = firmware notification = Notification(conn, XiaomiHT) conn.writeCharacteristic('0x10', '0100', response=True) notification.subscribe(2) result['id'] = mac logging.debug('XIAOMIHT------' + str(result)) return result except Exception as e: logging.error(str(e)) return result
def parse(self, data, mac, name, manuf): result = {} result['present'] = 1 try: conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return notification = Notification(conn, LogiSwitch) notification.subscribe(2) except Exception, e: logging.error(str(e))
def sendAction(self,conn,subhandle,data,sendNotif=True): try: packet = make_command_packet (self.session_key, conn.mac, self.mesh_id, subhandle, data) globals.PENDING_ACTION = True if sendNotif == True: notification = Notification(conn, Awoxmesh, {'mac': conn.mac, 'session':self.session_key}) notification.subscribe(timer=NOTIF_TIMEOUT,disconnect=False) logging.debug("Sending packet for "+ conn.mac + " : " + packet + " with session key : "+ "".join("%02x" % b for b in self.session_key)) return conn.writeCharacteristic(COMMAND_CHAR_UUID, packet) except Exception,e: logging.debug("Exception found while sending action") logging.debug(str(e)) return False
def parse(self,data,mac,name,manuf): action={} action['present'] = 1 if mac.upper() in globals.KNOWN_DEVICES and globals.KNOWN_DEVICES[mac.upper()]['emitterallowed'] == globals.daemonname and globals.KNOWN_DEVICES[mac.upper()]['islocked'] == 1: conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return action conn.writeCharacteristic('0x36','0100') notification = Notification(conn,Itag) notification.subscribe() globals.KEEPED_CONNECTION[mac.upper()]=conn return action
def read(self, mac, connection=''): if 'localname' in globals.KNOWN_DEVICES[mac.upper()]: # Make it works directly with devices paired by remote control (for testing) if globals.KNOWN_DEVICES[mac.upper()]['localname'].startswith( "R-"): self.mesh_name = globals.KNOWN_DEVICES[ mac.upper()]['localname'] self.mesh_password = P_DEFAULTPASSWORD if mac in globals.KEEPED_CONNECTION: logging.debug('Already a connection for ' + mac + ' use it') conn = globals.KEEPED_CONNECTION[mac] else: logging.debug('Creating a new connection for ' + mac) conn = Connector(mac) globals.KEEPED_CONNECTION[mac] = conn i = 1 authresult = False while i <= 2: i = i + 1 if not conn.isconnected: try: conn.connect(retry=1) except: pass if conn.isconnected: authresult = self.auth(conn) if authresult: break else: time.sleep(1) if not authresult: logging.debug( 'Ending action due to authentication error on device') globals.PENDING_ACTION = False return result # send message notification = Notification(conn, Awoxmesh, { 'mac': conn.mac, 'session': self.session_key }) notification.subscribe(timer=NOTIF_TIMEOUT, disconnect=False) conn.readCharacteristic(STATUS_CHAR_UUID) return {}
def parse(self, data, mac, name, manuf): action = {} action['present'] = 1 try: conn = Connector(mac) conn.connect() if not conn.isconnected: return notification = Notification(conn, KST1) conn.writeCharacteristic('0x2c', '0100', response=True) batteryData = (conn.readCharacteristic('0x3a')).hex() logging.debug(str(batteryData)) action['battery'] = int(batteryData, 16) notification.subscribe(20) except Exception as e: logging.error(str(e)) return action
def action(self,message): mac = message['device']['id'] handle = message['command']['handle'] value = message['command']['value'] conn = Connector(mac) conn.connect(type='random') if not conn.isconnected: conn.connect(type='random') if not conn.isconnected: return conn.writeCharacteristic('0x55','0100',type='random') notification = Notification(conn,Miband2,{'action':'write','handle':handle,'value':value,'type':'random'}) if handle == '0x2b': notification.subscribe(20) else: notification.subscribe(1) conn.writeCharacteristic('0x54', '0200',type='random') return
def read(self, mac): result = {} try: conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return #check pairing state first pairing = conn.readCharacteristic('0x3a') # check pairing state if pairing: #initiate pairing sequence conn.writeCharacteristic( '0x3a', '00', response=True) # set pairing state to 0 conn.writeCharacteristic('0x33', '0100', response=True) # tbd?? conn.writeCharacteristic('0x32', '00', response=True) # reset history days firmw = str(conn.readCharacteristic('0x18')) # get fw version name = str(conn.readCharacteristic('0x28')) # get name conn.writeCharacteristic('0x42', '0100', response=True) # tbd?? curdate = datetime.datetime.today().strftime('%y%m%d%H%M%S') conn.writeCharacteristic( '0x2b', binascii.hexlify(curdate), response=True) #set current time ascii: aammddhhmmss conn.writeCharacteristic('0x41', '00000000', response=True) conn.writeCharacteristic('0x42', '0000', response=True) # tbd?? notification = Notification(conn, BeeWiSmartDoor) logging.debug('BeeWi read firmw=' + str(firmw) + ' Name: ' + str(name)) notification.subscribe(2) else: battery = ord(conn.readCharacteristic('0x25')) result['battery'] = battery logging.debug('BeeWi read Battery=' + str(battery)) result['id'] = mac return result except Exception, e: logging.error(str(e))
def read(self,mac): result={} try: conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return batt = bytearray(conn.readCharacteristic('0x3a')) battery = batt[0] conn.writeCharacteristic('0x38','0100',response=True) conn.writeCharacteristic('0x46','f40100',response=True) notification = Notification(conn,Lywsd03) notification.subscribe(10) result['battery'] = battery result['id'] = mac logging.debug('LYWSD03------'+str(result)) return result except Exception as e: logging.error(str(e)) return result
def read(self, mac): result = {} try: conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return Firm = bytearray(conn.readCharacteristic('0x24')) batt = bytearray(conn.readCharacteristic('0x18')) battery = batt[0] firmware = "".join(map(chr, Firm)) notification = Notification(conn, XiaomiHT) conn.writeCharacteristic('0x10', '0100', response=True) notification.subscribe(2) result['battery'] = battery result['firmware'] = firmware result['id'] = mac logging.debug('XIAOMIHT------' + str(result)) return result except Exception, e: logging.error(str(e))
def read(self,mac): result={} try: conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return batteryFirm = bytearray(conn.readCharacteristic('0x38')) conn.writeCharacteristic('0x33','a01f',response=True) battery = batteryFirm[0] firmware = "".join(map(chr, batteryFirm[2:])) notification = Notification(conn,Miflora) conn.writeCharacteristic('0x36','0100',response=True) notification.subscribe(2) result['battery'] = battery result['firmware'] = firmware result['id'] = mac logging.debug(str(result)) return result except Exception,e: logging.error(str(e))
def action(self, message): #logging.debug('Doing Awox action') result = {} ret = True mac = message['device']['id'] result['id'] = mac globals.PENDING_ACTION = True localname = '' if 'localname' in message['device']: localname = message['device']['localname'] # Make it works directly with devices paired by remote control (for testing) if localname.startswith("R-"): self.mesh_name = localname self.mesh_password = P_DEFAULTPASSWORD handle = '' value = '0' cmd = '' if 'handle' in message['command']: handle = message['command']['handle'] if 'value' in message['command']: value = message['command']['value'] if '$' in value: # manage injection of cmd/group in value (ex : gp$value or cmd$gp$value) s = value.split('$') if len(s) == 2: # only group message['command']['gp'] = s[0] value = s[1] elif len(s) == 3: # command and group message['command']['cmd'] = s[0] message['command']['gp'] = s[1] value = s[2] else: logging.info('Incorrect parameter (' + message['command']['value'] + '). Format: [[cmd$]gp$]value') if 'cmd' in message['command']: cmd = message['command']['cmd'] if 'gp' in message['command']: self.mesh_id = int(message['command']['gp']) + 32768 if 'target' in message['command']: self.mesh_id = int(message['command']['target']) if cmd != '': logging.debug('Running action ' + cmd) # case of new pairing will work only if unpaired if cmd == 'setNewPairing' or cmd == 'firstInit': self.mesh_name = P_JEEDOM_MESHNAME self.mesh_password = P_JEEDOM_MESHPASSWORD ret = self.setMeshPairing(mac, localname, self.mesh_name, self.mesh_password, self.mesh_longtermkey) time.sleep(3) message['command']['cmd'] = "status" self.action(message) return if mac in globals.KEEPED_CONNECTION: logging.debug('Already a connection for ' + mac + ' use it') conn = globals.KEEPED_CONNECTION[mac] else: logging.debug('Creating a new connection for ' + mac) conn = Connector(mac) globals.KEEPED_CONNECTION[mac] = conn i = 1 authresult = False while i <= 3: i = i + 1 if not conn.isconnected: try: conn.connect(retry=1) except: pass if conn.isconnected: authresult = self.auth(conn) if authresult: break else: time.sleep(0.5) if not authresult: logging.debug( 'Ending action due to authentication error on device') globals.PENDING_ACTION = False return if cmd == 'power': data = struct.pack('B', int(value)) self.sendAction(conn, C_POWER, data) elif cmd == 'resetMesh': self.resetMesh(conn) elif cmd == 'setMeshId': self.setMeshId(conn, int(value)) elif cmd == 'addMeshGroup': self.setGroupId(conn, int(value), delete=False) elif cmd == 'delMeshGroup': self.setGroupId(conn, int(value), delete=True) elif cmd == 'setWhiteHex': # temperature + brightness (hex) data = value[:2].decode("hex") self.sendAction(conn, C_WHITE_TEMPERATURE, data, False) data = value[-2:].decode("hex") self.sendAction(conn, C_WHITE_BRIGHTNESS, data) elif cmd == 'setWhite': # temperature-brightness (value 0 to 100) data = value.split('-') result['whitetemperature'] = data[0] result['whitebrightness'] = data[1] temp = struct.pack('B', int(int(data[0]) * 127 / 100)) brightness = struct.pack('B', int(int(data[1]) * 127 / 100)) self.sendAction(conn, C_WHITE_TEMPERATURE, temp, False) self.sendAction(conn, C_WHITE_BRIGHTNESS, brightness) elif cmd == 'setWhiteTemperature': # temperature from 1 to 100) result['whitetemperature'] = value data = struct.pack('B', int(int(value) * 127 / 100)) self.sendAction(conn, C_WHITE_TEMPERATURE, data) elif cmd == 'setWhiteBrightness': # brightness (hex) #data = value.decode("hex") result['whitebrightness'] = value data = struct.pack('B', int(int(value) * 127 / 100)) self.sendAction(conn, C_WHITE_BRIGHTNESS, data) elif cmd == 'setColor': # red + green + blue (hex) result['color'] = value value = '04' + value.replace("#", "") data = value.decode("hex") self.sendAction(conn, C_COLOR, data) elif cmd == 'setColorLight': # color(#hex)-brightness(0-100) data = value.split('-') result['color'] = data[0] result['colorbrightness'] = data[1] color = '04' + data[0].replace("#", "") brightness = struct.pack('B', int(int(data[1]) * 64 / 100)) self.sendAction(conn, C_COLOR, color.decode("hex"), False) self.sendAction(conn, C_COLOR_BRIGHTNESS, brightness) elif cmd == 'setColorBrightness': # a value between 0xa and 0x64 result['colorbrightness'] = value data = struct.pack('B', int(int(value) * 64 / 100)) self.sendAction(conn, C_COLOR_BRIGHTNESS, data) elif cmd == 'setSequence': # number between 0 and 6 data = struct.pack('B', int(value)) ret = self.sendAction(conn, C_PRESET, data) elif cmd == 'setSequenceColorDuration': # duration: in milliseconds. data = struct.pack("<I", int(value)) ret = self.sendAction(conn, C_SEQUENCE_COLOR_DURATION, data) elif cmd == 'setSequenceFadeDuration': # duration: in milliseconds. data = struct.pack("<I", int(value)) ret = self.sendAction(conn, C_SEQUENCE_FADE_DURATION, data) elif cmd == 'playScenario': # color/white/power&duration|color/white/power&duration|...|X (number of iteration for last value) # ex: #DF0101-100&5|100-100&7|50-50&10|0&5|1&0|3 self.playScenario(conn, value) elif cmd == 'setLightMode': # does nothing # duration: in milliseconds. data = struct.pack("B", int(value)) ret = self.sendAction(conn, C_LIGHT_MODE, data) elif cmd == 'status': notification = Notification(conn, Awoxmesh, { 'mac': conn.mac, 'session': self.session_key }) notification.subscribe(timer=NOTIF_TIMEOUT, disconnect=False) conn.readCharacteristic(STATUS_CHAR_UUID) else: data = value.decode("hex") handle = int(handle, 16) self.sendAction(conn, handle, data) logging.info('Value ' + value + ' sent to controller') globals.PENDING_ACTION = False # prepare results if ret: if cmd == 'power': result['status'] = int(value) elif cmd == 'setWhite' or cmd == 'setWhiteHex' or cmd == 'setWhiteTemperature' or cmd == 'setWhiteBrightness': result['mode'] = 1 result['modestring'] = "Blanc" result['status'] = 1 elif cmd == 'setColor' or cmd == 'setColorLight' or cmd == 'setColorBrightness': result['mode'] = 3 result['modestring'] = "Couleur" result['status'] = 1 elif cmd == 'setSequence': result['mode'] = 7 result['modestring'] = "Sequence" result['status'] = 1 else: # other command for which we are unsure of the result return logging.debug('Action returned status before notification') else: # request somehow failed return return result