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写道: