controlifx

package module
v0.0.0-...-97cac35 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2019 License: MIT Imports: 8 Imported by: 1

README

Controlifx

Client side API for LIFX device control.

Projects built with Controlifx:

  • Clifx – command-line interface for LIFX device control
  • Emulifx – LIFX device emulator
  • Implifx – server side API for LIFX device implementations

Resources:

Contents:

Installation

Just run go get -u gopkg.in/lifx-tools/controlifx.v1 to get the latest version.

Getting Started

You'll always start off by opening up a UDP socket for sending and receiving messages:

conn, err := controlifx.Connect()
if err != nil {
    log.Fatalln(err)
}
defer conn.Close()

Next you'll need to create a message to send to LIFX devices on the LAN. All of the messages can be found and described on the official device messages and light messages docs. Remember that some messages are only sent by devices to the client (usually those that start with "State"), and so they cannot be sent from the client, only received. Here, GetLabel() will return a new device message that will eventually make the LIFX devices emit a StateLabel response.

msg := controlifx.GetLabel()

Now we need to send the message and wait for responses. SendToAllAndGet(...) will emit a message on the network that is received by all connected LIFX devices. The timeout value is how long to wait for the devices to respond, since we don't know how many of them will actually respond to the message (we'll talk about alternative methods later).

Finally, the TypeFilter(...) will assure that we only process messages that have the type we're expecting, since other clients on the network may cause devices to emit other types of responses. This ensures we don't try to process a message telling us information about its version when we just wanted to know its label.

recMsgs, err := conn.SendToAllAndGet(controlifx.NormalTimeout, msg,
	controlifx.TypeFilter(controlifx.StateLabelType))
if err != nil {
	log.Fatalln(err)
}

We now have all of our responses stored in recMsgs, which is a mapping between a responding device and its response. The type assertion of *controlifx.StateLabelLanMessage will always succeed because we know for sure, thanks to our TypeFilter(...) above, that the message is in fact a StateLabel response.

for device, recMsg := range recMsgs {
	payload := recMsg.Payload.(*controlifx.StateLabelLanMessage)

	log.Printf("Received StateLabel response from %s: '%s'\n",
		device.Addr.String(), payload.Label)
}

Completed example:

package main

import (
	"gopkg.in/lifx-tools/controlifx.v1"
	"log"
)

func main() {
	conn, err := controlifx.Connect()
	if err != nil {
		log.Fatalln(err)
	}
	defer conn.Close()

	msg := controlifx.GetLabel()

	recMsgs, err := conn.SendToAllAndGet(controlifx.NormalTimeout, msg,
		controlifx.TypeFilter(controlifx.StateLabelType))
	if err != nil {
		log.Fatalln(err)
	}

	for device, recMsg := range recMsgs {
		payload := recMsg.Payload.(*controlifx.StateLabelLanMessage)

		log.Printf("Received StateLabel response from %s: '%s'\n",
			device.Addr.String(), payload.Label)
	}
}

Example output:

2016/08/25 13:13:48 Received StateLabel response from 10.0.0.23:56700: 'Floor'
2016/08/25 13:13:48 Received StateLabel response from 10.0.0.111:56700: 'Nightstand'
2016/08/25 13:13:48 Received StateLabel response from 10.0.0.132:56700: 'Closet'

Examples

Changing colors

You'll undoubtedly want to change the light color of your LIFX bulbs at some point. In this example, we have to give the devices a payload so that they know what color we want them set to.

Afterwards, we send the message to all devices on the LAN. However, unlike in the Getting Started section, we use SendToAll(...) to emit the message on the network, ignoring responses. The method returns as soon as the message is written out to the network, unlike SendToAllAndGet(...), which has to block until it receives responses.

package main

import (
	"gopkg.in/lifx-tools/controlifx.v1"
	"log"
)

