示例#1
0
 def p026_get_value(self, ptype):
     value = 0
     if ptype == 1:
         value = misc.getuptime(0)
     elif ptype == 2:
         value = esp_os.FreeMem()
     elif ptype == 3:
         value = unet.get_rssi()
     elif ptype == 4:
         value = esp_os.read_cpu_temp()
     return value
示例#2
0
def getglobalvar(varname):
    global SysVars, suntimesupported, GlobalVars
    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
            ret = utime.localtime()
            return '{:02}:{:02}:{:02}'.format(ret[3], ret[4], ret[5])
        elif svname == SysVars[1]:  #%systm_hm% 	01:23
            ret = utime.localtime()
            return '{:02}:{:02}'.format(ret[3], ret[4])
        elif svname == SysVars[2]:  #%lcltime% 	2018-03-16 01:23:54
            ret = utime.localtime()
            return '{:04}-{:02}-{:02} {:02}:{:02}:{:02}'.format(
                ret[0], ret[1], ret[2], ret[3], ret[4], ret[5])
        elif svname == SysVars[3]:  #%syshour% 	11 	Current hour (hh)
            ret = utime.localtime()
            return '{:02}'.format(ret[3])
        elif svname == SysVars[4]:  #%sysmin% 	22 	Current minute (mm)
            ret = utime.localtime()
            return '{:02}'.format(ret[4])
        elif svname == SysVars[5]:  #%syssec% 	33 	Current second (ss)
            ret = utime.localtime()
            return '{:02}'.format(ret[5])
        elif svname == SysVars[6]:  #%sysday% 	16 	Current day of month (DD)
            ret = utime.localtime()
            return '{:02}'.format(ret[2])
        elif svname == SysVars[7]:  #%sysmonth% 	3 	Current month (MM)
            ret = utime.localtime()
            return '{:02}'.format(ret[1])
        elif svname == SysVars[8]:  #%sysyear% 	2018 	4 digits (YYYY)
            ret = utime.localtime()
            return '{:04}'.format(ret[0])
        elif svname == SysVars[9]:  #%sysyears% 	18 	2 digits (YY)
            ret = utime.localtime()
            ret2 = '{:04}'.format(ret[0])
            return ret2[2:]
        elif svname == SysVars[
                10]:  #%sysweekday% 	5 	Weekday (integer) - 1, 2, 3... (1=Sunday, 2=Monday etc.)
            ret = utime.localtime()
            mday = ret[6]
            if mday == 6:
                mday = 1
            else:
                mday = mday + 2
            return mday
        elif svname == SysVars[
                11]:  #%sysweekday_s% 	Fri 	Weekday (verbose) - Sun, Mon, Tue
            days = ['Mon', 'Tue', "Wed", "Thu", "Fri", "Sat", "Sun"]
            ret = utime.localtime()
            return days[int(ret[6])]
        elif svname == SysVars[
                12]:  #%unixtime% 	1521731277 	Unix time (seconds since epoch, 1970-01-01 00:00:00)
            import time
            return str(int(time.time()))
        elif svname == SysVars[13]:  #%uptime% 	3244 	Uptime in minutes
            return str(misc.getuptime(2))
        elif svname == SysVars[14]:  #%rssi% 	-45 	WiFi signal strength (dBm)
            return str(unet.get_rssi())
        elif svname == SysVars[15]:  #%ip% 	192.168.0.123 	Current IP address
            return str(unet.get_ip())
        elif svname == SysVars[16]:  #%ip4% ipcim 4.byte
            res2 = str(unet.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%
            return str(unet.get_ssid())
        elif svname == SysVars[20]:  #%mac% 	00:14:22:01:23:45 	MAC address
            return str(unet.get_mac())
        elif svname == SysVars[
                21]:  #%mac_int% 	2212667 	MAC address in integer to be used in rules (only the last 24 bit)
            res = ""
            try:
                res2 = str(unet.get_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(pglobals.BUILD)
            return bstr
        elif svname == SysVars[23]:  #sunrise
            try:
                from inc.suntime.Suntime import Sun
                suntimesupported = 1
            except Exception as e:
                suntimesupported = 0
            if suntimesupported == 1:
                try:
                    tz = int(settings.AdvSettings["timezone"]) / 60.0
                except Exception as e:
                    tz = 0
                try:
                    sun = Sun(settings.AdvSettings["Latitude"],
                              settings.AdvSettings["Longitude"], tz)
                    abd_sr = sun.get_sunrise_time()
                    if par != "":
                        abd_sr = addtoTime(abd_sr, par)
                    res = '{:02}:{:02}'.format(int(abd_sr[3]), int(abd_sr[4]))
                except Exception as e:
                    res = "00:00"
                return res
        elif svname == SysVars[24]:  #sunset
            try:
                from inc.suntime.Suntime import Sun
                suntimesupported = 1
            except:
                suntimesupported = 0
            if suntimesupported == 1:
                try:
                    tz = int(settings.AdvSettings["timezone"]) / 60
                except:
                    tz = 0
                try:
                    sun = Sun(settings.AdvSettings["Latitude"],
                              settings.AdvSettings["Longitude"], tz)
                    abd_ss = sun.get_sunset_time()
                    if par != "":
                        abd_ss = addtoTime(abd_ss, par)
                    res = '{:02}:{:02}'.format(int(abd_ss[3]), int(abd_ss[4]))
                except Exception as e:
                    res = "00:00"
                return res

        elif svname == SysVars[25]:  #sun altitude
            res = "0"
            return res

        elif svname == SysVars[26]:  #sun azimuth
            res = "0"
            return res

        elif svname == SysVars[27]:  #sun radiation
            res = "-1"
            return res

        elif svname == SysVars[28]:  #%br%
            return str("\r\n")

        elif svname == SysVars[29]:  #%lf%
            return str("\n")

        elif svname == SysVars[30]:  #%tab%
            return str("	")

        elif svname[0] == "v":
            try:
                sid = int(svname[1:]) - 1
            except Exception as e:
                sid = -1
            if sid >= 0 and sid < 16:
                return (str(GlobalVars[sid]))
    return res
示例#3
0
def handle_root(httpResponse, responsearr):
    ws.navMenuIndex = 0
    ws.TXBuffer = ""
    responsestr = ""
    try:
        redir = settings.AdvSettings["startpage"]
    except:
        redir = "/"
    if redir != "/":
        return httpResponse.WriteResponseRedirect(redir)
    httpResponse.WriteResponseOk(headers=({
        'Cache-Control': 'no-cache'
    }),
                                 contentType='text/html',
                                 contentCharset='UTF-8',
                                 content="")
    ws.sendHeadandTail("TmplStd", ws._HEAD)
    try:
        cmdline = ws.arg("cmd", responsearr).strip()
        if cmdline.startswith('reboot'):
            ws.sendHeadandTail("TmplStd", ws._TAIL)

        if len(cmdline) > 0:
            import commands
            responsestr = str(commands.doExecuteCommand(cmdline))
    except:
        pass

    if len(responsestr) > 0:
        ws.TXBuffer += "<P>{0}<p>".format(responsestr)

    ws.TXBuffer += "<form><table class='normal'><tr><TH style='width:150px;' align='left'>System Info<TH align='left'>Value<TR><TD>Unit:<TD>"
    ws.TXBuffer += str(settings.Settings["Unit"])
    ws.TXBuffer += "<TR><TD>Uptime:<TD>" + str(misc.getuptime(1))
    httpResponse._write(ws.TXBuffer, strEncoding='UTF-8')
    ws.TXBuffer = ""
    try:
        ws.TXBuffer += "<TR><TD>Free Mem:<TD>" + str(
            int(esp_os.get_memory()['f'] / 1024)) + " kB"
    except:
        pass
    ws.TXBuffer += "<TR><TD>IP:<TD>" + str(unet.get_ip())
    ws.TXBuffer += "<TR><TD>Wifi RSSI:<TD>" + str(unet.get_rssi())
    ws.TXBuffer += '<tr><td>Build<td>' + str(pglobals.PROGNAME) + " " + str(
        pglobals.PROGVER)
    ws.TXBuffer += "<TR><TD><TD>"
    ws.addButton("sysinfo", "More info")
    httpResponse._write(ws.TXBuffer, strEncoding='UTF-8')
    ws.TXBuffer = ""
    ws.TXBuffer += "</table><BR>"
    if len(settings.nodelist) > 0:
        ws.TXBuffer += "<BR><table class='multirow'><TR><TH>Node List<TH>Name<TH>Build<TH>Type<TH>IP<TH>Age"
        for n in settings.nodelist:
            ws.TXBuffer += "<TR><TD>Unit " + str(n["unitno"]) + "<TD>" + str(
                n["name"]) + "<TD>" + str(n["build"]) + "<TD>"
            ntype = ""
            if int(n["type"]) == pglobals.NODE_TYPE_ID_ESP_EASY_STD:
                ntype = "ESP Easy"
            elif int(n["type"]) == pglobals.NODE_TYPE_ID_ESP_EASYM_STD:
                ntype = "ESP Easy Mega"
            elif int(n["type"]) == pglobals.NODE_TYPE_ID_ESP_EASY32_STD:
                ntype = "ESP Easy32"
            elif int(n["type"]) == pglobals.NODE_TYPE_ID_ARDUINO_EASY_STD:
                ntype = "Arduino Easy"
            elif int(n["type"]) == pglobals.NODE_TYPE_ID_NANO_EASY_STD:
                ntype = "Nano Easy"
            elif int(n["type"]) == pglobals.NODE_TYPE_ID_RPI_EASY_STD:
                ntype = "RPI Easy"
            ws.TXBuffer += ntype + "<TD>"
            waddr = str(n["ip"])
            if str(n["port"]) != "" and str(n["port"]) != "0" and str(
                    n["port"]) != "80":
                waddr += ":" + str(n["port"])
            ws.addWideButton("http://" + waddr, waddr, "")
            ws.TXBuffer += "<TD>" + str(n["age"])
        ws.TXBuffer += "</table></form>"

    if len(settings.p2plist) > 0:
        try:
            ws.TXBuffer += "<BR><table class='multirow'><TR><TH>Protocol<TH>P2P node number<TH>Name<TH>Build<TH>Type<TH>MAC<TH>RSSI<TH>Last seen<TH>Capabilities"
            for n in settings.p2plist:
                hstr = str(n["protocol"])
                if hstr == "ESPNOW":
                    hstr = "<a href='espnow'>" + hstr + "</a>"
                ws.TXBuffer += "<TR><TD>" + hstr + "<TD>Unit " + str(
                    n["unitno"]) + "<TD>" + str(n["name"]) + "<TD>" + str(
                        n["build"]) + "<TD>"
                ntype = "Unknown"
                if int(n["type"]) == pglobals.NODE_TYPE_ID_ESP_EASY_STD:
                    ntype = "ESP Easy"
                elif int(n["type"]) == pglobals.NODE_TYPE_ID_ESP_EASYM_STD:
                    ntype = "ESP Easy Mega"
                elif int(n["type"]) == pglobals.NODE_TYPE_ID_ESP_EASY32_STD:
                    ntype = "ESP Easy32"
                elif int(n["type"]) == pglobals.NODE_TYPE_ID_ARDUINO_EASY_STD:
                    ntype = "Arduino Easy"
                elif int(n["type"]) == pglobals.NODE_TYPE_ID_NANO_EASY_STD:
                    ntype = "Nano Easy"
                elif int(n["type"]) == pglobals.NODE_TYPE_ID_RPI_EASY_STD:
                    ntype = "RPI Easy"
                elif int(n["type"]) == pglobals.NODE_TYPE_ID_ATMEGA_EASY_LORA:
                    ntype = "LoRa32u4"
                ws.TXBuffer += ntype
                ws.TXBuffer += "<TD>" + str(n["mac"])
                ws.TXBuffer += "<TD>" + str(n["lastrssi"])
                ldt = n["lastseen"]
                lstr = ""
                try:
                    lstr = '{:04}-{:02}-{:02} {:02}:{:02}:{:02}'.format(
                        ldt[0], ldt[1], ldt[2], ldt[3], ldt[4], ldt[5])
                except:
                    lstr = str(ldt)
                ws.TXBuffer += "<TD>" + lstr
                wm = int(n["cap"])
                wms = ""
                if (wm & 1) == 1:
                    wms = "SEND "
                if (wm & 2) == 2:
                    wms += "RECEIVE "
                ws.TXBuffer += "<TD>" + wms
            ws.TXBuffer += "</table></form>"
        except Exception as e:
            pass

    ws.sendHeadandTail("TmplStd", ws._TAIL)
    httpResponse._write(ws.TXBuffer, strEncoding='UTF-8')
    ws.TXBuffer = ""
示例#4
0
def handle_json(httpResponse, responsearr):
    ws.navMenuIndex = 7
    ws.TXBuffer = ""
    httpResponse.WriteResponseOk(headers=({
        'Cache-Control': 'no-cache'
    }),
                                 contentType='application/json',
                                 contentCharset='UTF-8',
                                 content="")

    tasknrstr = ws.arg("tasknr", responsearr).strip()
    showspectask = -1
    if (len(tasknrstr) > 0):
        try:
            showspectask = int(tasknrstr) - 1
        except:
            showspectask = -1
    showsystem = True
    showwifi = True
    showdataacq = True
    showtaskdetail = True
    view = ws.arg("view", responsearr)
    if (len(view) > 0):
        if view == "sensorupdate":
            showsystem = False
            showwifi = False
            showdataacq = False
            showtaskdetail = False
    ws.TXBuffer += "{"
    if showspectask == -1:
        if showsystem:
            ws.TXBuffer += '"System":{"Build":"'
            ws.TXBuffer += str(pglobals.PROGNAME) + " " + str(pglobals.PROGVER)
            try:
                uver = uos.uname()
                uver2 = 'uPython ' + str(uver.version)
                umach = str(uver.machine)
            except:
                uver2 = ""
                umach = ""

            ws.TXBuffer += '","System libraries":"Python '
            ws.TXBuffer += sys.version.replace('\n', '<br>') + " " + uver2
            ws.TXBuffer += '","Plugins":' + str(
                len(pglobals.deviceselector) - 1)
            ws.TXBuffer += ',"Local time":"' + misc.strtime(0)
            ws.TXBuffer += '","Unit":' + str(settings.Settings["Unit"])
            ws.TXBuffer += ',"Name":"' + str(settings.Settings["Name"])
            try:
                ws.TXBuffer += '","Uptime":' + str(misc.getuptime(2))
            except:
                ws.TXBuffer += '","Uptime":0'
            ws.TXBuffer += ',"Load":0'  #not supported
            try:
                ws.TXBuffer += ',"Free RAM":' + str(
                    int(esp_os.get_memory()['f']))
            except:
                ws.TXBuffer += ',"Free RAM":0'
            ws.TXBuffer += "},"
        if showwifi:
            ws.TXBuffer += '"WiFi":{'
            ws.TXBuffer += '"IP config":"DHCP'  #not supported
            ws.TXBuffer += '","IP":"' + unet.get_ip()
            ws.TXBuffer += '","Subnet Mask":"255.255.255.0'  #not supported
            ws.TXBuffer += '","Gateway IP":"'
            ws.TXBuffer += '","MAC address":"' + unet.get_mac()
            ws.TXBuffer += '","SSID":"' + str(unet.get_ssid()) + '"'
            ws.TXBuffer += ',"RSSI":' + str(unet.get_rssi())
            ws.TXBuffer += "},"
        senstart = 0
        senstop = len(settings.Tasks)
    else:
        senstart = showspectask
        senstop = senstart + 1
    ws.TXBuffer += '"Sensors":['
    ttl = 120
    for sc in range(senstart, senstop):
        httpResponse._write(ws.TXBuffer, strEncoding='UTF-8')
        ws.TXBuffer = ""
        if settings.Tasks[sc] != False:
            ws.TXBuffer += '{"TaskValues": ['
            for tv in range(0, settings.Tasks[sc].valuecount):
                ws.TXBuffer += '{"ValueNumber":' + str(
                    tv + 1) + ',"Name":"' + str(
                        settings.Tasks[sc].valuenames[tv]) + '",'
                ws.TXBuffer += '"NrDecimals":' + str(
                    settings.Tasks[sc].decimals[tv]) + ','
                ws.TXBuffer += '"Value":'
                if str(settings.Tasks[sc].uservar[tv]) == "":
                    ws.TXBuffer += '""'
                else:
                    if str(settings.Tasks[sc].decimals[tv]) == "-1":
                        ival = '"' + str(settings.Tasks[sc].uservar[tv]) + '"'
                    else:
                        try:
                            ival = float(settings.Tasks[sc].uservar[tv])
                        except:
                            ival = '"' + str(
                                settings.Tasks[sc].uservar[tv]) + '"'
                    ws.TXBuffer += str(ival)
                ws.TXBuffer += '},'
            if ws.TXBuffer[len(ws.TXBuffer) - 1] == ",":
                ws.TXBuffer = ws.TXBuffer[:-1]
            ws.TXBuffer += '],'
            ws.TXBuffer += '"DataAcquisition": ['
            for ca in range(pglobals.CONTROLLER_MAX):
                ws.TXBuffer += '{"Controller":' + str(
                    ca + 1) + ',"IDX":' + str(
                        settings.Tasks[sc].controlleridx[ca]
                    ) + ',"Enabled":"' + str(
                        settings.Tasks[sc].senddataenabled[ca]) + '"},'
            if ws.TXBuffer[len(ws.TXBuffer) - 1] == ",":
                ws.TXBuffer = ws.TXBuffer[:-1]
            ws.TXBuffer += '],'
            ws.TXBuffer += '"TaskInterval":' + str(
                settings.Tasks[sc].interval) + ','
            ws.TXBuffer += '"Type":"' + str(
                settings.Tasks[sc].getdevicename()) + '",'
            ws.TXBuffer += '"TaskName":"' + str(
                settings.Tasks[sc].gettaskname()) + '",'
            ws.TXBuffer += '"TaskEnabled":"' + str(
                settings.Tasks[sc].enabled) + '",'
            ws.TXBuffer += '"TaskNumber":' + str(sc + 1) + '},'
            if (settings.Tasks[sc].interval <
                    ttl) and (settings.Tasks[sc].interval > 0):
                ttl = settings.Tasks[sc].interval
    ws.TXBuffer += '],'
    ws.TXBuffer += '"TTL":' + str(ttl * 1000) + '}'

    httpResponse._write(ws.TXBuffer, strEncoding='UTF-8')
示例#5
0
def handle_sysinfo(httpResponse):
    ws.navMenuIndex = 7
    ws.TXBuffer = ""
    httpResponse.WriteResponseOk(headers=({
        'Cache-Control': 'no-cache'
    }),
                                 contentType='text/html',
                                 contentCharset='UTF-8',
                                 content="")
    ws.sendHeadandTail("TmplStd", ws._HEAD)
    ws.TXBuffer += "<table class='normal'><TR><TH style='width:150px;' align='left'>System Info<TH align='left'>"
    ws.TXBuffer += "<TR><TD>Unit<TD>"
    ws.TXBuffer += str(settings.Settings["Unit"])
    ws.TXBuffer += "<TR><TD>Local Time<TD>" + misc.strtime(
        0)  #Local Time: (2000, 1, 1, 0, 45, 20, 5, 1)
    ws.TXBuffer += "<TR><TD>Uptime<TD>" + str(misc.getuptime(1))
    httpResponse._write(ws.TXBuffer, strEncoding='UTF-8')
    ws.TXBuffer = ""
    ws.TXBuffer += "<TR><TD>Frequency<TD>" + str(
        int(esp_os.getfreq() / 1000000)) + " Mhz"
    try:
        ws.TXBuffer += "<TR><TD>Flash<TD>" + str(
            int(esp_os.getstorage() / 1024)) + " kB"
    except:
        pass
    try:
        ws.TXBuffer += "<TR><TD>Memory<TD>" + str(
            int(esp_os.get_memory()['t'] / 1024)) + " kB"
    except:
        pass
    try:
        ws.TXBuffer += "<TR><TD>Free Mem<TD>" + str(
            int(esp_os.get_memory()['f'] / 1024)) + " kB"
    except:
        pass
    ws.TXBuffer += "<TR><TD>PSRAM<TD>" + str(esp_os.psRamFound())
    httpResponse._write(ws.TXBuffer, strEncoding='UTF-8')
    ws.TXBuffer = ""
    try:
        uver = uos.uname()
        uver2 = 'uPython ' + str(uver.version)
        umach = str(uver.machine)
    except:
        uver2 = ""
        umach = ""
    if umach != "":
        ws.TXBuffer += "<TR><TD>Device<TD>" + str(umach)
    ws.TXBuffer += '<tr><td>Platform<td>' + sys.platform
    ws.TXBuffer += "<TR><TD>Wifi RSSI<TD>" + str(unet.get_rssi())
    ws.TXBuffer += '<tr><td>SSID<td>' + str(unet.get_ssid())
    ws.TXBuffer += '<tr><td>STA IP<td>' + str(unet.get_ip("STA"))
    ws.TXBuffer += '<tr><td>STA MAC<td>' + str(unet.get_mac("STA"))
    ws.TXBuffer += '<tr><td>AP IP<td>' + str(unet.get_ip("AP"))
    ws.TXBuffer += '<tr><td>AP MAC<td>' + str(unet.get_mac("AP"))
    ws.TXBuffer += '<tr><td>LAN IP<td>' + str(unet.get_ip("LAN"))
    ws.TXBuffer += '<tr><td>LAN MAC<td>' + str(unet.get_mac("LAN"))
    httpResponse._write(ws.TXBuffer, strEncoding='UTF-8')
    ws.TXBuffer = ""
    ws.TXBuffer += '<tr><td>Build<td>' + str(pglobals.PROGNAME) + " " + str(
        pglobals.PROGVER)
    ws.TXBuffer += '<tr><td>Libraries<td>Python ' + sys.version.replace(
        '\n', '<br>') + " " + uver2
    ws.TXBuffer += '<tr><td>Plugins<td>' + str(
        len(pglobals.deviceselector) - 1)
    ws.TXBuffer += "</tr></table>"
    try:
        import inc.lib_part as PartMgr
        pm = PartMgr.partmgr()
        pl = pm.getparts()
    except:
        pl = []
    if len(pl) > 0:
        try:
            pr = pm.getrunningpart()[4]
            pb = pm.getbootpart()[4]
        except:
            pr = ""
            pb = ""
        ws.TXBuffer += "<p><table class='normal'><tr><th>Partition</th><th>Start address</th><th>Size</th><th>Flags</th></tr>"
        for p in pl:
            prf = ""
            if pr == p[4]:
                prf = "Running "
            if pb == p[4]:
                prf += "Boot "
            ws.TXBuffer += "<tr><td>{0}</td><td>0x{1:07x}</td><td>{2}</td><td>{3}</td></tr>".format(
                p[4], p[2], p[3], prf)
        ws.TXBuffer += "</table>"
    ws.sendHeadandTail("TmplStd", ws._TAIL)
    httpResponse._write(ws.TXBuffer, strEncoding='UTF-8')
    ws.TXBuffer = ""