import { control, debugControl } from 'apis/system' import { useSystemStore } from 'stores/useSystemStore' export const sendControl = async (params: any, type?: string) => { const systemStore = useSystemStore() systemStore.systemList = [] const res = await (type ? debugControl(params) : control(params)) if (!res.ok) { return } const reader = res.body!.getReader() const decoder = new TextDecoder() systemStore.updateStreamVisible(true) let buffer = '' while (true) { const { done, value } = await reader.read() if (done) { break } const chunk = decoder.decode(value, { stream: true }) buffer += chunk const { objects, remaining } = extractJSONObjects(buffer) buffer = remaining objects.forEach((jsonStr) => { try { const json = JSON.parse(jsonStr) systemStore.pushSystemList(json) } catch (e) { console.log(e) } }) } } export const cmdNameMap = { slide_tray_in: '推入托盘', slide_tray_out: '推出托盘', device_status_get: '获取系统状态', dehumidifier_start: '除湿', dehumidifier_stop: '停止除湿', matrix_prefill: '基质预充', matrix_prefill_stop: '停止预充', nozzle_pipeline_wash: '清洗喷嘴管路', syringe_pipeline_wash: '清洗注射器管路', pipeline_wash_stop: '停止清洗', matrix_spray_start: '开始喷涂', matrix_spray_stop: '结束喷涂', matrix_spray_pause: '暂停喷涂', matrix_spray_continue: '继续喷涂', motor_x_to_home: 'X轴回原点', motor_y_to_home: 'Y轴回原点', motor_z_to_home: 'z轴回原点', motor_x_stop: 'x轴停止', motor_y_stop: 'y轴停止', motor_z_stop: 'z轴停止', motor_x_to_position: 'X轴移动', motor_y_to_position: 'Y轴移动', motor_z_to_position: 'Z轴移动', syringe_pump_injection_volume_set: '注射泵移动', syringe_pump_stop: '注射泵停止移动', high_voltage_open: '打开高压', high_voltage_close: '关闭高压', nozzle_valve_open: '喷嘴阀开启', nozzle_valve_close: '喷嘴阀关闭', dehumidifier_valve_open: '除湿阀开启', dehumidifier_valve_close: '除湿阀关闭', wash_valve_open: '清洗阀开启', wash_valve_close: '清洗阀关闭', three_way_valve_close_all: '三通阀管路关闭', three_way_valve_open_syringe_pipeline: '打开喷嘴管路', three_way_valve_open_spray_pipeline: '打开注射器管路', lighting_panel_open: '打开照明灯', lighting_panel_close: '关闭照明灯', } const extractJSONObjects = (data: string) => { const objects = [] let startIndex = -1 let bracketCount = 0 let inString = false let escape = false for (let i = 0; i < data.length; i++) { const char = data[i] if (inString) { if (escape) { escape = false } else if (char === '\\') { escape = true } else if (char === '"') { inString = false } } else { if (char === '"') { inString = true } else if (char === '{') { // 当 bracketCount 为0时,标记一个新的 JSON 对象开始 if (bracketCount === 0) { startIndex = i } bracketCount++ } else if (char === '}') { bracketCount-- // 当 bracketCount 归零时,说明一个 JSON 对象完整 if (bracketCount === 0 && startIndex !== -1) { const jsonStr = data.slice(startIndex, i + 1) objects.push(jsonStr) startIndex = -1 // 重置起始位置 } } } } // 如果最后 bracketCount 不为0,说明还有未完成的部分 const remaining = (bracketCount > 0 && startIndex !== -1) ? data.slice(startIndex) : '' return { objects, remaining } }