<commit_before>
var serialport = require('serialport'),
  SerialPort = serialport.SerialPort,
  sys = require('sys'),
  https = require('https');

function handleSerialData(data) {
  console.log('serial incoming -> ', data);
}

function handleSerialError(err) {
  console.error('error on serial connection: ', err);
}

/*
  servometer host application
  This nodeJS application handles requesting metrics data from an API
  and sending it over serial USB to the arduino for servo action

  @author <NAME>
*/
function ServoMeter(options) {
  if (!options) {
    throw new Error("ServoMeter options was empty. configure API endpoint and serial port");
  }

  this._serialPort = options.serialPort;
  this._httpOptions = options.httpOptions;
  this.formatSerialData = options.formatSerialData;
}

ServoMeter.prototype.start = function() {
  var self = this;
  console.log('starting ServoMeter');

  var port = new SerialPort(self._serialPort, {parser: serialport.parsers.readline('\n')});

  function refreshMetrics(){
    self.refreshMetrics(port);
  }

  port.on('open', function() {
    console.log('serial connection opened on ' + self._serialPort);

    // wait 1 second to make sure serial is established
    setTimeout(refreshMetrics, 1000)

    // refresh every 10 seconds
    setInterval(refreshMetrics, 10000)
  });

  // output any data sent back FROM the arduino (for confirmation)
  port.on('data', handleSerialData);

  port.on('error', handleSerialError);
};

ServoMeter.prototype.refreshMetrics = function(port) {
  var self = this;
  // call metrics api using the configured http options object
  var req = https.request(self._httpOptions, function(res) {
    console.log('recieved API status code: ', res.statusCode, ' -- ', res.headers.date);
    var response = '';

    // buffer the response until it ends<commit_msg>update serialport usage<commit_after>
const Readline = require('@serialport/parser-readline')
const SerialPort = require('serialport'),
  sys = require('sys'),
  https = require('https');

function handleSerialData(data) {
  console.log('serial incoming -> ', data);
}

function handleSerialError(err) {
  console.error('error on serial connection: ', err);
}

/*
  servometer host application
  This nodeJS application handles requesting metrics data from an API
  and sending it over serial USB to the arduino for servo action

  @author <NAME>
*/
function ServoMeter(options) {
  if (!options) {
    throw new Error("ServoMeter options was empty. configure API endpoint and serial port");
  }

  this._serialPort = options.serialPort;
  this._httpOptions = options.httpOptions;
  this.formatSerialData = options.formatSerialData;
}

ServoMeter.prototype.start = function() {
  var self = this;
  console.log('starting ServoMeter');

  const port = new SerialPort(self._serialPort);
  port.pipe(new Readline({ delimiter: '\n' }))
  function refreshMetrics(){
    self.refreshMetrics(port);
  }

  port.on('open', function() {
    console.log('serial connection opened on ' + self._serialPort);

    // wait 1 second to make sure serial is established
    setTimeout(refreshMetrics, 1000)

    // refresh every 10 seconds
    setInterval(refreshMetrics, 10000)
  });

  // output any data sent back FROM the arduino (for confirmation)
  port.on('data', handleSerialData);

  port.on('error', handleSerialError);
};

ServoMeter.prototype.refreshMetrics = function(port) {
  var self = this;
  // call metrics api using the configured http options object
  var req = https.request(self._httpOptions, function(res) {
    console.log('recieved API status code: ', res.statusCode, ' -- ', res.headers.date);
    var response = '';

    // buffer the response until it ends
{'fraction_non_alphanumeric': 0.11531190926275993, 'fraction_numerical': 0.006481231433972455, 'mean_word_length': 4.144444444444445, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3931875', 'n_tokens_mistral': 1087, 'n_tokens_neox': 1037, 'n_words': 318}