<commit_before>/**
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
module.exports = function(RED) {
  "use strict";
  var lifxObj = require("lifx-http-api");

  function lifxConfig(n) {
    RED.nodes.createNode(this, n);
    this.name = n.name;
    this.token = n.token;
  }
  RED.nodes.registerType("lifx-config", lifxConfig);

  function defaultTo(val, def) {
    return typeof val != "undefined" ? val : def != "" ? def : null;
  }

  function defaultToNumber(val, def) {
    return Number(defaultTo(val, def))
  }

  function togglePower(config) {
    RED.nodes.createNode(this, config);
    this.selector = config.selector;

    // Retrieve the config node
    this.api = RED.nodes.getNode(config.api);

    var lifx = new lifxObj({ bearerToken: this.api.token });
    var node = this;
    this.on("input", function(msg) {
      msg.payload = defaultTo(msg.payload, {});
      var selector = defaultTo(msg.payload.selector, this.selector);
      lifx.togglePower(selector, 0, function(err, data) {
        if (err) {
          node.error(err);
          return;
        }
      });
    });
  }
  RED.nodes.registerType("lifx-toggle-power", togglePower);

  function pulseEffect(config) {
    RED.nodes.createNode(this, config);
    this.selector = config.selector;
    this.color = config.color;
    this.from_color = config.from_color;
    this.period = config.period;
    this.cycles = config.cycles;
    this.persist = config.persist;
    this.power_on = config.power_on;

    // Retrieve the config node
    this.api = RED.nodes.getNode(config.api);

    var lifx = new lifxObj({ bearerToken: this.api.token });
    var node = this;
    this.on("input", function(msg) {
      msg.payload = defaultTo(msg.payload, {});
      var selector = defaultTo(msg.payload.selector, this.selector);
      var settings = {
        color: defaultTo(msg.payload.color, this.color),
        from_color: defaultTo(msg.payload.from_color, this.from_color),
        period: defaultTo(msg.payload.period, this.period),
        cycles: defaultTo(msg.payload.cycles, this.cycles),
        persist: defaultTo(msg.payload.persist, this.persist),
        power_on: defaultTo(msg.payload.power_on, this.power_on),
        peak: 0.5
      };
      lifx.pulse(selector, settings, function(err, data) {
        if (err) {
          node.error(err);
          return;
        }
      });
    });
  }
  RED.nodes.registerType("lifx-pulse-effect", pulseEffect);

  function breatheEffect(config) {
    RED.nodes.createNode(this, config);
    this.selector = config.selector;
    this.color = config.color;
    this.from_color = config.from_color;
    this.period = config.period;
    this.cycles = config.cycles;
    this.persist = config.persist;
    this.power_on = config.power_on;
    this.peak = config.peak;

    // Retrieve the config node
    this.api = RED.nodes.getNode(config.api);

    var lifx = new lifxObj({ bearerToken: this.api.token });
    var node = this;
    this.on("input", function(msg) {
      msg.payload = defaultTo(msg.payload, {});
      var selector = defaultTo(msg.payload.selector, this.selector);
      var settings = {
        color: defaultTo(msg.payload.color, this.color),
        from_color: defaultTo(msg.payload.from_color, this.from_color),
        period: defaultTo(msg.payload.period, this.period),<commit_msg>Refactor of pulseEffect<commit_after>/**
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
module.exports = function(RED) {
  "use strict";
  var lifxObj = require("lifx-http-api");

  function lifxConfig(n) {
    RED.nodes.createNode(this, n);
    this.name = n.name;
    this.token = n.token;
  }
  RED.nodes.registerType("lifx-config", lifxConfig);

  function defaultTo(val, def) {
    return typeof val != "undefined" ? val : def != "" ? def : null;
  }

  function defaultToNumber(val, def) {
    return Number(defaultTo(val, def))
  }

  function defaultToBoolean(val, def) {
    var tmp = defaultTo(val, def)
    return typeof tmp === "boolean"
      ? tmp
      : typeof tmp === "string"
        ? tmp.toLowerCase() === "true"
        : null
  }

  function togglePower(config) {
    RED.nodes.createNode(this, config);
    this.selector = config.selector;

    // Retrieve the config node
    this.api = RED.nodes.getNode(config.api);

    var lifx = new lifxObj({ bearerToken: this.api.token });
    var node = this;
    this.on("input", function(msg) {
      msg.payload = defaultTo(msg.payload, {});
      var selector = defaultTo(msg.payload.selector, this.selector);
      lifx.togglePower(selector, 0, function(err, data) {
        if (err) {
          node.error(err);
          return;
        }
      });
    });
  }
  RED.nodes.registerType("lifx-toggle-power", togglePower);

  function pulseEffect(config) {
    RED.nodes.createNode(this, config);
    this.selector = config.selector;
    this.color = config.color;
    this.from_color = config.from_color;
    this.period = config.period;
    this.cycles = config.cycles;
    this.persist = config.persist;
    this.power_on = config.power_on;

    // Retrieve the config node
    this.api = RED.nodes.getNode(config.api);

    var lifx = new lifxObj({ bearerToken: this.api.token });
    var node = this;
    this.on("input", function(msg) {
      msg.payload = defaultTo(msg.payload, {});
      var selector = defaultTo(msg.payload.selector, this.selector);
      var settings = {
        color: defaultTo(msg.payload.color, this.color),
        from_color: defaultTo(msg.payload.from_color, this.from_color),
        period: defaultToNumber(msg.payload.period, this.period),
        cycles: defaultToNumber(msg.payload.cycles, this.cycles),
        persist: defaultToBoolean(msg.payload.persist, this.persist),
        power_on: defaultToBoolean(msg.payload.power_on, this.power_on)
      };
      node.log(JSON.stringify(settings))
/*
      lifx.pulse(selector, settings, function(err, data) {
        if (err) {
          node.error(err);
          return;
        }
      });
      */
    });
  }
  RED.nodes.registerType("lifx-pulse-effect", pulseEffect);

  function breatheEffect(config) {
    RED.nodes.createNode(this, config);
    this.selector = config.selector;
    this.color = config.color;
    this.from_color = config.from_color;
    this.period = config.period;
    this.cycles = config.cycles;
    this.persist = config.persist;
    this.power_on = config.power_on;
    this.peak = config.peak;

    // Retrieve the config node
    this.api = RED.nodes.getNode(config.api);

    var lifx = new lifxObj({ bearerToken: this.api.token });
    var node = this;
    this.on("input", function(msg) {
      msg.payload = defaultTo(msg.payload, {});
      var selector = defaultTo(msg.payload.selector, this.selector);
      var settings = {
        color: defaultTo(msg.payload.color, this.color),
        from_color: defaultTo(msg.payload.from_color, this.from_color),
        period: defaultTo(msg.payload.period, this.period),
{'fraction_non_alphanumeric': 0.12850904553961323, 'fraction_numerical': 0.002495321272613849, 'mean_word_length': 2.9507146377525877, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '716588', 'n_tokens_mistral': 2519, 'n_tokens_neox': 2448, 'n_words': 582}