ssts-hospital-web-master项目实战记录二十九:项目迁移-核心模块实现(useDeviceDriver&DeviceDriver)

记录时间:2024-03-15

一、useDeviceDriver模块实现

types/map.ts

interface StatusMap {
  [key: string]: string
}

interface NamedStatusMap {
  name: string
  value: StatusMap
}

export type { StatusMap, NamedStatusMap }
 

types/device-driver.ts

import { NamedStatusMap } from './map'

// 定义 EventFunctionType 类型
interface EventFunctionType {
  (EventName: string, param: any): void
}

// 定义 EventResultType 类型
interface EventResultType {
  (EventName: string, param: any): void
}

// 定义 FnCallType 类型
interface FnCallType {
  (
    device: DeviceType,
    fnName: string,
    params: Array<any | undefined>
  ): Promise<any>
}

// 定义 DeviceType 类型
type DeviceType =
  | CRDType
  | IDCType
  | PTRType
  | BCRType
  | SIUType
  | PINType
  | BVType

// 定义 DeviceDriverType 类型
interface DeviceDriverType {
  EventFunction: EventFunctionType
  EventResult: EventResultType
  SetCallback: (
    callbackEventFunction?: EventFunctionType,
    callbackEventResult?: EventResultType
  ) => void
  InitProps(): void
  CheckDeviceStatus(deviceNames: string): Promise<string>

  m1: M1ParamType
  setM1CardParams: () => void
  cardDispenser: CRDType
  cardDispenserReader: IDCType
  cardReader: IDCType
  ssCardReader: IDCType
  idCardReader: IDCType
  icCardReader: IDCType
  swCardReader: IDCType
  receiptPrinter: PTRType
  labelPrinter: PTRType
  documentPrinter: PTRType
  billValidator: BVType
  pinPad: PINType
  barcode: BCRType
  siu: SIUType
}

// 定义 RealDeviceDriverType 类型
interface RealDeviceDriverType {
  EventFunction: EventFunctionType
  EventResult: EventResultType
  FnCall: FnCallType

  handleStatusResult: (
    device: DeviceType,
    fnName: string,
    result: any
  ) => Promise<object>
  changeStStatus: (
    deviceName: string,
    statusMap: NamedStatusMap,
    statusKey: string
  ) => string
  changeStCardUnit: (
    deviceName: string,
    Type: string,
    StCardUnitStatus: string
  ) => string

  m1: M1ParamType
  cardDispenser: CRDType
  cardDispenserReader: IDCType
  cardReader: IDCType
  ssCardReader: IDCType
  idCardReader: IDCType
  icCardReader: IDCType
  swCardReader: IDCType
  receiptPrinter: PTRType
  labelPrinter: PTRType
  documentPrinter: PTRType
  barcode: BCRType
  siu: SIUType
  pinPad: PINType
  billValidator: BVType
}

// 定义 M1ParamType 类型
interface M1ParamType {
  sectorNo: string
  sectorPwd: string
  blockNo: string
}

// 定义 CRDType 类型
interface CRDType {
  type: 'CRD'
  id?: string
  name?: string
  status: string
  dispenser: string
  media: string
  cardUnit: string
  open(logicalName?: string): Promise<void>
  close(): Promise<void>
  getStatus(): Promise<string>
  reset(action: string): Promise<void>
  getCardUnit(): Promise<string>
  setCardUnit(
    number: string,
    name: string,
    type: string,
    count: string,
    initialCount: string,
    retainCount: string,
    threshold: string
  ): Promise<void>
  dispenseCard(num: number, present: boolean): Promise<void>
  moveCard(pos: string): Promise<void>
  ejectCard(): Promise<void>
  retainCard(num: number): Promise<void>
}

// 定义 IDCType 类型
interface IDCType {
  type: 'IDC'
  id?: string
  name?: string
  status: string
  media: string
  open(logicalName?: string): Promise<void>
  close(): Promise<void>
  getStatus(): Promise<string>
  reset(action: string): Promise<void>
  resetCount(): Promise<void>
  readRawData(track: string, timeout: number): Promise<void>
  cancelReadRawData(): Promise<void>
  writeRawData(
    track1: string,
    track2: string,
    track3: string,
    chip: string,
    cardPTR: string
  ): Promise<void>
  chipPower(type: string): Promise<void>
  chipIO(protocol: string, data: string): Promise<void>
  ejectCard(): Promise<void>
  retainCard(): Promise<void>
  readM1Card(): Promise<string[]>
  readSSCard(): Promise<string[]>
  readSSCard1(): Promise<string[]>
  readSSCard2(): Promise<string[]>
  readTrack2FromChip(): Promise<string>
  readICDataFromChip(
    tellerNoAsii: string,
    tradeType: string,
    amt: string
  ): Promise<string[]>
}

// 定义 PTRType 类型
interface PTRType {
  type: 'PTR'
  id?: string
  name?: string
  status: string
  media: string
  papers: string
  open(logicalName?: string): Promise<void>
  close(): Promise<void>
  getStatus(): Promise<string>
  reset(mediaControl: string, retractBinNumber: string): Promise<void>
  printForm(formName: string, fields: string, mediaName: string): Promise<void>
  printRawData(rawdata: string): Promise<void>
  eject(cut: string): Promise<void>
}

// 定义 BCRType 类型
interface BCRType {
  type: 'BCR'
  id?: string
  name?: string
  status: string
  open(logicalName?: string): Promise<void>
  close(): Promise<void>
  getStatus(): Promise<string>
  reset(): Promise<void>
  readBarcode(type: string, timeout: number): Promise<void>
  cancelReadBarcode(): Promise<void>
}

