【问题标题】:Calculate Time and insert in array计算时间并插入数组
【发布时间】: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] ]
        }));
      }
    );

在您看来,我该如何解决此类问题?谢谢。

【问题讨论】:

    标签: javascript react-native


    【解决方案1】:

    您可以使用 setInterval() 函数来记录每个状态的时间:

    var Time = 0;
    
    function resetT()
    {
    Time=0;
    clearInterval(stateT)
    console.log("state_reset")
    }
    
    function stateInitiate(t)
    {
    //t will be your unit of time (1 = a millisecond, 1000 = a second)
    setInterval(stateT,t)
    }
    
    function stateT()
    {
     Time++;
    }
    
    function init()
    {
    device.monitorCharacteristicForService(
          service,
          this.Pressure,
          (error, characteristic) => {
            if (error) {
              this.error(error.message);
              return;
            }
            console.log("TimeP - ", Time)//variable time
            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] ]
            }));
          }
        );
    stateInitiate(1)//adds 1ms every ms
    }
    init()//begin sequence
    

    如果这就是你要找的东西,请告诉我... :)

    【讨论】:

    • 这就是我想要做的:(
    • 哦,好吧,从你的例子来看,你似乎可以:),你能以某种方式创建一个记录时间常数的变量吗?
    • 我曾尝试使用 "this.setState({timeP: this.state.time})" 但结果不正确。你有什么想法吗? :)
    • 实际上,您是否尝试记录从状态开始到状态将要更改的时间?
    • 我能想到的唯一另一件事是在创建状态时创建一个 setInterval() ,每毫秒计数一次。然后你可以调用这个变量来查看状态改变需要多长时间......如果没有,那么唯一的另一件事就是控制台记录 this.state 变量并查看其中的内容。 :)
    猜你喜欢
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2019-07-21
    • 2021-11-12
    • 1970-01-01
    相关资源
    最近更新 更多