| | |
| | | |
| | | private SiemensS7Net siemensS7Net; |
| | | |
| | | private Map<Integer, StaProtocol> station; |
| | | private StationService stationService; |
| | | |
| | | private final RedisSupport redis = RedisSupport.defaultRedisSupport; |
| | | |
| | |
| | | private long lastDbUpdateTime = 0; |
| | | |
| | | |
| | | public SiemensDevpThread(DevpSlave slave, SiemensS7Net siemensS7Net, Map<Integer, StaProtocol> station) { |
| | | public SiemensDevpThread(DevpSlave slave, SiemensS7Net siemensS7Net, StationService stationService) { |
| | | this.slave = slave; |
| | | this.siemensS7Net = siemensS7Net; |
| | | this.station = station; |
| | | this.stationService = stationService; |
| | | } |
| | | |
| | | |
| | |
| | | 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; |
| | | Map<Integer, StaProtocol> stationMap = null; |
| | | if (stationService == null) { |
| | | stationService = SpringContextUtil.getBean(StationService.class); |
| | | stationMap = stationService.getStationMap(slave.getId()); |
| | | } else { |
| | | stationMap = stationService.getStationMap(slave.getId()); |
| | | } |
| | | |
| | | List<Integer> staNos = slave.getStaNos(); |
| | |
| | | |
| | | byte[] content = result.Content; |
| | | for (int i = 0; i < staNoSize; i++) { |
| | | StaProtocol staProtocol = station.get(staNos.get(i)); |
| | | StaProtocol staProtocol = stationMap.get(staNos.get(i)); |
| | | parseStationStatus(content, i, staProtocol); |
| | | } |
| | | |
| | | // 读取条码 |
| | | readBarcodes(); |
| | | readBarcodes(stationMap); |
| | | |
| | | // 称重 |
| | | readWeight(); |
| | | readWeight(stationMap); |
| | | |
| | | // 读取外形检测错误 |
| | | readDimensionErrors(); |
| | | readDimensionErrors(stationMap); |
| | | |
| | | // 读取PLC故障 |
| | | readPlcAlarms(staNos, staNoSize); |
| | | readPlcAlarms(stationMap, staNos, staNoSize); |
| | | |
| | | // 读取安全交互信号 |
| | | readSaleSingle(slave.getPutSta(), slave.getTakeSta()); |
| | | readSaleSingle(stationMap, slave.getPutSta(), slave.getTakeSta()); |
| | | |
| | | |
| | | // 定期更新数据库(降低频率) |
| | | updateDatabaseIfNeeded(); |
| | | updateDatabaseIfNeeded(stationMap); |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 读取条码信息 |
| | | */ |
| | | private void readBarcodes() { |
| | | private void readBarcodes(Map<Integer, StaProtocol> stationMap) { |
| | | if (slave.getBarcode() == null || !slave.getBarcode()) { |
| | | return; |
| | | } |
| | |
| | | DeviceField.BARCODE.getByteLength() - seg[1], "UTF-8").trim(); |
| | | |
| | | if (!Cools.isEmpty(barcode) && !barcode.contains("Default") && !barcode.contains("Error") && staNo != 0) { |
| | | StaProtocol staProtocol = station.get(staNo); |
| | | StaProtocol staProtocol = stationMap.get(staNo); |
| | | if (staProtocol == null) { |
| | | log.warn("扫码站点不存在 [id:{}] [staNo:{}]", slave.getId(), staNo); |
| | | continue; |
| | |
| | | /** |
| | | * 读取重量信息 |
| | | */ |
| | | private void readWeight() { |
| | | private void readWeight(Map<Integer, StaProtocol> stationMap) { |
| | | if (slave.getWeight() == null || !slave.getWeight()) { |
| | | return; |
| | | } |
| | |
| | | Double weight = (double) siemensS7Net.getByteTransform().TransSingle( |
| | | content, seg[1] + i * DeviceField.WEIGHT.getByteLength()); |
| | | if (!Cools.isEmpty(weight) && staNo != 0) { |
| | | StaProtocol staProtocol = station.get(staNo); |
| | | StaProtocol staProtocol = stationMap.get(staNo); |
| | | if (staProtocol == null) { |
| | | log.warn(" [id:{}] [staNo:{}]", slave.getId(), staNo); |
| | | continue; |
| | |
| | | /** |
| | | * 读取外形检测错误 |
| | | */ |
| | | private void readDimensionErrors() { |
| | | private void readDimensionErrors(Map<Integer, StaProtocol> stationMap) { |
| | | if (slave.getStaNosError() == null || !slave.getStaNosError()) { |
| | | return; |
| | | } |
| | |
| | | for (int i = 0; i < DeviceField.DIMENSION_WORD.getArrLength(); i++) { |
| | | int[] seg = DeviceField.DIMENSION_WORD.getSeg(); |
| | | int staNo = siemensS7Net.getByteTransform().TransInt16( |
| | | content, seg[0]); |
| | | content, seg[0] + i * DeviceField.DIMENSION_WORD.getByteLength()); |
| | | if (staNo != 0) { |
| | | StaProtocol staProtocol = station.get(staNo); |
| | | StaProtocol staProtocol = stationMap.get(staNo); |
| | | if (staProtocol == null) { |
| | | log.warn("异常站点不存在 [id:{}] [staNo:{}]", slave.getId(), staNo); |
| | | continue; |
| | |
| | | /** |
| | | * 读取PLC故障信息 |
| | | */ |
| | | private void readPlcAlarms(List<Integer> staNos, int staNoSize) { |
| | | private void readPlcAlarms(Map<Integer, StaProtocol> stationMap, List<Integer> staNos, int staNoSize) { |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read( |
| | | PlcAlarmDefinition.ALL.buildAddress(), |
| | | (short) (staNoSize * PlcAlarmDefinition.ALL.getByteLength())); |
| | |
| | | byte[] content = result.Content; |
| | | for (int i = 0; i < staNoSize; i++) { |
| | | Integer siteId = staNos.get(i); |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | StaProtocol staProtocol = stationMap.get(siteId); |
| | | if (staProtocol == null) { |
| | | log.warn("站点不存在 [id:{}] [staNo:{}]", slave.getId(), siteId); |
| | | continue; |
| | |
| | | /** |
| | | * 读取安全交互信号 |
| | | */ |
| | | private void readSaleSingle(List<DevpSlave.Sta> putSta, List<DevpSlave.Sta> takeSta) { |
| | | private void readSaleSingle(Map<Integer, StaProtocol> stationMap, List<DevpSlave.Sta> putSta, List<DevpSlave.Sta> takeSta) { |
| | | int staNoSize = putSta.size(); |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read( |
| | | SafeSignalField.SAFE_SIGNAL_FROM_CONVEYOR.buildAddress(), |
| | |
| | | for (int i = 0; i < staNoSize; i++) { |
| | | Integer put = putSta.get(i).getStaNo(); |
| | | Integer take = takeSta.get(i).getStaNo(); |
| | | StaProtocol staProtocolPut = station.get(put); |
| | | StaProtocol staProtocolTake = station.get(take); |
| | | StaProtocol staProtocolPut = stationMap.get(put); |
| | | StaProtocol staProtocolTake = stationMap.get(take); |
| | | if (staProtocolPut == null || staProtocolTake == null) { |
| | | log.warn("站点不存在 [id:{}] [staNo:{},{}]", slave.getId(), put, take); |
| | | continue; |
| | |
| | | /** |
| | | * 按需更新数据库(降低更新频率) |
| | | */ |
| | | private void updateDatabaseIfNeeded() { |
| | | private void updateDatabaseIfNeeded(Map<Integer, StaProtocol> stationMap) { |
| | | long currentTime = System.currentTimeMillis(); |
| | | if (currentTime - lastDbUpdateTime < DB_UPDATE_INTERVAL_MS) { |
| | | return; |
| | |
| | | List<Integer> staNos = slave.getStaNos(); |
| | | List<Devp> devps = new ArrayList<>(staNos.size()); |
| | | for (Integer siteId : staNos) { |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | StaProtocol staProtocol = stationMap.get(siteId); |
| | | if (staProtocol != null) { |
| | | devps.add(staProtocol.toSqlModel()); |
| | | } |