def checkMachine(self, checkNetwork = True, checkLocation = True, checkWeather = True, checkTime = True):
        if checkNetwork:
            self.__diagNetwork()
            self.__diagInternet()

        if checkLocation:
            self.__diagLocation()

        if checkWeather:
            self.__diagWeather()

        if checkTime:
            self.__diagTime()

        self.__diagWifi()
        self.__diagCloud()

        self.__cpuUsage = RMCPUStats().getPercentage()
        self.__uptimeSeconds = self.getUptime()
        self.__uptimeString = str(timedelta(seconds = self.__uptimeSeconds))
        self.__memoryUsage = RMMemoryUsageStats().getFromProc()['rss']

        self.lastCheck = rmCurrentTimestamp()

        return self.__networkStatus, self.__locationStatus, self.__weatherStatus, self.__timeStatus
    def __init__(self, id, timestamp = None, processed = False):
        self.id = id
        self.timestamp = timestamp
        self.processed = processed

        if self.timestamp is None:
            self.timestamp = rmCurrentTimestamp()
    def __deleteExpiredTokens(self):
        timestamp = rmCurrentTimestamp()
        self.__tokensTable.deleteExpiredRecords(timestamp)

        if self.__tokens:
            for key, token in self.__tokens.iteritems():
                if token.expiration < timestamp:
                    self.__tokens.pop(key)
    def perform(self):
        # downloading data from a URL convenience function since other python libraries can be used
        URL = "http://lab.zanek.net/mesonet/api/currentobservations"
        stationID = "ALTU"
        data = self.openURL(URL)


        if data is None:
            return

        stationsData = json.loads(data.read())

        if stationsData is None:
            self.lastKnownError = "Error: Invalid response from server"
            return

        timestamp = rmCurrentTimestamp()

        for station in stationsData:
            if station['STID'] == stationID:
                try:
                    rain = self.__toFloat(station.get("RAIN"))
                except:
                    rain = None

                try:
                    tmin = self.__toFloat(station.get("TMIN"))
                    tmax = self.__toFloat(station.get("TMAX"))
                except:
                    self.lastKnownError = "Error: No minimum or maximum temperature can be retrieved"
                    return

                try:
                    pressure = self.__toFloat(station.get("PRES"))
                except:
                    pressure = None

                try:
                    wind = self.__toFloat(station.get("WSPD"))
                except:
                    wind = None

                try:
                    dew = self.__toFloat(station.get("TDEW"))
                except:
                    dew = None

                self.addValue(RMParser.dataType.MINTEMP, timestamp, tmin)
                self.addValue(RMParser.dataType.MAXTEMP, timestamp, tmax)
                self.addValue(RMParser.dataType.RAIN, timestamp, rain)
                self.addValue(RMParser.dataType.PRESSURE, timestamp, pressure)
                self.addValue(RMParser.dataType.WIND, timestamp, wind)
                self.addValue(RMParser.dataType.DEWPOINT, timestamp, dew)

                if self.parserDebug:
                    log.debug(self.result)
    def perform(self):
        URL = self.params.get("urlPath", None)
        d = self.openURL(URL)
        if d is None:
             return
        pwsContent = d.read()
        if pwsContent is None:
             return

        pwsContent = pwsContent.strip()
        pwsArray = pwsContent.split(" ")

        lat = float(pwsArray[160])
        lon = -float(pwsArray[161])

        distToPWS = self.distanceToStation(lat, lon)
        maxDist = self.params.get("maxAllowedDistance")
        if(distToPWS > maxDist):
             log.error("*** PWS Station too far from home!")
             return


        temperature = self.__toFloat(pwsArray[4])
        mintemp = self.__toFloat(pwsArray[47])
        maxtemp = self.__toFloat(pwsArray[46])
        rh = self.__toFloat(pwsArray[5])
        minrh = self.__toFloat(pwsArray[164])
        maxrh = self.__toFloat(pwsArray[163])
        wind = self.__toFloat(convertKnotsToMS(pwsArray[1]))  # converted from knos to m/s
        solarradiation = self.__toFloat(pwsArray[127])  # needs to be converted from watt/sqm*h to Joule/sqm

        if solarradiation is not None:
                    solarradiation *= 0.0864

        rain = self.__toFloat(pwsArray[7])
        dewpoint = self.__toFloat(pwsArray[72])
        pressure = self.__toFloat(pwsArray[50])
        conditionIcon = self.conditionConvert(self.__toFloat(pwsArray[48]))
        #skycover ?

        timestamp = rmCurrentTimestamp()
        self.addValue(RMParser.dataType.TEMPERATURE, timestamp, temperature)
        self.addValue(RMParser.dataType.MINTEMP, timestamp, mintemp)
        self.addValue(RMParser.dataType.MAXTEMP, timestamp, maxtemp)
        self.addValue(RMParser.dataType.RH, timestamp, rh)
        self.addValue(RMParser.dataType.MINRH, timestamp, minrh)
        self.addValue(RMParser.dataType.MAXRH, timestamp, maxrh)
        self.addValue(RMParser.dataType.WIND, timestamp, wind)
        self.addValue(RMParser.dataType.RAIN, timestamp, rain)
        self.addValue(RMParser.dataType.DEWPOINT, timestamp, dewpoint)
        self.addValue(RMParser.dataType.PRESSURE, timestamp, pressure)
        self.addValue(RMParser.dataType.CONDITION, timestamp, conditionIcon)
        self.addValue(RMParser.dataType.SOLARRADIATION, timestamp, solarradiation)

        print self.result
        return
    def load(self):
        if not self.__isLoaded:
            password = self.__settingsTable.getPassword()
            if password:
                self.__password = password

            self.__tokensTable.deleteExpiredRecords(rmCurrentTimestamp())

            self.__tokens = self.__tokensTable.getAllRecords(True)
            self.__isLoaded = True
    def __generateToken(self, longLiveToken = False):
        expirationTimeout = self.__tokenShortExpirationTimeout
        if longLiveToken:
            expirationTimeout = self.__tokenLongExpirationTimeout

        token = RMAuthToken(hashlib.sha224(str(uuid.uuid4())).hexdigest(), rmCurrentTimestamp() + expirationTimeout)
        self.__tokens[token.token] = token

        self.__tokensTable.addRecord(token.token, token.expiration)
        return token
    def __refreshWatchDog(self):
        timestamp = rmCurrentTimestamp()

        if self.__lastWatchdogTimestamp is None or (timestamp - self.__lastWatchdogTimestamp) >= self.__watchDogTimeout:
            if self.__watchDogDescriptor is None:
                try:
                    self.__watchDogDescriptor = open(self.__watchDogFile, 'w')
                    log.info("Opened system watchdog file %s with timeout %d" % (self.__watchDogFile, self.__watchDogTimeout))
                except Exception, e:
                    log.error(e)
            try:
                self.__watchDogDescriptor.write(`timestamp`)
                self.__watchDogDescriptor.flush()
                log.debug("PING Hardware Watchdog")
            except Exception, e:
                log.error(e)
    def __refreshWIFI(self):
        timestamp = rmCurrentTimestamp()
        lastWIFICheckTimestamp = globalWIFI.wifiInterface.lastWIFICheckTimestamp
        oldIP = globalWIFI.wifiInterface.ipAddress

        if lastWIFICheckTimestamp is None or oldIP is None or (timestamp - lastWIFICheckTimestamp) >= self.__wifiRefreshTimeout:
            try:
                globalWIFI.detect()

                if oldIP != globalWIFI.wifiInterface.ipAddress:
                    log.info("Refreshed WIFI Information. (old: %s new ip: %s)" % (`oldIP`, `globalWIFI.wifiInterface.ipAddress`))

                if RMOSPlatform().AUTODETECTED == RMOSPlatform.ANDROID:
                    return

                # Handle None IP
                if globalWIFI.wifiInterface.ipAddress is None:
                    if self.__lastNoneIpTimestamp is None or (timestamp - self.__lastNoneIpTimestamp) < self.__wifiNoneIpTimeout:
                        # First occurrence of None IP     OR    we can wait some more time.
                        if self.__lastNoneIpTimestamp is None:
                            self.__lastNoneIpTimestamp = timestamp
                        log.debug("Refreshed WIFI Information - no IP detected. Give it some more time: %d seconds!" % (self.__wifiNoneIpTimeout - (timestamp - self.__lastNoneIpTimestamp), ))
                        return
                    else:
                        globalWIFI.restart()
                        log.warn("Refreshed WIFI Information - WIFI quick reloaded because no IP detected. New IP is %s" % `globalWIFI.wifiInterface.ipAddress`)

                self.__lastNoneIpTimestamp = None # Reset None IP timestamp.

                # Check if we never connected to this AP, set back AP mode and restart app
                if globalWIFI.wifiInterface.mode == "managed" and not globalWIFI.hasConnectedOnce():
                    if globalWIFI.wifiInterface.hasClientLink:
                        globalWIFI.saveHasConnectedOnce(True)
                    else:
                        log.warning("WIFI Watcher Client IP (%s) configuration failed, restarting in AP mode." % oldIP)
                        globalWIFI.setDefaultAP()
                        globalWIFI.saveHasConnectedOnce(False)
                        globalWIFI.restart()
                        self.__mainManager.touchWakeMessage()

            except Exception, e:
                log.error(e)
    def __checkThreads(self):

        timestamp = rmCurrentTimestamp()

        # Work around for a system date change
        resetWatcher = not self.__lastCheckTimestamp is None and \
                       abs(timestamp - self.__lastCheckTimestamp) >= self.__systemTimeChangeThreshold

        everythingOk = True

        if resetWatcher:
            fromTime = rmTimestampToDateAsString(self.__lastCheckTimestamp) if self.__lastCheckTimestamp else None
            toTime = rmTimestampToDateAsString(timestamp)

            log.warning("System time has changed (from %s, to %s)! Resetting thread watcher!" % (fromTime, toTime))

            self.__lastWatchdogTimestamp = None
            for threadId in self.__data:
                details = self.__getThreadEntry(threadId)
                details[RMThreadWatcher.LastUpdateKey] = None

            if self.__mainManager:
                self.__mainManager.systemDateTimeChanged()
        else:

            for threadId in self.__data:
                details = self.__getThreadEntry(threadId)

                timeoutHint = details[RMThreadWatcher.TimeoutHintKey]
                lastUpdate = details[RMThreadWatcher.LastUpdateKey]

                if timeoutHint is None:
                    log.debug("Thread %d (%s) has no timeout hint!" % (threadId, details[RMThreadWatcher.NameHintKey]))
                elif lastUpdate is None:
                    log.debug("Thread %d (%s) is not started yet!" % (threadId, details[RMThreadWatcher.NameHintKey]))
                elif timeoutHint < (timestamp - lastUpdate):
                    everythingOk = False
                    log.debug("Thread %d (%s) didn't responde since [%s] (timeoutHint=%d, timeout=%d)!" % \
                              (threadId, details[RMThreadWatcher.NameHintKey], rmTimestampToDateAsString(lastUpdate), timeoutHint, (timestamp - lastUpdate)))

        self.__lastCheckTimestamp = timestamp
        return everythingOk
    def notifyCloud(code = None, action = None, parameter = None):
        if parameter is None:
            parameter = ''
        if action is None:
            action = ''

        if code == RMNotification.freezeTemperature:
            if RMNotification._freezeFlag is not action:
                RMNotification._freezeFlag = action
                string = code + "," + str(int(action)) + "," + str(parameter) + "\n"
            else:
                return
        elif code == RMNotification.rainDelay:
            isRainDelay = (globalSettings.restrictions.globalRestrictions.rainDelayDuration +
                globalSettings.restrictions.globalRestrictions.rainDelayStartTime) > rmCurrentTimestamp() \
                          and globalSettings.restrictions.globalRestrictions.rainDelayStartTime <= rmCurrentTimestamp()
            if RMNotification._rainDelayFlag != isRainDelay:
                RMNotification._rainDelayFlag = isRainDelay
                string = code + "," + str(int(isRainDelay)) + "\n"
            else:
                return
        elif code == RMNotification.rainSensor:
            if action != RMNotification._rainFlag:
                RMNotification._rainFlag = action
                string = code + "," + str(int(action)) + "\n"
            else:
                return
        elif code == RMNotification.cloudClient:
            string = code + "\n"
        else:
            if len(str(parameter)):
                string = str(code) + ',' + str(action) + "," + str(parameter) + "\n"
            else:
                string = str(code) + ',' + str(action) + "\n"

        RMNotification.__writeNotification(string)
 def __getUploadFolderName(self):
     return globalSettings.netName + "-SPK-" + str(globalSettings.hardwareVersion) + "-" \
            + globalWIFI.wifiInterface.macAddress + "-" + str(rmCurrentTimestamp())
 def updateThread(self):
     ### Call this method from the thread you want to update.
     with self.__lock:
         entry = self.__getThreadEntry(thread.get_ident(), True)
         entry[RMThreadWatcher.LastUpdateKey] = rmCurrentTimestamp()
 def expiresIn(self):
     if self.expiration:
         return self.expiration - rmCurrentTimestamp()
     return None