def timer_thirty_second(self): if self.enabled: #send alive signals dp = data_packet() try: defdev = Settings.NetMan.getprimarydevice() except: defdev = -1 if defdev != -1: dp.infopacket["mac"] = Settings.NetworkDevices[defdev].mac dp.infopacket["ip"] = Settings.NetworkDevices[defdev].ip else: dp.infopacket["mac"] = "00:00:00:00:00:00" dp.infopacket["ip"] = "" if dp.infopacket["ip"] == "": dp.infopacket["ip"] = str(OS.get_ip()) dp.infopacket["unitno"] = int(Settings.Settings["Unit"]) dp.infopacket["build"] = int(rpieGlobals.BUILD) dp.infopacket["name"] = Settings.Settings["Name"] dp.infopacket["type"] = int(rpieGlobals.NODE_TYPE_ID_RPI_EASY_STD) dp.infopacket["port"] = int(Settings.WebUIPort) dp.encode(1) self.udpsender(255,dp.buffer,1) #clear old nodes for n in range(len(Settings.nodelist)): if Settings.nodelist[n]["age"] >= 10: misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"P2P unit disappeared: "+str(Settings.nodelist[n]["unitno"])) del Settings.nodelist[n] else: Settings.nodelist[n]["age"] += 1 return True
def getIP(self): if time.time()-self.iptime>30: self.iptime=time.time() ip = "" try: defdev = Settings.NetMan.getprimarydevice() except: defdev = -1 if defdev != -1: ip = Settings.NetworkDevices[defdev].ip else: ip = "" if ip == "": ip = str(OS.get_ip()) if ip != "": self.ip = ip return self.ip
def getglobalvar(varname): global SysVars svname = varname.strip().lower() res = "" if svname in SysVars: if svname == SysVars[0]: #%systime% 01:23:54 return datetime.now().strftime('%H:%M:%S') elif svname == SysVars[1]: #%systm_hm% 01:23 return datetime.now().strftime('%H:%M') elif svname == SysVars[2]: #%lcltime% 2018-03-16 01:23:54 return datetime.now().strftime('%Y-%m-%d %H:%M:%S') elif svname == SysVars[3]: #%syshour% 11 Current hour (hh) return datetime.now().strftime('%H') elif svname == SysVars[4]: #%sysmin% 22 Current minute (mm) return datetime.now().strftime('%M') elif svname == SysVars[5]: #%syssec% 33 Current second (ss) return datetime.now().strftime('%S') elif svname == SysVars[6]: #%sysday% 16 Current day of month (DD) return datetime.now().strftime('%d') elif svname == SysVars[7]: #%sysmonth% 3 Current month (MM) return datetime.now().strftime('%m') elif svname == SysVars[8]: #%sysyear% 2018 4 digits (YYYY) return datetime.now().strftime('%Y') elif svname == SysVars[9]: #%sysyears% 18 2 digits (YY) return datetime.now().strftime('%y') elif svname == SysVars[ 10]: #%sysweekday% 5 Weekday (integer) - 1, 2, 3... (1=Sunday, 2=Monday etc.) return str(int(datetime.now().strftime('%w')) + 1) elif svname == SysVars[ 11]: #%sysweekday_s% Fri Weekday (verbose) - Sun, Mon, Tue return datetime.now().strftime('%a') elif svname == SysVars[ 12]: #%unixtime% 1521731277 Unix time (seconds since epoch, 1970-01-01 00:00:00) return str(int(time.time())) elif svname == SysVars[13]: #%uptime% 3244 Uptime in minutes return str(rpieTime.getuptime(2)) elif svname == SysVars[14]: #%rssi% -45 WiFi signal strength (dBm) return str(OS.get_rssi()) elif svname == SysVars[15]: #%ip% 192.168.0.123 Current IP address return str(OS.get_ip()) elif svname == SysVars[16]: #%ip4% ipcim 4.byte res2 = str(OS.get_ip()) resarr = res2.split(".") if len(resarr) > 3: return resarr[3] elif svname == SysVars[17]: #%sysname% name return Settings.Settings["Name"] elif svname == SysVars[18]: #%unit% 32 Unit number return Settings.Settings["Unit"] elif svname == SysVars[19]: #%ssid% H4XX0R njietwork! wdev = False try: wdev = Settings.NetMan.getfirstwirelessdev() except: wdev = False if wdev: res = str(Network.get_ssid(wdev)) elif svname == SysVars[20]: #%mac% 00:14:22:01:23:45 MAC address pd = -1 try: pd = Settings.NetMan.getprimarydevice() except: pd = -1 if pd < 0 and len(Settings.NetworkDevices) > 0: pd = 0 if pd != "" and pd >= 0: return Settings.NetworkDevices[pd].mac elif svname == SysVars[ 21]: #%mac_int% 2212667 MAC address in integer to be used in rules (only the last 24 bit) pd = -1 try: pd = Settings.NetMan.getprimarydevice() except: pd = -1 if pd < 0 and len(Settings.NetworkDevices) > 0: pd = 0 if pd >= 0: try: res2 = Settings.NetworkDevices[pd].mac resarr = res2.split(":") if len(resarr) > 5: res = str( int("0x" + resarr[3] + resarr[4] + resarr[5], 16)) except: res = "" return res
def getglobalvar(varname): global SysVars, suntimesupported svname = varname.strip().lower() par = "" if ("-" in svname): resarr = svname.split("-") svname = resarr[0] par = "-" + resarr[1] if ("+" in svname): resarr = svname.split("+") svname = resarr[0] par = "+" + resarr[1] res = "" if svname in SysVars: if svname == SysVars[0]: #%systime% 01:23:54 return datetime.now().strftime('%H:%M:%S') elif svname == SysVars[1]: #%systm_hm% 01:23 return datetime.now().strftime('%H:%M') elif svname == SysVars[2]: #%lcltime% 2018-03-16 01:23:54 return datetime.now().strftime('%Y-%m-%d %H:%M:%S') elif svname == SysVars[3]: #%syshour% 11 Current hour (hh) return int(datetime.now().strftime('%H')) elif svname == SysVars[4]: #%sysmin% 22 Current minute (mm) return int(datetime.now().strftime('%M')) elif svname == SysVars[5]: #%syssec% 33 Current second (ss) return int(datetime.now().strftime('%S')) elif svname == SysVars[6]: #%sysday% 16 Current day of month (DD) return int(datetime.now().strftime('%d')) elif svname == SysVars[7]: #%sysmonth% 3 Current month (MM) return int(datetime.now().strftime('%m')) elif svname == SysVars[8]: #%sysyear% 2018 4 digits (YYYY) return datetime.now().strftime('%Y') elif svname == SysVars[9]: #%sysyears% 18 2 digits (YY) return datetime.now().strftime('%y') elif svname == SysVars[ 10]: #%sysweekday% 5 Weekday (integer) - 1, 2, 3... (1=Sunday, 2=Monday etc.) return str(int(datetime.now().strftime('%w')) + 1) elif svname == SysVars[ 11]: #%sysweekday_s% Fri Weekday (verbose) - Sun, Mon, Tue return datetime.now().strftime('%a') elif svname == SysVars[ 12]: #%unixtime% 1521731277 Unix time (seconds since epoch, 1970-01-01 00:00:00) return str(int(time.time())) elif svname == SysVars[13]: #%uptime% 3244 Uptime in minutes return str(rpieTime.getuptime(2)) elif svname == SysVars[14]: #%rssi% -45 WiFi signal strength (dBm) return str(OS.get_rssi()) elif svname == SysVars[15]: #%ip% 192.168.0.123 Current IP address return str(OS.get_ip()) elif svname == SysVars[16]: #%ip4% ipcim 4.byte res2 = str(OS.get_ip()) resarr = res2.split(".") if len(resarr) > 3: return resarr[3] elif svname == SysVars[17]: #%sysname% name return Settings.Settings["Name"] elif svname == SysVars[18]: #%unit% 32 Unit number return Settings.Settings["Unit"] elif svname == SysVars[19]: #%ssid% H4XX0R njietwork! wdev = False try: wdev = Settings.NetMan.getfirstwirelessdev() except: wdev = False if wdev: res = str(Network.get_ssid(wdev)) elif svname == SysVars[20]: #%mac% 00:14:22:01:23:45 MAC address pd = -1 try: pd = Settings.NetMan.getprimarydevice() except: pd = -1 if pd < 0 and len(Settings.NetworkDevices) > 0: pd = 0 if pd != "" and pd >= 0: return Settings.NetworkDevices[pd].mac elif svname == SysVars[ 21]: #%mac_int% 2212667 MAC address in integer to be used in rules (only the last 24 bit) pd = -1 try: pd = Settings.NetMan.getprimarydevice() except: pd = -1 if pd < 0 and len(Settings.NetworkDevices) > 0: pd = 0 if pd >= 0: try: res2 = Settings.NetworkDevices[pd].mac resarr = res2.split(":") if len(resarr) > 5: res = str( int("0x" + resarr[3] + resarr[4] + resarr[5], 16)) except: res = "" return res elif svname == SysVars[22]: #%build% bstr = str(rpieGlobals.BUILD) return bstr elif svname == SysVars[23]: #sunrise try: from suntime import Sun suntimesupported = 1 except: suntimesupported = 0 if suntimesupported == 1: try: sun = Sun(Settings.AdvSettings["Latitude"], Settings.AdvSettings["Longitude"]) abd_sr = sun.get_local_sunrise_time(datetime.now()) if par != "": abd_sr = addtoTime(abd_sr, par) res = abd_sr.strftime('%H:%M') except Exception as e: res = "00:00" return res elif svname == SysVars[24]: #sunset try: from suntime import Sun suntimesupported = 1 except: suntimesupported = 0 if suntimesupported == 1: try: sun = Sun(Settings.AdvSettings["Latitude"], Settings.AdvSettings["Longitude"]) abd_ss = sun.get_local_sunset_time(datetime.now()) if par != "": abd_ss = addtoTime(abd_ss, par) res = abd_ss.strftime('%H:%M') except Exception as e: res = "00:00" return res elif svname == SysVars[25]: #sun altitude try: from pytz import reference from pysolar.solar import get_altitude pysolarsupported = 1 except: pysolarsupported = 0 res = "0" if pysolarsupported == 1: try: localtime = reference.LocalTimezone() today = datetime.now(localtime) res = get_altitude(Settings.AdvSettings["Latitude"], Settings.AdvSettings["Longitude"], today) except Exception as e: print(e) res = "0" return res elif svname == SysVars[26]: #sun azimuth try: from pytz import reference from pysolar.solar import get_azimuth pysolarsupported = 1 except: pysolarsupported = 0 res = "0" if pysolarsupported == 1: try: localtime = reference.LocalTimezone() today = datetime.now(localtime) res = get_azimuth(Settings.AdvSettings["Latitude"], Settings.AdvSettings["Longitude"], today) except Exception as e: print(e) res = "0" return res elif svname == SysVars[27]: #sun radiation try: from pytz import reference from pysolar.solar import get_altitude from pysolar.radiation import get_radiation_direct pysolarsupported = 1 except: pysolarsupported = 0 res = "-1" if pysolarsupported == 1: try: localtime = reference.LocalTimezone() today = datetime.now(localtime) altitude_deg = get_altitude( Settings.AdvSettings["Latitude"], Settings.AdvSettings["Longitude"], today) res = get_radiation_direct(today, altitude_deg) except Exception as e: print(e) return res return res
def initprogram(): global timer100ms, timer20ms, timer1s, timer2s, timer30s, init_ok try: os.chdir(os.path.dirname(os.path.realpath(__file__))) except: pass print("Starting "+rpieGlobals.PROGNAME+" "+rpieGlobals.PROGVER) try: Settings.loadsettings() hardwareInit() PluginInit() CPluginInit() NPluginInit() RulesInit() signal.signal(signal.SIGINT, signal_handler) # avoid stoling by a plugin timer100ms = millis() timer20ms = timer100ms timer1s = timer100ms timer2s = timer100ms timer30s = timer100ms init_ok = True except Exception as e: print(e) init_ok = False t = threading.Thread(target=mainloop) # starting sensors and background functions t.daemon = True t.start() ports = [] try: ports = Settings.AdvSettings["portlist"] except: ports = [80,8080,8008,591] # replace default ports if len(ports)<1: ports = [80,8080,8008,591] # replace default ports up = 0 ownaddr = '0.0.0.0' errors = "" for p in ports: up = p try: serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serversocket.bind((ownaddr, up)) serversocket.close() except Exception as e: up = 0 errors = errors + str(e) if up>0: break if up == 0: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Webserver can not be started, no available port found! "+str(errors)) else: try: import os_os as OS ipa = OS.get_ip() except: ipa = "localhost" misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"Webserver starting at http://"+str(ipa)+":"+str(up)) Settings.WebUIPort = up webserver.WebServer.start(ownaddr, up) # starts webserver GUI