long
2023-09-02 e379948bb83ebcc9ce0c467a7b970f74bc00e9a1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cn.stylefeng.guns.plcserver.server.BOP040.impl;
 
 
import cn.stylefeng.guns.modular.bs.collectionParamConf.model.params.CollectionParamConfParam;
import cn.stylefeng.guns.modular.bs.collectionParamConf.model.result.CollectionParamConfResult;
import cn.stylefeng.guns.modular.bs.collectionParamConf.service.CollectionParamConfService;
import cn.stylefeng.guns.modular.cm.paramCollection.model.params.ParamCollectionParam;
import cn.stylefeng.guns.modular.cm.paramCollection.service.ParamCollectionService;
import cn.stylefeng.guns.modular.cm.passingStationCollection.model.params.PassingStationCollectionParam;
import cn.stylefeng.guns.modular.cm.passingStationCollection.service.PassingStationCollectionService;
import cn.stylefeng.guns.plcserver.opc.GlobalVariable;
import cn.stylefeng.guns.plcserver.opc.OPCComm;
import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit;
 
import cn.stylefeng.guns.plcserver.server.BOP040.BOP040ServerInterface;
import cn.stylefeng.guns.plcserver.tool.DateTool;
import cn.stylefeng.guns.plcserver.tool.LogUtil;
import org.apache.log4j.Logger;
import org.openscada.opc.lib.da.Group;
import org.openscada.opc.lib.da.Item;
 
import java.util.Date;
import java.util.List;
 
 
public class BOP040ServerInterfaceImpl implements BOP040ServerInterface {
 
    private static Logger logger;
    LogUtil logUtil = new LogUtil();
    static {
        logger = Logger.getLogger(BOP040ServerInterfaceImpl.class);
    }
 
    private OPCComm opc = OPCComm.getInstance();
    private static Date IN_TIME = null;
    private static String S_ORDER_CODE = "";//工单编号
    private static String S_PRODUCT_CODE = "";//产品编号
    private static String S_PRODUCT_TYPE= "";//产品类型
    private static String S_PRODUCT_CODE_OLD = "";//产品编号
    private static String S_SFC_CODE = "";//总成编码
    private static String S_RIGHT_CODE  = "";//右壳体编码
    private static String PRODUCTION_LINE  = "BOP";//产线号
    private static String LOCATION_CODE  = "BOP040";//工位号
    private static String GROUP_NAME  = "BOP040_P";//组名
    private static List list = null;
    private static Group group = null;
    @Override
    public synchronized void monitor() {
        String ecpStr = "";//异常记录标记
        try{
            if(GlobalVariable.BOP040_F_HEART_BEAT) {
                //PC读工单
                String isPLCStart = OPCUnit.read(opc.getBOP040_J_PLC_START());//PLC开始
                if(isPLCStart.equals("true")){
                    String isFinish = OPCUnit.read(opc.getBOP040_J_PC_WRITE_ORDER());//PC下发工单完成
                    if(isFinish.equals("false")){
                        handleOrder();
                    }
                }
 
 
                //PC完成
                String isPLCFinish = OPCUnit.read(opc.getBOP040_J_PLC_FINISH());//PLC完成
                if(isPLCFinish.equals("true")){
                    String isFinish = OPCUnit.read(opc.getBOP040_J_PC_FINISH());//PC操作完成
                    if(isFinish.equals("false")){
                        handleInfo();
                        handleFinsh();
                    }
                }
 
            }else{
                logger.info("BOP040工位与PLC断开,请检查PLC或网络是否异常!");
            }
 
        }catch (Exception e) {
            ecpStr ="出现异常:" + logUtil.getTrace(e);
        }finally {
            if(!"".equals(ecpStr)){
                logger.info(ecpStr+"\r\n");
            }
        }
    }
 
    //PC读工单
    public void handleOrder() {
        IN_TIME = DateTool.getLocalTimeForDate();
        S_ORDER_CODE = OPCUnit.read(opc.getBOP040_S_ORDER_CODE());//工单编号
        S_PRODUCT_TYPE = OPCUnit.read(opc.getBOP040_S_PRODUCT_CODE());//产品编号
        S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
        S_SFC_CODE = OPCUnit.read(opc.getBOP040_S_SFC_CODE());//总成编码
        OPCUnit.write(opc.getBOP040_J_PC_WRITE_ORDER(), "true"); //PC读工单完成
        logger.info("BOP040工位,handleOrder PC处理完成!");
    }
 
