improve weather decoding

This commit is contained in:
Jakob Ketterl 2019-08-27 11:32:50 +02:00
parent 4409a369fa
commit 1a2f6b4970

View File

@ -12,6 +12,7 @@ logger = logging.getLogger(__name__)
knotsToKilometers = 1.852 knotsToKilometers = 1.852
feetToMeters = 0.3048 feetToMeters = 0.3048
milesToKilometers = 1.609344 milesToKilometers = 1.609344
inchesToMilimeters = 25.4
# not sure what the correct encoding is. it seems TAPR has set utf-8 as a standard, but not everybody is following it. # not sure what the correct encoding is. it seems TAPR has set utf-8 as a standard, but not everybody is following it.
encoding = "utf-8" encoding = "utf-8"
@ -63,7 +64,7 @@ class WeatherMapping(object):
self.scale = scale self.scale = scale
def matches(self, input): def matches(self, input):
return self.char == input[0] and len(input) > self.length + 1 return self.char == input[0] and len(input) > self.length
def updateWeather(self, weather, input): def updateWeather(self, weather, input):
def deepApply(obj, key, v): def deepApply(obj, key, v):
@ -74,10 +75,13 @@ class WeatherMapping(object):
deepApply(obj[keys[0]], ".".join(keys[1:]), v) deepApply(obj[keys[0]], ".".join(keys[1:]), v)
else: else:
obj[key] = v obj[key] = v
try:
value = int(input[1:1 + self.length]) value = int(input[1:1 + self.length])
if self.scale: if self.scale:
value = self.scale(value) value = self.scale(value)
deepApply(weather, self.key, value) deepApply(weather, self.key, value)
except ValueError:
pass
remain = input[1 + self.length:] remain = input[1 + self.length:]
return weather, remain return weather, remain
@ -88,9 +92,9 @@ class WeatherParser(object):
WeatherMapping("s", "wind.speed", 3, lambda x: x * milesToKilometers), WeatherMapping("s", "wind.speed", 3, lambda x: x * milesToKilometers),
WeatherMapping("g", "wind.gust", 3, lambda x: x * milesToKilometers), WeatherMapping("g", "wind.gust", 3, lambda x: x * milesToKilometers),
WeatherMapping("t", "temperature", 3), WeatherMapping("t", "temperature", 3),
WeatherMapping("r", "rain.hour", 3, lambda x: x / 100 * 25.4), WeatherMapping("r", "rain.hour", 3, lambda x: x / 100 * inchesToMilimeters),
WeatherMapping("p", "rain.day", 3, lambda x: x / 100 * 25.4), WeatherMapping("p", "rain.day", 3, lambda x: x / 100 * inchesToMilimeters),
WeatherMapping("P", "rain.sincemidnight", 3, lambda x: x / 100 * 25.4), WeatherMapping("P", "rain.sincemidnight", 3, lambda x: x / 100 * inchesToMilimeters),
WeatherMapping("h", "humidity", 2), WeatherMapping("h", "humidity", 2),
WeatherMapping("b", "barometricpressure", 5, lambda x: x/10), WeatherMapping("b", "barometricpressure", 5, lambda x: x/10),
WeatherMapping("s", "snowfall", 3, lambda x: x * 25.4), WeatherMapping("s", "snowfall", 3, lambda x: x * 25.4),
@ -107,6 +111,7 @@ class WeatherParser(object):
if mapping: if mapping:
(weather, remain) = mapping.updateWeather(weather, self.data) (weather, remain) = mapping.updateWeather(weather, self.data)
self.data = remain self.data = remain
doWork = len(self.data) > 0
else: else:
doWork = False doWork = False
return weather return weather