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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
module Server.Frontend.Tracker
(getTrackerViewR, getTrackersR, postTrackersR, postTrackerDeleteR,
postTrackerCommandR, postTrackerConfigR)
where
import Data.Aeson (Value, decode, encode)
import Data.ByteString (fromStrict, toStrict)
import Data.Coerce (coerce)
import Data.Function ((&))
import Data.Functor ((<&>))
import qualified Data.Map as M
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import Data.Time (getCurrentTime)
import qualified Data.UUID as UUID
import Database.Esqueleto.Experimental hiding ((<&>))
import Persist
import Server.Frontend.Routes (FrontendMessage (..), Handler,
Route (..), Widget)
import Yesod hiding (delete, update, (=.),
(==.))
import Fmt
import qualified OwnTracks
import OwnTracks.Command
import OwnTracks.Configuration (configHost)
import OwnTracks.Status
getTrackersR :: Handler Html
getTrackersR = do
trackers <- runDB $ select do
(t :& p) <- from $
(table @Tracker) `LeftOuterJoin` (table @Ping)
`on` \(t :& p) -> just (t ^. TrackerId) ==. p ?. PingTrackerId
pure (t, p)
& fmap associateJoin
createWidget <- trackerCreateWidget
defaultLayout [whamlet|
<h1> Trackers
<section>
<ul>
$forall (trackerId, (Tracker{..}, status)) <- M.toList trackers
<li><a href="@{TrackerViewR trackerName}">#{trackerName}</a>
<section>
^{createWidget}
|]
trackerCreateForm
:: Html
-> MForm Handler (FormResult Tracker, Widget)
trackerCreateForm = renderDivs $ Tracker
<$> areq textField (fieldSettingsLabel MsgTrackerName) Nothing
<*> pure False
<*> areq textField (fieldSettingsLabel MsgTrackerAgent) Nothing
<*> pure Nothing
<*> pure Nothing
trackerCreateWidget :: Handler Html
trackerCreateWidget = do
(widget, enctype) <- generateFormPost trackerCreateForm
defaultLayout [whamlet|
<h2> _{MsgCreateTracker}
<form method=post action="@{TrackersR}" enctype=#{enctype}>
^{widget}
<button>_{MsgSubmit}
|]
postTrackersR :: Handler Html
postTrackersR = do
((result, widget), enctype) <- runFormPost trackerCreateForm
case result of
FormSuccess ann -> do
runDB do
insert ann
redirect TrackersR
_ -> defaultLayout
[whamlet|
<p>_{MsgInvalidInput}.
<form method=post action=@{TrackersR} enctype=#{enctype}>
^{widget}
<button>_{MsgSubmit}
|]
trackerCommandForm
:: Html -> MForm Handler (FormResult Command, Widget)
trackerCommandForm = renderDivs do
text <- areq textField (fieldSettingsLabel MsgSendCommand) (Just "{\"action\": \"dump\"}")
let Just c = (decode (fromStrict (encodeUtf8 text)))
pure c
trackerCommandWidget :: Text -> Handler Html
trackerCommandWidget name = do
(widget, enctype) <- generateFormPost trackerCommandForm
defaultLayout [whamlet|
<h2> _{MsgSendCommand}
<form method=post action="@{TrackerCommandR name}" enctype=#{enctype}>
^{widget}
<button>_{MsgSubmit}
|]
postTrackerCommandR :: Text -> Handler Html
postTrackerCommandR name = do
((result, widget), enctype) <- runFormPost trackerCommandForm
case result of
FormSuccess command -> do
now <- liftIO $ getCurrentTime
res <- runDB $
(selectOne do
tracker <- from (table @Tracker)
where_ (tracker ^. TrackerName ==. val name)
pure tracker)
>>= mapM \tracker ->
insert $ TrackerCommand
{ trackerCommandTracker = entityKey tracker
, trackerCommandTimestamp = now
, trackerCommandStatus = Queued
, trackerCommandCommand = command
}
case res of
Just _ -> redirect $ TrackerViewR name
Nothing -> notFound
_ -> defaultLayout
[whamlet|
<p>_{MsgInvalidInput}.
<form method=post action=@{TrackerCommandR name} enctype=#{enctype}>
^{widget}
<button>_{MsgSubmit}
|]
trackerConfigForm
:: Maybe OwnTracks.Configuration
-> Html
-> MForm Handler (FormResult OwnTracks.Configuration, Widget)
trackerConfigForm maybeLastConfig = renderDivs do
-- TODO: default text should be last config, if known?
text <- areq textField
(fieldSettingsLabel MsgSendCommand)
(fmap (decodeUtf8 . toStrict . encode) maybeLastConfig)
let Just c = (decode (fromStrict (encodeUtf8 text)))
pure c
trackerConfigWidget :: Maybe OwnTracks.Configuration -> Text -> Handler Html
trackerConfigWidget maybeLastConfig name = do
(widget, enctype) <- generateFormPost (trackerConfigForm maybeLastConfig)
-- TODO: show which config version we're writing here, and which one was last?
defaultLayout [whamlet|
<h2> _{MsgSendCommand}
<form method=post action="@{TrackerConfigR name}" enctype=#{enctype}>
^{widget}
<button>_{MsgSubmit}
|]
postTrackerConfigR :: Text -> Handler Html
postTrackerConfigR name = do
((result, widget), enctype) <- runFormPost (trackerConfigForm Nothing)
case result of
FormSuccess config -> do
now <- liftIO $ getCurrentTime
res <- runDB $
(selectOne do
tracker <- from (table @Tracker)
where_ (tracker ^. TrackerName ==. val name)
pure tracker)
>>= mapM \tracker@(Entity _ Tracker{..}) -> do
insert $ TrackerCommand
{ trackerCommandTracker = entityKey tracker
, trackerCommandTimestamp = now
, trackerCommandStatus = Queued
, trackerCommandCommand = SetConfiguration (config
{ configHost = configHost config
& fmap \host -> case trackerConfigVersion of
Nothing -> host <> "&v=1"
Just v -> ""+|host|+ "&v="+|show (v + 1)|+""
-- FIXME: something less unsafe here?
})
}
insert $ TrackerConfig
{ trackerConfigTracker = entityKey tracker
, trackerConfigTimestamp = now
, trackerConfigSeen = False
, trackerConfigConfiguration = config
}
case res of
Just _ -> redirect $ TrackerViewR name
Nothing -> notFound
_ -> defaultLayout
[whamlet|
<p>_{MsgInvalidInput}.
<form method=post action=@{TrackerConfigR name} enctype=#{enctype}>
^{widget}
<button>_{MsgSubmit}
|]
getTrackerViewR :: Text -> Handler Html
getTrackerViewR name =
runDB (selectOne do
tracker <- from (table @Tracker)
where_ (tracker ^. TrackerName ==. val name)
pure tracker)
>>= \case
Nothing -> notFound
Just (Entity trackerId Tracker{..}) -> do
(maybeStatus, maybePing, config) <- runDB $ do
status <- selectOne do
status <- from (table @TrackerStatus)
where_ (status ^. TrackerStatusTracker ==. val trackerId)
orderBy [desc $ status ^. TrackerStatusTimestamp]
pure status
ping <- selectOne do
ping <- from (table @Ping)
where_ (ping ^. PingTrackerId ==. val trackerId)
orderBy [desc $ ping ^. PingTimestamp]
pure ping
config <- selectOne do
config <- from (table @TrackerConfig)
where_ (config ^. TrackerConfigTracker ==. val trackerId)
orderBy [desc $ config ^. TrackerConfigTimestamp]
pure config
pure (status, ping, config)
commandWidget <- trackerCommandWidget name
configWidget <- trackerConfigWidget (fmap (trackerConfigConfiguration . entityVal) config) name
-- TODO: leaflet map; auto updates?
defaultLayout [whamlet|
<h1> _{MsgTracker name}
<section>
<h1> _{MsgTracker name}
<p>
Agent: #{trackerAgent} <br>
UUID: #{trackerId}
<p>
<form action=@{TrackerDeleteR trackerName} method="post">
<button> _{Msgdelete}
<section>
<h2> _{MsgLastTrackerStatus}
$maybe Entity _ TrackerStatus{..} <- maybeStatus
LocationPermission: #{show $ statusLocationPermission trackerStatusStatus} <br>
BatteryOptimisations: #{show $ statusBatteryOptimizations trackerStatusStatus} <br>
Phone in power save mode: #{show $ statusPhonePowerSaveMode trackerStatusStatus}
$nothing
<em>Status unknown
<section>
<h2> _{MsgLastTrackerPosition}
$maybe Entity _ Ping{..} <- maybePing
Position: #{show pingGeopos} <br>
Timestamp: #{show pingTimestamp} <br>
$maybe ticketId <- pingTicket
Ticket: <a href="@{TicketViewR (coerce ticketId)}">#{UUID.toText (coerce ticketId)}</a>
$nothing
Ticket: (no assigned ticket)
$nothing
(none)
<section>
^{configWidget}
<section>
^{commandWidget}
|]
postTrackerDeleteR :: Text -> Handler Html
postTrackerDeleteR name = do
runDB $ delete do
tracker <- from (table @Tracker)
where_ (tracker ^. TrackerName ==. val name)
redirect TrackersR
|