1.11.3 • Published 1 year ago

hardware-api v1.11.3

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

硬件控制中心 API 访问库

安装

yarn

引用

1.在 vue 项目的 plugins 目录下新建一个 hardware.js 文件,将以下代码复制到文件中

/*
 * @Descripttion:
 * @version: 1.0
 * @Author: ZaccheoW
 * @Date: 2023-10-13 17:08:58
 * @LastEditors: ZaccheoW
 * @LastEditTime: 2023-11-10 16:35:16
 */
// 硬件控制中心插件封装
import {
  Host,
  Terminal,
  Document,
  Downloader,
  Drive,
  FileSystem,
  IDCardReader,
  Indicator,
  Printer,
  Scanner,
  SerialBarcodeReader,
  StorageCabinet,
  ThermalPrinter
} from '@zaccheo/hardware-api'
import PubSub from 'pubsub-js'

class Hardware {
  // 重连间隔
  _reconnectInterval = 30000

  _protocolType = ''

  // // 构造函数
  // constructor() {
  //   // 初始化

  // }

  // vue插件安装入口函数
  install (Vue, options) {
    Vue.prototype.$hardware = this
    // 初始化所有插件
    this.initial(Vue)
    // 根据插件启用情况与插件建立实时连接
    this.connect(Vue)
  }

  /**
   * @name: 初始化
   * @return {void}
   */
  initial (Vue) {
    // 引入硬件控制中心插件
    Vue.use(Host)
    Vue.use(Terminal)
    Vue.use(Document)
    Vue.use(Downloader)
    Vue.use(Drive)
    Vue.use(FileSystem)
    Vue.use(IDCardReader)
    Vue.use(Indicator)
    Vue.use(Printer)
    Vue.use(Scanner)
    Vue.use(SerialBarcodeReader)
    Vue.use(StorageCabinet)
    Vue.use(ThermalPrinter)
  }

  /**
   * @name: 建立各插件的hub连接
   * @return {void}
   */
  connect (Vue) {
    // 根据启用的插件进行初始化
    Host.getVersion()
      .then(res => {
        this._protocolType = res.data.hubProtocol
        return Host.getPlugins()
      })
      .then(res => {
        // 遍历所有插件,根据插件启用情况进行连接
        if (res.data) {
          debugger
          res.data.forEach(p => {
            if (p.enabled) {
              // 建立hub连接
              if (!Vue[p.code]) {
                console.warn(`[hardware]${p.code} 插件不存在。请检查。`)
                return
              }
              if (Vue[p.code].connect) {
                Vue[p.code].connect({ protocolType: this._protocolType.toUpperCase() })
              }
            }
          })
        }
      })
      .catch(err => {
        // 连接失败重连
        setTimeout(() => {
          this.connect(Vue)
        }, this._reconnectInterval)

        const msg = `请求硬件控制中心插件列表失败!${err.message}`
        //发布设备状态变更消息
        PubSub.publish('OnTerminalStateChanged', {
          isReadly: false,
          level: 'Error',
          title: '与硬件控制中心连接失败',
          message: msg,
          detail: ''
        })
        console.error(`[hardware]${msg}`, err)
      })
  }
}

export default new Hardware()

2.将以下代码添加到 main.js 中

import Hardware from "@/plugins/hardware"

// 引入硬件控制中心插件
Vue.use(Hardware, { baseUrl: "http://localhost:5000" })
1.11.3

1 year ago