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

unify no service audio code paths #844

Merged
merged 2 commits into from
Nov 14, 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
74 changes: 0 additions & 74 deletions lib/content/audio/closure.ex

This file was deleted.

25 changes: 9 additions & 16 deletions lib/content/audio/first_train_scheduled.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,17 @@ defmodule Content.Audio.FirstTrainScheduled do
%Content.Message.EarlyAm.DestinationTrain{destination: destination},
%Content.Message.EarlyAm.ScheduledTime{scheduled_time: scheduled_time}
) do
[
%__MODULE__{
destination: destination,
scheduled_time: scheduled_time
}
]
[%__MODULE__{destination: destination, scheduled_time: scheduled_time}]
end

def from_messages(%Content.Message.EarlyAm.DestinationScheduledTime{
destination: destination,
scheduled_time: scheduled_time
}) do
[
%__MODULE__{
destination: destination,
scheduled_time: scheduled_time
}
]
def from_messages(
%Content.Message.EarlyAm.DestinationScheduledTime{
destination: destination,
scheduled_time: scheduled_time
},
nil
) do
[%__MODULE__{destination: destination, scheduled_time: scheduled_time}]
end

defimpl Content.Audio do
Expand Down
105 changes: 105 additions & 0 deletions lib/content/audio/no_service.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
defmodule Content.Audio.NoService do
@enforce_keys [:destination, :route, :use_shuttle?]
defstruct @enforce_keys ++ [use_routes?: false]

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

@spec from_messages(Content.Message.t(), Content.Message.t()) :: [t()]
def from_messages(
%Content.Message.Alert.NoService{route: route, destination: destination},
%Content.Message.Alert.UseShuttleBus{}
) do
[%__MODULE__{route: route, destination: destination, use_shuttle?: true}]
end

def from_messages(
%Content.Message.Alert.NoService{route: route, destination: destination},
%Content.Message.Empty{}
) do
[%__MODULE__{route: route, destination: destination, use_shuttle?: false}]
end

def from_messages(
%Content.Message.Alert.NoService{route: route, destination: destination},
%Content.Message.Alert.UseRoutes{}
) do
[%__MODULE__{route: route, destination: destination, use_shuttle?: false, use_routes?: true}]
end

def from_messages(
%Content.Message.Alert.DestinationNoService{route: route, destination: destination},
nil
) do
[%__MODULE__{route: route, destination: destination, use_shuttle?: false}]
end

def from_messages(
%Content.Message.Alert.NoServiceUseShuttle{route: route, destination: destination},
nil
) do
[%__MODULE__{route: route, destination: destination, use_shuttle?: true}]
end

defimpl Content.Audio do
@there_is_no "861"
@service_at_this_station "863"

def to_params(%Content.Audio.NoService{use_routes?: true} = audio) do
{:ad_hoc, {tts_text(audio), :audio}}
end

def to_params(%Content.Audio.NoService{
destination: nil,
route: route,
use_shuttle?: use_shuttle?
}) do
line_var = PaEss.Utilities.line_to_var(route)

if use_shuttle? do
{:canned, {"199", [line_var], :audio}}
else
PaEss.Utilities.take_message([@there_is_no, line_var, @service_at_this_station], :audio)
end
end

def to_params(%Content.Audio.NoService{} = audio) do
{:ad_hoc, {tts_text(audio), :audio}}
end

def to_tts(%Content.Audio.NoService{} = audio) do
{tts_text(audio), nil}
end

def to_logs(%Content.Audio.NoService{}) do
[]
end

defp tts_text(%Content.Audio.NoService{
route: route,
destination: destination,
use_shuttle?: use_shuttle?,
use_routes?: use_routes?
}) do
shuttle = if(use_shuttle?, do: " Use shuttle.", else: "")

cond do
use_routes? ->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this clause not come first in the cond block? Based on the definition for from_messages/2 above for this special case, it looks like destination could still be truthy

Copy link
Collaborator Author

