View Source Jido.Sensor behaviour (Jido v1.0.0)

Defines the behavior and implementation for Sensors in the Jido system.

A Sensor is a GenServer that emits Signals on PubSub based on specific events and retains a configurable number of last values.

Usage

To define a new Sensor, use the Jido.Sensor behavior in your module:

defmodule MySensor do
  use Jido.Sensor,
    name: "my_sensor",
    description: "Monitors a specific metric",
    category: :monitoring,
    tags: [:example, :demo],
    vsn: "1.0.0",
    schema: [
      metric: [type: :string, required: true]
    ]

  @impl true
  def generate_signal(state) do
    # Your sensor logic here
    {:ok, Jido.Signal.new(%{
      source: "#{state.sensor.name}:#{state.id}",
      topic: "metric_update",
      payload: %{value: get_metric_value()},
      timestamp: DateTime.utc_now()
    })}
  end
end

Callbacks

Implementing modules can override the following callbacks:

Summary

Types

options()

@type options() :: [
  id: String.t(),
  topic: String.t(),
  heartbeat_interval: non_neg_integer(),
  pubsub: module(),
  retain_last: pos_integer()
]

t()

@type t() :: %Jido.Sensor{
  category: atom() | nil,
  description: String.t() | nil,
  name: String.t(),
  schema: NimbleOptions.t() | nil,
  tags: [atom()],
  vsn: String.t() | nil
}

Callbacks

before_publish(t, map)

@callback before_publish(Jido.Signal.t(), map()) ::
  {:ok, Jido.Signal.t()} | {:error, any()}

generate_signal(map)

@callback generate_signal(map()) :: {:ok, Jido.Signal.t()} | {:error, any()}

mount(map)

@callback mount(map()) :: {:ok, map()} | {:error, any()}

shutdown(map)

@callback shutdown(map()) :: {:ok, map()} | {:error, any()}