// 定义 SIUType 类型
interface SIUType {
  type: 'SIU'
  id?: string
  name?: string
  status: string
  open(logicalName?: string): Promise<void>
  close(): Promise<void>
  getStatus(): Promise<string>
  reset(): Promise<void>
  enableEvent(): Promise<void>
  disableEvent(): Promise<void>
  setGuidLight(item: string, common: string): Promise<void>
  setAuxiliary(item: string, common: string): Promise<void>
  setIndicator(item: string, common: string): Promise<void>
  setDoor(item: string, common: string): Promise<void>
}

// 定义 PINType 类型
interface PINType {
  type: 'PIN'
  id?: string
  name?: string
  status: string
  open(logicalName?: string): Promise<void>
  close(): Promise<void>
  getStatus(): Promise<string>
  reset(): Promise<void>
  loadMasterKey(KeyValue: string): Promise<string>
  loadPinKey(KeyValue: string): Promise<string>
  loadMacKey(KeyValue: string): Promise<string>
  getPin(MinLength: number, MaxLength: number, AutoEnd: boolean): Promise<void>
  getPinBlock(Data: string): Promise<string>
  cancelGetPin(): Promise<void>
  getData(MaxLength: number, AutoEnd: boolean): Promise<void>
  cancelGetData(): Promise<void>
  getMac(Data: string): Promise<string>
  initialize(): Promise<void>
  encryptData(KeyName: string, Data: string, Algorithm: string): Promise<string>
  generateMac(Data: string): Promise<string>
}

// 定义 BVType 类型
interface BVType {
  type: 'BV'
  id?: string
  name?: string
  status: string
  statusCode: string
  statusMsg: string
  statusList: { [key: string]: string }
  open(logicalName?: string): Promise<void>
  close(): Promise<void>
  getStatus(): Promise<string>
  getCashUnitInfo(): Promise<string>
  reset(): Promise<void>
  setNoteInfo(notes: string): Promise<void>
  cashInStart(): Promise<void>
  cashInEnd(): Promise<void>
}

export type {
  EventFunctionType,
  EventResultType,
  FnCallType,
  DeviceType,
  DeviceDriverType,
  RealDeviceDriverType,
  M1ParamType,
  CRDType,
  IDCType,
  BCRType,
  SIUType,
  PINType,
  BVType
}
 

service/device-driver/device-driver.ts

import * as config from '@/config'
import { DeviceDriverType } from '@/types'
import { useSystemStore } from '@/store'
import { useSystemUtil } from '@/utils/system'
import { useSystemService } from '@/service/system'
import { RealDeviceDriver as rdd } from './ezware'

const DeviceDriver: DeviceDriverType = {
  EventFunction: function (EventName, param) {
    console.log('DeviceDriver:EventFunction', EventName, param)
  },
  EventResult: function (EventName, param) {
    console.log('DeviceDriver:EventResult', EventName, param)
  },
  SetCallback: function (callbackEventFunction, callbackEventResult) {
    if (callbackEventFunction) {
      this.EventFunction = callbackEventFunction
    }
    if (callbackEventResult) {
      this.EventResult = callbackEventResult
    }
  },
  InitProps: function () {
    // 遍历this对象的所有属性
    Object.keys(this).forEach((key) => {
      // 检查属性值是否为一个对象
      if (
        typeof this[key] === 'object' &&
        this[key] !== null &&
        Object.prototype.hasOwnProperty.call(rdd, key)
      ) {
        // 遍历rdd对象的相应属性
        Object.keys(rdd[key]).forEach((sourceKey) => {
          // 检查rdd对象的属性值是否为字符串类型
          if (typeof rdd[key][sourceKey] === 'string') {
            // 将rdd对象的字符串类型的属性值赋给this对象的相应属性
            this[key][sourceKey] = rdd[key][sourceKey]
          }
        })
      }
    })
  },

  // 检查设备状态
  // deviceList:需要检查状态的设备列表(竖线分割,可传入英文名或中文名)
  // ShowMsg('--设备状态异常--\r\n' + error)
  CheckDeviceStatus: async function (deviceNames: string) {
    let hasError = false
    let errorMsg = ''
    let errorDescription = ''
    const systemStore = useSystemStore()

    systemStore.setDictDeviceStatusError('')
    if (!systemStore.dictDeviceList) {
      return ''
    }

    const deviceNameList = deviceNames.split('|')
    const { dictDeviceNameList, dictDeviceCnNameList } = systemStore

    const systemUtil = useSystemUtil()
    const systemService = useSystemService()
    for (let i = 0; i < deviceNameList.length; i++) {
      const deviceName = systemUtil.getDeviceName(
        deviceNameList[i],
        dictDeviceNameList
      )
      const deviceCnName = systemUtil.getDeviceCnName(
        deviceNameList[i],
        dictDeviceCnNameList
      )

      if (deviceCnName) {
        const status = systemUtil.getDeviceStatus(
          deviceName,
          deviceStatusMappers
        )
        const statusDesc =
          (await systemService.getDeviceStatusDescription(
            deviceCnName,
            status
          )) || `故障(${status})`

        if (status !== '0') {
          errorDescription = systemUtil.updateDeviceError(
            errorDescription,
            deviceCnName,
            statusDesc
          )
          errorMsg += `${deviceCnName}:${statusDesc}`
          hasError = true // 一旦发现错误,设置标志
        }
      }
    }

    if (hasError) {
      systemStore.setDictDeviceStatusError(errorDescription)
    }

    return errorMsg // 返回错误信息
  },

  // M1卡参数
  m1: {
    sectorNo: '',
    sectorPwd: '',
    blockNo: ''
  },
  setM1CardParams() {
    rdd.m1.blockNo = this.m1.sectorNo
    rdd.m1.sectorPwd = this.m1.sectorPwd
    rdd.m1.blockNo = this.m1.blockNo
  },

  // 发卡机
  cardDispenser: {
    type: 'CRD',
    status: '',
    dispenser: '',
    media: '',
    cardUnit: '',
    async open(logicalName?) {
      return await rdd.cardDispenser.open(logicalName)
    },
    async close() {
      return await rdd.cardDispenser.close()
    },
    async getStatus() {
      return await rdd.cardDispenser.getStatus()
    },
    async reset(action) {
      return await rdd.cardDispenser.reset(action)
    },
    async getCardUnit() {
      return await rdd.cardDispenser.getCardUnit()
    },
    async setCardUnit(
      number,
      name,
      type,
      count,
      initialCount,
      retainCount,
      threshold
    ) {
      return await rdd.cardDispenser.setCardUnit(
        number,
        name,
        type,
        count,
        initialCount,
        retainCount,
        threshold
      )
    },
    async dispenseCard(number, present) {
      return await rdd.cardDispenser.dispenseCard(number, present)
    },
    async moveCard(pos) {
      return await rdd.cardDispenser.moveCard(pos)
    },
    async ejectCard() {
      return await rdd.cardDispenser.ejectCard()
    },
    async retainCard(number) {
      return await rdd.cardDispenser.retainCard(number)
    }
  },

  // 发卡机读卡器
  cardDispenserReader: {
    type: 'IDC',
    status: '',
    media: '',
    async open(logicalName?) {
      return rdd.cardDispenserReader.open(logicalName)
    },
    async close() {
      return rdd.cardDispenserReader.close()
    },
    async getStatus() {
      return await rdd.cardDispenserReader.getStatus()
    },
    async reset(action) {
      return await rdd.cardDispenserReader.reset(action)
    },
    async resetCount() {
      return await rdd.cardDispenserReader.resetCount()
    },
    async readRawData(track, timeout) {
      return await rdd.cardDispenserReader.readRawData(track, timeout)
    },
    async cancelReadRawData() {
      return await rdd.cardDispenserReader.cancelReadRawData()
    },
    async writeRawData(track1, track2, track3, chip, cardPTR) {
      return await rdd.cardDispenserReader.writeRawData(
        track1,
        track2,
        track3,
        chip,
        cardPTR
      )
    },
    async chipPower(type) {
      return await rdd.cardDispenserReader.chipPower(type)
    },
    async chipIO(protocol, data) {
      await rdd.cardDispenserReader.chipIO(protocol, data)
    },
    async ejectCard() {
      return await rdd.cardDispenserReader.ejectCard()
    },
    async retainCard() {
      return await rdd.cardDispenserReader.retainCard()
    },
    async readM1Card() {
      throw new Error('Function not implemented.')
    },
    async readSSCard() {
      throw new Error('Function not implemented.')
    },
    async readSSCard1(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readSSCard2(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readTrack2FromChip() {
      throw new Error('Function not implemented.')
    },
    async readICDataFromChip(tellerNoAsii, tradeType, amt) {
      console.log(tellerNoAsii, tradeType, amt)
      throw new Error('Function not implemented.')
    }
  },

  // 银行卡读卡器
  cardReader: {
    type: 'IDC',
    status: '',
    media: '',
    async open(logicalName?) {
      return rdd.cardReader.open(logicalName)
    },
    async close() {
      return rdd.cardReader.close()
    },
    async getStatus() {
      return await rdd.cardReader.getStatus()
    },
    async reset(action) {
      return await rdd.cardReader.reset(action)
    },
    async resetCount() {
      return await rdd.cardReader.resetCount()
    },
    async readRawData(track, timeout) {
      return await rdd.cardReader.readRawData(track, timeout)
    },
    async cancelReadRawData() {
      return await rdd.cardReader.cancelReadRawData()
    },
    async writeRawData(track1, track2, track3, chip, cardPTR) {
      return await rdd.cardReader.writeRawData(
        track1,
        track2,
        track3,
        chip,
        cardPTR
      )
    },
    async chipPower(type) {
      return await rdd.cardReader.chipPower(type)
    },
    async chipIO(protocol, data) {
      return await rdd.cardReader.chipIO(protocol, data)
    },
    async ejectCard() {
      return await rdd.cardReader.ejectCard()
    },
    async retainCard() {
      await rdd.cardReader.retainCard()
    },
    async readM1Card() {
      throw new Error('Function not implemented.')
    },
    async readSSCard() {
      throw new Error('Function not implemented.')
    },
    async readSSCard1(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readSSCard2(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readTrack2FromChip() {
      throw new Error('Function not implemented.')
    },
    async readICDataFromChip(tellerNoAsii, tradeType, amt) {
      console.log(tellerNoAsii, tradeType, amt)
      throw new Error('Function not implemented.')
    }
  },

  // 社保卡读卡器
  ssCardReader: {
    type: 'IDC',
    status: '',
    media: '',
    async open(logicalName?) {
      return rdd.ssCardReader.open(logicalName)
    },
    async close() {
      return rdd.ssCardReader.close()
    },
    async getStatus() {
      return await rdd.ssCardReader.getStatus()
    },

    async reset(action) {
      return await rdd.ssCardReader.reset(action)
    },
    async resetCount() {
      return await rdd.ssCardReader.resetCount()
    },
    async readRawData(track, timeout) {
      return await rdd.ssCardReader.readRawData(track, timeout)
    },
    async cancelReadRawData() {
      return await rdd.ssCardReader.cancelReadRawData()
    },
    async writeRawData(track1, track2, track3, chip, cardPTR) {
      return await rdd.ssCardReader.writeRawData(
        track1,
        track2,
        track3,
        chip,
        cardPTR
      )
    },
    async chipPower(type) {
      return await rdd.ssCardReader.chipPower(type)
    },
    async chipIO(protocol, data) {
      return await rdd.ssCardReader.chipIO(protocol, data)
    },
    async ejectCard() {
      return await rdd.ssCardReader.ejectCard()
    },
    async retainCard() {
      await rdd.ssCardReader.retainCard()
    },
    async readM1Card() {
      throw new Error('Function not implemented.')
    },
    async readSSCard() {
      throw new Error('Function not implemented.')
    },
    async readSSCard1(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readSSCard2(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readTrack2FromChip() {
      throw new Error('Function not implemented.')
    },
    async readICDataFromChip(tellerNoAsii, tradeType, amt) {
      console.log(tellerNoAsii, tradeType, amt)
      throw new Error('Function not implemented.')
    }
  },

  // 身份证读卡器
  idCardReader: {
    type: 'IDC',
    status: '',
    media: '',
    async open(logicalName?) {
      return rdd.idCardReader.open(logicalName)
    },
    async close() {
      return rdd.idCardReader.close()
    },
    async getStatus() {
      return await rdd.idCardReader.getStatus()
    },

    async reset(action) {
      return await rdd.idCardReader.reset(action)
    },
    async resetCount() {
      return await rdd.idCardReader.resetCount()
    },
    async readRawData(track, timeout) {
      return await rdd.idCardReader.readRawData(track, timeout)
    },
    async cancelReadRawData() {
      return await rdd.idCardReader.cancelReadRawData()
    },
    async writeRawData(track1, track2, track3, chip, cardPTR) {
      return await rdd.idCardReader.writeRawData(
        track1,
        track2,
        track3,
        chip,
        cardPTR
      )
    },
    async chipPower(type) {
      return await rdd.idCardReader.chipPower(type)
    },
    async chipIO(protocol, data) {
      return await rdd.idCardReader.chipIO(protocol, data)
    },
    async ejectCard() {
      return await rdd.idCardReader.ejectCard()
    },
    async retainCard() {
      await rdd.idCardReader.retainCard()
    },
    async readM1Card() {
      throw new Error('Function not implemented.')
    },
    async readSSCard() {
      throw new Error('Function not implemented.')
    },
    async readSSCard1(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readSSCard2(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readTrack2FromChip() {
      throw new Error('Function not implemented.')
    },
    async readICDataFromChip(tellerNoAsii, tradeType, amt) {
      console.log(tellerNoAsii, tradeType, amt)
      throw new Error('Function not implemented.')
    }
  },

  // 非接读卡器
  icCardReader: {
    type: 'IDC',
    status: '',
    media: '',
    async open(logicalName?) {
      return rdd.icCardReader.open(logicalName)
    },
    async close() {
      return rdd.icCardReader.close()
    },
    async getStatus() {
      return await rdd.icCardReader.getStatus()
    },
    async reset(action) {
      return await rdd.icCardReader.reset(action)
    },
    async resetCount() {
      return await rdd.icCardReader.resetCount()
    },
    async readRawData(track, timeout) {
      return await rdd.icCardReader.readRawData(track, timeout)
    },
    async cancelReadRawData() {
      return await rdd.icCardReader.cancelReadRawData()
    },
    async writeRawData(track1, track2, track3, chip, cardPTR) {
      return await rdd.icCardReader.writeRawData(
        track1,
        track2,
        track3,
        chip,
        cardPTR
      )
    },
    async chipPower(type) {
      return await rdd.icCardReader.chipPower(type)
    },
    async chipIO(protocol, data) {
      return await rdd.icCardReader.chipIO(protocol, data)
    },
    async ejectCard() {
      return await rdd.icCardReader.ejectCard()
    },
    async retainCard() {
      await rdd.icCardReader.retainCard()
    },
    async readM1Card() {
      throw new Error('Function not implemented.')
    },
    async readSSCard() {
      throw new Error('Function not implemented.')
    },
    async readSSCard1(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readSSCard2(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readTrack2FromChip() {
      throw new Error('Function not implemented.')
    },
    async readICDataFromChip(tellerNoAsii, tradeType, amt) {
      console.log(tellerNoAsii, tradeType, amt)
      throw new Error('Function not implemented.')
    }
  },

  // 刷卡器
  swCardReader: {
    type: 'IDC',
    status: '',
    media: '',
    async open(logicalName?) {
      return rdd.swCardReader.open(logicalName)
    },
    async close() {
      return rdd.swCardReader.close()
    },
    async getStatus() {
      return await rdd.swCardReader.getStatus()
    },
    async reset(action) {
      return await rdd.swCardReader.reset(action)
    },
    async resetCount() {
      return await rdd.swCardReader.resetCount()
    },
    async readRawData(track, timeout) {
      return await rdd.swCardReader.readRawData(track, timeout)
    },
    async cancelReadRawData() {
      return await rdd.swCardReader.cancelReadRawData()
    },
    async writeRawData(track1, track2, track3, chip, cardPTR) {
      return await rdd.swCardReader.writeRawData(
        track1,
        track2,
        track3,
        chip,
        cardPTR
      )
    },
    async chipPower(type) {
      return await rdd.swCardReader.chipPower(type)
    },
    async chipIO(protocol, data) {
      return await rdd.swCardReader.chipIO(protocol, data)
    },
    async ejectCard() {
      return await rdd.swCardReader.ejectCard()
    },
    async retainCard() {
      await rdd.swCardReader.retainCard()
    },
    async readM1Card() {
      throw new Error('Function not implemented.')
    },
    async readSSCard() {
      throw new Error('Function not implemented.')
    },
    async readSSCard1(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readSSCard2(): Promise<string[]> {
      throw new Error('Function not implemented.')
    },
    async readTrack2FromChip() {
      throw new Error('Function not implemented.')
    },
    async readICDataFromChip(tellerNoAsii, tradeType, amt) {
      console.log(tellerNoAsii, tradeType, amt)
      throw new Error('Function not implemented.')
    }
  },

  // 凭条打印机
  receiptPrinter: {
    type: 'PTR',
    status: '',
    media: '',
    papers: '',
    async open(logicalName?) {
      return await rdd.receiptPrinter.open(logicalName)
    },
    async close() {
      return await rdd.receiptPrinter.close()
    },
    async getStatus() {
      return await rdd.receiptPrinter.getStatus()
    },
    async reset(mediaControl, retractBinNumber) {
      return await rdd.receiptPrinter.reset(mediaControl, retractBinNumber)
    },
    async printForm(formName, fields, mediaName) {
      return await rdd.receiptPrinter.printForm(formName, fields, mediaName)
    },
    async printRawData(rawdata) {
      return await rdd.receiptPrinter.printRawData(rawdata)
    },
    async eject(cut) {
      return await rdd.receiptPrinter.eject(cut)
    }
  },

  // 标签打印机
  labelPrinter: {
    type: 'PTR',
    status: '',
    media: '',
    papers: '',
    async open(logicalName?) {
      return await rdd.labelPrinter.open(logicalName)
    },
    async close() {
      return await rdd.labelPrinter.close()
    },
    async getStatus() {
      return await rdd.labelPrinter.getStatus()
    },
    async reset(mediaControl, retractBinNumber) {
      return await rdd.labelPrinter.reset(mediaControl, retractBinNumber)
    },
    async printForm(formName, fields, mediaName) {
      return await rdd.labelPrinter.printForm(formName, fields, mediaName)
    },
    async printRawData(rawdata) {
      return await rdd.labelPrinter.printRawData(rawdata)
    },
    async eject(cut) {
      return await rdd.labelPrinter.eject(cut)
    }
  },

  // 文档打印机
  documentPrinter: {
    type: 'PTR',
    status: '',
    media: '',
    papers: '',
    async open(logicalName?) {
      return await rdd.documentPrinter.open(logicalName)
    },
    async close() {
      return await rdd.documentPrinter.close()
    },
    async getStatus() {
      return await rdd.documentPrinter.getStatus()
    },
    async reset(mediaControl, retractBinNumber) {
      return await rdd.documentPrinter.reset(mediaControl, retractBinNumber)
    },
    async printForm(formName, fields, mediaName) {
      return await rdd.documentPrinter.printForm(formName, fields, mediaName)
    },
    async printRawData(rawdata) {
      return await rdd.documentPrinter.printRawData(rawdata)
    },
    async eject(cut) {
      return await rdd.documentPrinter.eject(cut)
    }
  },

  // 扫码器
  barcode: {
    type: 'BCR',
    status: '',
    async open(logicalName?) {
      return await rdd.barcode.open(logicalName)
    },
    async close() {
      return await rdd.barcode.close()
    },
    async getStatus() {
      return await rdd.barcode.getStatus()
    },
    async reset() {
      return await rdd.barcode.reset()
    },
    async readBarcode(type, timeout) {
      return await rdd.barcode.readBarcode(type, timeout)
    },
    async cancelReadBarcode() {
      return await rdd.barcode.cancelReadBarcode()
    }
  },

  // SIU
  siu: {
    type: 'SIU',
    status: '',
    async open(logicalName?) {
      return await rdd.siu.open(logicalName)
    },
    async close() {
      return await rdd.siu.close()
    },
    async getStatus() {
      return await rdd.siu.getStatus()
    },
    async reset() {
      return await rdd.siu.reset()
    },
    async enableEvent() {
      return await rdd.siu.enableEvent()
    },
    async disableEvent() {},
    async setGuidLight(item, common) {
      return await rdd.siu.setGuidLight(item, common)
    },
    async setAuxiliary(item, common) {
      return await rdd.siu.setAuxiliary(item, common)
    },
    async setIndicator(item, common) {
      return await rdd.siu.setIndicator(item, common)
    },
    async setDoor(item, common) {
      return await rdd.siu.setDoor(item, common)
    }
  },

  // 密码键盘
  pinPad: {
    type: 'PIN',
    status: '',
    async open(logicalName?) {
      return await rdd.pinPad.open(logicalName)
    },
    async close() {
      return await rdd.pinPad.close()
    },
    async getStatus() {
      return await rdd.pinPad.getStatus()
    },
    async reset() {
      return await rdd.pinPad.reset()
    },
    async loadMasterKey(KeyValue) {
      return await rdd.pinPad.loadMasterKey(KeyValue)
    },
    async loadPinKey(KeyValue) {
      return await rdd.pinPad.loadPinKey(KeyValue)
    },
    async loadMacKey(KeyValue) {
      return await rdd.pinPad.loadMacKey(KeyValue)
    },
    async getPin(MinLength, MaxLength, AutoEnd) {
      return await rdd.pinPad.getPin(MinLength, MaxLength, AutoEnd)
    },
    async cancelGetPin() {
      return await rdd.pinPad.cancelGetPin()
    },
    async getPinBlock(Data) {
      return await rdd.pinPad.getPinBlock(Data)
    },
    async getData(MaxLength, AutoEnd) {
      return await rdd.pinPad.getData(MaxLength, AutoEnd)
    },
    async cancelGetData() {
      return await rdd.pinPad.cancelGetData()
    },
    async getMac(Data) {
      return await rdd.pinPad.getMac(Data)
    },
    async initialize() {
      return await rdd.pinPad.initialize()
    },
    async encryptData(Key: string, Data: string, Algorithm: string) {
      return await rdd.pinPad.encryptData(Key, Data, Algorithm)
    },
    async generateMac(Data) {
      return await rdd.pinPad.generateMac(Data)
    }
  },

  // 纸币器
  billValidator: {
    type: 'BV',
    status: '',
    statusCode: '',
    statusMsg: '',
    statusList: config.BillValidatorStatusConfig,
    async open(logicalName?) {
      return await rdd.billValidator.open(logicalName)
    },
    async close() {
      return await rdd.billValidator.close()
    },
    async getStatus() {
      return await rdd.billValidator.getStatus()
    },
    async getCashUnitInfo() {
      return await rdd.billValidator.getCashUnitInfo()
    },
    async reset() {
      return await rdd.billValidator.reset()
    },
    async setNoteInfo(notes) {
      return await rdd.billValidator.setNoteInfo(notes)
    },
    async cashInStart() {
      return await rdd.billValidator.cashInStart()
    },
    async cashInEnd() {
      return await rdd.billValidator.cashInEnd()
    }
  }
}

// 设备状态获取函数映射
const deviceStatusMappers = {
  CardDispenser: () => DeviceDriver.cardDispenser.getStatus(),
  CardDispenserCardUnit: () => DeviceDriver.cardDispenser.getCardUnit(),
  CardDispenserReader: () => DeviceDriver.cardDispenserReader.getStatus(),
  CardReader: () => DeviceDriver.cardReader.getStatus(),
  SSCardReader: () => DeviceDriver.ssCardReader.getStatus(),
  IDCardReader: () => DeviceDriver.idCardReader.getStatus(),
  ICCardReader: () => DeviceDriver.icCardReader.getStatus(),
  SWCardReader: () => DeviceDriver.swCardReader.getStatus(),
  ReceiptPrinter: () => DeviceDriver.receiptPrinter.getStatus(),
  ReceiptPrinterPapers: async () => {
    await DeviceDriver.receiptPrinter.getStatus()
    return DeviceDriver.receiptPrinter.papers
  },
  LabelPrinter: () => DeviceDriver.labelPrinter.getStatus(),
  LabelPrinterPapers: async () => {
    await DeviceDriver.receiptPrinter.getStatus()
    return DeviceDriver.receiptPrinter.papers
  },
  DocumentPrinter: () => DeviceDriver.documentPrinter.getStatus(),
  DocumentPrinterPapers: async () => {
    await DeviceDriver.receiptPrinter.getStatus()
    return DeviceDriver.receiptPrinter.papers
  },
  Barcode: () => DeviceDriver.barcode.getStatus(),
  SIU: () => DeviceDriver.siu.getStatus(),
  PinPad: () => DeviceDriver.pinPad.getStatus(),
  BillValidator: () => DeviceDriver.billValidator.getStatus()
}

export { DeviceDriver }

 

service/device-driver/index.ts 

import { EventFunctionType, EventResultType } from '@/types'
import { DeviceDriver } from './device-driver'
export {
  LogDeviceInfo,
  LogDeviceFunctionInfo,
  LogDeviceEventInfo,
  LogDeviceStatusInfo
} from './log-device-info'

export const useDeviceDriver = (
  callbackEventFunction?: EventFunctionType,
  callbackEventResult?: EventResultType
) => {
  DeviceDriver.SetCallback(callbackEventFunction, callbackEventResult)
  DeviceDriver.InitProps()

  return {
    ...DeviceDriver
  }
}

 

二、调用示例

 

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { EventFunctionType, EventResultType } from '@/types'
import { hydate } from '@/common'
import { useDeviceDriver } from '@/service/device-driver'

const EventFunctionName = ref()
const EventFunctionParam = ref()
const EventResultName = ref()
const EventResultParam = ref()

const EventFunction: EventFunctionType = (EventName, param) => {
  console.log(
    hydate(new Date()).format('YYYY-MM-DD HH:mm:ss.SSS'),
    '\n',
    'Page-EventFunction',
    EventName,
    param
  )
  EventFunctionName.value = EventName
  EventFunctionParam.value = param
}
const EventResult: EventResultType = (EventName, param) => {
  console.log(
    hydate(new Date()).format('YYYY-MM-DD HH:mm:ss.SSS'),
    '\n',
    'Page-EventResult',
    EventName,
    param
  )
  EventResultName.value = EventName
  EventResultParam.value = param
}
const createObjectInfoToTable = (obj: object) => {
  // 遍历obj对象的所有属性
  const properties = Object.keys(obj)
    .filter((key) => {
      // 只保留字符串类型的属性
      return typeof obj[key] === 'string'
    })
    .map((key) => {
      // 格式化属性为 '属性名:属性值' 形式
      // return `${key}:[${obj[key]}]`;
      return `<tr><td>${key}</td><td>${obj[key]}</td></tr>`
    })

  // 将所有格式化后的属性拼接起来
  // return properties.join(',');
  return properties.join('')
}

const dd = reactive(useDeviceDriver(EventFunction, EventResult))

// 用于显示/隐藏fieldset的响应式状态
const currentFieldset = ref('')
function setCurrentFieldset(event: MouseEvent) {
  const button = event.target as HTMLButtonElement // 获取被点击的按钮元素
  // 读取按钮的文本内容作为currentFieldset的值
  currentFieldset.value = button.innerText.trim()

  // 清空事件信息
  EventFunctionName.value = ''
  EventFunctionParam.value = ''
  EventResultName.value = ''
  EventResultParam.value = ''
}
const rawData_Document = {
  Timeout: 0,
  PrintType: 0,
  TotalPaperNum: 0,
  SlotType: 0,
  ChapterType: 0,
  ChapterPosA: 0,
  ChapterPosB: 0,
  ChapterPosC: 0,
  ChapterNum: 0,
  FilePath: 'C:/test.pdf'
}

onMounted(async () => {
  console.log('onMounted')
})
</script>

<template>
  <div>
    <div>
      <p>EventFunction:{{ EventFunctionName }} {{ EventFunctionParam }}</p>
      <p>EventResult:{{ EventResultName }} {{ EventResultParam }}</p>

      <!-- 按钮组来切换fieldset -->
      <div>
        <button @click="setCurrentFieldset($event)">cardDispenser</button>
        <button @click="setCurrentFieldset($event)">cardDispenserReader</button>
        <button @click="setCurrentFieldset($event)">cardReader</button>
        <button @click="setCurrentFieldset($event)">ssCardReader</button>
        <button @click="setCurrentFieldset($event)">idCardReader</button>
        <button @click="setCurrentFieldset($event)">icCardReader</button>
        <button @click="setCurrentFieldset($event)">swCardReader</button>
        <button @click="setCurrentFieldset($event)">receiptPrinter</button>
        <button @click="setCurrentFieldset($event)">labelPrinter</button>
        <button @click="setCurrentFieldset($event)">documentPrinter</button>
        <button @click="setCurrentFieldset($event)">barcode</button>
        <button @click="setCurrentFieldset($event)">siu</button>
        <button @click="setCurrentFieldset($event)">pinPad</button>
        <!-- <button @click="setCurrentFieldset($event)">billValidator</button> -->
      </div>
    </div>

    <!--cardDispenser-->
    <fieldset v-show="currentFieldset === 'cardDispenser'">
      <legend>{{ dd.cardDispenser.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.cardDispenser)"></tbody>
        </table>
      </div>
      <button @click="dd.cardDispenser.open()">open()</button>
      <button @click="dd.cardDispenser.close()">close()</button>
      <button @click="dd.cardDispenser.getStatus()">getStatus()</button>
      <button @click="dd.cardDispenser.getCardUnit()">getCardUnit()</button>
      <button @click="dd.cardDispenser.dispenseCard(0, true)">
        dispenseCard(0, true)
      </button>
      <button @click="dd.cardDispenser.ejectCard()">ejectCard()</button>
    </fieldset>

    <!--cardDispenserReader-->
    <fieldset v-show="currentFieldset === 'cardDispenserReader'">
      <legend>{{ dd.cardDispenserReader.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody
            v-html="createObjectInfoToTable(dd.cardDispenserReader)"
          ></tbody>
        </table>
      </div>
      <button @click="dd.cardDispenserReader.open()">open()</button>
      <button @click="dd.cardDispenserReader.close()">close()</button>
      <button @click="dd.cardDispenserReader.getStatus()">getStatus()</button>
      <button @click="dd.cardDispenserReader.readRawData('Track2', 60000)">
        readRawData('Track2', 60000)
      </button>
      <button @click="dd.cardDispenserReader.cancelReadRawData()">
        cancelReadRawData()
      </button>
    </fieldset>

    <!--cardReader-->
    <fieldset v-show="currentFieldset === 'cardReader'">
      <legend>{{ dd.cardReader.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.cardReader)"></tbody>
        </table>
      </div>
      <button @click="dd.cardReader.open()">open()</button>
      <button @click="dd.cardReader.close()">close()</button>
      <button @click="dd.cardReader.getStatus()">getStatus()</button>
      <button @click="dd.cardReader.readRawData('Track2', 60000)">
        readRawData('Track2', 60000)
      </button>
      <button @click="dd.cardReader.cancelReadRawData()">
        cancelReadRawData()
      </button>
    </fieldset>

    <!--ssCardReader-->
    <fieldset v-show="currentFieldset === 'ssCardReader'">
      <legend>{{ dd.ssCardReader.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.ssCardReader)"></tbody>
        </table>
      </div>
      <button @click="dd.ssCardReader.open()">open()</button>
      <button @click="dd.ssCardReader.close()">close()</button>
      <button @click="dd.ssCardReader.getStatus()">getStatus()</button>
      <button @click="dd.ssCardReader.readRawData('SSSE', 60000)">
        readRawData('SSSE', 60000)
      </button>
      <button @click="dd.ssCardReader.cancelReadRawData()">
        cancelReadRawData()
      </button>
    </fieldset>

    <!--idCardReader-->
    <fieldset v-show="currentFieldset === 'idCardReader'">
      <legend>{{ dd.idCardReader.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.idCardReader)"></tbody>
        </table>
      </div>
      <button @click="dd.idCardReader.open()">open()</button>
      <button @click="dd.idCardReader.close()">close()</button>
      <button @click="dd.idCardReader.getStatus()">getStatus()</button>
      <button @click="dd.idCardReader.readRawData('CHIP', 60000)">
        readRawData('CHIP', 60000)
      </button>
      <button @click="dd.idCardReader.cancelReadRawData()">
        cancelReadRawData()
      </button>
    </fieldset>

    <!--icCardReader-->
    <fieldset v-show="currentFieldset === 'icCardReader'">
      <legend>{{ dd.icCardReader.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.icCardReader)"></tbody>
        </table>
      </div>
      <button @click="dd.icCardReader.open()">open()</button>
      <button @click="dd.icCardReader.close()">close()</button>
      <button @click="dd.icCardReader.getStatus()">getStatus()</button>
      <button @click="dd.icCardReader.readRawData('MIFARE', 60000)">
        readRawData('MIFARE',60000)
      </button>
      <button @click="dd.icCardReader.readM1Card()">readM1Card()</button>
      <button @click="dd.icCardReader.cancelReadRawData()">
        cancelReadRawData()
      </button>
    </fieldset>

    <!--swCardReader-->
    <fieldset v-show="currentFieldset === 'swCardReader'">
      <legend>{{ dd.swCardReader.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.swCardReader)"></tbody>
        </table>
      </div>
      <button @click="dd.swCardReader.open()">open()</button>
      <button @click="dd.swCardReader.close()">close()</button>
      <button @click="dd.swCardReader.getStatus()">getStatus()</button>
      <button @click="dd.swCardReader.readRawData('Track2', 0)">
        readRawData('Track2', 0)
      </button>
      <button @click="dd.swCardReader.cancelReadRawData()">
        cancelReadRawData()
      </button>
    </fieldset>

    <!--receiptPrinter-->
    <fieldset v-show="currentFieldset === 'receiptPrinter'">
      <legend>{{ dd.receiptPrinter.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.receiptPrinter)"></tbody>
        </table>
      </div>
      <button @click="dd.receiptPrinter.open()">open()</button>
      <button @click="dd.receiptPrinter.close()">close()</button>
      <button @click="dd.receiptPrinter.getStatus()">getStatus()</button>
      <button @click="dd.receiptPrinter.printRawData('abc123')">
        printRawData('abc123')
      </button>
      <button @click="dd.receiptPrinter.eject('cut')">eject('cut')</button>
    </fieldset>

    <!--labelPrinter-->
    <fieldset v-show="currentFieldset === 'labelPrinter'">
      <legend>{{ dd.labelPrinter.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.labelPrinter)"></tbody>
        </table>
      </div>
      <button @click="dd.labelPrinter.open()">open()</button>
      <button @click="dd.labelPrinter.close()">close()</button>
      <button @click="dd.labelPrinter.getStatus()">getStatus()</button>
      <button @click="dd.labelPrinter.printRawData('abc123')">
        printRawData('abc123')
      </button>
      <button @click="dd.labelPrinter.eject('cut')">eject('cut')</button>
    </fieldset>

    <!--documentPrinter-->
    <fieldset v-show="currentFieldset === 'documentPrinter'">
      <legend>{{ dd.documentPrinter.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.documentPrinter)"></tbody>
        </table>
      </div>
      <button @click="dd.documentPrinter.open()">open()</button>
      <button @click="dd.documentPrinter.close()">close()</button>
      <button @click="dd.documentPrinter.getStatus()">getStatus()</button>
      <button
        @click="
          dd.documentPrinter.printRawData(JSON.stringify(rawData_Document))
        "
      >
        printRawData(JSON.stringify(rawData_Document));
      </button>
    </fieldset>

    <!--billValidator-->
    <fieldset v-show="currentFieldset === 'billValidator'">
      <legend>{{ dd.billValidator.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.billValidator)"></tbody>
        </table>
      </div>
      <button @click="dd.billValidator.open()">open()</button>
      <button @click="dd.billValidator.close()">close()</button>
      <button @click="dd.billValidator.getStatus()">getStatus()</button>
    </fieldset>

    <!--barcode-->
    <fieldset v-show="currentFieldset === 'barcode'">
      <legend>{{ dd.barcode.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.barcode)"></tbody>
        </table>
      </div>
      <button @click="dd.barcode.open()">open()</button>
      <button @click="dd.barcode.close()">close()</button>
      <button @click="dd.barcode.getStatus()">getStatus()</button>
      <button @click="dd.barcode.readBarcode('', 0)">readBarcode()</button>
      <button @click="dd.barcode.cancelReadBarcode()">
        cancelReadBarcode()
      </button>
    </fieldset>

    <!--siu-->
    <fieldset v-show="currentFieldset === 'siu'">
      <legend>{{ dd.siu.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.siu)"></tbody>
        </table>
      </div>
      <button @click="dd.siu.open()">open()</button>
      <button @click="dd.siu.close()">close()</button>
      <button @click="dd.siu.getStatus()">getStatus()</button>
    </fieldset>

    <!--pinPad-->
    <fieldset v-show="currentFieldset === 'pinPad'">
      <legend>{{ dd.pinPad.id }}</legend>
      <div class="centered-table-container">
        <table border="1">
          <tbody v-html="createObjectInfoToTable(dd.pinPad)"></tbody>
        </table>
      </div>
      <button @click="dd.pinPad.open()">open()</button>
      <button @click="dd.pinPad.close()">close()</button>
      <button @click="dd.pinPad.getStatus()">getStatus()</button>
    </fieldset>
  </div>
</template>

<style scoped>
.centered-table-container {
  display: flex;
  justify-content: center;
  /* 水平居中 */
  align-items: center;
  /* 垂直居中 */
  /* height: 100vh; */
  /* 假设你想要表格在视口高度中居中 */
}
</style>
 

三、运行测试

--详见设备模块

 

posted @ 2024-03-15 00:13  lizhigang  阅读(4)  评论(0编辑  收藏  举报