Commit 64de612b authored by 何金镒's avatar 何金镒

dify 接口 2

parent 5f0fb9ed
...@@ -31,33 +31,42 @@ public class CDCController { ...@@ -31,33 +31,42 @@ public class CDCController {
public boolean synCDCToken(@RequestParam(name = "token") String token, public boolean synCDCToken(@RequestParam(name = "token") String token,
@RequestParam(name = "sn") String sn, @RequestParam(name = "sn") String sn,
@RequestParam(name = "ccuName") String ccuName){ @RequestParam(name = "ccuName") String ccuName){
long time = System.currentTimeMillis();
log.info("synCDCToken..同步CDCToken..token:{},sn:{},ccuName:{}",token,sn,ccuName); log.info("synCDCToken..同步CDCToken..token:{},sn:{},ccuName:{}",token,sn,ccuName);
CDCToken bySn = tokenService.findBySn(sn); try{
if(bySn==null){ CDCToken bySn = tokenService.findBySn(sn);
bySn = new CDCToken(); if(bySn==null){
bySn.setSn(sn); bySn = new CDCToken();
bySn.setAccessToken(token); bySn.setSn(sn);
bySn.setState(CDCToken.STATE_ENABLED);
bySn.setCcuId(CcuUtils.getCcuName(ccuName));
bySn.setCreateTime(LocalDateTime.now());
tokenService.save(bySn);
}else{
if(bySn.getAccessToken().equals(token)
&& bySn.getSn().equals(sn)
&& bySn.getCcuId().equals(CcuUtils.getCcuName(ccuName))){
return true;
}else{
CdcHttpUtils.CDCTokenCache.remove(sn);
bySn.setAccessToken(token); bySn.setAccessToken(token);
bySn.setRefreshToken(bySn.getAccessToken());
bySn.setState(CDCToken.STATE_ENABLED); bySn.setState(CDCToken.STATE_ENABLED);
bySn.setCreateTime(LocalDateTime.now());
bySn.setCcuId(CcuUtils.getCcuName(ccuName)); bySn.setCcuId(CcuUtils.getCcuName(ccuName));
tokenService.updateById(bySn); bySn.setCreateTime(LocalDateTime.now());
tokenService.save(bySn);
}else{
if(bySn.getAccessToken().equals(token)
&& bySn.getSn().equals(sn)
&& bySn.getCcuId().equals(CcuUtils.getCcuName(ccuName))){
log.info("synCDCToken..不需要同步CDCToken..耗时:{}ms",System.currentTimeMillis()-time);
return true;
}else{
CdcHttpUtils.CDCTokenCache.remove(sn);
bySn.setAccessToken(token);
bySn.setRefreshToken(bySn.getAccessToken());
bySn.setState(CDCToken.STATE_ENABLED);
bySn.setCreateTime(LocalDateTime.now());
bySn.setCcuId(CcuUtils.getCcuName(ccuName));
tokenService.updateById(bySn);
}
} }
log.info("synCDCToken..同步CDCToken成功..耗时:{}ms",System.currentTimeMillis()-time);
return true;
} catch (Exception e) {
log.error("synCDCToken..同步CDCToken失败..token:{},sn:{},ccuName:{}",token,sn,ccuName);
e.printStackTrace();
return false;
} }
return true;
} }
@GetMapping("/getSpacesList") @GetMapping("/getSpacesList")
......
...@@ -85,7 +85,7 @@ public class DifyController { ...@@ -85,7 +85,7 @@ public class DifyController {
JSONObject jsonObject = new JSONObject(body1); JSONObject jsonObject = new JSONObject(body1);
if(StrUtil.isBlank(conversationId)){//conversationId为null表示第一次对话 if(redis_conversationId == null){//conversationId为null表示第一次对话
conversationId = jsonObject.getStr("conversation_id"); conversationId = jsonObject.getStr("conversation_id");
redisTemplate.opsForValue().set(redis_key, conversationId); redisTemplate.opsForValue().set(redis_key, conversationId);
if(bySn!=null){ if(bySn!=null){
...@@ -146,7 +146,6 @@ public class DifyController { ...@@ -146,7 +146,6 @@ public class DifyController {
log.info("dify 对话 body:{}",body); log.info("dify 对话 body:{}",body);
String finalConversationId = conversationId;
return webClient.post() return webClient.post()
.uri(url) .uri(url)
.headers(httpHeaders -> { .headers(httpHeaders -> {
...@@ -157,17 +156,16 @@ public class DifyController { ...@@ -157,17 +156,16 @@ public class DifyController {
.retrieve() .retrieve()
.bodyToFlux(DifyStreamResponse.class)//实体转换 .bodyToFlux(DifyStreamResponse.class)//实体转换
.filter(this::shouldInclude) // 过滤掉不需要的数据【根据需求增加】 .filter(this::shouldInclude) // 过滤掉不需要的数据【根据需求增加】
.map((DifyStreamResponse difyStreamResponse) -> convertToCustomResponseAsync(difyStreamResponse, sn, ccuName, token, finalConversationId)) // 异步转换【如果返回格式自定义则通过异步转换实现】 .map((DifyStreamResponse difyStreamResponse) -> convertToCustomResponseAsync(difyStreamResponse, sn, ccuName, token, redis_conversationId)) // 异步转换【如果返回格式自定义则通过异步转换实现】
.onErrorResume(throwable -> { .onErrorResume(throwable -> {
log.info("异常输出:"+throwable.getMessage()); log.info("异常输出:"+throwable.getMessage());
return null; return null;
}); });
} }
private String convertToCustomResponseAsync(DifyStreamResponse difyStreamResponse,String sn,String ccuName,String token,String conversationId) { private String convertToCustomResponseAsync(DifyStreamResponse difyStreamResponse,String sn,String ccuName,String token,Object redis_conversationId) {
System.out.println("---->"+difyStreamResponse.getConversation_id()); if(redis_conversationId == null && StrUtil.isNotBlank(difyStreamResponse.getConversation_id())){//conversationId为null表示第一次对话
if(StrUtil.isBlank(conversationId) && StrUtil.isNotBlank(difyStreamResponse.getConversation_id())){//conversationId为null表示第一次对话 String conversationId = difyStreamResponse.getConversation_id();
conversationId = difyStreamResponse.getConversation_id();
String redis_key = RedisKeys.KONKE_DIFY_CONVERSATION_ID+ sn; String redis_key = RedisKeys.KONKE_DIFY_CONVERSATION_ID+ sn;
redisTemplate.opsForValue().set(redis_key, conversationId); redisTemplate.opsForValue().set(redis_key, conversationId);
......
...@@ -42,6 +42,7 @@ public class SycDeviceService { ...@@ -42,6 +42,7 @@ public class SycDeviceService {
private IDeviceModelService deviceModelService; private IDeviceModelService deviceModelService;
public JSONArray findByCcu(String ccuName,Integer type,Integer capabilities){ public JSONArray findByCcu(String ccuName,Integer type,Integer capabilities){
long start = System.currentTimeMillis();
log.info("findByCcu..根据主机号查询设备..ccuName:{}",ccuName); log.info("findByCcu..根据主机号查询设备..ccuName:{}",ccuName);
QueryWrapper<SynDevice> wrapper = new QueryWrapper<>(); QueryWrapper<SynDevice> wrapper = new QueryWrapper<>();
wrapper.eq("ccuId", ccuName); wrapper.eq("ccuId", ccuName);
...@@ -82,6 +83,7 @@ public class SycDeviceService { ...@@ -82,6 +83,7 @@ public class SycDeviceService {
jsonArray.set(jsonObject); jsonArray.set(jsonObject);
} }
} }
log.info("findByCcu..根据主机号查询设备..耗时:{}",System.currentTimeMillis()-start);
return jsonArray; return jsonArray;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment