<commit_before>'use strict';


var net = require('net');
var util = require('util');
var events = require('events');
var Q = require('q');
var serialport;
try {
  serialport = require('serialport');
} catch(e) {
  serialport = 0;
}

var https = require('https');
var querystring = require('querystring');
var EventSource = require('eventsource');

var debug = require('debug')('home-controller:insteon');

var utils = require('./utils');
var toByte = utils.toByte;
var DEV_CAT_NAMES = utils.DEV_CAT_NAMES;


var DEDUP_TIMER = 5000; // 5 seconds

var Light = require('./Light');
var Thermostat = require('./Thermostat');
var Motion = require('./Motion');
var Door = require('./Door');
var Leak = require('./Leak');
var Meter = require('./Meter');

var Insteon = function () {

  this.queue = [];
  this.buffer = '';
  this.commandTimeout = 5000; // default 5 second timeout
  this.commandRetries = 1;

  this.defaultTempUnits = Insteon.defaultTempUnits;

  this.devices = {};

  this.emitDuplicates = false;

};


util.inherits(Insteon, events.EventEmitter);

Insteon.prototype.connect = function(host, port, connectListener) {

  var that = this;

  if(typeof port !== 'number') {
    connectListener = port;
    port = 9761;
  }

  this.host = host;
  this.port = port || 9761;
  debug('connecting', this.port, this.host);

  var socket = this.socket = new net.Socket();
  socket.setEncoding('hex');
  socket.setKeepAlive(true, 30000);


  socket.on('connect', function() {
    debug('connected');
    that.emit('connect');
  });

  socket.on('data', function(data) {
    debug('Rcvd:', Date.now(), data);
    that.buffer = that.buffer + data;
    that.checkStatus();
  });

  socket.on('error', function(err) {
    that.emit('error', err);
  });

  socket.on('close', function(had_error) {
    that.emit('close', had_error);
  });

  this.write = function(raw) {
    return this.socket.write(raw, 'hex');
  };
  this.close = this.socket.end.bind(this.socket);

  this.socket.connect(this.port, this.host, connectListener);
};

Insteon.prototype.serial = function(comName, options, connectListener) {

  if(!serialport) {
    throw(new Error('serial is Unsupported on this platform'));
  }

  var that = this;

  if (typeof options === 'function') {
    connectListener = options;
    options = null;
  }
  if (!options) { options = { }; }
  if (typeof options.baudrate === 'undefined') { options.baudrate = 19200; }
  if (typeof options.databits === 'undefined') { options.databits = 8;     }
  if (typeof options.parity   === 'undefined') { options.parity = 'none';  }
  if (typeof options.stopbits === 'undefined') { options.stopbits = 1;     }
  options.parser = serialport.parsers.raw;

  this.comName = comName;
  this.options = options;
  debug('connecting', this.comName);

  this.socket = new serialport.SerialPort(comName, this.options, false)
  .on('open', function() {
    debug('connected');
    that.emit('connect');
    if (!!connectListener) { connectListener(that.comName); }
  })
  .on('data', function(data) {
    data = data.toString('hex');
    debug('Rcvd:', Date.now(), data);
    that.buffer = that.buffer + data;
    that.checkStatus();
  })
  .on('error', function(err) {
    that.emit('error', err);
  })
  .on('close', function(had_error) {
    that.emit('close', had_error);
  });

  this.write = function(raw) {
    return that.socket.write(new Buffer(raw, 'hex'));
  };
  this.close = this.socket.close;

  this.socket.open();
};

