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 action(self, message): logging.debug('ACTION BLPNR') mac = message['device']['id'] logging.debug('MAC BLPNR ' + mac) action = message['command']['action'] if mac not in globals.LAST_STORAGE: globals.LAST_STORAGE[mac] = {} logging.debug('action BLPNR ' + action) handle = '0X0011' conn = Connector(mac) conn.connect(type='random') if not conn.isconnected: conn.connect(type='random') if not conn.isconnected: return logging.debug('connected BLPNR') if action == 'refresh': logging.debug('refresh BLPNR') self.getStatus(mac) return if action == 'off': message = '050400900000009900' if action == 'on': value = message['command']['value'] logging.debug('value BLPNR ' + str(value)) liste = [5, 4, 0, 10, 0, int(value), 0] checksum1 = sum(liste) % 256 checksum2 = sum(liste) / 256 liste.append(checksum1) liste.append(checksum2) message = utils.tuple_to_hex(liste) conn.writeCharacteristic(handle, message) conn.disconnect() self.getStatus(mac)
def parse(self, data, mac, name): action = {} action['present'] = 1 action['version'] = 'awox' if mac.upper() not in globals.KNOWN_DEVICES and globals.LEARN_MODE: if name.lower() == P_DEFAULTNAME and self.auto_pairing: self.setMeshPairing(mac, name, self.mesh_name, self.mesh_password, self.mesh_longtermkey) if mac in globals.KEEPED_CONNECTION: conn = globals.KEEPED_CONNECTION[mac] else: conn = Connector(mac) conn.connect() char = conn.readCharacteristic(MODEL_UUID) conn.disconnect() logging.debug('Version is ' + char) if char.lower().startswith('EF'): action['version'] = 'awox' if char.lower().startswith('ERCUm'): action['version'] = 'awoxremote' if char.lower().startswith('ESMLm'): # ESMLm_c9 and ESMLm_c13 action['version'] = 'awox' return action
def action(self, message): if 'type' in message['command']: type = message['command']['type'] mac = message['device']['id'] data = {} data['source'] = globals.daemonname data['id'] = mac data['sendstatus'] = '-' globals.JEEDOM_COM.add_changes('devices::' + mac, data) handle = message['command']['handle'] value = message['command']['value'] conn = Connector(mac) conn.connect() if not conn.isconnected: data['sendstatus'] = 'KO' globals.JEEDOM_COM.add_changes('devices::' + mac, data) return conn.writeCharacteristic('0x29', '2107060504030201b8220000000000', response=True) if type == 'setTarget': logging.debug('IBBQ set Target for probe ' + message['command']['probe'] + ' value ' + value) valuetohex = hex(int(value) * 10)[2:].zfill(4) towrite = '010' + message['command'][ 'probe'] + '48f4' + valuetohex[2:4] + valuetohex[0:2] logging.debug('IBBQ writing ' + towrite + ' on handle ' + handle) conn.writeCharacteristic(handle, towrite, response=True) data['sendstatus'] = 'OK' globals.JEEDOM_COM.add_changes('devices::' + mac, data) conn.disconnect() return
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 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 action(self, message): mac = message['device']['id'] handle = message['command']['handle'] value = message['command']['value'] type = message['command']['type'] conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return try: if type == 'color': if value == '000000': conn.writeCharacteristic(handle, '234277305C72') else: value = self.intToStr(int(value[0:2], 16)) + self.intToStr( int(value[2:4], 16)) + self.intToStr( int(value[4:6], 16)) conn.writeCharacteristic(handle, '234577' + value + '5C72') elif type == 'ambient': conn.writeCharacteristic(handle, '234D77' + value + '5C72') elif type == 'brightness': conn.writeCharacteristic( handle, '234377' + self.intToStr(int(value)) + '5C72') else: conn.writeCharacteristic(handle, value) conn.disconnect() except Exception, e: logging.error('Oups ' + str(e)) conn.disconnect()
def findCharacteristics(self, mac, conn=''): logging.debug("Searching characteristics") if conn == '': conn = Connector(mac) conn.connect(type='random') if not conn.isconnected: return value = '' characteristics = [] try: characteristics = conn.conn.getCharacteristics(0x0001) except Exception as e: logging.debug(str(e)) try: characteristics = conn.conn.getCharacteristics(0x0001) except Exception as e: logging.debug(str(e)) try: characteristics = conn.conn.getCharacteristics(0x0001) except Exception as e: logging.debug(str(e)) conn.disconnect() battery_characteristic = next( iter( filter(lambda el: el.uuid == BATTERY_CHARACTERISTIC_UUID, characteristics))) logging.debug('Found ' + hex(battery_characteristic.getHandle())) return [hex(battery_characteristic.getHandle())]
def read(self,mac): result={} try: conn = Connector(mac) conn.connect(type='random') if not conn.isconnected: conn.connect(type='random') if not conn.isconnected: return battery = struct.unpack('2B',conn.readCharacteristic('0x2e')) temperature = struct.unpack('2B',conn.readCharacteristic('0x34')) humidity = struct.unpack('2B',conn.readCharacteristic('0x3c')) pression = struct.unpack('4B',conn.readCharacteristic('0x44')) temperature = int(str(hex(temperature[0])[2:].zfill(2) + hex(temperature[1])[2:].zfill(2)),16) humidity = int(str(hex(humidity[0])[2:].zfill(2) + hex(humidity[1])[2:].zfill(2)),16) logging.debug(str(pression)) pression = int(str(hex(pression[0])[2:].zfill(2) + hex(pression[1])[2:].zfill(2)+ hex(pression[2])[2:].zfill(2)+ hex(pression[3])[2:].zfill(2)),16) battery = int(str(hex(battery[0])[2:].zfill(2) + hex(battery[1])[2:].zfill(2)),16) result['temperature'] = float(temperature)/10 result['humidity'] = float(humidity)/10 result['pressure'] = float(pression)/100 result['batteryvoltage'] = round(float(battery)*0.00051029,2) if round(float(battery)*0.00051029,2) > 3: battery = 100 elif round(float(battery)*0.00051029,2) < 2: battery = 0 else: battery = round((float(battery)*0.00051029 - 2) *100,0) result['battery'] = battery except Exception,e: logging.error('HECTOR------'+str(e)) conn.disconnect()
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 action(self, message): logging.debug('toto') 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): mac = message['device']['id'] handle = message['command']['handle'] value = message['command']['value'] conn = Connector(mac) conn.connect() if not conn.isconnected: return conn.writeCharacteristic(handle,value) 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 batteryDatas = bytearray(conn.readCharacteristic('0x2c')) if len(batteryDatas) == 10: battery = batteryDatas[0] status = batteryDatas[9] cycle = batteryDatas[7] + batteryDatas[8] year = str(batteryDatas[1] + 2000) month = str(batteryDatas[2] + 1) day = str(batteryDatas[3]) hour = str(batteryDatas[4]) minutes = str(batteryDatas[5]) seconds = str(batteryDatas[6]) if status == 1: status = 'Faible' elif status == 2: status = 'En charge' elif status == 3: status = 'Branché Full' elif status == 4: status = 'Débranché' else: status = 'Inconnu' result['battery'] = battery result['status'] = status result['cycle'] = cycle result[ 'lastcharge'] = day + '/' + month + '/' + year + ' ' + hour + 'h' + minutes + 'min' + seconds + 's' firmwareDatas = bytearray(conn.readCharacteristic('0x12')) firmware = str(firmwareDatas[15]) + '.' + str( firmwareDatas[14]) + '.' + str(firmwareDatas[13]) + '.' + str( firmwareDatas[12]) stepsDatas = conn.readCharacteristic('0x1d') steps = ord(stepsDatas[0]) + (ord(stepsDatas[1]) << 8) result['steps'] = steps result['firmware'] = firmware result['id'] = mac logging.debug(str(result)) conn.disconnect() return result except Exception as e: logging.error(str(e)) conn.disconnect() return result
def read(self, mac, connection=''): result = {} chars = [] try: if mac in globals.KEEPED_CONNECTION: logging.debug('Already a connection for ' + mac + ' use it') conn = globals.KEEPED_CONNECTION[mac] else: if connection != '': conn = connection else: logging.debug('Creating a new connection for ' + mac) conn = Connector(mac) conn.connect(type='random') if not conn.isconnected: return if (mac in globals.KNOWN_DEVICES): if ('specificconfiguration' in globals.KNOWN_DEVICES[mac] and len(globals.KNOWN_DEVICES[mac] ['specificconfiguration']) > 0): logging.debug('Already known handles ' + str( globals.KNOWN_DEVICES[mac]['specificconfiguration'])) chars = [ globals.KNOWN_DEVICES[mac]['specificconfiguration'] ['batteryhandle'] ] if chars == []: logging.debug('Not known handles searching') chars = self.findCharacteristics(mac, conn) globals.JEEDOM_COM.add_changes( 'devices::' + mac, { "id": mac, "specificconfiguration": { "batteryhandle": chars[0] } }) char = chars[0] batteryDatas = bytearray( conn.readCharacteristic(char, type='random')) logging.debug(str(batteryDatas)) result['battery'] = batteryDatas[0] result['id'] = mac logging.debug(str(result)) return result except Exception as e: logging.error(str(e)) conn.disconnect() conn.disconnect() return result
def read(self, mac): result = {} try: conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return batterytrame = conn.readCharacteristic('0x1b') logging.debug('GIGASET------Parsing data ' + batterytrame) battery = int(batterytrame.encode("hex"), 16) result['battery'] = battery result['present'] = 1 except Exception, e: logging.error(str(e)) conn.disconnect()
def read(self, mac): result = {} try: conn = Connector(mac) conn.connect(type='random') if not conn.isconnected: conn.connect(type='random') if not conn.isconnected: return batteryDatas = bytearray(conn.readCharacteristic('0x17'), type='random') result['battery'] = batteryDatas[0] result['id'] = mac logging.debug(str(result)) return result except Exception, e: logging.error(str(e)) conn.disconnect()
def action_handler(message): manuf = '' if manuf in message['command']: manuf = message['command']['manuf'] name = message['command']['name'] if 'localname' in globals.KNOWN_DEVICES[message['device']['id']]: message['device']['localname'] = globals.KNOWN_DEVICES[ message['device']['id']]['localname'] result = {} if message['cmd'] == 'helper' or message['cmd'] == 'helperrandom': type = 'public' if message['cmd'] == 'helperrandom': type = 'random' try: globals.PENDING_ACTION = True mac = message['device']['id'] if mac in globals.KEEPED_CONNECTION: logging.debug('ACTION------Already a connection for ' + mac + ' use it') conn = globals.KEEPED_CONNECTION[mac] else: logging.debug('ACTION------Creating a new connection for ' + mac) conn = Connector(mac) globals.KEEPED_CONNECTION[mac] = conn conn.connect(type=type) if not conn.isconnected: conn.connect(type=type) if not conn.isconnected: globals.PENDING_ACTION = False return try: conn.helper() except Exception, e: logging.debug("ACTION------Helper failed : %s" % str(e)) globals.PENDING_ACTION = False conn.disconnect() globals.PENDING_ACTION = False return except Exception, e: logging.debug("ACTION------Helper failed : %s" % str(e)) globals.PENDING_ACTION = False
def read(self,mac): result={} try: conn = Connector(mac) conn.connect(type='random') if not conn.isconnected: conn.connect(type='random') if not conn.isconnected: return battery = struct.unpack('2B',conn.readCharacteristic('0x2e')) battery = int(str(hex(battery[0])[2:].zfill(2) + hex(battery[1])[2:].zfill(2)),16) temperature = struct.unpack('2B',conn.readCharacteristic('0x34')) temperature = int(str(hex(temperature[0])[2:].zfill(2) + hex(temperature[1])[2:].zfill(2)),16) humidity = struct.unpack('2B',conn.readCharacteristic('0x3c')) humidity = int(str(hex(humidity[0])[2:].zfill(2) + hex(humidity[1])[2:].zfill(2)),16) # If pression == Ox00014601 (83457) -> read again at least for 3 attempts # Hector's sensor seems to always return Ox00014601 at first read attempt pression = struct.unpack('4B',conn.readCharacteristic('0x44')) pression = int(str(hex(pression[0])[2:].zfill(2) + hex(pression[1])[2:].zfill(2)+ hex(pression[2])[2:].zfill(2)+ hex(pression[3])[2:].zfill(2)),16) pression_read = 0 while (pression <= 14601 and pression_read < 3): time.sleep(1) pression = struct.unpack('4B',conn.readCharacteristic('0x44')) pression = int(str(hex(pression[0])[2:].zfill(2) + hex(pression[1])[2:].zfill(2)+ hex(pression[2])[2:].zfill(2)+ hex(pression[3])[2:].zfill(2)),16) pression_read += 1 logging.debug(str(pression)) result['temperature'] = float(temperature)/10 result['humidity'] = float(humidity)/10 result['pressure'] = float(pression)/100 result['batteryvoltage'] = round(float(battery)*0.00051029,2) if round(float(battery)*0.00051029,2) > 3: battery = 100 elif round(float(battery)*0.00051029,2) < 2: battery = 0 else: battery = round((float(battery)*0.00051029 - 2) *100,0) result['battery'] = battery except Exception,e: logging.error('HECTOR------'+str(e)) conn.disconnect()
def findCharacteristics(self, mac, conn=''): logging.debug("Searching characteristics") if conn == '': conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return value = '' characteristics = [] try: characteristics = conn.conn.getCharacteristics(0x0001) except Exception as e: logging.debug(str(e)) try: characteristics = conn.conn.getCharacteristics(0x0001) except Exception as e: logging.debug(str(e)) try: characteristics = conn.conn.getCharacteristics(0x0001) except Exception as e: logging.debug(str(e)) conn.disconnect() color_characteristic = next( iter( filter(lambda el: el.uuid == COLOR_CHARACTERISTIC_UUID, characteristics))) effect_characteristic = next( iter( filter(lambda el: el.uuid == EFFECT_CHARACTERISTIC_UUID, characteristics))) logging.debug('Found ' + hex(color_characteristic.getHandle()) + ' ' + hex(effect_characteristic.getHandle())) return [ hex(color_characteristic.getHandle()), hex(effect_characteristic.getHandle()) ]
def read(self, mac, connection=''): result = {} try: if mac in globals.KEEPED_CONNECTION: self.log('Already a connection for ' + mac + ' use it') conn = globals.KEEPED_CONNECTION[mac] else: if connection != '': conn = connection else: self.log('Creating a new connection for ' + mac) conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() try: if not conn.isconnected: result['state'] = 0 result['white'] = False result['brightness'] = 0 result['tone'] = 0 result['color'] = '#000000' else: result = self.readSettings(conn) except Exception as e: self.log(str(e)) except Exception as e: self.log(str(e)) if conn.isconnected: conn.disconnect() return if conn.isconnected: conn.disconnect() result['id'] = mac return result
def action(self, message): type = '' mac = message['device']['id'] value = message['command']['value'] if 'type' in message['command']: type = message['command']['type'] if mac in globals.KEEPED_CONNECTION: self.log('Already a connection for ' + mac + ' use it') conn = globals.KEEPED_CONNECTION[mac] else: self.log('Creating a new connection for ' + mac) conn = Connector(mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return settings = self.readSettings(conn) # Turn On if type == 'switchon': self.switchBulbOn(conn) # Turn Off elif type == 'switchoff': self.switchBulbOff(conn) # Toggle On/Off elif type == 'toggle': if settings['state'] == 0: self.switchBulbOn(conn) else: self.switchBulbOff(conn) # Brightness elif type == 'brightness': if value == '0': self.switchBulbOff(conn) else: if settings['state'] == 0: self.switchBulbOn(conn) self.setBrightness(conn, value) elif settings['state'] == 1: # White tone if type == 'tone': self.setBulbWhiteTone(conn, value) # white mode elif type == 'white': self.setBulbWhite(conn) # color mode elif type == 'color': if value == '000000': self.setBulbWhite(conn) else: self.setBulbColor(conn, value) # sequence elif type == 'sequence': self.launchSequence(conn, value) time.sleep(0.1) result = self.readSettings(conn) result['id'] = mac conn.disconnect() return result
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('DOTTI------Already a connection for ' + mac + ' use it') conn = globals.KEEPED_CONNECTION[mac] else: logging.debug('DOTTI------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 == 'mode': logging.debug('DOTTI------Changing Mode') data = message['command']['value'] conn.writeCharacteristic('0x2a', data) globals.LAST_STORAGE[mac] = {} elif type == 'color': logging.debug('DOTTI------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('DOTTI------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( (int(value[0]), int(value[1]), int(value[2]))).lower(): save_pixel = save_pixel + 1 if (maxint + 1) > save_pixel and maxint > 2: logging.debug( 'DOTTI------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 isinstance(value, (list, )): logging.debug('DOTTI------this is a list') if int(pixel) in globals.LAST_STORAGE[ mac] and globals.LAST_STORAGE[mac][int( pixel)].lower() == self.rgb_to_hex( (int(value[0]), int(value[1]), int(value[2]))).lower(): save_pixel = save_pixel + 1 colorArray[int(pixel)] = self.rgb_to_hex( (int(value[0]), int(value[1]), int(value[2]))).lower() continue else: logging.debug('DOTTI------this is a string') if int(pixel) in globals.LAST_STORAGE[ mac] and globals.LAST_STORAGE[mac][int( pixel)].lower() == value.lower(): save_pixel = save_pixel + 1 colorArray[int(pixel)] = value.lower() continue if isinstance(value, (list, )): logging.debug('DOTTI------writing from rgb ' + self.rgb_to_hex((int(value[0]), int(value[1]), int(value[2])))) conn.writeCharacteristic( '0x2a', '0702' + utils.twoDigitHex(int(pixel)) + utils.twoDigitHex(int(value[0])) + utils.twoDigitHex(int(value[1])) + utils.twoDigitHex(int(value[2]))) colorArray[int(pixel)] = self.rgb_to_hex( (int(value[0]), int(value[1]), int(value[2]))) else: logging.debug('DOTTI------writing from hex ' + str(value)[1:]) conn.writeCharacteristic( '0x2a', '0702' + utils.twoDigitHex(int(pixel)) + str(value)[1:]) colorArray[int(pixel)] = value.lower() time.sleep(0.05) globals.LAST_STORAGE[mac] = colorArray logging.debug('DOTTI------I save ' + str(save_pixel) + '/' + str(total_pixel) + ' pixel to write so ' + str((save_pixel * 100) / total_pixel) + '%') logging.debug('DOTTI------Display sent') except Exception as e: logging.debug("DOTTI------Failed to finish : %s" % str(e)) conn.disconnect() return
def read(self, mac, connection=''): result = {} chars = [] try: if mac in globals.KEEPED_CONNECTION: logging.debug('Already a connection for ' + mac + ' use it') conn = globals.KEEPED_CONNECTION[mac] else: if connection != '': conn = connection 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 in globals.KNOWN_DEVICES): if ('specificconfiguration' in globals.KNOWN_DEVICES[mac] and len(globals.KNOWN_DEVICES[mac] ['specificconfiguration']) > 0): logging.debug('Already known handles ' + str( globals.KNOWN_DEVICES[mac]['specificconfiguration'])) chars = [ globals.KNOWN_DEVICES[mac]['specificconfiguration'] ['colorhandle'], globals.KNOWN_DEVICES[mac] ['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] refreshlist = globals.KNOWN_DEVICES[mac]['refreshlist'] logging.debug('Here is the list to refresh ' + str(refreshlist)) if 'color' in refreshlist: try: color = utils.tuple_to_hex( struct.unpack('4B', conn.readCharacteristic(chars[0]))) if color[0:2] != '00': color = 'FFFFFF' else: color = color[2:] result['color'] = '#' + color except Exception as e: logging.debug(str(e)) if 'effect' in refreshlist: try: effect = utils.tuple_to_hex( struct.unpack('8B', conn.readCharacteristic(chars[1]))) mode = effect[8:10] if mode == '04': result['mode'] = 'Bougie' elif mode == '01': result['mode'] = 'Fondu uni' elif mode == '00': result['mode'] = 'Flash uni' elif mode == '02': result['mode'] = 'Flash arc-en-ciel' elif mode == '03': result['mode'] = 'Fondu arc-en-ciel' else: result['mode'] = 'Aucun' speed = 255 - int(effect[12:14], 16) result['speed'] = speed except Exception as e: logging.debug(str(e)) if 'battery' in refreshlist: try: if 'hasbatteryinfo' in refreshlist and refreshlist[ 'hasbatteryinfo'] == 1: battery = struct.unpack( '2B', conn.readCharacteristic(refreshlist['battery'])) else: battery = struct.unpack( '1B', conn.readCharacteristic(refreshlist['battery'])) result['battery'] = battery[0] if 'hasbatteryinfo' in refreshlist and refreshlist[ 'hasbatteryinfo'] == 1: if battery[1]: result['mode'] = result['mode'] + ' (En charge)' else: result['mode'] = result['mode'] + ' (En décharge)' except Exception as e: logging.debug(str(e)) except Exception as e: logging.debug(str(e)) conn.disconnect() return logging.debug(str(result)) conn.disconnect() result['id'] = mac 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): 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_handler(message): manuf = '' if manuf in message['command']: manuf = message['command']['manuf'] name = message['command']['name'] if 'localname' in globals.KNOWN_DEVICES[message['device']['id']]: message['device']['localname'] = globals.KNOWN_DEVICES[ message['device']['id']]['localname'] result = {} if message['cmd'] == 'helper' or message['cmd'] == 'helperrandom': type = 'public' if message['cmd'] == 'helperrandom': type = 'random' try: mac = message['device']['id'] if mac in globals.KEEPED_CONNECTION: logging.debug('ACTION------Already a connection for ' + mac + ' use it') conn = globals.KEEPED_CONNECTION[mac] else: logging.debug('ACTION------Creating a new connection for ' + mac) conn = Connector(mac) globals.KEEPED_CONNECTION[mac] = conn conn.connect(type=type) if not conn.isconnected: conn.connect(type=type) if not conn.isconnected: return try: conn.helper() except Exception as e: logging.debug("ACTION------Helper failed : %s" % str(e)) conn.disconnect() return except Exception as e: logging.debug("ACTION------Helper failed : %s" % str(e)) elif message['cmd'] == 'refresh': for compatible in globals.COMPATIBILITY: classname = message['command']['device']['name'] if compatible().name.lower() == classname.lower(): logging.debug('ACTION------Attempt to refresh values') try: result = compatible().read(message['device']['id']) except Exception as e: logging.debug("ACTION------Refresh failed : %s" % str(e)) break if result and len(result) >= 2: if message['device'][ 'id'] in globals.LAST_STATE and result == globals.LAST_STATE[ message['device']['id']]: return else: globals.LAST_STATE[message['device']['id']] = result result['source'] = globals.daemonname globals.JEEDOM_COM.add_changes( 'devices::' + message['device']['id'], result) return else: for device in globals.COMPATIBILITY: if device().isvalid(name, manuf): try: result = device().action(message) except Exception as e: logging.debug("ACTION------Action failed :" + str(e)) if result: if message['device'][ 'id'] in globals.LAST_STATE and result == globals.LAST_STATE[ message['device']['id']]: return else: globals.LAST_STATE[message['device']['id']] = result result['source'] = globals.daemonname globals.JEEDOM_COM.add_changes( 'devices::' + message['device']['id'], result) return return return
def read(self, mac, connection=''): result = {} try: if mac in globals.KEEPED_CONNECTION: logging.debug('Already a connection for ' + mac + ' use it') conn = globals.KEEPED_CONNECTION[mac] else: if connection != '': conn = connection 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 refreshlist = globals.KNOWN_DEVICES[mac]['refreshlist'] logging.debug('Here is the list to refresh ' + str(refreshlist)) if 'color' in refreshlist: try: color = utils.tuple_to_hex( struct.unpack( '4B', conn.readCharacteristic(refreshlist['color']))) if color[0:2] != '00': color = 'FFFFFF' else: color = color[2:] result['color'] = '#' + color except Exception as e: logging.debug(str(e)) if 'effect' in refreshlist: try: effect = utils.tuple_to_hex( struct.unpack( '8B', conn.readCharacteristic(refreshlist['effect']))) mode = effect[8:10] if mode == '04': result['mode'] = 'Bougie' elif mode == '01': result['mode'] = 'Fondu uni' elif mode == '00': result['mode'] = 'Flash uni' elif mode == '02': result['mode'] = 'Flash arc-en-ciel' elif mode == '03': result['mode'] = 'Fondu arc-en-ciel' else: result['mode'] = 'Aucun' speed = 255 - int(effect[12:14], 16) result['speed'] = speed except Exception as e: logging.debug(str(e)) if 'battery' in refreshlist: try: if 'hasbatteryinfo' in refreshlist and refreshlist[ 'hasbatteryinfo'] == 1: battery = struct.unpack( '2B', conn.readCharacteristic(refreshlist['battery'])) else: battery = struct.unpack( '1B', conn.readCharacteristic(refreshlist['battery'])) result['battery'] = battery[0] if 'hasbatteryinfo' in refreshlist and refreshlist[ 'hasbatteryinfo'] == 1: if battery[1]: result['mode'] = result['mode'] + ' (En charge)' else: result['mode'] = result['mode'] + ' (En décharge)' except Exception as e: logging.debug(str(e)) except Exception as e: logging.debug(str(e)) conn.disconnect() return logging.debug(str(result)) conn.disconnect() result['id'] = mac return result