-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Trigger PA Messages from RTS * Make logs errors and remove something used for testing * Address PR feedback * Put active messages path only in application env * fix config * Remove extraneous config * fix tests * Trigger PA Messages from RTS * Make logs errors and remove something used for testing * Address PR feedback * Put active messages path only in application env * fix config * Remove extraneous config * fix tests * try adding stubs * add TTS expects * Address PR feedback
- Loading branch information
Showing
13 changed files
with
490 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
defmodule Engine.PaMessages do | ||
use GenServer | ||
require Logger | ||
|
||
alias PaMessages.PaMessage | ||
|
||
@type state :: %{ | ||
pa_messages_last_sent: %{non_neg_integer() => DateTime.t()} | ||
} | ||
|
||
@minute_in_ms 1000 * 60 | ||
|
||
def start_link([]) do | ||
GenServer.start_link(__MODULE__, [], name: __MODULE__) | ||
end | ||
|
||
@impl true | ||
def init(_) do | ||
# Add some delay to wait for sign processes to come up before sending PA messages | ||
Process.send_after(self(), :update, 10000) | ||
{:ok, %{pa_messages_last_sent: %{}}} | ||
end | ||
|
||
@impl true | ||
def handle_info(:update, state) do | ||
schedule_update(self()) | ||
|
||
state = | ||
case get_active_pa_messages() do | ||
{:ok, pa_messages} -> | ||
recent_sends = send_pa_messages(pa_messages, state.pa_messages_last_sent) | ||
|
||
%{state | pa_messages_last_sent: recent_sends} | ||
|
||
{:error, %HTTPoison.Response{status_code: status_code, body: body}} -> | ||
Logger.error("pa_messages_response_error: status_code=#{status_code} body=#{body}") | ||
state | ||
|
||
{:error, %HTTPoison.Error{reason: reason}} -> | ||
Logger.error("pa_messages_response_error: reason=#{reason}") | ||
state | ||
|
||
{:error, error} -> | ||
Logger.error("pa_messages_response_error: error=#{inspect(error)}") | ||
state | ||
end | ||
|
||
{:noreply, state} | ||
end | ||
|
||
defp send_pa_messages(pa_messages, pa_messages_last_sent) do | ||
for %{ | ||
"id" => pa_id, | ||
"visual_text" => visual_text, | ||
"audio_text" => audio_text, | ||
"interval_in_minutes" => interval_in_minutes, | ||
"priority" => priority, | ||
"sign_ids" => sign_ids | ||
} <- pa_messages, | ||
into: %{} do | ||
active_pa_message = %PaMessage{ | ||
id: pa_id, | ||
visual_text: visual_text, | ||
audio_text: audio_text, | ||
priority: priority, | ||
sign_ids: sign_ids, | ||
interval_in_ms: interval_in_minutes * @minute_in_ms | ||
} | ||
|
||
{_, last_sent_time} = Map.get(pa_messages_last_sent, pa_id, {nil, nil}) | ||
|
||
time_since_last_send = | ||
if last_sent_time, | ||
do: DateTime.diff(DateTime.utc_now(), last_sent_time, :millisecond), | ||
else: active_pa_message.interval_in_ms | ||
|
||
if time_since_last_send >= active_pa_message.interval_in_ms do | ||
send_pa_message(active_pa_message) | ||
{pa_id, {active_pa_message, DateTime.utc_now()}} | ||
else | ||
{pa_id, {active_pa_message, last_sent_time}} | ||
end | ||
end | ||
end | ||
|
||
defp send_pa_message(pa_message) do | ||
Enum.each(pa_message.sign_ids, fn sign_id -> | ||
send( | ||
String.to_existing_atom("Signs/#{sign_id}"), | ||
{:play_pa_message, pa_message} | ||
) | ||
end) | ||
end | ||
|
||
defp get_active_pa_messages() do | ||
active_pa_messages_url = | ||
Application.get_env(:realtime_signs, :screenplay_base_url) <> | ||
Application.get_env(:realtime_signs, :active_pa_messages_path) | ||
|
||
http_client = Application.get_env(:realtime_signs, :http_client) | ||
|
||
with {:ok, response} <- | ||
http_client.get( | ||
active_pa_messages_url, | ||
[ | ||
{"x-api-key", Application.get_env(:realtime_signs, :screenplay_api_key)} | ||
], | ||
timeout: 2000, | ||
recv_timeout: 2000 | ||
), | ||
%{status_code: 200, body: body} <- response, | ||
{:ok, pa_messages} <- Jason.decode(body) do | ||
{:ok, pa_messages} | ||
else | ||
error -> | ||
{:error, error} | ||
end | ||
end | ||
|
||
defp schedule_update(pid) do | ||
Process.send_after(pid, :update, 1_000) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule PaMessages.PaMessage do | ||
defstruct id: nil, | ||
visual_text: nil, | ||
audio_text: nil, | ||
priority: nil, | ||
sign_ids: [], | ||
interval_in_ms: nil | ||
|
||
@type t :: %__MODULE__{ | ||
id: integer(), | ||
visual_text: String.t(), | ||
audio_text: String.t(), | ||
priority: integer(), | ||
sign_ids: [String.t()], | ||
interval_in_ms: non_neg_integer() | ||
} | ||
|
||
defimpl Content.Audio do | ||
def to_params(%PaMessages.PaMessage{visual_text: visual_text}) do | ||
{:ad_hoc, {visual_text, :audio_visual}} | ||
end | ||
|
||
def to_tts(%PaMessages.PaMessage{visual_text: visual_text, audio_text: audio_text}) do | ||
{audio_text, PaEss.Utilities.paginate_text(visual_text)} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.