AutoApi.State behaviour (auto_api v13.2.0) View Source

State behaviour

Link to this section Summary

Functions

Clears a property from a state.

Link to this section Types

Link to this type

multiple_property(type)

View Source

Specs

multiple_property(type) :: [AutoApi.Property.t(type)]

Specs

property(type) :: AutoApi.Property.t(type) | nil

Specs

t() :: struct()

Link to this section Functions

Specs

clear(
  struct(),
  atom()
) :: struct()

Clears a property from a state.

If the property is multiple, all of its values will be removed.

Examples

iex> locks = [%AutoApi.Property{data: %{location: :front_left, lock_state: :locked}}]
iex> state = %AutoApi.DoorsState{locks: locks}
iex> AutoApi.State.clear(state, :locks)
%AutoApi.DoorsState{locks: []}

iex> state = %AutoApi.HoodState{position: %AutoApi.Property{data: :intermediate}}
iex> AutoApi.State.clear(state, :position)
%AutoApi.HoodState{position: nil}
Link to this function

put(state, property, property_component_or_params)

View Source

Specs

put(struct(), atom(), AutoApi.Property.t() | keyword() | map()) :: struct()

Sets the value of a property.

If the property is multiple the new value will be appended to the list of existing values.

The value can be passed either as an %AutoApi.Property{} struct, in which case it will be saved unchanged, or as a keyword list or map containing one of more of the components to be set. In this case a Property structure will be created.

Valid keys for the keyword list/map are:

  • :data
  • :timestamp
  • :failure
  • :availability

Examples

iex> state = %AutoApi.DiagnosticsState{}
iex> AutoApi.State.put(state, :odometer, data: %{value: 4325.4, unit: :miles}, timestamp: ~U[2020-10-28 13:45:56Z])
%AutoApi.DiagnosticsState{odometer: %AutoApi.Property{data: %{value: 4325.4, unit: :miles}, timestamp: ~U[2020-10-28 13:45:56Z]}}

iex> locks = [%AutoApi.Property{data: %{location: :front_left, lock_state: :locked}}]
iex> state = %AutoApi.DoorsState{locks: locks}
iex> new_value = %AutoApi.Property{data: %{location: :rear_right, lock_state: :unlocked}}
iex> AutoApi.State.put(state, :locks, new_value)
%AutoApi.DoorsState{locks: [
  %AutoApi.Property{data: %{location: :front_left, lock_state: :locked}},
  %AutoApi.Property{data: %{location: :rear_right, lock_state: :unlocked}}
]}

iex> failure = %{reason: :rate_limit, description: "Try again tomorrow"}
iex> availability = %{update_rate: :trip, rate_limit: %{value: 2, unit: :times_per_day}, applies_per: :app}
iex> state = %AutoApi.HoodState{}
iex> AutoApi.State.put(state, :position, %{failure: failure, availability: availability})
%AutoApi.HoodState{position: %AutoApi.Property{
  failure: %{reason: :rate_limit, description: "Try again tomorrow"},
  availability: %{update_rate: :trip, rate_limit: %{value: 2, unit: :times_per_day}, applies_per: :app}
}}

Link to this section Callbacks

Specs

base() :: struct()

Specs

from_bin(binary()) :: struct()

Specs

to_bin(struct()) :: binary()