| | |
| | | package com.zy.acs.conveyor.core.thread; |
| | | |
| | | import HslCommunication.Core.Types.OperateResult; |
| | | import HslCommunication.Core.Types.OperateResultExOne; |
| | | import HslCommunication.Profinet.Siemens.SiemensPLCS; |
| | | import HslCommunication.Profinet.Siemens.SiemensS7Net; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.zy.acs.common.utils.News; |
| | | import com.zy.acs.conveyor.core.DevpThread; |
| | | import com.zy.acs.conveyor.core.cache.MessageQueue; |
| | | import com.zy.acs.conveyor.core.cache.OutputQueue; |
| | | import com.zy.acs.conveyor.core.cache.SlaveConnection; |
| | | import com.zy.acs.conveyor.core.enums.SlaveType; |
| | | import com.zy.acs.conveyor.core.enums.TaskType; |
| | | import com.zy.acs.conveyor.core.model.Task; |
| | | import com.zy.acs.conveyor.core.model.protocol.StaProtocol; |
| | | import com.zy.acs.conveyor.core.constant.DeviceField; |
| | | import com.zy.acs.conveyor.core.constant.PlcAlarmDefinition; |
| | | import com.zy.acs.conveyor.core.constant.StationStatusField; |
| | | import com.zy.acs.conveyor.core.model.StaProtocol; |
| | | import com.zy.acs.conveyor.core.properties.DevpSlave; |
| | | import com.zy.acs.conveyor.core.service.DevpS7Service; |
| | | import com.zy.acs.conveyor.core.service.StationService; |
| | | import com.zy.acs.conveyor.entity.Devp; |
| | | import com.zy.acs.conveyor.service.DevpService; |
| | | import com.zy.acs.conveyor.utils.SpringContextUtil; |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.framework.common.DateUtils; |
| | | import com.zy.acs.framework.exception.CoolException; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.text.MessageFormat; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | /** |
| | | * 输送线线程 |
| | |
| | | */ |
| | | @Data |
| | | @Slf4j |
| | | public class SiemensDevpThread implements Runnable, DevpThread { |
| | | public class SiemensDevpThread implements Runnable { |
| | | |
| | | private DevpSlave slave; |
| | | |
| | | private SiemensS7Net siemensS7Net; |
| | | private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>(); |
| | | private short heartBeatVal = 1; |
| | | |
| | | public static final ArrayList<Integer> staNos1 = new ArrayList<Integer>() {{ |
| | | add(1001); |
| | | add(1002); |
| | | add(1003); |
| | | add(1004); |
| | | add(1005); |
| | | add(1006); |
| | | add(1007); |
| | | private Map<Integer, StaProtocol> station; |
| | | |
| | | }}; |
| | | private static final int WRITE_RETRY_MAX = 5; |
| | | |
| | | private static final int WRITE_RETRY_INTERVAL_MS = 200; |
| | | |
| | | private static final int READ_INTERVAL_MS = 100; |
| | | |
| | | private static final int DB_UPDATE_INTERVAL_MS = 2000; // 数据库更新间隔 |
| | | |
| | | private long lastDbUpdateTime = 0; |
| | | |
| | | |
| | | public static final ArrayList<Integer> BarcodeList = new ArrayList<Integer>() {{ |
| | | add(1); |
| | | }}; |
| | | |
| | | |
| | | /** |
| | | * 条码数量 |
| | | */ |
| | | private int barcodeSize = 1; |
| | | |
| | | |
| | | public SiemensDevpThread(DevpSlave slave) { |
| | | public SiemensDevpThread(DevpSlave slave, SiemensS7Net siemensS7Net, Map<Integer, StaProtocol> station) { |
| | | this.slave = slave; |
| | | this.siemensS7Net = siemensS7Net; |
| | | this.station = station; |
| | | } |
| | | |
| | | private ArrayList<Integer> getStaNo() { |
| | | switch (slave.getId()) { |
| | | case 1: |
| | | return staNos1; |
| | | default: |
| | | throw new CoolException("服务器异常"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @SuppressWarnings("InfiniteLoopStatement") |
| | | public void run() { |
| | | connect(); |
| | | while (true) { |
| | | while (!Thread.currentThread().isInterrupted()) { |
| | | try { |
| | | TaskType step = TaskType.READ; |
| | | Task task = MessageQueue.poll(SlaveType.Devp, slave.getId()); |
| | | if (task != null) { |
| | | step = task.getStep(); |
| | | } |
| | | switch (step) { |
| | | // 读数据 |
| | | case READ: |
| | | read(); |
| | | break; |
| | | // 写数据 ID+目标站 |
| | | case WRITE: |
| | | write((StaProtocol) task.getData()); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | // 心跳 |
| | | // heartbeat(); |
| | | Thread.sleep(100); |
| | | read(); |
| | | Thread.sleep(READ_INTERVAL_MS); |
| | | } catch (InterruptedException e) { |
| | | Thread.currentThread().interrupt(); |
| | | log.warn("SiemensDevp线程被中断 [id:{}]", slave.getId()); |
| | | break; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 初始化站点状态 |
| | | */ |
| | | private void initSite() { |
| | | ArrayList<Integer> staNos = getStaNo(); |
| | | // 站点编号 |
| | | for (Integer siteId : staNos) { |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | if (null == staProtocol) { |
| | | staProtocol = new StaProtocol(); |
| | | staProtocol.setSiteId(siteId); |
| | | station.put(siteId, staProtocol); |
| | | } |
| | | staProtocol.setWorkNo(0); // ID |
| | | staProtocol.setAutoing(false); // 自动 |
| | | staProtocol.setLoading(false); // 有物 |
| | | staProtocol.setInEnable(false); // 可入 |
| | | staProtocol.setOutEnable(false); // 可出 |
| | | staProtocol.setEmptyMk(false); // 空板信号 |
| | | staProtocol.setStaNo(0); // 目标站 |
| | | |
| | | if (!staProtocol.isPakMk() && !staProtocol.isLoading()) { |
| | | staProtocol.setPakMk(true); |
| | | log.error("SiemensDevp线程运行异常 [id:{}]", slave.getId(), e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | boolean result = false; |
| | | siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp()); |
| | | siemensS7Net.setRack(slave.getRack().byteValue()); |
| | | siemensS7Net.setSlot(slave.getSlot().byteValue()); |
| | | OperateResult connect = siemensS7Net.ConnectServer(); |
| | | if (connect.IsSuccess) { |
| | | result = true; |
| | | //OutputQueue.DEVP.offer(MessageFormat.format("【{0}】输送线plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot())); |
| | | News.info("SiemensDevp" + " - 1" + " - 输送线plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | } else { |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】输送线plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot())); |
| | | News.error("SiemensDevp" + " - 2" + " - 输送线plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | } |
| | | siemensS7Net.ConnectClose(); |
| | | initSite(); |
| | | return result; |
| | | log.info("SiemensDevp读线程已退出 [id:{}]", slave.getId()); |
| | | } |
| | | |
| | | /** |
| | | * 读取状态 ====> 整块plc |
| | | */ |
| | | private void read() throws InterruptedException { |
| | | ArrayList<Integer> staNos = getStaNo(); |
| | | if (siemensS7Net == null) { |
| | | DevpS7Service devpS7Service = SpringContextUtil.getBean(DevpS7Service.class); |
| | | if (devpS7Service != null) { |
| | | siemensS7Net = devpS7Service.get(slave.getId()); |
| | | } |
| | | log.warn("PLC未连接,跳过读取 [id:{}]", slave.getId()); |
| | | return; |
| | | } |
| | | if (station == null) { |
| | | StationService stationService = SpringContextUtil.getBean(StationService.class); |
| | | if (stationService != null) { |
| | | station = stationService.getStationMap(slave.getId()); |
| | | } |
| | | log.warn("站点未连接,跳过读取 [id:{}]", slave.getId()); |
| | | return; |
| | | } |
| | | |
| | | List<Integer> staNos = slave.getStaNos(); |
| | | int staNoSize = staNos.size(); |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (staNoSize * 2)); |
| | | OperateResultExOne<byte[]> result2 = siemensS7Net.Read("DB100.0", (short) (staNoSize * 4)); |
| | | if (result.IsSuccess) { |
| | | for (int i = 0; i < staNoSize; i++) { |
| | | Integer siteId = staNos.get(i); // 站点编号 |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | if (null == staProtocol) { |
| | | staProtocol = new StaProtocol(); |
| | | staProtocol.setSiteId(siteId); |
| | | station.put(siteId, staProtocol); |
| | | } |
| | | boolean[] status = null; |
| | | if (i < 7) { |
| | | short i1 = siemensS7Net.getByteTransform().TransInt16(result2.Content, i * 4 + 2); |
| | | staProtocol.setWorkNo((int) siemensS7Net.getByteTransform().TransInt16(result2.Content, i * 4)); |
| | | staProtocol.setStaNo((int) siemensS7Net.getByteTransform().TransInt16(result2.Content, i * 4 + 2)); |
| | | } |
| | | status = siemensS7Net.getByteTransform().TransBool(result.Content, i * 2, 2); |
| | | staProtocol.setAutoing(status[0]); // 自动 |
| | | staProtocol.setLoading(status[1]); // 有物 |
| | | staProtocol.setInEnable(status[2]); // 可入 |
| | | staProtocol.setOutEnable(status[3]);// 可出 |
| | | staProtocol.setEmptyMk(status[4]); // 空板信号 |
| | | staProtocol.setFullPlt(status[5]); // 满托盘 |
| | | staProtocol.setHigh(status[6]); // 高库位 |
| | | staProtocol.setLow(status[7]); // 低库位 |
| | | |
| | | if (!staProtocol.isPakMk() && !staProtocol.isLoading()) { |
| | | staProtocol.setPakMk(true); |
| | | } |
| | | } |
| | | // 读取站点状态 |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read( |
| | | StationStatusField.ALL.buildAddress(), |
| | | (short) (staNoSize * StationStatusField.ALL.getByteLength())); |
| | | |
| | | if (!result.IsSuccess) { |
| | | log.error("读取站点状态失败 [id:{}] [error:{}]", slave.getId(), result.Message); |
| | | return; |
| | | } |
| | | |
| | | |
| | | //条码扫描器 |
| | | ArrayList<Integer> barcodeList = BarcodeList; |
| | | OperateResultExOne<byte[]> result5 = siemensS7Net.Read("DB101.102", (short) (barcodeList.size() * 8)); |
| | | if (result5.IsSuccess) { |
| | | for (int i = 0; i < barcodeList.size(); i++) { |
| | | Integer barcodeId = barcodeList.get(i); |
| | | String barcode = siemensS7Net.getByteTransform().TransString(result5.Content, i * 8, 8, "UTF-8"); |
| | | BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeId); |
| | | if (Cools.isEmpty(barcode)) { |
| | | barcodeThread.clearBarcode(); |
| | | } else { |
| | | if (!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) { |
| | | barcodeThread.setBarcode(barcode); |
| | | log.info("料箱码:{}", barcode); |
| | | } |
| | | } |
| | | } |
| | | byte[] content = result.Content; |
| | | for (int i = 0; i < staNoSize; i++) { |
| | | StaProtocol staProtocol = station.get(staNos.get(staNoSize)); |
| | | parseStationStatus(content, i, staProtocol); |
| | | } |
| | | |
| | | if (result.IsSuccess) { |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", DateUtils.convert(new Date()), slave.getId())); |
| | | // 读取条码 |
| | | readBarcodes(); |
| | | |
| | | // 根据实时信息更新数据库 |
| | | try { |
| | | List<Devp> devps = new ArrayList<>(); |
| | | for (Integer siteId : staNos) { |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | devps.add(staProtocol.toSqlModel()); |
| | | // 称重 |
| | | readWeight(); |
| | | |
| | | // 读取外形检测错误 |
| | | readDimensionErrors(); |
| | | |
| | | // 读取PLC故障 |
| | | readPlcAlarms(staNos, staNoSize); |
| | | |
| | | // 定期更新数据库(降低频率) |
| | | updateDatabaseIfNeeded(); |
| | | } |
| | | |
| | | /** |
| | | * 解析单个站点状态 |
| | | */ |
| | | private void parseStationStatus(byte[] content, int index, StaProtocol staProtocol) { |
| | | int offset = index * StationStatusField.ALL.getByteLength(); |
| | | staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(content, offset)); |
| | | staProtocol.setStaNo((int) siemensS7Net.getByteTransform().TransInt16( |
| | | content, offset + StationStatusField.FINAL_TARGET.getOffset())); |
| | | |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool( |
| | | content, offset + StationStatusField.STATUS_WORD.getOffset(), |
| | | StationStatusField.STATUS_WORD.getByteLength()); |
| | | |
| | | staProtocol.setAutoing(status[0]); |
| | | staProtocol.setLoading(status[1]); |
| | | staProtocol.setInEnable(status[2]); |
| | | staProtocol.setOutEnable(status[3]); |
| | | staProtocol.setEmptyMk(status[4]); |
| | | staProtocol.setFullPlt(status[5]); |
| | | staProtocol.setHigh(status[6]); |
| | | staProtocol.setLow(status[7]); |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 读取条码信息 |
| | | */ |
| | | private void readBarcodes() { |
| | | List<Integer> barcodeArr = slave.getBarcodeArr(); |
| | | if (barcodeArr == null || barcodeArr.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read( |
| | | DeviceField.BARCODE.buildAddress(), |
| | | (short) (barcodeArr.size() * DeviceField.BARCODE.getByteLength())); |
| | | |
| | | if (!result.IsSuccess) { |
| | | log.warn("读取条码失败 [id:{}]", slave.getId()); |
| | | return; |
| | | } |
| | | |
| | | byte[] content = result.Content; |
| | | for (int i = 0; i < barcodeArr.size(); i++) { |
| | | String barcode = siemensS7Net.getByteTransform().TransString( |
| | | content, i * DeviceField.BARCODE.getByteLength(), |
| | | DeviceField.BARCODE.getByteLength(), "UTF-8"); |
| | | |
| | | if (!Cools.isEmpty(barcode)) { |
| | | StaProtocol staProtocol = station.get(barcodeArr.get(i)); |
| | | if (staProtocol == null) { |
| | | continue; |
| | | } |
| | | DevpService devpService = SpringContextUtil.getBean(DevpService.class); |
| | | if (null != devpService) { |
| | | devpService.updateBatchByDevpNo(devps); |
| | | } else { |
| | | throw new Exception("更新数据库数据失败"); |
| | | } |
| | | } catch (Exception e) { |
| | | initSite(); |
| | | e.printStackTrace(); |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】更新数据库数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot())); |
| | | News.error("SiemensDevp" + " - 3" + " - 更新数据库数据失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()); |
| | | staProtocol.setBarcode(barcode); |
| | | News.info("料箱码:{}", barcode); |
| | | } |
| | | |
| | | } else { |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】读取输送线plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot())); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 写入 ID+目标站 =====> 单站点写入 |
| | | * 读取条码信息 |
| | | */ |
| | | private void write(StaProtocol staProtocol) throws InterruptedException { |
| | | if (null == staProtocol) { |
| | | private void readWeight() { |
| | | List<Integer> weightArr = slave.getWeightArr(); |
| | | if (weightArr == null || weightArr.isEmpty()) { |
| | | return; |
| | | } |
| | | ArrayList<Integer> staNos = getStaNo(); |
| | | int index = staNos.indexOf(staProtocol.getSiteId()); |
| | | |
| | | OperateResult write = null; |
| | | OperateResult write1 = null; |
| | | //任务下发次数 |
| | | int writeCount = 0; |
| | | do { |
| | | write = siemensS7Net.Write("DB100." + index * 4, staProtocol.getWorkNo().shortValue()); // 工作号 |
| | | Thread.sleep(200); |
| | | write1 = siemensS7Net.Write("DB100." + (index * 4 + 2), staProtocol.getStaNo().shortValue()); // 目标站 |
| | | if (write.IsSuccess && write1.IsSuccess) { |
| | | log.info("写入输送线命令成功。输送线plc编号={},站点数据={},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount); |
| | | break; |
| | | } else { |
| | | writeCount++; |
| | | log.error("写入输送线命令失败。输送线plc编号={},站点数据={},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount); |
| | | } |
| | | } while (writeCount < 5); |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read( |
| | | DeviceField.WEIGHT.buildAddress(), |
| | | (short) (weightArr.size() * DeviceField.WEIGHT.getByteLength())); |
| | | |
| | | if (!write.IsSuccess) { |
| | | staProtocol = station.get(staProtocol.getSiteId()); |
| | | if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() == 0) { |
| | | staProtocol.setPakMk(true); |
| | | if (!result.IsSuccess) { |
| | | log.warn("读取重量失败 [id:{}]", slave.getId()); |
| | | return; |
| | | } |
| | | for (int i = 0; i < weightArr.size(); i++) { |
| | | StaProtocol staProtocol = station.get(weightArr.get(i)); |
| | | if (staProtocol == null) { |
| | | continue; |
| | | } |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", slave.getId(), JSON.toJSON(staProtocol))); |
| | | News.error("SiemensDevp" + " - 4" + " - 写入输送线站点数据失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol)); |
| | | double weight = siemensS7Net.getByteTransform().TransSingle(result.Content, i * 4); |
| | | staProtocol.setWeight(BigDecimal.valueOf(weight).setScale(2, RoundingMode.HALF_UP).doubleValue()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取外形检测错误 |
| | | */ |
| | | private void readDimensionErrors() { |
| | | List<Integer> staNosError = slave.getStaNosError(); |
| | | if (staNosError == null || staNosError.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | @Override |
| | | public void close() { |
| | | siemensS7Net.ConnectClose(); |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read( |
| | | DeviceField.DIMENSION_WORD.buildAddress(), |
| | | (short) (staNosError.size() * DeviceField.DIMENSION_WORD.getByteLength())); |
| | | |
| | | if (!result.IsSuccess) { |
| | | log.warn("读取外形检测错误失败 [id:{}]", slave.getId()); |
| | | return; |
| | | } |
| | | |
| | | byte[] content = result.Content; |
| | | for (int i = 0; i < staNosError.size(); i++) { |
| | | Integer siteId = staNosError.get(i); |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool( |
| | | content, i * DeviceField.DIMENSION_WORD.getByteLength(), |
| | | DeviceField.DIMENSION_WORD.getByteLength()); |
| | | |
| | | staProtocol.setFrontErr(status[0]); |
| | | staProtocol.setBackErr(status[1]); |
| | | staProtocol.setHighErr(status[2]); |
| | | staProtocol.setLeftErr(status[3]); |
| | | staProtocol.setRightErr(status[4]); |
| | | staProtocol.setWeightErr(status[5]); |
| | | staProtocol.setBarcodeErr(status[6]); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取PLC故障信息 |
| | | */ |
| | | private void readPlcAlarms(List<Integer> staNos, int staNoSize) { |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read( |
| | | PlcAlarmDefinition.ALL.buildAddress(), |
| | | (short) (staNoSize * PlcAlarmDefinition.ALL.getByteLength())); |
| | | |
| | | if (!result.IsSuccess) { |
| | | log.warn("读取PLC故障信息失败 [id:{}]", slave.getId()); |
| | | return; |
| | | } |
| | | |
| | | byte[] content = result.Content; |
| | | for (int i = 0; i < staNoSize; i++) { |
| | | Integer siteId = staNos.get(i); |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | if (staProtocol == null) { |
| | | continue; |
| | | } |
| | | |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool( |
| | | content, i * PlcAlarmDefinition.ALL.getByteLength(), 1); |
| | | |
| | | staProtocol.setBreakerErr(status[0]); |
| | | staProtocol.setInfraredErr(status[1]); |
| | | staProtocol.setOutTimeErr(status[2]); |
| | | staProtocol.setSeizeSeatErr(status[3]); |
| | | staProtocol.setWrkYgoodsN(status[4]); |
| | | staProtocol.setInverterErr(status[5]); |
| | | staProtocol.setContactErr(status[6]); |
| | | staProtocol.setUpcontactErr(status[7]); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 按需更新数据库(降低更新频率) |
| | | */ |
| | | private void updateDatabaseIfNeeded() { |
| | | long currentTime = System.currentTimeMillis(); |
| | | if (currentTime - lastDbUpdateTime < DB_UPDATE_INTERVAL_MS) { |
| | | return; |
| | | } |
| | | |
| | | try { |
| | | List<Integer> staNos = slave.getStaNos(); |
| | | List<Devp> devps = new ArrayList<>(staNos.size()); |
| | | for (Integer siteId : staNos) { |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | if (staProtocol != null) { |
| | | devps.add(staProtocol.toSqlModel()); |
| | | } |
| | | } |
| | | |
| | | if (devps.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | DevpService devpService = SpringContextUtil.getBean(DevpService.class); |
| | | if (devpService != null) { |
| | | devpService.updateBatchByDevpNo(devps); |
| | | lastDbUpdateTime = currentTime; |
| | | log.debug("批量更新数据库成功 [id:{}] [count:{}]", slave.getId(), devps.size()); |
| | | } else { |
| | | log.error("DevpService未找到,无法更新数据库 [id:{}]", slave.getId()); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("更新数据库数据失败 [id:{}]", slave.getId(), e); |
| | | News.error("SiemensDevp - 3 - 更新数据库数据失败 ===>> [id:{}]", slave.getId()); |
| | | } |
| | | } |
| | | |
| | | |