コード例 #1
0
    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)
コード例 #2
0
    def run(self, parserId=None, forceRunParser=False, forceRunMixer=False):
        currentTimestamp = rmCurrentTimestamp()
        forceRunParser = True

        if not forceRunParser and self.__lastRunningTimestamp is not None and (
                currentTimestamp -
                self.__lastRunningTimestamp) < self.__runningInterval:
            # We want to run the parser only each N minutes. This condition is not met, try later.
            log.debug(
                "Parser %r not run lastRunning timestamp %s current %s" %
                (parserId, self.__lastRunningTimestamp, currentTimestamp))
            return None, None

        self.__lastRunningTimestamp = currentTimestamp

        newValuesAvailable = False
        newForecast = RMForecastInfo(None, currentTimestamp)

        log.debug("*** BEGIN Running parsers: %d (%s)" %
                  (newForecast.timestamp,
                   rmTimestampToDateAsString(newForecast.timestamp)))
        for parserConfig in self.parsers:
            if parserId is not None and parserId != parserConfig.dbID:
                continue

            log.debug("   * Parser: %s -> %s" %
                      (parserConfig, parserConfig.runtimeLastForecastInfo))
            if parserConfig.enabled:
                if parserConfig.failCounter >= self.__maxFails:
                    if forceRunParser or parserConfig.lastFailTimestamp is None or (
                            abs(newForecast.timestamp -
                                parserConfig.lastFailTimestamp) >=
                            self.__delayAfterMaxFails):
                        parserConfig.failCounter = 0
                        parserConfig.lastFailTimestamp = None
                    else:
                        if parserConfig.failCounter == self.__maxFails:
                            log.warning(
                                "     * Parser: %s - ignored because of lack of data (failCounter=%s, lastFail=%s)!"
                                % (parserConfig, ` parserConfig.failCounter `,
                                   rmTimestampToDateAsString(
                                       parserConfig.lastFailTimestamp)))
                            parserConfig.failCounter += 1  # Increment this to get rid of the above message.
                        continue
                elif parserConfig.failCounter > 0:
                    retryDelay = min(
                        self.__minDelayBetweenFails +
                        (parserConfig.failCounter - 1) *
                        self.__stepDelayBetweenFails,
                        self.__maxDelayBetweenFails)
                    nextRetryTimestamp = parserConfig.lastFailTimestamp + retryDelay
                    if newForecast.timestamp < nextRetryTimestamp:
                        log.debug(
                            "     * Ignored because retry delay %d (sec) was not reached"
                            % retryDelay)
                        continue
                    log.debug("     * Parser retry after previous fail")

                parser = self.parsers[parserConfig]

                lastUpdate = None
                if parserConfig.runtimeLastForecastInfo:
                    # Check if parser hasn't run with an invalid future date
                    if parserConfig.runtimeLastForecastInfo.timestamp <= currentTimestamp:
                        lastUpdate = parserConfig.runtimeLastForecastInfo.timestamp

                # Save the newest parser run
                if lastUpdate is not None and lastUpdate > self.__lastUpdateTimestamp:
                    self.__lastUpdateTimestamp = lastUpdate

                if not forceRunParser and not self.forceParsersRun and (
                        lastUpdate != None and
                    (newForecast.timestamp - lastUpdate) <
                        parser.parserInterval):
                    log.debug(
                        "     * Ignored because interval %d not expired for timestamp %d lastUpdate: %d"
                        % (parser.parserInterval, newForecast.timestamp,
                           lastUpdate))
                    continue

                log.debug("  * Running parser %s with interval %d" %
                          (parser.parserName, parser.parserInterval))
                parser.settings = globalSettings.getSettings()
                parser.runtime[
                    RMParser.RuntimeDayTimestamp] = rmCurrentDayTimestamp()

                try:
                    parser.lastKnownError = ''
                    parser.isRunning = True
                    parser.perform()
                    parser.isRunning = False
                except Exception, e:
                    log.error("  * Cannot execute parser %s" %
                              parser.parserName)
                    log.exception(e)
                    parser.isRunning = False
                    if len(parser.lastKnownError) == 0:
                        parser.lastKnownError = 'Error: Failed to run'

                if not parser.hasValues():
                    parserConfig.failCounter += 1
                    parserConfig.lastFailTimestamp = newForecast.timestamp
                    if len(parser.lastKnownError) == 0:
                        parser.lastKnownError = 'Error: parser returned no values'
                    parser.isRunning = False
                    if parserConfig.failCounter == 1:
                        log.warn("  * Parser %s returned no values" %
                                 parser.parserName)
                    continue

                parserConfig.failCounter = 0
                parserConfig.lastFailTimestamp = None

                if newForecast.id == None:
                    self.forecastTable.addRecordEx(newForecast)
                parserConfig.runtimeLastForecastInfo = newForecast

                if not globalSettings.vibration:
                    self.parserDataTable.removeEntriesWithParserIdAndTimestamp(
                        parserConfig.dbID, parser.getValues())

                self.parserDataTable.addRecords(newForecast.id,
                                                parserConfig.dbID,
                                                parser.getValues())
                parser.clearValues()

                newValuesAvailable = True
