【发布时间】:2019-09-12 10:39:49
【问题描述】:
我有一个从外部设备读取值的应用程序。在此之后,这些值被写入数据库。
我有加速度、陀螺仪、磁力计和压力等值。
加速度、陀螺仪和磁力计是这样连同时间一起读取的:
(例如加速)
const buf = Buffer.from(characteristic.value, "base64");
const [time, ...acc] = [0,2,4,6].map(index => buf.readInt16LE(index));
this.setState(state => ({
time,
acc,
array_acc_sx: [
...state.array_acc_sx,
[time , acc ]
]
}));
对于压力我不能这样做,因为压力没有时间自动。
所以我认为设置一个变量 timeP 等于 acc,gyr,mag 的时间。
但是这样设置时间在读取压力值之前开始,所以结果是这样的:
"PL":
"[740,740,740,740,700,700,660,660,580,580,580,
560,500,500,500,500,500,440,400,400,340,340,320,300,
280,260,260,260,200,180,160,160,140,
// there start the time + pressure values.
[140,[0,0,0,0,0]],[160,[0,0,0,0,0]],[160,[0,0,0,0,0]],
[180,[0,0,0,0,0]],[200,[0,0,0,0,0]],[260,[0,0,0,0,0]],
[260,[0,0,0,0,0]],[260,[0,0,0,0,0]],[280,[0,0,0,0,0]],
[300,[0,0,0,0,0]],[320,[0,0,0,0,0]],[340,[0,0,0,0,0]],
[340,[0,0,0,0,0]],[400,[0,0,0,0,0]],[400,[0,0,0,0,0]],
[440,[0,0,0,0,0]],[500,[0,0,0,0,0]],[500,[0,0,0,0,0]],
.....
这是我使用的代码:
async setupNotifications2(device) {
const service = this.serviceGeneral();
/* Accelerometro + Giroscopio + Magnetometro */
device.monitorCharacteristicForService(
service,
this.AccGyrMg,
(error, characteristic) => {
if (error) {
this.error(error.message);
return;
}
const buf = Buffer.from(characteristic.value, "base64");
const [time, ...acc] = [0,2,4,6].map(index => buf.readInt16LE(index));
this.setState(state => ({
time,
acc,
array_acc_sx: [
...state.array_acc_sx,
[time , acc ]
]
}));
//console.log("this.state.time - ", this.state.time)
this.setState({timeP: this.state.time})
const [ ...gyr] = [8,10,12].map(index => buf.readInt16LE(index));
this.setState(state => ({
time,
gyr,
array_gyr_sx: [
...state.array_gyr_sx,
[time, gyr]
]
}));
const [ ...mg] = [14,16,18].map(index => buf.readInt16LE(index));
this.setState(state => ({
time,
mg,
array_mg_sx: [
...state.array_mg_sx,
[time, mg]
]
}));
}
);
/* Pressione */
device.monitorCharacteristicForService(
service,
this.Pressure,
(error, characteristic) => {
if (error) {
this.error(error.message);
return;
}
console.log("TimeP - ", this.state.timeP)
const buf = Buffer.from(characteristic.value, "base64");
const [...pressure_sx] = [0, 2, 4, 6, 8].map(index => buf.readUInt16LE(index));
this.setState(state => ({...state.timeP, pressure_sx,array_pressure_sx: [this.state.timeP, ...state.array_pressure_sx, [this.state.timeP, pressure_sx] ]
}));
}
);
在您看来,我该如何解决此类问题?谢谢。
【问题讨论】: