Beispiel #1
0
 def ShowScreen(self):
     self.PaintBase()
     r = config.fonts.Font(self.TitleFontSize, "", True, True).render("Console Maintenance", 0, wc(self.CharColor))
     rl = (config.screenwidth - r.get_width()) / 2
     config.screen.blit(r, (rl, config.topborder))
     r = config.fonts.Font(self.SubFontSize, "", True, True).render(
         "Up: " + interval_str(time.time() - config.starttime), 0, wc(self.CharColor)
     )
     rl = (config.screenwidth - r.get_width()) / 2
     config.screen.blit(r, (rl, config.topborder + self.TitleFontSize))
     self.PaintKeys()
     pygame.display.update()
Beispiel #2
0
 def ShowScreen(self):
     self.PaintBase()
     r = config.fonts.Font(self.TitleFontSize, '', True,
                           True).render("Console Maintenance", 0,
                                        wc(self.CharColor))
     rl = (config.screenwidth - r.get_width()) / 2
     config.screen.blit(r, (rl, config.topborder))
     r = config.fonts.Font(self.SubFontSize, '', True, True).render(
         "Up: " + interval_str(time.time() - config.starttime), 0,
         wc(self.CharColor))
     rl = (config.screenwidth - r.get_width()) / 2
     config.screen.blit(r, (rl, config.topborder + self.TitleFontSize))
     self.PaintKeys()
     pygame.display.update()
    def FetchWeather(self):
        progress = 0
        if time.time() > self.lastwebreq + 5 * 60:
            try:
                # refresh the conditions - don't do more than once per 5 minutes
                self.lastwebreq = time.time(
                )  # do this first so that even in error case we wait a while to try again
                f = urllib2.urlopen(self.url)
                val = f.read()
                if val.find("keynotfound") <> -1:
                    if self.location <> "":
                        # only report once in log
                        config.Logs.Log("Bad weatherunderground key:" +
                                        self.location,
                                        severity=logsupport.ConsoleError)
                    self.location = ""
                    self.lastwebreq = 0
                    return -1
                progress = 1
                parsed_json = json.loads(val)
                js = functools.partial(TreeDict, parsed_json)
                fcsts = TreeDict(parsed_json, 'forecast', 'simpleforecast',
                                 'forecastday')
                f.close()
                progress = 2
                self.ConditionVals = {}
                self.ForecastVals = []
                self.ConditionErr = []
                self.ForecastErr = []
                for cond, desc in WeatherInfo.ConditionMap.iteritems():
                    try:
                        self.ConditionVals[cond] = desc[0](js(*desc[1]))
                        progress = (4, cond)
                    except:
                        self.ConditionVals[cond] = desc[0]('0')
                        self.ConditionErr.append(cond)
                for i, fcst in enumerate(fcsts):
                    self.ForecastVals.append({})
                    self.ForecastErr.append([])
                    fs = functools.partial(TreeDict, fcst)
                    progress = (5, i)
                    for fc, desc in WeatherInfo.ForecastDay.iteritems():
                        try:
                            self.ForecastVals[i][fc] = desc[0](fs(*desc[1]))
                        except:
                            print "W2", i, fc
                            config.Logs.Log("Forecast error: ",
                                            i,
                                            fc,
                                            fs(*desc[1]),
                                            severity=logsupport.ConsoleError)
                            self.ForecastVals[i][fc] = desc[0]('0')
                            self.ForecastErr[i].append(fc)
                """
				Create synthetic fields and fix error cases
				"""
                # Moonrise/set
                if 'MoonriseH' not in self.ConditionErr and 'MoonriseM' not in self.ConditionErr:
                    # t1 = [self.ConditionVals[x] for x in ('MoonriseH','MoonriseM')]
                    self.ConditionVals[
                        'Moonrise'] = "{d[0]:02d}:{d[1]:02d}".format(d=[
                            self.ConditionVals[x]
                            for x in ('MoonriseH', 'MoonriseM')
                        ])
                else:
                    self.ConditionVals['Moonrise'] = 'n/a'
                    if 'MoonriseH' in self.ConditionErr:
                        self.ConditionErr.remove('MoonriseH')
                    if 'MoonriseM' in self.ConditionErr:
                        self.ConditionErr.remove('MoonriseM')
                if 'MoonsetH' not in self.ConditionErr and 'MoonsetM' not in self.ConditionErr:
                    self.ConditionVals[
                        'Moonset'] = "{d[0]:02d}:{d[1]:02d}".format(d=[
                            self.ConditionVals[x]
                            for x in ('MoonsetH', 'MoonsetM')
                        ])
                else:
                    self.ConditionVals['Moonset'] = 'n/a'
                    if 'MoonsetH' in self.ConditionErr:
                        self.ConditionErr.remove('MoonsetH')
                    if 'MoonsetM' in self.ConditionErr:
                        self.ConditionErr.remove('MoonsetM')

                # Wind not reported at station
                if self.ConditionVals['WindMPH'] < 0:
                    self.ConditionVals['WindStr'] = 'n/a'
                else:
                    self.ConditionVals[
                        'WindStr'] = "{d[0]}@{d[1]} gusts {d[2]}".format(d=[
                            self.ConditionVals[x]
                            for x in ('WindDir', 'WindMPH', 'WindGust')
                        ])

                if self.ConditionErr:
                    config.Logs.Log("Weather error: ", self.ConditionErr,
                                    logsupport.ConsoleError)

            except:
                config.Logs.Log("Error retrieving weather",
                                severity=logsupport.ConsoleError)
                # print "Getting fresh weather failed ", time.time()
                # print "Progress: ", progress
                # print self.ConditionVals
                # print self.ForecastVals
                # print self.url
                self.lastwebreq = 0
                return -1
        self.ConditionVals['Age'] = utilities.interval_str(
            time.time() - self.ConditionVals['Time'])
        return self.lastwebreq
Beispiel #4
0
    def FetchWeather(self):
        progress = 0
        if time.time() > self.lastwebreq + 30 * 60:
            try:
                # print 'WU call',time.time(),self.lastwebreq
                # print self.url

                # refresh the conditions - don't do more than once per 30 minutes
                self.lastwebreq = time.time()  # do this first so that even in error case we wait a while to try again
                try:
                    f = urllib2.urlopen(self.url, None, 15)  # wait at most 15 seconds for weather response then timeout
                    val = f.read()
                except:
                    # todo - can report actual error codes in case not a timeout?
                    config.Logs.Log(
                        "Error fetching weather: " + str(sys.exc_info()[0]), severity=logsupport.ConsoleWarning
                    )
                    return -1
                if val.find("keynotfound") <> -1:
                    if self.location <> "":
                        # only report once in log
                        config.Logs.Log("Bad weatherunderground key:" + self.location, severity=logsupport.ConsoleError)
                    self.location = ""
                    self.lastwebreq = 0
                    return -1
                progress = 1
                parsed_json = json.loads(val)
                js = functools.partial(TreeDict, parsed_json)
                fcsts = TreeDict(parsed_json, "forecast", "simpleforecast", "forecastday")
                f.close()
                progress = 2
                self.ConditionVals = {}
                self.ForecastVals = []
                self.ConditionErr = []
                self.ForecastErr = []
                for cond, desc in WeatherInfo.ConditionMap.iteritems():
                    try:
                        self.ConditionVals[cond] = desc[0](js(*desc[1]))
                        if desc[0] == str:
                            self.ConditionVals[cond] = self.TryShorten(self.ConditionVals[cond])
                        progress = (4, cond)
                    except:
                        self.ConditionVals[cond] = desc[0]("0")
                        self.ConditionErr.append(cond)
                for i, fcst in enumerate(fcsts):
                    self.ForecastVals.append({})
                    self.ForecastErr.append([])
                    fs = functools.partial(TreeDict, fcst)
                    progress = (5, i)
                    for fc, desc in WeatherInfo.ForecastDay.iteritems():
                        try:
                            self.ForecastVals[i][fc] = desc[0](fs(*desc[1]))
                            if desc[0] == str:
                                self.ForecastVals[i][fc] = self.TryShorten(self.ForecastVals[i][fc])
                        except:
                            print "W2", i, fc
                            config.Logs.Log("Forecast error: ", i, fc, fs(*desc[1]), severity=logsupport.ConsoleError)
                            self.ForecastVals[i][fc] = desc[0]("0")
                            self.ForecastErr[i].append(fc)
                """
				Create synthetic fields and fix error cases
				"""
                # Moonrise/set
                if "MoonriseH" not in self.ConditionErr and "MoonriseM" not in self.ConditionErr:
                    # t1 = [self.ConditionVals[x] for x in ('MoonriseH','MoonriseM')]
                    self.ConditionVals["Moonrise"] = "{d[0]:02d}:{d[1]:02d}".format(
                        d=[self.ConditionVals[x] for x in ("MoonriseH", "MoonriseM")]
                    )
                else:
                    self.ConditionVals["Moonrise"] = "n/a"
                    if "MoonriseH" in self.ConditionErr:
                        self.ConditionErr.remove("MoonriseH")
                    if "MoonriseM" in self.ConditionErr:
                        self.ConditionErr.remove("MoonriseM")
                if "MoonsetH" not in self.ConditionErr and "MoonsetM" not in self.ConditionErr:
                    self.ConditionVals["Moonset"] = "{d[0]:02d}:{d[1]:02d}".format(
                        d=[self.ConditionVals[x] for x in ("MoonsetH", "MoonsetM")]
                    )
                else:
                    self.ConditionVals["Moonset"] = "n/a"
                    if "MoonsetH" in self.ConditionErr:
                        self.ConditionErr.remove("MoonsetH")
                    if "MoonsetM" in self.ConditionErr:
                        self.ConditionErr.remove("MoonsetM")

                        # Wind not reported at station
                if self.ConditionVals["WindMPH"] < 0:
                    self.ConditionVals["WindStr"] = "n/a"
                else:
                    self.ConditionVals["WindStr"] = "{d[0]}@{d[1]} gusts {d[2]}".format(
                        d=[self.ConditionVals[x] for x in ("WindDir", "WindMPH", "WindGust")]
                    )

                if self.ConditionErr:
                    config.Logs.Log("Weather error: ", self.ConditionErr, logsupport.ConsoleError)

            except:
                config.Logs.Log("Error retrieving weather", severity=logsupport.ConsoleError)
                # print "Getting fresh weather failed ", time.time()
                # print "Progress: ", progress
                # print self.ConditionVals
                # print self.ForecastVals
                # print self.url
                self.lastwebreq = 0
                return -1
        self.ConditionVals["Age"] = utilities.interval_str(time.time() - self.ConditionVals["Time"])
        return self.lastwebreq