func main() {
	// Open the UDP socket.
	conn, err := controlifx.Connect()
	if err != nil {
		log.Fatalln(err)
	}
	defer conn.Close()

	// Create the payload to send to the LIFX devices.
	payload := controlifx.LightSetColorLanMessage{
		Color: controlifx.HSBK{
			Hue:        0xffff / 2,
			Saturation: 0xffff,
			Brightness: 0xffff,
			Kelvin:     3500,
		},
		Duration: 1500,
	}

	// Create the message with the payload that we're going to emit.
	msg := controlifx.LightSetColor(payload)

	// Send the message to all devices on the LAN, ignoring any responses.
	if err := conn.SendToAll(msg); err != nil {
		log.Fatalln(err)
	}
}

Additional Help

Visit #lifx-tools on chat.freenode.net to get help, ask questions, or discuss ideas.

Documentation

Index

Constants

View Source
const (
	// NormalTimeout is a sane number of milliseconds to wait before timing out during discovery.
	NormalTimeout = 250

	MaxReadSize    = LanHeaderSize + 64
	DefaultPort    = 56700
	DefaultPortStr = "56700"
)
View Source
const (
	// MessageRate is the maximum recommended number of messages a device should
	// receive in a second.
	MessageRate = 20

	// The LanHeaderSize is the size of the header for a LAN message in bytes.
	LanHeaderSize = 36
)
View Source
const (
	SetSiteType                         = 1
	GetServiceType                      = 2
	StateServiceType                    = 3
	GetTimeType                         = 4
	SetTimeType                         = 5
	StateTimeType                       = 6
	GetResetSwitchType                  = 7
	StateResetSwitchType                = 8
	GetDummyLoadType                    = 9
	SetDummyLoadType                    = 10
	StateDummyLoadType                  = 11
	GetHostInfoType                     = 12
	StateHostInfoType                   = 13
	GetHostFirmwareType                 = 14
	StateHostFirmwareType               = 15
	GetWifiInfoType                     = 16
	StateWifiInfoType                   = 17
	GetWifiFirmwareType                 = 18
	StateWifiFirmwareType               = 19
	GetPowerType                        = 20
	SetPowerType                        = 21
	StatePowerType                      = 22
	GetLabelType                        = 23
	SetLabelType                        = 24
	StateLabelType                      = 25
	GetTagsType                         = 26
	SetTagsType                         = 27
	StateTagsType                       = 28
	GetTagLabelsType                    = 29
	SetTagLabelsType                    = 30
	StateTagLabelsType                  = 31
	GetVersionType                      = 32
	StateVersionType                    = 33
	GetInfoType                         = 34
	StateInfoType                       = 35
	GetMcuRailVoltageType               = 36
	StateMcuRailVoltageType             = 37
	SetRebootType                       = 38
	SetFactoryTestModeType              = 39
	DisableFactoryTestModeType          = 40
	StateFactoryTestModeType            = 41
	StateSiteType                       = 42
	StateRebootType                     = 43
	SetPanGatewayType                   = 44
	AcknowledgementType                 = 45
	SetFactoryResetType                 = 46
	StateFactoryResetType               = 47
	GetLocationType                     = 48
	SetLocationType                     = 49
	StateLocationType                   = 50
	GetGroupType                        = 51
	SetGroupType                        = 52
	StateGroupType                      = 53
	GetOwnerType                        = 54
	SetOwnerType                        = 55
	StateOwnerType                      = 56
	GetFactoryTestModeType              = 57
	EchoRequestType                     = 58
	EchoResponseType                    = 59
	LightGetType                        = 101
	LightSetColorType                   = 102
	LightSetWaveformType                = 103
	LightSetDimAbsoluteType             = 104
	LightSetDimRelativeType             = 105
	LightSetRgbwType                    = 106
	LightStateType                      = 107
	LightGetRailVoltageType             = 108
	LightStateRailVoltageType           = 109
	LightGetTemperatureType             = 110
	LightStateTemperatureType           = 111
	LightSetCalibrationCoefficientsType = 112
	LightSetSimpleEventType             = 113
	LightGetSimpleEventType             = 114
	LightStateSimpleEventType           = 115
	LightGetPowerType                   = 116
	LightSetPowerType                   = 117
	LightStatePowerType                 = 118
	LightSetWaveformOptionalType        = 119
	WanGetType                          = 201
	WanSetType                          = 202
	WanStateType                        = 203
	WanGetAuthKeyType                   = 204
	WanSetAuthKeyType                   = 205
	WanStateAuthKeyType                 = 206
	WanSetKeepAliveType                 = 207
	WanStateKeepAliveType               = 208
	WanSetHostType                      = 209
	WanGetHostType                      = 210
	WanStateHostType                    = 211
	WifiGetType                         = 301
	WifiSetType                         = 302
	WifiStateType                       = 303
	WifiGetAccessPointsType             = 304
	WifiSetAccessPointType              = 305
	WifiStateAccessPointsType           = 306
	WifiGetAccessPointType              = 307
	WifiStateAccessPointType            = 308
	WifiSetAccessPointBroadcastType     = 309
	SensorGetAmbientLightType           = 401
	SensorStateAmbientLightType         = 402
	SensorGetDimmerVoltageType          = 403
	SensorStateDimmerVoltageType        = 404

	// Misc.
	UdpService        = 1
	TcpService        = 2
	OnboardingService = 3
	OtaService        = 4

	SawWaveform      = 0
	SineWaveform     = 1
	HalfSineWaveform = 2
	TriangleWaveform = 3
	PulseWaveform    = 4

	OffWanStatus                    = 0
	ConnectedWanStatus              = 1
	ErrorUnauthorizedWanStatus      = 2
	ErrorOverCapacityWanStatus      = 3
	ErrorOverRateWanStatus          = 4
	ErrorNoRouteWanStatus           = 5
	ErrorInternalClientWanStatus    = 6
	ErrorInternalServerWanStatus    = 7
	ErrorDnsFailureWanStatus        = 8
	ErrorSslFailureWanStatus        = 9
	ErrorConnectionRefusedWanStatus = 10
	ConnectingWanStatus             = 11

	SoftApWifiNetworkInterface  = 1
	StationWifiNetworkInterface = 2

	ConnectingWifiStatus = 0
	ConnectedWifiStatus  = 1
	FailedWifiStatus     = 2
	OffWifistatus        = 3

	UnknownWifiSecurity      = 0
	OpenWifiSecurity         = 1
	WepPskWifiSecurity       = 2
	WpaTkipPskWifiSecurity   = 3
	WpaAesPskWifiSecurity    = 4
	Wpa2AesPskWifiSecurity   = 5
	Wpa2TkipPskWifiSecurity  = 6
	Wpa2MixedPskWifiSecurity = 7

	Original1000VendorId     = 1
	Color650VendorId         = 1
	White800LowVVendorId     = 1
	White800HighVVendorId    = 1
	White900Br30LowVVendorId = 1
	Color1000Br30VendorId    = 1
	Color1000VendorId        = 1

	Original1000ProductId     = 1
	Color650ProductId         = 3
	White800LowVProductId     = 10
	White800HighVProductId    = 11
	White900Br30LowVProductId = 18
	Color1000Br30ProductId    = 20
	Color1000ProductId        = 22

	Original1000Color     = true
	Color650Color         = true
	White800LowVColor     = false
	White800HighVColor    = false
	White900Br30LowVColor = false
	Color1000Br30Color    = true
	Color1000Color        = true
)

