Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move terminal flag to source config object #831

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions lib/signs/utilities/predictions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ defmodule Signs.Utilities.Predictions do
) :: Signs.Realtime.sign_messages() | nil
def prediction_messages(
predictions,
%{sources: sources},
%{sources: sources, terminal?: terminal?},
%{pa_ess_loc: station_code, text_zone: zone} = sign
) do
predictions
|> Enum.filter(fn p ->
p.seconds_until_departure && p.schedule_relationship != :skipped
end)
|> Enum.sort_by(fn prediction ->
{if terminal_prediction?(prediction, sources) do
{if terminal? do
0
else
case prediction.stops_away do
Expand All @@ -50,7 +50,7 @@ defmodule Signs.Utilities.Predictions do
stopped_train?(prediction) ->
Content.Message.StoppedTrain.from_prediction(prediction)

terminal_prediction?(prediction, sources) ->
terminal? ->
Content.Message.Predictions.terminal(prediction, station_code, zone, sign)

true ->
Expand Down Expand Up @@ -91,7 +91,7 @@ defmodule Signs.Utilities.Predictions do
end

def prediction_certainty(prediction, config) do
if terminal_prediction?(prediction, config.sources) || !prediction.seconds_until_arrival do
if config.terminal? || !prediction.seconds_until_arrival do
prediction.departure_certainty
else
prediction.arrival_certainty
Expand Down Expand Up @@ -201,18 +201,6 @@ defmodule Signs.Utilities.Predictions do
false
end

defp terminal_prediction?(prediction, source_list) do
source_list
|> SourceConfig.get_source_by_stop_and_direction(
prediction.stop_id,
prediction.direction_id
)
|> case do
nil -> false
source -> source.terminal?
end
end

defp platform(prediction, source_list) do
source_list
|> SourceConfig.get_source_by_stop_and_direction(
Expand Down
12 changes: 5 additions & 7 deletions lib/signs/utilities/source_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ defmodule Signs.Utilities.SourceConfig do
exception is Ashmont, which handles both the Ashmont and Mattapan headway groups.
* headway_direction_name: The headsign used to generate the "trains every X minutes" message in
headway mode. Must be a value recognized by `PaEss.Utilities.headsign_to_destination/1`.
* terminal: whether this is a "terminal", and should use arrival or departure times in its
countdown.
* sources: A list of source objects (see below for details). The sources determine which
predictions to use in the `Engine.Predictions` process. When a list of multiple sources is
provided, their corresponding predictions are aggregated and sorted by arrival time (or
Expand All @@ -71,7 +73,6 @@ defmodule Signs.Utilities.SourceConfig do
"direction_id": 0,
"headway_direction_name": "Forest Hills",
"platform": null,
"terminal": false,
"announce_arriving": true,
"announce_boarding": false
}
Expand All @@ -81,8 +82,6 @@ defmodule Signs.Utilities.SourceConfig do
* direction_id: 0 or 1, used in tandem with the stop ID for predictions
* platform: mostly null, but :ashmont | :braintree for JFK/UMass, where it's used for the "next
train to X is approaching, on the Y platform" audio.
* terminal: whether this is a "terminal", and should use arrival or departure times in its
countdown.
* announce_arriving: whether to play audio when a sign goes to ARR.
* announce_boarding: whether to play audio when a sign goes to BRD. Generally we do one or the
other. Considerations include how noisy the station is, what we've done in the past, how
Expand All @@ -98,7 +97,6 @@ defmodule Signs.Utilities.SourceConfig do
:direction_id,
:routes,
:platform,
:terminal?,
:announce_arriving?,
:announce_boarding?
]
Expand All @@ -111,7 +109,6 @@ defmodule Signs.Utilities.SourceConfig do
direction_id: 0 | 1,
routes: [String.t()] | nil,
platform: Content.platform() | nil,
terminal?: boolean(),
announce_arriving?: boolean(),
announce_boarding?: boolean(),
multi_berth?: boolean()
Expand All @@ -120,20 +117,23 @@ defmodule Signs.Utilities.SourceConfig do
@type config :: %{
headway_group: String.t(),
headway_destination: PaEss.destination() | nil,
terminal?: boolean(),
sources: [source()]
}

@spec parse!(map() | [map()]) :: config() | {config(), config()}
def parse!(%{
"headway_group" => headway_group,
"headway_direction_name" => headway_direction_name,
"terminal" => terminal,
"sources" => sources
}) do
{:ok, headway_destination} = PaEss.Utilities.headsign_to_destination(headway_direction_name)

%{
headway_group: headway_group,
headway_destination: headway_destination,
terminal?: terminal,
sources: Enum.map(sources, &parse_source!/1)
}
end
Expand All @@ -147,7 +147,6 @@ defmodule Signs.Utilities.SourceConfig do
"stop_id" => stop_id,
"direction_id" => direction_id,
"platform" => platform,
"terminal" => terminal?,
"announce_arriving" => announce_arriving?,
"announce_boarding" => announce_boarding?
} = source
Expand All @@ -170,7 +169,6 @@ defmodule Signs.Utilities.SourceConfig do
direction_id: direction_id,
routes: source["routes"],
platform: platform,
terminal?: terminal?,
announce_arriving?: announce_arriving?,
announce_boarding?: announce_boarding?,
multi_berth?: multi_berth?
Expand Down
Loading
Loading