node

package
v2.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2022 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSkipSync = fmt.Errorf("skipping device sync after RequestStateChange")

Functions

This section is empty.

Types

type Node

type Node struct {
	UUID     string
	Type     string
	Version  string
	Protocol string

	Client websocket.Websocket

	Config *models.Config
	X509   *x509.Certificate
	TLS    *tls.Certificate
	CA     *x509.CertPool

	Devices *devices.List
	// contains filtered or unexported fields
}

Node is the main struct.

func New

func New(t string) *Node

New returns a new Node.

func NewWithClient

func NewWithClient(client websocket.Websocket) *Node

NewWithClient returns a new Node with a custom websocket client.

func (*Node) AddOrUpdate

func (n *Node) AddOrUpdate(d *devices.Device)

AddOrUpdate adds or updates a device in our local device store and notifies the server about the new state of the device.

func (*Node) Connect

func (n *Node) Connect() error

Connect starts the node and makes connection to the server. Normally discovered using mdns but can be configured aswell.

func (*Node) GetDevice

func (n *Node) GetDevice(id string) *devices.Device

func (*Node) On

func (n *Node) On(what string, cb OnFunc)

On sets up a callback that is run when a message received with type what.

func (*Node) OnConfig

func (n *Node) OnConfig(cb OnFunc)

OnConfig is run when node receives updated configuration from the server.

func (*Node) OnRequestStateChange

func (n *Node) OnRequestStateChange(cb func(state devices.State, device *devices.Device) error)

OnRequestStateChange is run if we get a state-change request from the server to update our devices (for example we are requested to turn on a light).

func (*Node) OnShutdown

func (n *Node) OnShutdown(cb func())

OnShutdown registers a callback that is run before the server shuts down.

func (*Node) SetDeviceOnline added in v2.2.1

func (n *Node) SetDeviceOnline(id string, online bool)

func (*Node) Stop

func (n *Node) Stop()

Stop will shutdown the node similar to a SIGTERM.

func (*Node) Stopped

func (n *Node) Stopped() <-chan struct{}

Stopped is closed when the node is stopped by n.Stop or os signal.

func (*Node) Subscribe

func (n *Node) Subscribe(what ...string) error

Subscribe subscribes to a topic in the server.

func (*Node) SyncDevice

func (n *Node) SyncDevice(id string)

SyncDevice sync single device.

func (*Node) SyncDevices

func (n *Node) SyncDevices() error

SyncDevices notifies the server about the state of all our known devices.

func (*Node) UpdateState

func (n *Node) UpdateState(id string, newState devices.State)

UpdateState updates the new state on the node if if differs and sends update to server if there was a diff.

func (*Node) Wait

func (n *Node) Wait()

Wait for node to be done after shutdown.

func (*Node) WaitForFirstConfig

func (n *Node) WaitForFirstConfig() func() error

WaitForFirstConfig blocks until we receive the first config from server.

func (*Node) WaitForMessage

func (n *Node) WaitForMessage(msgType string, dst interface{}) error

WaitForMessage is a helper method to wait for a specific message type.

func (*Node) WriteMessage

func (n *Node) WriteMessage(msgType string, data interface{}) error

WriteMessage writes a message to the server over websocket client.

type OnFunc

type OnFunc func(json.RawMessage) error

OnFunc is used in all the callbacks.

Jump to

Keyboard shortcuts

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