node

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	MetadataRealism        = "scos.playground.realism"
	MetadataRealismVirtual = "virtual"
	MetadataRealismModel   = "model"

	MetadataDeviceType = "scos.playground.device-type"
)

Variables

View Source
var AutoTraitMetadata = map[string]string{
	MetadataRealism: MetadataRealismModel,
}
View Source
var MetadataTraitNotSupported = errors.New("metadata is not supported")

Functions

This section is empty.

Types

type Announcer

type Announcer interface {
	// Announce signals that the named device has the given features.
	Announce(name string, features ...Feature)
}

Announcer defines the Announce method. Calling Announce signals that a given named device has the collection of features provided.

type ClientFactory

type ClientFactory func(conn *grpc.ClientConn) interface{}

ClientFactory is a function that creates a new instance of a trait client.

type Feature

type Feature interface {
	// contains filtered or unexported methods
}

Feature describes some aspect of a named device.

func HasClient

func HasClient(clients ...interface{}) Feature

HasClient indicates that the device has additional apis that aren't tied directly to a trait.

func HasSimulation

func HasSimulation(s scrub.Scrubber) Feature

HasSimulation indicates that the device can be scrubbed using the given scrub.Scrubber.

func HasTrait

func HasTrait(name trait.Name, opt ...TraitOption) Feature

HasTrait indicates that the device implements the named trait.

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node represents a unit of control for a smart core server. Each node has collection of supported apis, represented by router.Router instances. When new devices are created they should call Announce and with the features relevant to the device.

Example
package main

import (
	"github.com/smart-core-os/sc-api/go/traits"
	"github.com/smart-core-os/sc-golang/pkg/trait"
	"github.com/smart-core-os/sc-golang/pkg/trait/light"
	"github.com/smart-core-os/sc-golang/pkg/trait/onoff"
	"google.golang.org/grpc"
)

func main() {
	n := New("my-server")

	// add supported apis
	n.AddRouter(
		onoff.NewApiRouter(),
		light.NewApiRouter(),
	)

	// announce devices
	n.Announce(
		"my-device",
		HasClient(""),
		HasTrait(trait.OnOff, WithClients(onOffClient)),
		HasTrait(trait.Light, WithClients(lightClient)),
	)

	// register our node with the grpc server
	n.Register(grpcServer)
}

var (
	grpcServer  *grpc.Server
	onOffClient traits.OnOffApiClient
	lightClient traits.LightApiClient
)
Output:

func New

func New(name string) *Node

New creates a new Node with the given device name.

func (*Node) AddClientFactory

func (n *Node) AddClientFactory(traitName trait.Name, f ClientFactory)

AddClientFactory registers a client factory for the given trait name with this node.

func (*Node) AddRouter

func (n *Node) AddRouter(r ...router.Router)

AddRouter adds a router.Router to the published API of this node. AddRouter should not be called after Register is called.

func (*Node) AddTraitFactory

func (n *Node) AddTraitFactory(traitName trait.Name, f TraitFactory)

AddTraitFactory registers a trait factory for the given trait name with this node.

func (*Node) Announce

func (n *Node) Announce(name string, features ...Feature)

Announce adds a new device with the given features to this node. You may call Announce with the same name as a known device to add additional features, for example new traits.

func (*Node) AnnounceOnRouterChange

func (n *Node) AnnounceOnRouterChange(name trait.Name, opts ...TraitOption) router.Option

AnnounceOnRouterChange returns a router.Option that announces auto-created router changes with this node.

func (*Node) CreateDeviceTrait

func (n *Node) CreateDeviceTrait(deviceName string, traitName trait.Name, config proto.Message) error

CreateDeviceTrait creates a new virtual trait for the given device.

func (*Node) CreateTraitClient

func (n *Node) CreateTraitClient(traitName trait.Name, conn *grpc.ClientConn) (interface{}, error)

CreateTraitClient creates an instance of an ApiClient, i.e. traits.OnOffApiClient based on a trait name.

func (*Node) Name

func (n *Node) Name() string

Name returns the device name for this node, how this node refers to itself.

func (*Node) Register

func (n *Node) Register(s *grpc.Server)

Register implements server.GrpcApi and registers all supported routers with s.

func (*Node) ResolveRemoteConn

func (n *Node) ResolveRemoteConn(ctx context.Context, endpoint string, opts ...RemoteOption) (*grpc.ClientConn, error)

func (*Node) Scrub

func (n *Node) Scrub(t time.Time) error

Scrub implements the scrub.Scrubber interface and calls Scrub on each device with a simulation feature.

func (*Node) SupportedDeviceTraits

func (n *Node) SupportedDeviceTraits() []trait.Name

func (*Node) SupportedRemoteTraits

func (n *Node) SupportedRemoteTraits() []trait.Name

type RemoteOption

type RemoteOption func(n *remoteNode)

func WithRemoteInsecure

func WithRemoteInsecure() RemoteOption

func WithRemoteServerCA

func WithRemoteServerCA(ca []byte) RemoteOption

type TraitFactory

type TraitFactory func(name string, config proto.Message) error

TraitFactory is a function that creates a new instance of a trait with the given name and config.

type TraitOption

type TraitOption func(t *traitFeature)

TraitOption controls how a Node behaves when presented with a new device trait.

func NoAddChildTrait

func NoAddChildTrait() TraitOption

NoAddChildTrait instructs the Node not to add the trait to the nodes parent.Model.

func NoAddMetadata

func NoAddMetadata() TraitOption

NoAddMetadata instructs the Node not to add the trait to the nodes traits.Metadata.

func WithClients

func WithClients(client ...interface{}) TraitOption

WithClients indicates that the trait is implemented by these client instances. The clients will be added to the relevant routers when the trait is announced.

func WithTraitMetadata

func WithTraitMetadata(md map[string]string) TraitOption

WithTraitMetadata instructs the Node to use the given metadata when adding the trait to the nodes traits.Metadata. Metadata maps will be merged together, with conflicting keys in later calls overriding existing keys.

Jump to

Keyboard shortcuts

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