<commit_before>/*
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of iotagent-lwm2m-lib
 *
 * iotagent-lwm2m-lib 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-lwm2m-lib 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-lwm2m-lib.
 * 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 async = require('async'),
    apply = async.apply,
    coap = require('coap'),
    Readable = require('stream').Readable,
    errors = require('../errors'),
    registry = require('./deviceRegistry');

function sendRequest(request, callback) {
    var req = coap.request(request),
        rs = new Readable();

    req.on('response', function(res) {
        var data = '';

        res.on('data', function (chunk) {
            data += chunk;
        });

        res.on('end', function(chunk) {
            if (chunk) {
                data += chunk;
            }
            callback(null, res);
        });
    });

    if (request.payload) {
        rs.push(request.payload);
        rs.push(null);
        rs.pipe(req);
    } else {
        req.end();
    }
}

/**
 * Execute a read operation for the selected resource, identified following the LWTM2M conventions by its: deviceId,
 * objectType, objectId and resourceId.
 */
function read(deviceId, objectType, objectId, resourceId, callback) {
    function createReadRequest(obj, callback) {
        var request = {
            host: obj.address,
            port: 5683,
            method: 'GET',
            pathname: '/' + objectType + '/' + objectId + '/' + resourceId
        };

        callback(null, request);
    }

    function processResponse(res, callback) {
        if (res.code === '2.05') {
            callback(null, res.payload.toString('utf8'));
        } else {
            callback(new errors.ClientError(res.code));
        }
    }

    async.waterfall([
        apply(registry.get, deviceId),
        createReadRequest,
        sendRequest,
        processResponse
    ], callback);
}

function write(deviceId, objectType, objectId, resourceId, payload, callback) {
    function createUpdateRequest(obj, callback) {
        var request = {
            host: obj.address,
            port: 5683,
            method: 'PUT',
            pathname: '/' + objectType + '/' + objectId + '/' + resourceId,
            payload: payload
        };

        callback(null, request);
    }

    function processResponse(res, callback) {
        if (res.code === '2.04') {
            callback(null, res.payload.toString('utf8'));
        } else {
            callback(new errors.ClientError(res.code));
        }
    }

    async.waterfall([
        apply(registry.get, deviceId),<commit_msg>ADD JSDoc in the new methods<commit_after>/*
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of iotagent-lwm2m-lib
 *
 * iotagent-lwm2m-lib 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-lwm2m-lib 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-lwm2m-lib.
 * 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 async = require('async'),
    apply = async.apply,
    coap = require('coap'),
    Readable = require('stream').Readable,
    errors = require('../errors'),
    registry = require('./deviceRegistry');

/**
 * Send the COAP Request passed as a parameter. If the request contains a parameter "payload", the parameter is sent
 * as the payload of the request; otherwise, the request is sent without any payload.
 *
 * @param {Object} request          Object containing all the request information (in the Node COAP format).
 */
function sendRequest(request, callback) {
    var req = coap.request(request),
        rs = new Readable();

    req.on('response', function(res) {
        var data = '';

        res.on('data', function (chunk) {
            data += chunk;
        });

        res.on('end', function(chunk) {
            if (chunk) {
                data += chunk;
            }
            callback(null, res);
        });
    });

    if (request.payload) {
        rs.push(request.payload);
        rs.push(null);
        rs.pipe(req);
    } else {
        req.end();
    }
}

/**
 * Execute a read operation for the selected resource, identified following the LWTM2M conventions by its: deviceId,
 * objectType, objectId and resourceId.
 */
function read(deviceId, objectType, objectId, resourceId, callback) {
    function createReadRequest(obj, callback) {
        var request = {
            host: obj.address,
            port: 5683,
            method: 'GET',
            pathname: '/' + objectType + '/' + objectId + '/' + resourceId
        };

        callback(null, request);
    }

    function processResponse(res, callback) {
        if (res.code === '2.05') {
            callback(null, res.payload.toString('utf8'));
        } else {
            callback(new errors.ClientError(res.code));
        }
    }

    async.waterfall([
        apply(registry.get, deviceId),
        createReadRequest,
        sendRequest,
        processResponse
    ], callback);
}

/**
 * Execute a Write operation over the selected resource, identified following the LWTM2M conventions by its: deviceId,
 * objectType, objectId and resourceId, changing its value to the value passed as a parameter.
 */
function write(deviceId, objectType, objectId, resourceId, value, callback) {
    function createUpdateRequest(obj, callback) {
        var request = {
            host: obj.address,
            port: 5683,
            method: 'PUT',
            pathname: '/' + objectType + '/' + objectId + '/' + resourceId,
            payload: value
        };

        callback(null, request);
    }

    function processResponse(res, callback) {
        if (res.code === '2.04') {
            callback(null, res.payload.toString('utf8'));
        } else {
            callback(new errors.ClientError(res.code));
        }
    }

    async.waterfall([
        apply(registry.get, deviceId),
{'fraction_non_alphanumeric': 0.11422594142259414, 'fraction_numerical': 0.007391910739191074, 'mean_word_length': 2.3825471698113208, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4418318', 'n_tokens_mistral': 2021, 'n_tokens_neox': 1964, 'n_words': 633}