def handleRequest(command, args): respMsg = {'type':'response', 'command':command, 'data':None} if command == 'scanWifi': println("Command recevied to scan wifi") apInfo = Wifi.findMhaDevices() if len(apInfo) < 1: Logger.log(Logger.LogLevel.error, "Unable to find any MHA Devices in accespoint mode") else: data = [] for ap in apInfo: data.append({'macaddr': Wifi.getMacAddr(ap), 'ssid':Wifi.getSsid(ap)}) respMsg['data'] = data Logger.log(Logger.LogLevel.info, "New APs:" + str(data)) publishMessage(json.dumps(respMsg)) if command == 'connectToWifi': println('Command Received to connec to Wifi') if 'ssid' in args: ssid = args['ssid'] macaddr = '' if 'macaddr' in args: macaddr = args['macaddr'] status = {'connectedToWifi': 'True'} if connectToWifi(ssid, macaddr) != 0: status['connectedToWifi'] = 'False' respMsg['data'] = status publishMessage(json.dumps(respMsg))
def scanWifi(): availableWifiNetworks = [] shcmd = 'sudo iwlist ' + WIFI_INTERFACE + ' scan' response = subprocess.run(shcmd.split(' '), capture_output=True, timeout=30) if response.returncode == 0: output = response.stdout.decode("utf-8").split('\n') macAddr = '' ssid = '' for line in output: if 'Address' in line: macAddr = line.split('Address:')[-1][1:-1] if 'ESSID' in line: ssid = line.split(':')[-1][1:-1] if macAddr != '' and ssid != '': availableWifiNetworks.append(WifiAp(macAddr, ssid)) macAddr = '' ssid = '' else: Logger.log(Logger.LogLevel.error, "Wifi Scaning failed") return availableWifiNetworks