README
¶
Note: This library is at a moderately early stage - everything should work, but the V2 protocol implementation needs documentation and testing.
If you have Go installed, you can install the lifx
CLI application like so:
go get -u github.com/pdf/golifx/cmd/lifx
The lifx
command will be available at ${GOPATH}/bin/lifx
golifx
-- import "github.com/pdf/golifx"
Package golifx provides a simple Go interface to the LIFX LAN protocol.
Based on the protocol documentation available at: http://lan.developer.lifx.com/
Also included in cmd/lifx is a small CLI utility that allows interacting with your LIFX devices on the LAN.
In various parts of this package you may find references to a Device or a Light. The LIFX protocol makes room for future non-light devices by making a light a superset of a device, so a Light is a Device, but a Device is not necessarily a Light. At this stage, LIFX only produces lights though, so they are the only type of device you will interact with.
Usage
const (
// VERSION of this library
VERSION = `0.1.0`
)
func SetLogger
func SetLogger(logger common.Logger)
SetLogger allows assigning a custom levelled logger that conforms to the common.Logger interface. To capture logs generated during client creation, this should be called before creating a Client. Defaults to common.StubLogger, which does no logging at all.
type Client
type Client struct {
sync.RWMutex
}
Client provides a simple interface for interacting with LIFX devices. Client can not be instantiated manually or it will not function - always use NewClient() to obtain a Client instance.
func NewClient
func NewClient(p common.Protocol) (*Client, error)
NewClient returns a pointer to a new Client and any error that occurred initializing the client, using the protocol p. It also kicks off a discovery run.
func (*Client) Close
func (c *Client) Close() error
Close signals the termination of this client, and cleans up resources
func (*Client) CloseSubscription
func (c *Client) CloseSubscription(sub *common.Subscription) error
CloseSubscription is a callback for handling the closing of subscriptions.
func (*Client) GetDeviceByID
func (c *Client) GetDeviceByID(id uint64) (common.Device, error)
GetDeviceByID looks up a device by it's id and returns a common.Device. May return a common.ErrNotFound error if the lookup times out without finding the device.
func (*Client) GetDeviceByLabel
func (c *Client) GetDeviceByLabel(label string) (common.Device, error)
GetDeviceByLabel looks up a device by it's label and returns a common.Device. May return a common.ErrNotFound error if the lookup times out without finding the device.
func (*Client) GetDevices
func (c *Client) GetDevices() ([]common.Device, error)
GetDevices returns a slice of all devices known to the client, or common.ErrNotFound if no devices are currently known.
func (*Client) GetLightByID
func (c *Client) GetLightByID(id uint64) (light common.Light, err error)
GetLightByID looks up a light by it's id and returns a common.Light. May return a common.ErrNotFound error if the lookup times out without finding the light, or common.ErrDeviceInvalidType if the device exists but is not a light.
func (*Client) GetLightByLabel
func (c *Client) GetLightByLabel(label string) (common.Light, error)
GetLightByLabel looks up a light by it's label and returns a common.Light. May return a common.ErrNotFound error if the lookup times out without finding the light, or common.ErrDeviceInvalidType if the device exists but is not a light.
func (*Client) GetLights
func (c *Client) GetLights() (lights []common.Light, err error)
GetLights returns a slice of all lights known to the client, or common.ErrNotFound if no lights are currently known.
func (*Client) GetRetryInterval
func (c *Client) GetRetryInterval() *time.Duration
GetRetryInterval returns the currently configured retry interval for operations on this client
func (*Client) GetTimeout
func (c *Client) GetTimeout() *time.Duration
GetTimeout returns the currently configured timeout period for operations on this client
func (*Client) NewSubscription
func (c *Client) NewSubscription() (*common.Subscription, error)
NewSubscription returns a new *common.Subscription for receiving events from this client.
func (*Client) SetColor
func (c *Client) SetColor(color common.Color, duration time.Duration) error
SetColor broadcasts a request to change the color of all devices on the network.
func (*Client) SetDiscoveryInterval
func (c *Client) SetDiscoveryInterval(interval time.Duration) error
SetDiscoveryInterval causes the client to discover devices and state every interval. You should set this to a non-zero value for any long-running process, otherwise devices will only be discovered once.
func (*Client) SetPower
func (c *Client) SetPower(state bool) error
SetPower broadcasts a request to change the power state of all devices on the network. A state of true requests power on, and a state of false requests power off.
func (*Client) SetPowerDuration
func (c *Client) SetPowerDuration(state bool, duration time.Duration) error
SetPowerDuration broadcasts a request to change the power state of all devices on the network, transitioning over the specified duration. A state of true requests power on, and a state of false requests power off. Not all device types support transitioning, so if you wish to change the state of all device types, you should use SetPower instead.
func (*Client) SetRetryInterval
func (c *Client) SetRetryInterval(retryInterval time.Duration)
SetRetryInterval sets the retry interval for operations on this client. If a timeout has been set, and the retry interval exceeds the timeout, the retry interval will be set to half the timeout
func (*Client) SetTimeout
func (c *Client) SetTimeout(timeout time.Duration)
SetTimeout sets the time that client operations wait for results before returning an error. The special value of 0 may be set to disable timeouts, and all operations will wait indefinitely, but this is not recommended.
common
-- import "github.com/pdf/golifx/common"
Package common contains common elements for the golifx client and protocols
Usage
const (
// DefaultTimeout is the default duration after which operations time out
DefaultTimeout = 2 * time.Second
// DefaultRetryInterval is the default interval at which operations are
// retried
DefaultRetryInterval = 500 * time.Millisecond
)
var (
// ErrNotFound not found
ErrNotFound = errors.New(`Not found`)
// ErrProtocol protocol error
ErrProtocol = errors.New(`Protocol error`)
// ErrDuplicate already exists
ErrDuplicate = errors.New(`Already exists`)
// ErrInvalidArgument invalid argument
ErrInvalidArgument = errors.New(`Invalid argument`)
// ErrClosed connection closed
ErrClosed = errors.New(`Connection closed`)
// ErrTimeout timed out
ErrTimeout = errors.New(`Timed out`)
// ErrDeviceInvalidType invalid device type
ErrDeviceInvalidType = errors.New(`Invalid device type`)
)
func SetLogger
func SetLogger(logger Logger)
SetLogger wraps the supplied logger with a logPrefixer to denote golifx logs
type Client
type Client interface {
GetTimeout() *time.Duration
GetRetryInterval() *time.Duration
}
Client defines the interface required by protocols
type Color
type Color struct {
Hue uint16 // range 0 to 65535
Saturation uint16 // range 0 to 65535
Brightness uint16 // range 0 to 65535
Kelvin uint16 // range 2500° (warm) to 9000° (cool)
}
Color is used to represent the color and color temperature of a light. The color is represented as an HSB (Hue, Saturation, Brightness) value. The color temperature is represented in K (Kelvin) and is used to adjust the warmness / coolness of a white light, which is most obvious when saturation is close zero.
type Device
type Device interface {
SubscriptionTarget
// Returns the ID for the device
ID() uint64
// Returns the label for the device
GetLabel() (string, error)
// Sets the label for the device
SetLabel(label string) error
// Returns the power state of the device, true for on, false for off
GetPower() (bool, error)
// Sets the power state of the device, true for on, false for off
SetPower(state bool) error
}
Device represents a generic LIFX device
type ErrNotImplemented
type ErrNotImplemented struct {
Method string
}
ErrNotImplemented not implemented
func (*ErrNotImplemented) Error
func (e *ErrNotImplemented) Error() string
Error satisfies the error interface
type EventExpiredDevice
type EventExpiredDevice struct {
Device Device
}
EventExpiredDevice is emitted by a Client when a Device is no longer known
type EventNewDevice
type EventNewDevice struct {
Device Device
}
EventNewDevice is emitted by a Client when it discovers a new Device
type EventUpdateColor
type EventUpdateColor struct {
Color Color
}
EventUpdateColor is emitted by a Light when it's power state is updated
type EventUpdateLabel
type EventUpdateLabel struct {
Label string
}
EventUpdateLabel is emitted by a Device when it's label is updated
type EventUpdatePower
type EventUpdatePower struct {
Power bool
}
EventUpdatePower is emitted by a Device when it's power state is updated
type Light
type Light interface {
// A light is a superset of the Device interface
Device
// SetColor changes the color of the light, transitioning over the specified
// duration
SetColor(color Color, duration time.Duration) error
// GetColor returns the current color of the light
GetColor() (Color, error)
// SetPowerDuration sets the power of the light, transitioning over the
// speficied duration, state is true for on, false for off.
SetPowerDuration(state bool, duration time.Duration) error
}
Light represents a LIFX light device
type Logger
type Logger interface {
// Debugf handles debug level messages
Debugf(format string, args ...interface{})
// Infof handles info level messages
Infof(format string, args ...interface{})
// Warnf handles warn level messages
Warnf(format string, args ...interface{})
// Errorf handles error level messages
Errorf(format string, args ...interface{})
// Fatalf handles fatal level messages, and must exit the application
Fatalf(format string, args ...interface{})
// Panicf handles debug level messages, and must panic the application
Panicf(format string, args ...interface{})
}
Logger represents a minimal levelled logger
var (
// Log holds the global logger used by golifx, can be set via SetLogger() in
// the golifx package
Log Logger
)
type Protocol
type Protocol interface {
SubscriptionTarget
// SetClient sets the client on the protocol for bi-directional
// communication
SetClient(client Client)
// Discover initiates device discovery, this may be a noop in some future
// protocol versions. This is called immediately when the client connects
// to the protocol
Discover() error
// Close closes the protocol driver, no further communication with the
// protocol is possible
Close() error
// SetPower sets the power state globally, on all devices
SetPower(state bool) error
// SetPowerDuration sets the power state globally, on all lights, over the
// specified duration
SetPowerDuration(state bool, duration time.Duration) error
// SetColor changes the color globally, on all lights, over the specified
// duration
SetColor(color Color, duration time.Duration) error
}
Protocol defines the interface between the Client and a protocol implementation
type StubLogger
type StubLogger struct{}
StubLogger satisfies the Logger interface, and simply does nothing with received messages
func (*StubLogger) Debugf
func (l *StubLogger) Debugf(format string, args ...interface{})
Debugf handles debug level messages
func (*StubLogger) Errorf
func (l *StubLogger) Errorf(format string, args ...interface{})
Errorf handles error level messages
func (*StubLogger) Fatalf
func (l *StubLogger) Fatalf(format string, args ...interface{})
Fatalf handles fatal level messages, exits the application
func (*StubLogger) Infof
func (l *StubLogger) Infof(format string, args ...interface{})
Infof handles info level messages
func (*StubLogger) Panicf
func (l *StubLogger) Panicf(format string, args ...interface{})
Panicf handles debug level messages, and panics the application
func (*StubLogger) Warnf
func (l *StubLogger) Warnf(format string, args ...interface{})
Warnf handles warn level messages
type Subscription
type Subscription struct {
}
Subscription exposes an event channel for consumers, and attaches to a SubscriptionTarget, that will feed it with events
func NewSubscription
func NewSubscription(target SubscriptionTarget) *Subscription
NewSubscription returns a *Subscription attached to the specified target
func (*Subscription) Close
func (s *Subscription) Close()
Close cleans up resources and notifies the target that the subscription should no longer be used. It is important to close subscriptions when you are done with them to avoid blocking operations.
func (*Subscription) Events
func (s *Subscription) Events() <-chan interface{}
Events returns a chan reader for reading events published to this subscription
func (*Subscription) ID
func (s *Subscription) ID() string
ID returns the unique ID for this subscription
func (*Subscription) Write
func (s *Subscription) Write(event interface{}) error
Write pushes an event onto the events channel
type SubscriptionTarget
type SubscriptionTarget interface {
NewSubscription() (*Subscription, error)
CloseSubscription(*Subscription) error
}
SubscriptionTarget defines the interface between a subscription and it's target object
Documentation
¶
Overview ¶
Package golifx provides a simple Go interface to the LIFX LAN protocol.
Based on the protocol documentation available at: http://lan.developer.lifx.com/
Also included in cmd/lifx is a small CLI utility that allows interacting with your LIFX devices on the LAN.
In various parts of this package you may find references to a Device or a Light. The LIFX protocol makes room for future non-light devices by making a light a superset of a device, so a Light is a Device, but a Device is not necessarily a Light. At this stage, LIFX only produces lights though, so they are the only type of device you will interact with.
Index ¶
- Constants
- func SetLogger(logger common.Logger)
- type Client
- func (c *Client) Close() error
- func (c *Client) CloseSubscription(sub *common.Subscription) error
- func (c *Client) GetDeviceByID(id uint64) (common.Device, error)
- func (c *Client) GetDeviceByLabel(label string) (common.Device, error)
- func (c *Client) GetDevices() ([]common.Device, error)
- func (c *Client) GetLightByID(id uint64) (light common.Light, err error)
- func (c *Client) GetLightByLabel(label string) (common.Light, error)
- func (c *Client) GetLights() (lights []common.Light, err error)
- func (c *Client) GetRetryInterval() *time.Duration
- func (c *Client) GetTimeout() *time.Duration
- func (c *Client) NewSubscription() (*common.Subscription, error)
- func (c *Client) SetColor(color common.Color, duration time.Duration) error
- func (c *Client) SetDiscoveryInterval(interval time.Duration) error
- func (c *Client) SetPower(state bool) error
- func (c *Client) SetPowerDuration(state bool, duration time.Duration) error
- func (c *Client) SetRetryInterval(retryInterval time.Duration)
- func (c *Client) SetTimeout(timeout time.Duration)
Examples ¶
Constants ¶
const (
// VERSION of this library
VERSION = `0.1.0`
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
Client provides a simple interface for interacting with LIFX devices. Client can not be instantiated manually or it will not function - always use NewClient() to obtain a Client instance.
func NewClient ¶
NewClient returns a pointer to a new Client and any error that occurred initializing the client, using the protocol p. It also kicks off a discovery run.
Example (V2port) ¶
When using protocol V2, it's possible to choose an alternative client port. This is not recommended unless you need to use multiple client instances at the same time.
Output:
func (*Client) CloseSubscription ¶ added in v0.1.0
func (c *Client) CloseSubscription(sub *common.Subscription) error
CloseSubscription is a callback for handling the closing of subscriptions.
func (*Client) GetDeviceByID ¶
GetDeviceByID looks up a device by it's id and returns a common.Device. May return a common.ErrNotFound error if the lookup times out without finding the device.
func (*Client) GetDeviceByLabel ¶
GetDeviceByLabel looks up a device by it's label and returns a common.Device. May return a common.ErrNotFound error if the lookup times out without finding the device.
func (*Client) GetDevices ¶
GetDevices returns a slice of all devices known to the client, or common.ErrNotFound if no devices are currently known.
func (*Client) GetLightByID ¶
GetLightByID looks up a light by it's id and returns a common.Light. May return a common.ErrNotFound error if the lookup times out without finding the light, or common.ErrDeviceInvalidType if the device exists but is not a light.
func (*Client) GetLightByLabel ¶
GetLightByLabel looks up a light by it's label and returns a common.Light. May return a common.ErrNotFound error if the lookup times out without finding the light, or common.ErrDeviceInvalidType if the device exists but is not a light.
func (*Client) GetLights ¶
GetLights returns a slice of all lights known to the client, or common.ErrNotFound if no lights are currently known.
func (*Client) GetRetryInterval ¶
GetRetryInterval returns the currently configured retry interval for operations on this client
func (*Client) GetTimeout ¶
GetTimeout returns the currently configured timeout period for operations on this client
func (*Client) NewSubscription ¶ added in v0.1.0
func (c *Client) NewSubscription() (*common.Subscription, error)
NewSubscription returns a new *common.Subscription for receiving events from this client.
func (*Client) SetColor ¶
SetColor broadcasts a request to change the color of all devices on the network.
func (*Client) SetDiscoveryInterval ¶
SetDiscoveryInterval causes the client to discover devices and state every interval. You should set this to a non-zero value for any long-running process, otherwise devices will only be discovered once.
func (*Client) SetPower ¶
SetPower broadcasts a request to change the power state of all devices on the network. A state of true requests power on, and a state of false requests power off.
func (*Client) SetPowerDuration ¶ added in v0.0.2
SetPowerDuration broadcasts a request to change the power state of all devices on the network, transitioning over the specified duration. A state of true requests power on, and a state of false requests power off. Not all device types support transitioning, so if you wish to change the state of all device types, you should use SetPower instead.
func (*Client) SetRetryInterval ¶
SetRetryInterval sets the retry interval for operations on this client. If a timeout has been set, and the retry interval exceeds the timeout, the retry interval will be set to half the timeout
func (*Client) SetTimeout ¶
SetTimeout sets the time that client operations wait for results before returning an error. The special value of 0 may be set to disable timeouts, and all operations will wait indefinitely, but this is not recommended.
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
lifx
Command lifx allows basic performing operations on LIFX devices over the LAN
|
Command lifx allows basic performing operations on LIFX devices over the LAN |
Package common contains common elements for the golifx client and protocols
|
Package common contains common elements for the golifx client and protocols |
Package protocol implements the LIFX LAN protocol.
|
Package protocol implements the LIFX LAN protocol. |
v2/device
Package device implements a LIFX LAN protocol version 2 device.
|
Package device implements a LIFX LAN protocol version 2 device. |
v2/packet
Package packet implements a LIFX LAN protocol version 2 packet.
|
Package packet implements a LIFX LAN protocol version 2 packet. |
v2/shared
Package shared contains shared elements of the LIFX LAN protocol version 2.
|
Package shared contains shared elements of the LIFX LAN protocol version 2. |