| | |
| | | import com.zy.acs.conveyor.core.properties.SlaveProperties; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.text.MessageFormat; |
| | |
| | | @Autowired |
| | | private DevpS7Service devpS7Service; |
| | | |
| | | |
| | | private static final int WRITE_RETRY_MAX = 5; |
| | | @Value("${plc-write.retry}") |
| | | private int WRITE_RETRY; |
| | | |
| | | private static final int WRITE_RETRY_INTERVAL_MS = 200; |
| | | |
| | |
| | | |
| | | public void writeSafe(StaProtocol staProtocol) throws InterruptedException { |
| | | if (staProtocol == null) { |
| | | log.warn("写入数据为空,跳过 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | News.warn("写入数据为空,跳过 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | return; |
| | | } |
| | | |
| | | SiemensS7Net siemensS7Net = devpS7Service.get(staProtocol.getPlcId()); |
| | | if (siemensS7Net == null) { |
| | | log.error("PLC未连接,无法写入 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | News.error("PLC未连接,无法写入 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | return; |
| | | } |
| | | |
| | | List<DevpSlave> devp = slaveProperties.getDevp(); |
| | | DevpSlave devpSlave = devp.stream().filter(slave -> slave.getId().equals(staProtocol.getPlcId())).findFirst().orElse(null); |
| | | if (devpSlave == null) { |
| | | log.error("PLC未配置,无法写入 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | News.error("PLC未配置,无法写入 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | return; |
| | | } |
| | | int index = devpSlave.getStaNos().indexOf(staProtocol.getSiteId()); |
| | | if (index < 0) { |
| | | log.error("站点编号不存在于配置中 [id:{}] [siteId:{}]", staProtocol.getPlcId(), staProtocol.getSiteId()); |
| | | News.error("站点编号不存在于配置中 [id:{}] [siteId:{}]", staProtocol.getPlcId(), staProtocol.getSiteId()); |
| | | return; |
| | | } |
| | | SafeSignal safeSignal = staProtocol.getSafeSignal(); |
| | |
| | | int writeCount = 0; |
| | | boolean success = false; |
| | | |
| | | while (writeCount < WRITE_RETRY_MAX) { |
| | | while (writeCount <= WRITE_RETRY) { |
| | | OperateResult writeResult = siemensS7Net.Write(flagAddr, array); |
| | | |
| | | if (writeResult.IsSuccess) { |
| | | log.info("写入安全信号输送线命令成功 [id:{}] [siteId:{}] [workNo:{}] [destSta:{}] [retry:{}]", |
| | | News.info("写入安全信号输送线命令成功 [id:{}] [siteId:{}] [workNo:{}] [destSta:{}] [retry:{}]", |
| | | staProtocol.getPlcId(), staProtocol.getSiteId(), staProtocol.getWorkNo(), |
| | | staProtocol.getStaNo(), writeCount); |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入安全信号输送线命令成功。输送线plc编号={1},站点数据={2}", |
| | |
| | | } |
| | | |
| | | writeCount++; |
| | | log.error("写入安全信号输送线命令失败 [id:{}] [siteId:{}] [retry:{}] [workErr:{}] ", |
| | | News.error("写入安全信号输送线命令失败 [id:{}] [siteId:{}] [retry:{}] [workErr:{}] ", |
| | | staProtocol.getPlcId(), staProtocol.getSiteId(), writeCount, |
| | | writeResult.Message); |
| | | |
| | | if (writeCount <= WRITE_RETRY) { |
| | | Thread.sleep(WRITE_RETRY_INTERVAL_MS); |
| | | } |
| | | } |
| | | |
| | | if (!success) { |
| | |
| | | |
| | | public void write(StaProtocol staProtocol) throws InterruptedException { |
| | | if (staProtocol == null) { |
| | | log.warn("写入数据为空,跳过 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | News.warn("写入数据为空,跳过 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | return; |
| | | } |
| | | |
| | | SiemensS7Net siemensS7Net = devpS7Service.get(staProtocol.getPlcId()); |
| | | if (siemensS7Net == null) { |
| | | log.error("PLC未连接,无法写入 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | News.error("PLC未连接,无法写入 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | return; |
| | | } |
| | | |
| | | List<DevpSlave> devp = slaveProperties.getDevp(); |
| | | DevpSlave devpSlave = devp.stream().filter(slave -> slave.getId().equals(staProtocol.getPlcId())).findFirst().orElse(null); |
| | | if (devpSlave == null) { |
| | | log.error("PLC未配置,无法写入 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | News.error("PLC未配置,无法写入 [id:{}]", JSON.toJSONString(staProtocol)); |
| | | return; |
| | | } |
| | | int index = devpSlave.getStaNos().indexOf(staProtocol.getSiteId()); |
| | | if (index < 0) { |
| | | log.error("站点编号不存在于配置中 [id:{}] [siteId:{}]", staProtocol.getPlcId(), staProtocol.getSiteId()); |
| | | News.error("站点编号不存在于配置中 [id:{}] [siteId:{}]", staProtocol.getPlcId(), staProtocol.getSiteId()); |
| | | return; |
| | | } |
| | | |
| | |
| | | int writeCount = 0; |
| | | boolean success = false; |
| | | |
| | | while (writeCount < WRITE_RETRY_MAX) { |
| | | while (writeCount <= WRITE_RETRY) { |
| | | OperateResult writeResult = siemensS7Net.Write(workNoAddr, staProtocol.getWorkNo()); |
| | | Thread.sleep(WRITE_RETRY_INTERVAL_MS); |
| | | |
| | | OperateResult writeResult1 = siemensS7Net.Write(destStaAddr, staProtocol.getStaNo().shortValue()); |
| | | |
| | | if (writeResult.IsSuccess && writeResult1.IsSuccess) { |
| | | log.info("写入输送线命令成功 [id:{}] [siteId:{}] [workNo:{}] [destSta:{}] [retry:{}]", |
| | | News.info("写入输送线命令成功 [id:{}] [siteId:{}] [workNo:{}] [destSta:{}] [retry:{}]", |
| | | staProtocol.getPlcId(), staProtocol.getSiteId(), staProtocol.getWorkNo(), |
| | | staProtocol.getStaNo(), writeCount); |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线命令成功。输送线plc编号={1},站点数据={2}", |
| | |
| | | } |
| | | |
| | | writeCount++; |
| | | log.error("写入输送线命令失败 [id:{}] [siteId:{}] [retry:{}] [workErr:{}] [destErr:{}]", |
| | | News.error("写入输送线命令失败 [id:{}] [siteId:{}] [retry:{}] [workErr:{}] [destErr:{}]", |
| | | staProtocol.getPlcId(), staProtocol.getSiteId(), writeCount, |
| | | writeResult.Message, writeResult1.Message); |
| | | |
| | | if (writeCount < WRITE_RETRY_MAX) { |
| | | if (writeCount <= WRITE_RETRY) { |
| | | Thread.sleep(WRITE_RETRY_INTERVAL_MS); |
| | | } |
| | | } |
| | |
| | | OutputQueue.DEVP.offer(errorMsg); |
| | | News.error("SiemensDevp - 4 - 写入输送线站点数据失败。输送线plc编号={},站点数据={}", |
| | | staProtocol.getPlcId(), JSON.toJSON(staProtocol)); |
| | | log.error(errorMsg); |
| | | } |
| | | |
| | | } |