diff options
| author | stuebinm | 2026-04-16 00:09:44 +0200 |
|---|---|---|
| committer | stuebinm | 2026-04-16 01:18:19 +0200 |
| commit | fbfa662922a2dcf34e8a2cf1eb020210de18c0af (patch) | |
| tree | 5acc0ee9a3e0bd290da9330bf2f46e6ea68bda23 /lib/OwnTracks/Waypoint.hs | |
| parent | 2f7666c1a8d6b06718f58e1327d2e235c0d2d98d (diff) | |
Owntracks.{Configuration,Command,Waypoint}: init
Diffstat (limited to '')
| -rw-r--r-- | lib/OwnTracks/Waypoint.hs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/OwnTracks/Waypoint.hs b/lib/OwnTracks/Waypoint.hs new file mode 100644 index 0000000..002baa0 --- /dev/null +++ b/lib/OwnTracks/Waypoint.hs @@ -0,0 +1,69 @@ +{-# 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 + ] |