Variables

This section is empty.

Functions

func BToStr

func BToStr(b []byte) string

Types

type AcknowledgementLanMessage

type AcknowledgementLanMessage struct{}

func (*AcknowledgementLanMessage) UnmarshalBinary

func (o *AcknowledgementLanMessage) UnmarshalBinary(data []byte) error

type Connection

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

Connection is the connection between the client and the network devices.

func Connect

func Connect() (_ Connection, err error)

func ManualConnect

func ManualConnect(bcastAddr *net.UDPAddr) (o Connection, err error)

func (Connection) Close

func (o Connection) Close() error

func (Connection) DiscoverAllDevices

func (o Connection) DiscoverAllDevices(timeout int) ([]Device, error)

DiscoverAllDevices discovers as many devices as possible on the network within the timeout.

func (Connection) DiscoverDevices

func (o Connection) DiscoverDevices(timeout int, filter DiscoverFilter) (devices []Device, err error)

DiscoverDevices discovers as many devices as possible on the network within the timeout and filters devices.

func (Connection) SendTo

func (o Connection) SendTo(msg SendableLanMessage, devices []Device) error

SendTo sends the message to the devices without expecting responses.

func (Connection) SendToAll

func (o Connection) SendToAll(msg SendableLanMessage) error

SendToAll sends the message to all devices on the network without expecting responses.

func (Connection) SendToAllAndGet

func (o Connection) SendToAllAndGet(timeout int, msg SendableLanMessage, filter Filter) (recMsgs map[Device]ReceivableLanMessage, err error)

SendToAllAndGet sends the message to all devices on the network, filters the responses, and builds a mapping between a responding device and its response.

func (Connection) SendToAndGet

func (o Connection) SendToAndGet(msg SendableLanMessage, devices []Device, filter Filter) (recMsgs map[Device]ReceivableLanMessage, err error)

SendToAndGet sends the message to the devices, filters the responses, and builds a mapping between a responding device and its response.

type Device

type Device struct {
	// Addr is the remote address of the device.
	Addr *net.UDPAddr
	// Mac is the MAC address of the device.
	Mac uint64
}

Device is a LIFX device on the network.

type DiscoverFilter

type DiscoverFilter func(ReceivableLanMessage, Device) (register bool, cont bool)

DiscoverFilter first returns whether or not the device should be registered and later returned after discovery. The second return value specifies if discovery should continue if there's still time left.

type EchoRequestLanMessage

type EchoRequestLanMessage struct {
	Payload [64]byte
}

func (EchoRequestLanMessage) MarshalBinary

func (o EchoRequestLanMessage) MarshalBinary() ([]byte, error)

type EchoResponseLanMessage

type EchoResponseLanMessage struct {
	Payload [64]byte
}

func (*EchoResponseLanMessage) UnmarshalBinary

func (o *EchoResponseLanMessage) UnmarshalBinary(data []byte) error

type Filter

type Filter func(ReceivableLanMessage) bool

Filter returns false for messages that should not be further processed.

func TypeFilter

func TypeFilter(t uint16) Filter

TypeFilter filters out responses that do not have the payload type.

type HSBK

type HSBK struct {
	Hue        uint16
	Saturation uint16
	Brightness uint16
	Kelvin     uint16
}

func (HSBK) MarshalBinary

func (o HSBK) MarshalBinary() (data []byte, _ error)

func (*HSBK) UnmarshalBinary

func (o *HSBK) UnmarshalBinary(data []byte) error

type LanHeader

type LanHeader struct {
	Frame          LanHeaderFrame
	FrameAddress   LanHeaderFrameAddress
	ProtocolHeader LanHeaderProtocolHeader
}

func (LanHeader) MarshalBinary

func (o LanHeader) MarshalBinary() (data []byte, err error)

func (*LanHeader) UnmarshalBinary

func (o *LanHeader) UnmarshalBinary(data []byte) error

type LanHeaderFrame

type LanHeaderFrame struct {
	Size   uint16
	Tagged bool
	Source uint32
}

func (LanHeaderFrame) MarshalBinary

func (o LanHeaderFrame) MarshalBinary() (data []byte, _ error)

func (*LanHeaderFrame) UnmarshalBinary

func (o *LanHeaderFrame) UnmarshalBinary(data []byte) error

type LanHeaderFrameAddress

type LanHeaderFrameAddress struct {
	Target      uint64
	AckRequired bool
	ResRequired bool
	Sequence    uint8
}

