Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions lib/bacnet/protocol/object_types/analog_input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule BACnet.Protocol.ObjectTypes.AnalogInput do
# TODO: Docs

alias BACnet.Protocol.Constants
alias BACnet.Protocol.Utility
require Constants
use BACnet.Protocol.ObjectsMacro

Expand Down Expand Up @@ -39,14 +40,7 @@ defmodule BACnet.Protocol.ObjectTypes.AnalogInput do
field(:present_value, float(),
required: true,
default: 0.0,
validator_fun: fn
value, %{min_present_value: min, max_present_value: max}
when not is_nil(min) and not is_nil(max) ->
min <= value and value <= max

_value, _obj ->
true
end
validator_fun: &Utility.float_validator_fun/2
)

field(:reliability, Constants.reliability(),
Expand Down
19 changes: 3 additions & 16 deletions lib/bacnet/protocol/object_types/analog_output.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule BACnet.Protocol.ObjectTypes.AnalogOutput do

alias BACnet.Protocol.Constants
alias BACnet.Protocol.PriorityArray
alias BACnet.Protocol.Utility

require Constants
use BACnet.Protocol.ObjectsMacro
Expand Down Expand Up @@ -47,29 +48,15 @@ defmodule BACnet.Protocol.ObjectTypes.AnalogOutput do
field(:present_value, float(),
required: true,
default: 0.0,
validator_fun: fn
value, %{min_present_value: min, max_present_value: max}
when not is_nil(min) and not is_nil(max) ->
min <= value and value <= max

_value, _obj ->
true
end
validator_fun: &Utility.float_validator_fun/2
)

field(:priority_array, PriorityArray.t(float()), required: true, readonly: true)

field(:relinquish_default, float(),
required: true,
default: 0.0,
validator_fun: fn
value, %{min_present_value: min, max_present_value: max}
when not is_nil(min) and not is_nil(max) ->
min <= value and value <= max

_value, _obj ->
true
end
validator_fun: &Utility.float_validator_fun/2
)

field(:reliability, Constants.reliability(),
Expand Down
19 changes: 3 additions & 16 deletions lib/bacnet/protocol/object_types/analog_value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule BACnet.Protocol.ObjectTypes.AnalogValue do

alias BACnet.Protocol.Constants
alias BACnet.Protocol.PriorityArray
alias BACnet.Protocol.Utility

require Constants
use BACnet.Protocol.ObjectsMacro
Expand Down Expand Up @@ -48,28 +49,14 @@ defmodule BACnet.Protocol.ObjectTypes.AnalogValue do
field(:present_value, float(),
required: true,
default: 0.0,
validator_fun: fn
value, %{min_present_value: min, max_present_value: max}
when not is_nil(min) and not is_nil(max) ->
min <= value and value <= max

_value, _obj ->
true
end
validator_fun: &Utility.float_validator_fun/2
)

field(:priority_array, PriorityArray.t(float()), readonly: true)

field(:relinquish_default, float(),
default: 0.0,
validator_fun: fn
value, %{min_present_value: min, max_present_value: max}
when not is_nil(min) and not is_nil(max) ->
min <= value and value <= max

_value, _obj ->
true
end
validator_fun: &Utility.float_validator_fun/2
)

field(:reliability, Constants.reliability(),
Expand Down
25 changes: 25 additions & 0 deletions lib/bacnet/protocol/utility.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,29 @@ defmodule BACnet.Protocol.Utility do
end
end
end

def float_validator_fun(value, %{min_present_value: min, max_present_value: max})
when not is_nil(min) and not is_nil(max) do
min_ok =
case min do
:NaN -> true
:inf -> false
:infn -> true
min when is_float(min) -> min <= value
end

max_ok =
case max do
:NaN -> true
:inf -> true
:infn -> false
max when is_float(max) -> value <= max
end

min_ok and max_ok
end

def float_validator_fun(_value, _obj) do
true
end
end