Insteon.prototype.spark = function (sparkId, token) {

  var that = this;

  var eventUrl = 'https://api.spark.io/v1/devices/' +
                 sparkId +
                 '/events/insteon?access_token=' +
                 token;

  debug('Spark event url', eventUrl);
  var es = new EventSource(eventUrl);
  es.reconnectInterval = 3000;

  es.addEventListener('insteon', function(event) {
    debug('Spark Rcvd:', Date.now(), event);
    try {
      var eventData = JSON.parse(event.data);
      that.buffer = that.buffer + eventData.data;
      that.checkStatus();
    } catch (err) {
      debug('Spark Rcvd error - ignoring', err);
    }
  });

  es.onerror = function(err) {
    debug('Spark Error', err, es.readyState);
    setTimeout(function () {
      if (es.readyState === EventSource.CLOSED) {
        that.emit('error', err);
        that.close(true);
      }
    }, 100);
  };<commit_msg>[enhancement] configurable reconnect interval for spark<commit_after>'use strict';


var net = require('net');
var util = require('util');
var events = require('events');
var Q = require('q');
var serialport;
try {
  serialport = require('serialport');
} catch(e) {
  serialport = 0;
}

var https = require('https');
var querystring = require('querystring');
var EventSource = require('eventsource');

var debug = require('debug')('home-controller:insteon');

var utils = require('./utils');
var toByte = utils.toByte;
var DEV_CAT_NAMES = utils.DEV_CAT_NAMES;


var DEDUP_TIMER = 5000; // 5 seconds

var Light = require('./Light');
var Thermostat = require('./Thermostat');
var Motion = require('./Motion');
var Door = require('./Door');
var Leak = require('./Leak');
var Meter = require('./Meter');

var Insteon = function () {

  this.queue = [];
  this.buffer = '';
  this.commandTimeout = 5000; // default 5 second timeout
  this.commandRetries = 1;

  this.defaultTempUnits = Insteon.defaultTempUnits;

  this.devices = {};

  this.emitDuplicates = false;

};


util.inherits(Insteon, events.EventEmitter);

Insteon.prototype.connect = function(host, port, connectListener) {

  var that = this;

  if(typeof port !== 'number') {
    connectListener = port;
    port = 9761;
  }

  this.host = host;
  this.port = port || 9761;
  debug('connecting', this.port, this.host);

  var socket = this.socket = new net.Socket();
  socket.setEncoding('hex');
  socket.setKeepAlive(true, 30000);


  socket.on('connect', function() {
    debug('connected');
    that.emit('connect');
  });

  socket.on('data', function(data) {
    debug('Rcvd:', Date.now(), data);
    that.buffer = that.buffer + data;
    that.checkStatus();
  });

  socket.on('error', function(err) {
    that.emit('error', err);
  });

  socket.on('close', function(had_error) {
    that.emit('close', had_error);
  });

  this.write = function(raw) {
    return this.socket.write(raw, 'hex');
  };
  this.close = this.socket.end.bind(this.socket);

  this.socket.connect(this.port, this.host, connectListener);
};

Insteon.prototype.serial = function(comName, options, connectListener) {

  if(!serialport) {
    throw(new Error('serial is Unsupported on this platform'));
  }

  var that = this;

  if (typeof options === 'function') {
    connectListener = options;
    options = null;
  }
  if (!options) { options = { }; }
  if (typeof options.baudrate === 'undefined') { options.baudrate = 19200; }
  if (typeof options.databits === 'undefined') { options.databits = 8;     }
  if (typeof options.parity   === 'undefined') { options.parity = 'none';  }
  if (typeof options.stopbits === 'undefined') { options.stopbits = 1;     }
  options.parser = serialport.parsers.raw;

  this.comName = comName;
  this.options = options;
  debug('connecting', this.comName);

  this.socket = new serialport.SerialPort(comName, this.options, false)
  .on('open', function() {
    debug('connected');
    that.emit('connect');
    if (!!connectListener) { connectListener(that.comName); }
  })
  .on('data', function(data) {
    data = data.toString('hex');
    debug('Rcvd:', Date.now(), data);
    that.buffer = that.buffer + data;
    that.checkStatus();
  })
  .on('error', function(err) {
    that.emit('error', err);
  })
  .on('close', function(had_error) {
    that.emit('close', had_error);
  });

  this.write = function(raw) {
    return that.socket.write(new Buffer(raw, 'hex'));
  };
  this.close = this.socket.close;

  this.socket.open();
};

Insteon.prototype.spark = function (sparkId, token, reconnectInterval) {

  var that = this;

  var eventUrl = 'https://api.spark.io/v1/devices/' +
                 sparkId +
                 '/events/insteon?access_token=' +
                 token;

  debug('Spark event url', eventUrl);
  var es = new EventSource(eventUrl);
  es.reconnectInterval = reconnectInterval || 5000;

  es.addEventListener('insteon', function(event) {
    debug('Spark Rcvd:', Date.now(), event);
    try {
      var eventData = JSON.parse(event.data);
      that.buffer = that.buffer + eventData.data;
      that.checkStatus();
    } catch (err) {
      debug('Spark Rcvd error - ignoring', err);
    }
  });

  es.onerror = function(err) {
    debug('Spark Error', err, es.readyState);
    setTimeout(function () {
      if (es.readyState === EventSource.CLOSED) {
        that.emit('error', err);
        that.close(true);
      }
    }, 100);
  };
{'fraction_non_alphanumeric': 0.17260553790285973, 'fraction_numerical': 0.009078529278256923, 'mean_word_length': 3.43086978381096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1315876', 'n_tokens_mistral': 3000, 'n_tokens_neox': 2857, 'n_words': 613}