zettajs/zetta

Can Zetta APP collect and process data form changeable number of sensors?

Closed this issue · 2 comments

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 changeable. Is it possible to implement this function by Zetta?

  server.observe([pm25Query_1, pm25Query_2, ...], (pmSensor_1, pmSensor_2, ...) => {
   console.log("devices came online");
   console.log(pmSensor_1);
   console.log(pmSensor_2);
   pmSensor_1.streams.result.on("data", (m) => {
      var pm25 = m.data.value
      console.log("pm25_1: ", pm25);
   });
   pmSensor_2.streams.result.on("data", (m) => {
      var pm25 = m.data.value
      console.log("pm25_2: ", pm25);
   });	
   ...
});

Zetta in itself does not offer any capabilities to process the data but you're on the right track for how to collect data using Zetta.

See our dusk to dawn example that would "process" a fake light sensor and turn the lights on when it is dark. https://github.com/zettajs/zetta-hello-world/blob/master/apps/dusk_to_dawn_light.js To further process the data you will need to pipe the data into another tool. There are many open source options depending on what you're trying todo.

We have also published a number of data collectors that collects all data from every device and pumps the data into different tools:

@AdamMagaluk
Thank you for your reply, we eventually realized the function to process data within Zetta. Although the code is not perfect, we can process changeable numbers of sensors of the same type.

In the app.js:
First, a changeable sequence of queries is used as the first argument of "server.observe(query, 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 sensor data
     });
}