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.

195 lines
6.4 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. import { control, debugControl } from 'apis/system'
  2. import { FtMessage } from 'libs/message'
  3. import { socket } from 'libs/socket'
  4. import { useSystemStore } from 'stores/useSystemStore'
  5. export const sendControl = async (params: any, type?: string) => {
  6. if (!params.cmdId) {
  7. params.cmdId = Date.now()
  8. }
  9. const systemStore = useSystemStore()
  10. const systemStatus = systemStore.systemStatus
  11. const sprayingDisableCmd = [
  12. 'slide_tray_in',
  13. 'slide_tray_out',
  14. 'matrix_prefill',
  15. 'nozzle_pipeline_wash',
  16. 'syringe_pipeline_wash',
  17. 'matrix_spray_start',
  18. 'syringe_pipeline_wash_stop',
  19. 'matrix_prefill_stop',
  20. ]
  21. if (systemStatus.spraying && (type === 'debug' || sprayingDisableCmd.includes(params.cmdCode))) {
  22. FtMessage.error('设备正在喷涂中, 请等待喷涂完成')
  23. return
  24. }
  25. if (!systemStatus.spraying && ['matrix_spray_stop'].includes(params.cmdCode)) {
  26. FtMessage.error('设备没有正在喷涂的任务')
  27. return
  28. }
  29. if (systemStatus.paused && (type === 'debug' || sprayingDisableCmd.includes(params.cmdCode))) {
  30. FtMessage.error('喷涂暂停中,请等待喷涂完成')
  31. return
  32. }
  33. if (!systemStatus.paused && ['matrix_spray_continue'].includes(params.cmdCode)) {
  34. FtMessage.error('当前没有暂停的喷涂任务, 无法继续')
  35. return
  36. }
  37. if (!systemStatus.suspendable && ['matrix_spray_pause'].includes(params.cmdCode)) {
  38. FtMessage.error('当前不可以暂停')
  39. return
  40. }
  41. if (systemStatus.cleaningSyringePipeline && (type === 'debug' || ['nozzle_pipeline_wash', 'matrix_spray_start', 'matrix_prefill'].includes(params.cmdCode))) {
  42. FtMessage.error('正在清洗注射器管路, 无法执行')
  43. return
  44. }
  45. if (systemStatus.cleaningNozzlePipeline && (type === 'debug' || ['syringe_pipeline_wash', 'matrix_spray_start', 'matrix_prefill'].includes(params.cmdCode))) {
  46. FtMessage.error('正在清洗喷嘴管路, 无法执行')
  47. return
  48. }
  49. if (systemStatus.prefilling && (type === 'debug' || ['nozzle_pipeline_wash', 'syringe_pipeline_wash', 'matrix_spray_start'].includes(params.cmdCode))) {
  50. FtMessage.error('正在预充管路, 无法执行')
  51. return
  52. }
  53. systemStore.systemList = []
  54. const cmdName = cmdNameMap[params.cmdCode as keyof typeof cmdNameMap] || params.cmdCode
  55. socket.init((data: any) => {
  56. systemStore.pushSystemList(data)
  57. }, 'cmd_debug')
  58. socket.init((data: any) => {
  59. systemStore.pushSystemList(data)
  60. }, 'cmd_response')
  61. await (type === 'debug' ? debugControl(params) : control(params))
  62. systemStore.updateStreamVisible(true)
  63. FtMessage.success(`[${cmdName}]已发送`)
  64. // if (!res.ok) {
  65. // FtMessage.error(`[${cmdName}]发送失败`)
  66. // return
  67. // }
  68. // FtMessage.success(`[${cmdName}]已发送`)
  69. // const reader = res.body!.getReader()
  70. // const decoder = new TextDecoder()
  71. // systemStore.updateStreamVisible(true)
  72. // let buffer = ''
  73. // FtMessage.success(`[${cmdName}]已开始执行`)
  74. // while (true) {
  75. // const { done, value } = await reader.read()
  76. // if (done) {
  77. // console.log('done')
  78. // FtMessage.success(`[${cmdName}]执行完毕`)
  79. // break
  80. // }
  81. // const chunk = decoder.decode(value, { stream: true })
  82. // // console.log(chunk)
  83. // buffer += chunk
  84. // const { objects, remaining } = extractJSONObjects(buffer)
  85. // buffer = remaining
  86. // objects.forEach((jsonStr) => {
  87. // try {
  88. // const json = JSON.parse(jsonStr)
  89. // systemStore.pushSystemList(json)
  90. // }
  91. // catch (e) {
  92. // console.log(e)
  93. // }
  94. // })
  95. // }
  96. }
  97. export const cmdNameMap = {
  98. slide_tray_in: '推入托盘',
  99. slide_tray_out: '推出托盘',
  100. device_status_get: '获取系统状态',
  101. dehumidifier_start: '除湿',
  102. dehumidifier_stop: '停止除湿',
  103. matrix_prefill: '基质预充',
  104. matrix_prefill_stop: '停止预充',
  105. nozzle_pipeline_wash: '清洗喷嘴管路',
  106. syringe_pipeline_wash: '清洗注射器管路',
  107. syringe_pipeline_wash_stop: '结束清洗',
  108. matrix_spray_start: '开始喷涂',
  109. matrix_spray_stop: '结束喷涂',
  110. matrix_spray_pause: '暂停喷涂',
  111. matrix_spray_continue: '继续喷涂',
  112. motor_x_to_home: 'X轴回原点',
  113. motor_y_to_home: 'Y轴回原点',
  114. motor_z_to_home: 'z轴回原点',
  115. motor_x_stop: 'x轴停止',
  116. motor_y_stop: 'y轴停止',
  117. motor_z_stop: 'z轴停止',
  118. motor_x_to_position: 'X轴移动',
  119. motor_y_to_position: 'Y轴移动',
  120. motor_z_to_position: 'Z轴移动',
  121. syringe_pump_start: '注射泵移动',
  122. syringe_pump_stop: '注射泵停止移动',
  123. high_voltage_open: '打开高压',
  124. high_voltage_close: '关闭高压',
  125. nozzle_valve_open: '喷嘴阀开启',
  126. nozzle_valve_close: '喷嘴阀关闭',
  127. dehumidifier_valve_open: '除湿阀开启',
  128. dehumidifier_valve_close: '除湿阀关闭',
  129. wash_valve_open: '清洗阀开启',
  130. wash_valve_close: '清洗阀关闭',
  131. three_way_valve_close_all: '三通阀管路关闭',
  132. three_way_valve_open_syringe_pipeline: '打开喷嘴管路',
  133. three_way_valve_open_spray_pipeline: '打开注射器管路',
  134. lighting_panel_open: '打开照明灯',
  135. lighting_panel_close: '关闭照明灯',
  136. motor_xyz_origin: '三轴回原点',
  137. }
  138. // const extractJSONObjects = (data: string) => {
  139. // const objects = []
  140. // let startIndex = -1
  141. // let bracketCount = 0
  142. // let inString = false
  143. // let escape = false
  144. //
  145. // for (let i = 0; i < data.length; i++) {
  146. // const char = data[i]
  147. //
  148. // if (inString) {
  149. // if (escape) {
  150. // escape = false
  151. // }
  152. // else if (char === '\\') {
  153. // escape = true
  154. // }
  155. // else if (char === '"') {
  156. // inString = false
  157. // }
  158. // }
  159. // else {
  160. // if (char === '"') {
  161. // inString = true
  162. // }
  163. // else if (char === '{') {
  164. // // 当 bracketCount 为0时,标记一个新的 JSON 对象开始
  165. // if (bracketCount === 0) {
  166. // startIndex = i
  167. // }
  168. // bracketCount++
  169. // }
  170. // else if (char === '}') {
  171. // bracketCount--
  172. // // 当 bracketCount 归零时,说明一个 JSON 对象完整
  173. // if (bracketCount === 0 && startIndex !== -1) {
  174. // const jsonStr = data.slice(startIndex, i + 1)
  175. // objects.push(jsonStr)
  176. // startIndex = -1 // 重置起始位置
  177. // }
  178. // }
  179. // }
  180. // }
  181. // // 如果最后 bracketCount 不为0,说明还有未完成的部分
  182. // const remaining = (bracketCount > 0 && startIndex !== -1) ? data.slice(startIndex) : ''
  183. // return { objects, remaining }
  184. // }