<commit_before>'use strict';

module.exports = function (RED) {
  // Configuration node
  function GoogleHomeConfig(n) {
    RED.nodes.createNode(this, n);

    this.ipaddress = n.ipaddress;
    this.language = n.language;

    //Prepare language Select Box
    var obj = require('./languages');
    //map to Array:
    var languages = [];
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            languages.push({
              key: key,
              value: obj[key]
            });
        }
    };

    //Build an API for config node HTML to use
    RED.httpAdmin.get('/languages', function(req, res) {
      res.json(languages || []);
    });

    //Known issue: when 'language' is Default/Auto, this will fail & return undefined
    this.googlehomenotifier = require('google-home-notify')(this.ipaddress, this.language, 1);

    //Build another API to auto detect IP Addresses
    discoverIpAddresses('googlecast', function(ipaddresses) {
      RED.httpAdmin.get('/ipaddresses', function(req, res) {
        res.json(ipaddresses);
      });
    });
  };

  function discoverIpAddresses(serviceType, discoveryCallback)
  {
    var ipaddresses = [];
    var bonjour = require('bonjour')();
    var browser = bonjour.find({type: serviceType}, function(service) {
      service.addresses.forEach(function(element) {
        if (element.split(".").length == 4) {
          var label = "" + service.txt.md + " (" + element + ")";
          ipaddresses.push({
            label:label,
            value:element
          });
        }
      });

      //Add a bit of delay for all services to be discovered
      if (discoveryCallback) 
        setTimeout(function(){ 
          discoveryCallback(ipaddresses);
        }, 2000);
    });
  }

  RED.nodes.registerType("googlehome-config-node", GoogleHomeConfig);

  //--------------------------------------------------------

  function GoogleHomeNotifier(n) {
    RED.nodes.createNode(this, n);
    var node = this;

    //Validate config node
    var config = RED.nodes.getNode(n.server);
    if (config === null || config === undefined) {
      node.status({fill:"red", shape:"ring", text:"please create & select a config node"});
      return;
    }

    //On Input
    node.on('input', function (msg) {
      //Validate config node
      if (config === null || config === undefined) {
        node.status({fill:"red", shape:"ring", text:"please create & select a config node"});
        return;
      }

      // we can trigger a learning function
      config.googlehomenotifier.notify(msg.payload);
      node.status({fill:"green", shape:"dot", text:"text sent to Google device"});
    });

    //Workaround for a known issue
    if (config.googlehomenotifier === null || config.googlehomenotifier === undefined) {
      node.status({fill:"red", shape:"ring", text:"please select a non-Default language"});
      return;
    }

    node.status({fill:"blue", shape:"dot", text:"ready"});
    config.googlehomenotifier.setMaxListeners(Infinity);
  };

  RED.nodes.registerType("googlehome-notify", GoogleHomeNotifier);

};
<commit_msg>added some error handling (Github Issue #7)<commit_after>'use strict';

module.exports = function (RED) {
  // Configuration node
  function GoogleHomeConfig(n) {
    RED.nodes.createNode(this, n);

    this.ipaddress = n.ipaddress;
    this.language = n.language;

    //Prepare language Select Box
    var obj = require('./languages');
    //map to Array:
    var languages = [];
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        languages.push({
          key: key,
          value: obj[key]
        });
      }
    };

    //Build an API for config node HTML to use
    RED.httpAdmin.get('/languages', function (req, res) {
      res.json(languages || []);
    });

    //Known issue: when 'language' is Default/Auto, this will fail & return undefined
    this.googlehomenotifier = require('google-home-notify')(this.ipaddress, this.language, 1);

    //Build another API to auto detect IP Addresses
    discoverIpAddresses('googlecast', function (ipaddresses) {
      RED.httpAdmin.get('/ipaddresses', function (req, res) {
        res.json(ipaddresses);
      });
    });
  };

  function discoverIpAddresses(serviceType, discoveryCallback) {
    var ipaddresses = [];
    var bonjour = require('bonjour')();
    var browser = bonjour.find({
      type: serviceType
    }, function (service) {
      service.addresses.forEach(function (element) {
        if (element.split(".").length == 4) {
          var label = "" + service.txt.md + " (" + element + ")";
          ipaddresses.push({
            label: label,
            value: element
          });
        }
      });

      //Add a bit of delay for all services to be discovered
      if (discoveryCallback)
        setTimeout(function () {
          discoveryCallback(ipaddresses);
        }, 2000);
    });
  }

  RED.nodes.registerType("googlehome-config-node", GoogleHomeConfig);

  //--------------------------------------------------------

  function GoogleHomeNotifier(n) {
    RED.nodes.createNode(this, n);
    var node = this;

    //Validate config node
    var config = RED.nodes.getNode(n.server);
    if (config === null || config === undefined) {
      node.status({
        fill: "red",
        shape: "ring",
        text: "please create & select a config node"
      });
      return;
    }

    //On Input
    node.on('input', function (msg) {
      //Validate config node
      if (config === null || config === undefined) {
        node.status({
          fill: "red",
          shape: "ring",
          text: "please create & select a config node"
        });
        return;
      }

      config.googlehomenotifier.notify(msg.payload, function (result) {
          node.status({
            fill: "green",
            shape: "ring",
            text: "Successfully sent voice command"
          });
      });
    });

    config.googlehomenotifier.on('error', function (error) {
      node.status({
        fill: "red",
        shape: "ring",
        text: error.message
      });
    });

    //Workaround for a known issue
    if (config.googlehomenotifier === null || config.googlehomenotifier === undefined) {
      node.status({
        fill: "red",
        shape: "ring",
        text: "please select a non-Default language"
      });
      return;
    }

    node.status({
      fill: "blue",
      shape: "dot",
      text: "ready"
    });
    config.googlehomenotifier.setMaxListeners(Infinity);
  };

  RED.nodes.registerType("googlehome-notify", GoogleHomeNotifier);

};

{'fraction_non_alphanumeric': 0.1569459172852598, 'fraction_numerical': 0.0019693985759733374, 'mean_word_length': 2.2157817827569413, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2110871', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1863, 'n_words': 459}