-
懒羊羊
2024-03-29 0caf8761b4af8163a691ebb56723286926529ee4
jcdm-main/src/main/java/com/jcdm/main/plcserver/sub/OPCUaSubscription.java
@@ -1,6 +1,7 @@
package com.jcdm.main.plcserver.sub;
import com.jcdm.framework.websocket.WebSocketUsers;
import com.jcdm.main.da.collectionParamConf.domain.DaCollectionParamConf;
import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService;
import com.jcdm.main.da.paramCollection.domain.DaParamCollection;
@@ -14,22 +15,26 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.websocket.Session;
import java.util.*;
import java.util.stream.Collectors;
@Component
public class OPCUaSubscription implements SubscriptionCallback {
    //自动工位
    public List<String> automaticList = Arrays.asList("OP300","OP280", "OP320", "OP340", "OP350", "OP360","OP370", "OP390", "OP470", "OP530", "OP540");
    public static MiloService miloService;
    Map<String, Session> map = WebSocketUsers.getUsers();
    public IDaPassingStationCollectionService daPassingStationCollectionService;
    public IDaCollectionParamConfService collectionParamConfService;
    public static IDaCollectionParamConfService collectionParamConfService;
    public IDaParamCollectionService daParamCollectionService;
    public static IDaParamCollectionService daParamCollectionService;
    public OPCUaSubscription(MiloService miloService,
                             IDaPassingStationCollectionService daPassingStationCollectionService,
@@ -72,34 +77,51 @@
                    if("1".equals(tabVlaue)){
                        recordDataDoneValue = "11";
                        //校验入站是否可以工作
                        //进站保存数据
//                        inSaveDate(thoroughfare,device)
                        //记录数据完成
                        String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
                        miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(recordDataDoneValue).build());
                        miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(11).build());
                        //给前端发工件到位信号
                        if(automaticList.stream().noneMatch(s -> s.equals(device))){
                            //给前端发工件到位信号
                            WebSocketUsers.sendMessageToUserByText(map.get(device), "IN");
                        }
                        //请求工单
                    }else if("2".equals(tabVlaue)){
                        //出站保存数据
                        recordDataDoneValue = outSaveDate(thoroughfare,device);
                        //记录数据完成
                        String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
                        miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(recordDataDoneValue).build());
                    }else{
                        if(automaticList.stream().anyMatch(s -> s.equals(device))){
                            //自动工位
                            //出站保存数据
                            recordDataDoneValue = outSaveDate(thoroughfare,device);
                            //记录数据完成
                            String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
                            miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(Integer.valueOf(RecordDataDoneAddress)).build());
                        }else {
                            //手动工位
                            WebSocketUsers.sendMessageToUserByText(map.get(device), "END");
                        }
                    }else {
                        System.out.println("^");
                    }
                }
                //保存拧紧数据
                else if (("AngleResult").equals(tab)) {
                    if("OK".equals(tabVlaue)||"NG".equals(tabVlaue)){
                        List<String> list = new ArrayList<>();
                        String[] suffixes = {"Torque", "Angle", "TorqueResult", "AngleResult"};
                        for (String suffix : suffixes) {
                            String string = thoroughfare + "." + device + "." + suffix;
                            list.add(string);
                        }
                        List<ReadWriteEntity> list1 = miloService.readFromOpcUa(list);
                        List<Object> collect = list1.stream().map(ReadWriteEntity::getValue).collect(Collectors.toList());
                        String joinedString = String.join(",", collect.toString());
                        WebSocketUsers.sendMessageToUserByText(map.get(device), joinedString);
                    }
                }
            }
        } catch (Exception e) {
@@ -125,18 +147,18 @@
        String result = "";
        try {
            //读取SNCode
            String SNCodeAddress = thoroughfare + "." + device + ".SNCode";
            Object SNCodeObject = miloService.readFromOpcUa(SNCodeAddress).getValue();
            if(null == SNCodeObject || "".equals(SNCodeObject)){
            String PACKCode = thoroughfare + "." + device + ".PACKCode";
            Object PACKCodeObject = miloService.readFromOpcUa(PACKCode).getValue();
            if(null == PACKCodeObject || "".equals(PACKCodeObject)){
               result = "22";
            }else{
                String SNCode = SNCodeObject.toString();
                String PACKCodeParam = PACKCodeObject.toString();
                //1、更新工单信息
                //updateOrderInfo();
                //2、保存过站采集数据
                saveStationInfo(SNCode,thoroughfare,device);
                saveStationInfo(PACKCodeParam,thoroughfare,device);
                //3、保存参数采集数据
                SaveParamData(SNCode,thoroughfare,device);
                SaveParamData(PACKCodeParam,thoroughfare,device,"","");
                result = "21";
@@ -151,16 +173,23 @@
    /**
     * 保存过站采集
     */
    public void saveStationInfo(String SNCode,String thoroughfare,String device){
    public void saveStationInfo(String packCode,String thoroughfare,String device) throws Exception {
        String prefix = thoroughfare+"."+device+".";
        String workOrderNo = miloService.readFromOpcUa(prefix + "WorkOrderNumber").getValue().toString();
        String startTime = miloService.readFromOpcUa(prefix + "StartTime").getValue().toString();
        String stopTime = miloService.readFromOpcUa(prefix + "StopTime").getValue().toString();
        String stationStatus = miloService.readFromOpcUa(prefix + "StationStatus").getValue().toString();
        DaPassingStationCollection daPassingStationCollection = new DaPassingStationCollection();
        daPassingStationCollection.setSfcCode(SNCode);
        //daPassingStationCollection.setInboundTime();//入站时间
        daPassingStationCollection.setOutboundTime(new Date());//出站时间
        //daPassingStationCollection.setOutRsSign();//出站是否合格
        daPassingStationCollection.setSfcCode(packCode);
        daPassingStationCollection.setWorkOrderNo(workOrderNo);
//        daPassingStationCollection.setInboundTime(startTime);//入站时间
//        daPassingStationCollection.setOutboundTime(stopTime);//出站时间
        daPassingStationCollection.setOutRsSign(stationStatus);//出站是否合格
        daPassingStationCollectionService.insertDaPassingStationCollection(daPassingStationCollection);
    }
    public void SaveParamData(String SNCode,String thoroughfare,String device) throws Exception {
    public static void SaveParamData(String packCode,String thoroughfare,String device,String workOrderNo,String productType) throws Exception {
        List<DaCollectionParamConf> list;
        DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
        daCollectionParamConf.setGatherAddress(thoroughfare+ "." + device);
@@ -175,12 +204,13 @@
            List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
            List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
            for(int i=0;i<nodeIdList.size();i++){
                DaParamCollection ParamCollection = new DaParamCollection();
                if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
                    ParamCollection.setParamCode(readWriteEntityList.get(i).getIdentifier().toString().split("[.]")[2]);
                    DaParamCollection ParamCollection = new DaParamCollection();
//                    ParamCollection.setParamCode(readWriteEntityList.get(i).getIdentifier().toString().split("[.]")[2]);
                    ParamCollection.setParamCode(list.get(i).getCollectParameterId());
                    ParamCollection.setLocationCode(device);
                    ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
                    ParamCollection.setSfcCode(SNCode);
                    ParamCollection.setSfcCode(packCode);
                    ParamCollection.setParamName(list.get(i).getCollectParameterName());
                    ParamCollection.setParamUpper(list.get(i).getParamUpper());
                    ParamCollection.setParamLower(list.get(i).getParamLower());
@@ -188,10 +218,13 @@
                    //ParamCollection.setState("合格");
                    ParamCollection.setType(list.get(i).getCollectParameterType());
                    ParamCollection.setCollectionTime(new Date());
                    ParamCollection.setWorkOrderNo(workOrderNo);
                    ParamCollection.setProductCode(productType);
                    daParamCollectionlist.add(ParamCollection);
//                    daParamCollectionService.insertDaParamCollection(ParamCollection);
                }
            }
            //daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
            daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
        }
    }