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.service.ProductionOrdeInfoService;
21 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.params.ProductionOrderBatchInfoParam;
22 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.result.ProductionOrderBatchInfoResult;
23 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.service.ProductionOrderBatchInfoService;
24 import cn.stylefeng.guns.modular.om.productionOrderRecords.model.params.ProductionOrderRecordsParam;
25 import cn.stylefeng.guns.modular.om.productionOrderRecords.model.result.ProductionOrderRecordsResult;
26 import cn.stylefeng.guns.modular.om.productionOrderRecords.service.ProductionOrderRecordsService;
27 import cn.stylefeng.guns.modular.sc.repairManageInfo.model.params.RepairManageInfoParam;
28 import cn.stylefeng.guns.modular.sc.repairManageInfo.model.result.RepairManageInfoResult;
29 import cn.stylefeng.guns.modular.sc.repairManageInfo.service.RepairManageInfoService;
30 import cn.stylefeng.guns.modular.sc.serialNumbersConf.service.SerialNumbersConfService;
31 import cn.stylefeng.guns.plcserver.opc.GlobalVariable;
32 import cn.stylefeng.guns.plcserver.opc.OPCElement;
33
34 import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit;
35 import cn.stylefeng.guns.plcserver.tool.DateTool;
36 import cn.stylefeng.guns.plcserver.tool.LogUtil;
37 import com.kangaroohy.milo.model.ReadWriteEntity;
38 import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
39 import com.kangaroohy.milo.service.MiloService;
40 import org.apache.log4j.Logger;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
43
44 import java.util.ArrayList;
45 import java.util.Date;
46 import java.util.List;
47
48 @Component
49 public class OP080Callback implements SubscriptionCallback {
50
51     public MiloService miloService;
52
53     @Autowired
54     public ProductionOrdeInfoService productionOrdeInfoService;
55     @Autowired
56     public PassingStationCollectionService passingStationCollectionService;
57     @Autowired
58     public ParamCollectionService paramCollectionService;
59     @Autowired
60     public CollectionParamConfService collectionParamConfService;
61     @Autowired
62     public SerialNumbersConfService serialNumbersConfService;
63     @Autowired
64     private EquipmentStatusService equipmentStatusService;
65
66     @Autowired
67     private EquipmentAlarmService equipmentAlarmService;
68     @Autowired
69     private MaterialTraceabilityService materialTraceabilityService;
70     @Autowired
71     private EquipmentInfoService equipmentInfoService;
72     @Autowired
73     private RepairManageInfoService repairManageInfoService;
74
75     @Autowired
76     private LocationInfoService locationInfoService;
77
78     @Autowired
79     private GreaseManageService greaseManageService;
80
81     @Autowired
82     private ProductionOrderRecordsService productionOrderRecordsService;
83
84     @Autowired
85     private ProductionOrderBatchInfoService productionOrderBatchInfoService;
86
87     private static Logger logger;
88     LogUtil logUtil = new LogUtil();
89     static {
90         logger = Logger.getLogger(OP080Callback.class);
91     }
92     private static Date IN_TIME = null;
93     private static String S_ORDER_CODE = "";//工单编号
94     private static String S_PRODUCT_CODE = "";//产品编号
95     private static String S_PRODUCT_TYPE= "";//产品类型
96     private static String S_SFC_CODE = "";//总成编码
97     private static String PRODUCTION_LINE  = "OP";//产线号
98     private static String LOCATION_CODE  = "OP080";//工位号
99     private static List list = null;
100
101     public OP080Callback(MiloService miloService,
102                          ProductionOrdeInfoService productionOrdeInfoService,
103                          PassingStationCollectionService passingStationCollectionService,
104                          ParamCollectionService paramCollectionService,
105                          CollectionParamConfService collectionParamConfService,
106                          SerialNumbersConfService serialNumbersConfService,
107                          EquipmentStatusService equipmentStatusService,
108                          EquipmentAlarmService equipmentAlarmService,
109                          MaterialTraceabilityService materialTraceabilityService,
110                          EquipmentInfoService equipmentInfoService,
111                          RepairManageInfoService repairManageInfoService,
112                          LocationInfoService locationInfoService,
113                          GreaseManageService greaseManageService,
114                          ProductionOrderRecordsService productionOrderRecordsService,
115                          ProductionOrderBatchInfoService productionOrderBatchInfoService) {
116         this.miloService = miloService;
117         this.productionOrdeInfoService = productionOrdeInfoService;
118         this.passingStationCollectionService = passingStationCollectionService;
119         this.paramCollectionService = paramCollectionService;
120         this.collectionParamConfService = collectionParamConfService;
121         this.serialNumbersConfService = serialNumbersConfService;
122         this.equipmentStatusService = equipmentStatusService;
123         this.equipmentAlarmService = equipmentAlarmService;
124         this.materialTraceabilityService = materialTraceabilityService;
125         this.equipmentInfoService = equipmentInfoService;
126         this.repairManageInfoService = repairManageInfoService;
127         this.locationInfoService = locationInfoService;
128         this.greaseManageService = greaseManageService;
129         this.productionOrderRecordsService = productionOrderRecordsService;
130         this.productionOrderBatchInfoService = productionOrderBatchInfoService;
131     }
132
133     @Override
134     public void onSubscribe(String identifier, Object value) {
135         String ecpStr = "";//异常记录标记
136         try {
137             //心跳
138             if(null != value && identifier.equals(OPCElement.OP080_F_HEART_BEAT)){
139                 if( "false".equals(value.toString()))
140                 {
141                     ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP080_F_HEART_BEAT,true);
142                     miloService.writeToOpcUa(entity);
143                 }
144             }
145             //PLC进站
146             else if(null != value && identifier.equals(OPCElement.OP080_J_PLC_START)){
147                 if("true".equals(value.toString())){
148                     handleOrder();
149                 }
150             }
151             //PLC出站
152             else if(null != value && identifier.equals(OPCElement.OP080_J_PLC_FINISH)){
153                 if("true".equals(value.toString())){
154                     handleFinsh();
155                     handleInfo();
156                     handleMaterial();
157                 }
158             }else{
159                 System.out.println("值没有变");
160             }
161         } catch (Exception e) {
162             ecpStr ="出现异常:" + logUtil.getTrace(e);
163         }finally {
164             if(!"".equals(ecpStr)){
165                 logger.info(ecpStr+"\r\n");
166             }
167         }
168     }
169
170     public void handleRepair() throws Exception{
171         RepairManageInfoParam repairManageInfoParam = new RepairManageInfoParam();
172         repairManageInfoParam.setLocationCode(LOCATION_CODE);
173         repairManageInfoParam.setManageType("2");
174         repairManageInfoParam.setUpPlc("0");
175         List<RepairManageInfoResult> list = repairManageInfoService.findListBySpec(repairManageInfoParam);
176         if(list.size()>0) {
177             RepairManageInfoResult result = list.get(0);
178
179             S_SFC_CODE = result.getSfc();
180             S_ORDER_CODE = result.getWorkOrderNo();
181             S_PRODUCT_CODE = result.getProductCode();
182             S_PRODUCT_TYPE = GlobalVariable.PRODUCT_TYPE_MAP.get(S_PRODUCT_CODE);
183             String  repairLocation = result.getPoorLocationCode();//
184
185             //根据工位编码查询工位表中配置的返修工位号
186             LocationInfoParam param = new LocationInfoParam();
187             param.setLocationCode(repairLocation);
188             List<LocationInfoResult> results = locationInfoService.findListBySpec(param);
189             if(results.size()>0) {
190                 LocationInfoResult r = results.get(0);
191                 String repairLocationNo = r.getSpareField2();
192                 if(null != repairLocationNo && "" != repairLocationNo){
193                     List<ReadWriteEntity> lists = new ArrayList<>();
194                     lists.add(new ReadWriteEntity(OPCElement.OP080_S_ORDER_CODE,S_ORDER_CODE));
195                     lists.add(new ReadWriteEntity(OPCElement.OP080_S_PRODUCT_CODE,S_PRODUCT_TYPE));
196                     lists.add(new ReadWriteEntity(OPCElement.OP080_S_SFC_CODE,S_SFC_CODE));
197                     lists.add(new ReadWriteEntity(OPCElement.OP080_P_REPAIR_LOCATION,repairLocationNo));
198                     lists.add(new ReadWriteEntity(OPCElement.OP080_J_PC_WRITE_ORDER,true));
199                     miloService.writeToOpcUa(lists);
200
201                     //更新为已经返修上线
202                     Long id = result.getId();
203                     repairManageInfoParam.setId(id);
204                     repairManageInfoParam.setUpPlc("1");
205                     repairManageInfoService.update(repairManageInfoParam);
206
207                 }else{
208                     logger.info("OP080工位,handleRepair 请查询"+repairLocation+"工位是否配置返修工位号!");
209                 }
210             }else{
211                 logger.info("OP080工位,handleRepair 无法查询到"+repairLocation+"工位!");
212             }
213         }
214     }
215
216     //PC读工单
217     public void handleOrder() throws Exception {
218         logger.info("handleOrder PC处理开始!");
219         IN_TIME = DateTool.getLocalTimeForDate();
220         List<String> lists = new ArrayList<>();
221         lists.add(OPCElement.OP080_S_ORDER_CODE);
222         lists.add(OPCElement.OP080_S_PRODUCT_CODE);
223         lists.add(OPCElement.OP080_S_SFC_CODE);
224         List<ReadWriteEntity> returnLists= miloService.readFromOpcUa(lists);
225         returnLists.forEach(element->{
226             switch(element.getIdentifier()){
227                 case OPCElement.OP080_S_ORDER_CODE:
228                     S_ORDER_CODE = element.getValue().toString();
229                     break;
230                 case OPCElement.OP080_S_PRODUCT_CODE:
231                     S_PRODUCT_TYPE = element.getValue().toString();
232                     S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
233                     break;
234                 case OPCElement.OP080_S_SFC_CODE:
235                     S_SFC_CODE = element.getValue().toString();
236                     break;
237                 default:
238                     break;
239             }
240         });
241         ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP080_J_PC_WRITE_ORDER,true);
242         miloService.writeToOpcUa(entity);
243         logger.info("handleOrder PC处理完成!");
244     }
245
246     public void handleFinsh() throws Exception {
247         logger.info("handleFinsh PC处理开始!");
248         String S_PRODUCT_STATE_CODE = miloService.
249                 readFromOpcUa(OPCElement.OP080_S_PRODUCT_STATE_CODE)
250                 .getValue().toString();
251
252         if("2".equals(S_PRODUCT_STATE_CODE)){
253             ProductionOrderRecordsParam productionOrderRecordsParam = new ProductionOrderRecordsParam();
254             productionOrderRecordsParam.setWorkOrderNo(S_ORDER_CODE);
255             productionOrderRecordsParam.setProductNo(S_SFC_CODE);
256             List<ProductionOrderRecordsResult> list = productionOrderRecordsService.findListBySpec(productionOrderRecordsParam);
257             if(!list.isEmpty()){
258                 ProductionOrderRecordsResult productionOrderRecordsResult = list.get(0);
259                 productionOrderRecordsParam.setId(productionOrderRecordsResult.getId());
260                 productionOrderRecordsParam.setWhetherPass(S_PRODUCT_STATE_CODE);
261                 productionOrderRecordsService.update(productionOrderRecordsParam);
262             }
263         }
264         //处理过站信息
265         PassingStationCollectionParam param = new PassingStationCollectionParam();
266         param.setWorkOrderNo(S_ORDER_CODE);
267         param.setProductCode(S_PRODUCT_CODE);
268         param.setSfcCode(S_SFC_CODE);
269         param.setProductionLine(PRODUCTION_LINE);
270         param.setLocationCode(LOCATION_CODE);
271         param.setInboundTime(IN_TIME);
272         param.setOutboundTime(DateTool.getLocalTimeForDate());
273         param.setOutRsSign(S_PRODUCT_STATE_CODE);//出站是否合格
274         passingStationCollectionService.add(param);
275
276         ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP080_J_PC_FINISH,true);
277         miloService.writeToOpcUa(entity);//写PC完成
278         logger.info("handleFinsh PC处理完成!");
279     }
280
281     public void handleMaterial() {
282
283         //查询批次上料信息
284         ProductionOrderBatchInfoParam productionOrderBatchInfoParam = new ProductionOrderBatchInfoParam();
285         productionOrderBatchInfoParam.setWorkOrderNo(S_ORDER_CODE);
286         productionOrderBatchInfoParam.setLocationCode(LOCATION_CODE);
287         List<ProductionOrderBatchInfoResult> list = productionOrderBatchInfoService.findListBySpec(productionOrderBatchInfoParam);
288         for(int i=0;i<list.size();i++){
289             ProductionOrderBatchInfoResult productionOrderBatchInfoResult = list.get(i);
290             int remainingQuantity =  productionOrderBatchInfoResult.getResidueQuantity();//剩余数量
291             if(remainingQuantity>0 ){
292                 //新增物料追溯
293                 MaterialTraceabilityParam materialTraceabilityParam = new MaterialTraceabilityParam();
294                 materialTraceabilityParam.setWorkOrderNo(S_ORDER_CODE);
295                 materialTraceabilityParam.setProductCode(S_PRODUCT_CODE);
296                 materialTraceabilityParam.setProductNo(S_SFC_CODE);
297                 materialTraceabilityParam.setLineCode(PRODUCTION_LINE);
298                 materialTraceabilityParam.setLocationCode(LOCATION_CODE);
299                 materialTraceabilityParam.setAssemblyQty(1);
300                 materialTraceabilityParam.setAssemblyTime(DateTool.getLocalTimeForDate());
301                 materialTraceabilityParam.setMaterialCode(productionOrderBatchInfoResult.getMaterialCode());//物料编码
302                 materialTraceabilityParam.setMaterialBatchNo(productionOrderBatchInfoResult.getBatch());//物料批次
303
304                 materialTraceabilityService.add(materialTraceabilityParam);
305                 //更新剩余数量
306                 productionOrderBatchInfoParam.setId(productionOrderBatchInfoResult.getId());
307                 productionOrderBatchInfoParam.setResidueQuantity(remainingQuantity-1);//剩余数量
308                 productionOrderBatchInfoService.update(productionOrderBatchInfoParam);
309             }
310         }
311     }
312
313     //PC读参数信息
314     public void handleInfo() throws Exception {
315         logger.info("handleInfo START!");
316
317         if(null == list){
318             CollectionParamConfParam collectionParamConfParam = new CollectionParamConfParam();
319             collectionParamConfParam.setLocationCode(LOCATION_CODE);
320             list = collectionParamConfService.findListBySpec(collectionParamConfParam);
321         }
322         if(null != list && !list.isEmpty()) {
323             for (int i = 0; i < list.size(); i++) {
324                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
325                 String value = miloService.
326                         readFromOpcUa(result.getGatherAddress())
327                         .getValue().toString();//读参数值
328                 ParamCollectionParam paramCollectionParam = new ParamCollectionParam();
329                 paramCollectionParam.setParamCode(result.getParamCode());
330                 paramCollectionParam.setParamName(result.getParamName());
331                 paramCollectionParam.setWorkOrderNo(S_ORDER_CODE);
332                 paramCollectionParam.setSfcCode(S_SFC_CODE);
333                 paramCollectionParam.setProductCode(S_PRODUCT_CODE);
334                 paramCollectionParam.setProductionLine(result.getProductionLine());
335                 paramCollectionParam.setLocationCode(result.getLocationCode());
336                 paramCollectionParam.setParamValue(value);
337                 paramCollectionParam.setParamLower(result.getParamLower());
338                 paramCollectionParam.setParamUpper(result.getParamUpper());
339                 paramCollectionParam.setParamStandard(result.getParamCentral());
340                 paramCollectionParam.setCollectionTime(DateTool.getLocalTimeForDate());
341                 paramCollectionParam.setUnit(result.getUnit());
342                 paramCollectionService.add(paramCollectionParam);
343             }
344         }
345
346         logger.info("handleInfo END!");
347     }
348
349 }