<commit_before>var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var Q = require("q"); var _ = require("lodash");
var commands = {
  'kwhlife' : 'KWHLIFE?',
  'kwhtoday' : 'KWHTODAY?',
  'kwhsummary' : 'MEASENGY?',
  'inputsummary' : 'MEASIN?',
  'outputsummary' : 'MEASOUT?',
  'temperature' : 'MEASTEMP?'
}


function Xantrex(port, rate) {
  this.port = port;
  this.rate = rate;
  this.connection = new SerialPort(
    this.port,
    {
      baudrate: this.rate, 
      parser: serialport.parsers.readline("\r")
    },
    false
  );
}

var xantrex = Xantrex.prototype;

xantrex.connection = false;
xantrex.connectionState = false;
xantrex.callbuffer = [];
xantrex.connect = function() {
  var self = this;
  var deferred = Q.defer();
  this.connection.open(function() {
    self.connection.on("data", function(data) {
      var deferred = self.callbuffer.shift();
      deferred.resolve(data);
    });
    deferred.resolve(true);
  });
  return deferred.promise;
}

xantrex.disconnect = function() {
  this.connection.close(function() {
  });
}
xantrex._mapOutput = function(output, map) {
 var parts = output.split(" ");
 var result = {};
 _.each(map,function(val, index) {
  result[val] = parts[index].split(":")[1];
 });
 return result;
}; 

xantrex.runCommand = function(cmd) {
  var self = this;
  var deferred = Q.defer();
  Q.delay(3100).then(function () {
     self.callbuffer.shift();
     deferred.reject(new Error("Timed out"));
  });
  Q.delay(200).then( function() {
    self.connection.write(commands[cmd] + "\r", function(err,result) {
      self.callbuffer.push(deferred);
    });
  });
  return deferred.promise;
}

xantrex.getKwhToday = function() {
  return this.runCommand('kwhtoday');
};

xantrex.getKwhLife = function() {
  return this.runCommand('kwhlife');
};

xantrex.getKwhSummary = function() {
  var self = this;
  return this.runCommand('kwhsummary').then(
    function(result) {
      return self._mapOutput(result, ['wout', 'kwhtoday', 'kwhlife']); 
    });
};

xantrex.getTemperature = function() {
  var self = this;
  return this.runCommand('temperature').then(
    function(result) {
      return self._mapOutput(result, ['ctemp', 'ftemp']); 
    });
};

xantrex.getInputSummary = function() {
  var self = this;<commit_msg>Added delays when multiple commands in buffer<commit_after>"use strict";

var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var Q = require("q"); var _ = require("lodash");
var commands = {
  'kwhlife' : 'KWHLIFE?',
  'kwhtoday' : 'KWHTODAY?',
  'kwhsummary' : 'MEASENGY?',
  'inputsummary' : 'MEASIN?',
  'outputsummary' : 'MEASOUT?',
  'temperature' : 'MEASTEMP?'
}


function Xantrex(port, rate) {
  this.port = port;
  this.rate = rate;
  this.connection = new SerialPort(
    this.port,
    {
      baudrate: this.rate, 
      parser: serialport.parsers.readline("\r")
    },
    false
  );
}

var xantrex = Xantrex.prototype;

xantrex.connection = false;
xantrex.connectionState = false;
xantrex.sending = false;
xantrex.callbuffer = [];
xantrex.connect = function() {
  var self = this;
  var deferred = Q.defer();
  this.connection.open(function() {
    self.connection.on("data", function(data) {
      var deferred = self.callbuffer.shift();
      deferred.resolve(data);
    });
    deferred.resolve(true);
  });
  return deferred.promise;
}

xantrex.disconnect = function() {
  this.connection.close(function() {
  });
}
xantrex._mapOutput = function(output, map) {
 var parts = output.split(" ");
 var result = {};
 _.each(map,function(val, index) {
  result[val] = parts[index].split(":")[1];
 });
 return result;
}; 

xantrex.runCommand = function(cmd) {
  var self = this;
  var deferred = Q.defer();
  Q.delay(4000).then(function () {
     self.callbuffer.shift();
     deferred.reject(new Error("Timed out"));
  });
  var bufferlength = self.callbuffer.length;
  var delay = (bufferlength > 0) ? bufferlength * 250 : 0;
  self.callbuffer.push(deferred);
  Q.delay(delay).then( function() {
    self.connection.write(commands[cmd] + "\r", function(err,result) {});
  });
  return deferred.promise;
}

xantrex.getKwhToday = function() {
  return this.runCommand('kwhtoday');
};

xantrex.getKwhLife = function() {
  return this.runCommand('kwhlife');
};

xantrex.getKwhSummary = function() {
  var self = this;
  return this.runCommand('kwhsummary').then(
    function(result) {
      return self._mapOutput(result, ['pout', 'kwhtoday', 'kwhlife']); 
    });
};

xantrex.getTemperature = function() {
  var self = this;
  return this.runCommand('temperature').then(
    function(result) {
      return self._mapOutput(result, ['ctemp', 'ftemp']); 
    });
};

xantrex.getInputSummary = function() {
  var self = this;
{'fraction_non_alphanumeric': 0.17160883280757097, 'fraction_numerical': 0.0037854889589905363, 'mean_word_length': 3.8679631525076767, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 4, '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': '3656967', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1645, 'n_words': 305}