haungxing
7 months ago
23 changed files with 530 additions and 122 deletions
@ -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<Object> sendRemoteControl(ControlDTO controlDTO) { |
||||||
|
return remoteService.sendRemoteControl(controlDTO); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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<ReductionDataVO> 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)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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<Object> sendRemoteControl(ControlDTO controlDTO) { |
||||||
|
return remoteClient.sendCtrl(controlDTO); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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<Boolean> removeSessionId(@RequestParam(value = "id") String sessionId) { |
||||||
|
hznlmInvokeService.removeSessionId(sessionId); |
||||||
|
return R.success("操作成功!"); |
||||||
|
} |
||||||
|
|
||||||
|
@ApiOperation("获取热点问题") |
||||||
|
@ApiOperationSupport(order = 3) |
||||||
|
@GetMapping("/hotQuestions") |
||||||
|
public R<List<String>> hotQuestions() { |
||||||
|
return R.data(hznlmInvokeService.hotQuestions()); |
||||||
|
} |
||||||
|
} |
@ -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<String> sessionIds = sessionRedisManager.getSessionIds(); |
||||||
|
if(CollectionUtil.isEmpty(sessionIds)){ |
||||||
|
XxlJobLogger.log("问题都已经回答完毕!"); |
||||||
|
return ReturnT.SUCCESS; |
||||||
|
} |
||||||
|
List<AnswerVO> 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; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue