module Data.Meteo.Swiss.Types
(
SmnRecord (..)
, SmnStation(..)
) where
import Data.Aeson
import qualified Data.Text as T
import Data.Time
import Data.Default
import Data.Aeson.Types (Parser)
import Data.String (IsString)
import Control.Monad (mzero)
data SmnRecord = SmnRecord
{ smnStation :: Maybe SmnStation
, smnCode :: T.Text
, smnDateTime :: Maybe UTCTime
, smnTemperature :: T.Text
, smnSunshine :: T.Text
, smnPrecipitation :: T.Text
, smnWindDirection :: T.Text
, smnWindSpeed :: T.Text
, smnQnhPressure :: T.Text
, smnGustPeak :: T.Text
, smnHumidity :: T.Text
, smnQfePressure :: T.Text
, smnQffPressure :: T.Text
}
deriving Show
instance FromJSON SmnRecord where
parseJSON (Object v) = SmnRecord
<$> v .:? "station"
<*> v .:?! "code"
<*> v .:? "dateTime"
<*> v .:?! "temperature"
<*> v .:?! "sunshine"
<*> v .:?! "precipitation"
<*> v .:?! "windDirection"
<*> v .:?! "windSpeed"
<*> v .:?! "qnhPressure"
<*> v .:?! "gustPeak"
<*> v .:?! "humidity"
<*> v .:?! "qfePressure"
<*> v .:?! "qffPressure"
parseJSON _ = mzero
instance Default SmnRecord where
def = SmnRecord
{ smnStation = def
, smnCode = ""
, smnDateTime = Nothing
, smnTemperature = ""
, smnSunshine = ""
, smnPrecipitation = ""
, smnWindDirection = ""
, smnWindSpeed = ""
, smnQnhPressure = ""
, smnGustPeak = ""
, smnHumidity = ""
, smnQfePressure = ""
, smnQffPressure = ""
}
data SmnStation = SmnStation
{ staCode :: T.Text
, staName :: T.Text
, staCh1903Y :: Maybe Int
, staCh1903X :: Maybe Int
, staLat :: Maybe Double
, staLng :: Maybe Double
, staElevation :: Maybe Int
}
deriving Show
instance FromJSON SmnStation where
parseJSON (Object v) = SmnStation
<$> v .:?! "code"
<*> v .:?! "name"
<*> v .:? "ch1903Y"
<*> v .:? "ch1903X"
<*> v .:? "lat"
<*> v .:? "lng"
<*> v .:? "elevation"
parseJSON _ = mzero
instance Default SmnStation where
def = SmnStation
{ staCode = ""
, staName = ""
, staCh1903Y = Nothing
, staCh1903X = Nothing
, staLat = Nothing
, staLng = Nothing
, staElevation = Nothing
}
(.:?!) :: (Data.String.IsString a, FromJSON a) => Object -> T.Text -> Parser a
(.:?!) v k = v .: k .!= ""