diff --git a/apps/api_web/lib/api_web/controllers/vehicle_controller.ex b/apps/api_web/lib/api_web/controllers/vehicle_controller.ex index 171403c2..d3f58ca2 100644 --- a/apps/api_web/lib/api_web/controllers/vehicle_controller.ex +++ b/apps/api_web/lib/api_web/controllers/vehicle_controller.ex @@ -259,8 +259,7 @@ defmodule ApiWeb.VehicleController do %{ "label" => "some-carriage", "occupancy_status" => "MANY_SEATS_AVAILABLE", - "occupancy_percentage" => 80, - "carriage_sequence" => 1 + "occupancy_percentage" => 80 } ] ) diff --git a/apps/api_web/lib/api_web/swagger_helpers.ex b/apps/api_web/lib/api_web/swagger_helpers.ex index 22d0c82d..636a140d 100644 --- a/apps/api_web/lib/api_web/swagger_helpers.ex +++ b/apps/api_web/lib/api_web/swagger_helpers.ex @@ -91,10 +91,6 @@ defmodule ApiWeb.SwaggerHelpers do type: :string, description: "Carriage-specific label, used as an identifier" }, - carriage_sequence: %Schema{ - type: :integer, - description: "Provides a reliable order" - }, occupancy_status: %Schema{ type: :string, description: occupancy_status_description(), diff --git a/apps/api_web/lib/api_web/views/vehicle_view.ex b/apps/api_web/lib/api_web/views/vehicle_view.ex index 855216f0..c046d4fd 100644 --- a/apps/api_web/lib/api_web/views/vehicle_view.ex +++ b/apps/api_web/lib/api_web/views/vehicle_view.ex @@ -44,6 +44,7 @@ defmodule ApiWeb.VehicleView do vehicle |> super(conn) |> backwards_compatible_attributes(vehicle, conn.assigns.api_version) + |> encode_carriages() end for status <- ~w(in_transit_to incoming_at stopped_at)a do @@ -62,21 +63,25 @@ defmodule ApiWeb.VehicleView do end for status <- - ~w(empty many_seats_available few_seats_available standing_room_only crushed_standing_room_only full not_accepting_passengers)a do + ~w(empty many_seats_available few_seats_available standing_room_only crushed_standing_room_only full not_accepting_passengers no_data_available not_boardable)a do status_binary = status |> Atom.to_string() |> String.upcase() - def occupancy_status(%{occupancy_status: unquote(status)}, _conn) do + def occupancy_status(%{occupancy_status: unquote(status)}) do unquote(status_binary) end end - def occupancy_status(_, _) do + def occupancy_status(_) do nil end + def occupancy_status(conveyance, _conn) do + occupancy_status(conveyance) + end + defp backwards_compatible_attributes(attributes, vehicle, "2017-11-28") do Map.put(attributes, :last_updated, vehicle.updated_at) end @@ -84,4 +89,16 @@ defmodule ApiWeb.VehicleView do defp backwards_compatible_attributes(attributes, _, _) do attributes end + + defp encode_carriages(vehicle) do + Map.put(vehicle, :carriages, Enum.map(vehicle.carriages || [], &encode_carriage/1)) + end + + defp encode_carriage(carriage) do + %{ + label: carriage.label, + occupancy_status: occupancy_status(carriage, nil), + occupancy_percentage: carriage.occupancy_percentage + } + end end diff --git a/apps/api_web/test/api_web/controllers/vehicle_controller_test.exs b/apps/api_web/test/api_web/controllers/vehicle_controller_test.exs index da7afb15..56c08611 100644 --- a/apps/api_web/test/api_web/controllers/vehicle_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/vehicle_controller_test.exs @@ -43,7 +43,22 @@ defmodule ApiWeb.VehicleControllerTest do latitude: 42.01, longitude: -71.15, speed: 75, - stop_id: "current_stop" + stop_id: "current_stop", + occupancy_status: :empty, + carriages: [ + %Vehicle.Carriage{ + label: "carriage_1", + occupancy_status: :empty, + occupancy_percentage: 0, + carriage_sequence: 1 + }, + %Vehicle.Carriage{ + label: "carriage_2", + occupancy_status: :empty, + occupancy_percentage: 0, + carriage_sequence: 2 + } + ] } @stop %Model.Stop{id: "current_stop"} @vehicle hd(@vehicles) @@ -255,7 +270,7 @@ defmodule ApiWeb.VehicleControllerTest do "current_stop_sequence" => nil, "updated_at" => nil, "occupancy_status" => nil, - "carriages" => nil + "carriages" => [] } } end @@ -333,7 +348,22 @@ defmodule ApiWeb.VehicleControllerTest do latitude: 42.01, longitude: -71.15, speed: 75, - stop_id: "current_stop" + stop_id: "current_stop", + occupancy_status: :many_seats_available, + carriages: [ + %Vehicle.Carriage{ + label: "carriage_1", + occupancy_status: :empty, + occupancy_percentage: 0, + carriage_sequence: 1 + }, + %Vehicle.Carriage{ + label: "carriage_2", + occupancy_status: :empty, + occupancy_percentage: 0, + carriage_sequence: 2 + } + ] } State.Vehicle.new_state([vehicle]) diff --git a/apps/model/lib/model/vehicle.ex b/apps/model/lib/model/vehicle.ex index 209624e7..a3a9a8b8 100644 --- a/apps/model/lib/model/vehicle.ex +++ b/apps/model/lib/model/vehicle.ex @@ -50,6 +50,8 @@ defmodule Model.Vehicle do | :crushed_standing_room_only | :full | :not_accepting_passengers + | :no_data_available + | :not_boardable @typedoc """ Meters per second diff --git a/apps/parse/lib/parse/vehicle_positions_json.ex b/apps/parse/lib/parse/vehicle_positions_json.ex index 6fd07791..e5353196 100644 --- a/apps/parse/lib/parse/vehicle_positions_json.ex +++ b/apps/parse/lib/parse/vehicle_positions_json.ex @@ -101,6 +101,10 @@ defmodule Parse.VehiclePositionsJson do defp parse_occupancy_status("NOT_ACCEPTING_PASSENGERS"), do: :not_accepting_passengers + defp parse_occupancy_status("NO_DATA_AVAILABLE"), do: :no_data_available + + defp parse_occupancy_status("NOT_BOARDABLE"), do: :not_boardable + defp unix_to_local(timestamp) when is_integer(timestamp) do Parse.Timezone.unix_to_local(timestamp) end