Skip to content

Commit

Permalink
Merge branch 'main' into pk/early-am-predictions-suppression
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulJKim committed Jul 26, 2023
2 parents f5eaa8e + be77d8c commit 97f5224
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 89 deletions.
9 changes: 1 addition & 8 deletions lib/content/audio/following_train.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,7 @@ defmodule Content.Audio.FollowingTrain do
def to_params(audio) do
case Utilities.destination_var(audio.destination) do
{:ok, dest_var} ->
if audio.destination in [
:southbound,
:northbound,
:westbound,
:eastbound,
:inbound,
:outbound
] do
if Utilities.directional_destination?(audio.destination) do
do_ad_hoc_message(audio)
else
green_line_branch = Content.Utilities.route_branch_letter(audio.route_id)
Expand Down
9 changes: 1 addition & 8 deletions lib/content/audio/next_train_countdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@ defmodule Content.Audio.NextTrainCountdown do
green_line_branch = Content.Utilities.route_branch_letter(audio.route_id)

cond do
audio.destination in [
:southbound,
:northbound,
:eastbound,
:westbound,
:inbound,
:outbound
] ->
Utilities.directional_destination?(audio.destination) ->
do_ad_hoc_message(audio)

!is_nil(audio.track_number) ->
Expand Down
9 changes: 1 addition & 8 deletions lib/content/audio/stopped_train.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,7 @@ defmodule Content.Audio.StoppedTrain do
def to_params(audio) do
case PaEss.Utilities.destination_var(audio.destination) do
{:ok, dest_var} ->
if audio.destination in [
:southbound,
:northbound,
:eastbound,
:westbound,
:inbound,
:outbound
] do
if Utilities.directional_destination?(audio.destination) do
do_ad_hoc_message(audio)
else
vars = [
Expand Down
9 changes: 1 addition & 8 deletions lib/content/audio/train_is_boarding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ defmodule Content.Audio.TrainIsBoarding do
def to_params(audio) do
case PaEss.Utilities.destination_var(audio.destination) do
{:ok, destination_var} ->
if audio.destination in [
:southbound,
:northbound,
:eastbound,
:westbound,
:inbound,
:outbound
] do
if PaEss.Utilities.directional_destination?(audio.destination) do
do_ad_hoc_message(audio)
else
do_to_params(audio, destination_var)
Expand Down
2 changes: 1 addition & 1 deletion lib/content/message/early_am/destination_scheduled_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Content.Message.EarlyAm.DestinationScheduledTime do
destination: destination,
scheduled_time: scheduled_time
}) do
"#{String.capitalize(PaEss.Utilities.destination_to_sign_string(destination))} due #{scheduled_time.hour}:#{Content.Utilities.format_minutes(scheduled_time.minute)}"
"#{String.capitalize(PaEss.Utilities.destination_to_sign_string(destination))} due #{Content.Utilities.render_datetime_as_time(scheduled_time)}"
end
end
end
2 changes: 1 addition & 1 deletion lib/content/message/early_am/scheduled_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Content.Message.EarlyAm.ScheduledTime do

defimpl Content.Message do
def to_string(%Content.Message.EarlyAm.ScheduledTime{scheduled_time: scheduled_time}) do
"due #{scheduled_time.hour}:#{Content.Utilities.format_minutes(scheduled_time.minute)}"
"due #{Content.Utilities.render_datetime_as_time(scheduled_time)}"
end
end
end
19 changes: 19 additions & 0 deletions lib/content/message/generic_paging.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Content.Message.GenericPaging do
@moduledoc """
Can be used to page between multiple full-page messages e.g. headways and early AM timestamp
"""
@enforce_keys [:messages]
defstruct @enforce_keys

@type t :: %__MODULE__{
messages: [Content.Message.t()]
}

defimpl Content.Message do
def to_string(%Content.Message.GenericPaging{messages: messages}) do
Enum.map(messages, fn message ->
{Content.Message.to_string(message), 6}
end)
end
end
end
5 changes: 3 additions & 2 deletions lib/content/message/platform_prediction_bottom.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
defmodule Content.Message.PlatformPredictionBottom do
defstruct [:stop_id, :minutes]
defstruct [:stop_id, :minutes, :destination]

@type t :: %__MODULE__{
stop_id: String.t(),
minutes: integer() | :boarding | :arriving | :approaching | :max_time
minutes: integer() | :boarding | :arriving | :approaching | :max_time,
destination: PaEss.destination()
}

defimpl Content.Message do
Expand Down
9 changes: 4 additions & 5 deletions lib/content/utilities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ defmodule Content.Utilities do
def destination_for_prediction("Red", 0, _), do: {:ok, :southbound}

def destination_for_prediction(_, 0, "70151"), do: {:ok, :kenmore}
def destination_for_prediction(_, 0, "71151"), do: {:ok, :kenmore}
def destination_for_prediction(_, 0, "70202"), do: {:ok, :government_center}
def destination_for_prediction(_, 0, "70201"), do: {:ok, :government_center}
def destination_for_prediction(_, 0, "70175"), do: {:ok, :reservoir}
Expand All @@ -70,6 +71,7 @@ defmodule Content.Utilities do
def destination_for_prediction(_, 1, "70200"), do: {:ok, :park_street}
def destination_for_prediction(_, 1, "71199"), do: {:ok, :park_street}
def destination_for_prediction(_, 1, "70150"), do: {:ok, :kenmore}
def destination_for_prediction(_, 1, "71150"), do: {:ok, :kenmore}
def destination_for_prediction(_, 1, "70174"), do: {:ok, :reservoir}

def destination_for_prediction(_, _, "Government Center-Brattle"), do: {:ok, :government_center}
Expand Down Expand Up @@ -106,9 +108,6 @@ defmodule Content.Utilities do
def route_branch_letter("Green-E"), do: :e
def route_branch_letter(_), do: nil

def format_minutes(minutes) do
if minutes < 10,
do: "0#{minutes}",
else: "#{minutes}"
end
def render_datetime_as_time(time),
do: Calendar.strftime(time, "%I:%M") |> String.replace_leading("0", "")
end
89 changes: 45 additions & 44 deletions lib/pa_ess/utilities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -354,50 +354,18 @@ defmodule PaEss.Utilities do
end
end

def directional_destination?(destination),
do: destination in [:eastbound, :westbound, :southbound, :northbound, :inbound, :outbound]

@spec ad_hoc_trip_description(PaEss.destination(), String.t() | nil) ::
{:ok, String.t()} | {:error, :unknown}
def ad_hoc_trip_description(destination, route_id \\ nil)

def ad_hoc_trip_description(destination, nil)
when destination in [:eastbound, :westbound, :southbound, :northbound, :inbound, :outbound] do
case destination_to_ad_hoc_string(destination) do
{:ok, destination_string} ->
{:ok, "#{destination_string} train"}

_ ->
{:error, :unknown}
end
end

def ad_hoc_trip_description(destination, route_id)
when destination == :eastbound and route_id in ["Green-B", "Green-C", "Green-D", "Green-E"] do
ad_hoc_trip_description(destination)
end

def ad_hoc_trip_description(destination, route_id)
when destination in [:eastbound, :westbound, :southbound, :northbound, :inbound, :outbound] do
case {destination_to_ad_hoc_string(destination), route_to_ad_hoc_string(route_id)} do
{{:ok, destination_string}, {:ok, route_string}} ->
{:ok, "#{destination_string} #{route_string} train"}

{{:ok, _destination_string}, {:error, :unknown}} ->
ad_hoc_trip_description(destination)

_ ->
{:error, :unknown}
end
end

def ad_hoc_trip_description(destination, nil) do
case destination_to_ad_hoc_string(destination) do
{:ok, destination_string} ->
{:ok, "train to #{destination_string}"}

_ ->
{:error, :unknown}
end
end

def ad_hoc_trip_description(destination, route_id)
when destination in [
:lechmere,
Expand All @@ -413,15 +381,48 @@ defmodule PaEss.Utilities do
end

def ad_hoc_trip_description(destination, route_id) do
case {destination_to_ad_hoc_string(destination), route_to_ad_hoc_string(route_id)} do
{{:ok, destination_string}, {:ok, route_string}} ->
{:ok, "#{route_string} train to #{destination_string}"}

{{:ok, _destination_string}, {:error, :unknown}} ->
ad_hoc_trip_description(destination)

_ ->
{:error, :unknown}
case {directional_destination?(destination), route_id} do
{true, nil} ->
case destination_to_ad_hoc_string(destination) do
{:ok, destination_string} ->
{:ok, "#{destination_string} train"}

_ ->
{:error, :unknown}
end

{true, route_id} ->
case {destination_to_ad_hoc_string(destination), route_to_ad_hoc_string(route_id)} do
{{:ok, destination_string}, {:ok, route_string}} ->
{:ok, "#{destination_string} #{route_string} train"}

{{:ok, _destination_string}, {:error, :unknown}} ->
ad_hoc_trip_description(destination)

_ ->
{:error, :unknown}
end

{false, nil} ->
case destination_to_ad_hoc_string(destination) do
{:ok, destination_string} ->
{:ok, "train to #{destination_string}"}

_ ->
{:error, :unknown}
end

{false, route_id} ->
case {destination_to_ad_hoc_string(destination), route_to_ad_hoc_string(route_id)} do
{{:ok, destination_string}, {:ok, route_string}} ->
{:ok, "#{route_string} train to #{destination_string}"}

{{:ok, _destination_string}, {:error, :unknown}} ->
ad_hoc_trip_description(destination)

_ ->
{:error, :unknown}
end
end
end

Expand Down
12 changes: 11 additions & 1 deletion lib/signs/realtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,17 @@ defmodule Signs.Realtime do
defp fetch_predictions(%{sources: sources}, state) do
Enum.flat_map(sources, fn source ->
state.prediction_engine.for_stop(source.stop_id, source.direction_id)
|> Enum.filter(&(source.routes == nil or &1.route_id in source.routes))
|> Enum.filter(fn prediction ->
if source.routes == nil or prediction.route_id in source.routes do
true
else
Logger.info(
"filter_prediction_by_route sign_id=#{state.id} stop_id=#{source.stop_id} direction_id=#{source.direction_id} route_id=#{prediction.route_id}"
)

false
end
end)
end)
end

Expand Down
21 changes: 18 additions & 3 deletions lib/signs/utilities/audio.ex
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,15 @@ defmodule Signs.Utilities.Audio do
_multi_source?
) do
if length(top_messages) != length(bottom_messages) do
Logger.warn(
Logger.error(
"message_to_audio_warning Utilities.Audio generic_paging_mismatch some audios will be dropped: #{inspect(top_messages)} #{inspect(bottom_messages)}"
)
end

Enum.zip(top_messages, bottom_messages)
|> Enum.map(fn {top, bottom} ->
|> Enum.flat_map(fn {top, bottom} ->
get_audio(top, bottom, false)
end)
|> List.flatten()
end

defp get_audio(
Expand All @@ -307,11 +306,19 @@ defmodule Signs.Utilities.Audio do
Audio.FirstTrainScheduled.from_messages(top, bottom)
end

# Get audio for JFK/UMass special case two-line platform prediction
defp get_audio(
%Message.Predictions{station_code: "RJFK"} = top_content,
%Message.PlatformPredictionBottom{},
multi_source?
) do
# When the JFK/UMass Mezzanine sign is paging between two full pages where
# one page is a prediction with platform information on the second line,
# we have to override the zone field in Signs.Utilities.Messages.get_messages()
# to avoid triggering the usual paging platform prediction. The audio readout
# should be read normally though with platform info, so we add the zone back in here.
#
# Additionally, the second parameter here for from_sign_content/3 is arbitrary in this case.
Audio.Predictions.from_sign_content(%{top_content | zone: "m"}, :bottom, multi_source?)
end

Expand Down Expand Up @@ -358,6 +365,14 @@ defmodule Signs.Utilities.Audio do
Audio.FirstTrainScheduled.from_messages(message)
end

defp get_audio_for_line(
%Message.EarlyAm.DestinationScheduledTime{} = message,
_line,
_multi_source?
) do
Audio.FirstTrainScheduled.from_messages(message)
end

defp get_audio_for_line(%Message.Empty{}, _line, _multi_source?) do
[]
end
Expand Down
Loading

0 comments on commit 97f5224

Please sign in to comment.