summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Fabian Kirchner <kirchner@posteo.de>2024-08-13 11:20:10 +0200
committerLibravatar Fabian Kirchner <kirchner@posteo.de>2024-08-13 11:20:10 +0200
commit3b1384e8c1bfb199f87bf56e3f0ec0a062178b68 (patch)
treea8c0dc57af91a7af3d805b5b8c864e98d0b20d53
parent1083d34c8563d3ce200f2f363ce36b19bf792f6f (diff)
feat: show weather temperatures
-rw-r--r--app/Sensor.hs58
1 files changed, 48 insertions, 10 deletions
diff --git a/app/Sensor.hs b/app/Sensor.hs
index e5fd50f..edd41c3 100644
--- a/app/Sensor.hs
+++ b/app/Sensor.hs
@@ -2,6 +2,7 @@
{-# LANGUAGE Arrows #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DeriveAnyClass #-}
+{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
@@ -382,14 +383,16 @@ data ForecastTimeserie = ForecastTimeserie
deriving (Generic)
data ForecastData = ForecastData
- { next_6_hours :: Maybe Next6Hours
+ { next_6_hours :: Maybe Next6Hours,
+ instant :: Instant
}
deriving (Generic)
instance FromJSON ForecastData
data Next6Hours = Next6Hours
- { summary :: Summary
+ { summary :: Summary,
+ details :: Details
}
deriving (Generic)
@@ -402,14 +405,37 @@ data Summary = Summary
instance FromJSON Summary
+data Details = Details
+ { air_temperature_max :: Float,
+ air_temperature_min :: Float,
+ precipitation_amount :: Float
+ }
+ deriving (Generic)
+
+instance FromJSON Details
+
+data Instant = Instant
+ { details :: InstantDetails
+ }
+ deriving (Generic)
+
+instance FromJSON Instant
+
+data InstantDetails = InstantDetails
+ { air_temperature :: Float
+ }
+ deriving (Generic)
+
+instance FromJSON InstantDetails
+
instance FromJSON ForecastTimeserie where
parseJSON value =
fmap ForecastTimeserie (withObject "ForecastTimeserie" (\v -> v .: fromString "data") value)
-instance (S.MonadSensor m) => S.Aggregate m WeatherForecast (Maybe String) where
+instance (S.MonadSensor m) => S.Aggregate m WeatherForecast ForecastData where
aggregate (WeatherForecast location) = forever do
forecast <- liftIO (getForecast location)
- S.yield (fmap ((.symbol_code) . (.summary)) (head forecast.properties.timeseries).data_.next_6_hours)
+ S.yield (head forecast.properties.timeseries).data_
threadDelay ((5 * 60) * 10 ^ 6)
getForecast :: LocationData -> IO Forecast
@@ -419,13 +445,25 @@ getForecast LocationData {..} = do
where
addUserAgent req = addRequestHeader (fromString "user-agent") (fromString "astatusbar") req
-weatherForecast :: (S.MonadSensor m) => S.Sensor m () (Maybe String)
+weatherForecast :: (S.MonadSensor m) => S.Sensor m () P.Doc
weatherForecast = do
- value <- S.sensor . WeatherForecast =<< location
- return
- ( case value of
- Just "partlycloudy_night" -> Just "\xe37e"
- _ -> value
+ forecastData <- S.sensor . WeatherForecast =<< location
+ let airTemperature = P.pretty (show forecastData.instant.details.air_temperature)
+ pure
+ ( case forecastData.next_6_hours of
+ Nothing ->
+ airTemperature
+ Just (Next6Hours {summary, details}) ->
+ mconcat $ intersperse (P.pretty " ") [symbol, tempMax, tempMin, airTemperature]
+ where
+ symbol =
+ P.pretty
+ ( case summary.symbol_code of
+ "partlycloudy_night" -> "\xe37e"
+ _ -> summary.symbol_code
+ )
+ tempMax = P.color P.Red (P.pretty (show details.air_temperature_max))
+ tempMin = P.color P.Green (P.pretty (show details.air_temperature_min))
)
data Location = Location deriving (Show)