DE_ICE: rewrite to use an intermediary map; handle .trip.stops being absent

If iceportal.de is having a weird day, .trip.stops in tripInfo may be null,
which would cause that entire block to throw an error.

I originally did this with just string concatenation, but an intermediary
map like this feels less dirty and makes the code a bit more readable.
This commit is contained in:
embr 2022-10-26 16:17:32 +02:00
parent d4a9c8d11a
commit 942e6da0c9

View file

@ -8,44 +8,44 @@ set train_status (curl -s https://iceportal.de/api1/rs/status)
echo $train_status | jq type >/dev/null 2>&1; or exit
echo $train_status | jq -r '"
AMBIENT_DE_ICE_CONNECTION=\(.connection)
AMBIENT_DE_ICE_SERVICE_LEVEL=\(.servicelevel)
AMBIENT_DE_ICE_INTERNET=\(.internet)
AMBIENT_DE_ICE_SPEED=\(.speed)
AMBIENT_DE_ICE_SPEED_UNIT=km/h
AMBIENT_DE_ICE_GPS_STATUS=\(.gpsStatus)
AMBIENT_DE_ICE_TZN=\(.tzn)
AMBIENT_DE_ICE_SERIES=\(.series)
AMBIENT_DE_ICE_LATITUDE=\(.latitude)
AMBIENT_DE_ICE_LONGITUDE=\(.longitude)
AMBIENT_DE_ICE_SERVER_TIME=\(.serverTime / 1000 | round | tostring | strptime("%s") | todate)
AMBIENT_DE_ICE_WAGON_CLASS=\(.wagonClass)
AMBIENT_DE_ICE_TRAIN_TYPE=\(.trainType)
"'
echo $train_status | jq -r '{
"AMBIENT_DE_ICE_CONNECTION": (.connection),
"AMBIENT_DE_ICE_SERVICE_LEVEL": (.servicelevel),
"AMBIENT_DE_ICE_INTERNET": (.internet),
"AMBIENT_DE_ICE_SPEED": (.speed),
"AMBIENT_DE_ICE_SPEED_UNIT": "km/h",
"AMBIENT_DE_ICE_GPS_STATUS": (.gpsStatus),
"AMBIENT_DE_ICE_TZN": (.tzn),
"AMBIENT_DE_ICE_SERIES": (.series),
"AMBIENT_DE_ICE_LATITUDE": (.latitude),
"AMBIENT_DE_ICE_LONGITUDE": (.longitude),
"AMBIENT_DE_ICE_SERVER_TIME": (.serverTime / 1000 | round | tostring | strptime("%s") | todate),
"AMBIENT_DE_ICE_WAGON_CLASS": (.wagonClass),
"AMBIENT_DE_ICE_TRAIN_TYPE": (.trainType),
} | to_entries | map(.key + "=" + (.value | tostring)) | join("\n")'
curl -s https://iceportal.de/api1/rs/tripInfo/trip | jq -r '"
AMBIENT_DE_ICE_TRIP_DATE=\(.trip.tripDate)
AMBIENT_DE_ICE_VZN=\(.trip.vzn)
AMBIENT_DE_ICE_ACTUAL_POSITION=\(.trip.actualPosition)
AMBIENT_DE_ICE_ACTUAL_DISTANCE_FROM_LAST_STOP=\(.trip.distanceFromLastStop)
AMBIENT_DE_ICE_ACTUAL_TOTAL_DISTANCE=\(.trip.totalDistance)
curl -s https://iceportal.de/api1/rs/tripInfo/trip | jq -r '{
"AMBIENT_DE_ICE_TRIP_DATE": .trip.tripDate,
"AMBIENT_DE_ICE_VZN": .trip.vzn,
"AMBIENT_DE_ICE_ACTUAL_POSITION": .trip.actualPosition,
"AMBIENT_DE_ICE_ACTUAL_DISTANCE_FROM_LAST_STOP": .trip.distanceFromLastStop,
"AMBIENT_DE_ICE_ACTUAL_TOTAL_DISTANCE": .trip.totalDistance,
} + if .trip.stops then {
"AMBIENT_DE_ICE_STOP_FIRST": (.trip.stops | first).station.name,
"AMBIENT_DE_ICE_STOP_FIRST_DEPART_SCHEDULED": ((.trip.stops | first).timetable.scheduledDepartureTime / 1000 | round | tostring | strptime("%s") | todate),
"AMBIENT_DE_ICE_STOP_FIRST_DEPART_ACTUAL": ((.trip.stops | first).timetable.actualDepartureTime/ 1000 | round | tostring | strptime("%s") | todate),
"AMBIENT_DE_ICE_STOP_FIRST_DEPART_DELAY": (.trip.stops | first).timetable.departureDelay,
AMBIENT_DE_ICE_STOP_FIRST=\((.trip.stops | first).station.name)
AMBIENT_DE_ICE_STOP_FIRST_DEPART_SCHEDULED=\((.trip.stops | first).timetable.scheduledDepartureTime / 1000 | round | tostring | strptime("%s") | todate)
AMBIENT_DE_ICE_STOP_FIRST_DEPART_ACTUAL=\((.trip.stops | first).timetable.actualDepartureTime/ 1000 | round | tostring | strptime("%s") | todate)
AMBIENT_DE_ICE_STOP_FIRST_DEPART_DELAY=\((.trip.stops | first).timetable.departureDelay)
"AMBIENT_DE_ICE_STOP_LAST": (.trip.stops | last).station.name,
"AMBIENT_DE_ICE_STOP_LAST_ARRIVE_SCHEDULED": ((.trip.stops | last).timetable.scheduledArrivalTime / 1000 | round | tostring | strptime("%s") | todate),
"AMBIENT_DE_ICE_STOP_LAST_ARRIVE_ACTUAL": ((.trip.stops | last).timetable.actualArrivalTime/ 1000 | round | tostring | strptime("%s") | todate),
"AMBIENT_DE_ICE_STOP_LAST_ARRIVE_DELAY": (.trip.stops | last).timetable.arrivalDelay,
AMBIENT_DE_ICE_STOP_LAST=\((.trip.stops | last).station.name)
AMBIENT_DE_ICE_STOP_LAST_ARRIVE_SCHEDULED=\((.trip.stops | last).timetable.scheduledArrivalTime / 1000 | round | tostring | strptime("%s") | todate)
AMBIENT_DE_ICE_STOP_LAST_ARRIVE_ACTUAL=\((.trip.stops | last).timetable.actualArrivalTime/ 1000 | round | tostring | strptime("%s") | todate)
AMBIENT_DE_ICE_STOP_LAST_ARRIVE_DELAY=\((.trip.stops | last).timetable.arrivalDelay)
AMBIENT_DE_ICE_STOP_NEXT=\(([.trip.stops[] | select(.info.passed == false)] | first).station.name)
AMBIENT_DE_ICE_STOP_NEXT_DEPART_SCHEDULED=\(([.trip.stops[] | select(.info.passed == false)] | first).timetable.scheduledDepartureTime / 1000 | round | tostring | strptime("%s") | todate)
AMBIENT_DE_ICE_STOP_NEXT_DEPART_ACTUAL=\(([.trip.stops[] | select(.info.passed == false)] | first).timetable.actualDepartureTime/ 1000 | round | tostring | strptime("%s") | todate)
AMBIENT_DE_ICE_STOP_NEXT_DEPART_DELAY=\(([.trip.stops[] | select(.info.passed == false)] | first).timetable.departureDelay)
AMBIENT_DE_ICE_STOP_NEXT_ARRIVE_SCHEDULED=\(([.trip.stops[] | select(.info.passed == false)] | first).timetable.scheduledArrivalTime / 1000 | round | tostring | strptime("%s") | todate)
AMBIENT_DE_ICE_STOP_NEXT_ARRIVE_ACTUAL=\(([.trip.stops[] | select(.info.passed == false)] | first).timetable.actualArrivalTime/ 1000 | round | tostring | strptime("%s") | todate)
AMBIENT_DE_ICE_STOP_NEXT_ARRIVE_DELAY=\(([.trip.stops[] | select(.info.passed == false)] | first).timetable.arrivalDelay)
"'
"AMBIENT_DE_ICE_STOP_NEXT": ([.trip.stops[] | select(.info.passed == false)] | first).station.name,
"AMBIENT_DE_ICE_STOP_NEXT_DEPART_SCHEDULED": (([.trip.stops[] | select(.info.passed == false)] | first).timetable.scheduledDepartureTime / 1000 | round | tostring | strptime("%s") | todate),
"AMBIENT_DE_ICE_STOP_NEXT_DEPART_ACTUAL": (([.trip.stops[] | select(.info.passed == false)] | first).timetable.actualDepartureTime/ 1000 | round | tostring | strptime("%s") | todate),
"AMBIENT_DE_ICE_STOP_NEXT_DEPART_DELAY": ([.trip.stops[] | select(.info.passed == false)] | first).timetable.departureDelay,
"AMBIENT_DE_ICE_STOP_NEXT_ARRIVE_SCHEDULED": (([.trip.stops[] | select(.info.passed == false)] | first).timetable.scheduledArrivalTime / 1000 | round | tostring | strptime("%s") | todate),
"AMBIENT_DE_ICE_STOP_NEXT_ARRIVE_ACTUAL": (([.trip.stops[] | select(.info.passed == false)] | first).timetable.actualArrivalTime/ 1000 | round | tostring | strptime("%s") | todate),
"AMBIENT_DE_ICE_STOP_NEXT_ARRIVE_DELAY": ([.trip.stops[] | select(.info.passed == false)] | first).timetable.arrivalDelay,
} else {} end | to_entries | map(.key + "=" + (.value | tostring)) | join("\n")'