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