zhoushihao
2024-05-09 9e97ded305585c0bdc22aef3468dd031567c38a3
hangzhoumesParent/moduleService/CacheGlassModule/src/main/java/com/mes/job/CacheGlassTask.java
@@ -1,13 +1,13 @@
package com.mes.job;
import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONObject;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mes.common.S7object;
import com.mes.common.WebSocketServer;
import com.mes.common.config.Const;
import com.mes.device.PlcParameterObject;
import com.mes.edgstoragecage.entity.EdgStorageCage;
import com.mes.edgstoragecage.entity.EdgStorageCageDetails;
import com.mes.edgstoragecage.mapper.EdgStorageCageDetailsMapper;
import com.mes.edgstoragecage.service.EdgStorageCageDetailsService;
import com.mes.edgstoragecage.service.EdgStorageCageService;
import com.mes.glassinfo.entity.GlassInfo;
@@ -15,15 +15,14 @@
import com.mes.taskcache.entity.TaskCache;
import com.mes.taskcache.service.TaskCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * @Author : zhoush
@@ -42,161 +41,124 @@
    EdgStorageCageService edgStorageCageService;
    @Autowired
    EdgStorageCageDetailsService edgStorageCageDetailsService;
    @Resource
    private EdgStorageCageDetailsMapper edgStorageCageDetailsMapper;
    private Map<String, String> mapParameter = new HashMap<>();
    private Map<String, String> mapValue = new HashMap<>();
    private Map<String, String> mapType = new HashMap<>();
    private Map<String, Object> mapSettings = new HashMap<>();
    @Value("${mes.threshold}")
    private int threshold;
    @Scheduled(fixedDelay = 1000)
    @Scheduled(fixedDelay = 10000)
    public void plcHomeEdgTask() {
        initialize();
        log.info("当前参数值:{}", mapValue);
        JSONObject jsonObject = new JSONObject();
//        for (String key : mapParameter.keySet()) {
//            String value = "";
//            if ("String".equals(mapType.get(key))) {
//                value = S7object.getinstance().plccontrol.readString(mapParameter.get(key));
//            } else {
//                value = S7object.getinstance().plccontrol.readWord(mapParameter.get(key)) + "";
//            }
//            mapValue.put(key, value);
//        }
        PlcParameterObject plcParameterObject = S7object.getinstance().PlcMesObject;
        if ("0".equals(mapValue.get("A06_request_word"))) {
            log.info("获取到的请求字为0,将确认字改为0");
            //清除
            S7object.getinstance().plccontrol.WriteWord(mapParameter.get("MES_confirmation_word"), (short) 0);
        } else if ("1".equals(mapValue.get("A06_request_word")) &&
                "0".equals(mapValue.get("MES_confirmation_word"))) {
            log.info("进片请求,且确认字为0,执行进片任务");
            inTo();
        } else if ("2".equals(mapValue.get("A06_request_word")) &&
                "0".equals(mapValue.get("MES_confirmation_word")) && !"0".equals(mapValue.get("A09_glass_status"))) {
        String taskRequestTypeValue = plcParameterObject.getPlcParameter("A06_request_word").getValue();
        String glassIdeValue = plcParameterObject.getPlcParameter("A05_scanning_ID").getValue();
        String confirmationWrodValue = plcParameterObject.getPlcParameter("MES_confirmation_word").getValue();
//        1为A09空闲,2为A10空闲,3为A09A10都空闲,0为A09A10都有玻璃
        String outGlassstate = plcParameterObject.getPlcParameter("A09_glass_status").getValue();
        String confirmationWrodAddress = plcParameterObject.getPlcParameter("MES_confirmation_word").getAddress();
        if ("0".equals(taskRequestTypeValue)) {
            log.info("1、获取到的请求字为0,将确认字改为0");
            S7object.getinstance().plccontrol.WriteWord(confirmationWrodAddress, (short) 0);
            return;
        }
        if (!"0".equals(confirmationWrodValue)) {
            log.info("1、获取到的请求字不为0,将确认字改为0");
            S7object.getinstance().plccontrol.WriteWord(confirmationWrodAddress, (short) 0);
            return;
        }
        if ("1".equals(taskRequestTypeValue)) {
            log.info("2、进片请求,且确认字为0,执行进片任务");
            inTo(glassIdeValue, confirmationWrodAddress);
        } else if ("2".equals(taskRequestTypeValue)) {
            //09空闲 :1      10空闲 :2        都空闲:3    其他0
            log.info("出片请求,且确认字为0,执行进片任务");
            outTo(Integer.parseInt(mapValue.get("A09_glass_status")));
        } else if ("3".equals(mapValue.get("A06_request_word")) &&
                "0".equals(mapValue.get("MES_confirmation_word"))) {
            if ("0".equals(mapValue.get("A09_glass_status")) || "0".equals(mapValue.get("A10_glass_status"))) {
                outTo(Integer.parseInt(mapValue.get("A09_glass_status")));
            log.info("2、出片请求,且确认字为0,执行进片任务");
            outTo(Integer.parseInt(outGlassstate), confirmationWrodAddress);
        } else if ("3".equals(taskRequestTypeValue)) {
            log.info("2、进片和出片都空闲,执行出片任务");
            if ("0".equals(outGlassstate)) {
                inTo(glassIdeValue, confirmationWrodAddress);
            } else {
                inTo();
            }
        }
//            log.info("推数据");
        // jsonObject.append("params", new short[] { 30, 40, });
        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("Home");
        if (sendwServer != null) {
            for (WebSocketServer webserver : sendwServer) {
                webserver.sendMessage(jsonObject.toString());
                if (webserver != null) {
                    List<String> messages = webserver.getMessages();
                    if (!messages.isEmpty()) {
                        // // 将最后一个消息转换为整数类型的列表
                        webserver.clearMessages();
                    }
                }
                outTo(Integer.parseInt(outGlassstate), confirmationWrodAddress);
            }
        }
    }
    /**
     * 初始化plc数据
     */
    private void initialize() {
//        mapParameter.put("A06_request_word", "DB11.0");   //work
//        mapParameter.put("A05_scanning_ID", "DB11.2");
//        mapParameter.put("MES_confirmation_word", "DB11.40");
//        mapParameter.put("A09_glass_status", "DB11.70");
//        //mapParameter.put("A10_glass_status", "DB11.72");
//        mapParameter.put("A09_prohibit_film_production", "DB11.74");
//        mapParameter.put("A10_prohibit_film_production", "DB11.76");
//        mapSettings.put("territoryPoor", 3);
//        mapType.put("A06_request_word", "Word");
//        mapType.put("A05_scanning_ID", "String");
//        mapType.put("MES_confirmation_word", "Word");
//        mapType.put("A09_glass_status", "Word");
//        //mapType.put("A10_glass_status", "Word");
//        mapType.put("A09_prohibit_film_production", "Word");
//        mapType.put("A10_prohibit_film_production", "Word");
        mapValue.put("A06_request_word", "2");//请求字
        mapValue.put("A05_scanning_ID", "11111111111");//请求ID
        mapValue.put("MES_confirmation_word", "0");//MES发送字
        mapValue.put("A09_glass_status", "1");//A09玻璃状态
        mapValue.put("A10_glass_status", "0");//A09玻璃状态
        mapValue.put("A09_prohibit_film_production", "0");//A9禁止出片
        mapValue.put("A10_prohibit_film_production", "0");//A10禁止出片
    }
    private void inTo() {
        List<GlassInfo> glassInfos = glassInfoService.selectId(mapValue.get("A05_scanning_ID"));
        if (glassInfos.size() < 1) {
            log.info("此玻璃编号不存在");
        } else if (glassInfos.size() == 1) {
            //添加进片任务  查找空格
            log.info("正常");
            List<EdgStorageCage> list = edgStorageCageService.selectCacheEmpty();
            if (list.size() > 1) {
                EdgStorageCageDetails edgStorageCageDetails = edgStorageCageDetailsMapper.selectOne(new MPJLambdaWrapper<EdgStorageCageDetails>()
                        .eq(EdgStorageCageDetails::getGlassId, glassInfos.get(0).getGlassId()));
                edgStorageCageDetails.setState(200);
                TaskCache taskCache = new TaskCache();
                EdgStorageCage edgStorageCage = list.get(0);
                taskCache.setGlassId(glassInfos.get(0).getGlassId());
                taskCache.setTaskStatus(0);
                taskCache.setStartCell(0);
                taskCache.setEndCell(edgStorageCage.getSlot());
                taskCache.setTaskType(1);
                taskCache.setCreateTime(new DateTime());
                edgStorageCageDetailsMapper.updateById(edgStorageCageDetails);
                taskCacheService.insertTaskCache(taskCache);
//                S7object.getinstance().plccontrol.WriteWord("DB11.40", (short) 1);
            } else {
                log.info("不存在空格");
            }
        } else {
            log.info("此玻璃编号存在多个");
    private void inTo(String glassId, String confirmationWrodAddress) {
        log.info("1、按照玻璃id:{}获取玻璃小片信息", glassId);
        GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper<GlassInfo>().eq(GlassInfo::getGlassId, glassId));
        if (glassInfo == null) {
            log.info("2、此玻璃编号不存在");
            return;
        }
        log.info("2、获取到的玻璃信息为{}", glassInfo);
        //添加进片任务  查找空格
        List<EdgStorageCage> list = edgStorageCageService.selectCacheEmpty();
        log.info("3、查询卧式理片笼里面的空格:{}", list);
        if (CollectionUtil.isEmpty(list)) {
            log.info("4、不存在空格");
            return;
        }
        log.info("4、将玻璃信息插入卧式理片笼:{}", glassInfo);
        EdgStorageCageDetails details = new EdgStorageCageDetails();
        BeanUtils.copyProperties(glassInfo, details);
        details.setState(100);
        Integer slot = list.get(0).getSlot();
        Integer deviceId = list.get(0).getDeviceId();
        log.info("5、获取空闲格子为:{},设备号为{}", slot, deviceId);
        details.setSlot(slot);
        details.setDeviceId(deviceId);
        edgStorageCageDetailsService.save(details);
        log.info("6、生成进片任务信息存入任务表");
        TaskCache taskCache = new TaskCache();
        taskCache.setGlassId(glassId);
        taskCache.setTaskStatus(0);
        taskCache.setStartCell(0);
        taskCache.setEndCell(slot);
        taskCache.setTaskType(1);
        taskCache.setCreateTime(new Date());
        taskCacheService.insertTaskCache(taskCache);
        log.info("6、发送确认字");
        S7object.getinstance().plccontrol.WriteWord(confirmationWrodAddress, (short) 1);
    }
    private void outTo(int line) {
    private void outTo(int line, String confirmationWrodAddress) {
        Assert.isTrue(line != 0, "A09、A10都有玻璃,无法出片");
        log.info("1、出片任务出【{}】号线,备注(09空闲:1;10空闲:2;都空闲:3)", line);
        int endcell = 0;
        EdgStorageCageDetails outEdgStorageCageDetails = new EdgStorageCageDetails();
        TaskCache taskCacheA09 = taskCacheService.selectLastOutCacheInfo("");
        TaskCache taskCacheA10 = taskCacheService.selectLastOutCacheInfo("");
        //两线各自要出的玻璃
        EdgStorageCageDetails glassInfo09 = edgStorageCageDetailsService.selectConformGlass(taskCacheA09.getGlassId(), (int) mapSettings.get("territoryPoor"));
        EdgStorageCageDetails glassInfo10 = edgStorageCageDetailsService.selectConformGlass(taskCacheA10.getGlassId(), (int) mapSettings.get("territoryPoor"));
        //决定线路
        if (line == 1) {
            endcell = 9000;
            outEdgStorageCageDetails = glassInfo09;
        } else if (line == 2) {
            endcell = 1000;
            outEdgStorageCageDetails = glassInfo10;
        } else if (line == 3) {
            endcell = 9000;//默认走一号线  优化方向可根据  对比两线速度/两线当前任务情况做
            outEdgStorageCageDetails = glassInfo09;
        List<TaskCache> oldTaskCacheList;
        if (line == 2) {
            endcell = Const.A10_OUT_TARGET_POSITION;
            oldTaskCacheList = taskCacheService.selectLastOutCacheInfos(Const.A10_OUT_TARGET_POSITION);
        } else {
            endcell = Const.A09_OUT_TARGET_POSITION;
            oldTaskCacheList = taskCacheService.selectLastOutCacheInfos(Const.A09_OUT_TARGET_POSITION);
        }
        if (outEdgStorageCageDetails != null && endcell > 0) {
        log.info("2、判断出片线路{},获取最后该条线已出片的任务信息{}(备注:0待执行,1已执行).都空闲优先出A09线", endcell, oldTaskCacheList);
        String glassId = "";
        if (CollectionUtil.isNotEmpty(oldTaskCacheList)) {
            glassId = oldTaskCacheList.get(0).getGlassId();
        }
        EdgStorageCageDetails glassInfo = edgStorageCageDetailsService.selectOutGlass(glassId, threshold);
        log.info("3、按照历史已出玻璃id{}和阈值{},拿出理片笼内的出片信息{}", glassId, threshold, glassInfo);
        if (glassInfo != null) {
            log.info("4、添加出片任务,玻璃id:{},任务类型:{},起始位置:{},结束位置:{}", glassInfo.getGlassId(),
                    2, glassInfo.getSlot(), endcell);
            TaskCache taskCache = new TaskCache();
            taskCache.setGlassId(outEdgStorageCageDetails.getGlassId());
            taskCache.setGlassId(glassInfo.getGlassId());
            taskCache.setTaskStatus(0);
            taskCache.setStartCell(outEdgStorageCageDetails.getSlot());
            taskCache.setStartCell(glassInfo.getSlot());
            taskCache.setEndCell(endcell);
            taskCache.setTaskType(2);
            taskCache.setCreateTime(new DateTime());
            taskCache.setCreateTime(new Date());
            taskCacheService.insertTaskCache(taskCache);
//            S7object.getinstance().plccontrol.WriteWord(mapParameter.get("MES_confirmation_word"), (short) 1);
            S7object.getinstance().plccontrol.WriteWord(confirmationWrodAddress, (short) 1);
        }