diff options
Diffstat (limited to '')
| -rw-r--r-- | lib/OwnTracks/Configuration.hs | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/lib/OwnTracks/Configuration.hs b/lib/OwnTracks/Configuration.hs new file mode 100644 index 0000000..5880c69 --- /dev/null +++ b/lib/OwnTracks/Configuration.hs @@ -0,0 +1,175 @@ +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE DerivingVia #-} +{-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE TemplateHaskell #-} + + +module OwnTracks.Configuration +-- | https://owntracks.org/booklet/tech/json/ + (Configuration(..), LocatorPriority(..), MonitoringMode(..)) where + +import Data.Aeson +import qualified Data.Aeson.TH as TH +import Data.Aeson.Types (Parser) +import Data.ByteString (ByteString) +import Data.ByteString.Base64 +import Data.Char (toLower) +import Data.Data (Proxy (..)) +import Data.Functor ((<&>)) +import Data.Text (Text) +import qualified Data.Text as T +import Data.Text.Encoding (encodeUtf8) +import Data.Time (UTCTime, defaultTimeLocale, parseTimeM) +import GHC.Generics (Generic) + +import OwnTracks.Location (MonitoringMode) +import OwnTracks.Waypoint (Waypoint) + + +data LocatorPriority = + NoPower + -- ^ best accuracy possible with zero additional power consumption (Android) + | LowPower + -- ^ city level accuracy (Android) + | BalancedPower + -- ^ block level accuracy based on Wifi/Cell (Android) + | HighPower + -- ^ most accurate accuracy based on GPS (Android) + deriving (Show, Eq, Enum) + +instance FromJSON LocatorPriority where + parseJSON = fmap toEnum . parseJSON + +instance ToJSON LocatorPriority where + toJSON = toJSON . fromEnum + +data ProtocolMode = Mqtt | Http + deriving (Show, Eq) + +instance FromJSON ProtocolMode where + parseJSON (Number 0) = pure Mqtt + parseJSON (Number 3) = pure Http + parseJSON _ = fail "mode must be 0 (mqtt) or 3 (http)" + +instance ToJSON ProtocolMode where + toJSON Mqtt = Number 0 + toJSON Http = Number 3 + +data Configuration = Configuration + { configAdapt :: Maybe Int + -- ^ time in minutes of non-movement before switching from move to significant mode. 0 (zero) means disabled. Defaults to 0 (zero) (iOS/integer/minutes/optional) + , configAllowRemoteLocation :: Bool + -- ^ Respond to reportLocation cmd message (iOS/boolean) + , configAllowInvalidCerts :: Bool + -- ^ disable TLS certificate checks insecure (iOS/boolean) + , configAuth :: Bool + -- ^ Use username and password for endpoint authentication (iOS,Android/boolean) + , configAutostartOnBoot :: Bool + -- ^ Autostart the app on device boot (Android/boolean) + , configCleanSession :: Bool + -- ^ MQTT endpoint clean session (iOS,Android/boolean) + , configClientId :: Maybe Text + -- ^ client id to use for MQTT connect. Defaults to "user deviceId" (iOS,Android/string) + , configClientpkcs :: Maybe Text + -- ^ Name of the client pkcs12 file (iOS/string) + , configCmd :: Bool + -- ^ Respond to cmd messages (iOS,Android/boolean) + , configConnectionTimeoutSeconds :: Maybe Int + -- ^ (default 30) TCP timeout for establishing a connection to the MQTT / HTTP broker, (Android/int) + , configDay :: Maybe Int + -- ^ Number of days to keep locations stored locally. 0 means no local keeping of locations. A negative number indicates to use the positions value. Defaults to -1 for backward compatibility. (iOS/integer/days) + , configDebugLog :: Maybe Bool + -- ^ (default false) whether or not debug logs should be shown in the log viewer / exporter activity (Android/bool) + , configDeviceId :: Text + -- ^ id of the device used for pubTopicBase and clientId construction. Defaults to the os name of the device (iOS,Android/string) + , configDowngrade :: Maybe Int + -- ^ battery level below which to downgrade monitoring from move mode (iOS/integer/percent/optional) + , configEcryptionKey :: Maybe Text + -- ^ the secret key used for payload encryption (iOS,Android/string) + , configExtendedData :: Bool + -- ^ Add extended data attributes to location messages (iOS,Android/boolean) + , configHost :: Text + -- ^ MQTT endpoint host (iOS,Android/string) + , configHttpHeaders :: Maybe Text + -- ^ extra HTTP headers:field names and field content are separated by a colon (:), multiple fields by a backslash-n (\n) \<field-name>:\<field-content>\n\<field-name>:\<field-content>... (iOS only/string) + , configIgnoreInaccurateLocations :: Int + -- ^ Location accuracy below which reports are supressed. 0 means no locations are suppressed. (iOS,Android/integer/meters) + , configIgnoreStaleLocations :: Bool + -- ^ Number of days after which location updates are assumed stale. Locations sent by friends older than the number of days specified here will not be shown on map or in friends list. Defaults to 0, which means stale locations are not filtered. (iOS,Android/integer/days) + , configKeepalive :: Int + -- ^ MQTT endpoint keepalive (iOS,Android/integer/seconds) + , configLocatorDisplacement :: Int + -- ^ maximum distance between location source updates (iOS,Android/integer/meters) + , configLocatorInterval :: Int + -- ^ maximum interval between location source updates (iOS,Android/integer/seconds) + , configLocatorPriority :: LocatorPriority + -- ^ source/power setting for location updates (Android/integer) + , configLocked :: Maybe Bool + -- ^ Locks settings screen on device for editing (iOS/boolean) + , configMaxHistory :: Maybe Int + -- ^ Number of notifications to store historically. Zero (0) means no notifications are stored and history tab is hidden. Defaults to zero. (iOS/integer) + , configMode :: ProtocolMode + -- ^ Endpoint protocol mode (iOS,Android/integer) + , configMonitoring :: MonitoringMode + -- ^ Location reporting mode (iOS,Android/integer) + , configProtocolLevel :: Int + -- ^ MQTT broker protocol level (iOS,Android/integer) + , configNotificationLocation :: Bool + -- ^ Show last reported location in ongoing notification (Android/boolean) + , configOpencageApiKey :: Maybe Text + -- ^ API key for alternate Geocoding provider. See OpenCage for details. (Android/string) + , configOsmTemplate :: Maybe Text + -- ^ URL template for alternate tile provider. Defaults to https://tile.openstreetmap.org/{z}/{x}/{y}.png. (iOS/string) + , configOsmCopyright :: Maybe Text + -- ^ Attribution text shown with OSM map. Defaults to (c) OpenStreetMap contributors. (iOS/string) + , configPassphrase :: Maybe Text + -- ^ Passphrase of the client pkcs12 file (iOS/string) + , configPassword :: Maybe Text + -- ^ Endpoint password (iOS,Android/string) + , configPegLocatorFastestIntervalToInterval :: Bool + -- ^ (default false) - if true, requests that that the device provide locations no faster than the specified interval. Location providers often use the requested interval as a "at least every" setting, and may return locations more frequencly. Some people wanted the behaviour where it also meant "no more frequently than", so this setting lets them specify this (Android/bool) + , configPing :: Maybe Int + -- ^ Interval in which location messages of with t:p are reported (Android/integer) + , configPort :: Int + -- ^ MQTT endpoint port (iOS,Android/integer) + , configPositions :: Maybe Int + -- ^ Number of locations to keep for friends and own device and display (iOS/integer) + , configPubTopicBase :: Maybe Text + -- ^ MQTT topic base to which the app publishes; %u is replaced by the user name, %d by device (iOS,Android/string) + , configPubRetain :: Bool + -- ^ MQTT retain flag for reported messages (iOS,Android/boolean) + , configPubQos :: Int + -- ^ MQTT QoS level for reported messages (iOS,Android/integer) + , configRanging :: Maybe Bool + -- ^ Beacon ranging (iOS/boolean) + , configRemoteConfiguration :: Maybe Bool + -- ^ Allow remote configuration by sending a setConfiguration cmd message (Android/boolean) + , configSub :: Bool + -- ^ subscribe to subTopic via MQTT (iOS,Android/boolean) + , configSubTopic :: Text + -- ^ A whitespace separated list of MQTT topics to which the app subscribes if sub is true (defaults see topics) (iOS,Android/string) + , configSubQos :: Bool + -- ^ (iOS,Android/boolean) + , configTid :: Text + -- ^ Two digit Tracker ID used to display short name and default face of a user (iOS,Android/string) + , configTls :: Bool + -- ^ MQTT endpoint TLS connection (iOS,Android/boolean) + , configTlsClientCrtPassword :: Maybe Text + -- ^ Passphrase of the client pkcs12 file (Android/string) + , configUrl :: Text + -- ^ HTTP endpoint URL to which messages are POSTed (iOS,Android/string) + , configUsername :: Text + -- ^ Endpoint username (iOS,Android/string) + , configWs :: Bool + -- ^ use MQTT over Websocket, default false (iOS,Android/boolean) + , configWaypoints :: [Waypoint] + -- ^ Array of waypoint messages (iOS,Android/array) + } deriving (Show, Eq, Generic) + + +TH.deriveJSON (TH.defaultOptions + { TH.omitNothingFields = True + , TH.rejectUnknownFields = False + , TH.fieldLabelModifier = drop 5 . \(x:xs) -> toLower x : xs + }) 'Configuration |
