cl
2024-01-20 d434812c124e2dab22487f8d40d8ad6c3b8ad19c
提交 | 用户 | 时间
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.*;
50
51 @Component
52 public class COP010Callback implements SubscriptionCallback {
53
54     public MiloService miloService;
55
56     @Autowired
57     public ProductionOrdeInfoService productionOrdeInfoService;
58     @Autowired
59     public PassingStationCollectionService passingStationCollectionService;
60     @Autowired
61     public ParamCollectionService paramCollectionService;
62     @Autowired
63     public CollectionParamConfService collectionParamConfService;
64     @Autowired
65     public SerialNumbersConfService serialNumbersConfService;
66     @Autowired
67     private EquipmentStatusService equipmentStatusService;
68
69     @Autowired
70     private EquipmentAlarmService equipmentAlarmService;
71     @Autowired
72     private MaterialTraceabilityService materialTraceabilityService;
73     @Autowired
74     private EquipmentInfoService equipmentInfoService;
75     @Autowired
76     private RepairManageInfoService repairManageInfoService;
77
78     @Autowired
79     private LocationInfoService locationInfoService;
80
81     @Autowired
82     private GreaseManageService greaseManageService;
83
84     @Autowired
85     private ProductionOrderRecordsService productionOrderRecordsService;
86
87     @Autowired
88     private ProductionOrderBatchInfoService productionOrderBatchInfoService;
89
90     private static Logger logger;
91     LogUtil logUtil = new LogUtil();
92     static {
93         logger = Logger.getLogger(COP010Callback.class);
94     }
95     private static Date IN_TIME = null;
96     private static String S_ORDER_CODE = "";//工单编号
97     private static String S_PRODUCT_CODE = "";//产品编号
98     private static String S_PRODUCT_TYPE= "";//产品类型
99     private static String S_SFC_CODE = "";//总成编码
100     private static String PRODUCTION_LINE  = "COP";//产线号
101     private static String LOCATION_CODE  = "COP010";//工位号
102     private static List list = null;
103
104     public COP010Callback(MiloService miloService,
105                           ProductionOrdeInfoService productionOrdeInfoService,
106                           PassingStationCollectionService passingStationCollectionService,
107                           ParamCollectionService paramCollectionService,
108                           CollectionParamConfService collectionParamConfService,
109                           SerialNumbersConfService serialNumbersConfService,
110                           EquipmentStatusService equipmentStatusService,
111                           EquipmentAlarmService equipmentAlarmService,
112                           MaterialTraceabilityService materialTraceabilityService,
113                           EquipmentInfoService equipmentInfoService,
114                           RepairManageInfoService repairManageInfoService,
115                           LocationInfoService locationInfoService,
116                           GreaseManageService greaseManageService,
117                           ProductionOrderRecordsService productionOrderRecordsService,
118                           ProductionOrderBatchInfoService productionOrderBatchInfoService) {
119         this.miloService = miloService;
120         this.productionOrdeInfoService = productionOrdeInfoService;
121         this.passingStationCollectionService = passingStationCollectionService;
122         this.paramCollectionService = paramCollectionService;
123         this.collectionParamConfService = collectionParamConfService;
124         this.serialNumbersConfService = serialNumbersConfService;
125         this.equipmentStatusService = equipmentStatusService;
126         this.equipmentAlarmService = equipmentAlarmService;
127         this.materialTraceabilityService = materialTraceabilityService;
128         this.equipmentInfoService = equipmentInfoService;
129         this.repairManageInfoService = repairManageInfoService;
130         this.locationInfoService = locationInfoService;
131         this.greaseManageService = greaseManageService;
132         this.productionOrderRecordsService = productionOrderRecordsService;
133         this.productionOrderBatchInfoService = productionOrderBatchInfoService;
134     }
135     @Override
136     public void onSubscribe(String identifier, Object value) {
137         String ecpStr = "";//异常记录标记
138         try {
139             //心跳
140             if(null != value && identifier.equals(OPCElement.COP010_F_HEART_BEAT)){
141                 if( "false".equals(value.toString()))
142                 {
143                     ReadWriteEntity entity = new ReadWriteEntity(OPCElement.COP010_F_HEART_BEAT,true);
144                     miloService.writeToOpcUa(entity);
145                 }
146             }
147             //PLC进站
148             else if(null != value && identifier.equals(OPCElement.COP010_J_PLC_START)){
149                 if("true".equals(value.toString())){
150                     String isRepair = miloService.
151                             readFromOpcUa(OPCElement.COP010_P_REPAIR)
152                             .getValue().toString();//是否返修
153                     if(isRepair.equals("true")){
154                         handleRepair();
155                     }else{
156                         handleOrder();
157                     }
158                 }
159             }
160             //PLC出站
161             else if(null != value && identifier.equals(OPCElement.COP010_J_PLC_FINISH)){
162                 if("true".equals(value.toString())){
163                     handleFinsh();
164                     handleInfo();
165                     handleMaterial();
166                 }
167             }else{
168                 System.out.println("COP010值没有变");
169             }
170         } catch (Exception e) {
171             ecpStr ="出现异常:" + logUtil.getTrace(e);
172         }finally {
173             if(!"".equals(ecpStr)){
174                 logger.info(ecpStr+"\r\n");
175             }
176             System.out.println("finally" +ecpStr);
177         }
178     }
179
180     public void handleOrder() throws Exception{
181
182         IN_TIME = DateTool.getLocalTimeForDate();
183         ProductionOrdeInfoParam param = new ProductionOrdeInfoParam();
184         param.setProductionLine("");//产线
185         param.setOrderStatus("3");//工单状态
186         List<ProductionOrdeInfoResult> list = productionOrdeInfoService.findListBySpec(param);
187
188         if(list.size()>0) {
189             ProductionOrdeInfoResult result = list.get(0);
190
191             S_ORDER_CODE = result.getWorkOrderNo();
192             S_PRODUCT_CODE = result.getMaterialCode();
193             S_PRODUCT_TYPE = GlobalVariable.PRODUCT_TYPE_MAP.get(S_PRODUCT_CODE);
194
195             //自动生成唯一码
196             Random random = new Random(System.currentTimeMillis());
197             S_SFC_CODE = "COP"+random.nextLong();
198
199             List<ReadWriteEntity> lists = new ArrayList<>();
200             lists.add(new ReadWriteEntity(OPCElement.COP010_S_ORDER_CODE,S_ORDER_CODE));
201             lists.add(new ReadWriteEntity(OPCElement.COP010_S_PRODUCT_CODE,S_PRODUCT_TYPE));
202             lists.add(new ReadWriteEntity(OPCElement.COP010_S_SFC_CODE,S_SFC_CODE));
203             lists.add(new ReadWriteEntity(OPCElement.COP010_S_NUMBER,result.getPlanQty().toString()));
204             lists.add(new ReadWriteEntity(OPCElement.COP010_J_PC_WRITE_ORDER,true));
205             miloService.writeToOpcUa(lists);
206
207             logger.info("COP010工位,handleOrder PC处理完成!2");
208         }else{
209             logger.info("COP010工位没有可执行的工单,请联系工单管理员下发该产线的工单!");
210         }
211     }
212
213     public void handleFinsh() throws Exception {
214         logger.info("handleFinsh PC处理开始!");
215         String S_PRODUCT_STATE_CODE = miloService.
216                 readFromOpcUa(OPCElement.COP010_S_PRODUCT_STATE_CODE)
217                 .getValue().toString();
218
219         //处理过站信息
220         PassingStationCollectionParam param = new PassingStationCollectionParam();
221         param.setWorkOrderNo(S_ORDER_CODE);
222         param.setProductCode(S_PRODUCT_CODE);
223         param.setSfcCode(S_SFC_CODE);
224         param.setProductionLine(PRODUCTION_LINE);
225         param.setLocationCode(LOCATION_CODE);
226         param.setInboundTime(IN_TIME);
227         param.setOutboundTime(DateTool.getLocalTimeForDate());
228         param.setOutRsSign(S_PRODUCT_STATE_CODE);//出站是否合格
229         passingStationCollectionService.add(param);
230
231         ReadWriteEntity entity = new ReadWriteEntity(OPCElement.COP010_J_PC_FINISH,true);
232         miloService.writeToOpcUa(entity);//写PC完成
233         logger.info("handleFinsh PC处理完成!");
234     }
235
236     public void handleMaterial() {
237
238         //查询批次上料信息
239         ProductionOrderBatchInfoParam productionOrderBatchInfoParam = new ProductionOrderBatchInfoParam();
240         productionOrderBatchInfoParam.setWorkOrderNo(S_ORDER_CODE);
241         productionOrderBatchInfoParam.setLocationCode(LOCATION_CODE);
242         List<ProductionOrderBatchInfoResult> list = productionOrderBatchInfoService.findListBySpec(productionOrderBatchInfoParam);
243         for(int i=0;i<list.size();i++){
244             ProductionOrderBatchInfoResult productionOrderBatchInfoResult = list.get(i);
245             int remainingQuantity =  productionOrderBatchInfoResult.getResidueQuantity();//剩余数量
246             if(remainingQuantity>0 ){
247                 //新增物料追溯
248                 MaterialTraceabilityParam materialTraceabilityParam = new MaterialTraceabilityParam();
249                 materialTraceabilityParam.setWorkOrderNo(S_ORDER_CODE);
250                 materialTraceabilityParam.setProductCode(S_PRODUCT_CODE);
251                 materialTraceabilityParam.setProductNo(S_SFC_CODE);
252                 materialTraceabilityParam.setLineCode(PRODUCTION_LINE);
253                 materialTraceabilityParam.setLocationCode(LOCATION_CODE);
254                 materialTraceabilityParam.setAssemblyQty(1);
255                 materialTraceabilityParam.setAssemblyTime(DateTool.getLocalTimeForDate());
256                 materialTraceabilityParam.setMaterialCode(productionOrderBatchInfoResult.getMaterialCode());//物料编码
257                 materialTraceabilityParam.setMaterialBatchNo(productionOrderBatchInfoResult.getBatch());//物料批次
258
259                 materialTraceabilityService.add(materialTraceabilityParam);
260                 //更新剩余数量
261                 productionOrderBatchInfoParam.setId(productionOrderBatchInfoResult.getId());
262                 productionOrderBatchInfoParam.setResidueQuantity(remainingQuantity-1);//剩余数量
263                 productionOrderBatchInfoService.update(productionOrderBatchInfoParam);
264             }
265         }
266     }
267
268     //PC读参数信息
269     public void handleInfo() throws Exception {
270         logger.info("handleInfo START!");
271
272         if(null == list){
273             CollectionParamConfParam collectionParamConfParam = new CollectionParamConfParam();
274             collectionParamConfParam.setLocationCode(LOCATION_CODE);
275             list = collectionParamConfService.findListBySpec(collectionParamConfParam);
276         }
277         if(null != list && !list.isEmpty()) {
278             for (int i = 0; i < list.size(); i++) {
279                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
280                 String value = miloService.
281                         readFromOpcUa(result.getGatherAddress())
282                         .getValue().toString();//读参数值
283                 ParamCollectionParam paramCollectionParam = new ParamCollectionParam();
284                 paramCollectionParam.setParamCode(result.getParamCode());
285                 paramCollectionParam.setParamName(result.getParamName());
286                 paramCollectionParam.setWorkOrderNo(S_ORDER_CODE);
287                 paramCollectionParam.setSfcCode(S_SFC_CODE);
288                 paramCollectionParam.setProductCode(S_PRODUCT_CODE);
289                 paramCollectionParam.setProductionLine(result.getProductionLine());
290                 paramCollectionParam.setLocationCode(result.getLocationCode());
291                 paramCollectionParam.setParamValue(value);
292                 paramCollectionParam.setParamLower(result.getParamLower());
293                 paramCollectionParam.setParamUpper(result.getParamUpper());
294                 paramCollectionParam.setParamStandard(result.getParamCentral());
295                 paramCollectionParam.setCollectionTime(DateTool.getLocalTimeForDate());
296                 paramCollectionParam.setUnit(result.getUnit());
297                 paramCollectionService.add(paramCollectionParam);
298             }
299         }
300
301         logger.info("handleInfo END!");
302     }
303     public void handleRepair() throws Exception{
304         RepairManageInfoParam repairManageInfoParam = new RepairManageInfoParam();
305         repairManageInfoParam.setLocationCode(LOCATION_CODE);
306         repairManageInfoParam.setManageType("2");
307         repairManageInfoParam.setUpPlc("0");
308         List<RepairManageInfoResult> list = repairManageInfoService.findListBySpec(repairManageInfoParam);
309         if(list.size()>0) {
310             RepairManageInfoResult result = list.get(0);
311
312             //自动生成唯一码
313             Random random = new Random(System.currentTimeMillis());
314             S_SFC_CODE = "COP" + random.nextLong();
315
316
317             S_ORDER_CODE = result.getWorkOrderNo();
318             S_PRODUCT_CODE = result.getProductCode();
319             S_PRODUCT_TYPE = GlobalVariable.PRODUCT_TYPE_MAP.get(S_PRODUCT_CODE);
320             String  repairLocation = result.getPoorLocationCode();//
321
322             //根据工位编码查询工位表中配置的返修工位号
323             LocationInfoParam param = new LocationInfoParam();
324             param.setLocationCode(repairLocation);
325             List<LocationInfoResult> results = locationInfoService.findListBySpec(param);
326             if(results.size()>0) {
327                 LocationInfoResult r = results.get(0);
328                 String repairLocationNo = r.getSpareField2();
329                 if(null != repairLocationNo && "" != repairLocationNo){
330                     List<ReadWriteEntity> lists = new ArrayList<>();
331                     lists.add(new ReadWriteEntity(OPCElement.COP010_S_ORDER_CODE,S_ORDER_CODE));
332                     lists.add(new ReadWriteEntity(OPCElement.COP010_S_PRODUCT_CODE,S_PRODUCT_TYPE));
333                     lists.add(new ReadWriteEntity(OPCElement.COP010_S_SFC_CODE,S_SFC_CODE));
334                     lists.add(new ReadWriteEntity(OPCElement.COP010_P_REPAIR_LOCATION,repairLocationNo));
335                     lists.add(new ReadWriteEntity(OPCElement.COP010_J_PC_WRITE_ORDER,true));
336                     miloService.writeToOpcUa(lists);
337                     //更新为已经返修上线
338                     Long id = result.getId();
339                     repairManageInfoParam.setId(id);
340                     repairManageInfoParam.setUpPlc("1");
341                     repairManageInfoService.update(repairManageInfoParam);
342
343                 }else{
344                     logger.info("COP010工位,handleRepair 请查询"+repairLocation+"工位是否配置返修工位号!");
345                 }
346             }else{
347                 logger.info("COP010工位,handleRepair 无法查询到"+repairLocation+"工位!");
348             }
349         }
350     }
351 }