2024-06-05 12:23:25 -04:00
|
|
|
defmodule Chronoscope.ViewActivator do
|
2024-06-04 20:03:02 -04:00
|
|
|
use GenServer
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
alias Chronoscope.NTS
|
|
|
|
alias Chronoscope.NTS.Parse
|
|
|
|
|
|
|
|
@nts_file Application.compile_env(:chronoscope, :nts_file)
|
|
|
|
|
|
|
|
def start_link(_) do
|
|
|
|
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def init(_) do
|
|
|
|
File.touch(@nts_file)
|
|
|
|
activate_nts_clients()
|
|
|
|
{:ok, %{nts_servers: activate_nts_clients()}}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info(:activate_clients, _state) do
|
|
|
|
{:noreply, %{nts_servers: activate_nts_clients()}}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_call(:get_nts_servers, _from, state) do
|
|
|
|
{:reply, state.nts_servers, state}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp activate_nts_clients() do
|
|
|
|
@nts_file
|
|
|
|
|> File.stream!()
|
|
|
|
|> Stream.map(&String.trim/1)
|
2024-06-05 13:45:01 -04:00
|
|
|
|> Stream.filter(&(&1 != ""))
|
2024-06-04 20:03:02 -04:00
|
|
|
|> Stream.map(&Parse.parse_nts_server/1)
|
|
|
|
|> Enum.to_list()
|
|
|
|
|> tap(fn server -> Enum.each(server, &NTS.auto_refresh/1) end)
|
|
|
|
end
|
|
|
|
end
|