<commit_before>/*
 * Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of iotagent-onem2m
 *
 * iotagent-onem2m is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * iotagent-onem2m is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with iotagent-onem2m.
 * If not, seehttp://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::[<EMAIL>]
 */

'use strict';

var iotAgentLib = require('iotagent-node-lib'),
    aeService = require('./services/oneM2M/aeService'),
    resourceService = require('./services/oneM2M/resourceService'),
    containerService = require('./services/oneM2M/containerService'),
    subscriptionService = require('./services/oneM2M/subscriptionService'),
    configService = require('./services/configService'),
    logger = require('logops'),
    async = require('async'),
    apply = async.apply,
    context = {
        op: 'IoTAgentOneM2M.Agent'
    },
    config;

/**
 * Creates the Application Entity name based on the Device ID.
 *
 * @param {String} deviceId         Internal ID of the device.
 * @return {string}                 Name of the AE based on the given parameters.
 */
function getAEName(deviceId) {
    return deviceId;
}

/**
 * Check if a AE with the given name exists, and, if it doesn't creates a new one.
 *
 * @param {String} aeName       Name of the AE to be checked (or create).
 */
function checkAndCreateAE(aeName, callback) {
    aeService.get(aeName, function(error, result) {
        if (error) {
            aeService.createAE(aeName, callback);
        } else {
            callback(null, result);
        }
    });
}

/**
 * Handles the incoming device provisioning requests. For each device, the existance of its correspondent AE es tested,
 * and a new container for the AE with the name of the Device ID is created, along with a subscription for the contents
 * of this device.
 *
 * @param {Object} device           Information stored in the IoTA about the device.
 */
function provisioningHandler(device, callback) {
    var aeName = getAEName(device.id);

    function generateProcessHandler(attr, innerCallback) {
        return function(error, results) {
            if (error) {
                innerCallback(error);
            } else {
                attr.id = results[results.length - 1].ri;
                innerCallback(null, device);
            }
        };
    }

    function processActiveAttribute(attr, innerCallback) {
        async.series([
            apply(containerService.create, aeName, attr.name),
            apply(subscriptionService.create, aeName, attr.name, 'subs_' + attr.name)
        ], generateProcessHandler(attr, innerCallback));
    }

    function processLazy(attr, innerCallback) {
        async.series([
            apply(containerService.create, aeName, attr.name)
        ], generateProcessHandler(attr, innerCallback));
    }

    async.series([
        apply(checkAndCreateAE, aeName),
        apply(async.map, device.active, processActiveAttribute),
        apply(async.map, device.lazy, processLazy)
    ], function(error, result) {
        device.internalId = result[0].ri;

        callback(error, device);
    });
}

function generateRandomSeed() {
    return Math.floor(Math.random() * 1000000);
}

function notifyDeviceMeasure(notification, attributeName, attributeValue, attributeType, device, callback) {
    var attribute;

    for (var i = 0; i < device.active.length; i++) {
        if (device.active[i].name === attributeName) {<commit_msg>ADD Checks on error cases<commit_after>/*
 * Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of iotagent-onem2m
 *
 * iotagent-onem2m is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * iotagent-onem2m is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with iotagent-onem2m.
 * If not, seehttp://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::[<EMAIL>]
 */

'use strict';

var iotAgentLib = require('iotagent-node-lib'),
    aeService = require('./services/oneM2M/aeService'),
    resourceService = require('./services/oneM2M/resourceService'),
    containerService = require('./services/oneM2M/containerService'),
    subscriptionService = require('./services/oneM2M/subscriptionService'),
    configService = require('./services/configService'),
    logger = require('logops'),
    async = require('async'),
    apply = async.apply,
    context = {
        op: 'IoTAgentOneM2M.Agent'
    },
    config;

/**
 * Creates the Application Entity name based on the Device ID.
 *
 * @param {String} deviceId         Internal ID of the device.
 * @return {string}                 Name of the AE based on the given parameters.
 */
function getAEName(deviceId) {
    return deviceId;
}

/**
 * Check if a AE with the given name exists, and, if it doesn't creates a new one.
 *
 * @param {String} aeName       Name of the AE to be checked (or create).
 */
function checkAndCreateAE(aeName, callback) {
    aeService.get(aeName, function(error, result) {
        if (error) {
            aeService.createAE(aeName, callback);
        } else {
            callback(null, result);
        }
    });
}

/**
 * Handles the incoming device provisioning requests. For each device, the existance of its correspondent AE es tested,
 * and a new container for the AE with the name of the Device ID is created, along with a subscription for the contents
 * of this device.
 *
 * @param {Object} device           Information stored in the IoTA about the device.
 */
function provisioningHandler(device, callback) {
    var aeName = getAEName(device.id);

    function generateProcessHandler(attr, innerCallback) {
        return function(error, results) {
            if (error) {
                innerCallback(error);
            } else if (results && results.length > 0 && results[results.length - 1] && results[results.length - 1].ri) {
                attr.id = results[results.length - 1].ri;
                innerCallback(null, device);
            } else {
                innerCallback(new Error('Unknown error processing provisioning request.'));
            }
        };
    }

    function processActiveAttribute(attr, innerCallback) {
        async.series([
            apply(containerService.create, aeName, attr.name),
            apply(subscriptionService.create, aeName, attr.name, 'subs_' + attr.name)
        ], generateProcessHandler(attr, innerCallback));
    }

    function processLazy(attr, innerCallback) {
        async.series([
            apply(containerService.create, aeName, attr.name)
        ], generateProcessHandler(attr, innerCallback));
    }

    async.series([
        apply(checkAndCreateAE, aeName),
        apply(async.map, device.active, processActiveAttribute),
        apply(async.map, device.lazy, processLazy)
    ], function(error, result) {
        if (error) {
            callback(error);
        } else if (result && result[0] && result[0].ri) {
            device.internalId = result[0].ri;

            callback(null, device);
        } else {
            callback(new Error('Unknown error finding or creating AE.'));
        }
    });
}

function generateRandomSeed() {
    return Math.floor(Math.random() * 1000000);
}

function notifyDeviceMeasure(notification, attributeName, attributeValue, attributeType, device, callback) {
    var attribute;

    for (var i = 0; i < device.active.length; i++) {
        if (device.active[i].name === attributeName) {
{'fraction_non_alphanumeric': 0.10382128159905937, 'fraction_numerical': 0.006231628453850676, 'mean_word_length': 2.9072117593017914, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2188739', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2303, 'n_words': 763}