Advertisement
Guest User

weenatProvider.js

a guest
May 22nd, 2017
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var request = require("request");
  2.  
  3. module.exports = {
  4.   getSerie: function (idDevice, sensorName, startDate, sendFunction) {
  5.     connect(function (error, response, body) {
  6.       var token = JSON.parse(body)["access_token"];
  7.       var url = 'https://api.weenat.com/v2/values/series/device/' + idDevice + '/?start=' + startDate + '&step=all';
  8.  
  9.       if (error) throw new Error(error);
  10.       request({
  11.         method: 'GET',
  12.         url: url,
  13.         headers: {
  14.           'content-type': 'application/x-www-form-urlencoded',
  15.           'cache-control': 'no-cache',
  16.           "Authorization": 'Bearer ' + token
  17.         }
  18.       }, function (error, response, data) {
  19.         if (error) throw new Error(error);
  20.  
  21.         var d;
  22.         var found = false;
  23.         var index = 0;
  24.  
  25.         try {
  26.           d = JSON.parse(data);
  27.         } catch (err) {
  28.           d = null;
  29.         }
  30.         if (d && d.series) {
  31.           while (!found && index < d.series.length) {
  32.             if (d.series[index].name === sensorName) {
  33.               found = true;
  34.             } else {
  35.               ++index;
  36.             }
  37.           }
  38.           if (found) {
  39.             sendFunction(buildSerie(d.series[index].values, parseInt(startDate)));
  40.           } else {
  41.             sendFunction([]);
  42.           }
  43.         } else {
  44.           sendFunction([]);
  45.         }
  46.       });
  47.     });
  48.   },
  49.  
  50.   getDevices: function (done) {
  51.       var url = 'https://api.weenat.com/v2/weenats/';
  52.       getData(url,function (error,data) {
  53.         console.log(data);
  54.           done(data);
  55.       });
  56.  
  57.   },
  58.  
  59.  
  60.   getLast: function (idDevice, sensors, callback) {
  61.     connect(function (error, response, body) {
  62.       var token = JSON.parse(body)["access_token"];
  63.       var url = 'https://api.weenat.com/v2/values/last/device/' + idDevice + '/';
  64.  
  65.       if (error) throw new Error(error);
  66.       request({
  67.         method: 'GET',
  68.         url: url,
  69.         headers: {
  70.           'content-type': 'application/x-www-form-urlencoded',
  71.           'cache-control': 'no-cache',
  72.           "Authorization": 'Bearer ' + token
  73.         }
  74.       }, function (error, response, data) {
  75.         callback(JSON.parse(data));
  76.       });
  77.     });
  78.   }
  79. };
  80.  
  81. var connect = function (response) {
  82.   request({
  83.     method: 'POST',
  84.     url: 'https://api.weenat.com/o/token/',
  85.     headers: {
  86.       'content-type': 'application/x-www-form-urlencoded',
  87.       'cache-control': 'no-cache'
  88.     }, form: {
  89.       grant_type: 'password',
  90.       username: 'eric.ramat@gmail.com',
  91.       password: 'ulco127',
  92.       client_id: 'HHbtIugeHhe2R1acAneDRwFokDdpyXQ0PLxmJolw',
  93.       client_secret: 'tt9Ot2fIjX6BxIFfyzvf1tX4U8AJqIXiOJgc7lxaAeEvtcMACBZjqQSrL0gqQylNbB4UUoHrRwibtQLALoRdPYhHljRFooAMembOluJTVrzZXqAtEnrlIVu2fu9EChCy'
  94.     }
  95.   }, response);
  96. };
  97.  
  98. var getData = function (url, done) {
  99.     connect(function (error, response, body) {
  100.         try {
  101.             var token = JSON.parse(body)["access_token"];
  102.         } catch (e) {
  103.             //console.log(colors.red.bold("Failed to parse token from Weenat for deviceId :" + device.id));
  104.             console.log("error: ", error);
  105.             console.log("body: ", body);
  106.             done([]);
  107.             return;
  108.         }
  109.  
  110.         if (error) done([]);
  111.         request({
  112.             method: 'GET',
  113.             url: url,
  114.             headers: {
  115.                 'content-type': 'application/x-www-form-urlencoded',
  116.                 'cache-control': 'no-cache',
  117.                 "Authorization": 'Bearer ' + token
  118.             }
  119.         }, function (error, response, data) {
  120.  
  121.             console.log(url);
  122.             console.log(data);
  123.  
  124.             done(error, data);
  125.         });
  126.     });
  127. };
  128.  
  129.  
  130. var buildSerie = function (data, begin) {
  131.   var serie = [];
  132.   var t = begin;
  133.   var next = t + 24 * 3600;
  134.   var now = Math.floor(Date.now() / 1000);
  135.   var index = 0;
  136.   var sum = 0;
  137.  
  138.   if (data.length > 0) {
  139.     while (data[index][0] / 1000 > next) {
  140.       serie.push([t, 0]);
  141.       t = next;
  142.       next += 24 * 3600;
  143.     }
  144.  
  145.     while (index < data.length) {
  146.       var d = data[index][0] / 1000;
  147.  
  148.       if (t <= d && d < next) {
  149.         serie.push([d, data[index][1]]);
  150.         ++index;
  151.       } else {
  152.         t = next;
  153.         next += 24 * 3600;
  154.         while (d > next) {
  155.           serie.push([t, 0]);
  156.           t = next;
  157.           next += 24 * 3600;
  158.         }
  159.       }
  160.     }
  161. /*    t = next;
  162.     while (t < now) {
  163.       serie.push([t, 0]);
  164.       t += 24 * 3600;
  165.     } */
  166.   }
  167.   return serie;
  168. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement