import {Base} from "@/utils/usbSerial/base";
import {logs} from "@/utils/util";
import {orderToData} from "@/utils/ble/hex";

export class BmiMachine extends Base {

  constructor(topClass, usbSerial) {
    super(topClass, usbSerial)

    this.id = 'bmi'
    this.title = 'BMI体重秤'
    this.config = {
      baudRate: 9600,
      dataBits: 8,
      stopBits: 1,
      parity: 0,
      flowControl: 0,
    }
  }


  read(resp) {
    // resp.value = ASCIIUtils.decodeUtf8(resp.data)
    // logs('bmi usbSerial.read', resp)
    // 7.40kg   98.5cm   9???
    // let str = '0240303037343030303938353030303030303030303030303030303030303030303030303030303037373030303030403503'
    let resArr = this.packetManager(resp.data)
    resArr.forEach(it => {
      let code = it.substring(2, 4)
      let orderContent = it.slice(4, -6)
      let result = {}
      let acceptOrder = this.getAcceptOrder(code)
      if (acceptOrder) result = orderToData(orderContent, acceptOrder.c, 0)
      if (this.notifyFun) this.notifyFun(result)
    })
  }


  getAcceptOrder(cmd) {
    let order = {
      '40': {
        l: 20,
        c: [
          {t: 4, k: 'kg', l: 10, fun: (it) => it ? it / 100 : 0},
          {t: 4, k: 'cm', l: 10, fun: (it) => it / 10},
          {t: 4, k: 'k3', l: 10, fun: (it) => it / 10},
          {t: 4, k: 'k4', l: 10, fun: (it) => it / 10},
          {t: 4, k: 'k5', l: 10, fun: (it) => it / 10},
          {t: 4, k: 'k6', l: 10, fun: (it) => it / 10},
          {t: 4, k: 'k7', l: 10},
          {t: 4, k: 'k8', l: 10, fun: (it) => it / 10},
          {t: 4, k: 'k9', l: 10, fun: (it) => it / 10},
        ]
      },
    }
    return order[cmd] || null
  }

}
