<commit_before>/* jshint node: true */
'use strict';

var Q = require('q'),
    _ = require('busyman'),
    Ziee = require('ziee');

var Device = require('../model/device'),
    Endpoint = require('../model/endpoint');

var loader = {};

loader.reloadSingleDev = function (shepherd, devRec, callback) {
    var deferred = Q.defer(),
        dev = shepherd._devbox.get(devRec.id);

    if (dev && isSameDevice(dev, devRec)) {
        deferred.resolve(null);  // same dev exists, do not reload
        return deferred.promise.nodeify(callback);
    } else if (dev) {
        devRec.id = null;        // give new id to devRec
    }

    var recoveredDev = new Device(devRec);

    recoveredDev._recoverFromRecord(devRec, shepherd);
    return shepherd._registerDev(recoveredDev, callback);    // return (err, id)
};

loader.reloadDevs = function (shepherd, callback) {
    var deferred = Q.defer(),
        recoveredIds = [];

    Q.ninvoke(shepherd._devbox, 'findFromDb', {}).then(function (devRecs) {
        var total = devRecs.length;

        devRecs.forEach(function (devRec) {
            if (devRec.nwkAddr === 0) {  // coordinator
                total -= 1;
                if (total === 0)         // all done
                    deferred.resolve(recoveredIds);
            } else {
                loader.reloadSingleDev(shepherd, devRec).then(function (id) {
                    recoveredIds.push(id);
                }).fail(function (err) {
                    recoveredIds.push(null);
                }).done(function () {
                    total -= 1;
                    if (total === 0)     // all done
                        deferred.resolve(recoveredIds);
                });
            }
        });
    }).fail(function (err) {
        deferred.reject(err);
    }).done();

    return deferred.promise.nodeify(callback);
};

loader.reload = function (shepherd, callback) {
    var deferred = Q.defer();

    loader.reloadDevs(shepherd).then(function (devIds) {<commit_msg>improve loader robustness and logging<commit_after>/* jshint node: true */
'use strict';

var Q = require('q'),
    debug = {
        shepherd: require('debug')('zigbee-shepherd')
    };

var Device = require('../model/device'),
    Endpoint = require('../model/endpoint');

var loader = {};

loader.reloadSingleDev = function (shepherd, devRec, callback) {
    var dev = shepherd._devbox.get(devRec.id);

    if (dev && isSameDevice(dev, devRec)) {
        return Q(null).nodeify(callback);
    } else if (dev) {
        devRec.id = null;        // give new id to devRec
    }

    var recoveredDev = new Device(devRec);

    recoveredDev._recoverFromRecord(devRec, shepherd);
    return shepherd._registerDev(recoveredDev, callback);    // return (err, id)
};

loader.reloadDevs = function (shepherd, callback) {
    var recoveredIds = [];

    return Q.ninvoke(shepherd._devbox, 'findFromDb', {}).then(function (devRecs) {
        var all = devRecs.map(function (devRec) {
            return function(){
                if (devRec.nwkAddr !== 0) {  // coordinator
                    return loader.reloadSingleDev(shepherd, devRec).then(function (id) {
                        recoveredIds.push(id);
                    }).fail(function (err) {
                        recoveredIds.push(null);
                        debug.shepherd("Unable to load device record due to %s", err)
                    })
                }
            }
        });

        return Q.all(all)
    }).then(function(){
        return recoveredIds
    }, function (err) {
        debug.shepherd("Unable to load device records due to %s", err)
        deferred.reject(err);
    }).nodeify(callback);
};

loader.reload = function (shepherd, callback) {
    var deferred = Q.defer();

    loader.reloadDevs(shepherd).then(function (devIds) {
{'fraction_non_alphanumeric': 0.13866317169069461, 'fraction_numerical': 0.0015727391874180866, 'mean_word_length': 2.079903147699758, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6578655', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1088, 'n_words': 249}