export default function () {
  console.log('读卡')
  return new Promise((resolve, reject) => {
    const ffi = require('ffi')
    let dllPath = require('path').join(__static, 'pos', 'mwhrf_bj.dll')

    try {
      let reader = ffi.Library(dllPath, {
        'rf_usbopen': ['int', []],
        'rf_halt': ['int', ['int']],
        'rf_usbclose': ['short', ['int']],
        'rf_beep': ['int', ['int', 'int']],
        'rf_get_status': ['int', ['int', 'char*']],
        'rf_card': ['int', ['int', 'int', 'char*']],
        'rf_authentication': ['int', ['int', 'int', 'int']],
        'rf_read': ['int', ['int', 'int', 'char*']]
      })

      let icdev = reader.rf_usbopen()
      console.log('设备描述符', icdev)

      if (icdev > 0) {
        reader.rf_beep(icdev, 10)
        let status = Buffer.alloc(18)
        let res = reader.rf_get_status(icdev, status)
        console.log('状态', res, status.toString())

        let sn = Buffer.alloc(5)
        reader.rf_card(icdev, 0, sn)
        console.log('序列号', sn.toString())

        let sector = 3
        let a = reader.rf_authentication(icdev, 0, sector)
        if (a !== 0) {
          console.log('认证失败')
        } else {
          console.log('认证成功')
        }

        let data = Buffer.alloc(32)
        reader.rf_read(icdev, sector, data)
        console.log('读取数据', data.toString())
      }

      //   let sector = 3
      //   let a = reader.rf_authentication(icdev, 0, sector)
      //   console.log(a)
      //   let data = Buffer.alloc(32)
      //   reader.rf_read(icdev, sector, data)
      //   console.log('读取数据', data.toString())

      // 结束
      reader.rf_halt(icdev)
      reader.rf_usbclose(icdev)
    } catch (e) {
      console.log(e)
    }
    // const { fork } = require('child_process')
    // const path = require('path')

    // let child = fork(path.join(__static, 'pos', 'bj.js'))
    // child.send('?')
    // child.on('message', msg => {
    //   console.log('收到子进程消息', msg)
    // })

    // child.on('exit', () => {
    //   console.log('子进程退出')
    //   resolve()
    // })

    // child.on('uncaughtException', function (err) {
    //   console.log('捕获到一个未被处理的错误:', err)
    //   reject(err)
    // })
  })
}
