func Close()
Close notifies all known remote devices that this device is disconnecting from the network.
func CloseHandler()
CloseHandler adds a SIGTERM handler to automatically disconnect from the network.
Device represents a device's location and functionality within a home.
type Device struct { Name string Type Type Room Room Funcs map[string]*Func // contains filtered or unexported fields }
func NewDevice(name string, t Type, r Room) *Device
NewDevice creates a new device with the provided name, room, and type. This device is intended to be used locally, as a place to define functions that can be called remotely.
func (d *Device) AddFunction(name string, f func(...int), p ...Param) error
AddFunction adds a function f to device d. Remote devices can call this function using the provided name and params, p.
func (d Device) CallFunc(name string, p ...int) error
CallFunc calls a remote device's function with the provided parameter values. Parameter checking is handled internally, i.e. if the provided parameters are outside the range specified in AddFunction, CallFunc will return an error.
func (d Device) String() string
String returns a string representing this device, in the form (type, room) name: func func func...
Event represents a change in state in the network. Used in the Network struct, specifically when the user has enabled eventing via network.EnableEvents().
type Event struct { Type EventType Dev *Device }
EventType represents the different types of events that are supported by the eventing interface.
type EventType int
const ( Connect EventType = iota Disconnect )
func (t EventType) String() string
Func represents a function that a device performs. F will be nil in any remote device.
type Func struct { F func(...int) Params []Param }
func (f Func) String() string
String returns the string representation of the function, in the form (param, param, param...)
Network contains a list of all devices on the network, updated dynamically as devices connect and disconnect.
type Network struct {
// contains filtered or unexported fields
}
func Bootstrap(dev *Device, port int) (*Network, error)
Bootstrap starts networking services and broadcasts device information dev to the rest of the network.
func (n Network) CallAll(r Room, t Type, name string, p ...int) error
CallAll calls a function on all devices that match the provided room and type.
func (n *Network) EnableEvents() chan *Event
EnableEvents provides access to the Event channel, allowing a device to detect when a remote device connects or disconnects from the network.
func (n Network) GetDevice(name string) (*Device, error)
GetDevice gets a device named name from the network.
func (n Network) GetDevices() []*Device
GetDevices returns all known devices from the network.
func (n Network) GetMatching(r Room, t Type) []*Device
GetMatching gets all devices with the provide room and type.
Param represents a parameter to a function, with specific minimum and maximum values.
type Param struct { Min int Max int }
func (p Param) String() string
Room represents various rooms a device could be placed in.
type Room int
const ( Living Room = iota Dining Bed Bath Kitchen Foyer Closet OtherRoom )
func RoomFromString(s string) (Room, error)
RoomFromString converts a room string (in the same form as one returned from room.String()) into a valid Room. Supports wildcard (*) for use in network.CallAll().
func (r Room) String() string
Type represents the various types of devices that can exist on the network.
type Type int
const ( Light Type = iota Outlet Speaker Screen Controller OtherType )
func TypeFromString(s1 string) (Type, error)
TypeFromString converts a type string (in the same form as one returned from type.String()) into a valid Type. Supports wildcard (*) for use in network.CallAll().
func (t Type) String() string