コード例 #3
0
    def run(self, parserId = None, forceRunParser = False, forceRunMixer = False):
        currentTimestamp = rmCurrentTimestamp()
        forceRunParser = True

        if not forceRunParser and self.__lastRunningTimestamp is not None and (currentTimestamp - self.__lastRunningTimestamp) < self.__runningInterval:
            # We want to run the parser only each N minutes. This condition is not met, try later.
            log.debug("Parser %r not run lastRunning timestamp %s current %s" % (parserId, self.__lastRunningTimestamp, currentTimestamp))
            return None, None

        self.__lastRunningTimestamp = currentTimestamp

        newValuesAvailable = False
        newForecast = RMForecastInfo(None, currentTimestamp)

        log.debug("*** BEGIN Running parsers: %d (%s)" % (newForecast.timestamp, rmTimestampToDateAsString(newForecast.timestamp)))
        for parserConfig in self.parsers:
            if parserId is not None and parserId != parserConfig.dbID:
                continue

            log.debug("   * Parser: %s -> %s" % (parserConfig, parserConfig.runtimeLastForecastInfo))
            if parserConfig.enabled:
                if parserConfig.failCounter >= self.__maxFails:
                    if forceRunParser or parserConfig.lastFailTimestamp is None or (abs(newForecast.timestamp - parserConfig.lastFailTimestamp) >= self.__delayAfterMaxFails):
                        parserConfig.failCounter = 0
                        parserConfig.lastFailTimestamp = None
                    else:
                        if parserConfig.failCounter == self.__maxFails:
                            log.warning("     * Parser: %s - ignored because of lack of data (failCounter=%s, lastFail=%s)!" %
                                        (parserConfig, `parserConfig.failCounter`, rmTimestampToDateAsString(parserConfig.lastFailTimestamp)))
                            parserConfig.failCounter += 1 # Increment this to get rid of the above message.
                        continue
                elif parserConfig.failCounter > 0:
                    retryDelay = min(self.__minDelayBetweenFails + (parserConfig.failCounter - 1) * self.__stepDelayBetweenFails, self.__maxDelayBetweenFails)
                    nextRetryTimestamp = parserConfig.lastFailTimestamp + retryDelay
                    if newForecast.timestamp < nextRetryTimestamp:
                        log.debug("     * Ignored because retry delay %d (sec) was not reached" % retryDelay)
                        continue
                    log.debug("     * Parser retry after previous fail")

                parser = self.parsers[parserConfig]

                lastUpdate = None
                if parserConfig.runtimeLastForecastInfo:
                    # Check if parser hasn't run with an invalid future date
                    if parserConfig.runtimeLastForecastInfo.timestamp <= currentTimestamp:
                        lastUpdate = parserConfig.runtimeLastForecastInfo.timestamp

                # Save the newest parser run
                if lastUpdate is not None and lastUpdate > self.__lastUpdateTimestamp:
                    self.__lastUpdateTimestamp = lastUpdate

                if not forceRunParser and not self.forceParsersRun and (lastUpdate != None and (newForecast.timestamp - lastUpdate) < parser.parserInterval):
                    log.debug("     * Ignored because interval %d not expired for timestamp %d lastUpdate: %d" % (parser.parserInterval, newForecast.timestamp, lastUpdate))
                    continue

                log.debug("  * Running parser %s with interval %d" % (parser.parserName, parser.parserInterval))
                parser.settings = globalSettings.getSettings()
                parser.runtime[RMParser.RuntimeDayTimestamp] = rmCurrentDayTimestamp()

                try:
                    parser.lastKnownError = ''
                    parser.isRunning = True
                    parser.perform()
                    parser.isRunning = False
                except Exception, e:
                    log.error("  * Cannot execute parser %s" % parser.parserName)
                    log.exception(e)
                    parser.isRunning = False
                    if len(parser.lastKnownError) == 0:
                        parser.lastKnownError = 'Error: Failed to run'

                if not parser.hasValues():
                    parserConfig.failCounter += 1
                    parserConfig.lastFailTimestamp = newForecast.timestamp
                    if len(parser.lastKnownError) == 0:
                        parser.lastKnownError = 'Error: parser returned no values'
                    parser.isRunning = False
                    if parserConfig.failCounter == 1:
                        log.warn ("  * Parser %s returned no values" % parser.parserName)
                    continue


                parserConfig.failCounter = 0
                parserConfig.lastFailTimestamp = None

                if newForecast.id == None:
                    self.forecastTable.addRecordEx(newForecast)
                parserConfig.runtimeLastForecastInfo = newForecast

                if not globalSettings.vibration:
                    self.parserDataTable.removeEntriesWithParserIdAndTimestamp(parserConfig.dbID, parser.getValues())

                self.parserDataTable.addRecords(newForecast.id, parserConfig.dbID, parser.getValues())
                parser.clearValues()

                newValuesAvailable = True