index_old.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. getOnlineUsb(type = 1) {
  111. let list = this.permission.okList
  112. if (type === 1) {
  113. list = array_column(list, 'id')
  114. }
  115. return list
  116. }
  117. _init() {
  118. // logs('machine _init')
  119. // #ifdef APP-PLUS
  120. usbSerial.registerReceiver((resp) => {
  121. const action = resp.action;
  122. switch (action) {
  123. // USB授权结果
  124. case "com.android.wrs.USB_PERMISSION":
  125. const granted = resp.granted;
  126. // showToast("USB授权结果:" + granted);
  127. // logs(1, granted)
  128. this.getPermission(granted ? 1 : 2)
  129. break;
  130. case "android.hardware.usb.action.USB_DEVICE_ATTACHED":
  131. // logs(2)
  132. // showToast("有USB设备连接");
  133. this.getUsbList(true)
  134. break;
  135. case "android.hardware.usb.action.USB_DEVICE_DETACHED":
  136. // showToast("USB设备断开连接");
  137. this.getUsbList(false)
  138. // logs(3, resp)
  139. break;
  140. default:
  141. break;
  142. }
  143. });
  144. this.getUsbList()
  145. // #endif
  146. }
  147. async getPermission(type = 0) {
  148. // logs('getPermission', type)
  149. const l = Object.values(this.permission.list).length
  150. // logs('getPermission', type)
  151. if (l < 1) return false;
  152. let key = this.permission.key
  153. if (!key) {
  154. key = Object.keys(this.permission.list)[0]
  155. this.permission.key = key
  156. }
  157. // logs('key', key)
  158. let item = this.permission.list[key]
  159. // logs('getPermission2', this.permission.status, type, item)
  160. let status = this.permission.status
  161. // 已授权可跳出
  162. if (this.permission.okList[item.key]) {
  163. type = 3 // 已授权直接跳过
  164. showToast(`已跳出 ${this[item.id]().title} 授权(重复)`, {
  165. position: 'bottom'
  166. })
  167. }
  168. if (type == 0 && status == 0) {
  169. usbSerial.requestPermission({
  170. key: item.key,
  171. });
  172. this[item.id]().key = item.key
  173. this.permission.status = 1
  174. return;
  175. }
  176. // 同意
  177. if (type == 1) {
  178. // logs('connect')
  179. this[item.id]().auth = true
  180. this[item.id]().connect()
  181. // logs('connect1')
  182. this.permission.okList.push({
  183. key: item.key,
  184. id: item.id,
  185. })
  186. // logs('connect2')
  187. uni.$emit('machineUsb', {
  188. id: item.id,
  189. up: true,
  190. })
  191. // logs('connect3')
  192. }
  193. // 拒绝
  194. if (type == 2) {
  195. showToast(`拒绝了 ${this[item.id]().title} 授权,请重启软件再次授权`)
  196. await sleep(1000)
  197. uni.$emit('machineUsb', {
  198. id: item.id,
  199. up: false,
  200. })
  201. }
  202. // logs('getPermissiongetPermission', type)
  203. if (type != 0) {
  204. delete this.permission.list[item.key]
  205. this.permission.status = 0
  206. this.permission.key = null
  207. await this.getPermission(0)
  208. }
  209. }
  210. closeUsb() {
  211. // logs('closeUsb')
  212. let newList = {}
  213. if (this.usbList.length > 0) {
  214. newList = array_column(this.usbList, 'id', 'key')
  215. }
  216. this.permission.okList = this.permission.okList.filter(it => {
  217. if (!newList[it.key]) {
  218. this[it.id]().close();
  219. return false; // 过滤掉需要删除的元素
  220. }
  221. return true; // 保留不需要删除的元素
  222. });
  223. logs('closeUsb this.permission.okList', this.permission.okList)
  224. }
  225. }