<commit_before>/**
 * Wheel, copyright (c) 2020 - present by <NAME>
 * Distributed under an MIT license: https://arnovandervegt.github.io/wheel/license.txt
**/
const pup       = require('../../shared/device/poweredup/PoweredUp');
const PoweredUp = pup.PoweredUp;

let poweredUp = null;

pup.setLibrary(require('node-poweredup').Consts, require('node-poweredup'));

const getPoweredUp = function() {
        if (poweredUp) {
            return poweredUp;
        }
        poweredUp = new PoweredUp({});
        return poweredUp;
    };

exports.poweredUpRoutes = {
    discover(req, res) {
        getPoweredUp().discover(req.body.autoConnect || []);
        res.send(JSON.stringify({}));
    },

    deviceList(req, res) {
        let poweredUp   = getPoweredUp();
        let autoConnect = req.body.autoConnect || [];
        let list        = poweredUp.getDeviceList(autoConnect);
        res.send(JSON.stringify({result: true, changed: poweredUp.getChanged(), list: list}));
    },

    connectedDeviceList(req, res) {
        let poweredUp   = getPoweredUp();
        let autoConnect = req.body.autoConnect || [];
        res.send(JSON.stringify({result: true, changed: poweredUp.getChanged(), list: poweredUp.getConnectedDeviceList(autoConnect)}));
    },

    connect(req, res) {
        let uuid = req.body.uuid;
        getPoweredUp().connect(
            uuid,
            function(hub) {
                res.send(JSON.stringify(hub));
            }
        );
    },

    disconnect(req, res) {
        getPoweredUp().disconnect(() => {});
        res.send(JSON.stringify({}));
    },

    disconnectAll(req, res) {
        getPoweredUp().disconnectAll(() => {});
        res.send(JSON.stringify({}));
    },

    connecting(req, res) {
        let connected = getPoweredUp().getConnected();
        let state     = {};
        if (connected) {
            state = getPoweredUp().getState();
        }
        res.send(JSON.stringify({
            connected: connected,
            state:     state
        }));
    },

    connected(req, res) {
        res.send(JSON.stringify({connected: getPoweredUp().getConnected()}));
    },

    update(req, res) {
        let result           = {error: false, connected: true};
        let queue            = (typeof req.body.queue === 'string') ? JSON.parse(req.body.queue) : req.body.queue;
        let messagesReceived = {};
        queue.forEach((params) => {
            poweredUp.module(params.module, params.command, params.data);
            messagesReceived[params.messageId] = true;
        });
        result.state            = poweredUp.getState();
        result.messagesReceived = messagesReceived;
        res.send(JSON.stringify(result));
    },

    _createTimeoutCallback(res) {
        let done     = false;
        let timeout  = null;
        let callback = function(success) {
                if (done) {
                    return;
                }
                if (timeout) {
                    clearTimeout(timeout);
                    timeout = null;
                }
                done = true;
                res.send(JSON.stringify({error: !success}));
            };
        timeout = setTimeout(callback, 500);
        return callback;
    },

    stopAllMotors(req, res) {
        let result     = {success: true};
        let layerCount = req.body.layerCount;
        let brake      = req.body.brake ? 1 : 0;<commit_msg>Get poweredUp from getter.<commit_after>/**
 * Wheel, copyright (c) 2020 - present by <NAME>
 * Distributed under an MIT license: https://arnovandervegt.github.io/wheel/license.txt
**/
const pup       = require('../../shared/device/poweredup/PoweredUp');
const PoweredUp = pup.PoweredUp;

let poweredUp = null;

pup.setLibrary(require('node-poweredup').Consts, require('node-poweredup'));

const getPoweredUp = function() {
        if (poweredUp) {
            return poweredUp;
        }
        poweredUp = new PoweredUp({});
        return poweredUp;
    };

exports.poweredUpRoutes = {
    discover(req, res) {
        getPoweredUp().discover(req.body.autoConnect || []);
        res.send(JSON.stringify({}));
    },

    deviceList(req, res) {
        let poweredUp   = getPoweredUp();
        let autoConnect = req.body.autoConnect || [];
        let list        = poweredUp.getDeviceList(autoConnect);
        res.send(JSON.stringify({result: true, changed: poweredUp.getChanged(), list: list}));
    },

    connectedDeviceList(req, res) {
        let poweredUp   = getPoweredUp();
        let autoConnect = req.body.autoConnect || [];
        res.send(JSON.stringify({result: true, changed: poweredUp.getChanged(), list: poweredUp.getConnectedDeviceList(autoConnect)}));
    },

    connect(req, res) {
        let uuid = req.body.uuid;
        getPoweredUp().connect(
            uuid,
            function(hub) {
                res.send(JSON.stringify(hub));
            }
        );
    },

    disconnect(req, res) {
        getPoweredUp().disconnect(() => {});
        res.send(JSON.stringify({}));
    },

    disconnectAll(req, res) {
        getPoweredUp().disconnectAll(() => {});
        res.send(JSON.stringify({}));
    },

    connecting(req, res) {
        let connected = getPoweredUp().getConnected();
        let state     = {};
        if (connected) {
            state = getPoweredUp().getState();
        }
        res.send(JSON.stringify({
            connected: connected,
            state:     state
        }));
    },

    connected(req, res) {
        res.send(JSON.stringify({connected: getPoweredUp().getConnected()}));
    },

    update(req, res) {
        let result           = {error: false, connected: true};
        let queue            = (typeof req.body.queue === 'string') ? JSON.parse(req.body.queue) : req.body.queue;
        let messagesReceived = {};
        let poweredUp        = getPoweredUp();
        queue.forEach((params) => {
            poweredUp.module(params.module, params.command, params.data);
            messagesReceived[params.messageId] = true;
        });
        result.state            = poweredUp.getState();
        result.messagesReceived = messagesReceived;
        res.send(JSON.stringify(result));
    },

    _createTimeoutCallback(res) {
        let done     = false;
        let timeout  = null;
        let callback = function(success) {
                if (done) {
                    return;
                }
                if (timeout) {
                    clearTimeout(timeout);
                    timeout = null;
                }
                done = true;
                res.send(JSON.stringify({error: !success}));
            };
        timeout = setTimeout(callback, 500);
        return callback;
    },

    stopAllMotors(req, res) {
        let result     = {success: true};
        let layerCount = req.body.layerCount;
        let brake      = req.body.brake ? 1 : 0;
{'fraction_non_alphanumeric': 0.13864692218350755, 'fraction_numerical': 0.002613240418118467, 'mean_word_length': 1.9265080713678844, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '5977108', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1922, 'n_words': 363}