From 0cceec97b52fc00a10e4c7ae5c4d1ea5c27b2098 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Mon, 29 Apr 2019 13:28:30 +0200 Subject: [PATCH] Bees can now expose their state This allows Bees to store a state independent from event parameters. Those states can be checked in filters or used as action parameters like other values. Just as examples, Bees could store whether they're currently connected, the latest sensor read-out, or the timestamp of the most recent sync as their states. In Filters you could then check their state: {{test eq .context.irc.connected true}} --- api/resources/hives/hives_response.go | 2 + bees/chains.go | 1 + bees/context.go | 63 +++++++++++++++++++++++++++ bees/descriptors.go | 7 +++ bees/factories.go | 7 +++ 5 files changed, 80 insertions(+) create mode 100644 bees/context.go diff --git a/api/resources/hives/hives_response.go b/api/resources/hives/hives_response.go index 19acd40a..2c3e4082 100644 --- a/api/resources/hives/hives_response.go +++ b/api/resources/hives/hives_response.go @@ -47,6 +47,7 @@ type HiveInfoResponse struct { Image string `json:"image"` LogoColor string `json:"logocolor"` Options []bees.BeeOptionDescriptor `json:"options"` + States []bees.StateDescriptor `json:"states"` Events []bees.EventDescriptor `json:"events"` Actions []bees.ActionDescriptor `json:"actions"` } @@ -102,6 +103,7 @@ func PrepareHiveResponse(ctx smolder.APIContext, hive *bees.BeeFactoryInterface) Image: u.String(), LogoColor: (*hive).LogoColor(), Options: (*hive).Options(), + States: (*hive).States(), Events: (*hive).Events(), Actions: (*hive).Actions(), } diff --git a/bees/chains.go b/bees/chains.go index 7a586992..6f1bd429 100644 --- a/bees/chains.go +++ b/bees/chains.go @@ -94,6 +94,7 @@ func execChains(event *Event) { for _, opt := range event.Options { m[opt.Name] = opt.Value } + ctx.FillMap(m) failed := false log.Println("Executing chain:", c.Name, "-", c.Description) diff --git a/bees/context.go b/bees/context.go new file mode 100644 index 00000000..15c6f111 --- /dev/null +++ b/bees/context.go @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2019 Christian Muehlhaeuser + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * Authors: + * Christian Muehlhaeuser + */ + +// Package bees is Beehive's central module system. +package bees + +var ( + ctx = NewContext() +) + +type Context struct { + state map[*Bee]map[string]interface{} +} + +func NewContext() *Context { + return &Context{ + state: make(map[*Bee]map[string]interface{}), + } +} + +func (c *Context) Set(bee *Bee, key string, value interface{}) { + if _, ok := c.state[bee]; !ok { + c.state[bee] = make(map[string]interface{}) + } + c.state[bee][key] = value +} + +func (c *Context) Value(bee *Bee, key string) interface{} { + return c.state[bee][key] +} + +func (c *Context) FillMap(m map[string]interface{}) { + cd := make(map[string]interface{}) + for bee, d := range c.state { + cd[bee.Name()] = d + } + m["context"] = cd +} + +func (bee *Bee) ContextSet(key string, value interface{}) { + ctx.Set(bee, key, value) +} + +func (bee *Bee) ContextValue(key string) interface{} { + return ctx.Value(bee, key) +} diff --git a/bees/descriptors.go b/bees/descriptors.go index e57e91b1..916e3974 100644 --- a/bees/descriptors.go +++ b/bees/descriptors.go @@ -54,6 +54,13 @@ type BeeOptionDescriptor struct { Mandatory bool } +// StateDescriptor describes a State provided by a Bee. +type StateDescriptor struct { + Name string + Description string + Type string +} + // GetActionDescriptor returns the ActionDescriptor matching an action. func GetActionDescriptor(action *Action) ActionDescriptor { bee := GetBee(action.Bee) diff --git a/bees/factories.go b/bees/factories.go index b5b716b6..83297908 100644 --- a/bees/factories.go +++ b/bees/factories.go @@ -51,6 +51,11 @@ func (factory *BeeFactory) Options() []BeeOptionDescriptor { return []BeeOptionDescriptor{} } +// States returns the default empty states set. +func (factory *BeeFactory) States() []StateDescriptor { + return []StateDescriptor{} +} + // Events returns the default empty events set. func (factory *BeeFactory) Events() []EventDescriptor { return []EventDescriptor{} @@ -79,6 +84,8 @@ type BeeFactoryInterface interface { // Options supported by module Options() []BeeOptionDescriptor + // States provided by module + States() []StateDescriptor // Events defined by module Events() []EventDescriptor // Actions supported by module