cl
2024-01-19 e0fac38b26845f25de479783e0c76cf12a5311e0
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package cn.stylefeng.guns.plcserver.server.COP010.impl;
 
 
import cn.stylefeng.guns.modular.bs.locationInfo.model.params.LocationInfoParam;
import cn.stylefeng.guns.modular.bs.locationInfo.model.result.LocationInfoResult;
import cn.stylefeng.guns.modular.bs.locationInfo.service.LocationInfoService;
import cn.stylefeng.guns.modular.cm.passingStationCollection.model.params.PassingStationCollectionParam;
import cn.stylefeng.guns.modular.cm.passingStationCollection.service.PassingStationCollectionService;
import cn.stylefeng.guns.modular.dq.materialTraceability.model.params.MaterialTraceabilityParam;
import cn.stylefeng.guns.modular.dq.materialTraceability.service.MaterialTraceabilityService;
import cn.stylefeng.guns.modular.om.productionOrdeInfo.model.params.ProductionOrdeInfoParam;
import cn.stylefeng.guns.modular.om.productionOrdeInfo.model.result.ProductionOrdeInfoResult;
import cn.stylefeng.guns.modular.om.productionOrdeInfo.service.ProductionOrdeInfoService;
import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.params.ProductionOrderBatchInfoParam;
import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.result.ProductionOrderBatchInfoResult;
import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.service.ProductionOrderBatchInfoService;
import cn.stylefeng.guns.modular.sc.repairManageInfo.model.params.RepairManageInfoParam;
import cn.stylefeng.guns.modular.sc.repairManageInfo.model.result.RepairManageInfoResult;
import cn.stylefeng.guns.modular.sc.repairManageInfo.service.RepairManageInfoService;
import cn.stylefeng.guns.modular.sc.serialNumbersConf.model.params.SerialNumbersConfParam;
import cn.stylefeng.guns.modular.sc.serialNumbersConf.model.result.SerialNumbersConfResult;
import cn.stylefeng.guns.modular.sc.serialNumbersConf.service.SerialNumbersConfService;
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.opc.unit.qrCode;
import cn.stylefeng.guns.plcserver.server.COP010.COP010ServerInterface;
import cn.stylefeng.guns.plcserver.tool.DateTool;
import cn.stylefeng.guns.plcserver.tool.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.openscada.opc.lib.da.Group;
 
import java.util.Date;
import java.util.List;
import java.util.Random;
 
 
public class COP010ServerInterfaceImpl implements COP010ServerInterface {
 
