Carpincho/src/core/redes/awekas.js
2024-12-10 10:03:23 -03:00

220 lines
5.9 KiB
JavaScript

/**
* Este archivo es parte de EMA Libre y brinda la funcionalidad de envio de datos
* metorologicos recolectados por EMA Libre a una red AWEKAS (http://ws.awekas.at)
* 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 <http://www.gnu.org/licenses/>.
*
* @copyright Copyright (c) 2017 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
*/
var http = require('http');
(function () {
'use strict';
var server = {
host: 'ws.awekas.at',
port: 80,
fijo: '/weatherstation/updateweatherstation.php',
path: '/weatherstation/updateweatherstation.php',
method: 'GET'
};
var fields = [
'action',
'ID',
'PASSWORD',
'dateutc',
'winddir',
'windspeedmph',
'windgustmph',
'windgustdir',
'windspdmph_avg2m',
'winddir_avg2m',
'windgustmph_10m',
'windgustdir_10m',
'humidity',
'dewptf',
'tempf',
'rainin',
'dailyrainin',
'baromin',
'weather',
'clouds',
'soiltempf',
'soilmoisture',
'leafwetness',
'solarradiation',
'UV',
'visibility',
'indoortempf',
'indoorhumidity',
'AqNO',
'AqNO2T',
'AqNO2',
'AqNO2Y',
'AqNOX',
'AqNOY',
'AqNO3',
'AqSO4',
'AqSO2',
'AqSO2T',
'AqCO',
'AqCOT',
'AqEC',
'AqOC',
'AqBC',
'AqUV',
'AqPM2.5',
'AqPM10',
'AqOZONE',
'softwaretype'
];
var timeout = 5000;
function AWEKAS(ID, PASSWORD) {
if (!(this instanceof AWEKAS)) return new AWEKAS(ID, PASSWORD);
var payload = {
// action: 'updateraw',
// realtime: 1,
// rtfreq: 1,
ID: ID || '',
PASSWORD: PASSWORD || '',
dateutc: 'now'
};
this.setObservations = function () {
if (arguments.length > 0) {
switch (typeof arguments[0]) {
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('Invalid argument for 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('Observation ' + observation + ' not supported by WU.');
}
break;
default:
return new Error('Invalid argument for setObservations().');
}
} else {
return new Error('No argument supplied to setObservations().');
}
};
this.getObservations = function () {
return payload;
};
this.resetObservations = function () {
payload = {
// realtime: 1,
// rtfreq: 5,
// action: 'updateraw',
ID: payload.ID,
PASSWORD: payload.PASSWORD,
dateutc: 'now'
};
return true;
};
this.getFields = function () {
return fields;
};
this.setRequestTimeout = function (miliseconds) {
if (!isNaN(Number(miliseconds))) {
timeout = miliseconds;
return timeout;
} else {
return new Error('Invalid timeout.');
}
};
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.path += '?' + queryString.join('&');
var request = http.request(server, function (response) {
var message = '';
response.on('data', function (chunk) {
message += chunk;
});
response.on('end', function () {
var result = message.trim();
if (result === 'OK') {
that.resetObservations();
callback(null, result);
} else {
var detalle_error = result.substr(result, 100);
callback(new Error(detalle_error), 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 = AWEKAS;
}());