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