Can Zetta APP collect and process data form multiple sensors of the same type?

51 views
Skip to first unread message

Haoli Sun

unread,
Jul 25, 2017, 11:24:15 PM7/25/17
to zetta-discuss
I am trying to develop a Zetta server, in which the APP can collect and process (statics, analyze and so on) data from multiple sensors of the same type, and the number of sensors is undefined. Is it possible to implement this function by Zetta? 

Haoli Sun

unread,
Jul 31, 2017, 11:42:13 PM7/31/17
to zetta-discuss
I'm here to answer my own question, finally the problem is solved by basic node.js features which I wasn't familiar with.

In the app.js:
First, a changeable sequence of queries  is used as the first argument of "server.observe(queries, cb)".
this.queries = new Array();
for (var i=0; i < this.sensor_number; i++) { //start from 0
const pm25Query = server.from('*').where({type: 'pm_sensor', name: 'pm_sensor_'+i});
this.queries.push(pm25Query);
}

Second, Array.forEach() of Javascript is used to receive data from each sensor.
this.pmSensors = new Array();
for (var i=1; i <= this.sensor_number; i++) { //start from 1, can be changed later
this.pmSensors.push('pmsensor');
}
this.pmSensors.forEach(processValue);
function processValue(sensor, index, arr) {
console.log(sensor);
console.log(index);
server.observe(this.queries[index], (sensor) => {
      ...//calculate the average of all sensors
     });
}

I also thought to modify the code of server.observe(queries, cb), but the current solution may be better.


在 2017年7月26日星期三 UTC+8上午11:24:15,Haoli Sun写道:
Reply all
Reply to author
Forward
0 new messages