command

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package command provides commands that can be pushed into a command queue for the controller to consume.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command interface {
	// Execute executes the command. The passed in context contains the outlet
	// registry and switcher to be able to manipulate the state of outlets.
	// Must return true if a potential state change should be broadcasted to
	// all connected clients.
	Execute(context Context) (broadcast bool, err error)
}

Command is something that can be put into a command queue and is executed by a controller sequentially.

func Unpack added in v1.2.0

func Unpack(envelope Envelope) (cmd Command, err error)

Unpack unpacks the contents of a command envelope into the correct type.

type Context

type Context struct {
	// Registry contains all known outlets and outlet groups.
	*outlet.Registry
	// Switcher can switch an outlet on or off.
	outlet.Switcher
}

Context is passed to every command.

func NewTestContext

func NewTestContext() (Context, *outlet.Registry, *outlet.FakeSwitch)

NewTestContext creates a new Context which can be used in tests. It returns the wrapped registry and switcher as 2nd and 3rd return value.

type Envelope added in v1.2.0

type Envelope struct {
	Type Type
	Data *json.RawMessage
}

Envelope defines a command envelope which hold the command type and the raw json data of the command.

type GroupCommand added in v1.2.0

type GroupCommand struct {
	// GroupID is the ID of the outlet group that the action should be
	// performed on.
	GroupID string `json:"groupID"`
	// Action defines the action type that should be performed on the outlet
	// group.
	Action OutletAction `json:"action"`
}

GroupCommand switches a group of outlets based on the action.

func (GroupCommand) Execute added in v1.2.0

func (c GroupCommand) Execute(context Context) (bool, error)

Execute implements Command.

It switches a group of outlets based on the transmitted action.

type IntervalAction added in v1.2.0

type IntervalAction string

IntervalAction is the type of an action that can be performed on intervals of an outlet's schedule.

const (
	CreateIntervalAction IntervalAction = "create"
	UpdateIntervalAction IntervalAction = "update"
	DeleteIntervalAction IntervalAction = "delete"
)

Supported interval command actions.

type IntervalCommand added in v1.2.0

type IntervalCommand struct {
	// OutletID is the ID of the outlet where the intervals of the schedule
	// should be changed.
	OutletID string `json:"outletID"`
	// Action defines the action type that should be performed on the interval.
	Action IntervalAction `json:"action"`
	// Interval is the configuration of the interval.
	Interval schedule.Interval `json:"interval"`
}

IntervalCommand changes the intervals of an outlet based on the action.

func (IntervalCommand) Execute added in v1.2.0

func (c IntervalCommand) Execute(context Context) (bool, error)

Execute implements Command.

It add, updates or deletes an interval from the outlet's schedule based on the transmitted action.

type OutletAction added in v1.2.0

type OutletAction string

OutletAction is the type of an action that can be performed on an outlet or outlet group.

const (
	OnOutletAction     OutletAction = "on"
	OffOutletAction    OutletAction = "off"
	ToggleOutletAction OutletAction = "toggle"
)

Supported outlet command actions.

type OutletCommand added in v1.2.0

type OutletCommand struct {
	// OutletID is the ID of the outlet that the action should be performed on.
	OutletID string `json:"outletID"`
	// Action defines the action type that should be performed on the outlet.
	Action OutletAction `json:"action"`
}

OutletCommand switches a specific outlet based on the action.

func (OutletCommand) Execute added in v1.2.0

func (c OutletCommand) Execute(context Context) (bool, error)

Execute implements Command.

It switches an outlet based on the transmitted action.

type Sender

type Sender interface {
	// Send sends out a message.
	Send(msg []byte)
}

Sender can send messages.

type SenderAwareCommand

type SenderAwareCommand interface {
	Command

	// SetSender sets the sender on the command. The sender can be used to send
	// messages back to the client that issued the command.
	SetSender(sender Sender)
}

SenderAwareCommand is aware of the comand's sender.

type StateCorrectionCommand added in v1.2.0

type StateCorrectionCommand struct {
	// Outlet is the outlet that should be brought into the desired state.
	Outlet *outlet.Outlet
	// DesiredState is the state that the outlet should be in.
	DesiredState outlet.State
}

StateCorrectionCommand is sent out whenever an outlet should change its state based on detected rf codes.

func (StateCorrectionCommand) Execute added in v1.2.0

func (c StateCorrectionCommand) Execute(context Context) (bool, error)

Execute implements Command.

It switch an outlet to the detected state.

type StatusCommand added in v1.2.0

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

StatusCommand is sent by a connected client to retrieve the list of current outlet groups. This usually happens when the client first connects.

func (StatusCommand) Execute added in v1.2.0

func (c StatusCommand) Execute(context Context) (bool, error)

Execute implements Command.

It sends the registered outlet groups back to the sender.

func (*StatusCommand) SetSender added in v1.2.0

func (c *StatusCommand) SetSender(sender Sender)

SetSender implements SenderAwareCommand.

type Type added in v1.2.0

type Type string

Type is the type of a Command.

const (
	GroupType    Type = "group"
	IntervalType Type = "interval"
	OutletType   Type = "outlet"
	StatusType   Type = "status"
)

Supported command types.

Jump to

Keyboard shortcuts

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