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 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> result = remoteCameraService.getAllCamera(SecurityConstants.INNER); if (result == null) { return "错误:服务返回空结果"; } if (result.getCode() != R.SUCCESS) { return String.format("获取摄像机列表失败:%s", result.getMsg()); } List cameras = result.getData(); if (cameras == null || cameras.isEmpty()) { return "未找到任何摄像机"; } List 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> getFactoryResult = remoteDictService.dictTypeRpc("factory", SecurityConstants.INNER); if (getFactoryResult == null || getFactoryResult.getCode() != R.SUCCESS) { return String.format("获取厂商信息失败:%s", getFactoryResult != null ? getFactoryResult.getMsg() : "服务返回空结果"); } R> 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 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 updateResult = remoteCameraService.updateCamera(updateCamera, SecurityConstants.INNER); if (updateResult == null) { return "错误:服务返回空结果"; } if (updateResult.getCode() == R.SUCCESS) { // 返回更新后的信息 R 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(); } } }