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