Skip to content

Commit

Permalink
formalize audio announcement logic (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
panentheos authored Oct 5, 2023
1 parent 1335773 commit 78c953d
Show file tree
Hide file tree
Showing 14 changed files with 441 additions and 862 deletions.
14 changes: 14 additions & 0 deletions lib/content/audio/approaching.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Content.Audio.Approaching do
"""

require Logger
alias Content.Message
alias PaEss.Utilities

@enforce_keys [:destination]
Expand All @@ -19,6 +20,19 @@ defmodule Content.Audio.Approaching do
crowding_description: {atom(), atom()} | nil
}

def from_message(%Message.Predictions{} = message, include_crowding?) do
[
%__MODULE__{
destination: message.destination,
trip_id: message.trip_id,
platform: message.platform,
route_id: message.route_id,
new_cars?: message.new_cars?,
crowding_description: if(include_crowding?, do: message.crowding_description)
}
]
end

defimpl Content.Audio do
@attention_passengers "783"
@now_approaching_new_rl_cars "786"
Expand Down
16 changes: 16 additions & 0 deletions lib/content/audio/next_train_countdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ defmodule Content.Audio.NextTrainCountdown do
}

require Logger
alias Content.Message

def from_message(%Message.Predictions{} = message) do
[
%__MODULE__{
destination: message.destination,
route_id: message.route_id,
minutes: if(message.minutes == :approaching, do: 1, else: message.minutes),
verb: if(message.terminal?, do: :departs, else: :arrives),
track_number: Content.Utilities.stop_track_number(message.stop_id),
platform: message.platform,
station_code: message.station_code,
zone: message.zone
}
]
end

defimpl Content.Audio do
alias PaEss.Utilities
Expand Down
87 changes: 5 additions & 82 deletions lib/content/audio/predictions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ defmodule Content.Audio.Predictions do

require Logger
require Content.Utilities
alias Content.Audio.BoardingButton
alias Content.Audio.TrackChange
alias Content.Audio.TrainIsBoarding
alias Content.Audio.TrainIsArriving
alias Content.Audio.Approaching
alias Content.Audio.NextTrainCountdown
alias Content.Audio

@heavy_rail_routes ["Red", "Orange", "Blue"]

Expand All @@ -27,90 +22,18 @@ defmodule Content.Audio.Predictions do
multi_source?
) do
cond do
TrackChange.park_track_change?(predictions) and predictions.minutes == :boarding ->
[
%TrackChange{
destination: predictions.destination,
route_id: predictions.route_id,
berth: predictions.stop_id
}
]

predictions.minutes == :boarding ->
[
%TrainIsBoarding{
destination: predictions.destination,
trip_id: predictions.trip_id,
route_id: predictions.route_id,
track_number: Content.Utilities.stop_track_number(predictions.stop_id)
}
] ++
if predictions.station_code == "BBOW" && predictions.zone == "e" do
[%BoardingButton{}]
else
[]
end
Audio.TrainIsBoarding.from_message(predictions)

predictions.minutes == :arriving ->
[
%TrainIsArriving{
destination: predictions.destination,
trip_id: predictions.trip_id,
platform: predictions.platform,
route_id: predictions.route_id,
crowding_description:
if(predictions.crowding_data_confidence == :high,
do: predictions.crowding_description
)
}
]
Audio.TrainIsArriving.from_message(predictions, false)

predictions.minutes == :approaching and (line == :top or multi_source?) and
predictions.route_id in @heavy_rail_routes ->
[
%Approaching{
destination: predictions.destination,
trip_id: predictions.trip_id,
platform: predictions.platform,
route_id: predictions.route_id,
new_cars?: predictions.new_cars?,
crowding_description:
if(predictions.crowding_data_confidence == :high,
do: predictions.crowding_description
)
}
]

predictions.minutes == :approaching ->
[
%NextTrainCountdown{
destination: predictions.destination,
route_id: predictions.route_id,
minutes: 1,
verb: if(predictions.terminal?, do: :departs, else: :arrives),
track_number: Content.Utilities.stop_track_number(predictions.stop_id),
platform: predictions.platform,
station_code: predictions.station_code,
zone: predictions.zone
}
]

is_integer(predictions.minutes) ->
[
%NextTrainCountdown{
destination: predictions.destination,
route_id: predictions.route_id,
minutes: predictions.minutes,
verb: if(predictions.terminal?, do: :departs, else: :arrives),
track_number: Content.Utilities.stop_track_number(predictions.stop_id),
platform: predictions.platform,
station_code: predictions.station_code,
zone: predictions.zone
}
]
Audio.Approaching.from_message(predictions, false)

true ->
[]
Audio.NextTrainCountdown.from_message(predictions)
end
end
end
13 changes: 13 additions & 0 deletions lib/content/audio/train_is_arriving.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Content.Audio.TrainIsArriving do
"""

require Logger
alias Content.Message
alias PaEss.Utilities

@enforce_keys [:destination]
Expand All @@ -17,6 +18,18 @@ defmodule Content.Audio.TrainIsArriving do
crowding_description: {atom(), atom()} | nil
}

def from_message(%Message.Predictions{} = message, include_crowding?) do
[
%__MODULE__{
destination: message.destination,
trip_id: message.trip_id,
platform: message.platform,
route_id: message.route_id,
crowding_description: if(include_crowding?, do: message.crowding_description)
}
]
end

defimpl Content.Audio do
def to_params(
%Content.Audio.TrainIsArriving{crowding_description: crowding_description} = audio
Expand Down
28 changes: 28 additions & 0 deletions lib/content/audio/train_is_boarding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule Content.Audio.TrainIsBoarding do
"""

require Logger
alias Content.Audio
alias Content.Message

@enforce_keys [:destination, :route_id, :track_number]
defstruct @enforce_keys ++ [:trip_id]
Expand All @@ -15,6 +17,32 @@ defmodule Content.Audio.TrainIsBoarding do
track_number: Content.Utilities.track_number()
}

def from_message(%Message.Predictions{} = message) do
if Audio.TrackChange.park_track_change?(message) do
[
%Audio.TrackChange{
destination: message.destination,
route_id: message.route_id,
berth: message.stop_id
}
]
else
[
%__MODULE__{
destination: message.destination,
trip_id: message.trip_id,
route_id: message.route_id,
track_number: Content.Utilities.stop_track_number(message.stop_id)
}
] ++
if message.station_code == "BBOW" && message.zone == "e" do
[%Audio.BoardingButton{}]
else
[]
end
end
end

defimpl Content.Audio do
@the_next "501"
@train_to "507"
Expand Down
12 changes: 9 additions & 3 deletions lib/content/message/stopped_train.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ defmodule Content.Message.StoppedTrain do
require Logger

@enforce_keys [:destination, :stops_away]
defstruct @enforce_keys ++ [:certainty, :stop_id]
defstruct @enforce_keys ++ [:certainty, :stop_id, :trip_id, :route_id, :direction_id]

@type t :: %__MODULE__{
destination: PaEss.destination(),
stops_away: non_neg_integer(),
certainty: non_neg_integer() | nil,
stop_id: String.t()
stop_id: String.t(),
trip_id: Predictions.Prediction.trip_id(),
route_id: String.t(),
direction_id: 0 | 1
}

@spec from_prediction(Predictions.Prediction.t()) :: t() | nil
Expand All @@ -36,7 +39,10 @@ defmodule Content.Message.StoppedTrain do
destination: destination,
stops_away: stops_away,
certainty: prediction.arrival_certainty || prediction.departure_certainty,
stop_id: prediction.stop_id
stop_id: prediction.stop_id,
trip_id: prediction.trip_id,
route_id: prediction.route_id,
direction_id: prediction.direction_id
}

{:error, _} ->
Expand Down
15 changes: 11 additions & 4 deletions lib/signs/realtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ defmodule Signs.Realtime do
announced_approachings: [],
announced_approachings_with_crowding: [],
announced_passthroughs: [],
announced_boardings: [],
announced_stalls: [],
announced_custom_text: nil,
announced_alert: false,
prev_prediction_keys: nil,
uses_shuttles: true
]

Expand Down Expand Up @@ -68,6 +73,11 @@ defmodule Signs.Realtime do
announced_approachings: [Predictions.Prediction.trip_id()],
announced_approachings_with_crowding: [Predictions.Prediction.trip_id()],
announced_passthroughs: [Predictions.Prediction.trip_id()],
announced_boardings: [Predictions.Prediction.trip_id()],
announced_stalls: [{Predictions.Prediction.trip_id(), non_neg_integer()}],
announced_custom_text: String.t() | nil,
prev_prediction_keys: [{String.t(), 0 | 1}] | nil,
announced_alert: boolean(),
uses_shuttles: boolean()
}

Expand Down Expand Up @@ -157,10 +167,7 @@ defmodule Signs.Realtime do
sign
|> announce_passthrough_trains(predictions)
|> Utilities.Updater.update_sign(new_top, new_bottom, current_time)
|> Utilities.Reader.do_interrupting_reads(
sign.current_content_top,
sign.current_content_bottom
)
|> Utilities.Reader.do_announcements()
|> Utilities.Reader.read_sign()
|> decrement_ticks()

Expand Down
Loading

0 comments on commit 78c953d

Please sign in to comment.