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
276
|
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Server.ControlRoom (ControlRoom(..)) where
import Control.Monad (forM_)
import Control.Monad.Extra (maybeM)
import Control.Monad.IO.Class (MonadIO (liftIO))
import qualified Data.ByteString.Char8 as C8
import Data.Functor ((<&>))
import Data.List (lookup)
import Data.Map (Map)
import qualified Data.Map as M
import Data.Pool (Pool)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time (getCurrentTime, utctDay)
import Data.Time.Calendar (Day)
import Data.Time.Format.ISO8601 (iso8601Show)
import Data.UUID (UUID)
import qualified Data.UUID as UUID
import qualified Data.Vector as V
import Database.Persist (Entity (..), delete, entityVal, get,
insert, selectList, (==.))
import Database.Persist.Sql (PersistFieldSql, SqlBackend,
runSqlPool)
import Fmt ((+|), (|+))
import GHC.Generics (Generic)
import Persist (AmendmentStatus, Announcement (..),
EntityField (..), Key (..),
ScheduleAmendment (ScheduleAmendment),
runSql)
import Server.Util (Service)
import Text.Blaze.Html (ToMarkup (..))
import Text.Blaze.Internal (MarkupM(Empty))
import Text.ProtocolBuffers (Default (defaultValue))
import Text.Read (readMaybe)
import Text.Shakespeare.Text
import Yesod
import Yesod.Form
import GTFS
data ControlRoom = ControlRoom
{ getBaseurl :: Text
, getGtfs :: GTFS
, getPool :: Pool SqlBackend
}
mkMessage "ControlRoom" "messages" "en"
mkYesod "ControlRoom" [parseRoutes|
/ RootR GET
/trains TrainsR GET
/train/id/#TripID/#Day TrainViewR GET
/train/announce/#TripID/#Day AnnounceR POST
/train/del-announce/#UUID DelAnnounceR GET
/trips TripsViewR GET
/trip/#TripID TripViewR GET
|]
emptyMarkup :: MarkupM a -> Bool
emptyMarkup (Empty _) = True
emptyMarkup _ = False
instance Yesod ControlRoom where
approot = ApprootMaster (\cr -> getBaseurl cr)
defaultLayout w = do
PageContent{..} <- widgetToPageContent w
msgs <- getMessages
withUrlRenderer [hamlet|
$newline never
$doctype 5
<html>
<head>
<title>
$if emptyMarkup pageTitle
Tracktrain
$else
#{pageTitle}
$maybe description <- pageDescription
<meta name="description" content="#{description}">
^{pageHead}
<style>
section {
border: 1px solid black;
padding: 1rem;
margin: 2rem;
padding-top: 0;
}
body {
max-width: 50rem;
margin: auto;
}
form {
width:100%;
display: grid;
gap: 1rem;
}
label {
grid-column: 1;
}
form div {
display: grid;
grid-template-columns: 50% 50%;
width:100%;
}
input {
grid-column: 2;
}
<body>
$forall (status, msg) <- msgs
<p class="message #{status}">#{msg}
^{pageBody}
|]
instance RenderMessage ControlRoom FormMessage where
renderMessage _ _ = defaultFormMessage
instance YesodPersist ControlRoom where
type YesodPersistBackend ControlRoom = SqlBackend
runDB action = do
pool <- getYesod <&> getPool
runSqlPool action pool
getRootR :: Handler Html
getRootR = redirect (TrainsR)
getTrainsR :: Handler Html
getTrainsR = do
req <- getRequest
let maybeDay = lookup "day" (reqGetParams req) >>= (readMaybe . T.unpack)
day <- liftIO $ maybeM (getCurrentTime <&> utctDay) pure (pure maybeDay)
gtfs <- getYesod <&> getGtfs
let trips = tripsOnDay gtfs day
defaultLayout $ do
[whamlet|
<h1>Trains on #{day}
<section><ol>
$forall Trip{..} <- trips
<li><a href="@{TrainViewR tripTripID day}">#{tripTripID}</a>
: #{stopDeparture (V.head tripStops)} #{stationName (stopStation (V.head tripStops))}
|]
getTrainViewR :: TripID -> Day -> Handler Html
getTrainViewR trip day = do
GTFS{..} <- getYesod <&> getGtfs
(widget, enctype) <- generateFormPost (announceForm day trip)
case M.lookup trip trips of
Nothing -> notFound
Just res@Trip{..} -> do
anns <- runDB $ selectList [ AnnouncementTrip ==. trip, AnnouncementDay ==. day ] []
defaultLayout $ do
mr <- getMessageRender
setTitle (toHtml (""+|mr MsgTrip|+" "+|tripTripID|+" "+|mr Msgon|+" "+|day|+"" :: Text))
[whamlet|
<h1>_{MsgTrip} <a href="@{TripViewR tripTripID}">#{tripTripID}</a> _{Msgon} <a href="@?{(TrainsR, [("day", T.pack (iso8601Show day))])}">#{day}</a>
<section>
<h2>_{MsgAnnouncements}
<ul>
$forall Entity (AnnouncementKey uuid) Announcement{..} <- anns
<li><em>#{announcementHeader}: #{announcementMessage}</em> <a href="@{DelAnnounceR uuid}">delete</a>
$if null anns
<li><em>(_{MsgNone})</em>
<h3>_{MsgNewAnnouncement}
<form method=post action=@{AnnounceR trip day} enctype=#{enctype}>
^{widget}
<button>Submit
<section>
<h2>_{MsgStops}
<ol>
$forall Stop{..} <- tripStops
<li> #{stopArrival} #{stationName stopStation}
<section>
<h2>Vehicle Position
<div> Todo!
|]
getTripsViewR :: Handler Html
getTripsViewR = do
GTFS{..} <- getYesod <&> getGtfs
defaultLayout $ do
setTitle "List of Trips"
[whamlet|
<h1>List of Trips
<section><ul>
$forall Trip{..} <- trips
<li><a href="@{TripViewR tripTripID}">#{tripTripID}</a>
: #{stopDeparture (V.head tripStops)} #{stationName (stopStation (V.head tripStops))}
|]
getTripViewR :: TripID -> Handler Html
getTripViewR tripId = do
GTFS{..} <- getYesod <&> getGtfs
case M.lookup tripId trips of
Nothing -> notFound
Just Trip{..} -> defaultLayout [whamlet|
<h1>Trip #{tripTripID}
<section>
<h2>Stops
<ol>
$forall Stop{..} <- tripStops
<div>(#{stopSequence}) #{stopArrival} #{stationName stopStation}
<section>
<h2>Dates
<ul>
TODO!
|]
postAnnounceR :: TripID -> Day -> Handler Html
postAnnounceR trip day = do
((result, widget), enctype) <- runFormPost (announceForm day trip)
case result of
FormSuccess ann -> do
runDB $ insert ann
redirect (TrainViewR trip day)
_ -> defaultLayout
[whamlet|
<p>Invalid input, let's try again.
<form method=post action=@{AnnounceR trip day} enctype=#{enctype}>
^{widget}
<button>Submit
|]
getDelAnnounceR :: UUID -> Handler Html
getDelAnnounceR uuid = do
ann <- runDB $ do
a <- get (AnnouncementKey uuid)
delete (AnnouncementKey uuid)
pure a
case ann of
Nothing -> notFound
Just Announcement{..} ->
redirect (TrainViewR announcementTrip announcementDay)
announceForm :: Day -> TripID -> Html -> MForm Handler (FormResult Announcement, Widget)
announceForm day tripId = renderDivs $ Announcement
<$> pure tripId
<*> areq textField (fieldSettingsLabel MsgHeader) Nothing
<*> areq textField (fieldSettingsLabel MsgText) Nothing
<*> pure day
<*> aopt urlField (fieldSettingsLabel MsgMaybeWeblink) Nothing
<*> lift (liftIO getCurrentTime <&> Just)
--- some orphans to make hamlet easier to deal with
instance ToMarkup Time where
toMarkup time =
toMarkup (show time)
instance ToMarkup Day where
toMarkup day = toMarkup (iso8601Show day)
|