Commit e1afe5f4 authored by 何金镒's avatar 何金镒

思必驰的兜底技能6

parent 189b0678
......@@ -107,6 +107,7 @@ public class DifyControllerV2 {
// 异步执行对话????
eventPublisher.publishEvent(new DifyChatSteamEvent(this,token.getSn(), input));
res = String.format(res, "正在思考中", false);
response = JSONUtil.parseObj(res);
}else if(list.size() > 1){
......
......@@ -33,6 +33,11 @@ public class DifyChatSteamEventListener {
log.info("处理dify AI 聊天:sn:{},input:{}", event.getSn(), event.getInput());
try {
if (MsgWebSocket.getWebSocket(event.getSn())) {
log.error("未找到sn:{}的WebSocket连接,不处理对话", event.getSn());
return;
}
QueryWrapper<CDCToken> wrapper = new QueryWrapper<>();
wrapper.eq("sn", event.getSn());
List<CDCToken> list = tokenService.list(wrapper);
......@@ -49,11 +54,11 @@ public class DifyChatSteamEventListener {
log.info("dify请求返回:{}",ai_result);
String result_end = ai_result.replaceAll("(?m)^\\s*$[\r\n]*", "").replaceAll("[\r\n]+", "\n").replaceAll("[\r\n]", "").trim();
log.info("dify请求返回2--->:{}",result_end);
// String result_end = ai_result.replaceAll("(?m)^\\s*$[\r\n]*", "").replaceAll("[\r\n]+", "\n").replaceAll("[\r\n]", "").trim();
// log.info("dify请求返回2--->:{}",result_end);
// 通过WebSocket发送消息给屏端
MsgWebSocket.sendToSingle(event.getSn(), result_end);
MsgWebSocket.sendToSingle(event.getSn(), ai_result);
log.info("dify请求耗时--->:{}",System.currentTimeMillis() - startTime);
} catch (Exception e) {
......
......@@ -60,16 +60,17 @@ public class CdcHttpUtils {
*
*/
public UserEntity getOwner(CDCToken bySn){
String body = HttpRequest.get(getOwnerUrl)
.header("X-token", bySn.getAccessToken())
.header("gateway-id", bySn.getSn())
.timeout(timeout)
.execute().body();
log.info("去建发获取业主信息,sn:{},ccu:{}",bySn.getSn(),bySn.getCcuId());
if(CDCTokenCache.get(bySn.getSn())!=null){
String s = CDCTokenCache.get(bySn.getSn());
return JSONUtil.toBean(s, UserEntity.class);
}else{
String body = HttpRequest.get(getOwnerUrl)
.header("X-token", bySn.getAccessToken())
.header("gateway-id", bySn.getCcuId())
.timeout(timeout)
.execute().body();
JSONObject jsonObject = JSONUtil.parseObj(body);
if(jsonObject.getInt("code") == 200){
UserEntity userEntity = JSONUtil.toBean(jsonObject.getJSONObject("data"), UserEntity.class);
......@@ -92,7 +93,7 @@ public class CdcHttpUtils {
UserEntity userEntity = getOwner(bySn);
String body = HttpRequest.get(url)
.header("X-token",bySn.getAccessToken())
.header("gateway-id",sn)
.header("gateway-id",bySn.getCcuId())
.header("space-yr",userEntity.communityId)
.header("communityId",userEntity.communityId)
.header("space-phone",userEntity.phoneNum)
......@@ -113,7 +114,7 @@ public class CdcHttpUtils {
UserEntity userEntity = getOwner(bySn);
String result = HttpRequest.post(url)
.header("X-token",bySn.getAccessToken())
.header("gateway-id",sn)
.header("gateway-id",bySn.getCcuId())
.header("space-yr",userEntity.communityId)
.header("communityId",userEntity.communityId)
.header("space-phone",userEntity.phoneNum)
......
......@@ -45,12 +45,13 @@ public class MsgWebSocket {
public static void sendToSingle(String sn, String message) {
try {
log.info("===>APP发送:sn={},message={}",sn,message);
Session session = sessions.get(sn);
if (session != null && session.isOpen()) {
synchronized (session) { // 添加同步锁
session.getBasicRemote().sendText(message);
}
}else{
log.warn("发送消息的连接不存在:sn={}",sn);
}
}catch (Exception e){
e.printStackTrace();
......@@ -58,6 +59,10 @@ public class MsgWebSocket {
}
}
public static boolean getWebSocket(String sn){
Session session = sessions.get(sn);
return session != null && session.isOpen();
}
/**
* 建立连接调用的方法
......
package com.ikonke.konkeaialibabamcp;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ikonke.konkeaialibabamcp.service.cdc.spaces.SpacesService;
import com.ikonke.konkeaialibabamcp.utils.CDCUtils;
import com.ikonke.konkeaialibabamcp.utils.CcuUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
@SpringBootTest
class KonkeAiAlibabaMcpApplicationTests {
@Autowired
private SpacesService spacesService;
public static String sn = "00226DA86A12";
public static String date = "2025-08-13";
public static String roomId = "123309ac482d469c8608ce95e51da65a";
public static String subspaceId = null;
// @Test
// void testHttp(){
// String url = "http://172.17.12.12:9998/dify/chatSteamMessages?query=现在是不是已经秋天了";
// String body1 = HttpRequest.get(url)
// .header("ccuName", "CCU_321410" )
// .header("token", "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoiMDA3NjdkOTAzZDU0NDY1YmE4YjdjMjY3MTMzZmFkZTAiLCJ1c2VyX2tleSI6IjA3MWY5NzE1LTYxZjQtNDYwYS05ZjA5LWY1NDAxYTEwNGM2OCIsInVzZXJuYW1lIjoiSXgrNk5RUno1ZE1vNnFqMndrN2twQT09In0.FbMce1Zoe_Xkeoo9v1S04swrIP3Wb-WZw9FwS3k60UBBM25fbCgZFlUaJo08X4xcxgAbEmYFpXUG2lopFXsCng")
// .header("sn","00226DA86ABB")
// .timeout(1000000)
// .execute().body();
// System.out.println("====================");
// System.out.println(body1);
// System.out.println("=======================");
// }
// @Test
// void testThread(){
// int threadCount = 60;
// CountDownLatch latch = new CountDownLatch(threadCount);
//
// for (int i = 0; i < threadCount; i++) {
// int finalI = i;
// Thread thread = new Thread(() -> {
// try {
// long begin = System.currentTimeMillis();
// String url = "172.17.12.12:9998/dify/chatSteamMessages?query=小明一天能赚325,那么"+ finalI +"天能赚多少钱?";
// String ccuName = "CCU_32142"+ finalI;
// String sn = "00226DA86AD"+ finalI;
// String token = "11A"+ finalI;
// String result2 = HttpRequest.get(url)
// .header("ccuName", ccuName)//头信息,多个头信息多次调用此方法即可
// .header("sn", sn)
// .header("token", token)
// .timeout(2000000)//超时,毫秒
// .execute().body();
// System.out.println(finalI +"------->"+result2);
// System.out.println(finalI +"------->耗时:"+(System.currentTimeMillis()-begin));
// }finally {
// latch.countDown();
// }
// });
// thread.start();
// }
//
// try {
// // 等待所有线程执行完成
// latch.await();
// System.out.println("所有线程执行完成");
// } catch (InterruptedException e) {
// Thread.currentThread().interrupt();
// e.printStackTrace();
// }
// }
// @Test
// void test(){
// for(int i = 0; i < 1; i++){
// long begin = System.currentTimeMillis();
// String url = "172.17.12.12:9998/dify/chatSteamMessages?query=324252加"+i+"等于几";
// String ccuName = "CCU_32142"+i;
// String sn = "00226DA86AD"+i;
// String token = "11A"+i;
// String result2 = HttpRequest.get(url)
// .header("ccuName", ccuName)//头信息,多个头信息多次调用此方法即可
// .header("sn", sn)
// .header("token", token)
// .timeout(20000)//超时,毫秒
// .execute().body();
// System.out.println(i+"------->"+result2);
// System.out.println(i+"------->耗时:"+(System.currentTimeMillis()-begin));
// }
// }
@Test
void contextLoads() {
// JSONObject appointmentTimeByRoomId = spacesService.getAppointmentTimeByRoomId(sn, date, roomId, subspaceId);
// System.out.println(appointmentTimeByRoomId);
// mcp调用--->getUserSpacesCouponsByTime..获取用户当前预约可用的优惠券..sn:00226DA86A12,roomId:123309ac482d469c8608ce95e51da65a,beginTime:2025-08-15 00:00:00,endTime:2025-08-15 23:59:59
// JSONArray userSpacesCouponsByBooking = spacesService.getUserSpacesCouponsByTime(sn, roomId, "2025-08-15 00:00:00", "2025-08-15 23:59:59");
// System.out.println(userSpacesCouponsByBooking);
// Date begin_time = DateUtil.parse("2025-08-15 00:00:00");
// Date end_time = DateUtil.parse("2025-08-15 23:59:59");
// long time = DateUtil.beginOfDay(begin_time).getTime();
// long time1 = DateUtil.endOfDay(begin_time).getTime();
// System.out.println(time);
// System.out.println(begin_time.getTime());
// System.out.println(time1);
// System.out.println(end_time.toInstant());
// System.out.println(DateUtil.between(end_time, DateUtil.endOfDay(begin_time), DateUnit.SECOND));
// boolean b = spacesService.bookingOrder2(sn, "2025-08-14 18:00:00", "2025-08-14 21:00:00", "a550c009a8c041c2ba3fa22490583770", null, 1);
// System.out.println(b);
}
//package com.ikonke.konkeaialibabamcp;
//
//import cn.hutool.core.date.DateUnit;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.http.HttpRequest;
//import cn.hutool.json.JSONArray;
//import cn.hutool.json.JSONObject;
//import com.ikonke.konkeaialibabamcp.service.cdc.spaces.SpacesService;
//import com.ikonke.konkeaialibabamcp.utils.CDCUtils;
//import com.ikonke.konkeaialibabamcp.utils.CcuUtils;
//import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.concurrent.CompletableFuture;
//import java.util.concurrent.CountDownLatch;
//
//@SpringBootTest
//class KonkeAiAlibabaMcpApplicationTests {
//
// @Autowired
// private SpacesService spacesService;
//
// public static String sn = "00226DA86A12";
// public static String date = "2025-08-13";
// public static String roomId = "123309ac482d469c8608ce95e51da65a";
// public static String subspaceId = null;
//
//
//
//// @Test
//// void testHttp(){
//// String url = "http://172.17.12.12:9998/dify/chatSteamMessages?query=现在是不是已经秋天了";
//// String body1 = HttpRequest.get(url)
//// .header("ccuName", "CCU_321410" )
//// .header("token", "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoiMDA3NjdkOTAzZDU0NDY1YmE4YjdjMjY3MTMzZmFkZTAiLCJ1c2VyX2tleSI6IjA3MWY5NzE1LTYxZjQtNDYwYS05ZjA5LWY1NDAxYTEwNGM2OCIsInVzZXJuYW1lIjoiSXgrNk5RUno1ZE1vNnFqMndrN2twQT09In0.FbMce1Zoe_Xkeoo9v1S04swrIP3Wb-WZw9FwS3k60UBBM25fbCgZFlUaJo08X4xcxgAbEmYFpXUG2lopFXsCng")
//// .header("sn","00226DA86ABB")
//// .timeout(1000000)
//// .execute().body();
//// System.out.println("====================");
//// System.out.println(body1);
//// System.out.println("=======================");
//// }
//
//// @Test
//// void testThread(){
//// int threadCount = 60;
//// CountDownLatch latch = new CountDownLatch(threadCount);
////
//// for (int i = 0; i < threadCount; i++) {
//// int finalI = i;
//// Thread thread = new Thread(() -> {
//// try {
//// long begin = System.currentTimeMillis();
//// String url = "172.17.12.12:9998/dify/chatSteamMessages?query=小明一天能赚325,那么"+ finalI +"天能赚多少钱?";
//// String ccuName = "CCU_32142"+ finalI;
//// String sn = "00226DA86AD"+ finalI;
//// String token = "11A"+ finalI;
//// String result2 = HttpRequest.get(url)
//// .header("ccuName", ccuName)//头信息,多个头信息多次调用此方法即可
//// .header("sn", sn)
//// .header("token", token)
//// .timeout(2000000)//超时,毫秒
//// .execute().body();
//// System.out.println(finalI +"------->"+result2);
//// System.out.println(finalI +"------->耗时:"+(System.currentTimeMillis()-begin));
//// }finally {
//// latch.countDown();
//// }
//// });
//// thread.start();
//// }
////
//// try {
//// // 等待所有线程执行完成
//// latch.await();
//// System.out.println("所有线程执行完成");
//// } catch (InterruptedException e) {
//// Thread.currentThread().interrupt();
//// e.printStackTrace();
//// }
//// }
//
//// @Test
//// void test(){
//// for(int i = 0; i < 1; i++){
//// long begin = System.currentTimeMillis();
//// String url = "172.17.12.12:9998/dify/chatSteamMessages?query=324252加"+i+"等于几";
//// String ccuName = "CCU_32142"+i;
//// String sn = "00226DA86AD"+i;
//// String token = "11A"+i;
//// String result2 = HttpRequest.get(url)
//// .header("ccuName", ccuName)//头信息,多个头信息多次调用此方法即可
//// .header("sn", sn)
//// .header("token", token)
//// .timeout(20000)//超时,毫秒
//// .execute().body();
//// System.out.println(i+"------->"+result2);
//// System.out.println(i+"------->耗时:"+(System.currentTimeMillis()-begin));
//// }
//// }
//
// @Test
// void difyTest(){
// //workflows/run
// String url = "http://172.17.12.12:8088/v1/chat-messages";
// String userId = "00226DA86A12";
// String api_key = "app-YHXQcZVzokkdsrQ3mTOFhO9x";
// String query = "帮我打开客厅的单路灯控";
//
// JSONObject inputs = new JSONObject();
// inputs.set("ccuName","CCU_313858");
// inputs.set("sn",userId);
// inputs.set("token","eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoiMDA3NjdkOTAzZDU0NDY1YmE4YjdjMjY3MTMzZmFkZTAiLCJ1c2VyX2tleSI6IjcwZGUyNmU2LTQyYWQtNGE5MS05ZmY5LTE5ZjAzOWMyYWMyNCIsInVzZXJuYW1lIjoiSXgrNk5RUno1ZE1vNnFqMndrN2twQT09In0.g7dnCLgR_xGPbxu9nmUQJeCW8cGfz946vRyhO6fga783EBjfSQYd8Et8grm3M79Oalhe6ZvEwY5Ii1OHSKHcwQ");
//
// JSONObject body = new JSONObject();
// body.set("inputs",inputs);
// body.set("query",query);
// body.set("response_mode","blocking");
// body.set("user",userId);
// body.set("conversation_id","a5597ffd-18c5-430d-af16-60d9ea14ac41");
//
// String body1 = HttpRequest.post(url)
// .header("Authorization", "Bearer " + api_key)
// .header("Content-Type", "application/json")
// .body(body.toString())
// .timeout(1000000)
// .execute().body();
//
// System.out.println(body1);
// JSONObject jsonObject = new JSONObject(body1);
// System.out.println(jsonObject.getStr("answer"));
// void contextLoads() {
//// JSONObject appointmentTimeByRoomId = spacesService.getAppointmentTimeByRoomId(sn, date, roomId, subspaceId);
//// System.out.println(appointmentTimeByRoomId);
//// mcp调用--->getUserSpacesCouponsByTime..获取用户当前预约可用的优惠券..sn:00226DA86A12,roomId:123309ac482d469c8608ce95e51da65a,beginTime:2025-08-15 00:00:00,endTime:2025-08-15 23:59:59
//// JSONArray userSpacesCouponsByBooking = spacesService.getUserSpacesCouponsByTime(sn, roomId, "2025-08-15 00:00:00", "2025-08-15 23:59:59");
//// System.out.println(userSpacesCouponsByBooking);
//
//// Date begin_time = DateUtil.parse("2025-08-15 00:00:00");
//// Date end_time = DateUtil.parse("2025-08-15 23:59:59");
//// long time = DateUtil.beginOfDay(begin_time).getTime();
//// long time1 = DateUtil.endOfDay(begin_time).getTime();
//// System.out.println(time);
//// System.out.println(begin_time.getTime());
//// System.out.println(time1);
//// System.out.println(end_time.toInstant());
//// System.out.println(DateUtil.between(end_time, DateUtil.endOfDay(begin_time), DateUnit.SECOND));
//
//// boolean b = spacesService.bookingOrder2(sn, "2025-08-14 18:00:00", "2025-08-14 21:00:00", "a550c009a8c041c2ba3fa22490583770", null, 1);
//// System.out.println(b);
//
//
//
// }
}
//
//
//// @Test
//// void difyTest(){
//// //workflows/run
//// String url = "http://172.17.12.12:8088/v1/chat-messages";
//// String userId = "00226DA86A12";
//// String api_key = "app-YHXQcZVzokkdsrQ3mTOFhO9x";
//// String query = "帮我打开客厅的单路灯控";
////
//// JSONObject inputs = new JSONObject();
//// inputs.set("ccuName","CCU_313858");
//// inputs.set("sn",userId);
//// inputs.set("token","eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoiMDA3NjdkOTAzZDU0NDY1YmE4YjdjMjY3MTMzZmFkZTAiLCJ1c2VyX2tleSI6IjcwZGUyNmU2LTQyYWQtNGE5MS05ZmY5LTE5ZjAzOWMyYWMyNCIsInVzZXJuYW1lIjoiSXgrNk5RUno1ZE1vNnFqMndrN2twQT09In0.g7dnCLgR_xGPbxu9nmUQJeCW8cGfz946vRyhO6fga783EBjfSQYd8Et8grm3M79Oalhe6ZvEwY5Ii1OHSKHcwQ");
////
//// JSONObject body = new JSONObject();
//// body.set("inputs",inputs);
//// body.set("query",query);
//// body.set("response_mode","blocking");
//// body.set("user",userId);
//// body.set("conversation_id","a5597ffd-18c5-430d-af16-60d9ea14ac41");
////
//// String body1 = HttpRequest.post(url)
//// .header("Authorization", "Bearer " + api_key)
//// .header("Content-Type", "application/json")
//// .body(body.toString())
//// .timeout(1000000)
//// .execute().body();
////
//// System.out.println(body1);
//// JSONObject jsonObject = new JSONObject(body1);
//// System.out.println(jsonObject.getStr("answer"));
//// }
//
//}
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