Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
SunHaoli opened this issue Jul 27, 2017 · 2 comments
Closed

Comments

@SunHaoli
Copy link

SunHaoli commented Jul 27, 2017

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);
   });	
   ...
});
@AdamMagaluk
Copy link
Contributor

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:

@SunHaoli
Copy link
Author

SunHaoli commented Aug 1, 2017

@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
     });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants