Skip to content

Commit

Permalink
pass single route through messages (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
panentheos authored Oct 4, 2024
1 parent 8084379 commit 8e40add
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 133 deletions.
31 changes: 13 additions & 18 deletions lib/content/audio/closure.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ defmodule Content.Audio.Closure do
require Logger

@enforce_keys [:alert]
defstruct [routes: []] ++ @enforce_keys
defstruct @enforce_keys ++ [:route]

@type t :: %__MODULE__{
alert: :shuttles_closed_station | :suspension_closed_station,
routes: [String.t()]
route: String.t() | nil
}

@spec from_messages(Content.Message.t(), Content.Message.t()) :: [t()]
def from_messages(
%Content.Message.Alert.NoService{routes: routes},
%Content.Message.Alert.NoService{route: route},
%Content.Message.Alert.UseShuttleBus{}
) do
[%Content.Audio.Closure{alert: :shuttles_closed_station, routes: routes}]
[%Content.Audio.Closure{alert: :shuttles_closed_station, route: route}]
end

def from_messages(%Content.Message.Alert.NoService{routes: routes}, %Content.Message.Empty{}) do
[%Content.Audio.Closure{alert: :suspension_closed_station, routes: routes}]
def from_messages(%Content.Message.Alert.NoService{route: route}, %Content.Message.Empty{}) do
[%Content.Audio.Closure{alert: :suspension_closed_station, route: route}]
end

def from_messages(%Content.Message.Alert.NoService{}, %Content.Message.Alert.UseRoutes{}) do
Expand All @@ -38,22 +38,17 @@ defmodule Content.Audio.Closure do
@there_is_no "861"
@service_at_this_station "863"

def to_params(%Content.Audio.Closure{alert: :shuttles_closed_station, routes: routes}) do
line_var =
PaEss.Utilities.get_line_from_routes_list(routes) |> PaEss.Utilities.line_to_var()

{:canned, {"199", [line_var], :audio}}
def to_params(%Content.Audio.Closure{alert: :shuttles_closed_station, route: route}) do
{:canned, {"199", [PaEss.Utilities.line_to_var(route)], :audio}}
end

def to_params(%Content.Audio.Closure{alert: :suspension_closed_station, routes: routes}) do
line_var =
PaEss.Utilities.get_line_from_routes_list(routes) |> PaEss.Utilities.line_to_var()

def to_params(%Content.Audio.Closure{alert: :suspension_closed_station, route: route}) do
line_var = PaEss.Utilities.line_to_var(route)
PaEss.Utilities.take_message([@there_is_no, line_var, @service_at_this_station], :audio)
end

# Hardcoded for Union Square
def to_params(%Content.Audio.Closure{alert: :use_routes_alert, routes: []} = audio) do
def to_params(%Content.Audio.Closure{alert: :use_routes_alert, route: nil} = audio) do
{:ad_hoc, {tts_text(audio), :audio}}
end

Expand All @@ -65,13 +60,13 @@ defmodule Content.Audio.Closure do
[]
end

defp tts_text(%Content.Audio.Closure{} = audio) do
defp tts_text(%Content.Audio.Closure{route: route} = audio) do
if audio.alert == :use_routes_alert do
# Hardcoded for Union Square
"No Train Service. Use routes 86, 87, or 91"
else
shuttle = if(audio.alert == :shuttles_closed_station, do: " Use shuttle.", else: "")
line = PaEss.Utilities.get_line_from_routes_list(audio.routes)
line = if(route, do: "#{route} Line", else: "train")
"There is no #{line} service at this station.#{shuttle}"
end
end
Expand Down
20 changes: 8 additions & 12 deletions lib/content/audio/service_ended.ex
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
defmodule Content.Audio.ServiceEnded do
alias PaEss.Utilities
@enforce_keys [:location]
defstruct @enforce_keys ++ [:destination, :routes]
defstruct @enforce_keys ++ [:destination, :route]

@type location :: :platform | :station | :direction
@type t :: %__MODULE__{
destination: PaEss.destination(),
routes: [String.t()] | nil,
route: String.t() | nil,
location: location()
}

def from_message(%Content.Message.LastTrip.StationClosed{routes: routes}) do
[%__MODULE__{location: :station, routes: routes}]
def from_message(%Content.Message.LastTrip.StationClosed{route: route}) do
[%__MODULE__{location: :station, route: route}]
end

def from_message(%Content.Message.LastTrip.PlatformClosed{destination: destination}) do
Expand All @@ -27,12 +27,8 @@ defmodule Content.Audio.ServiceEnded do
# @station_closed "883"
@platform_closed "884"

def to_params(%Content.Audio.ServiceEnded{location: :station, routes: routes}) do
line_var =
routes
|> Utilities.get_line_from_routes_list()
|> Utilities.line_to_var()

def to_params(%Content.Audio.ServiceEnded{location: :station, route: route}) do
line_var = Utilities.line_to_var(route)
Utilities.take_message([line_var, @service_ended], :audio)
end

Expand Down Expand Up @@ -68,8 +64,8 @@ defmodule Content.Audio.ServiceEnded do
[]
end

defp tts_text(%Content.Audio.ServiceEnded{location: :station, routes: routes}) do
line = Utilities.get_line_from_routes_list(routes) |> String.capitalize()
defp tts_text(%Content.Audio.ServiceEnded{location: :station, route: route}) do
line = if(route, do: "#{route} line", else: "Train")
"#{line} service has ended for the night."
end

Expand Down
24 changes: 12 additions & 12 deletions lib/content/audio/vehicles_to_destination.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ defmodule Content.Audio.VehiclesToDestination do
alias PaEss.Utilities

@enforce_keys [:destination, :headway_range]
defstruct @enforce_keys ++ [:routes]
defstruct @enforce_keys ++ [:route]

@type t :: %__MODULE__{
destination: PaEss.destination() | nil,
headway_range: {non_neg_integer(), non_neg_integer()},
routes: [String.t()] | nil
route: String.t() | nil
}

def from_headway_message(
%Content.Message.Headways.Top{destination: destination, routes: routes},
%Content.Message.Headways.Top{destination: destination, route: route},
%Content.Message.Headways.Bottom{range: range}
) do
[
%__MODULE__{
destination: destination,
headway_range: range,
routes: routes
route: route
}
]
end
Expand All @@ -46,13 +46,13 @@ defmodule Content.Audio.VehiclesToDestination do
def to_params(
%Content.Audio.VehiclesToDestination{
headway_range: {range_low, range_high},
routes: routes
destination: destination
} = audio
) do
low_var = Utilities.number_var(range_low, :english)
high_var = Utilities.number_var(range_high, :english)

if low_var && high_var && !routes do
if low_var && high_var && destination do
{:canned, {message_id(audio), [low_var, high_var], :audio}}
else
{:ad_hoc, {tts_text(audio), :audio}}
Expand All @@ -70,16 +70,16 @@ defmodule Content.Audio.VehiclesToDestination do
defp tts_text(%Content.Audio.VehiclesToDestination{
headway_range: {range_low, range_high},
destination: destination,
routes: routes
route: route
}) do
trains =
case {destination, routes} do
{_, [route]} ->
"#{route} line trains"

{_, routes} when is_list(routes) ->
case {destination, route} do
{nil, nil} ->
"Trains"

{nil, route} ->
"#{route} line trains"

{destination, _} ->
{:ok, destination_text} = PaEss.Utilities.destination_to_ad_hoc_string(destination)
"#{destination_text} trains"
Expand Down
7 changes: 5 additions & 2 deletions lib/content/message/alert/destination_no_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ defmodule Content.Message.Alert.DestinationNoService do
"""

@enforce_keys [:destination]
defstruct @enforce_keys
defstruct @enforce_keys ++ [:route]

@type t :: %__MODULE__{}
@type t :: %__MODULE__{
destination: PaEss.destination(),
route: String.t() | nil
}

defimpl Content.Message do
@default_page_width 24
Expand Down
15 changes: 6 additions & 9 deletions lib/content/message/alert/no_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ defmodule Content.Message.Alert.NoService do
A message displayed when a station is closed due to shuttles or a suspension
"""

defstruct routes: []
defstruct [:route]

@type t :: %__MODULE__{}
@type t :: %__MODULE__{
route: String.t() | nil
}

defimpl Content.Message do
def to_string(%Content.Message.Alert.NoService{routes: routes}) do
service =
case PaEss.Utilities.get_line_from_routes_list(routes) do
"train" -> "train service"
line -> line
end

def to_string(%Content.Message.Alert.NoService{route: route}) do
service = if(route, do: "#{route} Line", else: "train service")
"No #{service}"
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/content/message/alert/no_service_use_shuttle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ defmodule Content.Message.Alert.NoServiceUseShuttle do
"""

@enforce_keys [:destination]
defstruct @enforce_keys
defstruct @enforce_keys ++ [:route]

@type t :: %__MODULE__{
destination: PaEss.destination()
destination: PaEss.destination(),
route: String.t() | nil
}

defimpl Content.Message do
Expand Down
6 changes: 4 additions & 2 deletions lib/content/message/headways/paging.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defmodule Content.Message.Headways.Paging do
defstruct [:destination, :range]
@enforce_keys [:destination, :range]
defstruct @enforce_keys ++ [:route]

@type t :: %__MODULE__{
destination: PaEss.destination() | nil,
range: {non_neg_integer(), non_neg_integer()}
range: {non_neg_integer(), non_neg_integer()},
route: String.t() | nil
}

defimpl Content.Message do
Expand Down
16 changes: 8 additions & 8 deletions lib/content/message/headways/top.ex
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
defmodule Content.Message.Headways.Top do
defstruct [:destination, :routes]
defstruct [:destination, :route]

@type t :: %__MODULE__{
destination: PaEss.destination() | nil,
routes: [String.t()] | nil
route: String.t() | nil
}

defimpl Content.Message do
def to_string(%Content.Message.Headways.Top{destination: nil, routes: ["Mattapan"]}) do
"Mattapan trains"
def to_string(%Content.Message.Headways.Top{destination: nil, route: nil}) do
"Trains"
end

def to_string(%Content.Message.Headways.Top{destination: nil, routes: [route]}) do
"#{route} line trains"
def to_string(%Content.Message.Headways.Top{destination: nil, route: "Mattapan"}) do
"Mattapan trains"
end

def to_string(%Content.Message.Headways.Top{destination: nil}) do
"Trains"
def to_string(%Content.Message.Headways.Top{destination: nil, route: route}) do
"#{route} line trains"
end

def to_string(%Content.Message.Headways.Top{destination: destination}) do
Expand Down
3 changes: 2 additions & 1 deletion lib/content/message/last_trip/no_service.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
defmodule Content.Message.LastTrip.NoService do
@enforce_keys [:destination, :line]
defstruct @enforce_keys
defstruct @enforce_keys ++ [:route]

@type t :: %__MODULE__{
destination: PaEss.destination(),
route: String.t() | nil,
line: :top | :bottom
}

Expand Down
9 changes: 3 additions & 6 deletions lib/content/message/last_trip/station_closed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ defmodule Content.Message.LastTrip.StationClosed do
A message displayed when a station is closed
"""
@enforce_keys []
defstruct @enforce_keys ++ [routes: []]
defstruct @enforce_keys ++ [:route]

@type t :: %__MODULE__{}

defimpl Content.Message do
def to_string(%Content.Message.LastTrip.StationClosed{routes: routes}) do
case PaEss.Utilities.get_line_from_routes_list(routes) do
"train" -> "Station closed"
line -> "No #{line}"
end
def to_string(%Content.Message.LastTrip.StationClosed{route: route}) do
if(route, do: "No #{route} Line", else: "Station closed")
end
end
end
30 changes: 5 additions & 25 deletions lib/pa_ess/utilities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -307,33 +307,13 @@ defmodule PaEss.Utilities do
def destination_to_ad_hoc_string(:medford_tufts), do: {:ok, "Medford/Tufts"}
def destination_to_ad_hoc_string(_unknown), do: {:error, :unknown}

def line_to_var("Red Line"), do: "3005"
def line_to_var("Orange Line"), do: "3006"
def line_to_var("Blue Line"), do: "3007"
def line_to_var("Green Line"), do: "3008"
def line_to_var("Mattapan Line"), do: "3009"
def line_to_var("Red"), do: "3005"
def line_to_var("Orange"), do: "3006"
def line_to_var("Blue"), do: "3007"
def line_to_var("Green"), do: "3008"
def line_to_var("Mattapan"), do: "3009"
def line_to_var(_), do: "864"

def get_unique_routes(routes) do
routes
|> Enum.map(fn route -> route |> String.split("-") |> List.first() end)
|> Enum.uniq()
end

def get_line_from_routes_list(routes) do
case get_unique_routes(routes) do
[route] ->
"#{route} Line"

_ ->
# Currently, the only case where there would be two fully distinct
# routes (disregarding GL Branches) is the Ashmont Mezzanine.
# Even in the Ashmont Mezzanine case though, we would page the Mattapan-specific
# shuttle alert on the second line and show Red line predictions on top.
"train"
end
end

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

Expand Down
11 changes: 4 additions & 7 deletions lib/signs/utilities/early_am_suppression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,9 @@ defmodule Signs.Utilities.EarlyAmSuppression do
{top, bottom}

match?({{%Headways.Top{}, _}, {%Headways.Top{}, _}}, {top_content, bottom_content}) ->
routes =
Signs.Utilities.SourceConfig.sign_routes(sign.source_config)
|> PaEss.Utilities.get_unique_routes()

route = Signs.Utilities.SourceConfig.single_route(sign.source_config)
{t1, t2} = top_content
{%{t1 | routes: routes, destination: nil}, t2}
{%{t1 | route: route, destination: nil}, t2}

true ->
paginate(top_content, bottom_content)
Expand Down Expand Up @@ -213,8 +210,8 @@ defmodule Signs.Utilities.EarlyAmSuppression do
{headway_top, %Headways.Bottom{range: {_, high}} = headway_bottom} ->
# Check against schedule to account for mezzanine sources having different first scheduled arrivals
if DateTime.add(current_time, high, :minute) |> DateTime.compare(scheduled) == :gt do
# Make sure routes is nil so that destination is used as headsign
{%{headway_top | destination: destination, routes: nil}, headway_bottom}
# Make sure route is nil so that destination is used as headsign
{%{headway_top | destination: destination, route: nil}, headway_bottom}
else
{%EarlyAm.DestinationTrain{
destination: destination
Expand Down
6 changes: 1 addition & 5 deletions lib/signs/utilities/headways.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ defmodule Signs.Utilities.Headways do

headways ->
if Signs.Utilities.SourceConfig.multi_source?(sign.source_config) do
{%Content.Message.Headways.Top{
routes:
SourceConfig.sign_routes(sign.source_config)
|> PaEss.Utilities.get_unique_routes()
},
{%Content.Message.Headways.Top{route: SourceConfig.single_route(sign.source_config)},
%Content.Message.Headways.Bottom{
range: {headways.range_low, headways.range_high}
}}
Expand Down
Loading

0 comments on commit 8e40add

Please sign in to comment.