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

场景相关

parent b24a7bae
......@@ -31,31 +31,35 @@ public class DeviceTools {
log.info("mcp调用--->根据主机号和设备ID查询设备状态信息....ccuName:{},devId:{}",ccuName,devId);
return deviceStatusUtil.findUserByUserId(CcuUtils.getCcuName(ccuName), devId);
}
@Tool(description = "根据主机号同步设备")
public boolean synchronousDevice(@ToolParam(description = "主机号")String ccuName){
log.info("mcp调用--->根据主机号同步设备....ccuName:{}",ccuName);
return sycDeviceService.synchronousDevice(CcuUtils.getCcuName(ccuName));
}
@Tool(description = "根据主机号和设备ID控制打开设备")
public String optDeviceOpen(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型")String operateId){
log.info("mcp调用--->根据主机号和设备ID控制打开设备....ccuName:{},devId:{},operateId:{}",ccuName,devId,operateId);
return konkeIotServer.optDeviceOpen(CcuUtils.getCcuName(ccuName), devId, operateId);
@ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型")String type){
log.info("mcp调用--->根据主机号和设备ID控制打开设备....ccuName:{},devId:{},type:{}",ccuName,devId,type);
return konkeIotServer.optDeviceOpen(CcuUtils.getCcuName(ccuName), devId, type);
}
@Tool(description = "根据主机号和设备ID控制关闭设备")
public String optDeviceClosed(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型")String operateId){
log.info("mcp调用--->根据主机号和设备ID控制关闭设备....ccuName:{},devId:{},operateId:{}",ccuName,devId,operateId);
return konkeIotServer.optDeviceClosed(CcuUtils.getCcuName(ccuName), devId, operateId);
}
@Tool(description = "根据主机号同步设备")
public boolean synchronousDevice(@ToolParam(description = "主机号")String ccuName){
log.info("mcp调用--->根据主机号同步设备....ccuName:{}",ccuName);
return sycDeviceService.synchronousDevice(CcuUtils.getCcuName(ccuName));
@ToolParam(description = "设备ID")String devId,
@ToolParam(description = "设备类型")String type){
log.info("mcp调用--->根据主机号和设备ID控制关闭设备....ccuName:{},devId:{},type:{}",ccuName,devId,type);
return konkeIotServer.optDeviceClosed(CcuUtils.getCcuName(ccuName), devId, type);
}
@Tool(description = "根据主机号和场景ID控制触发场景")
public String triggerScene(@ToolParam(description = "主机号")String ccuName,
@ToolParam(description = "场景ID")String devId,
@ToolParam(description = "场景类型,type等于scene")String type){
log.info("mcp调用--->根据主机号和场景ID控制关闭设备....ccuName:{},devId:{},type:{}",ccuName,devId,type);
return konkeIotServer.optScene(CcuUtils.getCcuName(ccuName), devId);
}
}
package com.ikonke.konkeaialibabamcp.controller;
import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.ikonke.konkeaialibabamcp.entity.mysql.CDCToken;
import com.ikonke.konkeaialibabamcp.service.mysqlservice.ICDCTokenService;
import com.ikonke.konkeaialibabamcp.utils.CcuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
@Slf4j
@RestController
@RequestMapping("/dify")
public class DifyController {
@Autowired
private ICDCTokenService tokenService;
public static Cache<String, String> conversation_id_cache = CacheUtil.newLRUCache(1024);
@GetMapping("/chatMessages")
public String chatMessages(@RequestParam(name = "query") String query,
@RequestHeader("ccuName") String ccuName,
@RequestHeader("sn") String sn,
@RequestHeader("token") String token){
log.info("开始对话 sn:{},ccuName:{}, query:{}",sn,ccuName,query);
//workflows/run
String url = "http://172.17.12.12:8088/v1/chat-messages";
String api_key = "app-YHXQcZVzokkdsrQ3mTOFhO9x";
JSONObject inputs = new JSONObject();
inputs.set("ccuName",ccuName);
inputs.set("sn",sn);
inputs.set("token",token);
JSONObject body = new JSONObject();
body.set("inputs",inputs);
body.set("query",query);
body.set("response_mode","blocking");//blocking 阻塞模式,streaming 流式模式
body.set("user",sn);
String conversationId = conversation_id_cache.get(sn);
CDCToken bySn = null;
if(StrUtil.isBlank(conversationId)){
bySn = tokenService.findBySn(sn);
if(bySn!=null && StrUtil.isNotBlank(bySn.getConversationId())){
conversationId = bySn.getConversationId();
body.set("conversation_id",conversationId);//一个user一个会话
}
}
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);
if(StrUtil.isBlank(conversationId)){//conversationId为null表示第一次对话
conversationId = jsonObject.getStr("conversation_id");
conversation_id_cache.put(sn, conversationId);
if(bySn!=null){
bySn.setConversationId(conversationId);
tokenService.updateById(bySn);
}else{
bySn = new CDCToken();
bySn.setSn(sn);
bySn.setCcuId(CcuUtils.getCcuName(ccuName));
bySn.setAccessToken(token);
bySn.setCreateTime(LocalDateTime.now());
bySn.setState(CDCToken.STATE_ENABLED);
bySn.setConversationId(conversationId);
tokenService.save(bySn);
}
}
System.out.println(jsonObject.getStr("answer"));
return jsonObject.getStr("answer");
}
}
package com.ikonke.konkeaialibabamcp.entity.mongodb;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@Document(collection = "t_scenes")
public class Scene {
@Id
private String id;
private Integer sceneId;
private String name;
private String ccuName;
private Integer roomId;
}
......@@ -31,6 +31,8 @@ public class CDCToken implements Serializable {
private LocalDateTime createTime;
@TableField("state")
private Integer state;
@TableField("conversationId")
private String conversationId;
public static final Integer STATE_DISABLED = 0; // 不可用
......
......@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ikonke.konkeaialibabamcp.entity.mongodb.Device;
import com.ikonke.konkeaialibabamcp.entity.mongodb.Region;
import com.ikonke.konkeaialibabamcp.entity.mongodb.Room;
import com.ikonke.konkeaialibabamcp.entity.mongodb.Scene;
import com.ikonke.konkeaialibabamcp.entity.mysql.SynDevice;
import com.ikonke.konkeaialibabamcp.service.mysqlservice.ISynDeviceService;
import lombok.extern.slf4j.Slf4j;
......@@ -45,7 +46,7 @@ public class SycDeviceService {
jsonObject.set("deviceName", synDevice.getDeviceName());
jsonObject.set("roomId", synDevice.getRoomId());
jsonObject.set("roomName", synDevice.getRoomName());
jsonObject.set("operateId", synDevice.getOperateId());
jsonObject.set("type", synDevice.getOperateId());
jsonArray.set(jsonObject);
}
......@@ -71,9 +72,12 @@ public class SycDeviceService {
log.error("主机:{} 没有找到匹配的设备",ccuName);
throw new StatefulException(-2, "没有设备信息");
}
//查询场景 todo
//查询场景
Query regionQuery3 = new Query(Criteria.where("ccuName").is(ccuName));
List<Scene> scenes = mongoTemplate.find(regionQuery3, Scene.class);
List<SynDevice> synDeviceList = getSynDevices(ccuName, device, rooms);
List<SynDevice> synDeviceList = getSynDevices(ccuName, device, rooms,scenes);
log.debug("要保存的设备:{}", JSONUtil.toJsonStr(synDeviceList));
synDeviceService.remove(new QueryWrapper<SynDevice>().eq("ccuId", ccuName));
......@@ -81,7 +85,7 @@ public class SycDeviceService {
}
@NotNull
private static List<SynDevice> getSynDevices(String ccuName, List<Device> device, List<Room> rooms) {
private static List<SynDevice> getSynDevices(String ccuName, List<Device> device, List<Room> rooms,List<Scene> scenes) {
List<SynDevice> synDeviceList = new ArrayList<>();
for(Device d : device){
if(d.getRoomId() == -1){
......@@ -102,6 +106,27 @@ public class SycDeviceService {
synDeviceList.add(synDevice);
}
if(scenes!=null && !scenes.isEmpty()){
for (Scene scene : scenes){
if(scene.getRoomId() == -1){
continue;
}
SynDevice synDevice = new SynDevice();
synDevice.setDeviceId(scene.getSceneId());
synDevice.setDeviceName(scene.getName());
synDevice.setOperateId("scene");
synDevice.setRoomId(scene.getRoomId());
for(Room room : rooms){
if(room.getId().equals(scene.getRoomId())){
synDevice.setRoomName(room.getName());
}
}
synDevice.setType(2);
synDevice.setCcuId(ccuName);
synDeviceList.add(synDevice);
}
}
return synDeviceList;
}
......
......@@ -201,6 +201,23 @@ public class KonkeIotUtils {
return optDevice(ccuName, devId, opt.toString());
}
/**
* 触发场景
*/
public String optScene(String ccuName, String sceneId){
String url = baseUrl + "/1.0/app/ccu/" + ccuName + "/dev/" + sceneId + "/opt";
log.info("KonkeIotServer optScene url {}, param {}, appId {}, appKey {}", url, "", appId, appKey);
JSONObject opt = new JSONObject();
opt.set("action", "SceneExecute");
String result2 = HttpRequest.post(url)
.header("appId", appId)
.header("appKey", appKey)
.body(opt.toString())
.execute().body();
log.info("KonkeIotServer optScene result {}", result2);
return result2;
}
/**
* {"action":"SwitchOpt","actionArg":{"on":true}}
*/
......
......@@ -2,6 +2,7 @@ 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;
......@@ -49,4 +50,37 @@ class KonkeAiAlibabaMcpApplicationTests {
}
// @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