github.com/martinohmann/rfoutlet@v1.2.1-0.20220707195255-8a66aa411105/internal/command/command.go (about) 1 // Package command provides commands that can be pushed into a command queue 2 // for the controller to consume. 3 package command 4 5 import "github.com/martinohmann/rfoutlet/internal/outlet" 6 7 // Context is passed to every command. 8 type Context struct { 9 // Registry contains all known outlets and outlet groups. 10 *outlet.Registry 11 // Switcher can switch an outlet on or off. 12 outlet.Switcher 13 } 14 15 // Command is something that can be put into a command queue and is executed by 16 // a controller sequentially. 17 type Command interface { 18 // Execute executes the command. The passed in context contains the outlet 19 // registry and switcher to be able to manipulate the state of outlets. 20 // Must return true if a potential state change should be broadcasted to 21 // all connected clients. 22 Execute(context Context) (broadcast bool, err error) 23 } 24 25 // Sender can send messages. 26 type Sender interface { 27 // Send sends out a message. 28 Send(msg []byte) 29 } 30 31 // SenderAwareCommand is aware of the comand's sender. 32 type SenderAwareCommand interface { 33 Command 34 35 // SetSender sets the sender on the command. The sender can be used to send 36 // messages back to the client that issued the command. 37 SetSender(sender Sender) 38 } 39 40 // NewTestContext creates a new Context which can be used in tests. It returns 41 // the wrapped registry and switcher as 2nd and 3rd return value. 42 func NewTestContext() (Context, *outlet.Registry, *outlet.FakeSwitch) { 43 r := outlet.NewRegistry() 44 s := &outlet.FakeSwitch{} 45 46 return Context{Registry: r, Switcher: s}, r, s 47 } 48 49 // Type is the type of a Command. 50 type Type string 51 52 // Supported command types. 53 const ( 54 GroupType Type = "group" 55 IntervalType Type = "interval" 56 OutletType Type = "outlet" 57 StatusType Type = "status" 58 ) 59 60 // OutletAction is the type of an action that can be performed on an outlet or 61 // outlet group. 62 type OutletAction string 63 64 // Supported outlet command actions. 65 const ( 66 OnOutletAction OutletAction = "on" 67 OffOutletAction OutletAction = "off" 68 ToggleOutletAction OutletAction = "toggle" 69 ) 70 71 // IntervalAction is the type of an action that can be performed on intervals 72 // of an outlet's schedule. 73 type IntervalAction string 74 75 // Supported interval command actions. 76 const ( 77 CreateIntervalAction IntervalAction = "create" 78 UpdateIntervalAction IntervalAction = "update" 79 DeleteIntervalAction IntervalAction = "delete" 80 )