-
懒羊羊
2024-03-23 e20d7373595960ad6be9bd6373a3c6f186e76e5c
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,16 +15,20 @@
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("OP230","OP300","OP280", "OP320", "OP340", "OP350", "OP360","OP370", "OP390", "OP470", "OP530", "OP540");
    public static MiloService miloService;
    Map<String, Session> map = WebSocketUsers.getUsers();
    public IDaPassingStationCollectionService daPassingStationCollectionService;
@@ -72,34 +77,60 @@
                    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("1".equals(tabVlaue)||"2".equals(tabVlaue)){
//                        String Torque = thoroughfare + "." + device + ".Torque";
//                        String Angle = thoroughfare + "." + device + ".Angle";
//                        String Result = thoroughfare + "." + device + ".Result";
//                        String Result1 = thoroughfare + "." + device + ".Result1";
//                        List<String> list = new ArrayList();
//                        list.add(Torque);
//                        list.add(Angle);
//                        list.add(Result);
//                        list.add(Result1);
                        List<String> list = new ArrayList<>();
                        String[] suffixes = {"Torque", "Angle", "Result", "Result1"};
                        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 +156,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 +182,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 void SaveParamData(String packCode,String thoroughfare,String device) throws Exception {
        List<DaCollectionParamConf> list;
        DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
        daCollectionParamConf.setGatherAddress(thoroughfare+ "." + device);
@@ -175,12 +213,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 +227,10 @@
                    //ParamCollection.setState("合格");
                    ParamCollection.setType(list.get(i).getCollectParameterType());
                    ParamCollection.setCollectionTime(new Date());
                    daParamCollectionlist.add(ParamCollection);
                    daParamCollectionService.insertDaParamCollection(ParamCollection);
                }
            }
            //daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
    //            daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
        }
    }