cl
2024-01-19 e0fac38b26845f25de479783e0c76cf12a5311e0
提交 | 用户 | 时间
71e81e 1 package cn.stylefeng.guns.plcserver.server.OP130B.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;
10 import cn.stylefeng.guns.modular.cm.passingStationCollection.service.PassingStationCollectionService;
cf6bff 11 import cn.stylefeng.guns.modular.dq.materialTraceability.model.params.MaterialTraceabilityParam;
C 12 import cn.stylefeng.guns.modular.dq.materialTraceability.service.MaterialTraceabilityService;
13 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.params.ProductionOrderBatchInfoParam;
14 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.result.ProductionOrderBatchInfoResult;
15 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.service.ProductionOrderBatchInfoService;
16 import cn.stylefeng.guns.modular.om.productionOrderRecords.model.params.ProductionOrderRecordsParam;
17 import cn.stylefeng.guns.modular.om.productionOrderRecords.model.result.ProductionOrderRecordsResult;
18 import cn.stylefeng.guns.modular.om.productionOrderRecords.service.ProductionOrderRecordsService;
71e81e 19 import cn.stylefeng.guns.plcserver.opc.GlobalVariable;
20 import cn.stylefeng.guns.plcserver.opc.OPCComm;
21 import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit;
22 import cn.stylefeng.guns.plcserver.server.OP130.OP130ServerInterface;
23 import cn.stylefeng.guns.plcserver.server.OP130B.OP130BServerInterface;
24 import cn.stylefeng.guns.plcserver.tool.DateTool;
25 import cn.stylefeng.guns.plcserver.tool.LogUtil;
26 import org.apache.log4j.Logger;
27 import org.openscada.opc.lib.da.Group;
28 import org.openscada.opc.lib.da.Item;
29
30 import java.util.Date;
31 import java.util.List;
32
33
34 public class OP130BServerInterfaceImpl implements OP130BServerInterface {
35
36     private static Logger logger;
37     LogUtil logUtil = new LogUtil();
38     static {
39         logger = Logger.getLogger(OP130BServerInterfaceImpl.class);
40     }
41
42     private OPCComm opc = OPCComm.getInstance();
43     private static Date IN_TIME = null;
44     private static String S_ORDER_CODE = "";//工单编号
45     private static String S_PRODUCT_CODE = "";//产品编号
46     private static String S_PRODUCT_TYPE= "";//产品类型
47     private static String S_PRODUCT_CODE_OLD = "";//产品编号
48     private static String S_SFC_CODE = "";//总成编码
49     private static String S_RIGHT_CODE  = "";//右壳体编码
50     private static String PRODUCTION_LINE  = "OP";//产线号
51     private static String LOCATION_CODE  = "OP130B";//工位号
52     private static String GROUP_NAME  = "OP130B_P";//工位号
53     private static List list = null;
54     private static Group group = null;
cf6bff 55
C 56     private int count = 0;
57
58     /**
59      * 设置心跳标志位,保存到全局变量
60      */
61     public void setHeartbeat(String value) {
62         if ("false".equals(value)) {
63             OPCUnit.write(opc.getOP130B_F_HEART_BEAT(), "true");
64             GlobalVariable.OP130B_F_HEART_BEAT = true;
65             count = 0;
66         } else {
67             count++;
68         }
69         // 循环读取频率是:500ms一次,如果超过6次,也就是3s认定超时
70         if (count > GlobalVariable.HEART_MAX_COUNT) {
71             GlobalVariable.OP130B_F_HEART_BEAT = false;
72         }
73     }
74
71e81e 75     @Override
76     public synchronized void monitor() {
77         String ecpStr = "";//异常记录标记
78         try{
cf6bff 79
e0fac3 80           /*  String isHeartBeat = OPCUnit.read(opc.getOP130B_F_HEART_BEAT());
C 81             setHeartbeat(isHeartBeat);*/
cf6bff 82
71e81e 83             if(GlobalVariable.OP130B_F_HEART_BEAT) {
84
85                     //PC读工单
86                     String isPLCStart = OPCUnit.read(opc.getOP130B_J_PLC_START());//PLC开始
87                     if(isPLCStart.equals("true")){
88                         String isFinish = OPCUnit.read(opc.getOP130B_J_PC_WRITE_ORDER());//PC下发工单完成
89                         if(isFinish.equals("false")){
90                             handleOrder();
91                         }
92                     }
93
94
95                     //PC完成
96                     String isPLCFinish = OPCUnit.read(opc.getOP130B_J_PLC_FINISH());//PLC完成
97                     if(isPLCFinish.equals("true")){
98                         String isFinish = OPCUnit.read(opc.getOP130B_J_PC_FINISH());//PC操作完成
99                         if(isFinish.equals("false")){
100                             handleFinsh();
e0fac3 101                             handleInfo();
C 102                             //handleMaterial();
103
71e81e 104                         }
105                     }
106
107             }else{
108                 logger.info("OP130B工位与PLC断开,请检查PLC或网络是否异常!");
109             }
110
111         }catch (Exception e) {
112             ecpStr ="出现异常:" + logUtil.getTrace(e);
113         }finally {
114             if(!"".equals(ecpStr)){
115                 logger.info(ecpStr+"\r\n");
116             }
117         }
118     }
119
120     //PC读工单
121     public void handleOrder() {
122         IN_TIME = DateTool.getLocalTimeForDate();
123         S_ORDER_CODE = OPCUnit.read(opc.getOP130B_S_ORDER_CODE());//工单编号
124         S_PRODUCT_TYPE = OPCUnit.read(opc.getOP130B_S_PRODUCT_CODE());//产品编号
125         S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
126         S_SFC_CODE = OPCUnit.read(opc.getOP130B_S_SFC_CODE());//总成编码
127         OPCUnit.write(opc.getOP130B_J_PC_WRITE_ORDER(), "true"); //PC读工单完成
128         logger.info("OP130B工位,handleOrder PC处理完成!");
cf6bff 129     }
C 130
131     public void handleMaterial() {
132         if("".equals(S_ORDER_CODE)){
133             IN_TIME = DateTool.getLocalTimeForDate();
134             S_ORDER_CODE = OPCUnit.read(opc.getOP130B_S_ORDER_CODE());//工单编号
135             S_PRODUCT_TYPE = OPCUnit.read(opc.getOP130B_S_PRODUCT_CODE());//产品编号
136             S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
137             S_SFC_CODE = OPCUnit.read(opc.getOP130B_S_SFC_CODE());//总成编码
138         }
139         //查询批次上料信息
140         ProductionOrderBatchInfoParam productionOrderBatchInfoParam = new ProductionOrderBatchInfoParam();
141         productionOrderBatchInfoParam.setWorkOrderNo(S_ORDER_CODE);
142         productionOrderBatchInfoParam.setLocationCode(LOCATION_CODE);
143         ProductionOrderBatchInfoService productionOrderBatchInfoService = opc.productionOrderBatchInfoService;
144         List<ProductionOrderBatchInfoResult> list1 = productionOrderBatchInfoService.findListBySpec(productionOrderBatchInfoParam);
145         for(int i=0;i<list1.size();i++){
146             ProductionOrderBatchInfoResult productionOrderBatchInfoResult = list1.get(i);
147
148             //判断是否有该工位
149             String[] locationCodes = productionOrderBatchInfoResult.getLocationCode().split("/");
150             logger.info("当前工位:"+locationCodes.toString());
151             boolean isLocationCode = false;
152             if(locationCodes.length>0)
153             {
154                 for (int j=0;j<locationCodes.length;j++){
155                     String location = locationCodes[j];
156                     if(location.equals(LOCATION_CODE)){
157                         isLocationCode = true;
158                     }
159                 }
160             }
161
162             int remainingQuantity =  productionOrderBatchInfoResult.getResidueQuantity();//剩余数量
163             if(remainingQuantity>0 && isLocationCode){
164                 //新增物料追溯
165                 MaterialTraceabilityParam materialTraceabilityParam = new MaterialTraceabilityParam();
166                 materialTraceabilityParam.setWorkOrderNo(S_ORDER_CODE);
167                 materialTraceabilityParam.setProductCode(S_PRODUCT_CODE);
168                 materialTraceabilityParam.setProductNo(S_SFC_CODE);
169                 materialTraceabilityParam.setLineCode(PRODUCTION_LINE);
170                 materialTraceabilityParam.setLocationCode(LOCATION_CODE);
171                 materialTraceabilityParam.setAssemblyQty(1);
172                 materialTraceabilityParam.setAssemblyTime(DateTool.getLocalTimeForDate());
173                 materialTraceabilityParam.setMaterialCode(productionOrderBatchInfoResult.getMaterialCode());//物料编码
174                 materialTraceabilityParam.setMaterialBatchNo(productionOrderBatchInfoResult.getBatch());//物料批次
175
176                 MaterialTraceabilityService materialTraceabilityService = opc.materialTraceabilityService;
177                 materialTraceabilityService.add(materialTraceabilityParam);
178                 //更新剩余数量
179                 productionOrderBatchInfoParam.setId(productionOrderBatchInfoResult.getId());
180                 productionOrderBatchInfoParam.setProductionLine((remainingQuantity-1)+"");
181                 productionOrderBatchInfoService.update(productionOrderBatchInfoParam);
182             }
183         }
71e81e 184     }
185
186     //PC读参数信息
187     public void handleInfo() throws Exception {
188         logger.info("OP130B工位,handleInfo START!");
189
190         if(null == list /*|| !S_PRODUCT_CODE_OLD.equals(S_PRODUCT_CODE)*/){
191             if(group != null){
192                 opc.getServer().removeGroup(group,true);
193             }
194             group = opc.getServer().addGroup(GROUP_NAME);
195
196             CollectionParamConfParam collectionParamConfParam = new CollectionParamConfParam();
197             //collectionParamConfParam.setProductCode(S_PRODUCT_CODE);
198             collectionParamConfParam.setLocationCode(LOCATION_CODE);
199             CollectionParamConfService collectionParamConfService = opc.collectionParamConfService;
200             list = collectionParamConfService.findListBySpec(collectionParamConfParam);
201
202
203             for(int i=0;i<list.size();i++){
204                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
205                 Item item = group.addItem(result.getGatherAddress());
206                 result.setItem(item);
207                 list.set(i,result);
208             }
209             //S_PRODUCT_CODE_OLD = S_PRODUCT_CODE;
210         }
211
212         if(null != list && list.size()>0) {
213             ParamCollectionService paramCollectionService = opc.paramCollectionService;
214             for (int i = 0; i < list.size(); i++) {
215                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
216                 Item item = result.getItem();
217                 String value = OPCUnit.read(item);//读参数值
218                 ParamCollectionParam paramCollectionParam = new ParamCollectionParam();
219                 paramCollectionParam.setParamCode(result.getParamCode());
220                 paramCollectionParam.setParamName(result.getParamName());
221                 paramCollectionParam.setWorkOrderNo(S_ORDER_CODE);
222                 paramCollectionParam.setSfcCode(S_SFC_CODE);
223                 paramCollectionParam.setProductCode(S_PRODUCT_CODE);
224                 paramCollectionParam.setProductionLine(result.getProductionLine());
225                 paramCollectionParam.setLocationCode(result.getLocationCode());
226                 paramCollectionParam.setParamValue(value);
227                 paramCollectionParam.setParamLower(result.getParamLower());
228                 paramCollectionParam.setParamUpper(result.getParamUpper());
229                 paramCollectionParam.setParamStandard(result.getParamCentral());
230                 paramCollectionParam.setCollectionTime(DateTool.getLocalTimeForDate());
231                 paramCollectionParam.setUnit(result.getUnit());
232                 paramCollectionService.add(paramCollectionParam);
233             }
234         }
235
236         logger.info("OP130B工位,handleInfo END!");
237     }
238
239     //PC完成
240     public void handleFinsh() {
241         logger.info("OP130B工位,handleFinsh PC处理开始!");
242         if("".equals(S_ORDER_CODE)){
243             IN_TIME = DateTool.getLocalTimeForDate();
244             S_ORDER_CODE = OPCUnit.read(opc.getOP130B_S_ORDER_CODE());//工单编号
245             S_PRODUCT_TYPE = OPCUnit.read(opc.getOP130B_S_PRODUCT_CODE());//产品编号
246             S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
247             S_SFC_CODE = OPCUnit.read(opc.getOP130B_S_SFC_CODE());//总成编码
248         }
249         //处理过站信息
250         String S_PRODUCT_STATE_CODE = OPCUnit.read(opc.getOP130B_S_PRODUCT_STATE_CODE());
cf6bff 251
C 252         //修改报工记录
253         if("false".equals(S_PRODUCT_STATE_CODE)){
254             ProductionOrderRecordsParam productionOrderRecordsParam = new ProductionOrderRecordsParam();
255             productionOrderRecordsParam.setWorkOrderNo(S_ORDER_CODE);
256             productionOrderRecordsParam.setProductNo(S_SFC_CODE);
257             ProductionOrderRecordsService productionOrderRecordsService = opc.productionOrderRecordsService;
258
259             List<ProductionOrderRecordsResult> list = productionOrderRecordsService.findListBySpec(productionOrderRecordsParam);
260             if(list.size()>0){
261                 ProductionOrderRecordsResult productionOrderRecordsResult = list.get(0);
262                 productionOrderRecordsParam.setId(productionOrderRecordsResult.getId());
263                 productionOrderRecordsParam.setWhetherPass(S_PRODUCT_STATE_CODE);
264                 productionOrderRecordsService.update(productionOrderRecordsParam);
265             }
266         }
267
268
71e81e 269         PassingStationCollectionParam param = new PassingStationCollectionParam();
270         param.setWorkOrderNo(S_ORDER_CODE);
271         param.setProductCode(S_PRODUCT_CODE);
272         param.setSfcCode(S_SFC_CODE);
273         param.setProductionLine(PRODUCTION_LINE);
274         param.setLocationCode(LOCATION_CODE);
275         param.setInboundTime(IN_TIME);
276         param.setOutboundTime(DateTool.getLocalTimeForDate());
277         param.setOutRsSign(S_PRODUCT_STATE_CODE);//出站是否合格
278         PassingStationCollectionService passingStationCollectionService = opc.passingStationCollectionService;
279         passingStationCollectionService.add(param);
280
281         OPCUnit.write(opc.getOP130B_J_PC_FINISH(), "true"); //PC完成
282
283         logger.info("OP130B工位,handleFinsh PC处理完成!");
284     }
285
286 }