1
zhang
3 天以前 36485d6d285438c2ccfa5c8352ab8b3fd831bed0
zy-acs-cv/src/main/java/com/zy/core/thread/SiemensDevpThread.java
@@ -7,7 +7,6 @@
import com.alibaba.fastjson.JSON;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.DateUtils;
import com.zy.acs.framework.exception.CoolException;
import com.zy.asrs.entity.Devp;
import com.zy.asrs.service.DevpService;
import com.zy.asrs.utils.SpringContextUtil;
@@ -16,6 +15,7 @@
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.constant.*;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.TaskType;
import com.zy.core.model.DevpSlave;
@@ -40,45 +40,16 @@
public class SiemensDevpThread implements Runnable, DevpThread {
    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);
    }};
    public static final ArrayList<Integer> BarcodeList = new ArrayList<Integer>() {{
        add(1);
    }};
    /**
     * 条码数量
     */
    private int barcodeSize = 1;
    public SiemensDevpThread(DevpSlave slave) {
        this.slave = slave;
    }
    private ArrayList<Integer> getStaNo() {
        switch (slave.getId()) {
            case 1:
                return staNos1;
            default:
                throw new CoolException("服务器异常");
        }
    }
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
@@ -103,8 +74,6 @@
                    default:
                        break;
                }
                // 心跳
//                heartbeat();
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
@@ -117,7 +86,7 @@
     * 初始化站点状态
     */
    private void initSite() {
        ArrayList<Integer> staNos = getStaNo();
        List<Integer> staNos = slave.getStaNos();
        // 站点编号
        for (Integer siteId : staNos) {
            StaProtocol staProtocol = station.get(siteId);
@@ -164,10 +133,9 @@
     * 读取状态 ====> 整块plc
     */
    private void read() throws InterruptedException {
        ArrayList<Integer> staNos = getStaNo();
        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));
        OperateResultExOne<byte[]> result = siemensS7Net.Read(StationStatusField.ALL.buildAddress(), (short) (staNoSize * StationStatusField.ALL.getByteLength()));
        if (result.IsSuccess) {
            for (int i = 0; i < staNoSize; i++) {
                Integer siteId = staNos.get(i); // 站点编号
@@ -177,12 +145,9 @@
                    staProtocol.setSiteId(siteId);
                    station.put(siteId, staProtocol);
                }
                boolean[] status = null;
                if (i < 7) {
                    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.setWorkNo((int) siemensS7Net.getByteTransform().TransUInt32(result.Content, i * StationStatusField.ALL.getByteLength()));
                staProtocol.setStaNo((int) siemensS7Net.getByteTransform().TransInt16(result.Content, i * StationStatusField.ALL.getByteLength() + 4));
                boolean[] status = siemensS7Net.getByteTransform().TransBool(result.Content, i * StationStatusField.ALL.getByteLength() + 6, 2);
                staProtocol.setAutoing(status[0]);  // 自动
                staProtocol.setLoading(status[1]);  // 有物
                staProtocol.setInEnable(status[2]); // 可入
@@ -200,13 +165,12 @@
        //条码扫描器
        ArrayList<Integer> barcodeList = BarcodeList;
        OperateResultExOne<byte[]> result5 = siemensS7Net.Read("DB101.102", (short) (barcodeList.size() * 8));
        List<Integer> barcodeArr = slave.getBarcodeArr();
        OperateResultExOne<byte[]> result5 = siemensS7Net.Read(DeviceField.BARCODE.buildAddress(), (short) (barcodeArr.size() * DeviceField.BARCODE.getByteLength()));
        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);
            for (int i = 0; i < barcodeArr.size(); i++) {
                String barcode = siemensS7Net.getByteTransform().TransString(result5.Content, i * DeviceField.BARCODE.getByteLength(), DeviceField.BARCODE.getByteLength(), "UTF-8");
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, barcodeArr.get(i));
                if (Cools.isEmpty(barcode)) {
                    barcodeThread.clearBarcode();
                } else {
@@ -214,6 +178,48 @@
                        barcodeThread.setBarcode(barcode);
                        log.info("料箱码:{}", barcode);
                    }
                }
            }
        }
        List<Integer> staNosError = slave.getStaNosError();
        OperateResultExOne<byte[]> resultError = siemensS7Net.Read(DeviceField.DIMENSION_WORD.buildAddress(), (short) (staNoSize * DeviceField.DIMENSION_WORD.getByteLength()));
        if (resultError.IsSuccess) {
            for (int i = 0; i < staNosError.size(); i++) {
                Integer siteId = staNosError.get(i); // 站点编号
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
                    staProtocol.setSiteId(siteId);
                    station.put(siteId, staProtocol);
                }
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultError.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故障
        OperateResultExOne<byte[]> resultErr2 = siemensS7Net.Read(PlcAlarmDefinition.ALL.buildAddress(), (short) (staNoSize * PlcAlarmDefinition.ALL.getByteLength()));
        if (resultErr2.IsSuccess) {
            for (int i = 0; i < staNoSize; i++) {
                Integer siteId = staNos.get(i); // 站点编号
                boolean[] status = siemensS7Net.getByteTransform().TransBool(resultErr2.Content, i * PlcAlarmDefinition.ALL.getByteLength(), PlcAlarmDefinition.ALL.getByteLength());
                StaProtocol staProtocol = station.get(siteId);
                if (staProtocol != null) {
                    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]);
                }
            }
        }
@@ -253,7 +259,7 @@
        if (null == staProtocol) {
            return;
        }
        ArrayList<Integer> staNos = getStaNo();
        List<Integer> staNos = slave.getStaNos();
        int index = staNos.indexOf(staProtocol.getSiteId());
        OperateResult write = null;
@@ -261,9 +267,9 @@
        //任务下发次数
        int writeCount = 0;
        do {
            write = siemensS7Net.Write("DB100." + index * 4, staProtocol.getWorkNo().shortValue());    // 工作号
            write = siemensS7Net.Write(TaskField.TASK_NUMBER.getAddressPattern() + PlcConstant.ADDRESS_CONCATENATION + (index * TaskField.ALL.getByteLength() + TaskField.TASK_NUMBER.getOffset()), staProtocol.getWorkNo().shortValue());    // 工作号
            Thread.sleep(200);
            write1 = siemensS7Net.Write("DB100." + (index * 4 + 2), staProtocol.getStaNo().shortValue());    // 目标站
            write1 = siemensS7Net.Write(TaskField.DEST_STATION.getAddressPattern() + PlcConstant.ADDRESS_CONCATENATION + (index * TaskField.ALL.getByteLength() + TaskField.DEST_STATION.getOffset()), staProtocol.getStaNo().shortValue());    // 目标站
            if (write.IsSuccess && write1.IsSuccess) {
                log.info("写入输送线命令成功。输送线plc编号={},站点数据={},写入次数={}", slave.getId(), JSON.toJSON(staProtocol), writeCount);
                break;