Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var request = require("request");
- module.exports = {
- getSerie: function (idDevice, sensorName, startDate, sendFunction) {
- connect(function (error, response, body) {
- var token = JSON.parse(body)["access_token"];
- var url = 'https://api.weenat.com/v2/values/series/device/' + idDevice + '/?start=' + startDate + '&step=all';
- if (error) throw new Error(error);
- request({
- method: 'GET',
- url: url,
- headers: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'cache-control': 'no-cache',
- "Authorization": 'Bearer ' + token
- }
- }, function (error, response, data) {
- if (error) throw new Error(error);
- var d;
- var found = false;
- var index = 0;
- try {
- d = JSON.parse(data);
- } catch (err) {
- d = null;
- }
- if (d && d.series) {
- while (!found && index < d.series.length) {
- if (d.series[index].name === sensorName) {
- found = true;
- } else {
- ++index;
- }
- }
- if (found) {
- sendFunction(buildSerie(d.series[index].values, parseInt(startDate)));
- } else {
- sendFunction([]);
- }
- } else {
- sendFunction([]);
- }
- });
- });
- },
- getDevices: function (done) {
- var url = 'https://api.weenat.com/v2/weenats/';
- getData(url,function (error,data) {
- console.log(data);
- done(data);
- });
- },
- getLast: function (idDevice, sensors, callback) {
- connect(function (error, response, body) {
- var token = JSON.parse(body)["access_token"];
- var url = 'https://api.weenat.com/v2/values/last/device/' + idDevice + '/';
- if (error) throw new Error(error);
- request({
- method: 'GET',
- url: url,
- headers: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'cache-control': 'no-cache',
- "Authorization": 'Bearer ' + token
- }
- }, function (error, response, data) {
- callback(JSON.parse(data));
- });
- });
- }
- };
- var connect = function (response) {
- request({
- method: 'POST',
- url: 'https://api.weenat.com/o/token/',
- headers: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'cache-control': 'no-cache'
- }, form: {
- grant_type: 'password',
- username: 'eric.ramat@gmail.com',
- password: 'ulco127',
- client_id: 'HHbtIugeHhe2R1acAneDRwFokDdpyXQ0PLxmJolw',
- client_secret: 'tt9Ot2fIjX6BxIFfyzvf1tX4U8AJqIXiOJgc7lxaAeEvtcMACBZjqQSrL0gqQylNbB4UUoHrRwibtQLALoRdPYhHljRFooAMembOluJTVrzZXqAtEnrlIVu2fu9EChCy'
- }
- }, response);
- };
- var getData = function (url, done) {
- connect(function (error, response, body) {
- try {
- var token = JSON.parse(body)["access_token"];
- } catch (e) {
- //console.log(colors.red.bold("Failed to parse token from Weenat for deviceId :" + device.id));
- console.log("error: ", error);
- console.log("body: ", body);
- done([]);
- return;
- }
- if (error) done([]);
- request({
- method: 'GET',
- url: url,
- headers: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'cache-control': 'no-cache',
- "Authorization": 'Bearer ' + token
- }
- }, function (error, response, data) {
- console.log(url);
- console.log(data);
- done(error, data);
- });
- });
- };
- var buildSerie = function (data, begin) {
- var serie = [];
- var t = begin;
- var next = t + 24 * 3600;
- var now = Math.floor(Date.now() / 1000);
- var index = 0;
- var sum = 0;
- if (data.length > 0) {
- while (data[index][0] / 1000 > next) {
- serie.push([t, 0]);
- t = next;
- next += 24 * 3600;
- }
- while (index < data.length) {
- var d = data[index][0] / 1000;
- if (t <= d && d < next) {
- serie.push([d, data[index][1]]);
- ++index;
- } else {
- t = next;
- next += 24 * 3600;
- while (d > next) {
- serie.push([t, 0]);
- t = next;
- next += 24 * 3600;
- }
- }
- }
- /* t = next;
- while (t < now) {
- serie.push([t, 0]);
- t += 24 * 3600;
- } */
- }
- return serie;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement