/** * Este archivo es parte de EMA Libre y brinda la funcionalidad de envio de datos * metorologicos recolectados por EMA Libre a una red EMA CENTER. * * Este archivo, contiene metodos que se encargan de enviar (por POST o GET) y detectar si dicho envio * fue recibido correctamente por la red, basandose en el mensaje emitido por la red. * * LICENSE: This file is part of EMA Libre. * EMA Libre is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * EMA Libre 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with EMA Libre. If not, see . * * @copyright Copyright (c) 2019 GUGLER (http://www.gugler.com.ar) * @license https://www.gnu.org/licenses/gpl-3.0.html GPL License * @version 3.0 * @link http://www.gugler.com.ar * @since File available since Release 3.0 */ (function () { 'use strict'; var server = { host: '127.0.0.1', port: 443, fijo: '/post/', path: '/post/', method: 'GET', llave: '' }; var fields = [ 'fecha', 'temperatura_externa', 'temperatura_interna', 'humedad_interna', 'humedad_externa', 'punto_de_rocio', 'luxer_uv', 'luxer_intencidad', 'viento_direccion', 'viento_direccion_nombre', 'viento_rafagas', 'viento_velocidad', 'lluvia_actual', 'lluvia_acumulado_hora', 'lluvia_acumulado_diario', 'lluvia_acumulado_semanal', 'lluvia_acumulado_mensual', 'lluvia_acumulado_anual', 'presion_relativa', 'presion_absoluta', 'software', 'tanque' ]; var timeout = 5000; function EMACENTER (HOST, PORT, LLAVE) { server.host = HOST; server.port = PORT; server.llave = LLAVE; // Segun el puerto se carga libreria para HTTPS o HTTP var http; if (server.port === 443) { http = require('https'); } else { http = require('http'); } if (!(this instanceof EMACENTER)) return new EMACENTER(HOST, PORT, LLAVE); var payload = { HOST: HOST || '', LLAVE: LLAVE || '' }; this.setObservations = function () { if (arguments.length > 0) { switch (typeof arguments[0]) { // Consulta el tipo de dato case 'object': var observations = arguments[0]; if (Object.prototype.toString.call(observations) === '[object Object]') { Object.keys(observations).forEach(function (observation) { if (fields.indexOf(observation) > -1) { payload[observation] = String(observations[observation]).toString(); } }); return true; } else { return new Error('Invalido dato en setObservations().'); } break; case 'string': var observation = arguments[0]; var reading = arguments[1] || ''; if (fields.indexOf(observation) > -1) { payload[observation] = reading; return true; } else { return new Error('Dato ' + observation + ' no soportado por EMACENTER.'); } break; default: return new Error('Invalido argumento en setObservations().'); } } else { return new Error('Dato no soportado en setObservations().'); } }; this.getObservations = function () { return payload; }; this.resetObservations = function () { payload = {}; return true; }; this.getFields = function () { return fields; }; this.setRequestTimeout = function (miliseconds) { if (!isNaN(Number(miliseconds))) { timeout = miliseconds; return timeout; } else { return new Error('Timeout excedido.'); } }; this.getRequestTimeout = function () { return timeout; }; this.sendObservations = function (callback) { if (typeof callback !== 'function') callback = function () {}; var queryString = []; var that = this; Object.keys(payload).forEach(function (observation) { queryString.push(observation + '=' + encodeURIComponent(payload[observation]).replace('%2B', '+')); }); server.path = server.fijo + server.llave + '/'; server.path += '?' + queryString.join('&'); // alert(server.path); // if(APP.Configuracion.debug>4) console.log('EMACENTER -> URL GET: '+server.path); var request = http.request(server, function (response) { var message = ''; response.on('data', function (chunk) { message += chunk; }); response.on('end', function () { // alert(response.statusCode); var result = message.trim(); if (response.statusCode === 200) { // RESPUESTA HTML Y COMPARA that.resetObservations(); callback(null, result); } else { var detalleError = 'Codigo de error: ' + response.statusCode; // ERROR callback(new Error(detalleError + server.path), null); } }); }); request.on('socket', function (socket) { socket.setTimeout(timeout); socket.on('timeout', function () { request.abort(); }); }).on('error', function (error) { callback(error, null); }); request.end(); }; } module.exports = EMACENTER; }());