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 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 device_user(dev_id): row = mb2db.get_device_by_id(cnxn_string, dev_id) if row: if request.method == "GET": u = mb2db.get_device_user(cnxn_string, dev_id) if u: return json.dumps({ "id": u.usuarioId, "name": u.nombre, "surname": u.apellidos, "email": u.correo, "weight": u.peso, "height": u.altura, "center": u.centroId, "dev_id": dev_id, "alias": mb2db.get_alias(u.nombre, u.apellidos, u.dni) }), 200 else: return json.dumps({}), 200 if row.mac in connected_devices.keys(): try: mb2 = connected_devices[row.mac] if request.method == "POST": user_id = request.form.get('user_id') position = request.form.get('position') if not position and not user_id: abort(400) if position not in ["left", "right"]: abort(400) else: pos_bit = 0 if (position == "left") else 0 udata = mb2db.get_user_data(cnxn_string, user_id) if udata: try: mb2.setUserInfo(udata["alias"], udata["sex"], udata["height"], udata["weight"], udata["birth"]) mb2.setWearLocation(position) except Exception as e: print(e) else: if mb2db.set_device_user(cnxn_string, dev_id, user_id, pos_bit): return json.dumps({ "linked": True, "dev_id": dev_id, "user_id": user_id, "position": position }), 200 abort(403) if request.method == "DELETE": if mb2db.release_device_user(cnxn_string, dev_id): return json.dumps({ "linked": False, "dev_id": dev_id }), 200 else: abort(403) except BTLEException as e: print( "There was a problem handling the user of 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)
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)