| | |
| | | import com.mes.device.service.DeviceConfigService; |
| | | import com.mes.device.service.DeviceGroupRelationService; |
| | | import com.mes.device.service.DevicePlcOperationService; |
| | | import com.mes.device.util.ConfigJsonHelper; |
| | | import com.mes.device.vo.DeviceGroupVO; |
| | | import com.mes.device.vo.DevicePlcVO; |
| | | import com.mes.service.PlcTestWriteService; |
| | |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | private final DeviceGroupRelationService deviceGroupRelationService; |
| | | private final PlcTestWriteService plcTestWriteService; |
| | | private final ObjectMapper objectMapper; |
| | | |
| | | public static enum PlcOperationType { |
| | | /** PLC请求操作 */ |
| | | REQUEST("PLC请求", "PLC 请求发送成功", "PLC 请求发送失败"), |
| | | /** PLC汇报操作 */ |
| | | REPORT("PLC汇报", "PLC 汇报模拟成功", "PLC 汇报模拟失败"), |
| | | /** PLC重置操作 */ |
| | | RESET("PLC重置", "PLC 状态已重置", "PLC 状态重置失败"); |
| | | |
| | | /** 操作显示名称 */ |
| | | private final String display; |
| | | /** 操作成功提示信息 */ |
| | | private final String successMsg; |
| | | /** 操作失败提示信息 */ |
| | | private final String failedMsg; |
| | | |
| | | /** |
| | | * 构造方法 |
| | | * @param display 操作显示名称 |
| | | * @param successMsg 成功提示信息 |
| | | * @param failedMsg 失败提示信息 |
| | | */ |
| | | PlcOperationType(String display, String successMsg, String failedMsg) { |
| | | this.display = display; |
| | | this.successMsg = successMsg; |
| | | this.failedMsg = failedMsg; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public DevicePlcVO.OperationResult triggerRequest(Long deviceId) { |
| | |
| | | return DevicePlcVO.StatusInfo.builder() |
| | | .deviceId(deviceId) |
| | | .deviceName("未知设备") |
| | | .data(Collections.emptyMap()) |
| | | .fieldValues(Collections.emptyMap()) |
| | | .timestamp(LocalDateTime.now()) |
| | | .build(); |
| | | } |
| | |
| | | .deviceName(device.getDeviceName()) |
| | | .deviceCode(device.getDeviceCode()) |
| | | .projectId(String.valueOf(device.getProjectId())) |
| | | .data(data) |
| | | .fieldValues(data) |
| | | .timestamp(LocalDateTime.now()) |
| | | .build(); |
| | | } catch (Exception e) { |
| | |
| | | .deviceName(device.getDeviceName()) |
| | | .deviceCode(device.getDeviceCode()) |
| | | .projectId(null) |
| | | .data(Collections.emptyMap()) |
| | | .fieldValues(Collections.emptyMap()) |
| | | .timestamp(LocalDateTime.now()) |
| | | .build(); |
| | | } |
| | |
| | | throw new IllegalArgumentException("设备信息为空"); |
| | | } |
| | | |
| | | // 优先从configJson中获取 |
| | | Map<String, Object> configParams = ConfigJsonHelper.parseToMap(device.getConfigJson(), objectMapper); |
| | | Object plcProjectId = configParams.get(PLC_PROJECT_ID_KEY); |
| | | if (plcProjectId != null) { |
| | | return String.valueOf(plcProjectId); |
| | | } |
| | | |
| | | // 其次从扩展参数中获取(兼容旧配置) |
| | | String extra = device.getExtraParams(); |
| | | if (extra != null && !extra.isEmpty()) { |
| | | try { |
| | | Map<String, Object> extraParams = objectMapper.readValue(extra, new TypeReference<Map<String, Object>>() {}); |
| | | Object plcProjectId = extraParams.get(PLC_PROJECT_ID_KEY); |
| | | if (plcProjectId != null) { |
| | | return String.valueOf(plcProjectId); |
| | | Object plcProjectIdFromExtra = extraParams.get(PLC_PROJECT_ID_KEY); |
| | | if (plcProjectIdFromExtra != null) { |
| | | return String.valueOf(plcProjectIdFromExtra); |
| | | } |
| | | } catch (Exception e) { |
| | | log.warn("解析设备扩展参数失败, deviceId={}", device.getId(), e); |
| | |
| | | throw new IllegalStateException("无法解析设备的 PLC 项目标识, deviceId=" + device.getId()); |
| | | } |
| | | |
| | | private enum PlcOperationType { |
| | | REQUEST("PLC请求", "PLC 请求发送成功", "PLC 请求发送失败"), |
| | | REPORT("PLC汇报", "PLC 汇报模拟成功", "PLC 汇报模拟失败"), |
| | | RESET("PLC重置", "PLC 状态已重置", "PLC 状态重置失败"); |
| | | |
| | | private final String display; |
| | | private final String successMsg; |
| | | private final String failedMsg; |
| | | |
| | | PlcOperationType(String display, String successMsg, String failedMsg) { |
| | | this.display = display; |
| | | this.successMsg = successMsg; |
| | | this.failedMsg = failedMsg; |
| | | } |
| | | } |
| | | } |
| | | |