events

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 38 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package events derives event descriptions at deploy time and runs the event worker role, which evaluates device and import data and correlates matching events (EVT-01 to EVT-08).

Index

Constants

View Source
const LocalDataSegment = "data"

LocalDataSegment is the segment the payload of a local event is.

View Source
const LocalEventPrefix = "event/"

LocalEventPrefix is the topic prefix of device events on the broker of a gateway.

Variables

View Source
var ErrUnavailable = errors.New("device-repository unavailable")

ErrUnavailable marks an evaluation that failed because device-repository could not answer; the message is read again instead of notifying the owner, as the event-worker did.

View Source
var ErrUnexpectedType = errors.New("unexpected type")
View Source
var LocalProtocol = models.Protocol{
	Id:      "urn:infai:ses:protocol:f3a63aeb-187e-4dd9-9ef5-d97a6eb6292b",
	Name:    "standard-connector",
	Handler: "connector",
	ProtocolSegments: []models.ProtocolSegment{
		{Id: "urn:infai:ses:protocol-segment:9956d8b5-46fa-4381-a227-c1df69808997", Name: "metadata"},
		{Id: "urn:infai:ses:protocol-segment:0d211842-cef8-41ec-ab6b-9dbc31bc3a65", Name: "data"},
	},
}

LocalProtocol is the protocol of the device connectors of a gateway.

Functions

func Columns

func Columns(service models.Service) []string

Columns lists the columns timescale-tableworker creates for the outputs of a service: the names of the content variables joined by dots, lists of variable length as one column.

func DecodeLocalEvent

func DecodeLocalEvent(payload []byte, service models.Service) (map[string]any, error)

DecodeLocalEvent turns the payload of a local event into the message of the service.

func LocalEventTopic

func LocalEventTopic(localDeviceId string, localServiceId string) string

LocalEventTopic is the topic of a service of a device on the broker of a gateway.

func MessageOf

func MessageOf(values map[string]any) map[string]any

MessageOf rebuilds the value of a device message from the columns of a row, the input of the evaluation. Lists with fixed entries come back as maps keyed by entry name.

func ServiceOf

func ServiceOf(d model.EventDescription) (service models.Service, err error)

ServiceOf reads the service stored in an event description.

Types

type CachedDeviceRepo

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

CachedDeviceRepo reads characteristics, concepts, functions and aspect nodes with a client token and caches them; cache invalidation signals of service-commons clear the entries.

func NewCachedDeviceRepo

func NewCachedDeviceRepo(url string, token auth.TokenSource, duration time.Duration) (*CachedDeviceRepo, error)

func (*CachedDeviceRepo) GetAspectNode

func (this *CachedDeviceRepo) GetAspectNode(id string) (models.AspectNode, error)

func (*CachedDeviceRepo) GetAspectNodes

func (this *CachedDeviceRepo) GetAspectNodes(ids []string) ([]models.AspectNode, error)

GetAspectNodes reads the aspect nodes of several ids in one request; every id has to resolve, because criteria with an unknown aspect cannot be answered

func (*CachedDeviceRepo) GetCharacteristic

func (this *CachedDeviceRepo) GetCharacteristic(id string) (models.Characteristic, error)

func (*CachedDeviceRepo) GetConcept

func (this *CachedDeviceRepo) GetConcept(id string) (models.Concept, error)

GetConcept reads the concept with characteristic ids and conversions, which is what the marshaller needs, not with the characteristics themselves

func (*CachedDeviceRepo) GetConceptIdOfFunction

func (this *CachedDeviceRepo) GetConceptIdOfFunction(id string) string

GetConceptIdOfFunction is empty if the function cannot be read; the marshaller then reports the missing conversion

func (*CachedDeviceRepo) GetFunction

func (this *CachedDeviceRepo) GetFunction(id string) (models.Function, error)

type Consumer

type Consumer func(ctx context.Context, topics []string, handle func(Message) error) error

Consumer reads topics until ctx is done; handle returning an error means the message is not acknowledged and read again.

func LocalConsumer

func LocalConsumer(broker mqtt.Broker, qos byte, logger *slog.Logger) Consumer

LocalConsumer reads the device events of the broker of a gateway (FOG-06). It subscribes to all events once, like the event-worker in fog mode; messages without a description are dropped by the worker. A restart of the consumer only replaces the handler, so that no unsubscribe of the previous consumer can race with the subscription of the next.

MQTT has no offsets: a message gets a unique number, so that message ids stay apart (EVT-05). A message that cannot be processed is logged and acknowledged anyway, because an unacknowledged message would block the delivery of later ones.

type Correlator

type Correlator interface {
	Correlate(ctx context.Context, request engine.CorrelationRequest) (model.CorrelationResult, error)
	QueueNotification(ctx context.Context, userId string, namespace string, title string, message string) error
}

type Describer

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

Describer derives the event descriptions of a deployment version (DEP-07), following event-deployment v0.2.0: a device group becomes device × matching service, a device without service all its matching services, an import one description with an artificial service.

func NewDescriber

func NewDescriber(repo devices.Repository, aspects *devices.AspectNodes, imports Imports) *Describer

func (*Describer) Describe

func (this *Describer) Describe(deployment model.Deployment, version int, process *bpmn.Process, selections map[string]model.Selection) (result []model.EventDescription, groups []string, err error)

Describe returns the descriptions and the device groups they depend on.

func (*Describer) Local

func (this *Describer) Local() *Describer

Local makes the describer describe events for a gateway: the topic is event/{local device id}/{local service id}, and imports are not available.

type DeviceMessage

