Skip to content

Instantly share code, notes, and snippets.

@fhunleth
Last active September 6, 2024 13:11
Show Gist options
  • Save fhunleth/887a15b19d88eba95db8600a8b318f40 to your computer and use it in GitHub Desktop.
Save fhunleth/887a15b19d88eba95db8600a8b318f40 to your computer and use it in GitHub Desktop.
MNDP Discovery Script
#!/usr/bin/env elixir
# Run as host even if invoked with MIX_TARGET set
Mix.start()
Mix.target(:host)
Mix.install([
{:mndp, github: "kevinschweikert/mndp", start_applications: false},
{:owl, "~> 0.11.0"}
])
defmodule Discover do
use GenServer
def start_link(opts) do
GenServer.start_link(__MODULE__, :ok, opts)
end
@impl GenServer
def init(:ok) do
Owl.LiveScreen.add_block(:discovered,
state: :init,
render: fn
:init -> "Scanning for devices..."
_ -> MNDP.list_discovered() |> MNDP.Render.to_owl_table()
end
)
{:ok, [], {:continue, :subscribe}}
end
@impl GenServer
def handle_continue(:subscribe, state) do
MNDP.subscribe()
{:noreply, state}
end
@impl GenServer
def handle_info({:mndp, _mndp}, socket) do
Owl.LiveScreen.update(:discovered, :update)
{:noreply, socket}
end
def run() do
Logger.configure(level: :info)
Registry.start_link(keys: :duplicate, name: MNDP.Subscribers)
MNDP.Listener.start_link([])
IO.puts("""
Press enter to end
""")
start_link([])
_ = IO.gets("")
:ok
end
end
Discover.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment