core

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ActorItemValueType = &itemValueType{"actor"}

ActorItemValueType represents an actor item value

View Source
var ItemValueType = &itemValueType{}

ItemValueType represents an item value

View Source
var SensorItemValueType = &itemValueType{"sensor"}

SensorItemValueType represents an sensor item value

Functions

func ActorItemValues added in v0.6.0

func ActorItemValues(b Bus, types ...api.Type) map[interface{}]string

ActorItemValues matching the given type or any item value if no type given

func Config added in v0.6.0

func Config() api.FieldMap

func ConfigRead

func ConfigRead(b Bus, key string) (interface{}, error)

ConfigRead returns the config value from the given key

func ConfigRemove

func ConfigRemove(b Bus, key string) error

ConfigRemove removes the given config key

func ConfigWrite

func ConfigWrite(b Bus, key string, value interface{}) error

ConfigWrite writes the given config value

func ItemAdd

func ItemAdd(b Bus, item Item) error

ItemAdd adds item to item service

func ItemRemove

func ItemRemove(b Bus, item Item) error

ItemRemove removes item to item service

func SensorItemValues added in v0.6.0

func SensorItemValues(b Bus, types ...api.Type) map[interface{}]string

SensorItemValues matching the given type or any item value if no type given

func SplitBusID added in v0.6.0

func SplitBusID(id string) (instance string, key string, action string)

SplitBusID into instance, key and action

Types

type APIPlugin

type APIPlugin interface {
	Plugin

	// APIEndpoints returns list of endpoints provided by plugin
	APIEndpoints() []PathEntry
}

APIPlugin provides API endpoints

type ActorItem

type ActorItem interface {
	ValuesItem

	// SetValues of actor
	SetValues(values map[string]interface{}) error
}

ActorItem provides the functionality to write values

func GetActorItem

func GetActorItem(item Item) (ActorItem, bool)

GetActorItem return the actor item if item is a actor

type ActorItemChecker

type ActorItemChecker interface {
	// IsActorItem returns true if item is a actor
	IsActorItem() bool
}

ActorItemChecker provides interface to check if actor item

type Bus

type Bus interface {
	// Start sends start event to all services
	Start() error

	// Shutdown bus and stop all services in reverse order of registration
	Shutdown()

	// RegisterService to bus instance
	RegisterService(service Service) error

	// HandleRequest forwards the request to the assigned service
	HandleRequest(request Request)

	// HandleRequestWait handles request and wait for response
	//
	// Note: This will overwrite the response channel if set
	HandleRequestWait(request Request, timeout time.Duration) Response

	// RegisterEventListener registers a new listener for the given key
	RegisterEventListener(id string, listener chan Event) error

	// UnregisterEventListener remove the registered listener
	UnregisterEventListener(id string, listener chan Event)
}

The Bus is the main communication interface between multiple components

func CreateBus

func CreateBus() Bus

CreateBus instance and prepare for usage

type ConfigPlugin

type ConfigPlugin interface {
	Plugin

	// Config returns the definition of the Config interface
	Config() *PluginConfigDef
}

ConfigPlugin provides generic configuration

type Event

type Event struct {
	// Identifier of event
	ID string

	Data map[string]interface{}
}

Event triggered from service

type EventItem

type EventItem interface {
	Item

	// Events returns all events that can be emitted
	Events() map[string]api.SimpleHandler

	// SetEventTrigger for item event
	SetEventTrigger(trigger func(action string, data map[string]interface{}))
}

EventItem will trigger events

func GetEventItem

func GetEventItem(item Item) (EventItem, bool)

GetEventItem return the event item if item is a event

type EventItemChecker

type EventItemChecker interface {
	// IsEventItem returns true if item is a event
	IsEventItem() bool
}

EventItemChecker provides interface to check if event item

type EventService

type EventService interface {
	Service

	// Events returns all events that can be emitted
	Events() map[string]api.SimpleHandler

	// SetEventTrigger for service
	SetEventTrigger(trigger EventTrigger)
}

EventService represent an endpoint that emit events

type EventTrigger

type EventTrigger func(id string, data map[string]interface{})

EventTrigger will trigger an event and forward to listener

type HTTPContext

type HTTPContext interface {
	// Request from HTTP client
	Request() *http.Request

	// Writer returns HTTP response object
	Writer() http.ResponseWriter

	// Next will execute pending handlers (inside current handler)
	Next()

	// # # # # # parse request # # # # #
	// GetHeader returns value from request headers
	GetHeader(key string) string

	// Header is a intelligent shortcut for c.Writer.Header().Set(key, value).
	// It writes a header in the response.
	// If value == "", this method removes the header `c.Writer.Header().Del(key)`
	Header(key, value string)

	// Param returns the value of the URL param.
	Param(key string) string

	// BodyValue returns value from body.
	BodyValue(key string) interface{}

	// PostForm returns the POST form data
	PostForm(key string) string

	// # # # # # request metadata # # # # #
	// Set store a key value pair for this context
	Set(key string, value interface{})

	// Get returns the value for the given key
	Get(key string) (value interface{}, exists bool)

	// MustGet returns the value for the given key or panics if not exist
	MustGet(key string) interface{}

	// # # # # # manipulate handling # # # # #
	// Abort prevents pending handlers from being called
	Abort()

	// IsAborted returns true if the current context was aborted.
	IsAborted() bool

	// AbortWithStatusJSON calls `Abort()` and then `JSON` internally.
	AbortWithStatusJSON(code int, jsonObj interface{})

	// # # # # # send response # # # # #
	// JSON serializes the given struct as JSON into the response body.
	// It also sets the Content-Type as "application/json".
	JSON(code int, obj interface{})

	// XML serializes the given struct as XML into the response body.
	// It also sets the Content-Type as "application/xml".
	XML(code int, obj interface{})

	// Status sets the HTTP response code.
	Status(code int)

	// Redirect redirect to location
	Redirect(code int, location string)

	// BusResponse writes a bus response to http context
	BusResponse(response Response)
}

HTTPContext provides an interface for handling of HTTP requests

type InitService

type InitService interface {
	Service

	// called if base initialization is done
	Init(bus Bus) error
}

InitService -> a service that handle init event

type Item

type Item interface {
	// Identifier of item
	ID() string

	// User friendly name of item
	Name() string
}

Item that can be handled by the item service

type ItemValue added in v0.6.0

type ItemValue struct {
	ID    string
	Value string
}

ItemValue represents a specific value from an item

func NewItemValue added in v0.6.0

func NewItemValue(value interface{}) (ItemValue, error)

NewItemValue from string

func (ItemValue) MarshalYAML added in v0.6.0

func (i ItemValue) MarshalYAML() (interface{}, error)

MarshalYAML converts struct to item value string

func (ItemValue) String added in v0.6.0

func (i ItemValue) String() string

String returns string representation of item value

type LoggerItem

type LoggerItem interface {
	Item

	// LogValues to persistent storage
	LogValues(id string, values map[string]interface{}) error
}

LoggerItem will log values to persistent storage

func GetLoggerItem

func GetLoggerItem(item Item) (LoggerItem, bool)

GetLoggerItem return the logger item if item is a logger

type LoggerItemChecker

type LoggerItemChecker interface {
	// IsLoggerItem returns true if item is a logger
	IsLoggerItem() bool
}

LoggerItemChecker provides interface to check if logger item

type PathEntry

type PathEntry struct {
	// Definition of entry (only used for endpoint)
	Def api.ExtendedHandler

	// Group of entry (only used for endpoint)
	Group string

	// Path to this entry
	Path string

	// Method that this entry will handle (only used for endpoint)
	Method string

	// FileSystem for handling of static files (can not be used with Handler and SubEntries)
	FileSystem http.FileSystem

	// Middleware to handle before endpoint
	Middleware []func(ctx HTTPContext)

	// Handler if this is an endpoint
	Handler func(ctx HTTPContext)

	// SubEntries under this one
	SubEntries []PathEntry
}

PathEntry defines part of http routing

func APIWrapper

func APIWrapper(b Bus, service RequestService, action string, entry PathEntry) PathEntry

APIWrapper creates a wrapper to forward an API request to the bus

type Plugin

type Plugin interface {
	// ID of plugin
	ID() string

	// Version information of plugin
	Version() string

	// Init plugin
	Init(b Bus) error
}

Plugin that provides functionality

type PluginConfig

