aboutsummaryrefslogtreecommitdiff
path: root/lib/OwnTracks/Waypoint.hs
blob: 002baa0dba6e0a80bd02bfa8f5a359b2b2bc12b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
    ]