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