event

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdapterOfflineEvent

type AdapterOfflineEvent struct {
	Reason    string    `json:"reason,omitempty"`
	OfflineAt time.Time `json:"offline_at"`
}

AdapterOfflineEvent is emitted when the adapter goes offline.

type AdapterOnlineEvent

type AdapterOnlineEvent struct {
	OnlineAt time.Time `json:"online_at"`
}

AdapterOnlineEvent is emitted when the adapter comes online.

type Attribute

type Attribute struct {
	ID    uint16      `json:"id"`
	Value interface{} `json:"value"`
}

Attribute represents a single attribute value.

type Bus

type Bus struct {
	// contains filtered or unexported fields
}

Bus is a publish-subscribe event bus. It allows multiple components to subscribe to events and publish events asynchronously.

func NewBus

func NewBus(logger *slog.Logger) *Bus

NewBus creates a new event bus.

func (*Bus) Close

func (b *Bus) Close()

Close stops the event bus and closes all subscriber channels.

func (*Bus) Publish

func (b *Bus) Publish(ctx context.Context, evt Event) error

Publish sends an event to all matching subscribers. Returns an error if the context is canceled.

func (*Bus) PublishAdapterOffline

func (b *Bus) PublishAdapterOffline(ctx context.Context, reason string) error

PublishAdapterOffline publishes an AdapterOfflineEvent.

func (*Bus) PublishAdapterOnline

func (b *Bus) PublishAdapterOnline(ctx context.Context) error

PublishAdapterOnline publishes an AdapterOnlineEvent.

func (*Bus) PublishDeviceJoined

func (b *Bus) PublishDeviceJoined(ctx context.Context, event DeviceJoinedEvent) error

PublishDeviceJoined publishes a DeviceJoinedEvent.

func (*Bus) PublishDeviceLeft

func (b *Bus) PublishDeviceLeft(ctx context.Context, event DeviceLeftEvent) error

PublishDeviceLeft publishes a DeviceLeftEvent.

func (*Bus) PublishDeviceStateChange

func (b *Bus) PublishDeviceStateChange(ctx context.Context, event DeviceStateChangeEvent) error

PublishDeviceStateChange publishes a DeviceStateChangeEvent.

func (*Bus) PublishSensorReport

func (b *Bus) PublishSensorReport(ctx context.Context, event SensorReportEvent) error

PublishSensorReport publishes a SensorReportEvent.

func (*Bus) PublishSync

func (b *Bus) PublishSync(ctx context.Context, evt Event) (int, error)

PublishSync publishes an event synchronously, waiting for all subscribers to receive it. This is useful for critical events where you need to ensure delivery. Returns the number of subscribers that received the event.

func (*Bus) Subscribe

func (b *Bus) Subscribe(_ context.Context, filter func(Event) bool) (<-chan Event, func())

Subscribe returns a channel for receiving events. The channel is buffered and will be closed when the bus is closed or Unsubscribe is called.

The filter function is optional. If provided, only events matching the filter will be sent.

Example:

ch, unsubscribe := bus.Subscribe(nil, EventDeviceStateChange)
defer unsubscribe()
for evt := range ch {
    stateChange := evt.Data.(DeviceStateChangeEvent)
    // Handle event
}

func (*Bus) SubscribeTo

func (b *Bus) SubscribeTo(ctx context.Context, eventTypes ...Type) (<-chan Event, func())

SubscribeTo returns a channel for receiving specific event types.

func (*Bus) SubscribersCount

func (b *Bus) SubscribersCount() map[Type]int

SubscribersCount returns the number of active subscribers for each event type.

type DeviceJoinedEvent

type DeviceJoinedEvent struct {
	IEEEAddr string    `json:"ieee_addr"`
	NwkAddr  string    `json:"nwk_addr"`
	Name     string    `json:"name,omitempty"`
	Model    string    `json:"model,omitempty"`
	JoinedAt time.Time `json:"joined_at"`
}

DeviceJoinedEvent is emitted when a device joins the network.

type DeviceLeftEvent

type DeviceLeftEvent struct {
	IEEEAddr string    `json:"ieee_addr"`
	Name     string    `json:"name,omitempty"`
	LeftAt   time.Time `json:"left_at"`
}

DeviceLeftEvent is emitted when a device leaves the network.

type DeviceStateChangeEvent

type DeviceStateChangeEvent struct {
	IEEEAddr  string      `json:"ieee_addr"`
	Slug      string      `json:"slug,omitempty"`
	ClusterID uint16      `json:"cluster_id"`
	AttrID    uint16      `json:"attr_id"`
	OldValue  interface{} `json:"old_value,omitempty"`
	NewValue  interface{} `json:"new_value"`
	ChangedAt time.Time   `json:"changed_at"`
}

DeviceStateChangeEvent is emitted when a device's state changes.

type DeviceUpdatedEvent

type DeviceUpdatedEvent struct {
	IEEEAddr   string    `json:"ieee_addr"`
	Slug       string    `json:"slug,omitempty"`
	UpdateType string    `json:"update_type"` // "name", "endpoints", "capabilities"
	OldValue   string    `json:"old_value,omitempty"`
	NewValue   string    `json:"new_value,omitempty"`
	UpdatedAt  time.Time `json:"updated_at"`
}

DeviceUpdatedEvent is emitted when a device's metadata is updated.

type Event

type Event struct {
	Type      Type        `json:"type"`
	Timestamp time.Time   `json:"timestamp"`
	Data      interface{} `json:"data"`
}

Event represents a generic event.

func (*Event) String

func (e *Event) String() string

String returns a string representation of the event.

type NetworkChangedEvent

type NetworkChangedEvent struct {
	ChangeType string                 `json:"change_type"` // "formed", "reformed", "channel", "pan_id"
	Details    map[string]interface{} `json:"details,omitempty"`
	ChangedAt  time.Time              `json:"changed_at"`
}

NetworkChangedEvent is emitted when network configuration changes.

type SensorReportEvent

type SensorReportEvent struct {
	IEEEAddr   string      `json:"ieee_addr"`
	Slug       string      `json:"slug,omitempty"`
	ClusterID  uint16      `json:"cluster_id"`
	Attributes []Attribute `json:"attributes"`
	ReceivedAt time.Time   `json:"received_at"`
}

SensorReportEvent is emitted when a device sends sensor data.

type Type added in v0.4.0

type Type string

Type represents the type of event.

const (
	// DeviceLifecycle events
	EventDeviceJoined  Type = "device.joined"
	EventDeviceLeft    Type = "device.left"
	EventDeviceUpdated Type = "device.updated"

	// State events
	EventDeviceStateChange Type = "device.state_change"

	// Sensor events
	EventSensorReport Type = "sensor.report"

	// Adapter events
	EventAdapterOnline  Type = "adapter.online"
	EventAdapterOffline Type = "adapter.offline"

	// Network events
	EventNetworkChanged Type = "network.changed"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL