def action(self, message): mac = message['device']['id'] handle = message['command']['handle'] value = message['command']['value'] 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 conn.writeCharacteristic(handle, value) logging.debug('Value ' + value + ' written in handle ' + handle) logging.debug('Refreshing ... ') result = self.read(mac) return result
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'] repeat = 0 if 'repeat' in message['command']: repeat = int(message['command']['repeat']) 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 if repeat != 0: conn.writeCharacteristic('0x25', '010000000000000000000d0a') time.sleep(2) for x in range(repeat): conn.writeCharacteristic(handle, value) time.sleep(0.4) else: conn.writeCharacteristic(handle, value) conn.disconnect() return
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): 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 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) conn.writeCharacteristic('0x0011','0d00000d00',response=True) notification.subscribe(10) 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 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) result['battery'] = battery result['firmware'] = firmware result['id'] = mac received = bytearray(conn.readCharacteristic('0x35')) temperature = float(received[1] * 256 + received[0]) / 10 if temperature > 3276.8: temperature = 0 - (6553.6 - temperature) sunlight = received[4] * 256 + received[3] moisture = received[7] fertility = received[9] * 256 + received[8] result['sunlight'] = sunlight result['moisture'] = moisture result['fertility'] = fertility result['temperature'] = temperature result['source'] = globals.daemonname logging.debug(str(result)) globals.JEEDOM_COM.add_changes('devices::' + conn.mac, result) return result except Exception as e: logging.error(str(e)) return result
def setMeshPairing(self, mac, localname, new_mesh_name, new_mesh_password, new_mesh_long_term_key): if mac in globals.KEEPED_CONNECTION: conn = globals.KEEPED_CONNECTION[mac] else: conn = Connector(mac) conn.connect() defaultmeshname = localname.encode() is_a_remote = False char = conn.readCharacteristic(MODEL_UUID) if char.lower() == 'ERCUm': #if this is a remote is_a_remote = True logging.info( "[Unpaired device] Remote detected, trying with name " + defaultmeshname) if defaultmeshname == self.mesh_name: logging.info( "Device already paired to Jeedom. Do a reset of the device beforehand" ) else: session_random = urandom(8) message = make_pair_packet(defaultmeshname, P_DEFAULTPASSWORD, session_random) conn.writeCharacteristic(PAIR_CHAR_UUID, message) conn.writeCharacteristic(STATUS_CHAR_UUID, '01') reply = bytearray(conn.readCharacteristic(PAIR_CHAR_UUID)) if reply[0] == 0xd: logging.info( "[Unpaired device] Connected (Auth OK with default password)." ) session_key = make_session_key(defaultmeshname, P_DEFAULTPASSWORD, session_random, reply[1:9]) else: if reply[0] == 0xe: logging.info( "[Unpaired device] Auth error : check name and password or make sure device is unpaired." ) else: logging.info( '[Unpaired device] Error while trying to auth..') return False logging.info('Step 1 - Mesh Name : ' + new_mesh_name.encode()) message = encrypt(session_key, new_mesh_name.encode()) message.insert(0, 0x4) conn.writeCharacteristic(PAIR_CHAR_UUID, "".join("%02x" % b for b in message)) logging.info('Step 2 - Mesh Password : '******'Step 3 - LongtermKey : ' + new_mesh_long_term_key.encode()) message = encrypt(session_key, new_mesh_long_term_key.encode()) message.insert(0, 0x6) conn.writeCharacteristic(PAIR_CHAR_UUID, "".join("%02x" % b for b in message)) time.sleep(2) logging.info('Step 4 - Get Confirmation') reply = bytearray(conn.readCharacteristic(PAIR_CHAR_UUID)) if reply[0] == 0x7: self.mesh_name = new_mesh_name.encode() self.mesh_password = new_mesh_password.encode() logging.info("Mesh network settings accepted.") else: logging.info("Mesh network settings change failed : %s", repr(reply)) time.sleep(2) if not is_a_remote and self.auth(conn): logging.info('Step 5 - Set a default group') self.setGroupId(conn, P_DEFAULTGROUP, delete=False) time.sleep(1) value = '0407ed3c' # green data = value.decode("hex") self.sendAction(conn, C_COLOR, data) conn.disconnect() return True
def action(self, message): mac = message['device']['id'] if mac not in globals.LAST_STORAGE: globals.LAST_STORAGE[mac] = {} handle = message['command']['handle'] value = message['command']['value'] type = message['command']['type'] 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 try: if type == 'color': logging.debug('Sending Color') data = message['command']['data'] conn.writeCharacteristic( '0x2a', '0601' + utils.twoDigitHex(int(data[0])) + utils.twoDigitHex(int(data[1])) + utils.twoDigitHex(int(data[2])) + '00') colorArray = {} for i in range(64): colorArray[int(i) + 1] = self.rgb_to_hex( (int(data[0]), int(data[1]), int(data[2]))) globals.LAST_STORAGE[mac] = colorArray logging.debug('Color sent') elif type == 'display': logging.debug('Sending Display') data = message['command']['data'] save_pixel = 0 total_pixel = 0 temp = {} maxint = 0 maxhex = '#FFFFFF' if len(data) == 64: for pixel, value in data.iteritems(): shex = self.rgb_to_hex( (int(value[0]), int(value[1]), int(value[2]))) if shex not in temp: temp[shex] = 1 else: temp[shex] = temp[shex] + 1 if temp[shex] > maxint: maxint = temp[shex] maxhex = shex for pixel, value in data.iteritems(): total_pixel = total_pixel + 1 if mac in globals.LAST_STORAGE: if int(pixel) in globals.LAST_STORAGE[ mac] and globals.LAST_STORAGE[mac][int( pixel)].lower() == self.rgb_to_hex( (value[0], value[1], value[2])).lower(): save_pixel = save_pixel + 1 if (maxint + 1) > save_pixel and maxint > 2: logging.debug( 'I use color all screen method to improve display speed in :' + str(maxhex)) conn.writeCharacteristic( handle, '0601' + str(maxhex).replace('#', '') + '00') colorArray = {} for i in range(64): colorArray[int(i) + 1] = str(maxhex) globals.LAST_STORAGE[mac] = colorArray save_pixel = 0 total_pixel = 0 colorArray = {} for pixel, value in data.iteritems(): total_pixel = total_pixel + 1 if mac in globals.LAST_STORAGE: if int(pixel) in globals.LAST_STORAGE[ mac] and globals.LAST_STORAGE[mac][int( pixel)].lower() == self.rgb_to_hex( (value[0], value[1], value[2])).lower(): save_pixel = save_pixel + 1 colorArray[int(pixel)] = self.rgb_to_hex( (value[0], value[1], value[2])).lower() continue conn.writeCharacteristic( '0x2a', '0702' + utils.twoDigitHex(int(pixel)) + utils.twoDigitHex(value[0]) + utils.twoDigitHex(value[1]) + utils.twoDigitHex(value[2])) colorArray[int(pixel)] = self.rgb_to_hex( (value[0], value[1], value[2])) time.sleep(0.05) globals.LAST_STORAGE[mac] = colorArray logging.debug('I save ' + str(save_pixel) + '/' + str(total_pixel) + ' pixel to write so ' + str((save_pixel * 100) / total_pixel) + '%') logging.debug('Display sent') except Exception, e: logging.debug("Failed to finish : %s" % str(e))
def action(self, message): type = '' mac = message['device']['id'] handle = message['command']['handle'] value = message['command']['value'] if 'type' in message['command']: type = message['command']['type'] 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 if type == 'pair': conn.writeCharacteristic('0x12', '4367' + self.key) time.sleep(5) if type == 'switch': conn.writeCharacteristic('0x12', '4367' + self.key) conn.writeCharacteristic(handle, value.ljust(36, '0')) if type == 'color': conn.writeCharacteristic('0x12', '4367' + self.key) if value == '000000': conn.writeCharacteristic(handle, ('434002').ljust(36, '0')) else: conn.writeCharacteristic(handle, ('4341' + value).ljust(36, '0')) if type == 'brightness': init = utils.tuple_to_hex( struct.unpack('18B', conn.readCharacteristic('0x12'))) conn.writeCharacteristic('0x12', '4367' + self.key) if str(init)[0:4] == '4343': logging.debug(str(init)[0:8]) conn.writeCharacteristic( handle, str(init)[0:8] + hex(int(value))[2:].zfill(2) + str(init)[10:36]) else: conn.writeCharacteristic( handle, str(init)[0:12] + hex(int(value))[2:].zfill(2) + str(init)[14:36]) if type == 'white': conn.writeCharacteristic('0x12', '4367' + self.key) logging.debug('4343' + (hex(int(value))[2:].zfill(4) + '00').ljust(36, '0')) conn.writeCharacteristic(handle, ('4343' + hex(int(value))[2:].zfill(4) + '00').ljust(36, '0')) conn.disconnect() return
def action(self, message): type = '' mac = message['device']['id'] value = message['command']['value'] chars = [] if 'type' in message['command']: type = message['command']['type'] 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) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return if (mac.upper() in globals.KNOWN_DEVICES): if ('specificconfiguration' in globals.KNOWN_DEVICES[mac.upper()] and len(globals.KNOWN_DEVICES[mac.upper()] ['specificconfiguration']) > 0): logging.debug('Already known handles ' + str(globals.KNOWN_DEVICES[mac.upper()] ['specificconfiguration'])) chars = [ globals.KNOWN_DEVICES[mac.upper()]['specificconfiguration'] ['colorhandle'], globals.KNOWN_DEVICES[ mac.upper()]['specificconfiguration']['effecthandle'] ] if chars == []: logging.debug('Not known handles searching') chars = self.findCharacteristics(mac, conn) globals.JEEDOM_COM.add_changes( 'devices::' + mac, { "id": mac, "specificconfiguration": { "colorhandle": chars[0], "effecthandle": chars[1] } }) char = chars[0] if type == 'speed': char = chars[1] init = utils.tuple_to_hex( struct.unpack('8B', conn.readCharacteristic(chars[1]))) speed = 255 - int(value) if speed == 0: speed = 1 value = str(init)[0:12] + hex(speed)[2:].zfill(2) + str( init)[14:16] elif type == 'effect': char = chars[1] init = utils.tuple_to_hex( struct.unpack('8B', conn.readCharacteristic(chars[1]))) initcolor = utils.tuple_to_hex( struct.unpack('4B', conn.readCharacteristic(chars[0]))) value = str(initcolor) + value + '00' + str(init)[12:16] elif type == 'color': char = chars[0] initeffect = utils.tuple_to_hex( struct.unpack('8B', conn.readCharacteristic(chars[1]))) if str(initeffect)[8:10] == '04': valueprep = str(initeffect)[0:8] + 'ff' + '00' + str( initeffect)[12:16] result = conn.writeCharacteristic(chars[1], valueprep) if not result: conn.disconnect() logging.debug( "Failed to write to device probably bad bluetooth connection" ) elif type == 'luminosity': value = utils.getTintedColor(message['command']['secondary'], value) arrayValue = [ int('0x' + value[i:i + 2], 16) for i in range(0, len(value), 2) ] result = conn.writeCharacteristic(char, value) if not result: result = conn.writeCharacteristic(char, value) if not result: logging.debug( "Failed to write to device probably bad bluetooth connection" ) data = {} data = self.read(mac, conn) if len(data) > 2: data['source'] = globals.daemonname if type == 'luminosity': data['luminosity'] = luminosityvalue globals.JEEDOM_COM.add_changes('devices::' + mac, data) conn.disconnect() return
def action(self, message): type = '' mac = message['device']['id'] handle = message['command']['handle'] value = message['command']['value'] if 'type' in message['command']: type = message['command']['type'] 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 if type == 'speed': init = utils.tuple_to_hex( struct.unpack('8B', conn.readCharacteristic(handle))) speed = 255 - int(value) if speed == 0: speed = 1 value = str(init)[0:12] + hex(speed)[2:].zfill(2) + str( init)[14:16] elif type == 'effect': init = utils.tuple_to_hex( struct.unpack('8B', conn.readCharacteristic(handle))) initcolor = utils.tuple_to_hex( struct.unpack( '4B', conn.readCharacteristic(message['command']['color']))) value = str(initcolor) + value + '00' + str(init)[12:16] elif type == 'color': initeffect = utils.tuple_to_hex( struct.unpack( '8B', conn.readCharacteristic(message['command']['effect']))) if str(initeffect)[8:10] == '04': valueprep = str(initeffect)[0:8] + 'ff' + '00' + str( initeffect)[12:16] result = conn.writeCharacteristic(message['command']['effect'], valueprep) if not result: conn.disconnect() logging.debug( "Failed to write to device probably bad bluetooth connection" ) elif type == 'luminosity': value = utils.getTintedColor(message['command']['secondary'], value) arrayValue = [ int('0x' + value[i:i + 2], 16) for i in range(0, len(value), 2) ] result = conn.writeCharacteristic(handle, value) if not result: result = conn.writeCharacteristic(handle, value) if not result: logging.debug( "Failed to write to device probably bad bluetooth connection" ) data = {} data = self.read(mac, conn) if len(data) > 2: data['source'] = globals.daemonname if type == 'luminosity': data['luminosity'] = luminosityvalue globals.JEEDOM_COM.add_changes('devices::' + mac, data) conn.disconnect() 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 as e: logging.error(str(e)) return result