严智鑫
2025-03-13 faa3b0410410415c4ad788c7e0955ef97158df2e
JiuMuMesParent/moduleService/DeviceInteractionModule/src/main/java/com/mes/plcTaskThread/MachineEdging.java
@@ -36,10 +36,11 @@
    LineConfigurationService lineConfigurationService;
    public static int thinness = 40;
    public static int edgSpeed = 40;
    public static Long machineId = 6L;
    //public static Long machineId = 6L;
    public List<Map> sendRecords = new ArrayList<>();
    public MachineEdging(TaskingMapper taskingMapper, MachineMapper machineMapper, TaskingService taskingService, PrimitiveTaskService primitiveTaskService, LineConfigurationService lineConfigurationService){
    public Machine thisMachine;
    public MachineEdging(Machine machine,TaskingMapper taskingMapper, MachineMapper machineMapper, TaskingService taskingService, PrimitiveTaskService primitiveTaskService, LineConfigurationService lineConfigurationService){
        this.thisMachine = machine;
        this.taskingMapper = taskingMapper;
        this.machineMapper = machineMapper;
        this.taskingService = taskingService;
@@ -48,8 +49,8 @@
    }
    public void plcStart() {
        Machine machine=machineMapper.selectById(machineId);
        ModbusTcp modbusTcp1 =new ModbusTcp(machine.getIp(),machine.getPort(),"PlcEdging");
        Machine machine=machineMapper.selectById(thisMachine.getId());
        ModbusTcp modbusTcp1 =new ModbusTcp(machine.getIp(),machine.getPort(),machine.getFileName());
        try {
            modbusTcp1.connect();
            PlcAgreement plcAgreement=modbusTcp1.getPlcAgreement("PlcEdging");
@@ -64,7 +65,6 @@
            PlcParameter plcRequest =plcAgreement.getPlcParameter("plcRequest");//请求字
            PlcParameter mesSend =plcAgreement.getPlcParameter("mesSend");//发送字
            PlcParameter plcReport =plcAgreement.getPlcParameter("plcReport");//汇报字
            PlcParameter plcReportID =plcAgreement.getPlcParameter("plcReportID");//汇报ID
            PlcParameter plcReport2 =plcAgreement.getPlcParameter("plcReport2");//手动汇报字
@@ -74,6 +74,21 @@
            PlcParameter mesConfirmID =plcAgreement.getPlcParameter("mesConfirmID");//确认ID
            PlcParameter mesConfirm2 =plcAgreement.getPlcParameter("mesConfirm2");//手动确认字
            PlcParameter mesConfirmID2 =plcAgreement.getPlcParameter("mesConfirmID2");//手动确认ID
            PlcParameter alarmStatus =plcAgreement.getPlcParameter("alarmStatus");//报警字
            PlcParameter machineStatusWord =plcAgreement.getPlcParameter("machineStatusWord");//设备状态字
//            if (alarmStatus.getValueInt()>0&&!"报警".equals(machine.getStatePowerOn())){
//                machine.setStatePowerOn("报警");
//            }else if(alarmStatus.getValueInt()==0&&!"报警解除".equals(machine.getStatePowerOn())){
//                machine.setStatePowerOn("报警解除");
//            }
//            if (machineStatusWord.getValueInt()!=2&&!"非联机".equals(machine.getStatePowerOn())){
//                machine.setStatePowerOn("非联机");
//            }else if(machineStatusWord.getValueInt()==2&&!"联机".equals(machine.getStatePowerOn())){
//                machine.setStatePowerOn("联机");
//            }
            if (machine.getIsLog()>0){
                modbusTcp1.consoleLogInfo(plcAgreement);
            }
@@ -180,13 +195,15 @@
            if(((1==plcRequest.getValueInt()&&0==mesSend.getValueInt())
                    &&"开工".equals(machine.getState())
                    &&taskingCount.size()<machine.getMaxTaskCount())){
                Machine loadMachine=machineMapper.selectById(2L);
                //Machine loadMachine=machineMapper.selectById(2L);
                LineConfiguration lineConfigurationBefore=lineConfigurationService.machineLineConfigurationBefore(machine);
                Machine loadMachine=machineMapper.selectById(lineConfigurationBefore.getMachineId());
                Tasking tasking;
                String scan_id="";
                if(loadMachine.getMode()==1){
                    //标准模式  找已领取未完成的第一条任务
                    LineConfiguration lineConfiguration=lineConfigurationService.machineLineConfiguration(machine);
                    List<PrimitiveTask> findPrimitiveTasking=primitiveTaskService.findPrimitiveTasking(machine,lineConfiguration.getLineId());
                    //LineConfiguration lineConfiguration=lineConfigurationService.machineLineConfiguration(machine);
                    List<PrimitiveTask> findPrimitiveTasking=primitiveTaskService.findPrimitiveTasking(machine,lineConfigurationBefore.getLineId());
                    //添加数据:
                    for(PrimitiveTask primitiveTask:findPrimitiveTasking){
                        List<Tasking> taskingTopList=taskingMapper.selectList(new QueryWrapper<Tasking>().lambda()
@@ -194,6 +211,7 @@
                        );
                        //任务数>已上数量
                        if(taskingTopList.size()<primitiveTask.getTaskQuantity()){
                            LineConfiguration lineConfiguration=lineConfigurationService.machineLineConfiguration(machine);
                            Tasking taskingAdd=primitiveTaskService.convertListTasking(primitiveTask,lineConfiguration.getId());
                            Integer taskSequence=1;
                            if (machine.getTodayCount()>0&&machine.getTodayCount()<5000){
@@ -211,7 +229,9 @@
                    tasking=taskingService.startMachineTask(machine);
                }else{
                    //定制模式
                    String PlcRequestID_=clientTCPSocket();
                    LineConfiguration lineConfigurationAfter=lineConfigurationService.machineLineConfigurationBefore(machine);
                    Machine scanMachine=machineMapper.selectById(lineConfigurationAfter.getMachineId());
                    String PlcRequestID_=clientTCPSocket(scanMachine);
                    //String PlcRequestID_=",,46408889";
                    if (PlcRequestID_==null||PlcRequestID_.lastIndexOf(",")<0){
                        modbusTcp1.close();
@@ -225,7 +245,7 @@
                    log.info("4、发送任务  任务数据:{}, 设备状态:{}, ",machine,tasking);
                    String send=
                            HexUtil.intTo2ByteHex(1)+
                                    HexUtil.intTo2ByteHex(tasking.getGlassId().intValue())+
                                    HexUtil.intTo2ByteHex(tasking.getTaskSequence().intValue())+
                                    HexUtil.intTo2ByteHex(((Double)(tasking.getLength()*10)).intValue())+
                                    HexUtil.intTo2ByteHex(((Double)(tasking.getWidth()*10)).intValue())+
                                    HexUtil.intTo2ByteHex(((Double)(tasking.getThickness()*10)).intValue());
@@ -236,6 +256,7 @@
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String sendRecord=df.format(new Date())+"  "+
                            tasking.getGlassId()+"-"+
                            tasking.getTaskSequence()+"-"+
                            tasking.getWidth()+"-"+
                            tasking.getLength()+"-"+
                            tasking.getThickness();
@@ -251,13 +272,14 @@
                    modbusTcp1.close();
                    return;
                }
            }else if(0==plcRequest.getValueInt()){
                errorMachine(machine,"");
            }
            //}
        } catch (Exception e) {
            log.info("交互逻辑错误");
        }
        modbusTcp1.close();
        //log.info("无可执行的条件");
    }
@@ -269,9 +291,9 @@
            machineMapper.updateById(machine);
        }
    }
    public String clientTCPSocket() {
    public String clientTCPSocket(Machine machine) {
        try {
            Machine machine=machineMapper.selectById(4L);
            //Machine machine=machineMapper.selectById(4L);
            Socket clientSocket = new Socket(machine.getIp(), machine.getPort());
            try {
                clientSocket.setSoTimeout(1000);
@@ -292,11 +314,12 @@
                clientSocket.close();
                return receivedMessage;
            } catch (Exception e) {
                e.printStackTrace();
                log.info("扫码异常");
                //e.printStackTrace();
            }
            clientSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.info("ip或端口异常");
        }
        return "";
    }