1
zhang
20 小时以前 80a29f7eb79e5a6929437e407d6179f1542fabde
zy-asc-conveyor/src/main/java/com/zy/acs/conveyor/core/thread/SiemensDevpThread.java
@@ -8,6 +8,8 @@
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;
@@ -15,6 +17,8 @@
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -32,8 +36,6 @@
    private SiemensS7Net siemensS7Net;
    private Map<Integer, StaProtocol> station;
    private volatile boolean connected = false;
    private static final int WRITE_RETRY_MAX = 5;
@@ -75,8 +77,20 @@
     * 读取状态 ====> 整块plc
     */
    private void read() throws InterruptedException {
        if (!connected || siemensS7Net == null) {
        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;
        }
@@ -90,7 +104,6 @@
        if (!result.IsSuccess) {
            log.error("读取站点状态失败 [id:{}] [error:{}]", slave.getId(), result.Message);
            connected = false;
            return;
        }
@@ -102,6 +115,9 @@
        // 读取条码
        readBarcodes();
        // 称重
        readWeight();
        // 读取外形检测错误
        readDimensionErrors();
@@ -164,13 +180,43 @@
            if (!Cools.isEmpty(barcode)) {
                StaProtocol staProtocol = station.get(barcodeArr.get(i));
                if (staProtocol == null) {
                    continue;
                }
                staProtocol.setBarcode(barcode);
                log.info("料箱码:{}", barcode);
                News.info("料箱码:{}", barcode);
            }
        }
    }
    /**
     * 读取条码信息
     */
    private void readWeight() {
        List<Integer> weightArr = slave.getWeightArr();
        if (weightArr == null || weightArr.isEmpty()) {
            return;
        }
        OperateResultExOne<byte[]> result = siemensS7Net.Read(
                DeviceField.WEIGHT.buildAddress(),
                (short) (weightArr.size() * DeviceField.WEIGHT.getByteLength()));
        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;
            }
            double weight = siemensS7Net.getByteTransform().TransSingle(result.Content, i * 4);
            staProtocol.setWeight(BigDecimal.valueOf(weight).setScale(2, RoundingMode.HALF_UP).doubleValue());
        }
    }
    /**
     * 读取外形检测错误
     */
    private void readDimensionErrors() {