Jido.Sensor behaviour (Jido v1.1.0-rc)
View SourceDefines the behavior and implementation for Sensors in the Jido system.
A Sensor is a GenServer that emits Signals using a Jido.Bus 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.deliver_signal/1
: Generates a signal based on the current state.on_before_deliver/2
: Called before a signal is delivered.shutdown/1
: Called when the sensor is shutting down.
Summary
Types
@type options() :: [ id: String.t(), bus_name: atom(), stream_id: String.t(), retain_last: pos_integer() ]
Callbacks
@callback deliver_signal(map()) :: {:ok, Jido.Signal.t()} | {:error, any()}
@callback on_before_deliver(Jido.Signal.t(), map()) :: {:ok, Jido.Signal.t()} | {:error, any()}