{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE DerivingVia #-} module OwnTracks.Waypoint -- | https://owntracks.org/booklet/tech/json/ (Waypoint(..)) where import Data.Aeson import Data.Aeson.Types (Parser) import Data.ByteString (ByteString) import Data.ByteString.Base64 import Data.Functor ((<&>)) import Data.Text (Text) import qualified Data.Text as T import Data.Text.Encoding (encodeUtf8) import Data.Time (UTCTime, defaultTimeLocale, formatTime, parseTimeM) import Database.Persist import GHC.Generics (Generic) data Waypoint = Waypoint { waypointDescription :: Text -- ^ Name of the waypoint that is included in the sent transition message, copied into the location message inregions array when a current position is within a region. (iOS,Android,string/required) , waypointLatitude :: Maybe Double -- ^ Latitude (iOS,Android/float/degree/optional) , waypointLongitude :: Maybe Double -- ^ Longitude (iOS,Android/float/degree/optional) , waypointRadius :: Maybe Int -- ^ Radius around the latitude and longitude coordinates (iOS,Android/integer/meters/optional) , waypointTimestamp :: UTCTime -- ^ Timestamp of creation of region, copied into the wtst element of the transition message (iOS,Android/integer/epoch/required) , waypointUUID :: Maybe Text -- ^ UUID of the BLE Beacon (iOS/string/optional) , waypointBLEMajor :: Maybe Int -- ^ Major number of the BLE Beacon (iOS/integer/optional) , waypointBLEMinor :: Maybe Int -- ^ Minor number of the BLE Beacon_(iOS/integer/optional)_ , waypointRegionId :: Maybe Text -- ^ region ID, created automatically, copied into the location payload inrids array (iOS/string) } deriving (Show, Eq, Generic) instance FromJSON Waypoint where parseJSON (Object o) = Waypoint <$> o .: "desc" <*> o .:? "lat" <*> o .:? "lon" <*> o .:? "rad" <*> (o .: "tst" >>= parseUnixTime) <*> o .:? "uuid" <*> o .:? "major" <*> o .:? "minor" <*> o .:? "rid" where parseUnixTime :: Int -> Parser UTCTime parseUnixTime = parseTimeM False defaultTimeLocale "%s" . show instance ToJSON Waypoint where toJSON Waypoint{..} = object [ "desc" .= waypointDescription , "lat" .= waypointLatitude , "lon" .= waypointLongitude , "rad" .= waypointRadius , "tst" .= formatTime defaultTimeLocale "%s" waypointTimestamp , "uuid" .= waypointUUID , "major" .= waypointBLEMajor , "minor" .= waypointBLEMinor , "rid" .= waypointRegionId ]