cl
2024-01-23 6dadf36ef118fbb3b2cd5aca94cde88e29fb55dc
提交 | 用户 | 时间
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.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 import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit;
34 import cn.stylefeng.guns.plcserver.tool.DateTool;
35 import cn.stylefeng.guns.plcserver.tool.LogUtil;
36 import com.kangaroohy.milo.model.ReadWriteEntity;
37 import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
38 import com.kangaroohy.milo.service.MiloService;
39 import org.apache.log4j.Logger;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42
43 import java.util.ArrayList;
44 import java.util.Date;
45 import java.util.List;
46 import java.util.Random;
47
48 @Component
49 public class BOP010Callback 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(BOP010Callback.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  = "BOP";//产线号
98     private static String LOCATION_CODE  = "BOP010";//工位号
99     private static List list = null;
100
101     public BOP010Callback(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     @Override
133     public void onSubscribe(String identifier, Object value) {
134         String ecpStr = "";//异常记录标记
135         try {
136             //心跳
137             if(null != value && identifier.equals(OPCElement.COP010_F_HEART_BEAT)){
138                 if( "false".equals(value.toString()))
139                 {
140                     ReadWriteEntity entity = new ReadWriteEntity(OPCElement.BOP010_F_HEART_BEAT,true);
141                     miloService.writeToOpcUa(entity);
142                 }
143             }
144             //PLC进站
145             else if(null != value && identifier.equals(OPCElement.BOP010_J_PLC_START)){
146                 if("true".equals(value.toString())){
147                     String isRepair = miloService.
148                             readFromOpcUa(OPCElement.BOP010_P_REPAIR)
149                             .getValue().toString();//是否返修
150                     if(isRepair.equals("true")){
151                         handleRepair();
152                     }else{
153                         handleOrder();
154                     }
155                 }
156             }
157             //PLC出站
158             else if(null != value && identifier.equals(OPCElement.BOP010_J_PLC_FINISH)){
159                 if("true".equals(value.toString())){
160                     handleFinsh();
161                     handleInfo();
162                     handleMaterial();
163                 }
164             }else{
165                 System.out.println("BOP010值没有变");
166             }
167         } catch (Exception e) {
168             ecpStr ="出现异常:" + logUtil.getTrace(e);
169         }finally {
170             if(!"".equals(ecpStr)){
171                 logger.info(ecpStr+"\r\n");
172             }
173         }
174     }
175
176     public void handleOrder() throws Exception{
177
178         IN_TIME = DateTool.getLocalTimeForDate();
179         ProductionOrdeInfoParam param = new ProductionOrdeInfoParam();
180         param.setProductionLine("");//产线
181         param.setOrderStatus("3");//工单状态
182         List<ProductionOrdeInfoResult> list = productionOrdeInfoService.findListBySpec(param);
183
184         if(list.size()>0) {
185             ProductionOrdeInfoResult result = list.get(0);
186
187             S_ORDER_CODE = result.getWorkOrderNo();
188             S_PRODUCT_CODE = result.getMaterialCode();
189             S_PRODUCT_TYPE = GlobalVariable.PRODUCT_TYPE_MAP.get(S_PRODUCT_CODE);
190
191             //自动生成唯一码
192             Random random = new Random(System.currentTimeMillis());
193             S_SFC_CODE = "BOP"+random.nextLong();
194
195             List<ReadWriteEntity> lists = new ArrayList<>();
196             lists.add(new ReadWriteEntity(OPCElement.BOP010_S_ORDER_CODE,S_ORDER_CODE));
314bc7 197             lists.add(new ReadWriteEntity(OPCElement.BOP010_S_PRODUCT_CODE,Short.parseShort(S_PRODUCT_TYPE)));
d43481 198             lists.add(new ReadWriteEntity(OPCElement.BOP010_S_SFC_CODE,S_SFC_CODE));
314bc7 199             lists.add(new ReadWriteEntity(OPCElement.BOP010_S_NUMBER,Short.parseShort(result.getPlanQty().toString())));
d43481 200             lists.add(new ReadWriteEntity(OPCElement.BOP010_J_PC_WRITE_ORDER,true));
C 201             miloService.writeToOpcUa(lists);
202
203             logger.info("BOP010工位,handleOrder PC处理完成!2");
204         }else{
205             logger.info("BOP010工位没有可执行的工单,请联系工单管理员下发该产线的工单!");
206         }
207     }
208
209     public void handleFinsh() throws Exception {
210         logger.info("handleFinsh PC处理开始!");
211         String S_PRODUCT_STATE_CODE = miloService.
212                 readFromOpcUa(OPCElement.BOP010_S_PRODUCT_STATE_CODE)
213                 .getValue().toString();
214
215         //处理过站信息
216         PassingStationCollectionParam param = new PassingStationCollectionParam();
217         param.setWorkOrderNo(S_ORDER_CODE);
218         param.setProductCode(S_PRODUCT_CODE);
219         param.setSfcCode(S_SFC_CODE);
220         param.setProductionLine(PRODUCTION_LINE);
221         param.setLocationCode(LOCATION_CODE);
222         param.setInboundTime(IN_TIME);
223         param.setOutboundTime(DateTool.getLocalTimeForDate());
224         param.setOutRsSign(S_PRODUCT_STATE_CODE);//出站是否合格
225         passingStationCollectionService.add(param);
226
227         ReadWriteEntity entity = new ReadWriteEntity(OPCElement.BOP010_J_PC_FINISH,true);
228         miloService.writeToOpcUa(entity);//写PC完成
229         logger.info("handleFinsh PC处理完成!");
230     }
231
232     public void handleMaterial() {
233
234         //查询批次上料信息
235         ProductionOrderBatchInfoParam productionOrderBatchInfoParam = new ProductionOrderBatchInfoParam();
236         productionOrderBatchInfoParam.setWorkOrderNo(S_ORDER_CODE);
237         productionOrderBatchInfoParam.setLocationCode(LOCATION_CODE);
238         List<ProductionOrderBatchInfoResult> list = productionOrderBatchInfoService.findListBySpec(productionOrderBatchInfoParam);
239         for(int i=0;i<list.size();i++){
240             ProductionOrderBatchInfoResult productionOrderBatchInfoResult = list.get(i);
241             int remainingQuantity =  productionOrderBatchInfoResult.getResidueQuantity();//剩余数量
242             if(remainingQuantity>0 ){
243                 //新增物料追溯
244                 MaterialTraceabilityParam materialTraceabilityParam = new MaterialTraceabilityParam();
245                 materialTraceabilityParam.setWorkOrderNo(S_ORDER_CODE);
246                 materialTraceabilityParam.setProductCode(S_PRODUCT_CODE);
247                 materialTraceabilityParam.setProductNo(S_SFC_CODE);
248                 materialTraceabilityParam.setLineCode(PRODUCTION_LINE);
249                 materialTraceabilityParam.setLocationCode(LOCATION_CODE);
250                 materialTraceabilityParam.setAssemblyQty(1);
251                 materialTraceabilityParam.setAssemblyTime(DateTool.getLocalTimeForDate());
252                 materialTraceabilityParam.setMaterialCode(productionOrderBatchInfoResult.getMaterialCode());//物料编码
253                 materialTraceabilityParam.setMaterialBatchNo(productionOrderBatchInfoResult.getBatch());//物料批次
254
255                 materialTraceabilityService.add(materialTraceabilityParam);
256                 //更新剩余数量
257                 productionOrderBatchInfoParam.setId(productionOrderBatchInfoResult.getId());
258                 productionOrderBatchInfoParam.setResidueQuantity(remainingQuantity-1);//剩余数量
259                 productionOrderBatchInfoService.update(productionOrderBatchInfoParam);
260             }
261         }
262     }
263
264     //PC读参数信息
265     public void handleInfo() throws Exception {
266         logger.info("handleInfo START!");
267
268         if(null == list){
269             CollectionParamConfParam collectionParamConfParam = new CollectionParamConfParam();
270             collectionParamConfParam.setLocationCode(LOCATION_CODE);
271             list = collectionParamConfService.findListBySpec(collectionParamConfParam);
272         }
273         if(null != list && !list.isEmpty()) {
274             for (int i = 0; i < list.size(); i++) {
275                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
6dadf3 276                 Object value = miloService.
d43481 277                         readFromOpcUa(result.getGatherAddress())
6dadf3 278                         .getValue();//读参数值
C 279                 if(null != value) {
280                     ParamCollectionParam paramCollectionParam = new ParamCollectionParam();
281                     paramCollectionParam.setParamCode(result.getParamCode());
282                     paramCollectionParam.setParamName(result.getParamName());
283                     paramCollectionParam.setWorkOrderNo(S_ORDER_CODE);
284                     paramCollectionParam.setSfcCode(S_SFC_CODE);
285                     paramCollectionParam.setProductCode(S_PRODUCT_CODE);
286                     paramCollectionParam.setProductionLine(result.getProductionLine());
287                     paramCollectionParam.setLocationCode(result.getLocationCode());
288                     paramCollectionParam.setParamValue(value.toString());
289                     paramCollectionParam.setParamLower(result.getParamLower());
290                     paramCollectionParam.setParamUpper(result.getParamUpper());
291                     paramCollectionParam.setParamStandard(result.getParamCentral());
292                     paramCollectionParam.setCollectionTime(DateTool.getLocalTimeForDate());
293                     paramCollectionParam.setUnit(result.getUnit());
294                     paramCollectionService.add(paramCollectionParam);
295                 }
d43481 296             }
C 297         }
298
299         logger.info("handleInfo END!");
300     }
301     public void handleRepair() throws Exception{
302         RepairManageInfoParam repairManageInfoParam = new RepairManageInfoParam();
303         repairManageInfoParam.setLocationCode(LOCATION_CODE);
304         repairManageInfoParam.setManageType("2");
305         repairManageInfoParam.setUpPlc("0");
306         List<RepairManageInfoResult> list = repairManageInfoService.findListBySpec(repairManageInfoParam);
307         if(list.size()>0) {
308             RepairManageInfoResult result = list.get(0);
309
310             //自动生成唯一码
311             Random random = new Random(System.currentTimeMillis());
312             S_SFC_CODE = "BOP" + random.nextLong();
313
314
315             S_ORDER_CODE = result.getWorkOrderNo();
316             S_PRODUCT_CODE = result.getProductCode();
317             S_PRODUCT_TYPE = GlobalVariable.PRODUCT_TYPE_MAP.get(S_PRODUCT_CODE);
318             String  repairLocation = result.getPoorLocationCode();//
319
320             //根据工位编码查询工位表中配置的返修工位号
321             LocationInfoParam param = new LocationInfoParam();
322             param.setLocationCode(repairLocation);
323             List<LocationInfoResult> results = locationInfoService.findListBySpec(param);
324             if(results.size()>0) {
325                 LocationInfoResult r = results.get(0);
326                 String repairLocationNo = r.getSpareField2();
327                 if(null != repairLocationNo && "" != repairLocationNo){
328                     List<ReadWriteEntity> lists = new ArrayList<>();
329                     lists.add(new ReadWriteEntity(OPCElement.BOP010_S_ORDER_CODE,S_ORDER_CODE));
314bc7 330                     lists.add(new ReadWriteEntity(OPCElement.BOP010_S_PRODUCT_CODE,Short.parseShort(S_PRODUCT_TYPE)));
d43481 331                     lists.add(new ReadWriteEntity(OPCElement.BOP010_S_SFC_CODE,S_SFC_CODE));
C 332                     lists.add(new ReadWriteEntity(OPCElement.BOP010_P_REPAIR_LOCATION,repairLocationNo));
333                     lists.add(new ReadWriteEntity(OPCElement.BOP010_J_PC_WRITE_ORDER,true));
334                     miloService.writeToOpcUa(lists);
335                     //更新为已经返修上线
336                     Long id = result.getId();
337                     repairManageInfoParam.setId(id);
338                     repairManageInfoParam.setUpPlc("1");
339                     repairManageInfoService.update(repairManageInfoParam);
340
341                 }else{
342                     logger.info("BOP010工位,handleRepair 请查询"+repairLocation+"工位是否配置返修工位号!");
343                 }
344             }else{
345                 logger.info("BOP010工位,handleRepair 无法查询到"+repairLocation+"工位!");
346             }
347         }
348     }
349 }