<commit_before>module.exports = BluetoothConnection;

function BluetoothConnection($config, $logger, $event) {
    var self = this;
    var fs = require("fs");
    var deviceIOs = [];
    var inBackgroundTask = false;
    this.keepAliveMessage = "2";
    this.init = function(args) {
        if (args.keepAliveMessage != null){
            self.keepAliveMessage = args.keepAliveMessage;
        }
        setInterval(function() {
            self.runBackGroundTask(self.keepAliveDevices);
        }, 5000);
        setInterval(function() {
            self.runBackGroundTask(self.reconnect);
        }, 10000);
        setInterval(function() {
            //self.runBackGroundTask(self.scan);
        }, 10000);
    }
    this.keepAliveDevices = function(callbackFn) {
        $logger.debug("keepAliveDevices", deviceIOs.length);
        onBackgroundTask = true;
        var disconnectedDeviceIOIdxs = [];
        if (deviceIOs.length > 0) {
            for (var i = 0; i < deviceIOs.length; i++) {
                var deviceIO = deviceIOs[i];
                deviceIO.write(self.keepAliveMessage + "\r\n", function(error) {
                    if (error != null) {
                        self.removeDeviceIO(deviceIO.getAddress());
                    }
                });
            }
        } else {

        }
        callbackFn();
    }
    this.runBackGroundTask = function(task) {
        if (inBackgroundTask === false) {
            inBackgroundTask = true;
            task(function() {
                inBackgroundTask = false;
            });
        }
    }
    this.reconnect = function(callbackFn) {
        var connectedDeviceLog = self.readConnectedDeviceLog();
        var requireConnect = false;
        for (var deviceAddress in connectedDeviceLog) {
            if (self.getDeviceIOByAddress(deviceAddress) == null) {
                $logger.debug("try reconnect to: " + deviceAddress + "-" + connectedDeviceLog[deviceAddress]);
                self.connect(deviceAddress, null, function() {
                    callbackFn();
                }, function() {
                    callbackFn();
                });
                requireConnect = true;
                break;
            }
        }
        if (requireConnect === false) {
            callbackFn();
        }
    }
    this.scan = function(callbackFn) {
        var devices = [];
        var btScanner = new(require('bluetooth-serial-port')).BluetoothSerialPort();
        btScanner.on('found', function(address, name) {
            devices.push({
                'address': address,
                'name': name
            });
        });
        btScanner.on('finished', function() {
            if (callbackFn != null) {
                callbackFn(devices);
            }
            btScanner.close();
        });
        btScanner.inquire();
    }
    this.connect = function(deviceAddress, deviceName, callbackFn, errorCallbackFn) {
        var btClient = new(require('bluetooth-serial-port')).BluetoothSerialPort();
        var deviceName = getDeviceName(deviceAddress, deviceName);
        btClient.findSerialPortChannel(deviceAddress, function(channel) {
            btClient.connect(deviceAddress,
                channel,
                function() {
                    console.log("btClient",btClient);<commit_msg>connect to device that was connected<commit_after>module.exports = BluetoothConnection;

function BluetoothConnection($config, $logger, $event) {
    var self = this;
    var fs = require("fs");
    var deviceIOs = [];
    var inBackgroundTask = false;
    this.keepAliveMessage = "2";
    this.init = function(args) {
        if (args.keepAliveMessage != null){
            self.keepAliveMessage = args.keepAliveMessage;
        }
        setInterval(function() {
            self.runBackGroundTask(self.keepAliveDevices);
        }, 5000);
        setInterval(function() {
            self.runBackGroundTask(self.reconnect);
        }, 10000);
        setInterval(function() {
            //self.runBackGroundTask(self.scan);
        }, 10000);
    }
    this.keepAliveDevices = function(callbackFn) {
        $logger.debug("keepAliveDevices", deviceIOs.length);
        onBackgroundTask = true;
        var disconnectedDeviceIOIdxs = [];
        if (deviceIOs.length > 0) {
            for (var i = 0; i < deviceIOs.length; i++) {
                var deviceIO = deviceIOs[i];
                deviceIO.write(self.keepAliveMessage + "\r\n", function(error) {
                    if (error != null) {
                        self.removeDeviceIO(deviceIO.getAddress());
                    }
                });
            }
        } else {

        }
        callbackFn();
    }
    this.runBackGroundTask = function(task) {
        if (inBackgroundTask === false) {
            inBackgroundTask = true;
            task(function() {
                inBackgroundTask = false;
            });
        }
    }
    this.reconnect = function(callbackFn) {
        var connectedDeviceLog = self.readConnectedDeviceLog();
        var requireConnect = false;
        for (var deviceAddress in connectedDeviceLog) {
            if (self.getDeviceIOByAddress(deviceAddress) == null) {
                $logger.debug("try reconnect to: " + deviceAddress + "-" + connectedDeviceLog[deviceAddress]);
                self.connect(deviceAddress, null, function() {
                    callbackFn();
                }, function() {
                    callbackFn();
                });
                requireConnect = true;
                break;
            }
        }
        if (requireConnect === false) {
            callbackFn();
        }
    }
    this.scan = function(callbackFn) {
        var devices = [];
        var btScanner = new(require('bluetooth-serial-port')).BluetoothSerialPort();
        btScanner.on('found', function(address, name) {
            devices.push({
                'address': address,
                'name': name
            });
        });
        btScanner.on('finished', function() {
            if (callbackFn != null) {
                callbackFn(devices);
            }
            btScanner.close();
        });
        btScanner.inquire();
    }
    this.connect = function(deviceAddress, deviceName, callbackFn, errorCallbackFn) {
        // Check whether is connected
        var existedDeviceIO = self.getDeviceIOByAddress(deviceAddress);
        if (existedDeviceIO != null) {
            callbackFn(existedDeviceIO);
            return;
        }
        // ELSE
        var btClient = new(require('bluetooth-serial-port')).BluetoothSerialPort();
        var deviceName = getDeviceName(deviceAddress, deviceName);
        btClient.findSerialPortChannel(deviceAddress, function(channel) {
            btClient.connect(deviceAddress,
                channel,
                function() {
                    console.log("btClient",btClient);
{'fraction_non_alphanumeric': 0.11530094271211022, 'fraction_numerical': 0.004931109499637419, 'mean_word_length': 1.7606084867894316, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4126267', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1767, 'n_words': 317}