defmodule BACnet.Protocol.EventTransitionBits do # TODO: Docs # TODO: Throw argument error in encode if not valid alias BACnet.Protocol.ApplicationTags alias BACnet.Protocol.Constants require Constants @type t :: %__MODULE__{ to_offnormal: boolean(), to_fault: boolean(), to_normal: boolean() } @fields [:to_offnormal, :to_fault, :to_normal] @enforce_keys @fields defstruct @fields @doc """ Encodes a BACnet event transition bits into application tags encoding. """ @spec encode(t(), Keyword.t()) :: {:ok, ApplicationTags.encoding_list()} | {:error, term()} def encode(%__MODULE__{} = flags, _opts \\ []) do {:ok, [to_bitstring(flags)]} end @doc """ Creates from an application tag bitstring an event transition bits. """ @spec from_bitstring(tuple()) :: t() def from_bitstring(bitstring) when is_tuple(bitstring) and tuple_size(bitstring) == 3 do %__MODULE__{ to_offnormal: elem(bitstring, 0), to_fault: elem(bitstring, 1), to_normal: elem(bitstring, 2) } end @doc """ Parses a BACnet event transition bits from application tags encoding. """ @spec parse(ApplicationTags.encoding_list()) :: {:ok, {t(), rest :: ApplicationTags.encoding_list()}} | {:error, term()} def parse(tags) when is_list(tags) do case tags do [{:bitstring, {_b1, _b2, _b3} = bs} | rest] -> {:ok, {from_bitstring(bs), rest}} _else -> {:error, :invalid_tags} end end @doc """ Creates an application tag bitstring from an event transition bits. """ @spec to_bitstring(t()) :: BACnet.Protocol.ApplicationTags.primitive_encoding() def to_bitstring(%__MODULE__{} = t) do {:bitstring, {t.to_offnormal, t.to_fault, t.to_normal}} end @doc """ Validates whether the given event transition bits is in form valid. It only validates the struct is valid as per type specification. """ @spec valid?(t()) :: boolean() def valid?( %__MODULE__{ to_offnormal: to_offnormal, to_fault: to_fault, to_normal: to_normal } = _t ) when is_boolean(to_offnormal) and is_boolean(to_fault) and is_boolean(to_normal), do: true def valid?(%__MODULE__{} = _t), do: false end