{"size": 1199, "ext": "js", "max_stars_repo_path": "lib/handlers/devIncomingHdlr.js", "max_stars_repo_name": "freebirdjs/freebird-netcore-mqtt", "max_stars_repo_stars_event_min_datetime": "2016-06-06T12:09:43.000Z", "max_stars_repo_stars_event_max_datetime": "2017-10-20T01:59:17.000Z", "max_issues_repo_path": "lib/handlers/devIncomingHdlr.js", "max_issues_repo_name": "freebirdjs/freebird-netcore-mqtt", "max_issues_repo_issues_event_min_datetime": "2016-07-22T04:27:20.000Z", "max_issues_repo_issues_event_max_datetime": "2016-07-22T04:27:20.000Z", "max_forks_repo_path": "lib/handlers/devIncomingHdlr.js", "max_forks_repo_name": "freebirdjs/freebird-netcore-mqtt", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 7, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 28.5476190476, "max_line_length": 82, "alphanum_fraction": 0.6013344454}
'use strict';

var _ = require('busyman'),
    helper = require('../helper');

module.exports = function (netcore, qnode) {
    // listen shepherd event: 'DEV_INCOMING'
    var rawDev = qnode.dump(),
        mac = rawDev.mac,
        clientId = rawDev.clientId,
        permAddr = helper.buildPermAddr(mac, clientId), // '00:aa:bb:...:ee/fooId'
        smartobjects = rawDev.so;

    process.nextTick(function () {
        netcore.commitDevIncoming(permAddr, rawDev);

        _.forEach(smartobjects, function (gadGroup, oid) {
            var classId = helper.getGadClassId(oid);

            if (classId)
                _commitGadGroup(netcore, permAddr, classId, gadGroup)
        });
    });
};

function _commitGadGroup(netcore, permAddr, classId, gadGroup) {
    _.forEach(gadGroup, function (resrcs, iid) {
        _commitSingleGad(netcore, permAddr, classId, iid, resrcs);
    });
}

function _commitSingleGad(netcore, permAddr, classId, iid, resrcs) {
    setImmediate(function () {
        var auxId = classId + '/' + iid;
        var x = netcore.commitGadIncoming(permAddr, auxId, {
            classId: classId,
            auxId: auxId,
            attrs: resrcs
        });
    });
}
