Accessor: devices/Lifx

devices/Lifx

This accessor discovers and controls a Lifx lightbulb.

If the bulbs are already set up (through the Lifx mobile app for example), then the host running this accessor needs to be connected to the same LAN.

Otherwise, with an unconfigured bulb, you can connect your computer to the Lifx network provided by the bulb (an open Wifi network with a name like "LIFX_Axx_xxxxxx").

If you have a bulb that has been configured to operate on some other network, you can reset it to factory defaults by turning it on and off five times in succession.

While it is possible to interact with Lifx bulbs over the Internet by sending HTTP requests to the Cloud, this module does not provide such mechanism.

The communication with Lifx light bulbs is done over UDP/IP. Messages are arrays of numeric bytes ordered in little-endian. The packets construction can be found in this link: https://lan.developer.lifx.com/docs/introduction

Upon initialization, this accessor creates a UDP socket for communication. There are two ways to configure this accessor in order to control a bulb:

  • the first one consists on discovering Lifx bulbs on the network and then selecting the one to use.
  • the second is by running a manual setup, where the light parameters are received in the input port. A use case of this scenario is when a server sends information about available devices and their parameters.

    Discovery starts if an input is provided in 'triggerDiscovery' input port. The accessor will broadcast discovery messages. Since available bulbs will be asynchronously sending State messages, the accessor will be listening. Discovery messages will be repeatedly sent every 'discoveryInterval' parameter value, if no device has been selected. Each newly discovered light bulb will be added to discoveredLifxLights array. Selecting a device
    is done by providing the index of the LifxLight in the array of discovered devices in the input port 'selectLight'.

    Once the light bulb is chosen and successfully configured, discovery will stop and the accessor will start to react to 'control' input.

    The control input is a JSON object that may have the following properties:

  • on: 'on' to turn on; 'off' to turn off.
  • hue: Color, for bulbs that support color. This is a number in the range 0-65535.
  • color: Color, for bulbs that support color. This is a string that is converted into a hue number using the colorToHexHue variable.

    HSBK is used to represent the color and color temperature of a light. The color is represented as an HSB (Hue, Saturation, Brightness) value. Please refer to https://en.wikipedia.org/wiki/HSL_and_HSV A LifxLight is a class that has the following parameters:

  • ipAddress: IP address of the bulb in the LAN.
  • port: integer. It defaults to 56700 as noted in the Lifx developer API.
  • macAddress: a string of 12 hex numbers (6bytes) that refer to the bulb macAddress.
  • color: the current hue values.
  • power: if true, then the bulb is switched on, false if switched off.
  • userName: this is the name of the user. It can be used to filter the received messages.

    LifxLight class declares a set of functions for light control:

  • swithOn(): switches the light on. The latest selected color is the one used.
  • swithOff(): switches the light off.
  • setColor(): changes the light color.
  • probe(): sends a 'getState' message to ckeck if the light bulb is still working. This feature allows for fault-tolerance.

    The supported communication schemas between the accessor and the bulbs are the following:

    • To run discovery, a discovery message will be broadcasted. Bulbs will answer with a 'stateService' message.
    • To switch the light on or off, a 'setPower' message is sent. The bulb will answer with a 'statePower' message.
    • To set the light color, a 'setColor' message is sent. The bulb will answer with a 'stateLight' message.
    • To probe the light, a 'getLight' message is sent. The bulb will answer with a 'stateLight' message.
Version:
  • $$Id: Lifx.js 1597 2017-04-29 15:41:50Z cxh $$
Author:
  • Chadlia Jerad
Source:
Inputs:
Name Type Description
control JSON JSON control for the Hue, for example, {"on" : "on", "color" : "red"}
triggerDiscovery signal to start discovery
selectLight int index in discoveredLifxLights array of the light to be configured.
manualBulbSetup JSON JSON object for the light configuration. At least, the mac address and the ip address should be provided. Example: {"ipAddress": "192.168.1.100", "macAddress": "d073d523995c"}
probe signal to probe the bulb
Outputs:
Name Type Description
data JSON Outputs the received information from the bulb
Parameters:
Name Type Description
discoveryInterval int The time interval to re-send discovery messages, if no light has been configured
listeningIpAddress string the IP address of the accessor to listen to the bulb packets. This defaults to 0.0.0.0 to listen to all UDP packets.
listeningPort int the port number for listening. If a swarmlet is using two or more instances of Lifx accessor, than each one needs to have its own distinct listening port.
userName string Name of the user. Should be 8bytes.

Methods

(inner) addToDiscovredLightsIfNew(packet)

For the passed light characteristics, add it to discoveredLifxLights array only if it is new. The light is new if the macAddress is new.

Parameters:
Name Type Description
packet

A JSON object containing the new light bulb characteristics.

Source:
Returns:

true if added, false if not.

(inner) buildPacket(options)

Builds a UDP packet to be sent, based on the provided options. Please refer to https://lan.developer.lifx.com/docs/building-a-lifx-packet to know about the message format.

Parameters:
Name Type Description
options

A JSON object that describes the packet features

Source:
Returns:

UDP Packet to be sent

(inner) closeAndOpenSocket()

Creates and opens a socket. Also sets the ractions to recieved messages.

Source:

(inner) convertHexStringToByteArray(hexString)

Convenience function for converting a string, which each character is an hexadecimal number to an array buffer of bytes. This latter will contain unsigned bytes with the value of two consecutive characters from the provided string.

Parameters:
Name Type Description
hexString

String of hexadecimal values in each character

Source:
Returns:

converted hexString into ArrayBuffer

(inner) discoverLifx(socket)

Broadcasts UPD discovery messages. If Lifx bubls are in the network, they will send back a State message.

Parameters:
Name Type Description
socket

The socket instance to use for sending the discovery message

Source:

(inner) getHexStringAt(bytesArray, start, end)

Convenience function for converting a bytesArray to a string of hexadecimal characters. For this, each character of is mapped into two characters that represent hexadecimal values.

Parameters:
Name Type Description
bytesArray

Array of raw bytes.

start

index from where to start the conversion.

end

index where to stop the conversion (index not included).

Source:
Returns:

converted bytes to a string of hexa values.

(inner) LifxLight(ipAddress, port, macAddress, userName)

Create using new a Lifx light bulb object. The created object includes the following properties:

  • ipAddress: The IP address of the bulb in the Local Area Network.
  • port: The port number. This usually defaults to 56700.
  • macAddress: The bulb's mac address.
  • userName: the userName, it is copied from the accessor parameter 'userName' and checked to be 8 characters long.
  • color: The current light color
  • power: Boolean. Says if the light is on or off
Parameters:
Name Type Description
ipAddress

String with the ipAddress of the bulb

port

Bulb's communication port, it defaults to 56700

macAddress

A 12 bytes string of the mac address of the bulb.

userName

An 8 bytes string of the user name. If a wrong value is provided, then it is corrected.

Source:

(inner) parseReceivedPacket(messageBytes, sender)

Returns a JSON object that describes the received packet. This object contains:

  • ipAddress
Parameters:
Name Type Description
messageBytes

The received message during listening as a bytes array.

sender

String containing the IP Address and port of the message sender.

Source:
Returns:

JSON object describing the device and the message features.