souplesse/frontend/src/Track.elm

44 lines
1.4 KiB
Elm

module Track exposing (Track, Point, parse)
import Result
import Xml.Decode as XD exposing (path, list, single, floatAttr, stringAttr)
import Time
import Iso8601
type alias Position = (Float, Float, Maybe Float) -- lat, lon, ele
type alias Point =
{ loc : Position
, time : Maybe Time.Posix
, power : Maybe Int
, cadence : Maybe Int
, hr : Maybe Int
}
type alias Track = List Point
timeDecoder : XD.Decoder Time.Posix
timeDecoder = XD.andThen (\ts ->
case Iso8601.toTime ts of
Result.Ok time -> XD.succeed time
Result.Err f -> XD.fail "err"
)
XD.string
triple x y z = (x,y,z)
pointDecoder = XD.map5 Point
(XD.map3 triple (floatAttr "lat") (floatAttr "lon")
(XD.maybe (path ["ele"] (single XD.float))))
(XD.maybe (path ["time"] (single timeDecoder)))
(XD.maybe (path ["extensions", "gpxtpx:TrackPointExtension", "pwr:PowerInWatts"] (single XD.int)))
(XD.maybe (path ["extensions", "gpxtpx:TrackPointExtension", "gpxtpx:cad"] (single XD.int)))
(XD.maybe (path ["extensions", "gpxtpx:TrackPointExtension", "gpxtpx:hr"] (single XD.int)))
gpxDecoder =
(path [ "trk", "trkseg", "trkpt" ] (list pointDecoder))
parse str = XD.run gpxDecoder str