Author: Adam Evyčędo <git@apiote.xyz>
add realtime data to Berlin VBB
traffic/berlin_vbb.go | 106 ++++++++++++++++++++++++++++++++++++++++++++
diff --git a/traffic/berlin_vbb.go b/traffic/berlin_vbb.go index 8d12d00e0012345917122ced9027c723610aca8b..f65d108dddb26683dd5ac870ebef1420632d5245 100644 --- a/traffic/berlin_vbb.go +++ b/traffic/berlin_vbb.go @@ -54,7 +54,111 @@ return map[RealtimeFeedType]string{} } func (BerlinVbb) LuaUpdatesScript(config.Auth) string { - return "" + return ` + function getUpdates(tripID, sequence, stopID) + + local http = require("http") + local json = require("json") + + error_struct = { + httpResponseCode=0, + message="", + willNextRequestFail=false + } + + ibnr = "" + i = 0 + for m in string.gmatch(stopID, "([^:]+)") do + if i == 2 then + ibnr = m + break + end + i = i+1 + end + + response, error_message = http.get("https://v6.vbb.transport.rest/stops/" .. ibnr .. "/departures", { + query="duration=30&results=24&pretty=false", + timeout="30s" + }) + + if response == nil then + error_struct.message = "while getting updates: " .. error_message + error_json, _ = json.encode(error_struct) + return "", error_json + end + + if response.status_code ~= 200 then + error_struct.message = "api returned code " .. response.status_code .. "; " .. response.body + error_struct.httpResponseCode = response.status_code + error_struct.willNextRequestFail = true + error_json, _ = json.encode(error_struct) + return "", error_json + end + + struct, error_message = json.decode(response.body) + + if struct == nil then + error_struct.message = "while decoding updates: " .. error_message + error_json, _ = json.encode(error_struct) + return "", error_json + end + + updates = { + areTripsInTimetable=false, + updates={} + } + updates.updates[''] = { + time="000000", + stopID=stopID, + delay=0, + timetableRelationship=1, + vehicleStatus={ + lineName="", + headsign="" + } + } + + + for i,entry in ipairs(struct.departures) do + stationIBNR = "" + i = 0 + for m in string.gmatch(entry.stop.stationDHID, "([^:]+)") do + if i == 2 then + stationIBNR = m + break + end + i = i+1 + end + + if stationIBNR == ibnr then + updates.updates[entry.tripId] = { + time=string.sub(entry.when, 12, 13) .. string.sub(entry.when, 15, 16) .. string.sub(entry.when, 18, 19), + stopID=stopID, + delay=entry.delay, + timetableRelationship=1, + vehicleStatus={ + lineName=entry.line.name, + headsign=entry.destination.name, + } + } + if entry.currentTripPosition ~= nil then + updates.updates[entry.tripId].vehicleStatus.latitude=entry.currentTripPosition.latitude + updates.updates[entry.tripId].vehicleStatus.longitude=entry.currentTripPosition.longitude + end + end + end + + result, error_message = json.encode(updates) + + if result == nil then + error_struct.message = "while encoding result: " .. error_message + error_json, _ = json.encode(error_struct) + return "", error_json + end + + return result, "" + end + ` } func (BerlinVbb) Transformer() transform.Transformer {