diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/controller/RemoteController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/controller/RemoteController.java new file mode 100644 index 0000000..c420ba7 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/controller/RemoteController.java @@ -0,0 +1,37 @@ +package com.hnac.hzims.bigmodel.data.controller; + +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; +import com.hnac.hzims.bigmodel.BigModelConstants; +import com.hnac.hzims.bigmodel.data.service.RemoteService; +import com.hnac.hzinfo.log.annotation.Business; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.api.R; +import org.springblade.system.dto.ControlDTO; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @Author: huangxing + * @Date: 2024/06/24 14:42 + */ +@AllArgsConstructor +@RequestMapping("/remote") +@RestController +@Api(value = "数据平台遥控指令管理",tags = "数据平台遥控指令管理") +@Business(module = BigModelConstants.APP_NAME,value = "数据平台遥控指令管理") +public class RemoteController { + + private final RemoteService remoteService; + + @ApiOperation("下发遥控指令") + @ApiOperationSupport(order = 1) + @PostMapping("/sendRemoteControl") + public R sendRemoteControl(ControlDTO controlDTO) { + return remoteService.sendRemoteControl(controlDTO); + } + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/service/HistoryDataService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/service/HistoryDataService.java new file mode 100644 index 0000000..4a3fc76 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/service/HistoryDataService.java @@ -0,0 +1,97 @@ +package com.hnac.hzims.bigmodel.data.service; + +import com.alibaba.fastjson.JSON; +import com.google.common.collect.Lists; +import com.hnac.hzims.bigmodel.interactive.constants.DataMethodEnum; +import com.hnac.hzims.bigmodel.interactive.constants.DateEnum; +import com.hnac.hzims.bigmodel.interactive.vo.HistoryDataSearchVO; +import com.hnac.hzinfo.sdk.core.response.Result; +import com.hnac.hzinfo.sdk.v5.device.DeviceDataClient; +import com.hnac.hzinfo.sdk.v5.device.dto.ReductionAttrDataDTO; +import com.hnac.hzinfo.sdk.v5.device.dto.ReductionDataDTO; +import com.hnac.hzinfo.sdk.v5.device.vo.ReductionDataVO; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.log.exception.ServiceException; +import org.springblade.core.log.logger.BladeLogger; +import org.springblade.core.tool.utils.DateUtil; +import org.springblade.core.tool.utils.Func; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; + +/** + * @Author: huangxing + * @Date: 2024/06/24 14:57 + */ +@Service +@Slf4j +@AllArgsConstructor +public class HistoryDataService { + + private final DeviceDataClient deviceDataClient; + private final BladeLogger logger; + public static final int DATA_COUNT_MAX = 8000; + + public Result getPolymerizationData(HistoryDataSearchVO searchVO) { + DataMethodEnum enumByMethod = DataMethodEnum.getEnumByMethod(searchVO.getMethod()); + Assert.isTrue(Func.isNotEmpty(enumByMethod),() -> { + throw new ServiceException("数据查询聚合方式传参有误,查询失败!"); + }); + // 聚合数据方式处理 + DateEnum dateEnum = DateEnum.getDateEnumByType(searchVO.getDataType()); + ReductionDataDTO dataDTO = new ReductionDataDTO(); + ReductionAttrDataDTO reductionAttrData = new ReductionAttrDataDTO(); + reductionAttrData.setSignage(searchVO.getSignage()); + reductionAttrData.setAccessRules(enumByMethod.getAccessRule()); + reductionAttrData.setKeepFigures(2); + dataDTO.setBeginTime(LocalDateTime.parse(searchVO.getStartTime(), DateUtil.DATETIME_FORMATTER)); + dataDTO.setEndTime(LocalDateTime.parse(searchVO.getEndTime(),DateUtil.DATETIME_FORMATTER)); + dataDTO.setDeviceCode(searchVO.getDeviceCode()); + dataDTO.setNeedPage(false); + dataDTO.setSaveTimeType(dateEnum.getSaveTimeType()); + dataDTO.setTimeInterval(1); + dataDTO.setDtos(Lists.newArrayList(reductionAttrData)); + logger.info("interactive:getPolymerizationData","config传参为:" + JSON.toJSONString(dataDTO)); + return deviceDataClient.pageDeviceCodeAndSignages(dataDTO); + } + + public DateEnum getDateEnumByDuration(String startTime,String endTime,DateEnum dateEnum) { + LocalDateTime start = LocalDateTime.parse(startTime, DateUtil.DATETIME_FORMATTER); + LocalDateTime end = LocalDateTime.parse(endTime, DateUtil.DATETIME_FORMATTER); + Duration duration = Duration.between(start, end); + int count; + switch(dateEnum) { + case YEAR: + count = Long.valueOf(ChronoUnit.YEARS.between(start.toLocalDate(), end.toLocalDate())).intValue(); + break; + case MONTH: + count = Long.valueOf(ChronoUnit.MONTHS.between(start.toLocalDate(), end.toLocalDate())).intValue(); + break; + case DAY: + count = Long.valueOf(ChronoUnit.DAYS.between(start.toLocalDate(), end.toLocalDate())).intValue(); + break; + case HOUR: + count = Long.valueOf(duration.toHours()).intValue(); + break; + case MINUTE: + count = Long.valueOf(duration.toMinutes()).intValue(); + break; + case SECOND: + count = Long.valueOf(duration.getSeconds()).intValue(); + break; + default: + throw new ServiceException("未找到相关时间类型,查询失败!"); + } + if(count <= DATA_COUNT_MAX) { + return dateEnum; + } + else { + return getDateEnumByDuration(startTime, endTime, DateEnum.getDateEnumByOrder(dateEnum.getOrder() + 1)); + } + } + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/service/RemoteService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/service/RemoteService.java new file mode 100644 index 0000000..551a10b --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/service/RemoteService.java @@ -0,0 +1,31 @@ +package com.hnac.hzims.bigmodel.data.service; + +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.api.R; +import org.springblade.system.dto.ControlDTO; +import org.springblade.system.feign.IRemoteClient; +import org.springframework.stereotype.Service; + +/** + * @Author: huangxing + * @Date: 2024/06/24 14:36 + * @Descirbe: 数据平台 - 遥控服务类 + */ +@Service +@Slf4j +@AllArgsConstructor +public class RemoteService { + + private final IRemoteClient remoteClient; + + /** + * 发送遥控指令 + * @param controlDTO 遥控指令 + * @return 结果 + */ + public R sendRemoteControl(ControlDTO controlDTO) { + return remoteClient.sendCtrl(controlDTO); + } + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/AnalyseDataController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/AnalyseDataController.java index 63c6750..5ecf019 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/AnalyseDataController.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/AnalyseDataController.java @@ -1,6 +1,5 @@ package com.hnac.hzims.bigmodel.interactive.controller; -import com.alibaba.fastjson.JSONObject; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import com.hnac.hzims.bigmodel.interactive.service.IAnalyseDataService; import com.hnac.hzims.bigmodel.interactive.vo.HistoryDataSearchVO; diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java new file mode 100644 index 0000000..8ec9e8a --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java @@ -0,0 +1,55 @@ +package com.hnac.hzims.bigmodel.interactive.controller; + +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; +import com.google.common.collect.Lists; +import com.hnac.hzims.bigmodel.BigModelConstants; +import com.hnac.hzims.bigmodel.interactive.service.IHznlmInvokeService; +import com.hnac.hzinfo.log.annotation.Business; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import lombok.AllArgsConstructor; +import org.springblade.core.tool.api.R; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * @Author: huangxing + * @Date: 2024/06/24 14:19 + */ +@RestController +@RequestMapping("/fontEnd/interactive") +@Api(value = "前端交互控制层",tags = "前端交互控制层") +@AllArgsConstructor +@Business(module = BigModelConstants.APP_NAME,value = "前端交互层管理",ignore = false) +public class FontEndInteractiveController { + + private final IHznlmInvokeService hznlmInvokeService; + + @ApiOperation("提问") + @ApiOperationSupport(order = 1) + @GetMapping("/ask") + public R ask(@RequestParam @ApiParam("用户提出问题") String question, @RequestParam @ApiParam("问答sessionId") String sessionId, @RequestParam @ApiParam("用户Id") String userId) { + hznlmInvokeService.ask(question, sessionId, userId); + return R.success("操作成功!"); + } + + @ApiOperation("删除对话sessionId") + @ApiOperationSupport(order = 2) + @GetMapping("/removeSessionId") + public R removeSessionId(@RequestParam(value = "id") String sessionId) { + hznlmInvokeService.removeSessionId(sessionId); + return R.success("操作成功!"); + } + + @ApiOperation("获取热点问题") + @ApiOperationSupport(order = 3) + @GetMapping("/hotQuestions") + public R> hotQuestions() { + return R.data(hznlmInvokeService.hotQuestions()); + } +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/HznlmInteractiveController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/HznlmInteractiveController.java index 2b125a9..3924173 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/HznlmInteractiveController.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/HznlmInteractiveController.java @@ -3,17 +3,16 @@ package com.hnac.hzims.bigmodel.interactive.controller; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import com.hnac.hzims.bigmodel.BigModelConstants; import com.hnac.hzims.bigmodel.interactive.dto.AuthDataDTO; +import com.hnac.hzims.bigmodel.interactive.req.ModelFunctionReq; import com.hnac.hzims.bigmodel.interactive.service.IHznlmInteractiveService; import com.hnac.hzims.bigmodel.interactive.vo.AuthDataVO; +import com.hnac.hzims.bigmodel.interactive.vo.ResolveResultVO; import com.hnac.hzinfo.log.annotation.Business; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import org.springblade.core.tool.api.R; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; @@ -38,4 +37,10 @@ public class HznlmInteractiveController { return R.data(interactiveService.getAuthData(req)); } + @ApiOperation("解析大模型函数") + @ApiOperationSupport(order = 2) + @PostMapping("/resolve") + public R resolve(@RequestBody @Valid ModelFunctionReq req) { + return R.data(interactiveService.resolve(req)); + } } 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 7305f78..ca90465 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 @@ -42,6 +42,7 @@ public class InteractiveController { @ApiOperation("提问") @ApiOperationSupport(order = 2) @GetMapping("/ask") + @Deprecated public R ask(@RequestParam @ApiParam("用户提出问题") String question,@RequestParam @ApiParam("问答sessionId") String sessionId,@RequestParam @ApiParam("用户Id") String userId) { return interactiveService.ask(question, sessionId, userId); } @@ -67,6 +68,7 @@ public class InteractiveController { @ApiOperation("删除对话sessionId") @ApiOperationSupport(order = 5) @GetMapping("/removeSessionId") + @Deprecated public R removeSessionId(@RequestParam(value = "id") String sessionId) { return R.data(interactiveService.removeSessionId(sessionId)); } @@ -99,6 +101,7 @@ public class InteractiveController { @ApiOperation("获取热点问题") @ApiOperationSupport(order = 8) @GetMapping("/hotQuestions") + @Deprecated public R hotQuestions() { try{ List questions = interactiveService.hotQuestions(); diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/factory/AnswerResolveFactory.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/factory/AnswerResolveFactory.java index 27a8978..9595974 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/factory/AnswerResolveFactory.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/factory/AnswerResolveFactory.java @@ -1,6 +1,9 @@ package com.hnac.hzims.bigmodel.interactive.factory; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum; +import com.hnac.hzims.bigmodel.interactive.service.IAnswerResolveService; import com.hnac.hzims.bigmodel.interactive.service.IResolveService; import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; import org.springblade.core.log.exception.ServiceException; @@ -19,7 +22,12 @@ public class AnswerResolveFactory { public static final String REMOTE_ANSWER_SERVICE = "remoteAnswerResolveService"; public static final String PARAM_ANSWER_SERVICE = "paramAnswerResolveService"; - public static IResolveService getResolveService(String funcCode) { + public static IAnswerResolveService getResolveService(Object extra) { + String funcCode = JSONObject.parseObject(JSON.toJSONString(extra)).getString("func"); + return AnswerResolveFactory.getResolveService(funcCode); + } + + public static IAnswerResolveService getResolveService(String funcCode) { FuncRouteEnum funcRouteEnum = FuncRouteEnum.getEnumByFuncCode(funcCode); if(Func.isNotEmpty(funcRouteEnum)) { switch (funcRouteEnum) { diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IAnswerResolveService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IAnswerResolveService.java index 49608bb..4800164 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IAnswerResolveService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IAnswerResolveService.java @@ -3,6 +3,7 @@ package com.hnac.hzims.bigmodel.interactive.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; +import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO; import java.util.Arrays; import java.util.stream.Stream; @@ -15,8 +16,19 @@ public interface IAnswerResolveService { AnswerVO resolve(AnswerVO answer); + /** + * 解析 extra + * @param originExtra 大模型传入 extra + * @return 解析后的 extra + */ + ExtraVO getExtra(JSONObject originExtra); + + /*** + * + * @param answer + * @return + */ default Stream extraStream(AnswerVO answer) { return Arrays.stream(answer.getExtras()).map(JSON::toJSONString).map(JSONObject::parseObject); } - } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/AnalyseDataServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/AnalyseDataServiceImpl.java index 1d574b1..9ff9828 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/AnalyseDataServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/AnalyseDataServiceImpl.java @@ -1,14 +1,12 @@ package com.hnac.hzims.bigmodel.interactive.service.impl; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; +import com.hnac.hzims.bigmodel.data.service.HistoryDataService; import com.hnac.hzims.bigmodel.interactive.constants.DataMethodEnum; import com.hnac.hzims.bigmodel.interactive.constants.DateEnum; import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum; -import com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants; import com.hnac.hzims.bigmodel.interactive.service.IAnalyseDataService; -import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO; import com.hnac.hzims.bigmodel.interactive.vo.HistoryDataSearchVO; import com.hnac.hzinfo.sdk.core.response.Result; @@ -28,7 +26,6 @@ import org.springframework.util.Assert; import java.time.Duration; import java.time.LocalDateTime; -import java.time.Period; import java.time.temporal.ChronoUnit; import java.util.HashMap; import java.util.List; @@ -43,9 +40,7 @@ import java.util.stream.Collectors; @AllArgsConstructor public class AnalyseDataServiceImpl implements IAnalyseDataService { - private final DeviceDataClient deviceDataClient; - private final BladeLogger logger; - public static final int DATA_COUNT_MAX = 8000; + private final HistoryDataService historyDataService; @Override public ExtraVO getHistoryData(HistoryDataSearchVO searchVO) { @@ -57,14 +52,14 @@ public class AnalyseDataServiceImpl implements IAnalyseDataService { DateEnum dateEnum = DateEnum.getDateEnumByType(searchVO.getDataType()); searchVO.setDataType(dateEnum.getDateType()); // 获取最适配时间段 - DateEnum adapterDate = this.getDateEnumByDuration(searchVO.getStartTime(), searchVO.getEndTime(), dateEnum); + DateEnum adapterDate = historyDataService.getDateEnumByDuration(searchVO.getStartTime(), searchVO.getEndTime(), dateEnum); String note = adapterDate.getDateType().equals(searchVO.getDataType()) ? "" : "(由于数据量过大,只能帮您" + adapterDate.getDateType() + "查询)"; searchVO.setDataType(adapterDate.getDateType()); ExtraVO extraVO = new ExtraVO(); extraVO.setType(FuncRouteEnum.HISTORY_DATA.getType().getType()); extraVO.setFuncCode(FuncRouteEnum.HISTORY_DATA.getFuncCode()); Map params = new HashMap<>(1); - Result reductionDataVOR = this.getPolymerizationData(searchVO); + Result reductionDataVOR = historyDataService.getPolymerizationData(searchVO); String label; if(!reductionDataVOR.isSuccess()) { params.put("errorInfo",reductionDataVOR.getMsg()); @@ -102,62 +97,4 @@ public class AnalyseDataServiceImpl implements IAnalyseDataService { extraVO.setParams(params); return extraVO; } - - private Result getPolymerizationData(HistoryDataSearchVO searchVO) { - DataMethodEnum enumByMethod = DataMethodEnum.getEnumByMethod(searchVO.getMethod()); - Assert.isTrue(Func.isNotEmpty(enumByMethod),() -> { - throw new ServiceException("数据查询聚合方式传参有误,查询失败!"); - }); - // 聚合数据方式处理 - DateEnum dateEnum = DateEnum.getDateEnumByType(searchVO.getDataType()); - ReductionDataDTO dataDTO = new ReductionDataDTO(); - ReductionAttrDataDTO reductionAttrData = new ReductionAttrDataDTO(); - reductionAttrData.setSignage(searchVO.getSignage()); - reductionAttrData.setAccessRules(enumByMethod.getAccessRule()); - reductionAttrData.setKeepFigures(2); - dataDTO.setBeginTime(LocalDateTime.parse(searchVO.getStartTime(),DateUtil.DATETIME_FORMATTER)); - dataDTO.setEndTime(LocalDateTime.parse(searchVO.getEndTime(),DateUtil.DATETIME_FORMATTER)); - dataDTO.setDeviceCode(searchVO.getDeviceCode()); - dataDTO.setNeedPage(false); - dataDTO.setSaveTimeType(dateEnum.getSaveTimeType()); - dataDTO.setTimeInterval(1); - dataDTO.setDtos(Lists.newArrayList(reductionAttrData)); - logger.info("interactive:getPolymerizationData","config传参为:" + JSON.toJSONString(dataDTO)); - return deviceDataClient.pageDeviceCodeAndSignages(dataDTO); - } - - private DateEnum getDateEnumByDuration(String startTime,String endTime,DateEnum dateEnum) { - LocalDateTime start = LocalDateTime.parse(startTime, DateUtil.DATETIME_FORMATTER); - LocalDateTime end = LocalDateTime.parse(endTime, DateUtil.DATETIME_FORMATTER); - Duration duration = Duration.between(start, end); - int count; - switch(dateEnum) { - case YEAR: - count = Long.valueOf(ChronoUnit.YEARS.between(start.toLocalDate(), end.toLocalDate())).intValue(); - break; - case MONTH: - count = Long.valueOf(ChronoUnit.MONTHS.between(start.toLocalDate(), end.toLocalDate())).intValue(); - break; - case DAY: - count = Long.valueOf(ChronoUnit.DAYS.between(start.toLocalDate(), end.toLocalDate())).intValue(); - break; - case HOUR: - count = Long.valueOf(duration.toHours()).intValue(); - break; - case MINUTE: - count = Long.valueOf(duration.toMinutes()).intValue(); - break; - case SECOND: - count = Long.valueOf(duration.getSeconds()).intValue(); - break; - default: - throw new ServiceException("未找到相关时间类型,查询失败!"); - } - if(count <= DATA_COUNT_MAX) { - return dateEnum; - } - else { - return getDateEnumByDuration(startTime, endTime, DateEnum.getDateEnumByOrder(dateEnum.getOrder() + 1)); - } - } } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ChoiceAnswerResolveServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ChoiceAnswerResolveServiceImpl.java index 7b27572..47bb78a 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ChoiceAnswerResolveServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ChoiceAnswerResolveServiceImpl.java @@ -28,13 +28,13 @@ public class ChoiceAnswerResolveServiceImpl implements IAnswerResolveService { @Override public AnswerVO resolve(AnswerVO answer) { - List extraList = this.extraStream(answer).map(this::getExtra).collect(Collectors.toList()); - answer.setExtras(extraList.toArray()); - + Object[] extraArray = this.extraStream(answer).map(this::getExtra).toArray(); + answer.setExtras(extraArray); return answer; } - private ExtraVO getExtra(JSONObject originExtra) { + @Override + public ExtraVO getExtra(JSONObject originExtra) { ExtraVO result = new ExtraVO(); JSONArray selections = JSONArray.parseArray(JSON.toJSONString(originExtra.get("data"))); result.setSelection(selections); diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/DiagnoseAnswerResolveServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/DiagnoseAnswerResolveServiceImpl.java index 984247a..b0dcc7f 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/DiagnoseAnswerResolveServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/DiagnoseAnswerResolveServiceImpl.java @@ -1,12 +1,20 @@ package com.hnac.hzims.bigmodel.interactive.service.impl; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum; +import com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants; import com.hnac.hzims.bigmodel.interactive.factory.AnswerResolveFactory; import com.hnac.hzims.bigmodel.interactive.service.IAnswerResolveService; import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; +import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO; import groovy.util.logging.Slf4j; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import java.util.HashMap; +import java.util.Map; + /** * @Author: huangxing * @Date: 2024/06/21 18:59 @@ -18,6 +26,25 @@ public class DiagnoseAnswerResolveServiceImpl implements IAnswerResolveService { @Override public AnswerVO resolve(AnswerVO answer) { - return null; + Object[] extraArray = this.extraStream(answer).map(this::getExtra).toArray(); + answer.setExtras(extraArray); + return answer; + } + + @Override + public ExtraVO getExtra(JSONObject originExtra) { + ExtraVO result = new ExtraVO(); + JSONObject data = JSONObject.parseObject(JSON.toJSONString(originExtra.get("data"))); + Map params = new HashMap<>(5); + params.put("faultId",data.getString("fault_id")); + params.put("name",data.getString("fault_name")); + params.put("station",data.getString("station_id")); + params.put("fdpDeviceName",data.getString("device_name")); + params.put("fdpOrd",data.getString("ord")); + result.setParams(params); + String funcCode = originExtra.getString("func"); + result.setFuncCode(funcCode); + result.setType(FuncRouteEnum.getEnumByFuncCode(funcCode).getType().getType()); + return result; } } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ExtraResolveStrategyService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ExtraResolveStrategyService.java index 1dc967a..40e7b0d 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ExtraResolveStrategyService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ExtraResolveStrategyService.java @@ -8,22 +8,15 @@ import com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants; import com.hnac.hzims.bigmodel.interactive.constants.SearchTypeEnum; import com.hnac.hzims.bigmodel.interactive.service.IAnalyseDataService; import com.hnac.hzims.bigmodel.interactive.vo.*; -import com.hnac.hzims.bigmodel.websocket.constants.RedisKeyConstants; -import com.hnac.hzims.operational.station.entity.StationEntity; import com.hnac.hzims.operational.station.entity.StationVideoTypeEntity; import com.hnac.hzims.operational.station.feign.IStationClient; import com.hnac.hzims.operational.station.feign.IStationVideoTypeClient; import com.hnac.hzinfo.sdk.v5.device.client.DeviceClient; -import com.hnac.hzinfo.sdk.v5.device.vo.DeviceInstanceAttrVO; import com.hnac.hzinfo.sdk.v5.device.vo.DeviceInstanceFuncVO; -import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.log.XxlJobLogger; import lombok.AllArgsConstructor; -import org.springblade.core.log.logger.BladeLogger; import org.springblade.core.tool.api.R; import org.springblade.core.tool.utils.*; -import org.springblade.system.dto.ControlDTO; -import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.time.format.DateTimeFormatter; @@ -147,6 +140,7 @@ public class ExtraResolveStrategyService { return result; } + @Deprecated private ExtraVO resolveConfirmRemote(Map extra) { ExtraVO result = new ExtraVO(); RemoteParamVO remoteParam = JSONObject.parseObject(JSON.toJSONString(extra.get("data")), RemoteParamVO.class); diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java index 34445c7..a09ffa0 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java @@ -4,15 +4,17 @@ import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.TypeReference; +import com.google.common.collect.Lists; import com.hnac.hzims.bigmodel.configuration.BigModelInvokeUrl; import com.hnac.hzims.bigmodel.interactive.service.IHznlmInvokeService; import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; import com.hnac.hzims.bigmodel.manager.SessionRedisManager; import com.hnac.hzims.bigmodel.utils.RequestClientUtil; import com.xxl.job.core.log.XxlJobLogger; -import groovy.util.logging.Slf4j; import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springblade.core.log.exception.ServiceException; import org.springblade.core.log.logger.BladeLogger; import org.springblade.core.tool.api.R; @@ -94,15 +96,20 @@ public class HznlmInvokeServiceImpl implements IHznlmInvokeService { @Override public List hotQuestions() { - return RequestClientUtil.postCall(fdpHost + bigModelInvokeUrl.getHotQuestion(), null, List.class); + try { + return RequestClientUtil.postCall(fdpHost + bigModelInvokeUrl.getHotQuestion(), null, new TypeReference>(){}); + } + catch (Exception e) { + log.error("An error occurred",e); + return Lists.newArrayList(); + } } @Override public List getAnswerBySessionIds(String sessionIds) { Map params = new HashMap<>(); params.put("ids",Func.toStrList(",",sessionIds).toArray()); - List answerVOList = RequestClientUtil.postCall(fdpHost + bigModelInvokeUrl.getAssistantStatus(), params, List.class); - //TODO 解析答案 + List answerVOList = RequestClientUtil.postCall(fdpHost + bigModelInvokeUrl.getAssistantStatus(), params, new TypeReference>(){}); return answerVOList; } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ParamAnswerResolveServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ParamAnswerResolveServiceImpl.java index e982d7f..507b306 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ParamAnswerResolveServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ParamAnswerResolveServiceImpl.java @@ -1,11 +1,26 @@ package com.hnac.hzims.bigmodel.interactive.service.impl; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum; +import com.hnac.hzims.bigmodel.interactive.constants.SearchTypeEnum; import com.hnac.hzims.bigmodel.interactive.factory.AnswerResolveFactory; +import com.hnac.hzims.bigmodel.interactive.service.IAnalyseDataService; import com.hnac.hzims.bigmodel.interactive.service.IAnswerResolveService; -import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; +import com.hnac.hzims.bigmodel.interactive.vo.*; +import com.xxl.job.core.log.XxlJobLogger; import groovy.util.logging.Slf4j; import lombok.AllArgsConstructor; +import org.springblade.core.log.exception.ServiceException; +import org.springblade.core.tool.utils.DateUtil; +import org.springblade.core.tool.utils.Func; +import org.springblade.core.tool.utils.StringUtil; import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; /** * @Author: huangxing @@ -16,8 +31,76 @@ import org.springframework.stereotype.Service; @AllArgsConstructor public class ParamAnswerResolveServiceImpl implements IAnswerResolveService { + private final IAnalyseDataService analyseDataService; + + public static final String PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss.SSS"; + @Override public AnswerVO resolve(AnswerVO answer) { - return null; + Object[] extraArray = this.extraStream(answer).map(this::getExtra).toArray(); + answer.setExtras(extraArray); + return answer; + } + + @Override + public ExtraVO getExtra(JSONObject originExtra) { + DataVO data = JSONObject.parseObject(JSON.toJSONString(originExtra.get("data")),DataVO.class); + Assert.isTrue(Func.isNotEmpty(data),() -> { + XxlJobLogger.log("解析参数答案失败,extra不能为空!"); + throw new ServiceException("解析参数答案失败,extra不能为空!"); + }); + if(Func.isNotEmpty(data.getReal())) { + // 处理查询实时数据 + RealDataVO real = data.getReal(); + Date date = DateUtil.parse(real.getTime(), PATTERN_DATETIME); + real.setTime(DateUtil.format(date,DateUtil.PATTERN_DATETIME)); + } + else if(StringUtil.isNotBlank(data.getType()) && SearchTypeEnum.HISTORY.getSearchType().equals(data.getType())) { + // 处理查询历史数据 + this.getHistoryData(data); + } + String funcCode = originExtra.getString("func"); + ExtraVO result = new ExtraVO(); + result.setFuncCode(funcCode); + result.setType(FuncRouteEnum.getEnumByFuncCode(funcCode).getType().getType()); + Map param = new HashMap(1); + param.put("data", data); + result.setParams(param); + return result; } + + /** + * 查询历史数据 + * @param data 大模型识别数据信息 + * @return 历史数据 + */ + private ExtraVO getHistoryData(DataVO data) { + // 确认查询聚合方式传入 + Assert.isTrue(Func.isNotEmpty(data.getItem()) && StringUtil.isNotBlank(data.getMethod()),() -> { + XxlJobLogger.log("历史数据查询方式为空,查询失败!"); + throw new ServiceException("历史数据查询方式为空,查询失败!"); + }); + return analyseDataService.getHistoryData(this.convertHistoryDataSearch(data)); + } + + /** + * 获取历史数据查询对象 + * @param data 大模型解析点位信息 + * @return 历史数据查询对象 + */ + private HistoryDataSearchVO convertHistoryDataSearch(DataVO data) { + HistoryDataSearchVO searchVO = new HistoryDataSearchVO(); + DataItemVO item = data.getItem(); + searchVO.setDataType(data.getDataType()); + searchVO.setAttrName(item.getAttrName()); + searchVO.setDeviceCode(item.getDeviceId()); + searchVO.setDeviceName(item.getDeviceName()); + searchVO.setStationName(item.getProjectName()); + searchVO.setMethod(data.getMethod()); + searchVO.setSignage(item.getSignage()); + searchVO.setStartTime(data.getStartTime()); + searchVO.setEndTime(data.getEndTime()); + return searchVO; + } + } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/RemoteAnswerResolveServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/RemoteAnswerResolveServiceImpl.java index 9ba5e42..95f271a 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/RemoteAnswerResolveServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/RemoteAnswerResolveServiceImpl.java @@ -1,12 +1,24 @@ package com.hnac.hzims.bigmodel.interactive.service.impl; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum; import com.hnac.hzims.bigmodel.interactive.factory.AnswerResolveFactory; import com.hnac.hzims.bigmodel.interactive.service.IAnswerResolveService; import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; +import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO; +import com.hnac.hzims.bigmodel.interactive.vo.RemoteParamVO; +import com.hnac.hzinfo.sdk.v5.device.client.DeviceClient; +import com.hnac.hzinfo.sdk.v5.device.vo.DeviceInstanceFuncVO; import groovy.util.logging.Slf4j; import lombok.AllArgsConstructor; +import org.springblade.core.tool.api.R; import org.springframework.stereotype.Service; +import java.util.HashMap; +import java.util.Map; + /** * @Author: huangxing * @Date: 2024/06/21 19:02 @@ -16,8 +28,32 @@ import org.springframework.stereotype.Service; @AllArgsConstructor public class RemoteAnswerResolveServiceImpl implements IAnswerResolveService { + private final DeviceClient deviceClient; + @Override public AnswerVO resolve(AnswerVO answer) { - return null; + Object[] extraArray = this.extraStream(answer).map(this::getExtra).toArray(); + answer.setExtras(extraArray); + return answer; + } + + @Override + public ExtraVO getExtra(JSONObject originExtra) { + ExtraVO result = new ExtraVO(); + RemoteParamVO remoteParam = JSONObject.parseObject(JSON.toJSONString(originExtra.get("data")), RemoteParamVO.class); + String funcCode = originExtra.getString("func"); + result.setFuncCode(funcCode); + result.setType(FuncRouteEnum.getEnumByFuncCode(funcCode).getType().getType()); + R funcVOR = deviceClient.getFuncById(remoteParam.getFuncId()); + if(funcVOR.isSuccess()) { + Map param = new HashMap(); + param.put("control",funcVOR.getData()); + param.put("deviceName",remoteParam.getDeviceName()); + param.put("projectName",remoteParam.getProjectName()); + param.put("deviceCode",remoteParam.getDeviceId()); + param.put("value",remoteParam.getValue()); + result.setParams(param); + } + return result; } } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/vo/AnswerVO.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/vo/AnswerVO.java index e4b9e1c..b42e0a3 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/vo/AnswerVO.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/vo/AnswerVO.java @@ -7,6 +7,7 @@ import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; import org.springblade.core.tool.utils.BeanUtil; import java.io.Serializable; @@ -19,6 +20,7 @@ import java.io.Serializable; @ApiModel("HZLLM答案VO对象") @EqualsAndHashCode @AllArgsConstructor +@NoArgsConstructor public class AnswerVO implements Serializable { @ApiModelProperty("发起问答时的随机ID") diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/manager/SessionRedisManager.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/manager/SessionRedisManager.java index decbf60..43c7af3 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/manager/SessionRedisManager.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/manager/SessionRedisManager.java @@ -1,10 +1,13 @@ package com.hnac.hzims.bigmodel.manager; +import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; import lombok.AllArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; +import java.util.List; + import static com.hnac.hzims.bigmodel.schedule.XxlJobHandlerConstant.HZIMS_BIGMODEL_ASK_KEY; /** @@ -25,4 +28,7 @@ public class SessionRedisManager { redisTemplate.opsForList().remove(HZIMS_BIGMODEL_ASK_KEY,1,sessionId); } + public List getSessionIds() { + return redisTemplate.opsForList().range(HZIMS_BIGMODEL_ASK_KEY, 0, -1); + } } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/FrontEndInteractiveSchedule.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/FrontEndInteractiveSchedule.java new file mode 100644 index 0000000..cb70eb0 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/FrontEndInteractiveSchedule.java @@ -0,0 +1,85 @@ +package com.hnac.hzims.bigmodel.schedule; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.hnac.hzims.bigmodel.interactive.factory.AnswerResolveFactory; +import com.hnac.hzims.bigmodel.interactive.service.IAnswerResolveService; +import com.hnac.hzims.bigmodel.interactive.service.IHznlmInteractiveService; +import com.hnac.hzims.bigmodel.interactive.service.IHznlmInvokeService; +import com.hnac.hzims.bigmodel.interactive.service.IResolveService; +import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; +import com.hnac.hzims.bigmodel.manager.SessionRedisManager; +import com.hnac.hzims.bigmodel.websocket.service.InteractiveWsService; +import com.hnac.hzims.bigmodel.websocket.sessionManager.InteractiveSessionManager; +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.annotation.XxlJob; +import com.xxl.job.core.log.XxlJobLogger; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.log.logger.BladeLogger; +import org.springblade.core.tool.utils.CollectionUtil; +import org.springblade.core.tool.utils.Func; +import org.springblade.core.tool.utils.ObjectUtil; +import org.springframework.stereotype.Component; +import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketSession; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.stream.Collectors; + +import static com.hnac.hzims.bigmodel.schedule.XxlJobHandlerConstant.*; + +/** + * @Author: huangxing + * @Date: 2024/06/24 09:53 + */ +@Component +@AllArgsConstructor +@Slf4j +public class FrontEndInteractiveSchedule { + + private final SessionRedisManager sessionRedisManager; + private final InteractiveWsService wsService; + private final ThreadPoolExecutor getAnswerPoolExecutor; + private final IHznlmInvokeService hznlmInvokeService; + private final BladeLogger logger; + + @XxlJob(GET_INTERACTIVE_RESULT) + public ReturnT getInteractiveResult(String params) { + List sessionIds = sessionRedisManager.getSessionIds(); + if(CollectionUtil.isEmpty(sessionIds)){ + XxlJobLogger.log("问题都已经回答完毕!"); + return ReturnT.SUCCESS; + } + List answerList = hznlmInvokeService.getAnswerBySessionIds(String.join(",", sessionIds)); + answerList.stream().parallel().forEach(answerVO -> CompletableFuture.runAsync(() -> { + // 如果已经获取到答案 则删除缓存 + if(answerVO.getRunning() == 0) { + logger.info("hzims:bigmodel:getInteractiveResult:answer","获取大模型解析答案:" + JSON.toJSONString(answerVO)); + sessionRedisManager.removeSessionId(answerVO.getSessionId()); + } + Object[] extras = answerVO.getExtras(); + if(ObjectUtil.isNotEmpty(answerVO.getExtras()) && extras.length > 0){ + try { + // 解析答案 + Object[] resolveExtras = Arrays.stream(extras).map(extra -> { + IAnswerResolveService answerResolveService = AnswerResolveFactory.getResolveService(extra); + JSONObject extraObject = JSONObject.parseObject(JSON.toJSONString(extra)); + return answerResolveService.getExtra(extraObject); + }).toArray(); + answerVO.setExtras(resolveExtras); + } + catch(Exception e) { + log.error("An error occurred",e); + AnswerVO.error(answerVO.getSessionId(), answerVO.getUserId(), answerVO.getQuery()); + } + } + TextMessage message = InteractiveSessionManager.getTextMessage("1",JSON.toJSONString(answerVO)); + wsService.sendMessage(answerVO.getSessionId(),message); + }, getAnswerPoolExecutor)); + return ReturnT.SUCCESS; + } +} 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 32c132d..cc5cde6 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 @@ -66,30 +66,7 @@ public class InteractiveSchedule { private final ExtraResolveStrategyService extraResolveStrategyService; -// @XxlJob(GET_INTERACTIVE_RESULT) -// public ReturnT execute(String params) { -// String resultKey = ParamCache.getValue(GET_INTERACTIVE_RESULT); -// Set keySet = redisTemplate.keys(resultKey + "*"); -// keySet.parallelStream().forEach(key -> { -// // 根据Key获取sessionId -// List keySplits = Func.toStrList(":", key); -// String sessionId = keySplits.get(2); -// // 查询websocket是否存在连接session -// WebSocketSession session = InteractiveSessionManager.get(sessionId); -// if(session == null) { -// return; -// } -// TextMessage message = new TextMessage(JSON.toJSONString(redisTemplate.opsForValue().get(key))); -// Boolean sendResult = wsService.sendMessage(sessionId, message); -// Assert.isTrue(sendResult, () -> { -// throw new ServiceException(key + "推送消息失败,推送消息体为:" + JSON.toJSONString(redisTemplate.opsForValue().get(key))); -// }); -// redisTemplate.delete(key); -// }); -// return ReturnT.SUCCESS; -// } - - @XxlJob(GET_INTERACTIVE_RESULT) + // @XxlJob(GET_INTERACTIVE_RESULT) public ReturnT getInteractiveResult(String params) { List sessionIds = redisTemplate.opsForList().range(HZIMS_BIGMODEL_ASK_KEY, 0, -1); if(CollectionUtil.isEmpty(sessionIds)){ diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/utils/RequestClientUtil.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/utils/RequestClientUtil.java index d49dc3c..a878fff 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/utils/RequestClientUtil.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/utils/RequestClientUtil.java @@ -4,6 +4,7 @@ import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.TypeReference; import groovy.util.logging.Slf4j; import org.springblade.core.log.exception.ServiceException; import org.springframework.util.Assert; @@ -37,12 +38,12 @@ public class RequestClientUtil { * @return 结果 * @param 结果解析对象类型 */ - public static T postCall(String url, Map body, Class resultT) { + public static T postCall(String url, Map body, TypeReference typeRef) { HttpResponse response = HttpRequest.post(url).body(JSON.toJSONString(body)).execute(); Assert.isTrue(response.getStatus() == HttpServletResponse.SC_OK, () -> { throw new ServiceException("远程调用大模型接口" + url + "失败!"); }); - return JSONObject.parseObject(response.body(), resultT); + return JSONObject.parseObject(response.body(), typeRef); } } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/config/WebSocketConfig.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/config/WebSocketConfig.java index 04a4405..ed9a2a5 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/config/WebSocketConfig.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/config/WebSocketConfig.java @@ -18,7 +18,7 @@ public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { - registry.addHandler(interactiveHandler(), "/interactive/{sessionId}").setAllowedOrigins("*"); + registry.addHandler(interactiveHandler(), "/websocket/interactive/{sessionId}").setAllowedOrigins("*"); } @Bean public WebSocketHandler interactiveHandler() { diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/service/InteractiveWsService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/service/InteractiveWsService.java index 258a376..c02fbab 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/service/InteractiveWsService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/service/InteractiveWsService.java @@ -20,8 +20,14 @@ public class InteractiveWsService implements WebSocketService { @Override public Boolean sendMessage(String sessionId, TextMessage message) { - WebSocketSession session = InteractiveSessionManager.get(sessionId); - return this.sendMessage(session,message); + if(InteractiveSessionManager.SESSION_POOL.containsKey(sessionId)) { + WebSocketSession session = InteractiveSessionManager.get(sessionId); + return this.sendMessage(session,message); + } + else { + log.error("未查询到sessionId,消息推送失败;"); + return false; + } } @Override