2
liusuyi
2026-05-12 9b5c9db9189493fbc6db48f55a7b7311b2a3253c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.ard.agent.tool;
 
import com.alibaba.fastjson2.JSON;
import com.ard.common.core.constant.SecurityConstants;
import com.ard.common.core.domain.R;
import com.ard.system.api.RemoteDictService;
import com.ard.system.api.domain.SysDictData;
import com.ard.work.api.RemoteCameraService;
import com.ard.work.api.domian.ArdCamera;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
@Slf4j
@Component
public class MonitorTools {
 
    @Resource
    private RemoteCameraService remoteCameraService;
    @Resource
    private RemoteDictService remoteDictService;
 
    @Tool(description = "获取指定摄像机的详细信息,包括IP、名称、状态等")
    public String getCameraDetail(@ToolParam(description = "摄像机ID,如CAM-001") String cameraId) {
        log.info("AI调用getCameraDetail工具,参数: cameraId={}", cameraId);
 
        if (cameraId == null || cameraId.isEmpty()) {
            return "错误:摄像机ID不能为空";
        }
 
        try {
            R<ArdCamera> result = remoteCameraService.getCameraInfoRpc(cameraId, SecurityConstants.INNER);
 
            if (result == null) {
                return "错误:服务返回空结果";
            }
 
            if (result.getCode() == R.SUCCESS) {
                ArdCamera camera = result.getData();
                if (camera == null) {
                    return "提示:未找到摄像机ID为 " + cameraId + " 的摄像机";
                }
                return JSON.toJSONString(camera);
            } else {
                return String.format("获取摄像机信息失败:%s (code: %d)", result.getMsg(), result.getCode());
            }
        } catch (Exception e) {
            log.error("调用getCameraInfoRpc异常", e);
            return "系统异常:" + e.getMessage();
        }
    }
    @Tool(description = "搜索摄像机,支持名称、IP、端口、用户名、密码、坐标、状态关键词模糊查询")
    public String searchCameras(@ToolParam(description = "搜索关键词,支持摄像机名称或ID") String keyword) {
        log.info("AI调用searchCameras工具,参数: keyword={}", keyword);
 
        try {
            R<List<ArdCamera>> result = remoteCameraService.getAllCamera(SecurityConstants.INNER);
 
            if (result == null) {
                return "错误:服务返回空结果";
            }
 
            if (result.getCode() != R.SUCCESS) {
                return String.format("获取摄像机列表失败:%s", result.getMsg());
            }
 
            List<ArdCamera> cameras = result.getData();
            if (cameras == null || cameras.isEmpty()) {
                return "未找到任何摄像机";
            }
 
            List<ArdCamera> filteredCameras = cameras;
//            if (keyword != null && !keyword.isEmpty()) {
//                filteredCameras = cameras.stream()
//                        .filter(c -> (c.getName() != null && c.getName().contains(keyword)) ||
//                                (c.getId() != null && c.getId().contains(keyword)))
//                        .toList();
//            }
 
            if (filteredCameras.isEmpty()) {
                return "未找到符合条件的摄像机";
            }
            R<List<SysDictData>> getFactoryResult =
                    remoteDictService.dictTypeRpc("factory", SecurityConstants.INNER);
            if (getFactoryResult == null || getFactoryResult.getCode() != R.SUCCESS) {
                return String.format("获取厂商信息失败:%s",
                        getFactoryResult != null ? getFactoryResult.getMsg() : "服务返回空结果");
            }
            R<List<SysDictData>> getCameraTypeResult =
                    remoteDictService.dictTypeRpc("camera_type", SecurityConstants.INNER);
            if (getCameraTypeResult == null || getCameraTypeResult.getCode() != R.SUCCESS) {
                return String.format("获取相机类型失败:%s",
                        getCameraTypeResult != null ? getCameraTypeResult.getMsg() : "服务返回空结果");
            }
            StringBuilder resultStr = new StringBuilder();
            resultStr.append(String.format("找到%d台摄像机:\n", filteredCameras.size()));
            for (ArdCamera camera : filteredCameras) {
 
                String factoryName = getFactoryResult.getData().stream()
                        .filter(dict -> camera.getFactory().equals(dict.getDictValue()))
                        .findFirst()
                        .map(SysDictData::getDictLabel)
                        .orElseGet(() -> {
                            log.warn("未找到厂商代码对应的字典项: {}", camera.getFactory());
                            return "未知厂商(" + camera.getFactory() + ")";
                        });
                String cameraTypeName = getCameraTypeResult.getData().stream()
                        .filter(dict -> camera.getType().equals(dict.getDictValue()))
                        .findFirst()
                        .map(SysDictData::getDictLabel)
                        .orElseGet(() -> {
                            log.warn("未找到相机类型对应的字典项: {}", camera.getType());
                            return "未知厂商(" + camera.getType() + ")";
                        });
                resultStr.append(String.format("  - ID: %s, 名称: %s, IP: %s, 厂商: %s,状态: %s, 类型: %s\n",
                        camera.getId(),
                        camera.getName(),
                        camera.getIp(),
                        factoryName,
                        Objects.equals(camera.getState(), "1") ? "在线" : "离线",
                        cameraTypeName));
            }
            return resultStr.toString();
        } catch (Exception e) {
            log.error("调用getAllCamera异常", e);
            return "系统异常:" + e.getMessage();
        }
    }
 
    @Tool(description = "修改摄像机信息,支持修改名称、IP地址、端口、位置、状态等字段。注意:摄像机ID不能修改")
    public String updateCameraInfo(
            @ToolParam(description = "摄像机ID,如CAM-001") String cameraId,
            @ToolParam(description = "摄像机名称,如不需要修改可不传") String name,
            @ToolParam(description = "摄像机IP地址,如不需要修改可不传") String ip,
            @ToolParam(description = "摄像机位置/区域,如不需要修改可不传") String location,
            @ToolParam(description = "摄像机状态:1-在线,0-离线,如不需要修改可不传") String state,
            @ToolParam(description = "摄像机类型,如不需要修改可不传") String type,
            @ToolParam(description = "摄像机厂家/品牌,如不需要修改可不传") String factory
    ) {
        log.info("AI调用updateCameraInfo工具,参数: cameraId={}, name={}, ip={}, location={}, state={}, type={}, factory={}",
                cameraId, name, ip, location, state, type, factory);
 
        // 参数校验
        if (cameraId == null || cameraId.isEmpty()) {
            return "错误:摄像机ID不能为空";
        }
 
        try {
            // 1. 先获取当前摄像机信息
            R<ArdCamera> getResult = remoteCameraService.getCameraInfoRpc(cameraId, SecurityConstants.INNER);
 
            if (getResult == null || getResult.getCode() != R.SUCCESS) {
                return String.format("获取摄像机信息失败:%s",
                        getResult != null ? getResult.getMsg() : "服务返回空结果");
            }
 
            ArdCamera camera = getResult.getData();
            if (camera == null) {
                return "错误:未找到摄像机ID为 " + cameraId + " 的摄像机";
            }
 
            // 2. 构建更新对象(只更新有值的字段)
            ArdCamera updateCamera = new ArdCamera();
            updateCamera.setId(cameraId);
 
            if (name != null && !name.isEmpty()) {
                updateCamera.setName(name);
            } else {
                updateCamera.setName(camera.getName());
            }
 
            if (ip != null && !ip.isEmpty()) {
                updateCamera.setIp(ip);
            } else {
                updateCamera.setIp(camera.getIp());
            }
 
            if (state != null && !state.isEmpty()) {
                updateCamera.setState(state);
            }
 
            if (type != null && !type.isEmpty()) {
                updateCamera.setType(type);
            }
 
            if (factory != null && !factory.isEmpty()) {
                updateCamera.setFactory(factory);
            }
 
            // 3. 调用更新接口(假设有 updateCamera 方法)
            R<Boolean> updateResult = remoteCameraService.updateCamera(updateCamera, SecurityConstants.INNER);
 
            if (updateResult == null) {
                return "错误:服务返回空结果";
            }
 
            if (updateResult.getCode() == R.SUCCESS) {
                // 返回更新后的信息
                R<ArdCamera> newResult = remoteCameraService.getCameraInfoRpc(cameraId, SecurityConstants.INNER);
                ArdCamera newCamera = newResult.getData();
                return String.format("摄像机信息修改成功!\n更新后的信息:\n" +
                                "  - ID: %s\n" +
                                "  - 名称: %s\n" +
                                "  - IP地址: %s\n" +
                                "  - 状态: %s\n" +
                                "  - 类型: %s\n" +
                                "  - 厂家: %s",
                        newCamera.getId(),
                        newCamera.getName(),
                        newCamera.getIp(),
                        Objects.equals(newCamera.getState(), "1") ? "在线" : "离线",
                        newCamera.getType(),
                        newCamera.getFactory());
            } else {
                return String.format("修改摄像机信息失败:%s (code: %d)",
                        updateResult.getMsg(), updateResult.getCode());
            }
 
        } catch (Exception e) {
            log.error("调用updateCamera异常", e);
            return "系统异常:" + e.getMessage();
        }
    }
}