<commit_before>/*
    TelemetrySender

    A TelemetrySender pairs a SensorReader to a websocket connection.

    Whenever a data point is ready from the SensorReader, this object stores it. 
    Then on a regular basisc, it sends the data points accumulated on the 
    websocket. This basically data points to be send as a group and not individually
    which would be costly.
*/
var TelemetrySender = function(telemetryReceiver, websocketConnection) {
    this._telemetryReceiver = telemetryReceiver;
    this._websocketConnection = websocketConnection;

    this._interval = setInterval(this._sendData.bind(this), 50);

    this._maxNumTelemetrySamplesToSend = 2000;
    this._telemetrySamplesToSend = [];

    this._onTelemetrySampleReadyHandler = this._onTelemetrySampleReady.bind(this);
    this._telemetryReceiver._onTelemetrySampleReadyListeners.push(this._onTelemetrySampleReadyHandler);

    this._instanceID = TelemetrySender.numInstances; // Just for debug
    TelemetrySender.numInstances++;

    console.log('TelemetrySender #' + this._instanceID + ': created');
}
TelemetrySender.numInstances = 0;

TelemetrySender.prototype.dispose = function() {
    clearInterval(this._interval);

    var index = this._telemetryReceiver._onTelemetrySampleReadyListeners.indexOf(this._onSensorDataReadyHandler);
    if (index !== -1) {
        this._telemetryReceiver._onTelemetrySampleReadyListeners.splice(index, 1);
    } else {
        console.error("something's wrong");
    }

    console.log('TelemetrySender#' + this._instanceID + ': dispose');
};

TelemetrySender.prototype._onTelemetrySampleReady = function(telemetrySample) {
    this._telemetrySamplesToSend.splice(0, 0, telemetrySample);
    if (this._telemetrySamplesToSend.length > this._maxNumTelemetrySamplesToSend) {
        this._telemetrySamplesToSend.shift();
        console.warn('buffer full');
    }
};

TelemetrySender.prototype._sendData = function() {
    //console.log("TelemetrySender#"+ this._instanceID + ": sending " + this._telemetrySamplesToSend.length);
    var jsonData = JSON.stringify(this._telemetrySamplesToSend);
    this._websocketConnection.sendUTF(jsonData);
    this._telemetrySamplesToSend = [];
};

exports.TelemetrySender = TelemetrySender;<commit_msg>Fixed TelemetrySender.numInstances on dispose<commit_after>/*
    TelemetrySender

    A TelemetrySender pairs a SensorReader to a websocket connection.

    Whenever a data point is ready from the SensorReader, this object stores it. 
    Then on a regular basisc, it sends the data points accumulated on the 
    websocket. This basically data points to be send as a group and not individually
    which would be costly.
*/
var TelemetrySender = function(telemetryReceiver, websocketConnection) {
    this._telemetryReceiver = telemetryReceiver;
    this._websocketConnection = websocketConnection;

    this._interval = setInterval(this._sendData.bind(this), 50);

    this._maxNumTelemetrySamplesToSend = 2000;
    this._telemetrySamplesToSend = [];

    this._onTelemetrySampleReadyHandler = this._onTelemetrySampleReady.bind(this);
    this._telemetryReceiver._onTelemetrySampleReadyListeners.push(this._onTelemetrySampleReadyHandler);

    this._instanceID = TelemetrySender.numInstances; // Just for debug
    TelemetrySender.numInstances++;

    console.log('TelemetrySender #' + this._instanceID + ': created');
};
TelemetrySender.numInstances = 0;

TelemetrySender.prototype.dispose = function() {
    console.log('TelemetrySender#' + this._instanceID + ': dispose');

    clearInterval(this._interval);

    var index = this._telemetryReceiver._onTelemetrySampleReadyListeners.indexOf(this._onSensorDataReadyHandler);
    if (index !== -1) {
        this._telemetryReceiver._onTelemetrySampleReadyListeners.splice(index, 1);
        TelemetrySender.numInstances--;
    } else {
        console.error("something's wrong");
    }
};

TelemetrySender.prototype._onTelemetrySampleReady = function(telemetrySample) {
    this._telemetrySamplesToSend.splice(0, 0, telemetrySample);
    if (this._telemetrySamplesToSend.length > this._maxNumTelemetrySamplesToSend) {
        this._telemetrySamplesToSend.shift();
        console.warn('buffer full');
    }
};

TelemetrySender.prototype._sendData = function() {
    //console.log("TelemetrySender#"+ this._instanceID + ": sending " + this._telemetrySamplesToSend.length);
    var jsonData = JSON.stringify(this._telemetrySamplesToSend);
    this._websocketConnection.sendUTF(jsonData);
    this._telemetrySamplesToSend = [];
};

exports.TelemetrySender = TelemetrySender;

{'fraction_non_alphanumeric': 0.09698558322411534, 'fraction_numerical': 0.004805591961555264, 'mean_word_length': 4.611519607843137, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, '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': '2876735', 'n_tokens_mistral': 1314, 'n_tokens_neox': 1243, 'n_words': 251}