<commit_before>'use strict';
util           = require 'util'
url            = require 'url'
_              = require 'lodash'
tinycolor      = require 'tinycolor2'
HueUtil        = require 'hue-util'
{EventEmitter} = require 'events'
debug          = require('debug')('meshblu-hue')

MESSAGE_SCHEMA =
  type: 'object'
  properties:
    lightNumber:
      type: 'number',
      required: true
    useGroup:
      type: 'boolean',
      required: true,
      default: false
    on:
      type: 'boolean',
      required: true
    color:
      type: 'string',
      required: true
    transitiontime:
      type: 'number'
    alert:
      type: 'string'
    effect:
      type: 'string'

OPTIONS_SCHEMA =
  type: 'object'
  properties:
    ipAddress:
      type: 'string',
      required: true
    apiUsername:
      type: 'string',
      required: true,
      default: 'octoblu'

class Plugin extends EventEmitter
  constructor: ->
    debug 'starting plugin'
    @options = {}
    @messageSchema = MESSAGE_SCHEMA
    @optionsSchema = OPTIONS_SCHEMA

  onMessage: (message) =>
    debug 'on message', message
    payload = message.payload
    @updateHue payload

  onConfig: (device={}) =>
    debug 'on config', username: device.username
    @username = device.username
    @setOptions device.options

  setOptions: (options={}) =>
    debug 'setOptions', options
    @options = _.extend apiUsername: 'octoblu', options
    @hue = new HueUtil @options.apiUsername, @options.ipAddress, @username, @onUsernameChange

  onUsernameChange: (username) =>
    debug 'onUsernameChange', username
    @username = username
    @emit 'update', username: @username

  updateHue: (payload={}) =>
    debug 'updating hue', payload
    @hue.changeLights payload, (error, response) =>
      return @emit 'message', devices: ['*'], topic: 'error', payload: error: error if error?
      @emit 'message', devices: ['*'], payload: response: response

module.exports =
  messageSchema: MESSAGE_SCHEMA
  optionsSchema: OPTIONS_SCHEMA
  Plugin: Plugin
<commit_msg>Changed storage of username to object called apikey which stores both the user-supplied apiUsername as 'devicetype' and the hue-specific username as 'username'. This allows for checks against changes.<commit_after>'use strict';
util           = require 'util'
url            = require 'url'
_              = require 'lodash'
tinycolor      = require 'tinycolor2'
HueUtil        = require 'hue-util'
{EventEmitter} = require 'events'
debug          = require('debug')('meshblu-hue')

MESSAGE_SCHEMA =
  type: 'object'
  properties:
    lightNumber:
      type: 'number',
      required: true
    useGroup:
      type: 'boolean',
      required: true,
      default: false
    on:
      type: 'boolean',
      required: true
    color:
      type: 'string',
      required: true
    transitiontime:
      type: 'number'
    alert:
      type: 'string'
    effect:
      type: 'string'

OPTIONS_SCHEMA =
  type: 'object'
  properties:
    ipAddress:
      type: 'string',
      required: true
    apiUsername:
      type: 'string',
      required: true,
      default: 'octoblu'

class Plugin extends EventEmitter
  constructor: ->
    debug 'starting plugin'
    @options = {}
    @messageSchema = MESSAGE_SCHEMA
    @optionsSchema = OPTIONS_SCHEMA

  onMessage: (message) =>
    debug 'on message', message
    payload = message.payload
    @updateHue payload

  onConfig: (device={}) =>
    debug 'on config', apikey: device.apikey
    @apikey = device.apikey || {}
    @setOptions device.options

  setOptions: (options={}) =>
    debug 'setOptions', options
    @options = _.extend apiUsername: 'octoblu', options

    if @apikey && @options.apiUsername != @apikey?.devicetype
      @apikey.devicetype = @options.apiUsername
      @apikey.username = null

    @hue = new HueUtil @options.apiUsername, @options.ipAddress, @apikey?.username, @onUsernameChange

  onUsernameChange: (username) =>
    debug 'onUsernameChange', username
    @apikey.username = username
    @emit 'update', apikey: @apikey

  updateHue: (payload={}) =>
    debug 'updating hue', payload
    @hue.changeLights payload, (error, response) =>
      return @emit 'message', devices: ['*'], topic: 'error', payload: error: error if error?
      @emit 'message', devices: ['*'], payload: response: response

module.exports =
  messageSchema: MESSAGE_SCHEMA
  optionsSchema: OPTIONS_SCHEMA
  Plugin: Plugin

{'fraction_non_alphanumeric': 0.11747397012222725, 'fraction_numerical': 0.0004526935264825713, 'mean_word_length': 2.9703504043126685, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2913707', 'n_tokens_mistral': 1347, 'n_tokens_neox': 1318, 'n_words': 371}