def checkNetwork(): global netmode, lastdisconntime, lastaptime try: if Settings.NetMan.WifiDevWatch>=0 and Settings.NetMan.WifiDevNum>=0: if Settings.NetworkDevices[Settings.NetMan.WifiDevNum].apmode==0: anetmode = Settings.NetworkDevices[Settings.NetMan.WifiDevWatch].isconnected() if anetmode!=netmode: # network mode changed netmode=anetmode if netmode: # if connected lastdisconntime = 0 # forgive last disconnect time commands.rulesProcessing("Network#Connected",rpieGlobals.RULE_SYSTEM) return True else: lastdisconntime = time.time() # store last disconnect time commands.rulesProcessing("Network#Disconnected",rpieGlobals.RULE_SYSTEM) return True elif anetmode==False: # otherwise, if in disconnect state if Settings.NetMan.APMode not in [-1,100]: if lastdisconntime!=0: if (time.time()-lastdisconntime)>int(Settings.NetMan.APModeTime): Network.AP_start(Settings.NetMan.WifiDevNum) lastdisconntime = 0 # forgive last disconnect time lastaptime = time.time() else: lastdisconntime = time.time() # store last disconnect time else: # apmode active if Settings.NetMan.APStopTime>-1: if Settings.NetMan.APMode not in [-1,100]: if lastaptime!=0: if (time.time()-lastaptime)>int(Settings.NetMan.APStopTime): Network.AP_stop(Settings.NetMan.WifiDevNum) lastaptime = 0 lastdisconntime = 0 # forgive last disconnect time except Exception as e: print(e)
def doCleanup(): rulesProcessing("System#Shutdown", rpieGlobals.RULE_SYSTEM) if (len(Settings.Tasks) > 1) or ((len(Settings.Tasks) == 1) and (Settings.Tasks[0] != False)): Settings.savetasks() procarr = [] for x in range(0, len(Settings.Tasks)): if (Settings.Tasks[x]) and type( Settings.Tasks[x]) is not bool: # device exists try: if (Settings.Tasks[x].enabled): # device enabled t = threading.Thread(target=Settings.Tasks[x].plugin_exit) t.daemon = True procarr.append(t) t.start() except: pass if len(procarr) > 0: for process in procarr: process.join(1) try: for t in range(0, rpieGlobals.RULES_TIMER_MAX): rpieTime.Timers[t].pause() for t in range(0, rpieGlobals.SYSTEM_TIMER_MAX): rpieTime.SysTimers[t].pause() except: pass procarr = [] for y in range(0, len(Settings.Controllers)): if (Settings.Controllers[y]): if (Settings.Controllers[y].enabled): t = threading.Thread( target=Settings.Controllers[y].controller_exit) t.daemon = True procarr.append(t) t.start() if int(Settings.NetMan.WifiDevNum) >= 0 and int( Settings.NetMan.APMode > -1): apdev = int(Settings.NetMan.WifiDevNum) Network.AP_stop(apdev) # try to stop AP mode if needed if len(procarr) > 0: for process in procarr: process.join()
def doExecuteCommand(cmdline, Parse=True): if Parse: retval, state = parseruleline(cmdline) else: retval = cmdline cmdarr = retval.split(",") if (" " in retval) and not ("," in retval): cmdarr = retval.split(" ") elif (" " in retval) and ( "," in retval): # workaround for possible space instead comma problem fsp = retval.find(" ") fco = retval.find(",") if fsp < fco: c2 = retval.split(" ") cmdarr = retval[(fsp + 1):].split(",") cmdarr = [c2[0]] + cmdarr cmdarr[0] = cmdarr[0].strip().lower() commandfound = False misc.addLog(rpieGlobals.LOG_LEVEL_INFO, "CMD: " + cmdline.replace("==", "=")) if cmdarr[0] == "delay": try: s = float(cmdarr[1]) except: s = 1000 s = (s / 1000) time.sleep(s) commandfound = True return commandfound elif cmdarr[0] == "taskrun": if len(Settings.Tasks) < 1: return False try: s = int(cmdarr[1]) except: s = -1 if s > 0 and (s <= len(Settings.Tasks)): s = s - 1 # array is 0 based, tasks is 1 based if (type(Settings.Tasks[s]) != bool) and (Settings.Tasks[s]): if (Settings.Tasks[s].enabled): Settings.Tasks[s].plugin_read() commandfound = True return commandfound elif cmdarr[0] == "taskvalueset": if len(Settings.Tasks) < 1: return False try: s = int(cmdarr[1]) except: s = -1 try: v = int(cmdarr[2]) except: v = 1 #v=v-1 if s == -1: s, v = Settings.getTaskValueIndex(cmdarr[1], cmdarr[2]) if s > 0 and (s <= len(Settings.Tasks)): s = s - 1 # array is 0 based, tasks is 1 based if (type(Settings.Tasks[s]) != bool) and (Settings.Tasks[s]): if v > (Settings.Tasks[s].valuecount): v = Settings.Tasks[s].valuecount if v < 1: v = 1 try: Settings.Tasks[s].set_value( v, parsevalue(str(cmdarr[3].strip())), False) except Exception as e: pass # print("Set value error: ",e) commandfound = True return commandfound elif cmdarr[0] == "taskvaluesetandrun": if len(Settings.Tasks) < 1: return False try: s = int(cmdarr[1]) except: s = -1 try: v = int(cmdarr[2]) except: v = 1 #v=v-1 if s == -1: s, v = Settings.getTaskValueIndex(cmdarr[1], cmdarr[2]) if s > 0 and (s <= len(Settings.Tasks)): s = s - 1 # array is 0 based, tasks is 1 based if (type(Settings.Tasks[s]) != bool) and (Settings.Tasks[s]): if v > (Settings.Tasks[s].valuecount): v = Settings.Tasks[s].valuecount if v < 1: v = 1 Settings.Tasks[s].set_value(v, parsevalue(str(cmdarr[3]).strip()), True) commandfound = True return commandfound elif cmdarr[0] == "timerpause": if len(rpieTime.Timers) < 1: return False try: s = int(cmdarr[1]) except: s = -1 if s > 0 and (s < len(rpieTime.Timers)): s = s - 1 # array is 0 based, timers is 1 based rpieTime.Timers[s].pause() commandfound = True return commandfound elif cmdarr[0] == "timerresume": if len(rpieTime.Timers) < 1: return False try: s = int(cmdarr[1]) except: s = -1 if s > 0 and (s < len(rpieTime.Timers)): s = s - 1 # array is 0 based, timers is 1 based rpieTime.Timers[s].resume() commandfound = True return commandfound elif cmdarr[0] == "timerset": if len(rpieTime.Timers) < 1: return False try: s = int(cmdarr[1]) except: s = -1 try: v = int(cmdarr[2]) except: v = 1 if s > 0 and (s < len(rpieTime.Timers)): s = s - 1 # array is 0 based, timers is 1 based if v == 0: rpieTime.Timers[s].stop(False) else: rpieTime.Timers[s].addcallback(TimerCallback) rpieTime.Timers[s].start(v) commandfound = True return commandfound elif cmdarr[0] == "event": rulesProcessing(cmdarr[1], rpieGlobals.RULE_USER) commandfound = True return commandfound elif cmdarr[0] == "sendto": try: unitno = int(cmdarr[1]) except: unitno = -1 data = "" if len(cmdarr) > 2: sepp = (len(cmdarr[0]) + len(cmdarr[1]) + 1) sepp = cmdline.find(',', sepp) data = cmdline[sepp + 1:].replace("==", "=") else: unitno = -1 if unitno >= 0 and unitno <= 255: cfound = False for y in range(len(Settings.Controllers)): if (Settings.Controllers[y]): if (Settings.Controllers[y].enabled): if "ESPEasy P2P" in Settings.Controllers[ y].getcontrollername(): Settings.Controllers[y].udpsender(unitno, data, 1) cfound = True if cfound == False: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, "ESPEasy P2P controller not found!") commandfound = True return commandfound elif cmdarr[0] == "blecommand": try: unitno = int(cmdarr[1]) except: unitno = -1 data = "" if len(cmdarr) > 2: sepp = (len(cmdarr[0]) + len(cmdarr[1]) + 1) sepp = cmdline.find(',', sepp) data = cmdline[sepp + 1:].replace("==", "=") else: unitno = -1 if unitno >= 0 and unitno <= 255: cfound = False for y in range(len(Settings.Controllers)): if (Settings.Controllers[y]): if (Settings.Controllers[y].enabled): if "BLE Direct" in Settings.Controllers[ y].getcontrollername(): Settings.Controllers[y].sendcommand(unitno, data) cfound = True if cfound == False: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, "BLE controller not found!") commandfound = True return commandfound elif cmdarr[0] == "loracommand": try: unitno = int(cmdarr[1]) except: unitno = -1 data = "" if len(cmdarr) > 2: sepp = (len(cmdarr[0]) + len(cmdarr[1]) + 1) sepp = cmdline.find(',', sepp) data = cmdline[sepp + 1:].replace("==", "=") else: unitno = -1 if unitno >= 0 and unitno <= 255: cfound = False for y in range(len(Settings.Controllers)): if (Settings.Controllers[y]): if (Settings.Controllers[y].enabled): if "LORA Direct" in Settings.Controllers[ y].getcontrollername(): Settings.Controllers[y].sendcommand(unitno, data) cfound = True if cfound == False: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, "LORA controller not found!") commandfound = True return commandfound elif cmdarr[0] == "espnowcommand": try: unitno = int(cmdarr[1]) except: unitno = -1 data = "" if len(cmdarr) > 2: sepp = (len(cmdarr[0]) + len(cmdarr[1]) + 1) sepp = cmdline.find(',', sepp) data = cmdline[sepp + 1:].replace("==", "=") else: unitno = -1 if unitno >= 0 and unitno <= 255: cfound = False for y in range(len(Settings.Controllers)): if (Settings.Controllers[y]): if (Settings.Controllers[y].enabled): if "ESPNow" in Settings.Controllers[ y].getcontrollername(): Settings.Controllers[y].sendcommand(unitno, data) cfound = True if cfound == False: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, "ESPNow controller not found!") commandfound = True return commandfound elif cmdarr[0] == "serialcommand": data = "" if len(cmdarr) > 1: sepp = cmdline.find(',') data = cmdline[sepp + 1:].replace("==", "=") else: return False cfound = False for y in range(len(Settings.Controllers)): if (Settings.Controllers[y]): if (Settings.Controllers[y].enabled): if "ESPNow" in Settings.Controllers[y].getcontrollername(): Settings.Controllers[y].serialcommand(data) cfound = True if cfound == False: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, "ESPNow controller not found!") commandfound = True return commandfound elif cmdarr[0] == "publish": topic = cmdarr[1].strip() data = "" if len(cmdarr) > 2: sepp = (len(cmdarr[0]) + len(cmdarr[1]) + 1) sepp = cmdline.find(',', sepp) data = cmdline[sepp + 1:].replace("==", "=") else: topic = "" commandfound = False if topic != "": cfound = False for y in range(len(Settings.Controllers)): if (Settings.Controllers[y]): if (Settings.Controllers[y].enabled): try: if Settings.Controllers[y].mqttclient is not None: Settings.Controllers[y].mqttclient.publish( topic, data) commandfound = True cfound = True break except: cfound = False if cfound == False: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, "MQTT capable controller not found!") return commandfound elif cmdarr[0] == "sendtohttp": destaddr = cmdarr[1].strip() try: destport = int(cmdarr[2]) except: destport = -1 data = "" if len(cmdarr) > 3: sepp = (len(cmdarr[0]) + len(cmdarr[1]) + len(cmdarr[2]) + 2) sepp = cmdline.find(',', sepp) data = cmdline[sepp + 1:].replace("==", "=") else: destport = -1 if destport > 0: commandfound = True curl = "http://" + destaddr + ":" + str(destport) + data t = threading.Thread(target=urlget, args=(curl, )) t.daemon = True t.start() else: commandfound = False return commandfound elif cmdarr[0] == "sendtoudp": destaddr = cmdarr[1].strip() try: destport = int(cmdarr[2]) except: destport = -1 data = "" if len(cmdarr) > 3: sepp = (len(cmdarr[0]) + len(cmdarr[1]) + len(cmdarr[2]) + 2) sepp = cmdline.find(',', sepp) data = cmdline[sepp + 1:].replace("==", "=") else: destport = -1 if destport > 0: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) for r in range(0, 1): s.sendto(bytes(data, "utf-8"), (destaddr, int(destport))) if r < 1: time.sleep(0.1) commandfound = True else: commandfound = False return commandfound elif cmdarr[0] == "wifiapmode": if int(Settings.NetMan.WifiDevNum) >= 0: apdev = int(Settings.NetMan.WifiDevNum) else: apdev = Settings.NetMan.getfirstwirelessdevnum() Network.AP_start(apdev, True) commandfound = True return commandfound elif cmdarr[0] == "wifistamode": if int(Settings.NetMan.WifiDevNum) >= 0: apdev = int(Settings.NetMan.WifiDevNum) else: apdev = Settings.NetMan.getfirstwirelessdevnum() Network.AP_stop(apdev) Settings.NetMan.APMode = -1 commandfound = True return commandfound elif cmdarr[0] == "wificonnect": # implement it commandfound = False return commandfound elif cmdarr[0] == "wifimode": # implement it commandfound = False return commandfound elif cmdarr[0] == "reboot": doCleanup() os.popen(OS.cmdline_rootcorrect("sudo reboot")) # os.kill(os.getpid(), signal.SIGINT) commandfound = True return commandfound elif cmdarr[0] == "reset": try: os.popen("rm -r data/*.json") except: pass Settings.Controllers = [False] Settings.NetworkDevices = [] Settings.Pinout = [] Settings.Settings = { "Name": "RPIEasy", "Unit": 0, "Password": "", "Delay": 60 } Settings.Tasks = [False] Settings.NetMan.networkinit() commandfound = True return commandfound elif cmdarr[0] == "halt": doCleanup() os.popen(OS.cmdline_rootcorrect("sudo shutdown -h now")) # os.kill(os.getpid(), signal.SIGINT) commandfound = True return commandfound elif cmdarr[0] == "update": misc.addLog(rpieGlobals.LOG_LEVEL_INFO, "Starting git clone") os.popen( "cp -rf run.sh run.sh.bak && rm -rf update && git clone https://github.com/enesbcs/rpieasy.git update" ).read() time.sleep(2) if os.path.isdir("update"): misc.addLog(rpieGlobals.LOG_LEVEL_INFO, "Download successful, starting to overwrite files") os.popen( "rm -rf .git && rm -rf update/data update/files && mv -f update/.git .git && cp -rf update/lib/* lib/ && cp -rf update/img/* img/ && rm -rf update/lib update/img && mv -f update/* . && rm -rf update && cp -rf run.sh.bak run.sh" ).read() time.sleep(0.5) os.kill(os.getpid(), signal.SIGINT) else: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, "Update failed") commandfound = True return commandfound elif cmdarr[0] == "exit": os.kill(os.getpid(), signal.SIGINT) commandfound = True return commandfound elif cmdarr[0] == "notify": try: plugin = int(cmdarr[1]) except: plugin = 0 data = "" if len(cmdarr) > 1 and plugin > 0: sepp = (len(cmdarr[0]) + len(cmdarr[1]) + 2) data = cmdline[sepp:].replace("==", "=") commandfound = doExecuteNotification(plugin - 1, data) return commandfound elif cmdarr[0] == "setvolume": vmr = 0 if "+" in cmdarr[1] or "-" in cmdarr[1]: vmr = 1 try: vol = int(cmdarr[1]) except: vol = -200 if (vmr == 0): # absolute vol if (vol >= 0 and vol <= 100): try: OS.setvolume(vol) commandfound = True except Exception as e: commandfound = str(e) else: if (vol >= -100 and vol <= 100): # relative vol try: avol = OS.getvolume() OS.setvolume(int(avol) + vol) commandfound = True except Exception as e: commandfound = str(e) return commandfound if commandfound == False: commandfound = doExecutePluginCommand(retval) if commandfound == False: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, "Unknown command: " + cmdline) return commandfound