Skip to content

Commit cd0e377

Browse files
committed
Added support for audio URLs for PA Messages
- Modified the `to_tts` to have an alternate type containing :url and value - In migrated SCUs, we now account for the new audio_url field and we download the file to pass through in cases where we have it
1 parent 8c081f2 commit cd0e377

File tree

5 files changed

+51
-17
lines changed

5 files changed

+51
-17
lines changed

lib/content/audio.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ defprotocol Content.Audio do
1616
@type language :: :english | :spanish
1717
@type value :: canned_message() | ad_hoc_message() | nil
1818
@type tts_value ::
19-
{audio :: String.t() | {:spanish, String.t()}, visual :: Content.Message.pages() | nil}
19+
{audio :: String.t() | {:spanish, String.t()} | {:url, String.t()},
20+
visual :: Content.Message.pages() | nil}
2021

2122
@doc "Converts an audio struct to the mid/vars params for the PA system"
2223
@spec to_params(Content.Audio.t()) :: value()

lib/engine/pa_messages.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ defmodule Engine.PaMessages do
107107
"id" => pa_id,
108108
"visual_text" => visual_text,
109109
"audio_text" => audio_text,
110+
"audio_url" => audio_url,
110111
"interval_in_minutes" => interval_in_minutes,
111112
"priority" => priority,
112113
"sign_ids" => sign_ids
@@ -115,6 +116,7 @@ defmodule Engine.PaMessages do
115116
id: pa_id,
116117
visual_text: visual_text,
117118
audio_text: audio_text,
119+
audio_url: audio_url,
118120
priority: priority,
119121
sign_ids: sign_ids,
120122
interval_in_ms: interval_in_minutes * @minute_in_ms

lib/fake/httpoison.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ defmodule Fake.HTTPoison do
135135
"alert_id" => nil,
136136
"audio_text" =>
137137
"This is an example of a PA message that will be played at an MBTA station",
138+
"audio_url" => nil,
138139
"days_of_week" => [1, 2, 3, 4, 5, 6, 7],
139140
"end_time" => "2033-12-05T23:53:23Z",
140141
"id" => 4,
@@ -153,6 +154,7 @@ defmodule Fake.HTTPoison do
153154
%{
154155
"alert_id" => nil,
155156
"audio_text" => "This is another PA message that will play at MBTA stations",
157+
"audio_url" => nil,
156158
"days_of_week" => [1, 2, 3, 4, 5, 6, 7],
157159
"end_time" => "2027-08-05T05:41:10Z",
158160
"id" => 5,
@@ -177,6 +179,7 @@ defmodule Fake.HTTPoison do
177179
%{
178180
"alert_id" => nil,
179181
"audio_text" => "This is another PA message that will play at MBTA stations",
182+
"audio_url" => nil,
180183
"days_of_week" => [1, 2, 3, 4, 5, 6, 7],
181184
"end_time" => "2027-08-05T05:41:10Z",
182185
"id" => 5,
@@ -202,6 +205,7 @@ defmodule Fake.HTTPoison do
202205
"alert_id" => nil,
203206
"audio_text" =>
204207
"This is an example of a PA message that will be played at an MBTA station",
208+
"audio_url" => nil,
205209
"days_of_week" => [1, 2, 3, 4, 5, 6, 7],
206210
"end_time" => "2033-12-05T23:53:23Z",
207211
"id" => 4,
@@ -220,6 +224,7 @@ defmodule Fake.HTTPoison do
220224
%{
221225
"alert_id" => nil,
222226
"audio_text" => "This is another PA message that will play at MBTA stations",
227+
"audio_url" => nil,
223228
"days_of_week" => [1, 2, 3, 4, 5, 6, 7],
224229
"end_time" => "2027-08-05T05:41:10Z",
225230
"id" => 5,

lib/pa_ess/updater.ex

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,31 @@ defmodule PaEss.Updater do
7777

7878
log_metas =
7979
Enum.zip([tts_audios, tags, log_metas])
80-
|> Enum.map(fn {{text, pages}, tag, log_meta} ->
81-
[
82-
sign_id: id,
83-
audio:
84-
case text do
85-
{:spanish, text} -> text
86-
text -> text
87-
end
88-
|> inspect(),
89-
visual: format_pages(pages) |> Jason.encode!(),
90-
tag: inspect(tag),
91-
legacy: !scu_migrated?
92-
] ++
80+
|> Enum.map(fn {{tts_audio, pages}, tag, log_meta} ->
81+
case tts_audio do
82+
{:url, url} ->
83+
[audio_url: url]
84+
85+
{:spanish, text} ->
86+
[audio: text]
87+
88+
text ->
89+
[audio: text]
90+
end ++
91+
[
92+
sign_id: id,
93+
tag: inspect(tag),
94+
legacy: !scu_migrated?,
95+
visual: format_pages(pages) |> Jason.encode!()
96+
] ++
9397
log_meta
9498
end)
9599

96100
if scu_migrated? do
97101
Task.Supervisor.start_child(PaEss.TaskSupervisor, fn ->
98102
files =
99-
Enum.map(tts_audios, fn {text, _} ->
100-
Task.async(fn -> fetch_tts(text) end)
103+
Enum.map(tts_audios, fn {tts_audio, _} ->
104+
Task.async(fn -> fetch_audio_file(tts_audio) end)
101105
end)
102106
|> Task.await_many()
103107

@@ -158,7 +162,16 @@ defmodule PaEss.Updater do
158162
}
159163
end
160164

161-
defp fetch_tts(text) do
165+
defp fetch_audio_file({:url, url}) do
166+
http_client = Application.get_env(:realtime_signs, :http_client)
167+
168+
case http_client.get(url) do
169+
{:ok, %HTTPoison.Response{status_code: status, body: body}} when status == 200 ->
170+
body
171+
end
172+
end
173+
174+
defp fetch_audio_file(text) do
162175
http_poster = Application.get_env(:realtime_signs, :http_poster_mod)
163176
watts_url = Application.get_env(:realtime_signs, :watts_url)
164177
watts_api_key = Application.get_env(:realtime_signs, :watts_api_key)

lib/pa_messages/pa_message.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule PaMessages.PaMessage do
22
defstruct id: nil,
33
visual_text: nil,
44
audio_text: nil,
5+
audio_url: nil,
56
priority: nil,
67
sign_ids: [],
78
interval_in_ms: nil
@@ -10,16 +11,28 @@ defmodule PaMessages.PaMessage do
1011
id: integer(),
1112
visual_text: String.t(),
1213
audio_text: String.t(),
14+
audio_url: String.t(),
1315
priority: integer(),
1416
sign_ids: [String.t()],
1517
interval_in_ms: non_neg_integer()
1618
}
1719

1820
defimpl Content.Audio do
21+
def to_params(%PaMessages.PaMessage{audio_url: audio_url}) when not is_nil(audio_url) do
22+
nil
23+
end
24+
1925
def to_params(%PaMessages.PaMessage{visual_text: visual_text}) do
2026
{:ad_hoc, {visual_text, :audio_visual}}
2127
end
2228

29+
def to_tts(
30+
%PaMessages.PaMessage{visual_text: visual_text, audio_url: audio_url},
31+
max_text_length
32+
) do
33+
{{:url, audio_url}, PaEss.Utilities.paginate_text(visual_text, max_text_length)}
34+
end
35+
2336
def to_tts(
2437
%PaMessages.PaMessage{visual_text: visual_text, audio_text: audio_text},
2538
max_text_length

0 commit comments

Comments
 (0)