def device_add(): res = {"status": None, "msg": None} mac = request.args["mac"] if "mac" in request.args else "" pin = request.args["pin"] if "pin" in request.args else "0000" if mac and BTCtl().macPattern.match(mac): dev = Hub().findDevice(mac) if dev is None: BTCtl().remove(mac) idx = Hub().addDevice(mac, pin) dev = Hub().findDevice(idx) if dev is not None: try: Hub().addEvent("setup", dev, 0) res["status"] = True res["msg"] = idx except Exception as ex: Logger().write(ex, tag="EXCEPT") Hub().removeDevice(idx) res["status"] = False res["msg"] = "Failed to setup device" else: res["status"] = False res["msg"] = "Failed to add device" else: res["status"] = False res["msg"] = "Device is already paired" else: res["status"] = False res["msg"] = "Mac address is required" return jsonify(res)
def connect(self): while self.busy is True: sleep(1) self.busy = True self.sock = BTCtl().connect(self.mac) if self.sock is None: self.close()
def setup(self): ret = BTCtl().pair(self.mac, self.pin) if ret is False: return False self.connect() pin = "%04d" % randint(0,9999) ret = self.send("setup%s%d" % (pin, 0)) self.close() self.busy = True BTCtl().remove(self.mac) if ret == -1: self.close() return False self.pin = pin ret = BTCtl().pair(self.mac, self.pin) self.close() return ret
def scan(): res = {"status": None, "msg": None} try: res["status"] = True res["msg"] = BTCtl().scan() except Exception as ex: Logger().write(ex, tag="EXCEPT") res["status"] = False res["msg"] = "Failed to scan for bluetooth devices" return jsonify(res)
def removeDevice(self, idx): dev = self.findDevice(idx) self.removeObj(self.devices, idx) dev.close() BTCtl().remove(dev["mac"]) for idx in tuple(self.events.keys()): ev = self.events[idx] if ev.dev["mac"] == dev["mac"]: self.removeEvent(idx) self.save() return True
class TrueDevice: def __init__(self, mac, pin="0000"): self.mac = mac self.pin = pin self.history = [] self.sock = None self.busy = False def __del__(self): self.close() def __getitem__(self, key): return self.__dict__[key] def __setitem__(self, key, value): self.__dict__[key] = value def __str__(self): return "Device %s" % self.mac def close(self): if self.busy is True: if self.sock is not None: self.sock.close() self.sock = None self.busy = False sleep(1) def connect(self): while self.busy is True: sleep(1) self.busy = True self.sock = BTCtl().connect(self.mac) if self.sock is None: self.close() def recv(self): if self.sock is None: return None data = bytes() while 10 not in data: # "\n" in data try: rcv = self.sock.recv(128) data += rcv except Exception as ex: Logger().write("[!] Failed to recieve from %s" % str(self), tag="DEVICE") Logger().write(ex, tag="EXCEPT") self.close() return None out = data.decode().strip() Logger().write("[>] Recieved '%s' from %s" % (out, str(self)), tag="DEVICE") return out def send(self, msg): if self.sock is None: return -1 if isinstance(msg, bytes): data = msg else: cmd = msg.strip() Logger().write("[<] Sending '%s' to %s" % (cmd, str(self)), tag="DEVICE") data = (cmd + "\n").encode() try: ret = self.sock.send(data) sleep(1) # sleep for proper hc06 reading return ret except Exception as ex: Logger().write("[!] %s has disconnected" % str(self), tag="DEVICE") Logger().write(ex, tag="EXCEPT") return -1 def setup(self): ret = BTCtl().pair(self.mac, self.pin) if ret is False: return False self.connect() pin = "%04d" % randint(0,9999) ret = self.send("setup%s%d" % (pin, 0)) self.close() self.busy = True BTCtl().remove(self.mac) if ret == -1: self.close() return False self.pin = pin ret = BTCtl().pair(self.mac, self.pin) self.close() return ret
res["msg"] = "OK" if res["status"] else "Failed to remove event" else: res["status"] = False res["msg"] = "There is no such event" return jsonify(res) if __name__ == "__main__": Logger("hub") Logger().write("[!] SmartHub is booting", tag="BOOT") WF = WiFiCtl() Logger().write("[!] Checking Wi-Fi connection", tag="BOOT") ip = WF.check() if ip is False: Logger().write( "[!] Wi-Fi network is not available. Please setup Wi-Fi network using Bluetooth CLI with PIN '%s'" % BTCtl().pin, tag="BOOT") while ip is False: ip = WF.check() sleep(1) Logger().write("[!] Connected to Wi-Fi network with ip address '%s'" % ip, tag="BOOT") Logger().write("[!] SmartHub has started", tag="BOOT") Hub().start() App.run(host="0.0.0.0")
ip = WiFiCtl().connect(name, password) dev.send(ip) elif cmd[0] == "scan": networks = WiFiCtl().scan() data = dumps(networks) dev.send(data) elif cmd[0] == "check": ip = WiFiCtl().check() dev.send(ip) elif cmd[0] == "disconnect": WiFiCtl().disconnect() dev.send("OK") else: dev.send("Unknown command") sleep(1) Logger().write("[-] Client has disconnected", tag="INFO") dev.close() if __name__ == "__main__": Logger("cli") BT = BTCtl() Logger().write("[!] Bluetooth Command Line Interface has started", tag="BOOT") while True: sock = BT.accept(check_pin=True) Thread(target=threadFunc, args=(sock, )).start() sleep(1)