index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import {array_column, logs, sleep} from "@/utils/util";
  2. import * as ASCIIUtils from "@/uni_modules/wrs-js-modbusCRCHex/js_sdk/wrs-ASCIIUtils";
  3. import {ScanMachine} from "@/utils/machine/scanMachine";
  4. import {showToast} from "@/utils/app";
  5. import {usbConfigList} from "@/utils/machine/config";
  6. import {BmiMachine} from "@/utils/machine/bmiMachine";
  7. import Debounce from "@/utils/debounce";
  8. let usbSerial = null
  9. // #ifdef APP-PLUS
  10. usbSerial = uni.requireNativePlugin("wrs-usbSerial")
  11. // logs('usbSerial', usbSerial)
  12. // #endif
  13. export class Machine {
  14. constructor() {
  15. this.usbList = []
  16. this.usbNum = 0
  17. this.scanMachineClass = null
  18. this.bmiMachineClass = null
  19. this.permission = {
  20. key: null,// usb地址
  21. status: 0,
  22. okList: [],// 已申请列表
  23. list: {},// 记录列表
  24. }
  25. this.notify = false
  26. this.notifyThis = null
  27. // logs('Machine')
  28. this.debounce = new Debounce()
  29. this._init()
  30. }
  31. /**
  32. * @returns {ScanMachine}
  33. */
  34. scan() {
  35. if (!this.scanMachineClass) {
  36. this.scanMachineClass = new ScanMachine(this, usbSerial)
  37. }
  38. return this.scanMachineClass
  39. }
  40. /**
  41. * @returns {BmiMachine}
  42. */
  43. bmi() {
  44. if (!this.bmiMachineClass) {
  45. this.bmiMachineClass = new BmiMachine(this, usbSerial)
  46. }
  47. return this.bmiMachineClass
  48. }
  49. async open(types = []) {
  50. // logs('open')
  51. for (const k in types) {
  52. let it = types[k]
  53. console.log('it', it)
  54. if (it == 1) {
  55. // await this.scans()._init()
  56. }
  57. }
  58. await sleep(1000)
  59. this.getUsbList()
  60. }
  61. /**
  62. * @param open 插入usb 或 拔出usb
  63. */
  64. async getUsbList(open = true) {
  65. this.debounce.do('getUsbList', () => {
  66. // logs('getUsbList1')
  67. this.usbList = {};
  68. this.usbNum = 0
  69. let that = this
  70. // #ifdef APP-PLUS
  71. this.permission.list = {}
  72. usbSerial.findSerialPortDevice((resp) => {
  73. // logs('getUsbList2', resp)
  74. if (resp.devices) {
  75. that.usbList = resp.devices;
  76. that.usbNum = resp.devices.length
  77. for (const key in this.usbList) {
  78. let it = this.usbList[key]
  79. // logs('it', it)
  80. const {vendorId, productId} = it.device
  81. it.k = `${vendorId}_${productId}`
  82. it.id = usbConfigList[it.k]
  83. // logs('k', k, it.key, usbConfigList[k])
  84. if (usbConfigList[it.k]) {
  85. let data = {
  86. id: it.id,
  87. k: it.k,
  88. key: it.key,
  89. vendorId,
  90. productId,
  91. }
  92. this.permission.list[data.key] = data
  93. } else {
  94. showToast(`未知设备:${key}`)
  95. }
  96. }
  97. }
  98. logs('this.usbList', this.usbList)
  99. logs('this.permission.list', this.permission.list)
  100. logs('this.permission.okList', this.permission.okList)
  101. if (open) {
  102. this.getPermission()
  103. } else {
  104. this.closeUsb()
  105. }
  106. });
  107. // #endif
  108. }, 800)
  109. }
  110. _init() {
  111. // logs('machine _init')
  112. // #ifdef APP-PLUS
  113. usbSerial.registerReceiver((resp) => {
  114. const action = resp.action;
  115. switch (action) {
  116. // USB授权结果
  117. case "com.android.wrs.USB_PERMISSION":
  118. const granted = resp.granted;
  119. // showToast("USB授权结果:" + granted);
  120. // logs(1, granted)
  121. this.getPermission(granted ? 1 : 2)
  122. break;
  123. case "android.hardware.usb.action.USB_DEVICE_ATTACHED":
  124. // logs(2)
  125. // showToast("有USB设备连接");
  126. this.getUsbList(true)
  127. break;
  128. case "android.hardware.usb.action.USB_DEVICE_DETACHED":
  129. // showToast("USB设备断开连接");
  130. this.getUsbList(false)
  131. // logs(3, resp)
  132. break;
  133. default:
  134. break;
  135. }
  136. });
  137. this.getUsbList()
  138. // #endif
  139. }
  140. async getPermission(type = 0) {
  141. // logs('getPermission', type)
  142. const l = Object.values(this.permission.list).length
  143. // logs('getPermission', type)
  144. if (l < 1) return false;
  145. let key = this.permission.key
  146. if (!key) {
  147. key = Object.keys(this.permission.list)[0]
  148. this.permission.key = key
  149. }
  150. // logs('key', key)
  151. let item = this.permission.list[key]
  152. // logs('getPermission2', this.permission.status, type, item)
  153. let status = this.permission.status
  154. // 已授权可跳出
  155. if (this.permission.okList[item.key]) {
  156. type = 3 // 已授权直接跳过
  157. showToast(`已跳出 ${this[item.id]().title} 授权(重复)`, {
  158. position: 'bottom'
  159. })
  160. }
  161. if (type == 0 && status == 0) {
  162. usbSerial.requestPermission({
  163. key: item.key,
  164. });
  165. this[item.id]().key = item.key
  166. this.permission.status = 1
  167. return;
  168. }
  169. // 同意
  170. if (type == 1) {
  171. // logs('connect')
  172. this[item.id]().auth = true
  173. this[item.id]().connect()
  174. // logs('connect1')
  175. this.permission.okList.push({
  176. key: item.key,
  177. id: item.id,
  178. })
  179. // logs('connect2')
  180. uni.$emit('machineUsb', {
  181. id: item.id,
  182. up: true,
  183. })
  184. // logs('connect3')
  185. }
  186. // 拒绝
  187. if (type == 2) {
  188. showToast(`拒绝了 ${this[item.id]().title} 授权,请重启软件再次授权`)
  189. await sleep(1000)
  190. uni.$emit('machineUsb', {
  191. id: item.id,
  192. up: false,
  193. })
  194. }
  195. // logs('getPermissiongetPermission', type)
  196. if (type != 0) {
  197. delete this.permission.list[item.key]
  198. this.permission.status = 0
  199. this.permission.key = null
  200. await this.getPermission(0)
  201. }
  202. }
  203. closeUsb() {
  204. // logs('closeUsb')
  205. let newList = {}
  206. if (this.usbList.length > 0) {
  207. newList = array_column(this.usbList, 'id', 'key')
  208. }
  209. this.permission.okList = this.permission.okList.filter(it => {
  210. if (!newList[it.key]) {
  211. this[it.id]().close();
  212. return false; // 过滤掉需要删除的元素
  213. }
  214. return true; // 保留不需要删除的元素
  215. });
  216. logs('closeUsb this.permission.okList', this.permission.okList)
  217. }
  218. }