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
- Variables
- func Columns(service models.Service) []string
- func DecodeLocalEvent(payload []byte, service models.Service) (map[string]any, error)
- func LocalEventTopic(localDeviceId string, localServiceId string) string
- func MessageOf(values map[string]any) map[string]any
- func ServiceOf(d model.EventDescription) (service models.Service, err error)
- type CachedDeviceRepo
- func (this *CachedDeviceRepo) GetAspectNode(id string) (models.AspectNode, error)
- func (this *CachedDeviceRepo) GetAspectNodes(ids []string) ([]models.AspectNode, error)
- func (this *CachedDeviceRepo) GetCharacteristic(id string) (models.Characteristic, error)
- func (this *CachedDeviceRepo) GetConcept(id string) (models.Concept, error)
- func (this *CachedDeviceRepo) GetConceptIdOfFunction(id string) string
- func (this *CachedDeviceRepo) GetFunction(id string) (models.Function, error)
- type Consumer
- type Correlator
- type Describer
- type DeviceMessage
- type DeviceRepo
- type Evaluation
- type Evaluator
- type Imports
- type Marshaller
- type Message
- type MessageDesc
- type Row
- type Series
- type Timescale
- type Unmarshaller
- type Worker
- type WorkerConfig
Constants ¶
const LocalDataSegment = "data"
LocalDataSegment is the segment the payload of a local event is.
const LocalEventPrefix = "event/"
LocalEventPrefix is the topic prefix of device events on the broker of a gateway.
Variables ¶
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.
var ErrUnexpectedType = errors.New("unexpected type")
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 ¶
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 ¶
DecodeLocalEvent turns the payload of a local event into the message of the service.
func LocalEventTopic ¶
LocalEventTopic is the topic of a service of a device on the broker of a gateway.
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 ¶
Consumer reads topics until ctx is done; handle returning an error means the message is not acknowledged and read again.
func LocalConsumer ¶
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 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
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 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 ¶
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 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 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 ¶
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 ¶
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).
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