From 57755dbc43d64dfb698b0036c0228a4fa33c4da8 Mon Sep 17 00:00:00 2001 From: haungxing <1203316822@qq.com> Date: Tue, 7 May 2024 11:16:20 +0800 Subject: [PATCH] =?UTF-8?q?add:=E5=A4=A7=E6=A8=A1=E5=9E=8B=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interactive/constants/FuncRouteEnum.java | 1 + .../controller/InteractiveController.java | 4 +- .../interactive/service/IInteractiveService.java | 2 +- .../service/impl/InteractiveServiceImpl.java | 4 +- .../service/impl/JumpRouteJoinStrategy.java | 46 +++++++++++++++------- .../bigmodel/schedule/InteractiveSchedule.java | 2 +- .../websocket/handler/InteractiveHandler.java | 21 ++++++++-- .../sessionManager/InteractiveSessionManager.java | 12 +++++- 8 files changed, 66 insertions(+), 26 deletions(-) diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/constants/FuncRouteEnum.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/constants/FuncRouteEnum.java index 11ef13c..4d51364 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/constants/FuncRouteEnum.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/constants/FuncRouteEnum.java @@ -13,6 +13,7 @@ import java.util.Optional; @AllArgsConstructor public enum FuncRouteEnum { OPEN_SCADA("open_scada","打开实时画面"), + OPEN_VIDEO("open_video","打开视频监控"), ; @Getter private String funcCode; diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/InteractiveController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/InteractiveController.java index eebb4bf..550007c 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/InteractiveController.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/InteractiveController.java @@ -48,8 +48,8 @@ public class InteractiveController { @RequestMapping(value = "/authentication",method = {RequestMethod.GET,RequestMethod.POST}) public R authentication(@RequestParam(required = false) @ApiParam("站点编号") String stationId, @RequestParam @ApiParam("用户ID") String userId, - @RequestParam(required = false) @ApiParam("菜单ID") String menuId) { - return R.status(interactiveService.authentication(stationId,userId,menuId)); + @RequestParam(required = false) @ApiParam("菜单ID") String funcCode) { + return R.status(interactiveService.authentication(stationId,userId,funcCode)); } @ApiOperation("获取问答sessionId") diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IInteractiveService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IInteractiveService.java index 1c718ac..938b518 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IInteractiveService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IInteractiveService.java @@ -21,6 +21,6 @@ public interface IInteractiveService { List getAnswerBySessionIds(String sessionIds); - Boolean authentication(String stationId, String userId, String menuId); + Boolean authentication(String stationId, String userId, String funcCode); } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java index 91c6820..8d81e3b 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java @@ -90,14 +90,14 @@ public class InteractiveServiceImpl implements IInteractiveService { HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAssistantStatus()) .body(JSON.toJSONString(params)).execute(); Assert.isTrue(response.getStatus() == HttpServletResponse.SC_OK && "1".equals(JSONObject.parseObject(response.body()).getString("success")), () -> { - throw new ServiceException("远程调用大模型【发起问答】接口失败!"); + throw new ServiceException("远程调用大模型【获取问题答案】接口失败!"); }); String data = JSONObject.parseObject(response.body()).getString("data"); return JSONArray.parseArray(data,AnswerVO.class); } @Override - public Boolean authentication(String stationId, String userId, String menuId) { + public Boolean authentication(String stationId, String userId, String funcCode) { //TODO 鉴权逻辑完善 return true; } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/JumpRouteJoinStrategy.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/JumpRouteJoinStrategy.java index 11e0cd4..3e2f616 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/JumpRouteJoinStrategy.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/JumpRouteJoinStrategy.java @@ -16,6 +16,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.IntStream; /** * @Author: huangxing @@ -35,6 +36,8 @@ public class JumpRouteJoinStrategy { switch(routeEnum) { case OPEN_SCADA: return this.getScadaExtra(args,function); + case OPEN_VIDEO: + return this.getVideoExtra(args,function); default: break; } @@ -77,28 +80,41 @@ public class JumpRouteJoinStrategy { return extraVO; } + private ExtraVO getVideoExtra(Map args,FunctionEntity function) { + // 跳转页面逻辑 + ExtraVO extraVO = new ExtraVO(); + Map params = this.videoResolve(args); + String path = this.replacePath(function.getPath(), params); + extraVO.setRoute(path); + return extraVO; + } + /** - * 打开实时画面 + * 解析实时画面参数 * @param args 大模型解析参数 * @return 实时画面路径拼接所需参数 */ private Map scadaResolve(Map args) { - Map result = new HashMap<>(); - Assert.isTrue(args.containsKey("params"), () -> { - throw new ServiceException("大模型传参缺少params参数"); - }); + String params = args.get("params"); // 参数格式为:picResource^context^stationNum^projectId^taskId^name^id - List params = Func.toStrList("\\^", args.get("params")); - Assert.isTrue(params.size() == 7,() -> { - throw new ServiceException("大模型传参params长度错误,传参为:" + args.get("params")); + String[] keys = new String[]{"picResource","context","stationNum","projectId","taskId","name","id"}; + return this.resolve(params,keys); + } + + private Map videoResolve(Map args) { + String params = args.get("params"); + // 参数格式为:id^name^stationCode^pointCode^host^appKey^appSecret + String[] keys = new String[]{"id","name","stationCode","pointCode","host","appKey","appSecret"}; + return this.resolve(params,keys); + } + + private Map resolve(String paramsStr,String... keys) { + Map result = new HashMap<>(); + List params = Func.toStrList("\\^", paramsStr); + Assert.isTrue(params.size() == keys.length, () -> { + throw new ServiceException("大模型传参params长度错误,传参为:" + paramsStr); }); - result.put("picResource",params.get(0)); - result.put("context",params.get(1)); - result.put("stationNum",params.get(2)); - result.put("projectId",params.get(3)); - result.put("taskId",params.get(4)); - result.put("name",params.get(5)); - result.put("id",params.get(6)); + IntStream.iterate(0,index -> index + 1).limit(params.size()).forEach(index -> result.put(keys[index],params.get(index))); return result; } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java index a9c3ba1..49607a3 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java @@ -72,7 +72,7 @@ public class InteractiveSchedule { AnswerList.parallelStream().forEach(answerVO -> { CompletableFuture.runAsync(() -> { WebSocketSession session = InteractiveSessionManager.get(answerVO.getSessionId()); - TextMessage message = new TextMessage(JSON.toJSONString(answerVO)); + TextMessage message = InteractiveSessionManager.getTextMessage("1",JSON.toJSONString(answerVO)); try { session.sendMessage(message); } catch (IOException e) { diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java index cf4a92f..551e5f7 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java @@ -1,6 +1,7 @@ package com.hnac.hzims.bigmodel.websocket.handler; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.hnac.hzims.bigmodel.interactive.service.IInteractiveService; import com.hnac.hzims.bigmodel.websocket.sessionManager.InteractiveSessionManager; import lombok.extern.slf4j.Slf4j; @@ -13,6 +14,7 @@ import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; +import java.io.IOException; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -46,8 +48,21 @@ public class InteractiveHandler extends TextWebSocketHandler { @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) { - IInteractiveService interactiveService = SpringUtil.getBean(IInteractiveService.class); - R askResult = interactiveService.ask(message.getPayload(), InteractiveSessionManager.getEntryBySession(session).getKey()); - log.info("message handle successful!返回结果为:"+ JSON.toJSONString(askResult)); + JSONObject messageJSON = JSONObject.parseObject(message.getPayload()); + if("0".equals(messageJSON.getString("type"))) { + // 接收心跳消息,返回一条心跳消息 + try { + session.sendMessage(InteractiveSessionManager.getTextMessage("0","收到心跳消息")); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + else { + // 发送问题 + String context = messageJSON.getString("context"); + IInteractiveService interactiveService = SpringUtil.getBean(IInteractiveService.class); + R askResult = interactiveService.ask(context, InteractiveSessionManager.getEntryBySession(session).getKey()); + log.info("message handle successful!返回结果为:"+ JSON.toJSONString(askResult)); + } } } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/sessionManager/InteractiveSessionManager.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/sessionManager/InteractiveSessionManager.java index 5e3d1a6..4458970 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/sessionManager/InteractiveSessionManager.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/sessionManager/InteractiveSessionManager.java @@ -1,7 +1,10 @@ package com.hnac.hzims.bigmodel.websocket.sessionManager; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springblade.core.log.exception.ServiceException; +import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import java.io.IOException; @@ -23,8 +26,6 @@ public class InteractiveSessionManager { /** ws会话池 **/ public static ConcurrentHashMap SESSION_POOL = new ConcurrentHashMap<>(); - private static final Lock lock = new ReentrantLock(); - /** * 获取sessionIds * @return sessionIds @@ -85,4 +86,11 @@ public class InteractiveSessionManager { } SESSION_POOL.remove(sessionId); } + + public static TextMessage getTextMessage(String type,String context) { + JSONObject message = new JSONObject(); + message.put("type",type); + message.put("context",context); + return new TextMessage(JSON.toJSONString(message)); + } }