astarte_device v0.11.3 Astarte.Device.Handler behaviour

This module defines a behaviour for handling incoming messages directed to an Astarte.Device.

Modules implementing this behaviour will be executed in a separate process, to avoid blocking the MQTT connection process.

Link to this section Summary

Callbacks

Handle incoming data from Astarte.

Initialize the state that will be passed as second argument to handle_message/2.

Link to this section Callbacks

Link to this callback

handle_message(message, state)

handle_message(message :: Astarte.Device.Handler.Message.t(), state :: term()) ::
  {:ok, new_state :: term()} | {:error, reason :: term()}

Handle incoming data from Astarte.

message is an %Astarte.Device.Handler.Message{}, which contains the following keys:

  • realm - the realm of the device.
  • device_id - the device id of the device.
  • interface_name - the interface name of the incoming message.
  • path_tokens - the path of the incoming message, split in a list of tokens (e.g. String.split(path, "/", trim: true)).
  • value - the value contained in the incoming message, already decoded to a standard Elixir type.
  • timestamp - if present, the timestamp contained in the incoming message, nil otherwise

state is the current state of the handler.

It's possible to return an updated state that will be passed to next handle_message/2 calls.

Link to this callback

init_state(args)

init_state(args :: term()) ::
  {:ok, state :: term()} | {:error, reason :: term()}

Initialize the state that will be passed as second argument to handle_message/2.

If this function returns {:error, reason}, the handler process is stopped with reason reason.