| | |
| | | return Result.error("设备配置数据格式错误"); |
| | | } |
| | | |
| | | deviceConfig.setId(request.getDeviceId()); |
| | | deviceConfig.setId(request.getId()); |
| | | boolean success = deviceConfigService.updateDevice(deviceConfig); |
| | | if (success) { |
| | | // 更新成功后,重新获取设备对象 |
| | | DeviceConfig updated = deviceConfigService.getDeviceById(request.getDeviceId()); |
| | | DeviceConfig updated = deviceConfigService.getDeviceById(request.getId()); |
| | | return Result.success(updated); |
| | | } else { |
| | | return Result.error("设备配置不存在"); |
| | |
| | | public Result<Void> deleteDevice( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | | deviceConfigService.deleteDevice(request.getDeviceId()); |
| | | deviceConfigService.deleteDevice(request.getId()); |
| | | return Result.success(null); |
| | | } catch (Exception e) { |
| | | log.error("删除设备配置失败", e); |
| | |
| | | public Result<DeviceConfig> getDeviceById( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | | DeviceConfig device = deviceConfigService.getDeviceById(request.getDeviceId()); |
| | | DeviceConfig device = deviceConfigService.getDeviceById(request.getId()); |
| | | return Result.success(device); |
| | | } catch (Exception e) { |
| | | log.error("获取设备配置失败", e); |
| | |
| | | public Result<Void> enableDevice( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | | deviceConfigService.enableDevice(request.getDeviceId()); |
| | | deviceConfigService.enableDevice(request.getId()); |
| | | return Result.success(null); |
| | | } catch (Exception e) { |
| | | log.error("启用设备失败", e); |
| | |
| | | public Result<Void> disableDevice( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | | deviceConfigService.disableDevice(request.getDeviceId()); |
| | | deviceConfigService.disableDevice(request.getId()); |
| | | return Result.success(null); |
| | | } catch (Exception e) { |
| | | log.error("禁用设备失败", e); |
| | |
| | | public Result<Boolean> checkDeviceCodeExists( |
| | | @ApiParam("设备配置请求") @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | | boolean exists = deviceConfigService.isDeviceCodeExists(request.getDeviceCode(), request.getDeviceId()); |
| | | boolean exists = deviceConfigService.isDeviceCodeExists(request.getDeviceCode(), request.getId()); |
| | | return Result.success(exists); |
| | | } catch (Exception e) { |
| | | log.error("检查设备编码失败", e); |
| | |
| | | public Result<DeviceConfigVO.HealthCheckResult> performHealthCheck( |
| | | @Valid @RequestBody DeviceConfigRequest request) { |
| | | try { |
| | | DeviceConfigVO.HealthCheckResult result = deviceConfigService.performHealthCheck(request.getDeviceId()); |
| | | DeviceConfigVO.HealthCheckResult result = deviceConfigService.performHealthCheck(request.getId()); |
| | | return Result.success(result); |
| | | } catch (Exception e) { |
| | | log.error("设备健康检查失败", e); |
| | |
| | | /** |
| | | * 测试设备PLC连接 |
| | | * 支持两种方式: |
| | | * 1. 传入 deviceId,根据已保存的设备配置测试 |
| | | * 1. 传入 id,根据已保存的设备配置测试 |
| | | * 2. 直接传入 plcIp / plcPort / timeout 进行一次性测试 |
| | | */ |
| | | @PostMapping("/devices/test-connection") |
| | |
| | | Integer plcPort = null; |
| | | Integer timeoutMs = null; |
| | | |
| | | // 优先根据 deviceId 读取已保存配置 |
| | | Object deviceIdObj = body.get("deviceId"); |
| | | if (deviceIdObj != null) { |
| | | Long deviceId = deviceIdObj instanceof Number |
| | | ? ((Number) deviceIdObj).longValue() |
| | | : Long.parseLong(deviceIdObj.toString()); |
| | | DeviceConfig device = deviceConfigService.getDeviceById(deviceId); |
| | | // 优先根据 id 读取已保存配置 |
| | | Object idObj = body.get("id"); |
| | | if (idObj != null) { |
| | | Long id = idObj instanceof Number |
| | | ? ((Number) idObj).longValue() |
| | | : Long.parseLong(idObj.toString()); |
| | | DeviceConfig device = deviceConfigService.getDeviceById(id); |
| | | if (device == null) { |
| | | return Result.error("设备不存在: " + deviceId); |
| | | return Result.error("设备不存在: " + id); |
| | | } |
| | | plcIp = device.getPlcIp(); |
| | | plcPort = device.getPlcPort(); |