cl
2024-01-16 cf6bff3922bbd0624b98834f6ea85c8e619e564f
提交 | 用户 | 时间
71e81e 1 package cn.stylefeng.guns.plcserver.server.OP050.impl;
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.cm.paramCollection.model.params.ParamCollectionParam;
8 import cn.stylefeng.guns.modular.cm.paramCollection.service.ParamCollectionService;
9 import cn.stylefeng.guns.modular.cm.passingStationCollection.model.params.PassingStationCollectionParam;
cf6bff 10 import cn.stylefeng.guns.modular.cm.passingStationCollection.model.result.PassingStationCollectionResult;
71e81e 11 import cn.stylefeng.guns.modular.cm.passingStationCollection.service.PassingStationCollectionService;
cf6bff 12 import cn.stylefeng.guns.modular.dq.materialTraceability.model.params.MaterialTraceabilityParam;
C 13 import cn.stylefeng.guns.modular.dq.materialTraceability.service.MaterialTraceabilityService;
c64c9f 14 import cn.stylefeng.guns.modular.gm.greaseManage.model.params.GreaseManageParam;
C 15 import cn.stylefeng.guns.modular.gm.greaseManage.model.result.GreaseManageResult;
16 import cn.stylefeng.guns.modular.gm.greaseManage.service.GreaseManageService;
cf6bff 17 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.params.ProductionOrderBatchInfoParam;
C 18 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.result.ProductionOrderBatchInfoResult;
19 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.service.ProductionOrderBatchInfoService;
20 import cn.stylefeng.guns.modular.om.productionOrderRecords.model.params.ProductionOrderRecordsParam;
21 import cn.stylefeng.guns.modular.om.productionOrderRecords.model.result.ProductionOrderRecordsResult;
22 import cn.stylefeng.guns.modular.om.productionOrderRecords.service.ProductionOrderRecordsService;
71e81e 23 import cn.stylefeng.guns.plcserver.opc.GlobalVariable;
24 import cn.stylefeng.guns.plcserver.opc.OPCComm;
25 import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit;
26
27 import cn.stylefeng.guns.plcserver.server.OP050.OP050ServerInterface;
28 import cn.stylefeng.guns.plcserver.tool.DateTool;
29 import cn.stylefeng.guns.plcserver.tool.LogUtil;
30 import org.apache.log4j.Logger;
31 import org.openscada.opc.lib.da.Group;
32 import org.openscada.opc.lib.da.Item;
33
21fde8 34 import java.text.SimpleDateFormat;
c64c9f 35 import java.time.LocalDate;
21fde8 36 import java.time.format.DateTimeFormatter;
c64c9f 37 import java.time.temporal.ChronoUnit;
71e81e 38 import java.util.Date;
39 import java.util.List;
40
41
42 public class OP050ServerInterfaceImpl implements OP050ServerInterface {
43
44     private static Logger logger;
45     LogUtil logUtil = new LogUtil();
46     static {
47         logger = Logger.getLogger(OP050ServerInterfaceImpl.class);
48     }
49
50     private OPCComm opc = OPCComm.getInstance();
51     private static Date IN_TIME = null;
52     private static String S_ORDER_CODE = "";//工单编号
53     private static String S_PRODUCT_CODE = "";//产品编号
54     private static String S_PRODUCT_TYPE= "";//产品类型
55     private static String S_PRODUCT_CODE_OLD = "";//产品编号
56     private static String S_SFC_CODE = "";//总成编码
57     private static String S_RIGHT_CODE  = "";//右壳体编码
58     private static String PRODUCTION_LINE  = "OP";//产线号
59     private static String LOCATION_CODE  = "OP050";//工位号
60     private static String GROUP_NAME  = "OP050_P";//组名
61     private static List list = null;
62     private static Group group = null;
cf6bff 63
C 64     private int count = 0;
65
66     /**
67      * 设置心跳标志位,保存到全局变量
68      */
69     public void setHeartbeat(String value) {
70         if ("false".equals(value)) {
71             OPCUnit.write(opc.getOP050_F_HEART_BEAT(), "true");
72             GlobalVariable.OP050_F_HEART_BEAT = true;
73             count = 0;
74         } else {
75             count++;
76         }
77         // 循环读取频率是:500ms一次,如果超过6次,也就是3s认定超时
78         if (count > GlobalVariable.HEART_MAX_COUNT) {
79             GlobalVariable.OP050_F_HEART_BEAT = false;
80         }
81     }
82
71e81e 83     @Override
84     public synchronized void monitor() {
85         String ecpStr = "";//异常记录标记
86         try{
cf6bff 87
C 88             String isHeartBeat = OPCUnit.read(opc.getOP050_F_HEART_BEAT());
89             setHeartbeat(isHeartBeat);
90
71e81e 91             if(GlobalVariable.OP050_F_HEART_BEAT) {
92                 //PC读工单
93                 String isPLCStart = OPCUnit.read(opc.getOP050_J_PLC_START());//PLC开始
94                 if(isPLCStart.equals("true")){
95                     String isFinish = OPCUnit.read(opc.getOP050_J_PC_WRITE_ORDER());//PC下发工单完成
96                     if(isFinish.equals("false")){
97                         handleOrder();
98                     }
99                 }
100
101
102                 //PC完成
103                 String isPLCFinish = OPCUnit.read(opc.getOP050_J_PLC_FINISH());//PLC完成
104                 if(isPLCFinish.equals("true")){
105                     String isFinish = OPCUnit.read(opc.getOP050_J_PC_FINISH());//PC操作完成
106                     if(isFinish.equals("false")){
cf6bff 107                         handleMaterial();
71e81e 108                         handleInfo();
109                         handleFinsh();
110                     }
111                 }
112
c64c9f 113                 handleOilState();
C 114
71e81e 115             }else{
116                 logger.info("OP050工位与PLC断开,请检查PLC或网络是否异常!");
117             }
118
119         }catch (Exception e) {
120             ecpStr ="出现异常:" + logUtil.getTrace(e);
121         }finally {
122             if(!"".equals(ecpStr)){
123                 logger.info(ecpStr+"\r\n");
124             }
125         }
126     }
127
c64c9f 128
C 129     public void handleOilState(){
130         GreaseManageParam param = new GreaseManageParam();
131         GreaseManageService greaseManageService = opc.greaseManageService;
132         param.setLocationCode(LOCATION_CODE);
133         List<GreaseManageResult> list = greaseManageService.findListBySpec(param);
134         if(list.size()>0) {
135             String duaDate = list.get(0).getDuaDate();
136
21fde8 137             SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
138             String currentDate = format.format(new Date());
139             long  daysBetween = getRemainder(currentDate,duaDate);
c64c9f 140             if(daysBetween>0){
C 141                 OPCUnit.write(opc.getOP050_OIL_STATE(), "false"); //写油脂状态
142             }else{
143                 OPCUnit.write(opc.getOP050_OIL_STATE(), "true"); //写油脂状态
144             }
cf6bff 145             //logger.info("到期日期:"+duaDate+"当前日期:"+currentDate+"剩余天数::"+daysBetween);
c64c9f 146         }
C 147     }
148
21fde8 149     public static long getRemainder(String currentDate,String tableDate){
150         DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
151         LocalDate localDate1 = LocalDate.parse(currentDate, dateFormatter);
152         LocalDate localDate2 = LocalDate.parse(tableDate, dateFormatter);
153         long days = ChronoUnit.DAYS.between(localDate1, localDate2);
154         return days;
155     }
71e81e 156     //PC读工单
157     public void handleOrder() {
158         IN_TIME = DateTool.getLocalTimeForDate();
159         S_ORDER_CODE = OPCUnit.read(opc.getOP050_S_ORDER_CODE());//工单编号
160         S_PRODUCT_TYPE = OPCUnit.read(opc.getOP050_S_PRODUCT_CODE());//产品编号
161         S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
162         S_SFC_CODE = OPCUnit.read(opc.getOP050_S_SFC_CODE());//总成编码
163         OPCUnit.write(opc.getOP050_J_PC_WRITE_ORDER(), "true"); //PC读工单完成
164         logger.info("OP050工位,handleOrder PC处理完成!");
cf6bff 165     }
C 166
167     public void handleMaterial() {
168         if("".equals(S_ORDER_CODE)){
169             IN_TIME = DateTool.getLocalTimeForDate();
170             S_ORDER_CODE = OPCUnit.read(opc.getOP050_S_ORDER_CODE());//工单编号
171             S_PRODUCT_TYPE = OPCUnit.read(opc.getOP050_S_PRODUCT_CODE());//产品编号
172             S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
173             S_SFC_CODE = OPCUnit.read(opc.getOP050_S_SFC_CODE());//总成编码
174         }
175         //查询批次上料信息
176         ProductionOrderBatchInfoParam productionOrderBatchInfoParam = new ProductionOrderBatchInfoParam();
177         productionOrderBatchInfoParam.setWorkOrderNo(S_ORDER_CODE);
178         productionOrderBatchInfoParam.setLocationCode(LOCATION_CODE);
179         ProductionOrderBatchInfoService productionOrderBatchInfoService = opc.productionOrderBatchInfoService;
180         List<ProductionOrderBatchInfoResult> list1 = productionOrderBatchInfoService.findListBySpec(productionOrderBatchInfoParam);
181         for(int i=0;i<list1.size();i++){
182             ProductionOrderBatchInfoResult productionOrderBatchInfoResult = list1.get(i);
183
184             //判断是否有该工位
185             String[] locationCodes = productionOrderBatchInfoResult.getLocationCode().split("/");
186             logger.info("当前工位:"+locationCodes.toString());
187             boolean isLocationCode = false;
188             if(locationCodes.length>0)
189             {
190                 for (int j=0;j<locationCodes.length;j++){
191                     String location = locationCodes[j];
192                     if(location.equals(LOCATION_CODE)){
193                         isLocationCode = true;
194                     }
195                 }
196             }
197
198             int remainingQuantity =  productionOrderBatchInfoResult.getResidueQuantity();//剩余数量
199             if(remainingQuantity>0 && isLocationCode){
200                 //新增物料追溯
201                 MaterialTraceabilityParam materialTraceabilityParam = new MaterialTraceabilityParam();
202                 materialTraceabilityParam.setWorkOrderNo(S_ORDER_CODE);
203                 materialTraceabilityParam.setProductCode(S_PRODUCT_CODE);
204                 materialTraceabilityParam.setProductNo(S_SFC_CODE);
205                 materialTraceabilityParam.setLineCode(PRODUCTION_LINE);
206                 materialTraceabilityParam.setLocationCode(LOCATION_CODE);
207                 materialTraceabilityParam.setAssemblyQty(1);
208                 materialTraceabilityParam.setAssemblyTime(DateTool.getLocalTimeForDate());
209                 materialTraceabilityParam.setMaterialCode(productionOrderBatchInfoResult.getMaterialCode());//物料编码
210                 materialTraceabilityParam.setMaterialBatchNo(productionOrderBatchInfoResult.getBatch());//物料批次
211
212                 MaterialTraceabilityService materialTraceabilityService = opc.materialTraceabilityService;
213                 materialTraceabilityService.add(materialTraceabilityParam);
214                 //更新剩余数量
215                 productionOrderBatchInfoParam.setId(productionOrderBatchInfoResult.getId());
216                 productionOrderBatchInfoParam.setResidueQuantity(remainingQuantity-1);//剩余数量
217                 productionOrderBatchInfoService.update(productionOrderBatchInfoParam);
218             }
219         }
71e81e 220     }
221
222     //PC读参数信息
223     public void handleInfo() throws Exception {
224         logger.info("OP050工位,handleInfo START!");
225
226         if(null == list /*|| !S_PRODUCT_CODE_OLD.equals(S_PRODUCT_CODE)*/){
227             if(group != null){
228                 opc.getServer().removeGroup(group,true);
229             }
230             group = opc.getServer().addGroup(GROUP_NAME);
231
232             CollectionParamConfParam collectionParamConfParam = new CollectionParamConfParam();
233             //collectionParamConfParam.setProductCode(S_PRODUCT_CODE);
234             collectionParamConfParam.setLocationCode(LOCATION_CODE);
235             CollectionParamConfService collectionParamConfService = opc.collectionParamConfService;
236             list = collectionParamConfService.findListBySpec(collectionParamConfParam);
237
238
239             for(int i=0;i<list.size();i++){
240                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
241                 Item item = group.addItem(result.getGatherAddress());
242                 result.setItem(item);
243                 list.set(i,result);
244             }
245             //S_PRODUCT_CODE_OLD = S_PRODUCT_CODE;
246         }
247
248         if(null != list && list.size()>0) {
249             ParamCollectionService paramCollectionService = opc.paramCollectionService;
250             for (int i = 0; i < list.size(); i++) {
251                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
252                 Item item = result.getItem();
253                 String value = OPCUnit.read(item);//读参数值
254                 ParamCollectionParam paramCollectionParam = new ParamCollectionParam();
255                 paramCollectionParam.setParamCode(result.getParamCode());
256                 paramCollectionParam.setParamName(result.getParamName());
257                 paramCollectionParam.setWorkOrderNo(S_ORDER_CODE);
258                 paramCollectionParam.setSfcCode(S_SFC_CODE);
259                 paramCollectionParam.setProductCode(S_PRODUCT_CODE);
260                 paramCollectionParam.setProductionLine(result.getProductionLine());
261                 paramCollectionParam.setLocationCode(result.getLocationCode());
262                 paramCollectionParam.setParamValue(value);
263                 paramCollectionParam.setParamLower(result.getParamLower());
264                 paramCollectionParam.setParamUpper(result.getParamUpper());
265                 paramCollectionParam.setParamStandard(result.getParamCentral());
266                 paramCollectionParam.setCollectionTime(DateTool.getLocalTimeForDate());
267                 paramCollectionParam.setUnit(result.getUnit());
268                 paramCollectionService.add(paramCollectionParam);
269             }
270         }
271
272         logger.info("OP050工位,handleInfo END!");
273     }
274
275     //PC完成
276     public void handleFinsh() {
277
278         if("".equals(S_ORDER_CODE)){
279             IN_TIME = DateTool.getLocalTimeForDate();
280             S_ORDER_CODE = OPCUnit.read(opc.getOP050_S_ORDER_CODE());//工单编号
281             S_PRODUCT_TYPE = OPCUnit.read(opc.getOP050_S_PRODUCT_CODE());//产品编号
282             S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
283             S_SFC_CODE = OPCUnit.read(opc.getOP050_S_SFC_CODE());//总成编码
284         }
285
286         //处理过站信息
287         String S_PRODUCT_STATE_CODE = OPCUnit.read(opc.getOP050_S_PRODUCT_STATE_CODE());
cf6bff 288
71e81e 289         PassingStationCollectionParam param = new PassingStationCollectionParam();
290         param.setWorkOrderNo(S_ORDER_CODE);
291         param.setProductCode(S_PRODUCT_CODE);
292         param.setSfcCode(S_SFC_CODE);
293         param.setProductionLine(PRODUCTION_LINE);
294         param.setLocationCode(LOCATION_CODE);
295         param.setInboundTime(IN_TIME);
296         param.setOutboundTime(DateTool.getLocalTimeForDate());
297         param.setOutRsSign(S_PRODUCT_STATE_CODE);//出站是否合格
298         PassingStationCollectionService passingStationCollectionService = opc.passingStationCollectionService;
299         passingStationCollectionService.add(param);
300
cf6bff 301
C 302
71e81e 303         String  copSfcCode = OPCUnit.read(opc.getOP050_S_SFC_CODE_COP());//COP分总成
304         String  bopSfcCode = OPCUnit.read(opc.getOP050_S_SFC_CODE_BOP());//BOP分总成
cf6bff 305
C 306         //根据分总成编码,将EOP物料数据绑定关系修改为总成编码
307         MaterialTraceabilityService materialTraceabilityService = opc.materialTraceabilityService;
308         materialTraceabilityService.updateSFC(S_SFC_CODE,copSfcCode);
309         materialTraceabilityService.updateSFC(S_SFC_CODE,bopSfcCode);
310
311         //根据分总成编码,将EOP过站数据绑定关系修改为总成编码
71e81e 312         passingStationCollectionService.updateSFC(S_SFC_CODE,copSfcCode);
313         passingStationCollectionService.updateSFC(S_SFC_CODE,bopSfcCode);
314
315
316         //根据分总成编码,将EOP采集数据绑定关系修改为总成编码
317         ParamCollectionService paramCollectionService = opc.paramCollectionService;
318         paramCollectionService.updateSFC(S_SFC_CODE,copSfcCode);
319         paramCollectionService.updateSFC(S_SFC_CODE,bopSfcCode);
320
cf6bff 321         logger.info("S_SFC_CODE:"+S_SFC_CODE);
C 322         logger.info("copSfcCode:"+copSfcCode);
323         logger.info("bopSfcCode:"+bopSfcCode);
324
325         //查询COP和BOP是否合格
326         PassingStationCollectionParam passingStationCollectionParam = new PassingStationCollectionParam();
327         passingStationCollectionParam.setSfcCode(S_SFC_CODE);
328         List<PassingStationCollectionResult> passingStationCollectionResultList =
329                 opc.passingStationCollectionService.findListBySpec(passingStationCollectionParam);
330
331         String isProductState = "true";
332         for(int i=0;i<passingStationCollectionResultList.size();i++){
333             PassingStationCollectionResult passingStationCollectionResult = passingStationCollectionResultList.get(i);
334             if("false".equals(passingStationCollectionResult.getOutRsSign())){
335                 isProductState = "false";
336                 break;
337             }
338         }
339         //修改报工记录
340         if("false".equals(S_PRODUCT_STATE_CODE) || "false".equals(isProductState)){
341             ProductionOrderRecordsParam productionOrderRecordsParam = new ProductionOrderRecordsParam();
342             productionOrderRecordsParam.setWorkOrderNo(S_ORDER_CODE);
343             productionOrderRecordsParam.setProductNo(S_SFC_CODE);
344             ProductionOrderRecordsService productionOrderRecordsService = opc.productionOrderRecordsService;
345
346             List<ProductionOrderRecordsResult> list = productionOrderRecordsService.findListBySpec(productionOrderRecordsParam);
347             if(list.size()>0){
348                 ProductionOrderRecordsResult productionOrderRecordsResult = list.get(0);
349                 productionOrderRecordsParam.setId(productionOrderRecordsResult.getId());
350                 productionOrderRecordsParam.setWhetherPass("false");
351                 productionOrderRecordsService.update(productionOrderRecordsParam);
352             }
353         }
354
71e81e 355
356         OPCUnit.write(opc.getOP050_J_PC_FINISH(), "true"); //PC完成
357
358         logger.info("OP050工位,handleFinsh PC处理完成!");
359     }
360
361 }