From e76f0739e647fe8a7e0e2618914e2faff554b1b7 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期一, 17 十一月 2025 17:33:23 +0800
Subject: [PATCH] 解决冲突
---
mes-processes/mes-plcSend/src/main/java/com/mes/device/controller/DeviceConfigController.java | 385 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 385 insertions(+), 0 deletions(-)
diff --git a/mes-processes/mes-plcSend/src/main/java/com/mes/device/controller/DeviceConfigController.java b/mes-processes/mes-plcSend/src/main/java/com/mes/device/controller/DeviceConfigController.java
new file mode 100644
index 0000000..7a57917
--- /dev/null
+++ b/mes-processes/mes-plcSend/src/main/java/com/mes/device/controller/DeviceConfigController.java
@@ -0,0 +1,385 @@
+package com.mes.device.controller;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+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 lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 璁惧閰嶇疆绠$悊鎺у埗鍣�
+ *
+ * @author mes
+ * @since 2024-10-30
+ */
+@Slf4j
+@RestController
+@RequestMapping("device/config")
+@Tag(name = "璁惧閰嶇疆绠$悊", description = "璁惧閰嶇疆绠$悊鐩稿叧鎺ュ彛")
+public class DeviceConfigController {
+
+ @Autowired
+ private DeviceConfigService deviceConfigService;
+
+ /**
+ * 鍒涘缓璁惧閰嶇疆
+ */
+ @PostMapping("/devices")
+ @Operation(summary = "鍒涘缓璁惧閰嶇疆", description = "鍒涘缓鏂扮殑璁惧閰嶇疆")
+ public Result<DeviceConfig> createDevice(
+ @Valid @RequestBody DeviceConfig deviceConfig) {
+ try {
+ boolean success = deviceConfigService.createDevice(deviceConfig);
+ if (success) {
+ // 鍒涘缓鎴愬姛鍚庯紝閲嶆柊鑾峰彇璁惧瀵硅薄
+ DeviceConfig created = deviceConfigService.getDeviceByCode(deviceConfig.getDeviceCode());
+ return Result.success(created);
+ } else {
+ return Result.error("璁惧閰嶇疆宸插瓨鍦�");
+ }
+ } catch (Exception e) {
+ log.error("鍒涘缓璁惧閰嶇疆澶辫触", e);
+ return Result.error("鍒涘缓璁惧閰嶇疆澶辫触");
+ }
+ }
+
+ /**
+ * 鏇存柊璁惧閰嶇疆
+ */
+ @PostMapping("/devices/update")
+ @Operation(summary = "鏇存柊璁惧閰嶇疆", description = "鏇存柊鎸囧畾ID鐨勮澶囬厤缃�")
+ public Result<DeviceConfig> updateDevice(
+ @Valid @RequestBody DeviceConfigRequest request) {
+ try {
+ DeviceConfig deviceConfig = (DeviceConfig) request.getDeviceConfig();
+ deviceConfig.setId(request.getDeviceId());
+ boolean success = deviceConfigService.updateDevice(deviceConfig);
+ if (success) {
+ // 鏇存柊鎴愬姛鍚庯紝閲嶆柊鑾峰彇璁惧瀵硅薄
+ DeviceConfig updated = deviceConfigService.getDeviceById(request.getDeviceId());
+ return Result.success(updated);
+ } else {
+ return Result.error("璁惧閰嶇疆涓嶅瓨鍦�");
+ }
+ } catch (Exception e) {
+ log.error("鏇存柊璁惧閰嶇疆澶辫触", e);
+ return Result.error("鏇存柊璁惧閰嶇疆澶辫触");
+ }
+ }
+
+ /**
+ * 鍒犻櫎璁惧閰嶇疆
+ */
+ @PostMapping("/devices/delete")
+ @Operation(summary = "鍒犻櫎璁惧閰嶇疆", description = "鍒犻櫎鎸囧畾ID鐨勮澶囬厤缃�")
+ public Result<Void> deleteDevice(
+ @Valid @RequestBody DeviceConfigRequest request) {
+ try {
+ deviceConfigService.deleteDevice(request.getDeviceId());
+ return Result.success(null);
+ } catch (Exception e) {
+ log.error("鍒犻櫎璁惧閰嶇疆澶辫触", e);
+ return Result.error("鍒犻櫎璁惧閰嶇疆澶辫触");
+ }
+ }
+
+ /**
+ * 鏍规嵁ID鑾峰彇璁惧閰嶇疆
+ */
+ @PostMapping("/devices/detail")
+ @Operation(summary = "鑾峰彇璁惧閰嶇疆璇︽儏", description = "鏍规嵁ID鑾峰彇璁惧閰嶇疆鐨勮缁嗕俊鎭�")
+ public Result<DeviceConfig> getDeviceById(
+ @Valid @RequestBody DeviceConfigRequest request) {
+ try {
+ DeviceConfig device = deviceConfigService.getDeviceById(request.getDeviceId());
+ return Result.success(device);
+ } catch (Exception e) {
+ log.error("鑾峰彇璁惧閰嶇疆澶辫触", e);
+ return Result.error("鑾峰彇璁惧閰嶇疆澶辫触");
+ }
+ }
+
+ /**
+ * 鍒嗛〉鏌ヨ璁惧閰嶇疆鍒楄〃
+ */
+ @PostMapping("/devices/list")
+ @Operation(summary = "鍒嗛〉鏌ヨ璁惧閰嶇疆", description = "鍒嗛〉鏌ヨ璁惧閰嶇疆鍒楄〃")
+ public Result<Page<DeviceConfigVO.DeviceInfo>> getDeviceList(
+ @Valid @RequestBody DeviceConfigRequest request) {
+ try {
+ Page<DeviceConfigVO.DeviceInfo> pageResult = deviceConfigService.getDeviceList(
+ request.getProjectId(),
+ request.getDeviceType(),
+ request.getDeviceStatus(),
+ request.getKeyword(),
+ request.getPage() != null ? request.getPage() : 1,
+ request.getSize() != null ? request.getSize() : 10);
+ return Result.success(pageResult);
+ } catch (Exception e) {
+ log.error("鏌ヨ璁惧閰嶇疆鍒楄〃澶辫触", e);
+ return Result.error("鏌ヨ璁惧閰嶇疆鍒楄〃澶辫触");
+ }
+ }
+
+ /**
+ * 鍚敤璁惧
+ */
+ @PostMapping("/devices/enable")
+ @Operation(summary = "鍚敤璁惧", description = "鍚敤鎸囧畾ID鐨勮澶�")
+ public Result<Void> enableDevice(
+ @Valid @RequestBody DeviceConfigRequest request) {
+ try {
+ deviceConfigService.enableDevice(request.getDeviceId());
+ return Result.success(null);
+ } catch (Exception e) {
+ log.error("鍚敤璁惧澶辫触", e);
+ return Result.error("鍚敤璁惧澶辫触");
+ }
+ }
+
+ /**
+ * 绂佺敤璁惧
+ */
+ @PostMapping("/devices/disable")
+ @Operation(summary = "绂佺敤璁惧", description = "绂佺敤鎸囧畾ID鐨勮澶�")
+ public Result<Void> disableDevice(
+ @Valid @RequestBody DeviceConfigRequest request) {
+ try {
+ deviceConfigService.disableDevice(request.getDeviceId());
+ return Result.success(null);
+ } catch (Exception e) {
+ log.error("绂佺敤璁惧澶辫触", e);
+ return Result.error("绂佺敤璁惧澶辫触");
+ }
+ }
+
+ /**
+ * 鎵归噺鍚敤璁惧
+ */
+ @PostMapping("/devices/batch-enable")
+ @Operation(summary = "鎵归噺鍚敤璁惧", description = "鎵归噺鍚敤鎸囧畾ID鍒楄〃鐨勮澶�")
+ public Result<Void> batchEnableDevices(
+ @Valid @RequestBody DeviceConfigRequest request) {
+ try {
+ deviceConfigService.batchEnableDevices(request.getDeviceIds());
+ return Result.success(null);
+ } catch (Exception e) {
+ log.error("鎵归噺鍚敤璁惧澶辫触", e);
+ return Result.error("鎵归噺鍚敤璁惧澶辫触");
+ }
+ }
+
+ /**
+ * 鎵归噺绂佺敤璁惧
+ */
+ @PostMapping("/devices/batch-disable")
+ @Operation(summary = "鎵归噺绂佺敤璁惧", description = "鎵归噺绂佺敤鎸囧畾ID鍒楄〃鐨勮澶�")
+ public Result<Void> batchDisableDevices(
+ @Valid @RequestBody DeviceConfigRequest request) {
+ try {
+ deviceConfigService.batchDisableDevices(request.getDeviceIds());
+ return Result.success(null);
+ } catch (Exception e) {
+ log.error("鎵归噺绂佺敤璁惧澶辫触", e);
+ return Result.error("鎵归噺绂佺敤璁惧澶辫触");
+ }
+ }
+
+ /**
+ * 鑾峰彇璁惧缁熻淇℃伅
+ */
+ @PostMapping("/statistics/devices")
+ @Operation(summary = "鑾峰彇璁惧缁熻淇℃伅", description = "鑾峰彇璁惧鐩稿叧鐨勭粺璁′俊鎭�")
+ public Result<StatisticsVO.DeviceStatistics> getDeviceStatistics(
+ @Parameter(description = "璁惧閰嶇疆璇锋眰") @RequestBody(required = false) DeviceConfigRequest request) {
+ try {
+ StatisticsVO.DeviceStatistics statistics = deviceConfigService.getDeviceStatistics(request != null ? request.getProjectId() : null);
+ return Result.success(statistics);
+ } catch (Exception e) {
+ log.error("鑾峰彇璁惧缁熻淇℃伅澶辫触", e);
+ return Result.error("鑾峰彇璁惧缁熻淇℃伅澶辫触");
+ }
+ }
+
+ /**
+ * 妫�鏌ヨ澶囩紪鐮佹槸鍚﹀凡瀛樺湪
+ */
+ @PostMapping("/devices/check-code")
+ @Operation(summary = "妫�鏌ヨ澶囩紪鐮�", description = "妫�鏌ヨ澶囩紪鐮佹槸鍚﹀凡瀛樺湪")
+ public Result<Boolean> checkDeviceCodeExists(
+ @Parameter(description = "璁惧閰嶇疆璇锋眰") @RequestBody DeviceConfigRequest request) {
+ try {
+ boolean exists = deviceConfigService.isDeviceCodeExists(request.getDeviceCode(), request.getDeviceId());
+ return Result.success(exists);
+ } catch (Exception e) {
+ log.error("妫�鏌ヨ澶囩紪鐮佸け璐�", e);
+ return Result.error("妫�鏌ヨ澶囩紪鐮佸け璐�");
+ }
+ }
+
+ /**
+ * 鑾峰彇璁惧绫诲瀷鍒楄〃
+ */
+ @PostMapping("/devices/types")
+ @Operation(summary = "鑾峰彇璁惧绫诲瀷鍒楄〃", description = "鑾峰彇鎵�鏈夊彲鐢ㄧ殑璁惧绫诲瀷")
+ public Result<List<String>> getDeviceTypes(@RequestBody(required = false) Map<String, Object> request) {
+ try {
+ List<String> deviceTypes = deviceConfigService.getAllDeviceTypes();
+ return Result.success(deviceTypes);
+ } catch (Exception e) {
+ log.error("鑾峰彇璁惧绫诲瀷鍒楄〃澶辫触", e);
+ return Result.error("鑾峰彇璁惧绫诲瀷鍒楄〃澶辫触");
+ }
+ }
+
+ /**
+ * 鑾峰彇璁惧鐘舵�佸垪琛�
+ */
+ @PostMapping("/devices/statuses")
+ @Operation(summary = "鑾峰彇璁惧鐘舵�佸垪琛�", description = "鑾峰彇鎵�鏈夊彲鐢ㄧ殑璁惧鐘舵��")
+ public Result<List<String>> getDeviceStatuses(@RequestBody(required = false) Map<String, Object> request) {
+ try {
+ List<String> deviceStatuses = deviceConfigService.getAllDeviceStatuses();
+ return Result.success(deviceStatuses);
+ } catch (Exception e) {
+ log.error("鑾峰彇璁惧鐘舵�佸垪琛ㄥけ璐�", e);
+ return Result.error("鑾峰彇璁惧鐘舵�佸垪琛ㄥけ璐�");
+ }
+ }
+
+ /**
+ * 鑾峰彇璁惧閰嶇疆鏍戠粨鏋�
+ */
+ @PostMapping("/devices/tree")
+ @Operation(summary = "鑾峰彇璁惧閰嶇疆鏍戠粨鏋�", description = "鑾峰彇璁惧鍜岃澶囩粍鐨勬爲褰㈢粨鏋勬暟鎹�")
+ public Result<List<DeviceConfigVO.DeviceTreeNode>> getDeviceTree(
+ @Parameter(description = "璁惧閰嶇疆璇锋眰") @RequestBody(required = false) DeviceConfigRequest request) {
+ try {
+ List<DeviceConfigVO.DeviceTreeNode> treeData = deviceConfigService.getDeviceTree(request != null ? request.getProjectId() : null);
+ return Result.success(treeData);
+ } catch (Exception e) {
+ log.error("鑾峰彇璁惧閰嶇疆鏍戠粨鏋勫け璐�", e);
+ return Result.error("鑾峰彇璁惧閰嶇疆鏍戠粨鏋勫け璐�");
+ }
+ }
+
+ /**
+ * 璁惧鍋ュ悍妫�鏌�
+ */
+ @PostMapping("/devices/health-check")
+ @Operation(summary = "璁惧鍋ュ悍妫�鏌�", description = "瀵规寚瀹氳澶囪繘琛屽仴搴锋鏌�")
+ public Result<DeviceConfigVO.HealthCheckResult> performHealthCheck(
+ @Valid @RequestBody DeviceConfigRequest request) {
+ try {
+ DeviceConfigVO.HealthCheckResult result = deviceConfigService.performHealthCheck(request.getDeviceId());
+ return Result.success(result);
+ } catch (Exception e) {
+ log.error("璁惧鍋ュ悍妫�鏌ュけ璐�", e);
+ return Result.error("璁惧鍋ュ悍妫�鏌ュけ璐�");
+ }
+ }
+
+ /**
+ * 娴嬭瘯璁惧PLC杩炴帴
+ * 鏀寔涓ょ鏂瑰紡锛�
+ * 1. 浼犲叆 deviceId锛屾牴鎹凡淇濆瓨鐨勮澶囬厤缃祴璇�
+ * 2. 鐩存帴浼犲叆 plcIp / plcPort / timeout 杩涜涓�娆℃�ф祴璇�
+ */
+ @PostMapping("/devices/test-connection")
+ @Operation(summary = "娴嬭瘯璁惧PLC杩炴帴", description = "鏍规嵁璁惧閰嶇疆娴嬭瘯PLC杩炴帴鏄惁鍙揪")
+ public Result<String> testDeviceConnection(@RequestBody Map<String, Object> body) {
+ try {
+ String plcIp = null;
+ 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);
+ if (device == null) {
+ return Result.error("璁惧涓嶅瓨鍦�: " + deviceId);
+ }
+ plcIp = device.getPlcIp();
+ plcPort = device.getPlcPort();
+ timeoutMs = 3000;
+ } else {
+ // 鐩存帴浠庤姹備綋涓幏鍙栨祴璇曞弬鏁�
+ Object ipObj = body.get("plcIp");
+ Object portObj = body.get("plcPort");
+ Object timeoutObj = body.get("timeout");
+ if (ipObj != null) {
+ plcIp = String.valueOf(ipObj);
+ }
+ if (portObj instanceof Number) {
+ plcPort = ((Number) portObj).intValue();
+ } else if (portObj != null) {
+ plcPort = Integer.parseInt(portObj.toString());
+ }
+ if (timeoutObj instanceof Number) {
+ timeoutMs = ((Number) timeoutObj).intValue() * 1000;
+ } else if (timeoutObj != null) {
+ timeoutMs = Integer.parseInt(timeoutObj.toString()) * 1000;
+ }
+ }
+
+ if (plcIp == null || plcIp.trim().isEmpty()) {
+ return Result.error("PLC IP涓嶈兘涓虹┖");
+ }
+ if (plcPort == null || plcPort <= 0 || plcPort > 65535) {
+ plcPort = 102;
+ }
+ if (timeoutMs == null || timeoutMs <= 0) {
+ timeoutMs = 3000;
+ }
+
+ boolean ok = testTcpConnection(plcIp, plcPort, timeoutMs);
+ if (ok) {
+ String msg = String.format("杩炴帴娴嬭瘯鎴愬姛锛�%s:%d", plcIp, plcPort);
+ log.info(msg);
+ return Result.success(msg);
+ } else {
+ String msg = String.format("杩炴帴娴嬭瘯澶辫触锛�%s:%d", plcIp, plcPort);
+ log.warn(msg);
+ return Result.error(msg);
+ }
+ } catch (Exception e) {
+ log.error("璁惧PLC杩炴帴娴嬭瘯澶辫触", e);
+ return Result.error("杩炴帴娴嬭瘯寮傚父: " + e.getMessage());
+ }
+ }
+
+ private boolean testTcpConnection(String ip, int port, int timeoutMs) {
+ java.net.Socket socket = null;
+ try {
+ socket = new java.net.Socket();
+ socket.connect(new java.net.InetSocketAddress(ip, port), timeoutMs);
+ return true;
+ } catch (Exception e) {
+ log.warn("TCP杩炴帴娴嬭瘯澶辫触: {}:{}, err={}", ip, port, e.getMessage());
+ return false;
+ } finally {
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (Exception ignore) {
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.8.0