Skip to content

Commit

Permalink
Add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
pojiro committed Nov 16, 2024
1 parent 855ab32 commit 30f7b9e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/modbuzz/rtu/client/receiver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Modbuzz.RTU.Client.Receiver do

alias Modbuzz.PDU
alias Modbuzz.RTU.ADU
alias Modbuzz.RTU.Log

@server_device_failure 0x04
@server_device_busy 0x06
Expand Down Expand Up @@ -78,7 +79,8 @@ defmodule Modbuzz.RTU.Client.Receiver do
# already responded
{:noreply, state}
else
# something wrong, treat as server failure
Log.error("RTU server didn't respond.")
# treat as server device failure
{:ok, req} = PDU.decode_request(adu.pdu)
err = PDU.to_error(req, @server_device_failure)
GenServer.reply(caller, {:error, err})
Expand Down Expand Up @@ -107,7 +109,8 @@ defmodule Modbuzz.RTU.Client.Receiver do
{:error, :binary_is_short} ->
{:noreply, %{state | binary: new_binary}}

{:error, %ADU{unit_id: unit_id, pdu: _pdu, crc_valid?: false}} ->
{:error, %ADU{unit_id: unit_id, pdu: _pdu, crc_valid?: false} = adu} ->
Log.warning("CRC error detected, #{inspect(adu)}.")
caller = Enum.fetch!(callers, unit_id)
if not is_nil(caller), do: GenServer.reply(caller, {:error, :crc_error})

Expand Down
4 changes: 4 additions & 0 deletions lib/modbuzz/rtu/log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ defmodule Modbuzz.RTU.Log do
Logger.error(what_happend <> why(reason) <> where(state))
end

def warning(what_happend) do
Logger.warning(what_happend)
end

def warning(what_happend, reason, state) do
Logger.warning(what_happend <> why(reason) <> where(state))
end
Expand Down
4 changes: 3 additions & 1 deletion lib/modbuzz/rtu/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Modbuzz.RTU.Server do

alias Modbuzz.PDU
alias Modbuzz.RTU.ADU
alias Modbuzz.RTU.Log

@spec start_link(keyword()) :: GenServer.on_start()
def start_link(args) when is_list(args) do
Expand Down Expand Up @@ -58,7 +59,8 @@ defmodule Modbuzz.RTU.Server do
{:error, :binary_is_short} ->
{:noreply, %{state | binary: new_binary}}

{:error, %ADU{unit_id: _unit_id, pdu: _pdu, crc_valid?: false}} ->
{:error, %ADU{unit_id: _unit_id, pdu: _pdu, crc_valid?: false} = adu} ->
Log.warning("CRC error detected, #{inspect(adu)}.")
# ignore request
{:noreply, %{state | binary: <<>>}}
end
Expand Down

0 comments on commit 30f7b9e

Please sign in to comment.