Zetta (zettajs github)是一个基于Node的IoT框架,利用zetta 可在树莓派等开发板上实现对于传感器数据的封装、发送和接收。
一般情况下,传感器会以一定的间隔向数据接收端(server)发送数据。这个一定间隔更新数据的操作一般使用setInterval()函数实现:
setInterval(() => {
this._driver.readData(counter).then((result) => {
stream.write(result);
this.value = result.value;
this.result = {'value':this.value};
console.log('value: ', this.value);
},function(err) {
console.log(err);
});
}, this.interval);
setTimeout(()=>{
this.setSendingData(this.sensor_data.length, 0, stream, killPortError);
}, 300)
mock_sensor.prototype.setSendingData = function(dataLength, index, stream, killPortError) {
console.log('index',index);
if(index < dataLength) {
stream.write(this.sensor_data[index]);
this.value = this.sensor_data[index].value;
this.result = {'value': this.value};
console.log('value', this.value);
setTimeout(()=>{
this.setSendingData(dataLength, index + 1, stream, killPortError);
}, this.interval);
}else if(index == dataLength) {
this.value = 'end';
this.result = {'value':this.value};
console.log('value: ', this.value);
setTimeout(()=>{
this.setSendingData(dataLength, index + 1, stream, killPortError);
}, 5000);
}
else {
//终止进程
var stop_command = 'start "" "' + this.project_path + '/edge/utils/kill_process_with_port" ' + this.port;
console.log('killPortError', killPortError);
if(!killPortError)
{
console.log('killPortError_process', killPortError);
process.exec(stop_command, function(err, stdout, stderr) {
console.log('Command Error: ', err);
console.log(stdout);
console.log('STD Error: ', stderr);
if(err!=null)
{
killPortError = true;
console.log('killPortError_true', killPortError);
}
});
}
}
}