| | |
| | | package com.mes.device.controller; |
| | | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.mes.device.entity.DeviceConfig; |
| | | import com.mes.device.request.DeviceConfigRequest; |
| | | import com.mes.device.service.DeviceConfigService; |
| | | import com.mes.device.vo.DeviceConfigVO; |
| | | import com.mes.device.vo.StatisticsVO; |
| | | import com.mes.vo.Result; |
| | | import io.swagger.v3.oas.annotations.Operation; |
| | | import io.swagger.v3.oas.annotations.Parameter; |
| | | import io.swagger.v3.oas.annotations.tags.Tag; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import io.swagger.annotations.ApiParam; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | |
| | | @Slf4j |
| | | @RestController |
| | | @RequestMapping("device/config") |
| | | @Tag(name = "设备配置管理", description = "设备配置管理相关接口") |
| | | @Api(tags = "设备配置管理") |
| | | public class DeviceConfigController { |
| | | |
| | | @Autowired |
| | | private DeviceConfigService deviceConfigService; |
| | | |
| | | @Autowired |
| | | private ObjectMapper objectMapper; |
| | | |
| | | /** |
| | | * 创建设备配置 |
| | | */ |
| | | @PostMapping("/devices") |
| | | @Operation(summary = "创建设备配置", description = "创建新的设备配置") |
| | | @ApiOperation("创建设备配置") |
| | | public Result<DeviceConfig> createDevice( |
| | | @Valid @RequestBody DeviceConfig deviceConfig) { |
| | | try { |
| | |
| | | * 更新设备配置 |
| | | */ |
| | | @PostMapping("/devices/update") |
| | | @Operation(summary = "更新设备配置", description = "更新指定ID的设备配置") |
| | | @ApiOperation("更新设备配置") |
| | | public Result<DeviceConfig> updateDevice( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | | DeviceConfig deviceConfig = (DeviceConfig) request.getDeviceConfig(); |
| | | DeviceConfig deviceConfig; |
| | | Object deviceConfigObj = request.getDeviceConfig(); |
| | | |
| | | // 如果 deviceConfig 是 Map 类型(JSON 反序列化后的 LinkedHashMap),需要转换为 DeviceConfig |
| | | if (deviceConfigObj instanceof Map) { |
| | | deviceConfig = objectMapper.convertValue(deviceConfigObj, DeviceConfig.class); |
| | | } else if (deviceConfigObj instanceof DeviceConfig) { |
| | | deviceConfig = (DeviceConfig) deviceConfigObj; |
| | | } else { |
| | | log.error("不支持的 deviceConfig 类型: {}", deviceConfigObj != null ? deviceConfigObj.getClass() : "null"); |
| | | return Result.error("设备配置数据格式错误"); |
| | | } |
| | | |
| | | deviceConfig.setId(request.getDeviceId()); |
| | | boolean success = deviceConfigService.updateDevice(deviceConfig); |
| | | if (success) { |
| | |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("更新设备配置失败", e); |
| | | return Result.error("更新设备配置失败"); |
| | | return Result.error("更新设备配置失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | * 删除设备配置 |
| | | */ |
| | | @PostMapping("/devices/delete") |
| | | @Operation(summary = "删除设备配置", description = "删除指定ID的设备配置") |
| | | @ApiOperation("删除设备配置") |
| | | public Result<Void> deleteDevice( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | |
| | | * 根据ID获取设备配置 |
| | | */ |
| | | @PostMapping("/devices/detail") |
| | | @Operation(summary = "获取设备配置详情", description = "根据ID获取设备配置的详细信息") |
| | | @ApiOperation("获取设备配置详情") |
| | | public Result<DeviceConfig> getDeviceById( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | |
| | | * 分页查询设备配置列表 |
| | | */ |
| | | @PostMapping("/devices/list") |
| | | @Operation(summary = "分页查询设备配置", description = "分页查询设备配置列表") |
| | | @ApiOperation("分页查询设备配置") |
| | | public Result<Page<DeviceConfigVO.DeviceInfo>> getDeviceList( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | |
| | | * 启用设备 |
| | | */ |
| | | @PostMapping("/devices/enable") |
| | | @Operation(summary = "启用设备", description = "启用指定ID的设备") |
| | | @ApiOperation("启用设备") |
| | | public Result<Void> enableDevice( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | |
| | | * 禁用设备 |
| | | */ |
| | | @PostMapping("/devices/disable") |
| | | @Operation(summary = "禁用设备", description = "禁用指定ID的设备") |
| | | @ApiOperation("禁用设备") |
| | | public Result<Void> disableDevice( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | |
| | | * 批量启用设备 |
| | | */ |
| | | @PostMapping("/devices/batch-enable") |
| | | @Operation(summary = "批量启用设备", description = "批量启用指定ID列表的设备") |
| | | @ApiOperation("批量启用设备") |
| | | public Result<Void> batchEnableDevices( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | |
| | | * 批量禁用设备 |
| | | */ |
| | | @PostMapping("/devices/batch-disable") |
| | | @Operation(summary = "批量禁用设备", description = "批量禁用指定ID列表的设备") |
| | | @ApiOperation("批量禁用设备") |
| | | public Result<Void> batchDisableDevices( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | |
| | | * 获取设备统计信息 |
| | | */ |
| | | @PostMapping("/statistics/devices") |
| | | @Operation(summary = "获取设备统计信息", description = "获取设备相关的统计信息") |
| | | @ApiOperation("获取设备统计信息") |
| | | public Result<StatisticsVO.DeviceStatistics> getDeviceStatistics( |
| | | @Parameter(description = "设备配置请求") @RequestBody(required = false) DeviceConfigRequest request) { |
| | | @ApiParam("设备配置请求") @RequestBody(required = false) DeviceConfigRequest request) { |
| | | try { |
| | | StatisticsVO.DeviceStatistics statistics = deviceConfigService.getDeviceStatistics(request != null ? request.getProjectId() : null); |
| | | return Result.success(statistics); |
| | |
| | | * 检查设备编码是否已存在 |
| | | */ |
| | | @PostMapping("/devices/check-code") |
| | | @Operation(summary = "检查设备编码", description = "检查设备编码是否已存在") |
| | | @ApiOperation("检查设备编码") |
| | | public Result<Boolean> checkDeviceCodeExists( |
| | | @Parameter(description = "设备配置请求") @RequestBody DeviceConfigRequest request) { |
| | | @ApiParam("设备配置请求") @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | | boolean exists = deviceConfigService.isDeviceCodeExists(request.getDeviceCode(), request.getDeviceId()); |
| | | return Result.success(exists); |
| | |
| | | * 获取设备类型列表 |
| | | */ |
| | | @PostMapping("/devices/types") |
| | | @Operation(summary = "获取设备类型列表", description = "获取所有可用的设备类型") |
| | | @ApiOperation("获取设备类型列表") |
| | | public Result<List<String>> getDeviceTypes(@RequestBody(required = false) Map<String, Object> request) { |
| | | try { |
| | | List<String> deviceTypes = deviceConfigService.getAllDeviceTypes(); |
| | |
| | | * 获取设备状态列表 |
| | | */ |
| | | @PostMapping("/devices/statuses") |
| | | @Operation(summary = "获取设备状态列表", description = "获取所有可用的设备状态") |
| | | @ApiOperation("获取设备状态列表") |
| | | public Result<List<String>> getDeviceStatuses(@RequestBody(required = false) Map<String, Object> request) { |
| | | try { |
| | | List<String> deviceStatuses = deviceConfigService.getAllDeviceStatuses(); |
| | |
| | | * 获取设备配置树结构 |
| | | */ |
| | | @PostMapping("/devices/tree") |
| | | @Operation(summary = "获取设备配置树结构", description = "获取设备和设备组的树形结构数据") |
| | | @ApiOperation("获取设备配置树结构") |
| | | public Result<List<DeviceConfigVO.DeviceTreeNode>> getDeviceTree( |
| | | @Parameter(description = "设备配置请求") @RequestBody(required = false) DeviceConfigRequest request) { |
| | | @ApiParam("设备配置请求") @RequestBody(required = false) DeviceConfigRequest request) { |
| | | try { |
| | | List<DeviceConfigVO.DeviceTreeNode> treeData = deviceConfigService.getDeviceTree(request != null ? request.getProjectId() : null); |
| | | return Result.success(treeData); |
| | |
| | | * 设备健康检查 |
| | | */ |
| | | @PostMapping("/devices/health-check") |
| | | @Operation(summary = "设备健康检查", description = "对指定设备进行健康检查") |
| | | @ApiOperation("设备健康检查") |
| | | public Result<DeviceConfigVO.HealthCheckResult> performHealthCheck( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | |
| | | * 2. 直接传入 plcIp / plcPort / timeout 进行一次性测试 |
| | | */ |
| | | @PostMapping("/devices/test-connection") |
| | | @Operation(summary = "测试设备PLC连接", description = "根据设备配置测试PLC连接是否可达") |
| | | @ApiOperation("测试设备PLC连接") |
| | | public Result<String> testDeviceConnection(@RequestBody Map<String, Object> body) { |
| | | try { |
| | | String plcIp = null; |