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:
mount/1
: Called when the sensor is initialized.generate_signal/1
: Generates a signal based on the current state.before_publish/2
: Called before a signal is published.shutdown/1
: Called when the sensor is shutting down.
Summary
Types
@type options() :: [ id: String.t(), topic: String.t(), heartbeat_interval: non_neg_integer(), pubsub: module(), retain_last: pos_integer() ]
Callbacks
@callback before_publish(Jido.Signal.t(), map()) :: {:ok, Jido.Signal.t()} | {:error, any()}
@callback generate_signal(map()) :: {:ok, Jido.Signal.t()} | {:error, any()}