<commit_before>// LIFX - the smart wifi light bulb -- http://lifx.co

var lifx        = require('lifx')
  , util        = require('util')
  , tinycolor   = require('tinycolor2')
  , devices     = require('./../../core/device')
  , steward     = require('./../../core/steward')
  , utility     = require('./../../core/utility')
  , broker      = utility.broker
  , lighting    = require('./../device-lighting')
  ;


var logger = lighting.logger;


var LIFX = exports.Device = function(deviceID, deviceUID, info) {
  var self = this;

  self.whatami = info.deviceType;
  self.deviceID = deviceID.toString();
  self.deviceUID = deviceUID;
  self.name = info.device.name;
  self.getName();

  self.status = 'waiting';
  self.changed();
  self.lx = info.lx;
  self.bulb = info.bulb;
  self.info = {};

  broker.subscribe('actors', function(request, taskID, actor, perform, parameter) {
    if (actor !== ('device/' + self.deviceID)) return;

    if (request === 'perform') return self.perform(self, taskID, perform, parameter);
  });
};
util.inherits(LIFX, lighting.Device);


// the low-level driver calls this function when its state changes.
// possibly somebody else is changing the state of the hardware directly,
// or the hardware has built-in controls, like a light switch.

/*
>>> bulbstate
{ bulb: { lifxAddress: <Buffer d0 73 d5 00 b0 05>, name: '' },
  state:
   { hue: 0,
     saturation: 0,
     brightness: 65535,
     kelvin: 3500,
     dim: 0,
     power: 65535,
     bulbLabel: '',
     tags: <Buffer 00 00 00 00 00 00 00 00> } }
 */
LIFX.prototype.update = function(self, state) {
  logger.info('device/' + self.deviceID, { update: state });

  self.status = state.power ? 'on' : 'off';
  if ((state.kelvin > 0) || (state.hue > 0) || (state.saturation > 0)) {
    if ((state.kelvin > 0) && (state.hue === 0) && (state.saturation === 0)) {
      self.info.color = { model       : 'temperature'
                        , temperature : { temperature : Math.round(1000000 / state.kelvin) }
                        };
    } else {
      self.info.color = { model       : 'rgb'
                        , rgb         : tinycolor({ h : devices.degreesValue(state.hue, 65535)
                                                  , s : devices.percentageValue(state.saturation, 65535) / 100
                                                  , l : devices.percentageValue(state.brightness, 65535) / 100
                                                  }).toRgb()
                        };
    }
  }
  if (!!state.brightness) self.info.brightness = devices.percentageValue(state.brightness, 65535);
  if ((!!state.bulblabel) && (state.bulblabel !== '')) self.setName(state.bulbLabel);

  self.changed();
};


// handle the calls from the steward to change things.
// set: set the internal name of the bulb.
// off: turn the bulb off.
// on: turn the bulb on and set its bulb to the 'color' and 'brightness' parameter.
LIFX.prototype.perform = function(self, taskID, perform, parameter) {
  var color, params, state, tc;

  try { params = JSON.parse(parameter); } catch(ex) { params = {}; }

  if (perform === 'set') return self.setName(params.name, taskID);

  state = {};
  if (perform === 'off') state.on = false;
  else if (perform !== 'on') return false;
  else {
    state.on = true;

    if ((!!params.brightness) && (lighting.validBrightness(params.brightness))) state.brightness = params.brightness;
    if (!state.brightness) state.brightness = self.info.brightness;

    state.color = params.color || self.info.color;
    if (!state.color) state.color = { model: 'rgb', rgb: { r: 255, g: 255, b: 255 } };
    switch (state.color.model) {
      case 'temperature':
        if (!state.color.temperature) return false;
        if (!lighting.validTemperature(state.color.temperature.temperature)) return false;<commit_msg>fix handling of LIFX bulb label correctly on update packets<commit_after>// LIFX - the smart wifi light bulb -- http://lifx.co

var lifx        = require('lifx')
  , util        = require('util')
  , tinycolor   = require('tinycolor2')
  , devices     = require('./../../core/device')
  , steward     = require('./../../core/steward')
  , utility     = require('./../../core/utility')
  , broker      = utility.broker
  , lighting    = require('./../device-lighting')
  ;


var logger = lighting.logger;


var LIFX = exports.Device = function(deviceID, deviceUID, info) {
  var self = this;

  self.whatami = info.deviceType;
  self.deviceID = deviceID.toString();
  self.deviceUID = deviceUID;
  self.name = info.device.name;
  self.getName();

  self.status = 'waiting';
  self.changed();
  self.lx = info.lx;
  self.bulb = info.bulb;
  self.info = {};

  broker.subscribe('actors', function(request, taskID, actor, perform, parameter) {
    if (actor !== ('device/' + self.deviceID)) return;

    if (request === 'perform') return self.perform(self, taskID, perform, parameter);
  });
};
util.inherits(LIFX, lighting.Device);


// the low-level driver calls this function when its state changes.
// possibly somebody else is changing the state of the hardware directly,
// or the hardware has built-in controls, like a light switch.

/*
>>> bulbstate
{ bulb: { lifxAddress: <Buffer d0 73 d5 00 b0 05>, name: '' },
  state:
   { hue: 0,
     saturation: 0,
     brightness: 65535,
     kelvin: 3500,
     dim: 0,
     power: 65535,
     bulbLabel: '',
     tags: <Buffer 00 00 00 00 00 00 00 00> } }
 */
LIFX.prototype.update = function(self, state) {
  logger.info('device/' + self.deviceID, { event      : 'update'
                                         , hue        : state.hue
                                         , saturation : state.saturation
                                         , brightness : state.brightness
                                         , kelvin     : state.kelvin
                                         , dim        : state.dim
                                         , power      : state.power
                                         });

  self.status = state.power ? 'on' : 'off';
  if ((state.kelvin > 0) || (state.hue > 0) || (state.saturation > 0)) {
    if ((state.kelvin > 0) && (state.hue === 0) && (state.saturation === 0)) {
      self.info.color = { model       : 'temperature'
                        , temperature : { temperature : Math.round(1000000 / state.kelvin) }
                        };
    } else {
      self.info.color = { model       : 'rgb'
                        , rgb         : tinycolor({ h : devices.degreesValue(state.hue, 65535)
                                                  , s : devices.percentageValue(state.saturation, 65535) / 100
                                                  , l : devices.percentageValue(state.brightness, 65535) / 100
                                                  }).toRgb()
                        };
    }
  }
  if (!!state.brightness) self.info.brightness = devices.percentageValue(state.brightness, 65535);
  if ((!!state.bulbLabel) && (state.bulbLabel !== '')) self.setName(state.bulbLabel);

  self.changed();
};


// handle the calls from the steward to change things.
// set: set the internal name of the bulb.
// off: turn the bulb off.
// on: turn the bulb on and set its bulb to the 'color' and 'brightness' parameter.
LIFX.prototype.perform = function(self, taskID, perform, parameter) {
  var color, params, state, tc;

  try { params = JSON.parse(parameter); } catch(ex) { params = {}; }

  if (perform === 'set') return self.setName(params.name, taskID);

  state = {};
  if (perform === 'off') state.on = false;
  else if (perform !== 'on') return false;
  else {
    state.on = true;

    if ((!!params.brightness) && (lighting.validBrightness(params.brightness))) state.brightness = params.brightness;
    if (!state.brightness) state.brightness = self.info.brightness;

    state.color = params.color || self.info.color;
    if (!state.color) state.color = { model: 'rgb', rgb: { r: 255, g: 255, b: 255 } };
    switch (state.color.model) {
      case 'temperature':
        if (!state.color.temperature) return false;
        if (!lighting.validTemperature(state.color.temperature.temperature)) return false;
{'fraction_non_alphanumeric': 0.1481028151774786, 'fraction_numerical': 0.022276621787025705, 'mean_word_length': 2.1572642967542506, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3223087', 'n_tokens_mistral': 2643, 'n_tokens_neox': 2380, 'n_words': 640}