@panentheos panentheos Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently it doesn't matter, because we always generate a destination-less Message.Alert.NoService for this case. I've got a followup PR that changes that code path and this logic, but I went ahead and swapped it anyway.

# Hardcoded for Union Square
"No Train Service. Use routes 86, 87, or 91"

destination ->
{:ok, destination_text} = PaEss.Utilities.destination_to_ad_hoc_string(destination)
"No #{destination_text} service.#{shuttle}"

true ->
line = if(route, do: "#{route} Line", else: "train")
"There is no #{line} service at this station.#{shuttle}"
end
end
end
end
57 changes: 0 additions & 57 deletions lib/content/audio/no_service_to_destination.ex

This file was deleted.

25 changes: 7 additions & 18 deletions lib/content/audio/vehicles_to_destination.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,18 @@ defmodule Content.Audio.VehiclesToDestination do
route: String.t() | nil
}

def from_headway_message(
def from_messages(
%Content.Message.Headways.Top{destination: destination, route: route},
%Content.Message.Headways.Bottom{range: range}
) do
[
%__MODULE__{
destination: destination,
headway_range: range,
route: route
}
]
[%__MODULE__{destination: destination, headway_range: range, route: route}]
end

def from_paging_headway_message(%Content.Message.Headways.Paging{
destination: destination,
range: range
}) do
[
%__MODULE__{
destination: destination,
headway_range: range
}
]
def from_messages(
%Content.Message.Headways.Paging{destination: destination, route: route, range: range},
nil
) do
[%__MODULE__{destination: destination, headway_range: range, route: route}]
end

defimpl Content.Audio do
Expand Down
45 changes: 4 additions & 41 deletions lib/signs/utilities/audio.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,15 @@ defmodule Signs.Utilities.Audio do
end

defp get_passive_readout({:headway, top, bottom}) do
case top do
%Message.Headways.Top{} ->
Audio.VehiclesToDestination.from_headway_message(top, bottom)

%Message.Headways.Paging{} ->
Audio.VehiclesToDestination.from_paging_headway_message(top)
end
Audio.VehiclesToDestination.from_messages(top, bottom)
end

defp get_passive_readout({:scheduled_train, top, bottom}) do
case top do
%Message.EarlyAm.DestinationTrain{} ->
Audio.FirstTrainScheduled.from_messages(top, bottom)

%Message.EarlyAm.DestinationScheduledTime{} ->
Audio.FirstTrainScheduled.from_messages(top)
end
Audio.FirstTrainScheduled.from_messages(top, bottom)
end

defp get_passive_readout({:alert, top, bottom}) do
case top do
%Message.Alert.NoService{destination: nil} ->
Audio.Closure.from_messages(top, bottom)

%Message.Alert.NoService{} ->
Audio.NoServiceToDestination.from_messages(top, bottom)

%Message.Alert.DestinationNoService{} ->
Audio.NoServiceToDestination.from_message(top)

%Message.Alert.NoServiceUseShuttle{} ->
Audio.NoServiceToDestination.from_message(top)
end
Audio.NoService.from_messages(top, bottom)
end

defp get_passive_readout({:predictions, predictions}) do
Expand Down Expand Up @@ -151,20 +127,7 @@ defmodule Signs.Utilities.Audio do
defp get_alert_announcements({audios, sign}, items) do
case Enum.find(items, &match?({:alert, _, _}, &1)) do
{:alert, top, bottom} ->
new_audios =
case top do
%Message.Alert.NoService{destination: nil} ->
Audio.Closure.from_messages(top, bottom)

%Message.Alert.NoService{} ->
Audio.NoServiceToDestination.from_messages(top, bottom)

%Message.Alert.DestinationNoService{} ->
Audio.NoServiceToDestination.from_message(top)

%Message.Alert.NoServiceUseShuttle{} ->
Audio.NoServiceToDestination.from_message(top)
end
new_audios = Content.Audio.NoService.from_messages(top, bottom)

if !sign.announced_alert do
{audios ++ new_audios, %{sign | announced_alert: true}}
Expand Down
Loading
Loading