def do_devices(self, line): tmp_mibands = copy.deepcopy(self.scd.tmp_devices) self.mibands = {k: v["device"] for k, v in tmp_mibands.items()} for idx, mb in enumerate(self.mibands.keys()): name = "Someone" uid = 0 udata = None if args.mode == "db": udata = mb2db.get_user_data( mb2db.cnxn, mb2db.get_device_user(mb2db.cnxn, mb)) else: # TODO: User Data on local storage??? pass if udata: name = udata["alias"] uid = udata["id"] str = "[%s]%10s's MB2 <U:%05d> (%s) %sdB " % ( idx, name, uid, mb, self.mibands[self.mibands.keys()[idx]].rssi) if (args.mode == "db" and mb2db.is_device_registered( mb2db.cnxn, mb)) or (args.mode == "json" and mb in self.registered_devices): str += "[R]" if mb in connected_devices: mb2 = connected_devices[mb] if args.mode == "db": mb2db.update_battery(mb2db.cnxn, mb2.addr, mb2.battery_info['level']) str += "[C] [B:{0:03d}%]".format(mb2.battery_info["level"]) print str
def do_connect(self, params): try: l = int(params) except ValueError: print "*** argument should be number" return except IndexError: print "*** connect takes at least one parameter" return dev_id = l if dev_id >= len(self.mibands.keys()): print "*** device not in the device list" return if len(connected_devices.keys()) >= 5: print( "Can't connect to more than 5 devices at the same time, disconnect some" ) else: if ((args.mode == "db" and mb2db.is_device_registered(mb2db.cnxn, self.mibands.keys()[dev_id])) or args.mode == "json" and self.mibands.keys()[dev_id] in self.registered_devices): if self.mibands.keys()[dev_id] in connected_devices.keys(): print("That MiBand2 is already connected") else: try: addr = self.mibands.keys()[dev_id] self.scd.tmp_devices[addr]["strikes"] = -9999 if not addr in self.devices_keys.keys(): self.devices_keys[addr] = random_key() mb2 = MiBand2(addr, self.devices_keys[addr], initialize=False) connected_devices[self.mibands.keys()[dev_id]] = mb2 if args.mode == "db": alarms = mb2db.get_device_alarms( mb2db.cnxn, mb2.addr) mb2db.update_battery(mb2db.cnxn, mb2.addr, mb2.battery_info['level']) else: if mb2.addr in self.devices_alarms.keys(): alarms = self.devices_alarms[mb2.addr] else: alarms = [] for a in alarms: mb2.alarms += [ MiBand2Alarm(a["hour"], a["minute"], enabled=a["enabled"], repetitionMask=a["repetition"]) ] except BTLEException as e: print( "There was a problem connecting to this MiBand2, try again later" ) print e else: print( "You have to register the MiBand2 before connecting to it")
def devices(): if request.method == "GET": dev_list = [] print(connected_devices) mibands = copy.deepcopy(tmp_mibands) for idx, mb in enumerate(mibands.keys()): dev_id = mb2db.get_device_id(cnxn_string, mb) dev_user = mb2db.get_device_user(cnxn_string, dev_id) device = mb2db.get_device_by_id(cnxn_string, dev_id) battery = -1 if device: battery = device.bateria username = (dev_user.nombre + " " + dev_user.apellidos) if dev_user else "Unregistered" dev_dict = { "address": mb, "signal": mibands[mibands.keys()[idx]].rssi, "registered": False, "connected": False, "dev_id": dev_id, "user_name": username, "battery": battery, "strikes": strikes[mibands[mibands.keys()[idx]].addr.upper()] } if mb2db.is_device_registered(cnxn_string, mb): dev_dict["registered"] = True if mb in connected_devices.keys(): dev_dict["connected"] = True dev_list += [dev_dict] print json.dumps(dev_list) return json.dumps(dev_list) elif request.method == "POST": addr = request.form["address"].upper() if mb2db.is_device_registered(cnxn_string, addr): abort(403) else: try: strikes[addr] = -9999 mb2 = MiBand2(addr, initialize=True) mb2.cleanAlarms() dev_id = mb2db.register_device(cnxn_string, mb2.addr) mb2db.delete_all_alarms(cnxn_string, dev_id) mb2db.update_battery(cnxn_string, mb2.addr, mb2.battery_info['level']) # Device stays connected after initialize, but we don't want that mb2.disconnect() strikes[addr] = 0 return json.dumps({"dev_id": dev_id, "registered": True}) except BTLEException as e: print( "There was a problem registering this MiBand2, try again later" ) print e abort(500) except BTLEException.DISCONNECTED as d: print("Device disconnected, removing from connected devices") del connected_devices[addr] del mb2 abort(500)
def do_register(self, params): try: l = int(params) except ValueError: print "*** argument should be number" return except IndexError: print "*** register takes at least one parameter" return dev_id = l if dev_id >= len(self.mibands.keys()): print "*** device not in the device list" return if ((args.mode == "db" and mb2db.is_device_registered(mb2db.cnxn, self.mibands.keys()[dev_id])) or args.mode == "json" and self.mibands.keys()[dev_id] in self.registered_devices): print("That MiBand2 is already registered") else: mb2 = None try: addr = self.mibands.keys()[dev_id] self.scd.tmp_devices[addr]["strikes"] = -9999 if not addr in self.devices_keys.keys(): self.devices_keys[addr] = random_key() mb2 = MiBand2(addr, self.devices_keys[addr], initialize=False) mb2.cleanAlarms() if args.mode == "db": mb2db.delete_all_alarms(mb2db.cnxn, mb2.addr) mb2db.register_device(mb2db.cnxn, mb2.addr) mb2db.update_battery(mb2db.cnxn, mb2.addr, mb2.battery_info['level']) else: self.registered_devices += [mb2.addr] # Device stays connected after initialize, but we don't want that mb2.disconnect() except BTLEException as e: print( "There was a problem registering this MiBand2, try again later" ) print e mb2.disconnect
def device(dev_id): row = mb2db.get_device_by_id(cnxn_string, dev_id) if row: if request.method == "GET": connected = True if row.mac in connected_devices else False signal = 0 mibands = copy.deepcopy(tmp_mibands) if row.mac in mibands.keys(): signal = mibands[row.mac].rssi dev_user = mb2db.get_device_user(cnxn_string, dev_id) username = (dev_user.nombre + " " + dev_user.apellidos) if dev_user else "Unregistered" detail_dict = { "dev_id": row.dispositivoId, "battery": row.bateria, "registered": row.registrado, "address": row.mac, "connected": connected, "signal": signal, "visible": (signal < 0), "user_name": username } return json.dumps(detail_dict) elif request.method == "PUT": if mb2db.is_device_registered(cnxn_string, row.mac): action = request.form.get("action") strikes[row.mac] = -9999 if action == "connect" and row.mac not in connected_devices.keys( ): try: mb2 = MiBand2(row.mac, initialize=False) connected_devices[row.mac] = mb2 alarms = mb2db.get_device_alarms(cnxn_string, mb2.addr) mb2db.update_battery(cnxn_string, mb2.addr, mb2.battery_info['level']) for a in alarms: mb2.alarms += [ MiBand2Alarm(a["hour"], a["minute"], enabled=a["enabled"], repetitionMask=a["repetition"]) ] return json.dumps({ "connected": True, "dev_id": row.dispositivoId }), 200 except BTLEException as e: print( "There was a problem (dis)connecting to this MiBand2, try again later" ) print e abort(500) except BTLEException.DISCONNECTED as d: print( "Device disconnected, removing from connected devices" ) del connected_devices[row.mac] del mb2 abort(500) elif action == "disconnect" and row.mac in connected_devices.keys( ): try: mb2 = connected_devices[row.mac] mb2.disconnect() del connected_devices[row.mac] del mb2 print("MiBand2 disconnected!") return json.dumps({ "connected": False, "dev_id": row.dispositivoId }), 200 except BTLEException as e: print( "There was a problem disconnecting this MiBand2, try again later" ) print e abort(500) except BTLEException.DISCONNECTED as d: print( "Device disconnected, removing from connected devices" ) del connected_devices[row.mac] del mb2 abort(500) elif request.method == "DELETE": # Just Unregister MiBand2 if mb2db.is_device_registered(cnxn_string, row.mac): if not row.mac in connected_devices.keys(): try: dev_id = mb2db.get_device_id(cnxn_string, row.mac) mb2db.unregister_device(cnxn_string, dev_id) mb2db.delete_all_alarms(cnxn_string, dev_id) print("MiBand2 unregistered!") return json.dumps({ "registered": False, "dev_id": row.dispositivoId }), 200 except BTLEException as e: print( "There was a problem unregistering this MiBand2, try again later" ) print e abort(500) except BTLEException.DISCONNECTED as d: print( "Device disconnected, removing from connected devices" ) del connected_devices[row.mac] del mb2 abort(500) abort(403) else: abort(404)