Skip to content

Commit

Permalink
Merge pull request #3 from redwirelabs/use-port
Browse files Browse the repository at this point in the history
Use erlang term based port instead of a c-node
  • Loading branch information
abelino authored Oct 11, 2024
2 parents 2bf6d14 + 28b3a66 commit 62be087
Show file tree
Hide file tree
Showing 15 changed files with 442 additions and 400 deletions.
File renamed without changes.
71 changes: 63 additions & 8 deletions lib/bacnet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,73 @@ defmodule BACNet do
BACNet client.
"""

use GenServer

require Logger

defmodule Device do
@typedoc "Placeholder"
@type t :: term
defmodule State do
@doc false

defstruct [:port]
end

@doc """
Start the BACnet client.
"""
@spec start_link(any, GenServer.options()) :: GenServer.on_start()
def start_link(args, opts \\ []) do
GenServer.start_link(__MODULE__, args, opts)
end

@spec add_device(device :: Device.t) :: :ok | {:error, term}
def add_device(device) do
GenServer.call({:global, :bacnetd}, {:add_device, device})
@impl GenServer
def init(_args) do
bacnetd_exe = "#{:code.priv_dir(:bacnet)}/bacnetd"

port = Port.open(
{:spawn, bacnetd_exe},
[
:binary,
:use_stdio,
packet: 4,
env: [],
]
)

state = %State{port: port}

{:ok, state}
end

@doc false
defdelegate ei_log(level, term), to: Logger, as: :log
@impl GenServer
def handle_call(cmd, from, state) do
encoded_term = :erlang.term_to_binary({:"$gen_call", from, cmd})
Port.command(state.port, encoded_term)

{:noreply, state}
end

@impl GenServer
def handle_info({_port, {:data, data}}, state) do
try do
data
|> :erlang.binary_to_term
|> process_message
catch
_ -> nil
end

{:noreply, state}
end

defp process_message({:log, level, message}) do
Logger.log(level, message)
end

defp process_message({:"$gen_reply", to, result}) do
GenServer.reply(to, result)
end

defp process_message(unknown) do
Logger.warning("Unknown message received #{inspect(unknown)}")
end
end
24 changes: 0 additions & 24 deletions lib/bacnet/application.ex

This file was deleted.

4 changes: 1 addition & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defmodule BACNet.MixProject do
start_permanent: Mix.env() == :prod,
test_coverage: [tool: ExCoveralls, test_task: "espec"],
dialyzer: [
ignore_warnings: "dialyzer.ignore.exs",
list_unused_filters: true,
plt_file: {:no_warn, plt_file_path()},
],
Expand All @@ -33,7 +32,6 @@ defmodule BACNet.MixProject do
def application do
[
extra_applications: [:logger],
mod: {BACNet.Application, []}
]
end

Expand Down Expand Up @@ -71,7 +69,7 @@ defmodule BACNet.MixProject do

defp package do
[
licenses: ["Apache-2.0"],
licenses: ["MIT"],
links: %{"GitHub" => "https://github.com/redwirelabs/bacnet_ex"},
maintainers: ["Abelino Romo"],
files: [
Expand Down
31 changes: 0 additions & 31 deletions src/arg.c

This file was deleted.

13 changes: 0 additions & 13 deletions src/arg.h

This file was deleted.

120 changes: 0 additions & 120 deletions src/ei_client.c

This file was deleted.

12 changes: 0 additions & 12 deletions src/ei_client.h

This file was deleted.

52 changes: 0 additions & 52 deletions src/ei_log.c

This file was deleted.

26 changes: 0 additions & 26 deletions src/ei_log.h

This file was deleted.

Loading

0 comments on commit 62be087

Please sign in to comment.