type PluginConfig struct {
	// Configuration of current entry
	Config map[string]interface{}

	// configuration of sub entries
	Entries map[string]map[string]map[string]interface{}
}

PluginConfig stores current configuration

func (*PluginConfig) Dump

func (c *PluginConfig) Dump() map[string]interface{}

Dump configuration

type PluginConfigDef

type PluginConfigDef struct {
	// Global plugin configuration
	Config api.FieldMap `json:"config,omitempty"`

	// Called to load Config after startup
	LoadFunc func(config map[string]interface{}) error `json:"-"`
	// Called if Config was changed
	ChangeFunc func(config map[string]interface{}) error `json:"-"`

	// Map of sub entry configuration
	Entries PluginConfigEntryDefMap `json:"entries,omitempty"`
}

PluginConfigDef defines configuration structure of plugin

func (*PluginConfigDef) Load

func (d *PluginConfigDef) Load(data interface{}) (*PluginConfig, error)

Load configuration from raw data

func (*PluginConfigDef) Validate

func (d *PluginConfigDef) Validate() error

Validate if all required functions are provided

type PluginConfigEntryDef

type PluginConfigEntryDef struct {
	// Config of entry
	Config api.FieldMap `json:"config,omitempty"`
	// Order index of entry
	Order int

	// Called to load Config entries after startup
	LoadEntryFunc func(entries map[string]map[string]interface{}) error `json:"-"`
	// Called to add a new Config entry
	AddEntryFunc func(key string, config map[string]interface{}) error `json:"-"`
	// Called to change a Config entry (optional)
	ChangeEntryFunc func(key string, config map[string]interface{}) error `json:"-"`
	// Called to remove a Config entry
	RemoveEntryFunc func(key string) error `json:"-"`
}

PluginConfigEntryDef defines configuration structure of sub Config entries

func (*PluginConfigEntryDef) Load

func (d *PluginConfigEntryDef) Load(data interface{}) (map[string]map[string]interface{}, error)

Load configuration from raw data

func (*PluginConfigEntryDef) Validate

func (d *PluginConfigEntryDef) Validate() error

Validate if all required functions are provided

type PluginConfigEntryDefMap

type PluginConfigEntryDefMap map[string]PluginConfigEntryDef

PluginConfigEntryDefMap provides a sortable map of PluginConfigEntryDef

func (PluginConfigEntryDefMap) SortedKeys

func (d PluginConfigEntryDefMap) SortedKeys() []string

SortedKeys returns list with sorted map keys (uses Order if set)

type Request

type Request struct {
	// Identifier of request handler
	ID string

	// Parameters of action
	Parameters map[string]interface{}

	// Channel for response
	ResponseChannel chan Response
}

Request that is handled by a service

type RequestHandler

type RequestHandler struct {
	Def api.SimpleHandler

	// Function that handles the request
	Func func(map[string]interface{}) (map[string]interface{}, error)
}

RequestHandler will handle a request

type RequestService

type RequestService interface {
	Service

	// Handlers returns a map with all supported action and their handle functions
	Handlers() map[string]RequestHandler
}

RequestService represent an endpoint for the bus that handles received requests

type Response

type Response struct {
	// Original response
	OrgRequest Request

	// Error if not successful
	Error error

	Data map[string]interface{}
}

Response of a handled request

type Script

type Script interface {
	Start()
	Stop()
	Content() string
	IsRunning() bool
}

Script object

type ScriptDefinition

type ScriptDefinition struct {
	// modules available in script
	Modules map[string]ScriptModuleDefinition

	// function available outside packages
	GlobalFunctions map[string]ScriptFunction

	// global const values
	ConstantValues map[string]interface{}
}

ScriptDefinition defines the functionality of a script

type ScriptFunction

type ScriptFunction struct {
	Def  api.Function
	Func func(script Script, args []interface{}) []interface{}
}

ScriptFunction defines a function that can be called from script

type ScriptModule

type ScriptModule interface {
	Name() string
	Def() ScriptModuleDefinition
}

ScriptModule object

type ScriptModuleDefinition

type ScriptModuleDefinition struct {
	Description         string
	Functions           map[string]ScriptFunction
	CleanupFunc         func(script Script)
	CallbackChannelFunc func(script Script, c chan func())
}

ScriptModuleDefinition groups multiple functions

func (*ScriptModuleDefinition) FunctionDef

func (s *ScriptModuleDefinition) FunctionDef() map[string]api.Function

FunctionDef returns function definitions for module

type SensorItem

type SensorItem interface {
	ValuesItem

	// GetValues of sensor
	GetValues() map[string]interface{}
}

SensorItem provides the functionality to read values

func GetSensorItem

func GetSensorItem(item Item) (SensorItem, bool)

GetSensorItem return the sensor item if item is a sensor

type SensorItemChecker

type SensorItemChecker interface {
	// IsSensorItem returns true if item is a sensor
	IsSensorItem() bool
}

SensorItemChecker provides interface to check if sensor item

type SensorItemSingle

type SensorItemSingle interface {
	SensorItem

	// GetValue returns the given value
	GetValue(name string) interface{}
}

SensorItemSingle provides the functionality to read a specific value

If not set this will be provided by item service

type Service

type Service interface {
	// Key returns a unique identifier for this Service
	Key() string
}

Service that can be registered to a bus

type ShutdownPlugin

type ShutdownPlugin interface {
	Plugin

	// Shutdown called if plugin should be stopped
	Shutdown()
}

ShutdownPlugin handles shutdown event

type ShutdownService

type ShutdownService interface {
	Service

	// called if service should be stopped
	Shutdown(bus Bus)
}

ShutdownService -> a service that handle shutdown event

type SimplePathEntry

type SimplePathEntry struct {
	// Definition of entry
	Def api.SimpleHandler

	// Path to this entry
	Path string

	// Method that this entry will handle
	Method string

	// Handler for this entry
	Handler SimplePathEntryHandler
}

SimplePathEntry defines simpler path entry variant

func (SimplePathEntry) PathEntry

func (s SimplePathEntry) PathEntry() PathEntry

PathEntry created from simple entry

type SimplePathEntryHandler

type SimplePathEntryHandler func(param map[string]interface{}) (map[string]interface{}, error)

SimplePathEntryHandler handles simple path entries

type StartService

type StartService interface {
	Service

	// called if all plugins and services are initialized
	Start(bus Bus) error
}

StartService -> a service that handle start event

type TestBus

type TestBus struct {
	StartFunc                   func() error
	ShutdownFunc                func()
	RegisterServiceFunc         func(Service) error
	HandleRequestFunc           func(Request)
	HandleRequestWaitFunc       func(Request, time.Duration) Response
	RegisterEventListenerFunc   func(string, chan Event) error
	UnregisterEventListenerFunc func(string, chan Event)
}

TestBus implements a bus for units testing

func (*TestBus) HandleRequest

func (t *TestBus) HandleRequest(request Request)

HandleRequest forwards the request to the assigned service

func (*TestBus) HandleRequestWait

func (t *TestBus) HandleRequestWait(request Request, timeout time.Duration) Response

HandleRequestWait handles request and wait for response

func (*TestBus) RegisterEventListener

func (t *TestBus) RegisterEventListener(id string, listener chan Event) error

RegisterEventListener registers a new listener for the given key

func (*TestBus) RegisterService

func (t *TestBus) RegisterService(service Service) error

RegisterService to bus instance

func (*TestBus) Shutdown

func (t *TestBus) Shutdown()

Shutdown bus and stop all services in reverse order of registration

func (*TestBus) Start

func (t *TestBus) Start() error

Start sends start event to all services

func (*TestBus) UnregisterEventListener

func (t *TestBus) UnregisterEventListener(id string, listener chan Event)

UnregisterEventListener remove the registered listener

type UpdateIntervalService

type UpdateIntervalService interface {
	UpdateService

	// duration for update calls
	UpdateInterval() time.Duration
}

UpdateIntervalService -> defines a custom interval for update events

type UpdateService

type UpdateService interface {
	Service

	// called periodically (default: each minute)
	Update(bus Bus)
}

UpdateService -> a service that handle update events

type ValuesItem

type ValuesItem interface {
	Item

	// Get possible values type
	GetValueTypes() api.FieldMap
}

ValuesItem is used by all items that can read or write values

type WebPlugin

type WebPlugin interface {
	Plugin

	// WebEndpoints returns list of endpoints provided by plugin
	WebEndpoints() []PathEntry
}

WebPlugin provides web endpoints

Jump to

Keyboard shortcuts

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