js对接欧姆龙OMRON血压仪获取数据(需要服务端解析)

获取navigator

mounted() {
    this.navigator = window.navigator;
    if (!window.device) {
      this.$emit("changeValue", "1");
    }

    // // 全局HID设备拔出事件
    navigator.hid.ondisconnect = event => {
      this.$emit("changeValue", "1");
    };
  },

连接设备

    async requestDevice() {
      const [device] = await this.navigator.hid.requestDevice({
        filters: [
          {
            vendorId: 0x0590,
          },
        ],
      });
      if (device && !device.opened) {
        // 检查设备是否打开
        this.$emit("changeValue", "2");
        await device.open(); // 打开设备
      }
      if (device && device.opened) {
        this.$emit("changeValue", "3");
        window.device = device;

        // 设置数据监听器
        window.device.oninputreport = event => {
          let { data, device, reportId } = event;
          this.buffer = new Uint8Array(data.buffer);
        };
      }
    },

测完之后,获取结果

    getInfo() {
      if (this.buffer) {
        //交给后台解析
        this.$api.postOmronBloodData({
              result: btoa(String.fromCharCode.apply(null, this.buffer)),
            })
            .then(res => {
              let { sys, pulse, dia } = res.data;
              if (res.data) {
                this.ruleForm.highPressure = sys;
                this.ruleForm.lowPressure = dia;
                this.ruleForm.heartRate = pulse;
                this.$message.success("获取成功");
              } else {
                this.$message.error(`${res.msg}`);
              }
              // sys: 收缩压, pulse: 脉搏, dia: 舒张压
            })
            .catch(err => {
              this.$message.error("请重新测量");
            });
      } else {
        this.$message.error("暂无数据");
      }
    },

后台解析

@ApiOperation("欧姆龙血压计")
@PostMapping("/omronSphygmomanometer")
public AjaxResult omronSphygmomanometer(@RequestBody ReceiveInfoBo receiveInfoBo) {
byte[] bytes= Base64.getMimeDecoder().decode(receiveInfoBo.getResult());
int i = 0;
byte a=0;
byte b=0;
byte c=0;
byte d=0;
byte e=0;
byte f=0;
byte g=0;
byte h=0;
byte m=0;
byte n=0;
for (byte by : bytes) {
switch (i) {
case 5:
a=by;
break;
case 6:
b=by;
break;
case 7:
c=by;
break;
case 8:
d=by;
break;
case 9:
e=by;
break;
case 10:
f=by;
break;
case 11:
g=by;
break;
case 12:
h=by;
break;
case 13:
m=by;
break;
case 14:
n=by;
break;
}
i = i + 1;
}
byte[] byteSys = new byte[]{a,b,c,d};
byte[] byteDia =new byte[]{e,f,g,h};
byte[] bytePulse =new byte[]{m,n};
int sys = Integer.parseInt(new String(byteSys, Charset.forName("UTF-8")), 16) / 128;
int dia = Integer.parseInt(new String(byteDia, Charset.forName("UTF-8")), 16) / 128;
int pulse = Integer.parseInt(new String(bytePulse, Charset.forName("UTF-8")), 16);
JSONObject jsonObject = new JSONObject();
jsonObject.set("sys", sys);
jsonObject.set("dia", dia);
jsonObject.set("pulse", pulse);
return new AjaxResult(200,"成功",jsonObject);
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值