Documentation ¶
Index ¶
- Constants
- Variables
- type Announcer
- type ClientFactory
- type Feature
- type Node
- func (n *Node) AddClientFactory(traitName trait.Name, f ClientFactory)
- func (n *Node) AddRouter(r ...router.Router)
- func (n *Node) AddTraitFactory(traitName trait.Name, f TraitFactory)
- func (n *Node) Announce(name string, features ...Feature)
- func (n *Node) AnnounceOnRouterChange(name trait.Name, opts ...TraitOption) router.Option
- func (n *Node) CreateDeviceTrait(deviceName string, traitName trait.Name, config proto.Message) error
- func (n *Node) CreateTraitClient(traitName trait.Name, conn *grpc.ClientConn) (interface{}, error)
- func (n *Node) Name() string
- func (n *Node) Register(s *grpc.Server)
- func (n *Node) ResolveRemoteConn(ctx context.Context, endpoint string, opts ...RemoteOption) (*grpc.ClientConn, error)
- func (n *Node) Scrub(t time.Time) error
- func (n *Node) SupportedDeviceTraits() []trait.Name
- func (n *Node) SupportedRemoteTraits() []trait.Name
- type RemoteOption
- type TraitFactory
- type TraitOption
Examples ¶
Constants ¶
const ( MetadataRealism = "scos.playground.realism" MetadataRealismVirtual = "virtual" MetadataRealismModel = "model" MetadataDeviceType = "scos.playground.device-type" )
Variables ¶
var AutoTraitMetadata = map[string]string{ MetadataRealism: MetadataRealismModel, }
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 ¶
HasSimulation indicates that the device can be scrubbed using the given scrub.Scrubber.
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 (*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 ¶
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 ¶
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 ¶
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 ¶
CreateTraitClient creates an instance of an ApiClient, i.e. traits.OnOffApiClient based on a trait name.
func (*Node) Register ¶
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 ¶
Scrub implements the scrub.Scrubber interface and calls Scrub on each device with a simulation feature.
func (*Node) SupportedDeviceTraits ¶
func (*Node) SupportedRemoteTraits ¶
type RemoteOption ¶
type RemoteOption func(n *remoteNode)
func WithRemoteInsecure ¶
func WithRemoteInsecure() RemoteOption
func WithRemoteServerCA ¶
func WithRemoteServerCA(ca []byte) RemoteOption
type TraitFactory ¶
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.