    private static Logger logger;
    LogUtil logUtil = new LogUtil();
    static {
        logger = Logger.getLogger(COP010ServerInterfaceImpl.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  = "COP";//产线号
    private static String LOCATION_CODE  = "COP010";//工位号
    private static String GROUP_NAME  = "COP010_P";//工位号
    private static List list = null;
    private static Group group = null;
 
    private int count = 0;
 
    /**
     * 设置心跳标志位,保存到全局变量
     */
    public void setHeartbeat(String value) {
        if ("false".equals(value)) {
            OPCUnit.write(opc.getCOP010_F_HEART_BEAT(), "true");
            GlobalVariable.COP010_F_HEART_BEAT = true;
            count = 0;
        } else {
            count++;
        }
        // 循环读取频率是:500ms一次,如果超过6次,也就是3s认定超时
        if (count > GlobalVariable.HEART_MAX_COUNT) {
            GlobalVariable.COP010_F_HEART_BEAT = false;
        }
    }
 
    @Override
    public synchronized void monitor() {
        String ecpStr = "";//异常记录标记
        try{
        /*    String isHeartBeat = OPCUnit.read(opc.getCOP010_F_HEART_BEAT());
            setHeartbeat(isHeartBeat);*/
 
 
            if(GlobalVariable.COP010_F_HEART_BEAT) {
 
                //PC读工单
                String isPLCStart = OPCUnit.read(opc.getCOP010_J_PLC_START());//PLC开始
                if(isPLCStart.equals("true")){
                    String isFinish = OPCUnit.read(opc.getCOP010_J_PC_WRITE_ORDER());//PC下发工单完成
                    if(isFinish.equals("false")){
                        String isRepair = OPCUnit.read(opc.getCOP010_P_REPAIR());//是否返修
                        if(isRepair.equals("true")){
                            handleRepair();
                        }else{
                            handleOrder();
                        }
                    }
                }
 
 
                //PC完成
                String isPLCFinish = OPCUnit.read(opc.getCOP010_J_PLC_FINISH());//PLC完成
                if(isPLCFinish.equals("true")){
                    String isFinish = OPCUnit.read(opc.getCOP010_J_PC_FINISH());//PC操作完成
                    if(isFinish.equals("false")){
                        handleFinsh();
                        handleMaterial();
                    }
                }
 
 
            }else{
                logger.info("COP010工位与PLC断开,请检查PLC或网络是否异常!");
            }
 
        }catch (Exception e) {
            ecpStr ="出现异常:" + logUtil.getTrace(e);
        }finally {
            if(!"".equals(ecpStr)){
                logger.info(ecpStr+"\r\n");
            }
        }
    }
 
    public void handleRepair() {
        RepairManageInfoParam repairManageInfoParam = new RepairManageInfoParam();
        repairManageInfoParam.setLocationCode(LOCATION_CODE);
        repairManageInfoParam.setManageType("2");
        repairManageInfoParam.setUpPlc("0");
        RepairManageInfoService repairManageInfoService = opc.repairManageInfoService;
        List<RepairManageInfoResult> list = repairManageInfoService.findListBySpec(repairManageInfoParam);
        if(list.size()>0) {
            RepairManageInfoResult result = list.get(0);
 
            //自动生成唯一码
            Random random = new Random(System.currentTimeMillis());
            S_SFC_CODE = "COP" + random.nextLong();
 
 
            S_ORDER_CODE = result.getWorkOrderNo();
            S_PRODUCT_CODE = result.getProductCode();
            S_PRODUCT_TYPE = GlobalVariable.PRODUCT_TYPE_MAP.get(S_PRODUCT_CODE);
            String  repairLocation = result.getPoorLocationCode();//
 
            //根据工位编码查询工位表中配置的返修工位号
            LocationInfoParam param = new LocationInfoParam();
            param.setLocationCode(repairLocation);
            LocationInfoService locationInfoService = opc.locationInfoService;
            List<LocationInfoResult> results = locationInfoService.findListBySpec(param);
            if(results.size()>0) {
                LocationInfoResult r = results.get(0);
                String repairLocationNo = r.getSpareField2();
                if(null != repairLocationNo && "" != repairLocationNo){
                    OPCUnit.write(opc.getCOP010_S_ORDER_CODE(),S_ORDER_CODE); //工单编号
                    OPCUnit.write(opc.getCOP010_S_PRODUCT_CODE(), S_PRODUCT_TYPE); //产品编号
                    OPCUnit.write(opc.getCOP010_S_SFC_CODE(), S_SFC_CODE); //总成编码
                    OPCUnit.write(opc.getCOP010_P_REPAIR_LOCATION(), repairLocationNo); //返修工位号
                    OPCUnit.write(opc.getCOP010_J_PC_WRITE_ORDER(), "true"); //PC下发工单完成
 
                    //更新为已经返修上线
                    Long id = result.getId();
                    repairManageInfoParam.setId(id);
                    repairManageInfoParam.setUpPlc("1");
                    repairManageInfoService.update(repairManageInfoParam);
 
                }else{
                    logger.info("COP010工位,handleRepair 请查询"+repairLocation+"工位是否配置返修工位号!");
                }
            }else{
                logger.info("COP010工位,handleRepair 无法查询到"+repairLocation+"工位!");
            }
        }
    }
 
    //PC下发工单
    public void handleOrder() {
 
        IN_TIME = DateTool.getLocalTimeForDate();
        ProductionOrdeInfoParam param = new ProductionOrdeInfoParam();
        param.setProductionLine("");//产线
        param.setOrderStatus("3");//工单状态
 
        ProductionOrdeInfoService productionOrdeInfoService = opc.productionOrdeInfoService;
        List<ProductionOrdeInfoResult> list = productionOrdeInfoService.findListBySpec(param);
 
        if(list.size()>0) {
            ProductionOrdeInfoResult result = list.get(0);
 
            S_ORDER_CODE = result.getWorkOrderNo();
            S_PRODUCT_CODE = result.getMaterialCode();
            S_PRODUCT_TYPE = GlobalVariable.PRODUCT_TYPE_MAP.get(S_PRODUCT_CODE);
 
            //自动生成唯一码
            Random random = new Random(System.currentTimeMillis());
            S_SFC_CODE = "COP"+ random.nextLong();
 
 
            OPCUnit.write(opc.getCOP010_S_ORDER_CODE(),S_ORDER_CODE); //工单编号
            OPCUnit.write(opc.getCOP010_S_PRODUCT_CODE(), S_PRODUCT_TYPE); //产品类型
            OPCUnit.write(opc.getCOP010_S_SFC_CODE(), S_SFC_CODE); //总成编码
           // OPCUnit.write(opc.getCOP010_S_RIGHT_CODE(), S_RIGHT_CODE); //右壳体编码
            OPCUnit.write(opc.getCOP010_S_NUMBER(), result.getPlanQty().toString()); //计划数量
 
            OPCUnit.write(opc.getCOP010_J_PC_WRITE_ORDER(), "true"); //PC下发工单完成
            logger.info("COP010工位,handleOrder PC处理完成!2");
        }else{
            logger.info("COP010工位没有可执行的工单,请联系工单管理员下发该产线的工单!");
        }
    }
 
    public void handleMaterial() {
 
        //查询批次上料信息
        ProductionOrderBatchInfoParam productionOrderBatchInfoParam = new ProductionOrderBatchInfoParam();
        productionOrderBatchInfoParam.setWorkOrderNo(S_ORDER_CODE);
        productionOrderBatchInfoParam.setLocationCode(LOCATION_CODE);
        ProductionOrderBatchInfoService productionOrderBatchInfoService = opc.productionOrderBatchInfoService;
        List<ProductionOrderBatchInfoResult> list1 = productionOrderBatchInfoService.findListBySpec(productionOrderBatchInfoParam);
        for(int i=0;i<list1.size();i++){
            ProductionOrderBatchInfoResult productionOrderBatchInfoResult = list1.get(i);
 
 
            int remainingQuantity =  productionOrderBatchInfoResult.getResidueQuantity();//剩余数量
            if(remainingQuantity>0 ){
                //新增物料追溯
                MaterialTraceabilityParam materialTraceabilityParam = new MaterialTraceabilityParam();
                materialTraceabilityParam.setWorkOrderNo(S_ORDER_CODE);
                materialTraceabilityParam.setProductCode(S_PRODUCT_CODE);
                materialTraceabilityParam.setProductNo(S_SFC_CODE);
                materialTraceabilityParam.setLineCode(PRODUCTION_LINE);
                materialTraceabilityParam.setLocationCode(LOCATION_CODE);
                materialTraceabilityParam.setAssemblyQty(1);
                materialTraceabilityParam.setAssemblyTime(DateTool.getLocalTimeForDate());
                materialTraceabilityParam.setMaterialCode(productionOrderBatchInfoResult.getMaterialCode());//物料编码
                materialTraceabilityParam.setMaterialBatchNo(productionOrderBatchInfoResult.getBatch());//物料批次
 
                MaterialTraceabilityService materialTraceabilityService = opc.materialTraceabilityService;
                materialTraceabilityService.add(materialTraceabilityParam);
                //更新剩余数量
                productionOrderBatchInfoParam.setId(productionOrderBatchInfoResult.getId());
                productionOrderBatchInfoParam.setResidueQuantity(remainingQuantity-1);//剩余数量
                productionOrderBatchInfoService.update(productionOrderBatchInfoParam);
            }
        }
    }
 
    //PC完成
    public void handleFinsh() {
        if("".equals(S_ORDER_CODE)){
            IN_TIME = DateTool.getLocalTimeForDate();
            S_ORDER_CODE = OPCUnit.read(opc.getCOP010_S_ORDER_CODE());//工单编号
            S_PRODUCT_TYPE = OPCUnit.read(opc.getCOP010_S_PRODUCT_CODE());//产品编号
            S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
            S_SFC_CODE = OPCUnit.read(opc.getCOP010_S_SFC_CODE());//总成编码
        }
        //处理过站信息
        String S_PRODUCT_STATE_CODE = OPCUnit.read(opc.getCOP010_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.getCOP010_J_PC_FINISH(), "true"); //PC完成
 
        logger.info("COP010工位,handleFinsh PC处理完成!");
    }
 
 
}