#
zjj
2024-06-21 9372f61dcb41eeb2e09871caefce0f764d6ab021
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/impl/SiemensDevpThread.java
@@ -5,16 +5,20 @@
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.common.DateUtils;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.framework.common.SpringUtils;
import com.zy.asrs.wcs.core.entity.BasConveyor;
import com.zy.asrs.wcs.core.entity.BasConveyorSta;
import com.zy.asrs.wcs.core.service.BasConveyorService;
import com.zy.asrs.wcs.core.service.BasConveyorStaService;
import com.zy.asrs.wcs.core.utils.RedisUtil;
import com.zy.asrs.wcs.rcs.News;
import com.zy.asrs.wcs.rcs.cache.MessageQueue;
import com.zy.asrs.wcs.rcs.cache.OutputQueue;
import com.zy.asrs.wcs.rcs.entity.Device;
import com.zy.asrs.wcs.rcs.model.Task;
import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
import com.zy.asrs.wcs.rcs.model.dto.WorkModeTypeDto;
import com.zy.asrs.wcs.rcs.model.enums.WorkModeType;
import com.zy.asrs.wcs.rcs.model.protocol.StaProtocol;
import com.zy.asrs.wcs.rcs.thread.DevpThread;
import lombok.extern.slf4j.Slf4j;
@@ -22,6 +26,7 @@
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -34,66 +39,74 @@
    private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>();
    public static final ArrayList<Integer> staNos1 = new ArrayList<Integer>() {{
        add(100);add(101);add(102);add(103);add(104);add(105);
        add(200);add(201);add(202);add(203);add(204);add(205);add(206);
    }};
    public static ArrayList<BasConveyorSta> stationList = new ArrayList<>();
    /**
     * 条码数量
     */
    private int barcodeSize = 2;
    /**
     * 入出库模式
     * 0:未知
     * 1:入库启动中
     * 2.入库模式
     * 3.出库启动中 (不能生成入库工作档)
     * 4.出库模式
     */
    private Map<Integer, WorkModeTypeDto> workModeTypes = new ConcurrentHashMap<>();
    public SiemensDevpThread(Device device, RedisUtil redisUtil) {
        this.device = device;
        this.redisUtil = redisUtil;
        workModeTypes.put(101, new WorkModeTypeDto(101, WorkModeType.NONE, "DB1001.120"));
    }
    private ArrayList<Integer> getStaNo() {
        switch (Integer.parseInt(device.getDeviceNo())) {
            case 1:
                return staNos1;
            default:
                throw new CoolException("获取站点失败!");
    private ArrayList<BasConveyorSta> getStaNo() {
        try {
            if (stationList.isEmpty()) {
                BasConveyorService basConveyorService = SpringUtils.getBean(BasConveyorService.class);
                BasConveyorStaService basConveyorStaService = SpringUtils.getBean(BasConveyorStaService.class);
                BasConveyor basConveyor = basConveyorService.getOne(new LambdaQueryWrapper<BasConveyor>()
                        .eq(BasConveyor::getDeviceId, device.getId())
                        .eq(BasConveyor::getHostId, device.getHostId()));
                if(basConveyor != null) {
                    List<BasConveyorSta> stations = basConveyorStaService.list(new LambdaQueryWrapper<BasConveyorSta>()
                            .eq(BasConveyorSta::getConveyorId, basConveyor.getId())
                            .eq(BasConveyorSta::getHostId, device.getHostId()));
                    stationList.addAll(stations);
                }
            }
            return stationList;
        } catch (Exception e) {
            return stationList;
        }
    }
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        connect();
        News.info("{}号输送线线程启动", device.getDeviceNo());
        this.connect();
        while (true) {
            try {
                int step = 1;
                Task task = MessageQueue.poll(SlaveType.Devp, device.getId().intValue());
                if (task != null) {
                    step = task.getStep();
                }
                switch (step) {
                    // 读数据
                    case 1:
                        read();
                        break;
                    default:
                        break;
                }
                // 心跳
//                heartbeat();
                Thread.sleep(400);
                read();
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    private void read() throws InterruptedException {
        ArrayList<Integer> staNos = getStaNo();
        ArrayList<BasConveyorSta> staNos = getStaNo();
        int staNoSize = staNos.size();
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) (staNoSize*8));
        if (result.IsSuccess) {
            for (int i = 0; i < staNoSize; i++) {
                Integer siteId = staNos.get(i); // 站点编号
                BasConveyorSta siteStation = staNos.get(i);
                int siteId = siteStation.getSiteNo();// 站点编号
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
@@ -120,28 +133,14 @@
            }
        }
        // 条码扫描器
        Thread.sleep(200);
        OperateResultExOne<byte[]> result2 = null;
        result2 = siemensS7Net.Read("DB101.840.0", (short) (barcodeSize * 8));
        if (result2.IsSuccess) {
            for (int i = 0; i < barcodeSize; i++) {
                String barcode = siemensS7Net.getByteTransform().TransString(result2.Content, i * 8, 8, "UTF-8");
//                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, i + 1);
//                if (!Cools.isEmpty(barcodeThread) && !barcodeThread.getBarcode().equals(barcode)) {
//                    barcodeThread.setBarcode(barcode);
//                }
            }
        }
        Thread.sleep(200);
        ArrayList<Integer> errorStaNo = getStaNo();
        ArrayList<BasConveyorSta> errorStaNo = getStaNo();
        OperateResultExOne<byte[]> result3 = siemensS7Net.Read("DB101.800.0", (short) (errorStaNo.size() * 4));
        if (result3.IsSuccess) {
            for (int i = 0; i < errorStaNo.size(); i++) {
                Integer siteId = errorStaNo.get(i); // 站点编号
                BasConveyorSta siteStation = errorStaNo.get(i);
                Integer siteId = siteStation.getSiteNo(); // 站点编号
                StaProtocol staProtocol = station.get(siteId);
//                staProtocol.setLocType1((short) siemensS7Net.getByteTransform().TransInt32(result3.Content, i * 4));//高低信号
                boolean[] status = siemensS7Net.getByteTransform().TransBool(result3.Content, (i * 4 + 2), 2);
                staProtocol.setFrontErr(status[0]);//前超限
                staProtocol.setBackErr(status[1]);//后超限
@@ -153,24 +152,26 @@
            }
        }
        if (result.IsSuccess && !Cools.isEmpty(result) && result.IsSuccess) {
        if (!Cools.isEmpty(result) && result.IsSuccess) {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), device.getId()));
            // 根据实时信息更新数据库
            try {
//                List<BasDevp> basDevps = new ArrayList<>();
//                for (Integer siteId : staNos) {
//                    StaProtocol staProtocol = station.get(siteId);
//                    basDevps.add(staProtocol.toSqlModel());
//                }
//
//                BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
//                if (null != basDevpService && !basDevpService.updateBatchById(basDevps)) {
//                    throw new Exception("更新数据库数据失败");
//                }
                List<BasConveyorSta> stations = new ArrayList<>();
                for (BasConveyorSta sta : getStaNo()) {
                    StaProtocol staProtocol = station.get(sta.getSiteNo());
                    BasConveyorSta sqlModel = staProtocol.toSqlModel(sta);
                    stations.add(sqlModel);
                }
                if (!stations.isEmpty()) {
                    BasConveyorStaService basConveyorStaService = SpringUtils.getBean(BasConveyorStaService.class);
                    if (null != basConveyorStaService && !basConveyorStaService.updateBatchById(stations)) {
                        throw new Exception("更新数据库数据失败");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】更新数据库数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot()));
@@ -209,8 +210,8 @@
    @Override
    public boolean writeWorkNo(int siteId, short workNo) {
        ArrayList<Integer> staNos = getStaNo();
        int index = staNos.indexOf(siteId);
        int index = findStaNosIndex(siteId);
        OperateResult write = siemensS7Net.Write("DB100." + index*6, workNo);    // 工作号
        if (!write.IsSuccess) {
@@ -230,8 +231,8 @@
    @Override
    public boolean writeStaNo(int siteId,short staNo) {
        ArrayList<Integer> staNos = getStaNo();
        int index = staNos.indexOf(siteId);
        int index = findStaNosIndex(siteId);
        OperateResult write = siemensS7Net.Write("DB100." + (index*6+4), staNo);    // 目标站
        if (!write.IsSuccess) {
@@ -250,7 +251,79 @@
    }
    @Override
    public boolean writeWorkSta(int siteId, short staNo, short workNo) {
        int index = findStaNosIndex(siteId);
        OperateResult write1 = siemensS7Net.Write("DB100." + index*6, workNo);    // 工作号
        OperateResult write2 = siemensS7Net.Write("DB100." + (index*6+4), staNo);    // 目标站
        if (!(write1.IsSuccess && write2.IsSuccess)) {
            StaProtocol staProtocol = station.get(siteId);
            if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) {
                staProtocol.setPakMk(true);
            }
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", device.getId(), JSON.toJSON(staProtocol)));
            log.error("写入输送线站点数据失败。输送线plc编号={},站点数据={}", device.getId(), JSON.toJSON(staProtocol));
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】 输送线命令下发 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), device.getId(), JSON.toJSON(staNo)));
            log.info("输送线命令下发 [id:{}] >>>>> 命令下发: {}",  device.getId(), JSON.toJSON(staNo));
            return true;
        }
        return false;
    }
    @Override
    public Map<Integer, StaProtocol> getStation() {
        return this.station;
    }
    private int findStaNosIndex(int siteId) {
        ArrayList<BasConveyorSta> staNos = getStaNo();
        int index = -1;
        for (int i = 0; i < staNos.size(); i++) {
            BasConveyorSta sta = staNos.get(i);
            if (sta.getSiteNo() == siteId) {
                index = i;
                break;
            }
        }
        return index;
    }
    /**
     * 设置入库标记
     */
    @Override
    public void setPakMk(Integer siteId, boolean pakMk) {
        StaProtocol staProtocol = station.get(siteId);
        if (null != staProtocol) {
            staProtocol.setPakMk(pakMk);
        }
    }
    private void updateWorkMode() {
        for (Map.Entry<Integer, WorkModeTypeDto> entry : workModeTypes.entrySet()) {
            WorkModeTypeDto workModeTypeDto = entry.getValue();
            WorkModeType workModeType = workModeTypeDto.getWorkModeType();
            if (workModeType != WorkModeType.NONE) {
                if (!siemensS7Net.Write(workModeTypeDto.getAddress(), workModeType.id).IsSuccess) {
                    OutputQueue.DEVP.offer(MessageFormat.format("写入输送线{1}入出库模式失败。输送线编号={0}", device.getId(), workModeTypeDto.getSiteId()));
                    log.error("写入输送线{1}入出库模式失败。输送线编号={0}", device.getId(), workModeTypeDto.getSiteId());
                }
            }
        }
    }
    @Override
    public boolean switchWorkMode(int siteId, int workMode) {
        WorkModeTypeDto workModeTypeDto = workModeTypes.get(siteId);
        if(workModeTypeDto == null) {
            return false;
        }
        workModeTypeDto.setWorkModeType(WorkModeType.get((short) workMode));
        workModeTypes.put(siteId, workModeTypeDto);
        return false;
    }
}