func (LanHeaderFrameAddress) MarshalBinary

func (o LanHeaderFrameAddress) MarshalBinary() (data []byte, _ error)

func (*LanHeaderFrameAddress) UnmarshalBinary

func (o *LanHeaderFrameAddress) UnmarshalBinary(data []byte) error

type LanHeaderProtocolHeader

type LanHeaderProtocolHeader struct {
	Type uint16
}

func (LanHeaderProtocolHeader) MarshalBinary

func (o LanHeaderProtocolHeader) MarshalBinary() (data []byte, _ error)

func (*LanHeaderProtocolHeader) UnmarshalBinary

func (o *LanHeaderProtocolHeader) UnmarshalBinary(data []byte) error

type LightSetColorLanMessage

type LightSetColorLanMessage struct {
	Color    HSBK
	Duration uint32
}

func (LightSetColorLanMessage) MarshalBinary

func (o LightSetColorLanMessage) MarshalBinary() (data []byte, err error)

type LightSetPowerLanMessage

type LightSetPowerLanMessage struct {
	Level    uint16
	Duration uint32
}

func (LightSetPowerLanMessage) MarshalBinary

func (o LightSetPowerLanMessage) MarshalBinary() (data []byte, _ error)

type LightStateLanMessage

type LightStateLanMessage struct {
	Color HSBK
	Power uint16
	Label string
}

func (*LightStateLanMessage) UnmarshalBinary

func (o *LightStateLanMessage) UnmarshalBinary(data []byte) error

type LightStatePowerLanMessage

type LightStatePowerLanMessage struct {
	Level uint16
}

func (*LightStatePowerLanMessage) UnmarshalBinary

func (o *LightStatePowerLanMessage) UnmarshalBinary(data []byte) error

type ReceivableLanMessage

type ReceivableLanMessage struct {
	Header  LanHeader
	Payload encoding.BinaryUnmarshaler
}

func (*ReceivableLanMessage) UnmarshalBinary

func (o *ReceivableLanMessage) UnmarshalBinary(data []byte) error

type SendableLanMessage

type SendableLanMessage struct {
	Header  LanHeader
	Payload encoding.BinaryMarshaler
}

func EchoRequest

func EchoRequest(payload EchoRequestLanMessage) SendableLanMessage

func GetGroup

func GetGroup() SendableLanMessage

func GetHostFirmware

func GetHostFirmware() SendableLanMessage

func GetHostInfo

func GetHostInfo() SendableLanMessage

func GetInfo

func GetInfo() SendableLanMessage

func GetLabel

func GetLabel() SendableLanMessage

func GetLocation

func GetLocation() SendableLanMessage

func GetOwner

func GetOwner() SendableLanMessage

func GetPower

func GetPower() SendableLanMessage

func GetService

func GetService() SendableLanMessage

func GetVersion

func GetVersion() SendableLanMessage

func GetWifiFirmware

func GetWifiFirmware() SendableLanMessage

func GetWifiInfo

func GetWifiInfo() SendableLanMessage

func LightGet

func LightGet() SendableLanMessage

func LightGetPower

func LightGetPower() SendableLanMessage

func LightSetColor

func LightSetColor(payload LightSetColorLanMessage) SendableLanMessage

func LightSetPower

func LightSetPower(payload LightSetPowerLanMessage) SendableLanMessage

func SetLabel

func SetLabel(payload SetLabelLanMessage) SendableLanMessage

func SetOwner

func SetOwner(payload SetOwnerLanMessage) SendableLanMessage

func SetPower

func SetPower(payload SetPowerLanMessage) SendableLanMessage

func (SendableLanMessage) MarshalBinary

func (o SendableLanMessage) MarshalBinary() (data []byte, err error)

type SetLabelLanMessage

type SetLabelLanMessage struct {
	Label string
}

func (SetLabelLanMessage) MarshalBinary

func (o SetLabelLanMessage) MarshalBinary() (data []byte, _ error)

type SetOwnerLanMessage

type SetOwnerLanMessage struct {
	Owner     [16]byte
	Label     string
	UpdatedAt uint64
}

func (SetOwnerLanMessage) MarshalBinary

func (o SetOwnerLanMessage) MarshalBinary() (data []byte, _ error)

type SetPowerLanMessage

type SetPowerLanMessage struct {
	Level uint16
}

func (SetPowerLanMessage) MarshalBinary

func (o SetPowerLanMessage) MarshalBinary() (data []byte, _ error)

type StateGroupLanMessage

type StateGroupLanMessage struct {
	Group     [16]byte
	Label     string
	UpdatedAt uint64
}

func (*StateGroupLanMessage) UnmarshalBinary

func (o *StateGroupLanMessage) UnmarshalBinary(data []byte) error

type StateHostFirmwareLanMessage

type StateHostFirmwareLanMessage struct {
	Build   uint64
	Version uint32
}

func (*StateHostFirmwareLanMessage) UnmarshalBinary

func (o *StateHostFirmwareLanMessage) UnmarshalBinary(data []byte) error

type StateHostInfoLanMessage

type StateHostInfoLanMessage struct {
	Signal float32
	Tx     uint32
	Rx     uint32
}

func (*StateHostInfoLanMessage) UnmarshalBinary

func (o *StateHostInfoLanMessage) UnmarshalBinary(data []byte) error

type StateInfoLanMessage

type StateInfoLanMessage struct {
	Time     uint64
	Uptime   uint64
	Downtime uint64
}

func (*StateInfoLanMessage) UnmarshalBinary

func (o *StateInfoLanMessage) UnmarshalBinary(data []byte) error

type StateLabelLanMessage

type StateLabelLanMessage struct {
	Label string
}

func (*StateLabelLanMessage) UnmarshalBinary

func (o *StateLabelLanMessage) UnmarshalBinary(data []byte) error

type StateLocationLanMessage

type StateLocationLanMessage struct {
	Location  [16]byte
	Label     string
	UpdatedAt uint64
}

func (*StateLocationLanMessage) UnmarshalBinary

func (o *StateLocationLanMessage) UnmarshalBinary(data []byte) error

type StateOwnerLanMessage

type StateOwnerLanMessage struct {
	Owner     [16]byte
	Label     string
	UpdatedAt uint64
}

func (*StateOwnerLanMessage) UnmarshalBinary

func (o *StateOwnerLanMessage) UnmarshalBinary(data []byte) error

type StatePowerLanMessage

type StatePowerLanMessage struct {
	Level uint16
}

func (*StatePowerLanMessage) UnmarshalBinary

func (o *StatePowerLanMessage) UnmarshalBinary(data []byte) error

type StateServiceLanMessage

type StateServiceLanMessage struct {
	Service uint8
	Port    uint32
}

func (*StateServiceLanMessage) UnmarshalBinary

func (o *StateServiceLanMessage) UnmarshalBinary(data []byte) error

type StateVersionLanMessage

type StateVersionLanMessage struct {
	Vendor  uint32
	Product uint32
	Version uint32
}

func (*StateVersionLanMessage) UnmarshalBinary

func (o *StateVersionLanMessage) UnmarshalBinary(data []byte) error

type StateWifiFirmwareLanMessage

type StateWifiFirmwareLanMessage struct {
	Build   uint64
	Version uint32
}

func (*StateWifiFirmwareLanMessage) UnmarshalBinary

func (o *StateWifiFirmwareLanMessage) UnmarshalBinary(data []byte) error

type StateWifiInfoLanMessage

type StateWifiInfoLanMessage struct {
	Signal float32
	Tx     uint32
	Rx     uint32
}

func (*StateWifiInfoLanMessage) UnmarshalBinary

func (o *StateWifiInfoLanMessage) UnmarshalBinary(data []byte) error

Jump to

Keyboard shortcuts

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