daily pastebin goal
44%
SHARE
TWEET

promisses crazy

a guest Dec 12th, 2018 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calculateTheParameter(index, calculated, sensor, measurement, sensors) {
  2.   return new Promise((resolve, reject) => {
  3.     // Create params and args and push own measurement as first one
  4.     let params = [];
  5.     //params.push(sensor.template.code);
  6.     let args = [];
  7.     //args.push(measurement.value);
  8.     // Get the external params
  9.     let baseCode = sensor.template.code;
  10.     for(let param of calculated.params) {
  11.       if(!params.includes(param)) {
  12.         params.push(param);
  13.       }
  14.     }
  15.     let tasks = [];
  16.     for(let i = 0; i < params.length; i++) {
  17.       if(params[i].indexOf(baseCode != 0)){
  18.         tasks.push(getTheExternalParam(i, params[i], measurement.date, sensors));
  19.       }else{
  20.         args.push(measurement.value);
  21.       }
  22.     }
  23.     measurement.args = args;
  24.     Promise.all(tasks)
  25.       .then((results) => {
  26.         results.map((result) => args[result.index] = result.data);
  27.         try {
  28.           let func = Function(params, calculated.formula);
  29.           var calc = func.apply({}, args) || 0;
  30.         } catch(e) {
  31.           reject(e);
  32.         }
  33.         resolve({index: index, data: Math.round( calc * 10 ) / 10});
  34.       }).catch((err) => reject(err));
  35.   });
  36. }
  37. //here do the calc but need measurements of other sensors
  38. function calculateTheMeasurements(index, sensor, measurement, sensors) {
  39.   return new Promise((resolve, reject) => {
  40.     measurement.calculated = [];
  41.     // Go through all calculated parameters to process
  42.     let tasks = sensor.calculated.map((calculated, ci) => calculateTheParameter(ci, calculated, sensor, measurement, sensors));
  43.     Promise.all(tasks)
  44.       .then((results) => {
  45.         measurement.calculated = [];
  46.         results.map((result) => measurement.calculated[result.index] = result.data);
  47.         resolve({index: index, data: measurement});
  48.       })
  49.       .catch((err) => reject(err));
  50.   });
  51. }
  52. //we got to calculate for each measurement in main sensor measurements...
  53. function processTheCalculatedParameters(res,sensor, measurements, sensors){
  54.   let tasks = measurements.map((measurement, i) => calculateTheMeasurements(i, sensor, measurement, sensors));
  55.   Promise.all(tasks)
  56.     .then((results) => {
  57.       measurements = [];
  58.       results.map((result) => measurements[result.index] = result.data);
  59.       return responses.successResponse(res, measurements);
  60.     })
  61.     .catch((err) => {
  62.       logger.error('sensors: Error on calculated parameters:', err.message);
  63.       return responses.genericError(res);
  64.     });
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top