You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.7 KiB

5 months ago
  1. import { control, debugControl } from 'apis/system'
  2. import { useSystemStore } from 'stores/useSystemStore'
  3. export const sendControl = async (params: any, type?: string) => {
  4. const systemStore = useSystemStore()
  5. systemStore.systemList = []
  6. const res = await (type ? debugControl(params) : control(params))
  7. if (!res.ok) {
  8. return
  9. }
  10. const reader = res.body!.getReader()
  11. const decoder = new TextDecoder()
  12. systemStore.updateStreamVisible(true)
  13. let buffer = ''
  14. while (true) {
  15. const { done, value } = await reader.read()
  16. if (done) {
  17. break
  18. }
  19. const chunk = decoder.decode(value, { stream: true })
  20. buffer += chunk
  21. const { objects, remaining } = extractJSONObjects(buffer)
  22. buffer = remaining
  23. objects.forEach((jsonStr) => {
  24. try {
  25. const json = JSON.parse(jsonStr)
  26. systemStore.pushSystemList(json)
  27. }
  28. catch (e) {
  29. console.log(e)
  30. }
  31. })
  32. }
  33. }
  34. export const cmdNameMap = {
  35. slide_tray_in: '推入托盘',
  36. slide_tray_out: '推出托盘',
  37. device_status_get: '获取系统状态',
  38. dehumidifier_start: '除湿',
  39. dehumidifier_stop: '停止除湿',
  40. matrix_prefill: '基质预充',
  41. matrix_prefill_stop: '停止预充',
  42. nozzle_pipeline_wash: '清洗喷嘴管路',
  43. syringe_pipeline_wash: '清洗注射器管路',
  44. pipeline_wash_stop: '停止清洗',
  45. matrix_spray_start: '开始喷涂',
  46. matrix_spray_stop: '结束喷涂',
  47. matrix_spray_pause: '暂停喷涂',
  48. matrix_spray_continue: '继续喷涂',
  49. motor_x_to_home: 'X轴回原点',
  50. motor_y_to_home: 'Y轴回原点',
  51. motor_z_to_home: 'z轴回原点',
  52. motor_x_stop: 'x轴停止',
  53. motor_y_stop: 'y轴停止',
  54. motor_z_stop: 'z轴停止',
  55. motor_x_to_position: 'X轴移动',
  56. motor_y_to_position: 'Y轴移动',
  57. motor_z_to_position: 'Z轴移动',
  58. syringe_pump_injection_volume_set: '注射泵移动',
  59. syringe_pump_stop: '注射泵停止移动',
  60. high_voltage_open: '打开高压',
  61. high_voltage_close: '关闭高压',
  62. nozzle_valve_open: '喷嘴阀开启',
  63. nozzle_valve_close: '喷嘴阀关闭',
  64. dehumidifier_valve_open: '除湿阀开启',
  65. dehumidifier_valve_close: '除湿阀关闭',
  66. wash_valve_open: '清洗阀开启',
  67. wash_valve_close: '清洗阀关闭',
  68. three_way_valve_close_all: '三通阀管路关闭',
  69. three_way_valve_open_syringe_pipeline: '打开喷嘴管路',
  70. three_way_valve_open_spray_pipeline: '打开注射器管路',
  71. lighting_panel_open: '打开照明灯',
  72. lighting_panel_close: '关闭照明灯',
  73. }
  74. const extractJSONObjects = (data: string) => {
  75. const objects = []
  76. let startIndex = -1
  77. let bracketCount = 0
  78. let inString = false
  79. let escape = false
  80. for (let i = 0; i < data.length; i++) {
  81. const char = data[i]
  82. if (inString) {
  83. if (escape) {
  84. escape = false
  85. }
  86. else if (char === '\\') {
  87. escape = true
  88. }
  89. else if (char === '"') {
  90. inString = false
  91. }
  92. }
  93. else {
  94. if (char === '"') {
  95. inString = true
  96. }
  97. else if (char === '{') {
  98. // 当 bracketCount 为0时,标记一个新的 JSON 对象开始
  99. if (bracketCount === 0) {
  100. startIndex = i
  101. }
  102. bracketCount++
  103. }
  104. else if (char === '}') {
  105. bracketCount--
  106. // 当 bracketCount 归零时,说明一个 JSON 对象完整
  107. if (bracketCount === 0 && startIndex !== -1) {
  108. const jsonStr = data.slice(startIndex, i + 1)
  109. objects.push(jsonStr)
  110. startIndex = -1 // 重置起始位置
  111. }
  112. }
  113. }
  114. }
  115. // 如果最后 bracketCount 不为0,说明还有未完成的部分
  116. const remaining = (bracketCount > 0 && startIndex !== -1) ? data.slice(startIndex) : ''
  117. return { objects, remaining }
  118. }