| | |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.zy.ai.entity.DeviceConfigsData; |
| | | import com.zy.ai.entity.DeviceRealTimeData; |
| | | import com.zy.ai.entity.ChatCompletionRequest; |
| | | import com.zy.ai.entity.WcsDiagnosisRequest; |
| | | import com.zy.ai.entity.WcsDiagnosisResponse; |
| | | import com.zy.ai.log.AiLogAppender; |
| | | import com.zy.ai.service.WcsDiagnosisService; |
| | | import com.core.annotations.ManagerAuth; |
| | | import com.zy.common.web.BaseController; |
| | | import com.zy.asrs.entity.BasCrnp; |
| | | import com.zy.asrs.entity.WrkMast; |
| | | import com.zy.asrs.service.BasCrnpService; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | |
| | | @RestController |
| | | @RequestMapping("/ai/diagnose") |
| | | @RequiredArgsConstructor |
| | | public class WcsDiagnosisController { |
| | | public class WcsDiagnosisController extends BaseController { |
| | | |
| | | @Autowired |
| | | private WcsDiagnosisService wcsDiagnosisService; |
| | |
| | | |
| | | request.setAlarmMessage("系统不执行任务"); |
| | | |
| | | List<String> logs = AiLogAppender.getRecentLogs(300); |
| | | List<String> logs = AiLogAppender.getRecentLogs(100); |
| | | request.setLogs(logs); |
| | | |
| | | List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<>()); |
| | |
| | | } |
| | | |
| | | request.setDeviceRealtimeData(deviceRealTimeDataList); |
| | | request.setDeviceConfigs(deviceConfigsDataList); |
| | | WcsDiagnosisResponse response = diagnose(request); |
| | | return response; |
| | | } |
| | | |
| | | @GetMapping("/runAiStream") |
| | | public SseEmitter runAiStream() { |
| | | SseEmitter emitter = new SseEmitter(0L); |
| | | |
| | | new Thread(() -> { |
| | | try { |
| | | WcsDiagnosisRequest request = new WcsDiagnosisRequest(); |
| | | request.setAlarmMessage("系统不执行任务"); |
| | | |
| | | List<String> logs = AiLogAppender.getRecentLogs(100); |
| | | request.setLogs(logs); |
| | | |
| | | List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<>()); |
| | | request.setTasks(wrkMasts); |
| | | |
| | | List<DeviceRealTimeData> deviceRealTimeDataList = new ArrayList<>(); |
| | | List<DeviceConfigsData> deviceConfigsDataList = new ArrayList<>(); |
| | | |
| | | List<BasCrnp> basCrnps = basCrnpService.selectList(new EntityWrapper<>()); |
| | | for (BasCrnp basCrnp : basCrnps) { |
| | | CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo()); |
| | | if (crnThread == null) { |
| | | continue; |
| | | } |
| | | |
| | | CrnProtocol protocol = crnThread.getStatus(); |
| | | |
| | | for (StationObjModel stationObjModel : basCrnp.getInStationList$()) { |
| | | StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo()); |
| | | if (stationThread == null) { |
| | | continue; |
| | | } |
| | | |
| | | Map<Integer, StationProtocol> map = stationThread.getStatusMap(); |
| | | StationProtocol stationProtocol = map.get(stationObjModel.getStationId()); |
| | | if (stationProtocol == null) { |
| | | continue; |
| | | } |
| | | |
| | | DeviceRealTimeData stationData = new DeviceRealTimeData(); |
| | | stationData.setDeviceNo(stationObjModel.getDeviceNo()); |
| | | stationData.setDeviceType(String.valueOf(SlaveType.Devp)); |
| | | stationData.setDeviceData(stationProtocol); |
| | | deviceRealTimeDataList.add(stationData); |
| | | } |
| | | |
| | | |
| | | DeviceRealTimeData deviceRealTimeData = new DeviceRealTimeData(); |
| | | deviceRealTimeData.setDeviceNo(basCrnp.getCrnNo()); |
| | | deviceRealTimeData.setDeviceType(String.valueOf(SlaveType.Crn)); |
| | | deviceRealTimeData.setDeviceData(protocol); |
| | | deviceRealTimeDataList.add(deviceRealTimeData); |
| | | |
| | | DeviceConfigsData deviceConfigsData = new DeviceConfigsData(); |
| | | deviceConfigsData.setDeviceNo(basCrnp.getCrnNo()); |
| | | deviceConfigsData.setDeviceType(String.valueOf(SlaveType.Crn)); |
| | | deviceConfigsData.setDeviceData(basCrnp); |
| | | deviceConfigsDataList.add(deviceConfigsData); |
| | | } |
| | | |
| | | request.setDeviceRealtimeData(deviceRealTimeDataList); |
| | | request.setDeviceConfigs(deviceConfigsDataList); |
| | | |
| | | wcsDiagnosisService.diagnoseStream(request, emitter); |
| | | } catch (Exception e) { |
| | | emitter.completeWithError(e); |
| | | } |
| | | }).start(); |
| | | |
| | | return emitter; |
| | | } |
| | | |
| | | @GetMapping("/askStream") |
| | | public SseEmitter askStream(@RequestParam("prompt") String prompt, |
| | | @RequestParam(value = "chatId", required = false) String chatId, |
| | | @RequestParam(value = "reset", required = false, defaultValue = "false") boolean reset) { |
| | | SseEmitter emitter = new SseEmitter(0L); |
| | | new Thread(() -> { |
| | | try { |
| | | WcsDiagnosisRequest request = new WcsDiagnosisRequest(); |
| | | |
| | | List<String> logs = AiLogAppender.getRecentLogs(100); |
| | | request.setLogs(logs); |
| | | |
| | | List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<>()); |
| | | request.setTasks(wrkMasts); |
| | | |
| | | List<DeviceRealTimeData> deviceRealTimeDataList = new ArrayList<>(); |
| | | List<DeviceConfigsData> deviceConfigsDataList = new ArrayList<>(); |
| | | |
| | | List<BasCrnp> basCrnps = basCrnpService.selectList(new EntityWrapper<>()); |
| | | for (BasCrnp basCrnp : basCrnps) { |
| | | CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo()); |
| | | if (crnThread == null) { |
| | | continue; |
| | | } |
| | | |
| | | CrnProtocol protocol = crnThread.getStatus(); |
| | | |
| | | for (StationObjModel stationObjModel : basCrnp.getInStationList$()) { |
| | | StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo()); |
| | | if (stationThread == null) { |
| | | continue; |
| | | } |
| | | |
| | | Map<Integer, StationProtocol> map = stationThread.getStatusMap(); |
| | | StationProtocol stationProtocol = map.get(stationObjModel.getStationId()); |
| | | if (stationProtocol == null) { |
| | | continue; |
| | | } |
| | | |
| | | DeviceRealTimeData stationData = new DeviceRealTimeData(); |
| | | stationData.setDeviceNo(stationObjModel.getDeviceNo()); |
| | | stationData.setDeviceType(String.valueOf(SlaveType.Devp)); |
| | | stationData.setDeviceData(stationProtocol); |
| | | deviceRealTimeDataList.add(stationData); |
| | | } |
| | | |
| | | DeviceRealTimeData deviceRealTimeData = new DeviceRealTimeData(); |
| | | deviceRealTimeData.setDeviceNo(basCrnp.getCrnNo()); |
| | | deviceRealTimeData.setDeviceType(String.valueOf(SlaveType.Crn)); |
| | | deviceRealTimeData.setDeviceData(protocol); |
| | | deviceRealTimeDataList.add(deviceRealTimeData); |
| | | |
| | | DeviceConfigsData deviceConfigsData = new DeviceConfigsData(); |
| | | deviceConfigsData.setDeviceNo(basCrnp.getCrnNo()); |
| | | deviceConfigsData.setDeviceType(String.valueOf(SlaveType.Crn)); |
| | | deviceConfigsData.setDeviceData(basCrnp); |
| | | deviceConfigsDataList.add(deviceConfigsData); |
| | | } |
| | | |
| | | request.setDeviceRealtimeData(deviceRealTimeDataList); |
| | | request.setDeviceConfigs(deviceConfigsDataList); |
| | | |
| | | wcsDiagnosisService.askStream(request, prompt, chatId, reset, emitter); |
| | | } catch (Exception e) { |
| | | emitter.completeWithError(e); |
| | | } |
| | | }).start(); |
| | | return emitter; |
| | | } |
| | | |
| | | @GetMapping("/chats") |
| | | public List<Map<String, Object>> listChats() { |
| | | return wcsDiagnosisService.listChats(); |
| | | } |
| | | |
| | | @DeleteMapping("/chats/{chatId}") |
| | | public Boolean deleteChat(@PathVariable("chatId") String chatId) { |
| | | return wcsDiagnosisService.deleteChat(chatId); |
| | | } |
| | | |
| | | @GetMapping("/chats/{chatId}/history") |
| | | public List<ChatCompletionRequest.Message> getChatHistory(@PathVariable("chatId") String chatId) { |
| | | return wcsDiagnosisService.getChatHistory(chatId); |
| | | } |
| | | |
| | | /** |
| | |
| | | resp.setOriginalRequest(request); |
| | | return resp; |
| | | } |
| | | } |
| | | } |