| | |
| | | @Resource |
| | | private PlcDynamicDataService plcDynamicDataService; |
| | | |
| | | @Resource |
| | | private com.mes.plc.factory.PlcClientFactory plcClientFactory; |
| | | |
| | | private final ObjectMapper objectMapper = new ObjectMapper(); |
| | | private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {}; |
| | | |
| | | private static final int ON = 1; |
| | | private static final int OFF = 0; |
| | | |
| | | // 缓存不同设备的S7Serializer实例 |
| | | // 缓存不同设备的S7Serializer实例(保持兼容,逐步迁移) |
| | | private final ConcurrentMap<String, EnhancedS7Serializer> serializerCache = new ConcurrentHashMap<>(); |
| | | |
| | | // ==================== 基于DeviceConfig的新API(推荐使用) ==================== |
| | | |
| | | /** |
| | | * 根据设备ID模拟PLC发送请求字 |
| | |
| | | return false; |
| | | } |
| | | try { |
| | | // 尝试使用新的PLC客户端工厂 |
| | | com.mes.plc.client.PlcClient plcClient = plcClientFactory.getClient(device); |
| | | if (plcClient != null) { |
| | | // 使用新的PLC客户端读取数据 |
| | | Map<String, Object> currentData = plcClient.readAllData(); |
| | | if (currentData != null && !currentData.isEmpty()) { |
| | | // 检查联机状态 |
| | | Integer onlineState = parseInteger(currentData.get("onlineState")); |
| | | if (onlineState != null && onlineState == OFF) { |
| | | log.info("当前PLC联机模式为0,停止联机: deviceId={}", deviceId); |
| | | return false; |
| | | } |
| | | |
| | | // 检查汇报字,如果为1则重置为0 |
| | | Integer plcReport = parseInteger(currentData.get("plcReport")); |
| | | if (plcReport != null && plcReport == ON) { |
| | | log.info("当前上片PLC汇报字为1,重置为0: deviceId={}", deviceId); |
| | | currentData.put("plcReport", OFF); |
| | | } |
| | | |
| | | // 设置请求字为1 |
| | | currentData.put("plcRequest", ON); |
| | | |
| | | // 使用新的PLC客户端写入数据 |
| | | boolean success = plcClient.writeData(currentData); |
| | | if (success) { |
| | | log.info("模拟PLC发送请求字成功:plcRequest=1, deviceId={}", deviceId); |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 如果新客户端失败,回退到旧实现(保持兼容) |
| | | log.warn("新PLC客户端失败,回退到旧实现: deviceId={}", deviceId); |
| | | return simulatePlcRequestByDeviceLegacy(device); |
| | | } catch (Exception e) { |
| | | log.error("根据设备模拟PLC请求字失败: deviceId={}", deviceId, e); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 旧版实现:根据设备ID模拟PLC发送请求字 |
| | | * |
| | | * @param device 设备配置 |
| | | * @return 是否成功 |
| | | */ |
| | | private boolean simulatePlcRequestByDeviceLegacy(DeviceConfig device) { |
| | | try { |
| | | EnhancedS7Serializer s7Serializer = getSerializerForDevice(device); |
| | | if (s7Serializer == null) { |
| | | log.error("获取S7Serializer失败: deviceId={}", deviceId); |
| | | log.error("获取S7Serializer失败: deviceId={}", device.getId()); |
| | | return false; |
| | | } |
| | | |
| | | // 使用PlcDynamicDataService读取数据(支持addressMapping) |
| | | Map<String, Object> currentData = plcDynamicDataService.readAllPlcData(device, s7Serializer); |
| | | if (currentData == null || currentData.isEmpty()) { |
| | | log.error("读取PLC数据失败,返回空: deviceId={}", deviceId); |
| | | log.error("读取PLC数据失败,返回空: deviceId={}", device.getId()); |
| | | return false; |
| | | } |
| | | |
| | |
| | | onlineState = Integer.parseInt(strValue); |
| | | } |
| | | } catch (NumberFormatException e) { |
| | | log.warn("解析onlineState失败: deviceId={}, value={}", deviceId, onlineStateObj, e); |
| | | log.warn("解析onlineState失败: deviceId={}, value={}", device.getId(), onlineStateObj, e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (onlineState != null && onlineState == OFF) { |
| | | log.info("当前PLC联机模式为0,停止联机: deviceId={}", deviceId); |
| | | log.info("当前PLC联机模式为0,停止联机: deviceId={}", device.getId()); |
| | | return false; |
| | | } |
| | | |
| | |
| | | plcReport = Integer.parseInt(strValue); |
| | | } |
| | | } catch (NumberFormatException e) { |
| | | log.warn("解析plcReport失败: deviceId={}, value={}", deviceId, plcReportObj, e); |
| | | log.warn("解析plcReport失败: deviceId={}, value={}", device.getId(), plcReportObj, e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (plcReport != null && plcReport == ON) { |
| | | log.info("当前上片PLC汇报字为1,重置为0: deviceId={}", deviceId); |
| | | log.info("当前上片PLC汇报字为1,重置为0: deviceId={}", device.getId()); |
| | | currentData.put("plcReport", OFF); |
| | | } |
| | | |
| | |
| | | // 使用PlcDynamicDataService写入数据 |
| | | plcDynamicDataService.writePlcData(device, currentData, s7Serializer); |
| | | |
| | | log.info("模拟PLC发送请求字成功:plcRequest=1, deviceId={}", deviceId); |
| | | log.info("模拟PLC发送请求字成功(旧版):plcRequest=1, deviceId={}", device.getId()); |
| | | return true; |
| | | } catch (Exception e) { |
| | | log.error("根据设备模拟PLC请求字失败: deviceId={}", deviceId, e); |
| | | log.error("根据设备模拟PLC请求字失败(旧版): deviceId={}", device.getId(), e); |
| | | return false; |
| | | } |
| | | } |
| | |
| | | EnhancedS7Serializer serializer = EnhancedS7Serializer.newInstance(s7Plc); |
| | | if (serializer == null) { |
| | | log.error("创建EnhancedS7Serializer失败: deviceId={}, plcIp={}, plcType={}", |
| | | device.getId(), plcIp, plcType); |
| | | device.getId(), plcIp, plcType.name()); |
| | | } |
| | | return serializer; |
| | | } catch (Exception e) { |