<commit_before>var util = require('util')
  , EventEmitter2 = require('eventemitter2').EventEmitter2
  , _ = require('lodash')
  , async = require('async');

function DeviceGuider(deviceLoader) {
    var self = this;

    // call super class
    EventEmitter2.call(this, {
        wildcard: true,
        delimiter: ':',
        maxListeners: 1000 // default would be 10!
    });

    if (this.log) {
        this.log = _.wrap(this.log, function(func, msg) {
            func(self.constructor.name + ': ' + msg);
        });
    }

    this.deviceLoader = deviceLoader;
    this.reEmitPlugged = [];
    this.currentState = {
        connectionMode: 'manualconnect',
        plugged: [],
        connected: [],
        getDevice: function(id) {
            return _.find(self.currentState.plugged, function(d) {
                return d.id === id;
            });
        },
        getConnection: function(id) {
            var found = _.find(self.currentState.connected, function(dc) {
                return dc.connection.id === id;
            });

            return found ? found.connection : null;
        },
        getDeviceByPort: function(port) {
            return _.find(self.currentState.plugged, function(d) {
                return d.get('portName') === port;
            });
        },
        getConnectedDevice: function(id) {
            return _.find(self.currentState.connected, function(dc) {
                return dc.id === id;
            });
        },
        getConnectedDeviceByPort: function(id) {
            return _.find(self.currentState.connected, function(dc) {
                return dc.get('portName') === port;
            });
        },
        getDeviceByConnection: function(id) {
            return _.find(self.currentState.connected, function(dc) {
                return dc.connection.id === id;
            });
        },
        getConnectionByDevice: function(id) {
            var found =  _.find(self.currentState.connected, function(dc) {
                return dc.id === id;
            });

            return found ? found.connection : null;
        }
    };

    this.deviceLoader.on('plug', function(device) {

        var plugSubs = self.listeners('plug').length;
        if (plugSubs === 0) {
            self.reEmitPlugged.push(device);
        }

        self.currentState.plugged.push(device);

        if (self.log) self.log('plug device with id ' + device.id);
        self.emit('plug', device);

        if (self.currentState.connectionMode !== 'manualconnect') {
            if (self.currentState.connectionMode === 'autoconnect' || self.currentState.connectionMode === 'autoconnectOne' && self.currentState.connected.length === 0) {
                self.connectDevice(device);
            }
        }
    });

    this.deviceLoader.on('unplug', function(device) {
        self.currentState.plugged = _.reject(self.currentState.plugged, function(d) {
            return d.id === device.id;
        });
        self.currentState.connected = _.reject(self.currentState.connected, function(dc) {
            return dc.id === device.id;
        });

        if (self.log) self.log('unplug device with id ' + device.id);
        self.emit('unplug', device);
    });

    this.deviceLoader.startLookup();
}

util.inherits(DeviceGuider, EventEmitter2);


DeviceGuider.prototype.checkConnectionMode = function(mode) {
    return mode !== this.currentState.connectionMode;
};

DeviceGuider.prototype.changeConnectionMode = function(mode) {
    var self = this;

    var somethingChanged = this.checkConnectionMode(mode);
    this.currentState.connectionMode = mode;

    if (somethingChanged) {
        if (this.reEmitPlugged.length > 0) {
            _.each(this.reEmitPlugged, function(dev) {
                self.emit('plug', dev);
            });
            this.reEmitPlugged = [];
        }
        this.emit('connectionModeChanged', this.currentState.connectionMode);
    }

    return somethingChanged;
};

DeviceGuider.prototype.getCurrentState = function(callback) {<commit_msg>remove reEmitPlugged<commit_after>var util = require('util')
  , EventEmitter2 = require('eventemitter2').EventEmitter2
  , _ = require('lodash')
  , async = require('async');

function DeviceGuider(deviceLoader) {
    var self = this;

    // call super class
    EventEmitter2.call(this, {
        wildcard: true,
        delimiter: ':',
        maxListeners: 1000 // default would be 10!
    });

    if (this.log) {
        this.log = _.wrap(this.log, function(func, msg) {
            func(self.constructor.name + ': ' + msg);
        });
    }

    this.deviceLoader = deviceLoader;
    this.currentState = {
        connectionMode: 'manualconnect',
        plugged: [],
        connected: [],
        getDevice: function(id) {
            return _.find(self.currentState.plugged, function(d) {
                return d.id === id;
            });
        },
        getConnection: function(id) {
            var found = _.find(self.currentState.connected, function(dc) {
                return dc.connection.id === id;
            });

            return found ? found.connection : null;
        },
        getDeviceByPort: function(port) {
            return _.find(self.currentState.plugged, function(d) {
                return d.get('portName') === port;
            });
        },
        getConnectedDevice: function(id) {
            return _.find(self.currentState.connected, function(dc) {
                return dc.id === id;
            });
        },
        getConnectedDeviceByPort: function(id) {
            return _.find(self.currentState.connected, function(dc) {
                return dc.get('portName') === port;
            });
        },
        getDeviceByConnection: function(id) {
            return _.find(self.currentState.connected, function(dc) {
                return dc.connection.id === id;
            });
        },
        getConnectionByDevice: function(id) {
            var found =  _.find(self.currentState.connected, function(dc) {
                return dc.id === id;
            });

            return found ? found.connection : null;
        }
    };

    this.deviceLoader.on('plug', function(device) {
        self.currentState.plugged.push(device);

        if (self.log) self.log('plug device with id ' + device.id);
        self.emit('plug', device);

        if (self.currentState.connectionMode !== 'manualconnect') {
            if (self.currentState.connectionMode === 'autoconnect' || self.currentState.connectionMode === 'autoconnectOne' && self.currentState.connected.length === 0) {
                self.connectDevice(device);
            }
        }
    });

    this.deviceLoader.on('unplug', function(device) {
        self.currentState.plugged = _.reject(self.currentState.plugged, function(d) {
            return d.id === device.id;
        });
        self.currentState.connected = _.reject(self.currentState.connected, function(dc) {
            return dc.id === device.id;
        });

        if (self.log) self.log('unplug device with id ' + device.id);
        self.emit('unplug', device);
    });

    this.deviceLoader.startLookup();
}

util.inherits(DeviceGuider, EventEmitter2);


DeviceGuider.prototype.checkConnectionMode = function(mode) {
    return mode !== this.currentState.connectionMode;
};

DeviceGuider.prototype.changeConnectionMode = function(mode) {
    var self = this;

    var somethingChanged = this.checkConnectionMode(mode);
    this.currentState.connectionMode = mode;

    if (somethingChanged) {
        this.emit('connectionModeChanged', this.currentState.connectionMode);
    }

    return somethingChanged;
};

DeviceGuider.prototype.getCurrentState = function(callback) {
{'fraction_non_alphanumeric': 0.13309166235111342, 'fraction_numerical': 0.0033661315380631796, 'mean_word_length': 2.1376929325751424, 'pattern_counts': {'":': 0, '<': 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': '3905623', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2128, 'n_words': 375}