61 lines
2.2 KiB
Nim
61 lines
2.2 KiB
Nim
import ../types
|
|
import ./stopover
|
|
import ./msg
|
|
import ./date
|
|
import json
|
|
import options
|
|
|
|
proc parseLegPart(common: CommonData, lp: JsonNode): LegPart =
|
|
let h = lp.to(HafasStopParams)
|
|
let plannedDepartureTime = parseDate(common, h.dTimeS, h.dTZOffset)
|
|
let plannedArrivalTime = parseDate(common, h.aTimeS, h.aTZOffset)
|
|
|
|
if h.dPlatfS.isSome: result.plannedPlatform = h.dPlatfS
|
|
elif h.aPlatfS.isSome: result.plannedPlatform = h.aPlatfS
|
|
if h.dPlatfR.isSome: result.prognosedPlatform = h.dPlatfR
|
|
elif h.aPlatfR.isSome: result.prognosedPlatform = h.aPlatfR
|
|
if h.dTimeR.isSome: result.prognosedTime = parseDate(common, h.dTimeR, h.dTZOffset)
|
|
elif h.aTimeR.isSome: result.prognosedTime = parseDate(common, h.aTimeR, h.aTZOffset)
|
|
|
|
if plannedDepartureTime.isSome: result.plannedTime = plannedDepartureTime.get
|
|
elif plannedArrivalTime.isSome: result.plannedTime = plannedArrivalTime.get
|
|
else: raise newException(CatchableError, "missing departure and arrival time for leg")
|
|
|
|
result.point = common.points[h.locX.get]
|
|
|
|
proc mkParseLeg*(common: CommonData): proc =
|
|
proc parseLeg(l: JsonNode): Leg =
|
|
|
|
let typeStr = l{"type"}.getStr()
|
|
if typeStr == "JNY":
|
|
result.direction = some(l{"jny"}{"dirTxt"}.getStr())
|
|
result.tripId = some(l{"jny"}{"jid"}.getStr())
|
|
result.line = common.lines[l{"jny"}{"prodX"}.getInt()]
|
|
|
|
let stopovers = l{"jny"}{"stopL"}.getElems()
|
|
if stopovers.len > 0:
|
|
result.stopovers = some(stopovers.map(mkParseStopover(common)))
|
|
|
|
let remarks = l{"jny"}{"msgL"}.getElems()
|
|
if remarks.len > 0:
|
|
result.remarks = some(remarks.map(mkParseMsg(common)))
|
|
|
|
elif typeStr == "WALK" or typeStr == "TRSF":
|
|
result.isWalking = true
|
|
result.distance = some(l{"gis"}{"dist"}.getInt())
|
|
|
|
elif typeStr == "DEVI":
|
|
result.isTransfer = true
|
|
result.distance = some(l{"gis"}{"dist"}.getInt())
|
|
|
|
else:
|
|
raise newException(CatchableError, "Unimplemented hafas leg type: " & typeStr)
|
|
|
|
result.departure = common.parseLegPart(l{"dep"})
|
|
result.arrival = common.parseLegPart(l{"arr"})
|
|
|
|
result.cancelled = l{"dep"}{"dCncl"}.getBool(false) or l{"arr"}{"aCncl"}.getBool(false)
|
|
|
|
return parseLeg
|
|
|