type DeviceMessage struct {
	DeviceId  string         `json:"device_id"`
	ServiceId string         `json:"service_id"`
	Value     map[string]any `json:"value"`
}

DeviceMessage is a message of platform-connector on a device data topic: the value of a service, by protocol segment, as the service describes it.

type DeviceRepo

type DeviceRepo interface {
	GetCharacteristic(id string) (characteristic models.Characteristic, err error)
	GetConcept(id string) (concept models.Concept, err error)
	GetConceptIdOfFunction(id string) string
	// GetAspectNode is used by the aspect distance sorting of the marshaller, which walks the
	// tree one node at a time
	GetAspectNode(id string) (models.AspectNode, error)
	GetAspectNodes(ids []string) ([]models.AspectNode, error)
}

DeviceRepo is the read access the unmarshalling needs to device-repository.

type Evaluation

type Evaluation struct {
	Value     any
	Triggered bool
	Logs      []string
}

type Evaluator

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

Evaluator applies the evaluation of the event-worker: unmarshalling, then the condition script with the value and the variables. The debug reconstruction uses the same code (EVT-10).

func NewEvaluator

func NewEvaluator(unmarshaller Unmarshaller) *Evaluator

func (*Evaluator) Evaluate

func (this *Evaluator) Evaluate(d model.EventDescription, message map[string]any) (result Evaluation, err error)

type Imports

type Imports interface {
	Instance(token devices.Token, id string) (models.Import, error)
	Type(token devices.Token, id string) (models.ImportType, error)
}

Imports reads import instances and types with the token of the deployment owner.

func NewImports

func NewImports(importDeployUrl string, importRepositoryUrl string) Imports

NewImports reads from import-deploy and import-repository.

type Marshaller

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

Marshaller converts device and import messages into the characteristic of an event as the event-worker did (EVT-04).

func NewMarshaller

func NewMarshaller(repo DeviceRepo) (*Marshaller, error)

func (*Marshaller) Unmarshal

func (this *Marshaller) Unmarshal(desc MessageDesc) (value any, err error)

type Message

type Message struct {
	Topic     string
	Partition int
	Offset    int64
	Value     []byte
	Time      time.Time
}

Message is a record of a device or import data topic.

type MessageDesc

type MessageDesc struct {
	Service          models.Service
	CharacteristicId string
	FunctionId       string
	AspectIds        []string
	// a path set at deploy time wins over the search by function and aspects
	Path    string
	Message map[string]any
}

MessageDesc is what the unmarshalling needs of an event description and the message it reads.

type Row

type Row struct {
	Time   time.Time
	Values map[string]any
}

Row is a stored message of a device service: its time and its values by column.

type Series

type Series interface {
	// Range returns rows after from and before to in ascending time, at most limit
	Range(ctx context.Context, token string, deviceId string, serviceId string, columns []string, from time.Time, to time.Time, limit int) ([]Row, error)
	// Last returns the newest row; nil if there is none
	Last(ctx context.Context, token string, deviceId string, serviceId string, columns []string) (*Row, error)
}

Series reads stored device messages, the source of the reconstruction of event evaluations (EVT-10) and of the current value of a waiting event (DBG-18).

type Timescale

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

Timescale reads rows through timescale-wrapper /queries with the token of the owner.

func NewTimescale

func NewTimescale(timescaleWrapperUrl string) *Timescale

func (*Timescale) Last

func (this *Timescale) Last(ctx context.Context, token string, deviceId string, serviceId string, columns []string) (*Row, error)

func (*Timescale) Range

func (this *Timescale) Range(ctx context.Context, token string, deviceId string, serviceId string, columns []string, from time.Time, to time.Time, limit int) ([]Row, error)

type Unmarshaller

type Unmarshaller interface {
	Unmarshal(desc MessageDesc) (value any, err error)
}

Unmarshaller converts a device or import message into the characteristic of the event; the Marshaller does this as the event-worker did (EVT-04).

type Worker

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

Worker is the event worker role: it holds the descriptions in memory, subscribes to exactly the topics they name, evaluates every message against the descriptions of its device and service or import, and correlates triggered events in the process (EVT-01 to EVT-08).

func NewWorker

func NewWorker(config WorkerConfig, s *store.Store, evaluator *Evaluator, correlator Correlator, consume Consumer, logger *slog.Logger) *Worker

func (*Worker) Handle

func (this *Worker) Handle(ctx context.Context, m Message) error

Handle evaluates a message against its descriptions; qos 1 descriptions are evaluated and correlated before the message is acknowledged, qos 0 ones afterwards (EVT-05).

func (*Worker) Run

func (this *Worker) Run(ctx context.Context)

Run syncs the descriptions and restarts the consumer when the set of topics changes.

func (*Worker) Sync

func (this *Worker) Sync(ctx context.Context) ([]string, error)

Sync reads descriptions changed since the last revision, and all of them periodically, so that a worker that missed purged tombstones recovers; it returns the topics to read.

type WorkerConfig

type WorkerConfig struct {
	// messages older than this are dropped (EVT-06)
	MaxMessageAge time.Duration
	// how often changed descriptions are read, and how often all of them
	SyncInterval       time.Duration
	FullReloadInterval time.Duration
	// at most one notification per description in this window (EVT-07)
	ErrorNotificationWindow time.Duration
	// concurrent evaluations of qos 0 descriptions
	AsyncWorkers int
	// on a gateway: descriptions carry the topic event/{local device}/{local service}, and
	// messages are the raw payload of the device (FOG-06)
	Local bool
}

func DefaultWorkerConfig

func DefaultWorkerConfig() WorkerConfig

Jump to

Keyboard shortcuts

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