Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
konke-ai-alibaba-mcp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
何金镒
konke-ai-alibaba-mcp
Commits
d7dcfb2d
Commit
d7dcfb2d
authored
Aug 18, 2025
by
何金镒
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
场景相关
parent
b24a7bae
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
210 additions
and
20 deletions
+210
-20
src/main/java/com/ikonke/konkeaialibabamcp/aitools/DeviceTools.java
...ava/com/ikonke/konkeaialibabamcp/aitools/DeviceTools.java
+20
-16
src/main/java/com/ikonke/konkeaialibabamcp/controller/DifyController.java
...m/ikonke/konkeaialibabamcp/controller/DifyController.java
+90
-0
src/main/java/com/ikonke/konkeaialibabamcp/entity/mongodb/Scene.java
...va/com/ikonke/konkeaialibabamcp/entity/mongodb/Scene.java
+18
-0
src/main/java/com/ikonke/konkeaialibabamcp/entity/mysql/CDCToken.java
...a/com/ikonke/konkeaialibabamcp/entity/mysql/CDCToken.java
+2
-0
src/main/java/com/ikonke/konkeaialibabamcp/service/SycDeviceService.java
...om/ikonke/konkeaialibabamcp/service/SycDeviceService.java
+29
-4
src/main/java/com/ikonke/konkeaialibabamcp/utils/KonkeIotUtils.java
...ava/com/ikonke/konkeaialibabamcp/utils/KonkeIotUtils.java
+17
-0
src/test/java/com/ikonke/konkeaialibabamcp/KonkeAiAlibabaMcpApplicationTests.java
.../konkeaialibabamcp/KonkeAiAlibabaMcpApplicationTests.java
+34
-0
No files found.
src/main/java/com/ikonke/konkeaialibabamcp/aitools/DeviceTools.java
View file @
d7dcfb2d
...
@@ -31,31 +31,35 @@ public class DeviceTools {
...
@@ -31,31 +31,35 @@ public class DeviceTools {
log
.
info
(
"mcp调用--->根据主机号和设备ID查询设备状态信息....ccuName:{},devId:{}"
,
ccuName
,
devId
);
log
.
info
(
"mcp调用--->根据主机号和设备ID查询设备状态信息....ccuName:{},devId:{}"
,
ccuName
,
devId
);
return
deviceStatusUtil
.
findUserByUserId
(
CcuUtils
.
getCcuName
(
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控制打开设备"
)
@Tool
(
description
=
"根据主机号和设备ID控制打开设备"
)
public
String
optDeviceOpen
(
@ToolParam
(
description
=
"主机号"
)
String
ccuName
,
public
String
optDeviceOpen
(
@ToolParam
(
description
=
"主机号"
)
String
ccuName
,
@ToolParam
(
description
=
"设备ID"
)
String
devId
,
@ToolParam
(
description
=
"设备ID"
)
String
devId
,
@ToolParam
(
description
=
"设备类型"
)
String
operateId
){
@ToolParam
(
description
=
"设备类型"
)
String
type
){
log
.
info
(
"mcp调用--->根据主机号和设备ID控制打开设备....ccuName:{},devId:{},
operateId:{}"
,
ccuName
,
devId
,
operateId
);
log
.
info
(
"mcp调用--->根据主机号和设备ID控制打开设备....ccuName:{},devId:{},
type:{}"
,
ccuName
,
devId
,
type
);
return
konkeIotServer
.
optDeviceOpen
(
CcuUtils
.
getCcuName
(
ccuName
),
devId
,
operateId
);
return
konkeIotServer
.
optDeviceOpen
(
CcuUtils
.
getCcuName
(
ccuName
),
devId
,
type
);
}
}
@Tool
(
description
=
"根据主机号和设备ID控制关闭设备"
)
@Tool
(
description
=
"根据主机号和设备ID控制关闭设备"
)
public
String
optDeviceClosed
(
@ToolParam
(
description
=
"主机号"
)
String
ccuName
,
public
String
optDeviceClosed
(
@ToolParam
(
description
=
"主机号"
)
String
ccuName
,
@ToolParam
(
description
=
"设备ID"
)
String
devId
,
@ToolParam
(
description
=
"设备ID"
)
String
devId
,
@ToolParam
(
description
=
"设备类型"
)
String
operateId
){
@ToolParam
(
description
=
"设备类型"
)
String
type
){
log
.
info
(
"mcp调用--->根据主机号和设备ID控制关闭设备....ccuName:{},devId:{},operateId:{}"
,
ccuName
,
devId
,
operateId
);
log
.
info
(
"mcp调用--->根据主机号和设备ID控制关闭设备....ccuName:{},devId:{},type:{}"
,
ccuName
,
devId
,
type
);
return
konkeIotServer
.
optDeviceClosed
(
CcuUtils
.
getCcuName
(
ccuName
),
devId
,
operateId
);
return
konkeIotServer
.
optDeviceClosed
(
CcuUtils
.
getCcuName
(
ccuName
),
devId
,
type
);
}
@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
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
);
}
}
}
src/main/java/com/ikonke/konkeaialibabamcp/controller/DifyController.java
0 → 100644
View file @
d7dcfb2d
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"
);
}
}
src/main/java/com/ikonke/konkeaialibabamcp/entity/mongodb/Scene.java
0 → 100644
View file @
d7dcfb2d
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
;
}
src/main/java/com/ikonke/konkeaialibabamcp/entity/mysql/CDCToken.java
View file @
d7dcfb2d
...
@@ -31,6 +31,8 @@ public class CDCToken implements Serializable {
...
@@ -31,6 +31,8 @@ public class CDCToken implements Serializable {
private
LocalDateTime
createTime
;
private
LocalDateTime
createTime
;
@TableField
(
"state"
)
@TableField
(
"state"
)
private
Integer
state
;
private
Integer
state
;
@TableField
(
"conversationId"
)
private
String
conversationId
;
public
static
final
Integer
STATE_DISABLED
=
0
;
// 不可用
public
static
final
Integer
STATE_DISABLED
=
0
;
// 不可用
...
...
src/main/java/com/ikonke/konkeaialibabamcp/service/SycDeviceService.java
View file @
d7dcfb2d
...
@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
...
@@ -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.Device
;
import
com.ikonke.konkeaialibabamcp.entity.mongodb.Region
;
import
com.ikonke.konkeaialibabamcp.entity.mongodb.Region
;
import
com.ikonke.konkeaialibabamcp.entity.mongodb.Room
;
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.entity.mysql.SynDevice
;
import
com.ikonke.konkeaialibabamcp.service.mysqlservice.ISynDeviceService
;
import
com.ikonke.konkeaialibabamcp.service.mysqlservice.ISynDeviceService
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
...
@@ -45,7 +46,7 @@ public class SycDeviceService {
...
@@ -45,7 +46,7 @@ public class SycDeviceService {
jsonObject
.
set
(
"deviceName"
,
synDevice
.
getDeviceName
());
jsonObject
.
set
(
"deviceName"
,
synDevice
.
getDeviceName
());
jsonObject
.
set
(
"roomId"
,
synDevice
.
getRoomId
());
jsonObject
.
set
(
"roomId"
,
synDevice
.
getRoomId
());
jsonObject
.
set
(
"roomName"
,
synDevice
.
getRoomName
());
jsonObject
.
set
(
"roomName"
,
synDevice
.
getRoomName
());
jsonObject
.
set
(
"
operateId
"
,
synDevice
.
getOperateId
());
jsonObject
.
set
(
"
type
"
,
synDevice
.
getOperateId
());
jsonArray
.
set
(
jsonObject
);
jsonArray
.
set
(
jsonObject
);
}
}
...
@@ -71,9 +72,12 @@ public class SycDeviceService {
...
@@ -71,9 +72,12 @@ public class SycDeviceService {
log
.
error
(
"主机:{} 没有找到匹配的设备"
,
ccuName
);
log
.
error
(
"主机:{} 没有找到匹配的设备"
,
ccuName
);
throw
new
StatefulException
(-
2
,
"没有设备信息"
);
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
));
log
.
debug
(
"要保存的设备:{}"
,
JSONUtil
.
toJsonStr
(
synDeviceList
));
synDeviceService
.
remove
(
new
QueryWrapper
<
SynDevice
>().
eq
(
"ccuId"
,
ccuName
));
synDeviceService
.
remove
(
new
QueryWrapper
<
SynDevice
>().
eq
(
"ccuId"
,
ccuName
));
...
@@ -81,7 +85,7 @@ public class SycDeviceService {
...
@@ -81,7 +85,7 @@ public class SycDeviceService {
}
}
@NotNull
@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
<>();
List
<
SynDevice
>
synDeviceList
=
new
ArrayList
<>();
for
(
Device
d
:
device
){
for
(
Device
d
:
device
){
if
(
d
.
getRoomId
()
==
-
1
){
if
(
d
.
getRoomId
()
==
-
1
){
...
@@ -102,6 +106,27 @@ public class SycDeviceService {
...
@@ -102,6 +106,27 @@ public class SycDeviceService {
synDeviceList
.
add
(
synDevice
);
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
;
return
synDeviceList
;
}
}
...
...
src/main/java/com/ikonke/konkeaialibabamcp/utils/KonkeIotUtils.java
View file @
d7dcfb2d
...
@@ -201,6 +201,23 @@ public class KonkeIotUtils {
...
@@ -201,6 +201,23 @@ public class KonkeIotUtils {
return
optDevice
(
ccuName
,
devId
,
opt
.
toString
());
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}}
* {"action":"SwitchOpt","actionArg":{"on":true}}
*/
*/
...
...
src/test/java/com/ikonke/konkeaialibabamcp/KonkeAiAlibabaMcpApplicationTests.java
View file @
d7dcfb2d
...
@@ -2,6 +2,7 @@ package com.ikonke.konkeaialibabamcp;
...
@@ -2,6 +2,7 @@ package com.ikonke.konkeaialibabamcp;
import
cn.hutool.core.date.DateUnit
;
import
cn.hutool.core.date.DateUnit
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.http.HttpRequest
;
import
cn.hutool.json.JSONArray
;
import
cn.hutool.json.JSONArray
;
import
cn.hutool.json.JSONObject
;
import
cn.hutool.json.JSONObject
;
import
com.ikonke.konkeaialibabamcp.service.cdc.spaces.SpacesService
;
import
com.ikonke.konkeaialibabamcp.service.cdc.spaces.SpacesService
;
...
@@ -49,4 +50,37 @@ class KonkeAiAlibabaMcpApplicationTests {
...
@@ -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"));
// }
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment