Skip to content

Commit

Permalink
Bees can now expose their state
Browse files Browse the repository at this point in the history
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}}
  • Loading branch information
muesli committed Apr 29, 2019
1 parent 38e045f commit 0cceec9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/resources/hives/hives_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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(),
}
Expand Down
1 change: 1 addition & 0 deletions bees/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
63 changes: 63 additions & 0 deletions bees/context.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
* Authors:
* Christian Muehlhaeuser <[email protected]>
*/

// 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)
}
7 changes: 7 additions & 0 deletions bees/descriptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions bees/factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0cceec9

Please sign in to comment.