cl
2024-01-22 314bc7a1733856bd82c9d99bdfa27b34a4a762c3
提交 | 用户 | 时间
d43481 1 package cn.stylefeng.guns.plcserver.callback;
C 2
3
4 import cn.stylefeng.guns.modular.bs.collectionParamConf.model.params.CollectionParamConfParam;
5 import cn.stylefeng.guns.modular.bs.collectionParamConf.model.result.CollectionParamConfResult;
6 import cn.stylefeng.guns.modular.bs.collectionParamConf.service.CollectionParamConfService;
7 import cn.stylefeng.guns.modular.bs.equipmentInfo.service.EquipmentInfoService;
8 import cn.stylefeng.guns.modular.bs.locationInfo.model.params.LocationInfoParam;
9 import cn.stylefeng.guns.modular.bs.locationInfo.model.result.LocationInfoResult;
10 import cn.stylefeng.guns.modular.bs.locationInfo.service.LocationInfoService;
11 import cn.stylefeng.guns.modular.cm.paramCollection.model.params.ParamCollectionParam;
12 import cn.stylefeng.guns.modular.cm.paramCollection.service.ParamCollectionService;
13 import cn.stylefeng.guns.modular.cm.passingStationCollection.model.params.PassingStationCollectionParam;
14 import cn.stylefeng.guns.modular.cm.passingStationCollection.service.PassingStationCollectionService;
15 import cn.stylefeng.guns.modular.dq.materialTraceability.model.params.MaterialTraceabilityParam;
16 import cn.stylefeng.guns.modular.dq.materialTraceability.service.MaterialTraceabilityService;
17 import cn.stylefeng.guns.modular.em.equipmentAlarm.service.EquipmentAlarmService;
18 import cn.stylefeng.guns.modular.em.equipmentStatus.service.EquipmentStatusService;
19 import cn.stylefeng.guns.modular.gm.greaseManage.service.GreaseManageService;
20 import cn.stylefeng.guns.modular.om.productionOrdeInfo.model.params.ProductionOrdeInfoParam;
21 import cn.stylefeng.guns.modular.om.productionOrdeInfo.model.result.ProductionOrdeInfoResult;
22 import cn.stylefeng.guns.modular.om.productionOrdeInfo.service.ProductionOrdeInfoService;
23 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.params.ProductionOrderBatchInfoParam;
24 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.result.ProductionOrderBatchInfoResult;
25 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.service.ProductionOrderBatchInfoService;
26 import cn.stylefeng.guns.modular.om.productionOrderRecords.model.params.ProductionOrderRecordsParam;
27 import cn.stylefeng.guns.modular.om.productionOrderRecords.model.result.ProductionOrderRecordsResult;
28 import cn.stylefeng.guns.modular.om.productionOrderRecords.service.ProductionOrderRecordsService;
29 import cn.stylefeng.guns.modular.sc.repairManageInfo.model.params.RepairManageInfoParam;
30 import cn.stylefeng.guns.modular.sc.repairManageInfo.model.result.RepairManageInfoResult;
31 import cn.stylefeng.guns.modular.sc.repairManageInfo.service.RepairManageInfoService;
32 import cn.stylefeng.guns.modular.sc.serialNumbersConf.model.params.SerialNumbersConfParam;
33 import cn.stylefeng.guns.modular.sc.serialNumbersConf.model.result.SerialNumbersConfResult;
34 import cn.stylefeng.guns.modular.sc.serialNumbersConf.service.SerialNumbersConfService;
35 import cn.stylefeng.guns.plcserver.opc.GlobalVariable;
36 import cn.stylefeng.guns.plcserver.opc.OPCElement;
37 import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit;
38 import cn.stylefeng.guns.plcserver.opc.unit.SFCCodeTool;
39 import cn.stylefeng.guns.plcserver.tool.DateTool;
40 import cn.stylefeng.guns.plcserver.tool.LogUtil;
41 import com.kangaroohy.milo.model.ReadWriteEntity;
42 import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
43 import com.kangaroohy.milo.service.MiloService;
44 import org.apache.commons.lang3.StringUtils;
45 import org.apache.log4j.Logger;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.stereotype.Component;
48
49 import java.util.ArrayList;
50 import java.util.Calendar;
51 import java.util.Date;
52 import java.util.List;
53
54 @Component
55 public class OP010Callback implements SubscriptionCallback {
56
57     public MiloService miloService;
58
59     @Autowired
60     public ProductionOrdeInfoService productionOrdeInfoService;
61     @Autowired
62     public PassingStationCollectionService passingStationCollectionService;
63     @Autowired
64     public ParamCollectionService paramCollectionService;
65     @Autowired
66     public CollectionParamConfService collectionParamConfService;
67     @Autowired
68     public SerialNumbersConfService serialNumbersConfService;
69     @Autowired
70     private EquipmentStatusService equipmentStatusService;
71
72     @Autowired
73     private EquipmentAlarmService equipmentAlarmService;
74     @Autowired
75     private MaterialTraceabilityService materialTraceabilityService;
76     @Autowired
77     private EquipmentInfoService equipmentInfoService;
78     @Autowired
79     private RepairManageInfoService repairManageInfoService;
80
81     @Autowired
82     private LocationInfoService locationInfoService;
83
84     @Autowired
85     private GreaseManageService greaseManageService;
86
87     @Autowired
88     private ProductionOrderRecordsService productionOrderRecordsService;
89
90     @Autowired
91     private ProductionOrderBatchInfoService productionOrderBatchInfoService;
92
93     private static Logger logger;
94     LogUtil logUtil = new LogUtil();
95     static {
96         logger = Logger.getLogger(OP010Callback.class);
97     }
98     private static Date IN_TIME = null;
99     private static String S_ORDER_CODE = "";//工单编号
100     private static String S_PRODUCT_CODE = "";//产品编号
101     private static String S_PRODUCT_TYPE= "";//产品类型
102     private static String S_SFC_CODE = "";//总成编码
103     private static String PRODUCTION_LINE  = "OP";//产线号
104     private static String LOCATION_CODE  = "OP010";//工位号
105     private static List list = null;
106
107     private static Integer ACTUAL_ONLINE_QTY  = 0 ;//上线数量
108     public OP010Callback(MiloService miloService,
109                          ProductionOrdeInfoService productionOrdeInfoService,
110                          PassingStationCollectionService passingStationCollectionService,
111                          ParamCollectionService paramCollectionService,
112                          CollectionParamConfService collectionParamConfService,
113                          SerialNumbersConfService serialNumbersConfService,
114                          EquipmentStatusService equipmentStatusService,
115                          EquipmentAlarmService equipmentAlarmService,
116                          MaterialTraceabilityService materialTraceabilityService,
117                          EquipmentInfoService equipmentInfoService,
118                          RepairManageInfoService repairManageInfoService,
119                          LocationInfoService locationInfoService,
120                          GreaseManageService greaseManageService,
121                          ProductionOrderRecordsService productionOrderRecordsService,
122                          ProductionOrderBatchInfoService productionOrderBatchInfoService) {
123         this.miloService = miloService;
124         this.productionOrdeInfoService = productionOrdeInfoService;
125         this.passingStationCollectionService = passingStationCollectionService;
126         this.paramCollectionService = paramCollectionService;
127         this.collectionParamConfService = collectionParamConfService;
128         this.serialNumbersConfService = serialNumbersConfService;
129         this.equipmentStatusService = equipmentStatusService;
130         this.equipmentAlarmService = equipmentAlarmService;
131         this.materialTraceabilityService = materialTraceabilityService;
132         this.equipmentInfoService = equipmentInfoService;
133         this.repairManageInfoService = repairManageInfoService;
134         this.locationInfoService = locationInfoService;
135         this.greaseManageService = greaseManageService;
136         this.productionOrderRecordsService = productionOrderRecordsService;
137         this.productionOrderBatchInfoService = productionOrderBatchInfoService;
138     }
139     @Override
140     public void onSubscribe(String identifier, Object value) {
141         String ecpStr = "";//异常记录标记
142         try {
143             //心跳
144             if(null != value && identifier.equals(OPCElement.OP010_F_HEART_BEAT)){
145                 if( "false".equals(value.toString()))
146                 {
147                     ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP010_F_HEART_BEAT,true);
148                     miloService.writeToOpcUa(entity);
149                 }
150             }
151             //PLC进站
152             else if(null != value && identifier.equals(OPCElement.OP010_J_PLC_START)){
153                 if("true".equals(value.toString())){
154
155                     String isRepair = miloService.
156                             readFromOpcUa(OPCElement.OP010_P_REPAIR)
157                             .getValue().toString();//是否返修
158                     if(isRepair.equals("true")){
159                         handleRepair();
160                     }else{
161                         handleOrder();
162                     }
163
164                     handleOrder();
165                 }
166             }
167             //PLC出站
168             else if(null != value && identifier.equals(OPCElement.OP010_J_PLC_FINISH)){
169                 if("true".equals(value.toString())){
170                     handleFinsh();
171                     handleMaterial();
172                 }
173             }else{
174                 System.out.println("值没有变");
175             }
176         } catch (Exception e) {
177             ecpStr ="出现异常:" + logUtil.getTrace(e);
178         }finally {
179             if(!"".equals(ecpStr)){
180                 logger.info(ecpStr+"\r\n");
181             }
182             System.out.println("finally" +ecpStr);
183         }
184     }
185
186     //PC读工单
187     public void handleOrder() throws Exception{
188
189         logger.info("OP010工位,handleOrder PC处理开始!");
190         IN_TIME = DateTool.getLocalTimeForDate();
191         ProductionOrdeInfoParam param = new ProductionOrdeInfoParam();
192         param.setProductionLine("");//产线
193         param.setOrderStatus("3");//工单状态
194         List<ProductionOrdeInfoResult> list = productionOrdeInfoService.findListBySpec(param);
195
196         if(!list.isEmpty()) {
197             ProductionOrdeInfoResult result = list.get(0);
198
199             String softwareVersionCode = result.getSoftwareVersionCode();//产品型号
200             String productCompanyCode = result.getProductCompanyCode();//班次
201
202             //序列号
203             String serialNumbers = "0000";
204             SerialNumbersConfParam serialNumbersConfParam = new SerialNumbersConfParam();
205             Calendar calendar = Calendar.getInstance();
206             String Years = String.valueOf(calendar.get(Calendar.YEAR)); //年份
207             String Months = StringUtils.leftPad(String.valueOf(calendar.get(Calendar.MONTH) + 1),2, "0"); //月份
208             String Dates = StringUtils.leftPad(String.valueOf(calendar.get(Calendar.DATE)),2, "0"); //日期
209             serialNumbersConfParam.setYears(Years+Months+Dates);//获取当前是哪一天
210             serialNumbersConfParam.setState("1");
211
212             List<SerialNumbersConfResult> serialNumberslist = serialNumbersConfService.findListBySpec(serialNumbersConfParam);
213             if(!serialNumberslist.isEmpty()){
214                 SerialNumbersConfResult serialNumbersConfResult = serialNumberslist.get(0);
215                 serialNumbers = serialNumbersConfResult.getSerialNumber();
216                 serialNumbersConfParam.setId(serialNumbersConfResult.getId());
217             }else{
218                 serialNumbersConfParam.setSerialNumber(serialNumbers);
219                 serialNumbersConfParam.setLengths("4");
220                 serialNumbersConfService.add(serialNumbersConfParam);
221                 List<SerialNumbersConfResult> serialNumberslist2 = serialNumbersConfService.findListBySpec(serialNumbersConfParam);
222                 if(!serialNumberslist2.isEmpty()){
223                     SerialNumbersConfResult serialNumbersConfResult = serialNumberslist2.get(0);
224                     //serialNumbers = serialNumbersConfResult.getSerialNumber();
225                     serialNumbersConfParam.setId(serialNumbersConfResult.getId());
226                 }
227             }
228
229             //更新序列号+1
230             int i = Integer.valueOf(serialNumbers)+1;
231             serialNumbers = StringUtils.leftPad(String.valueOf(i),4, "0");
232             serialNumbersConfParam.setSerialNumber(serialNumbers);
233             serialNumbersConfService.update(serialNumbersConfParam);
234
235             //生成总成编码
236             String sfcCode = SFCCodeTool.createCodingRulesSFC(softwareVersionCode,productCompanyCode,serialNumbers);//总成号
237             S_SFC_CODE = sfcCode;
238             S_ORDER_CODE = result.getWorkOrderNo();
239             S_PRODUCT_CODE = result.getMaterialCode();
240             S_PRODUCT_TYPE = GlobalVariable.PRODUCT_TYPE_MAP.get(S_PRODUCT_CODE);
241             ACTUAL_ONLINE_QTY = result.getActualOnlineQty();
242
243
244             //新增报工记录
245             ProductionOrderRecordsParam productionOrderRecordsParam = new ProductionOrderRecordsParam();
246             productionOrderRecordsParam.setWorkOrderNo(S_ORDER_CODE);
247             productionOrderRecordsParam.setProductNo(S_SFC_CODE);
248             productionOrderRecordsParam.setMaterialCode(S_PRODUCT_CODE);
249             productionOrderRecordsParam.setStartTime(DateTool.getLocalTimeForDate());
250             productionOrderRecordsParam.setWhetherPass("true");
251             productionOrderRecordsService.add(productionOrderRecordsParam);
252
253             List<ReadWriteEntity> lists = new ArrayList<>();
254             lists.add(new ReadWriteEntity(OPCElement.OP010_S_ORDER_CODE,S_ORDER_CODE));
314bc7 255             lists.add(new ReadWriteEntity(OPCElement.OP010_S_PRODUCT_CODE,Short.parseShort(S_PRODUCT_TYPE)));
d43481 256             lists.add(new ReadWriteEntity(OPCElement.OP010_S_SFC_CODE,S_SFC_CODE));
314bc7 257             lists.add(new ReadWriteEntity(OPCElement.OP010_S_NUMBER,Short.parseShort(result.getPlanQty().toString())));
d43481 258             lists.add(new ReadWriteEntity(OPCElement.OP010_J_PC_WRITE_ORDER,true));
C 259             miloService.writeToOpcUa(lists);
260             logger.info("OP010工位,handleOrder PC处理完成!^--------------------------------------------");
261         }else{
262             logger.info("OP010工位没有可执行的工单,请联系工单管理员下发该产线的工单!");
263         }
264     }
265
266     public void handleFinsh() throws Exception {
267         logger.info("handleFinsh PC处理开始!");
268         String S_PRODUCT_STATE_CODE = miloService.
269                 readFromOpcUa(OPCElement.OP010_S_PRODUCT_STATE_CODE)
270                 .getValue().toString();
271
272         if("2".equals(S_PRODUCT_STATE_CODE)){
273             ProductionOrderRecordsParam productionOrderRecordsParam = new ProductionOrderRecordsParam();
274             productionOrderRecordsParam.setWorkOrderNo(S_ORDER_CODE);
275             productionOrderRecordsParam.setProductNo(S_SFC_CODE);
276             List<ProductionOrderRecordsResult> list = productionOrderRecordsService.findListBySpec(productionOrderRecordsParam);
277             if(!list.isEmpty()){
278                 ProductionOrderRecordsResult productionOrderRecordsResult = list.get(0);
279                 productionOrderRecordsParam.setId(productionOrderRecordsResult.getId());
280                 productionOrderRecordsParam.setWhetherPass(S_PRODUCT_STATE_CODE);
281                 productionOrderRecordsService.update(productionOrderRecordsParam);
282             }
283         }
284         //处理过站信息
285         PassingStationCollectionParam param = new PassingStationCollectionParam();
286         param.setWorkOrderNo(S_ORDER_CODE);
287         param.setProductCode(S_PRODUCT_CODE);
288         param.setSfcCode(S_SFC_CODE);
289         param.setProductionLine(PRODUCTION_LINE);
290         param.setLocationCode(LOCATION_CODE);
291         param.setInboundTime(IN_TIME);
292         param.setOutboundTime(DateTool.getLocalTimeForDate());
293         param.setOutRsSign(S_PRODUCT_STATE_CODE);//出站是否合格
294         passingStationCollectionService.add(param);
295
296         ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP010_J_PC_FINISH,true);
297         miloService.writeToOpcUa(entity);//写PC完成
298         logger.info("handleFinsh PC处理完成!");
299     }
300
301
302     public void handleMaterial() {
303
304         //查询批次上料信息
305         ProductionOrderBatchInfoParam productionOrderBatchInfoParam = new ProductionOrderBatchInfoParam();
306         productionOrderBatchInfoParam.setWorkOrderNo(S_ORDER_CODE);
307         productionOrderBatchInfoParam.setLocationCode(LOCATION_CODE);
308         List<ProductionOrderBatchInfoResult> list = productionOrderBatchInfoService.findListBySpec(productionOrderBatchInfoParam);
309         for(int i=0;i<list.size();i++){
310             ProductionOrderBatchInfoResult productionOrderBatchInfoResult = list.get(i);
311             int remainingQuantity =  productionOrderBatchInfoResult.getResidueQuantity();//剩余数量
312             if(remainingQuantity>0 ){
313                 //新增物料追溯
314                 MaterialTraceabilityParam materialTraceabilityParam = new MaterialTraceabilityParam();
315                 materialTraceabilityParam.setWorkOrderNo(S_ORDER_CODE);
316                 materialTraceabilityParam.setProductCode(S_PRODUCT_CODE);
317                 materialTraceabilityParam.setProductNo(S_SFC_CODE);
318                 materialTraceabilityParam.setLineCode(PRODUCTION_LINE);
319                 materialTraceabilityParam.setLocationCode(LOCATION_CODE);
320                 materialTraceabilityParam.setAssemblyQty(1);
321                 materialTraceabilityParam.setAssemblyTime(DateTool.getLocalTimeForDate());
322                 materialTraceabilityParam.setMaterialCode(productionOrderBatchInfoResult.getMaterialCode());//物料编码
323                 materialTraceabilityParam.setMaterialBatchNo(productionOrderBatchInfoResult.getBatch());//物料批次
324
325                 materialTraceabilityService.add(materialTraceabilityParam);
326                 //更新剩余数量
327                 productionOrderBatchInfoParam.setId(productionOrderBatchInfoResult.getId());
328                 productionOrderBatchInfoParam.setResidueQuantity(remainingQuantity-1);//剩余数量
329                 productionOrderBatchInfoService.update(productionOrderBatchInfoParam);
330             }
331         }
332     }
333
334     public void handleRepair() throws Exception{
335         RepairManageInfoParam repairManageInfoParam = new RepairManageInfoParam();
336         repairManageInfoParam.setLocationCode(LOCATION_CODE);
337         repairManageInfoParam.setManageType("2");
338         repairManageInfoParam.setUpPlc("0");
339
340         List<RepairManageInfoResult> list = repairManageInfoService.findListBySpec(repairManageInfoParam);
341         if(list.size()>0) {
342             RepairManageInfoResult result = list.get(0);
343
344             S_SFC_CODE = result.getSfc();
345             S_ORDER_CODE = result.getWorkOrderNo();
346             S_PRODUCT_CODE = result.getProductCode();
347             S_PRODUCT_TYPE = GlobalVariable.PRODUCT_TYPE_MAP.get(S_PRODUCT_CODE);
348             String  repairLocation = result.getPoorLocationCode();//
349
350             //根据工位编码查询工位表中配置的返修工位号
351             LocationInfoParam param = new LocationInfoParam();
352             param.setLocationCode(repairLocation);
353
354             List<LocationInfoResult> results = locationInfoService.findListBySpec(param);
355             if(results.size()>0) {
356                 LocationInfoResult r = results.get(0);
357                 String repairLocationNo = r.getSpareField2();
358                 if(null != repairLocationNo && "" != repairLocationNo){
359
360                     List<ReadWriteEntity> lists = new ArrayList<>();
361                     lists.add(new ReadWriteEntity(OPCElement.OP010_S_ORDER_CODE,S_ORDER_CODE));
314bc7 362                     lists.add(new ReadWriteEntity(OPCElement.OP010_S_PRODUCT_CODE,Short.parseShort(S_PRODUCT_TYPE)));
d43481 363                     lists.add(new ReadWriteEntity(OPCElement.OP010_S_SFC_CODE,S_SFC_CODE));
C 364                     lists.add(new ReadWriteEntity(OPCElement.OP010_P_REPAIR_LOCATION,repairLocationNo));
365                     lists.add(new ReadWriteEntity(OPCElement.OP010_J_PC_WRITE_ORDER,true));
366                     miloService.writeToOpcUa(lists);
367
368                     //更新为已经返修上线
369                     Long id = result.getId();
370                     repairManageInfoParam.setId(id);
371                     repairManageInfoParam.setUpPlc("1");
372                     repairManageInfoService.update(repairManageInfoParam);
373
374                 }else{
375                     logger.info("OP010工位,handleRepair 请查询"+repairLocation+"工位是否配置返修工位号!");
376                 }
377             }else{
378                 logger.info("OP010工位,handleRepair 无法查询到"+repairLocation+"工位!");
379             }
380
381         }
382     }
383
384     //PC读参数信息
385     public void handleInfo() throws Exception {
386         logger.info("handleInfo START!");
387
388         if(null == list){
389             CollectionParamConfParam collectionParamConfParam = new CollectionParamConfParam();
390             collectionParamConfParam.setLocationCode(LOCATION_CODE);
391             list = collectionParamConfService.findListBySpec(collectionParamConfParam);
392         }
393         if(null != list && !list.isEmpty()) {
394             for (int i = 0; i < list.size(); i++) {
395                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
396                 String value = miloService.
397                         readFromOpcUa(result.getGatherAddress())
398                         .getValue().toString();//读参数值
399                 ParamCollectionParam paramCollectionParam = new ParamCollectionParam();
400                 paramCollectionParam.setParamCode(result.getParamCode());
401                 paramCollectionParam.setParamName(result.getParamName());
402                 paramCollectionParam.setWorkOrderNo(S_ORDER_CODE);
403                 paramCollectionParam.setSfcCode(S_SFC_CODE);
404                 paramCollectionParam.setProductCode(S_PRODUCT_CODE);
405                 paramCollectionParam.setProductionLine(result.getProductionLine());
406                 paramCollectionParam.setLocationCode(result.getLocationCode());
407                 paramCollectionParam.setParamValue(value);
408                 paramCollectionParam.setParamLower(result.getParamLower());
409                 paramCollectionParam.setParamUpper(result.getParamUpper());
410                 paramCollectionParam.setParamStandard(result.getParamCentral());
411                 paramCollectionParam.setCollectionTime(DateTool.getLocalTimeForDate());
412                 paramCollectionParam.setUnit(result.getUnit());
413                 paramCollectionService.add(paramCollectionParam);
414             }
415         }
416
417         logger.info("handleInfo END!");
418     }
419
420 }