From e90c9c0850e4bbee6ceeba05d93b5671cb150e7f Mon Sep 17 00:00:00 2001 From: Jens Petersen Date: Wed, 12 Sep 2007 09:09:49 +0200 Subject: Parse the temperature separately in Celcius and Fahrenheit. Parse the pressure in hPa. Generalize showWithColors to (Num a, Ord a) so it works for both Int's and Float's. Use an Int for temperature, pressure, and relative humidity. darcs-hash:20070912070949-740ef-0e79d57888c1519e1324d06b17fe2fa0d3d0fb2d.gz --- Plugins/Monitors/Common.hs | 2 +- Plugins/Monitors/Weather.hs | 42 +++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/Plugins/Monitors/Common.hs b/Plugins/Monitors/Common.hs index 5177fb8..7887e14 100644 --- a/Plugins/Monitors/Common.hs +++ b/Plugins/Monitors/Common.hs @@ -265,7 +265,7 @@ setColor str s = Just c -> return $ "" ++ str ++ "" -showWithColors :: (Float -> String) -> Float -> Monitor String +showWithColors :: (Num a, Ord a) => (a -> String) -> a -> Monitor String showWithColors f x = do h <- getConfigValue high l <- getConfigValue low diff --git a/Plugins/Monitors/Weather.hs b/Plugins/Monitors/Weather.hs index 447ac2c..cc3a58b 100644 --- a/Plugins/Monitors/Weather.hs +++ b/Plugins/Monitors/Weather.hs @@ -39,7 +39,7 @@ weatherConfig = mkMConfig , "tempF" , "dewPoint" , "rh" - ,"pressure" + , "pressure" ] data WeatherInfo = @@ -52,10 +52,11 @@ data WeatherInfo = , wind :: String , visibility :: String , skyCondition :: String - , temperature :: Float + , tempC :: Int + , tempF :: Int , dewPoint :: String - , humidity :: Float - , pressure :: String + , humidity :: Int + , pressure :: Int } deriving (Show) pTime :: Parser (String, String, String, String) @@ -69,16 +70,23 @@ pTime = do y <- getNumbersAsString char ' ' return (y, m, d ,([h]++[hh]++":"++[mi]++mimi)) -pTemp :: Parser Float -pTemp = do manyTill anyChar $ char '(' - s <- manyTill digit $ (char ' ' <|> char '.') +pTemp :: Parser (Int, Int) +pTemp = do f <- manyTill digit $ char ' ' + manyTill anyChar $ char '(' + c <- manyTill digit $ (char ' ' <|> char '.') skipRestOfLine - return $read s + return $ (read c, read f) -pRh :: Parser Float +pRh :: Parser Int pRh = do s <- manyTill digit $ (char '%' <|> char '.') return $ read s +pPressure :: Parser Int +pPressure = do manyTill anyChar $ char '(' + s <- manyTill digit $ char ' ' + skipRestOfLine + return $ read s + parseData :: Parser [WeatherInfo] parseData = do st <- getAllBut "," @@ -90,13 +98,14 @@ parseData = v <- getAfterString "Visibility: " sk <- getAfterString "Sky conditions: " skipTillString "Temperature: " - temp <- pTemp + (tC,tF) <- pTemp dp <- getAfterString "Dew Point: " skipTillString "Relative Humidity: " rh <- pRh - p <- getAfterString "Pressure (altimeter): " + skipTillString "Pressure (altimeter): " + p <- pPressure manyTill skipRestOfLine eof - return $ [WI st ss y m d h w v sk temp dp rh p] + return $ [WI st ss y m d h w v sk tC tF dp rh p] defUrl :: String defUrl = "http://weather.noaa.gov/pub/data/observations/metar/decoded/" @@ -115,11 +124,10 @@ getData url= return "Could not retrieve data" formatWeather :: [WeatherInfo] -> Monitor String -formatWeather [(WI st ss y m d h w v sk temp dp r p)] = - do cel <- showWithColors show temp - far <- showWithColors (showDigits 1) (((9 / 5) * temp) + 32) - rh <- showWithColors show r - parseTemplate [st, ss, y, m, d, h, w, v, sk, cel, far, dp, rh , p ] +formatWeather [(WI st ss y m d h w v sk tC tF dp r p)] = + do cel <- showWithColors show tC + far <- showWithColors show tF + parseTemplate [st, ss, y, m, d, h, w, v, sk, cel, far, dp, show r , show p ] formatWeather _ = return "N/A" runWeather :: [String] -> Monitor String -- cgit v1.2.3