    //PC读参数信息
    public void handleInfo() throws Exception {
        logger.info("BOP040工位,handleInfo START!");
 
        if(null == list /*|| !S_PRODUCT_CODE_OLD.equals(S_PRODUCT_CODE)*/){
            if(group != null){
                opc.getServer().removeGroup(group,true);
            }
            group = opc.getServer().addGroup(GROUP_NAME);
 
            CollectionParamConfParam collectionParamConfParam = new CollectionParamConfParam();
            //collectionParamConfParam.setProductCode(S_PRODUCT_CODE);
            collectionParamConfParam.setLocationCode(LOCATION_CODE);
            CollectionParamConfService collectionParamConfService = opc.collectionParamConfService;
            list = collectionParamConfService.findListBySpec(collectionParamConfParam);
 
 
            for(int i=0;i<list.size();i++){
                CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
                Item item = group.addItem(result.getGatherAddress());
                result.setItem(item);
                list.set(i,result);
            }
            //S_PRODUCT_CODE_OLD = S_PRODUCT_CODE;
        }
 
        if(null != list && list.size()>0) {
            ParamCollectionService paramCollectionService = opc.paramCollectionService;
            for (int i = 0; i < list.size(); i++) {
                CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
                Item item = result.getItem();
                String value = OPCUnit.read(item);//读参数值
                ParamCollectionParam paramCollectionParam = new ParamCollectionParam();
                paramCollectionParam.setParamCode(result.getParamCode());
                paramCollectionParam.setParamName(result.getParamName());
                paramCollectionParam.setWorkOrderNo(S_ORDER_CODE);
                paramCollectionParam.setSfcCode(S_SFC_CODE);
                paramCollectionParam.setProductCode(S_PRODUCT_CODE);
                paramCollectionParam.setProductionLine(result.getProductionLine());
                paramCollectionParam.setLocationCode(result.getLocationCode());
                paramCollectionParam.setParamValue(value);
                paramCollectionParam.setParamLower(result.getParamLower());
                paramCollectionParam.setParamUpper(result.getParamUpper());
                paramCollectionParam.setParamStandard(result.getParamCentral());
                paramCollectionParam.setCollectionTime(DateTool.getLocalTimeForDate());
                paramCollectionParam.setUnit(result.getUnit());
                paramCollectionService.add(paramCollectionParam);
            }
        }
 
        logger.info("BOP040工位,handleInfo END!");
    }
 
    //PC完成
    public void handleFinsh() {
        if("".equals(S_ORDER_CODE)){
            IN_TIME = DateTool.getLocalTimeForDate();
            S_ORDER_CODE = OPCUnit.read(opc.getBOP040_S_ORDER_CODE());//工单编号
            S_PRODUCT_TYPE = OPCUnit.read(opc.getBOP040_S_PRODUCT_CODE());//产品编号
            S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
            S_SFC_CODE = OPCUnit.read(opc.getBOP040_S_SFC_CODE());//总成编码
        }
        //处理过站信息
        String S_PRODUCT_STATE_CODE = OPCUnit.read(opc.getBOP040_S_PRODUCT_STATE_CODE());
        PassingStationCollectionParam param = new PassingStationCollectionParam();
        param.setWorkOrderNo(S_ORDER_CODE);
        param.setProductCode(S_PRODUCT_CODE);
        param.setSfcCode(S_SFC_CODE);
        param.setProductionLine(PRODUCTION_LINE);
        param.setLocationCode(LOCATION_CODE);
        param.setInboundTime(IN_TIME);
        param.setOutboundTime(DateTool.getLocalTimeForDate());
        param.setOutRsSign(S_PRODUCT_STATE_CODE);//出站是否合格
        PassingStationCollectionService passingStationCollectionService = opc.passingStationCollectionService;
        passingStationCollectionService.add(param);
 
        OPCUnit.write(opc.getBOP040_J_PC_FINISH(), "true"); //PC完成
 
        logger.info("BOP040工位,handleFinsh PC处理完成!");
    }
 
}