cl
2024-01-16 cf6bff3922bbd0624b98834f6ea85c8e619e564f
提交 | 用户 | 时间
71e81e 1 package cn.stylefeng.guns.plcserver.server.OP030.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
23 import cn.stylefeng.guns.plcserver.server.OP030.OP030ServerInterface;
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 OP030ServerInterfaceImpl implements OP030ServerInterface {
35
36     private static Logger logger;
37     LogUtil logUtil = new LogUtil();
38     static {
39         logger = Logger.getLogger(OP030ServerInterfaceImpl.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  = "OP030";//工位号
52     private static String GROUP_NAME  = "OP030_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.getOP030_F_HEART_BEAT(), "true");
64             GlobalVariable.OP030_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.OP030_F_HEART_BEAT = false;
72         }
73     }
74
71e81e 75     @Override
76     public synchronized void monitor() {
77         String ecpStr = "";//异常记录标记
78         try{
cf6bff 79
C 80             String isHeartBeat = OPCUnit.read(opc.getOP030_F_HEART_BEAT());
81             setHeartbeat(isHeartBeat);
82
71e81e 83             if(GlobalVariable.OP030_F_HEART_BEAT) {
84                 //PC读工单
85                 String isPLCStart = OPCUnit.read(opc.getOP030_J_PLC_START());//PLC开始
86                 if(isPLCStart.equals("true")){
87                     String isFinish = OPCUnit.read(opc.getOP030_J_PC_WRITE_ORDER());//PC下发工单完成
88                     if(isFinish.equals("false")){
89                         handleOrder();
90                     }
91                 }
92
93
94                 //PC完成
95                 String isPLCFinish = OPCUnit.read(opc.getOP030_J_PLC_FINISH());//PLC完成
96                 if(isPLCFinish.equals("true")){
97                     String isFinish = OPCUnit.read(opc.getOP030_J_PC_FINISH());//PC操作完成
98                     if(isFinish.equals("false")){
cf6bff 99                         handleMaterial();
71e81e 100                         handleInfo();
101                         handleFinsh();
102                     }
103                 }
104
105             }else{
106                 logger.info("OP030工位与PLC断开,请检查PLC或网络是否异常!");
107             }
108
109         }catch (Exception e) {
110             ecpStr ="出现异常:" + logUtil.getTrace(e);
111         }finally {
112             if(!"".equals(ecpStr)){
113                 logger.info(ecpStr+"\r\n");
114             }
115         }
116     }
117
118     //PC读工单
119     public void handleOrder() {
120         IN_TIME = DateTool.getLocalTimeForDate();
121         S_ORDER_CODE = OPCUnit.read(opc.getOP030_S_ORDER_CODE());//工单编号
122         S_PRODUCT_TYPE = OPCUnit.read(opc.getOP030_S_PRODUCT_CODE());//产品编号
123         S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
124         S_SFC_CODE = OPCUnit.read(opc.getOP030_S_SFC_CODE());//总成编码
125         OPCUnit.write(opc.getOP030_J_PC_WRITE_ORDER(), "true"); //PC读工单完成
126         logger.info("OP030工位,handleOrder PC处理完成!");
cf6bff 127     }
C 128
129     public void handleMaterial() {
130         if("".equals(S_ORDER_CODE)){
131             IN_TIME = DateTool.getLocalTimeForDate();
132             S_ORDER_CODE = OPCUnit.read(opc.getOP030_S_ORDER_CODE());//工单编号
133             S_PRODUCT_TYPE = OPCUnit.read(opc.getOP030_S_PRODUCT_CODE());//产品编号
134             S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
135             S_SFC_CODE = OPCUnit.read(opc.getOP030_S_SFC_CODE());//总成编码
136         }
137         //查询批次上料信息
138         ProductionOrderBatchInfoParam productionOrderBatchInfoParam = new ProductionOrderBatchInfoParam();
139         productionOrderBatchInfoParam.setWorkOrderNo(S_ORDER_CODE);
140         productionOrderBatchInfoParam.setLocationCode(LOCATION_CODE);
141         ProductionOrderBatchInfoService productionOrderBatchInfoService = opc.productionOrderBatchInfoService;
142         List<ProductionOrderBatchInfoResult> list1 = productionOrderBatchInfoService.findListBySpec(productionOrderBatchInfoParam);
143         for(int i=0;i<list1.size();i++){
144             ProductionOrderBatchInfoResult productionOrderBatchInfoResult = list1.get(i);
145
146             //判断是否有该工位
147             String[] locationCodes = productionOrderBatchInfoResult.getLocationCode().split("/");
148             logger.info("当前工位:"+locationCodes.toString());
149             boolean isLocationCode = false;
150             if(locationCodes.length>0)
151             {
152                 for (int j=0;j<locationCodes.length;j++){
153                     String location = locationCodes[j];
154                     if(location.equals(LOCATION_CODE)){
155                         isLocationCode = true;
156                     }
157                 }
158             }
159
160             int remainingQuantity =  productionOrderBatchInfoResult.getResidueQuantity();//剩余数量
161             if(remainingQuantity>0 && isLocationCode){
162                 //新增物料追溯
163                 MaterialTraceabilityParam materialTraceabilityParam = new MaterialTraceabilityParam();
164                 materialTraceabilityParam.setWorkOrderNo(S_ORDER_CODE);
165                 materialTraceabilityParam.setProductCode(S_PRODUCT_CODE);
166                 materialTraceabilityParam.setProductNo(S_SFC_CODE);
167                 materialTraceabilityParam.setLineCode(PRODUCTION_LINE);
168                 materialTraceabilityParam.setLocationCode(LOCATION_CODE);
169                 materialTraceabilityParam.setAssemblyQty(1);
170                 materialTraceabilityParam.setAssemblyTime(DateTool.getLocalTimeForDate());
171                 materialTraceabilityParam.setMaterialCode(productionOrderBatchInfoResult.getMaterialCode());//物料编码
172                 materialTraceabilityParam.setMaterialBatchNo(productionOrderBatchInfoResult.getBatch());//物料批次
173
174                 MaterialTraceabilityService materialTraceabilityService = opc.materialTraceabilityService;
175                 materialTraceabilityService.add(materialTraceabilityParam);
176                 //更新剩余数量
177                 productionOrderBatchInfoParam.setId(productionOrderBatchInfoResult.getId());
178                 productionOrderBatchInfoParam.setResidueQuantity(remainingQuantity-1);//剩余数量
179                 productionOrderBatchInfoService.update(productionOrderBatchInfoParam);
180             }
181         }
71e81e 182     }
183
184     //PC读参数信息
185     public void handleInfo() throws Exception {
186         logger.info("OP030工位,handleInfo START!");
187
188         if(null == list /*|| !S_PRODUCT_CODE_OLD.equals(S_PRODUCT_CODE)*/){
189             if(group != null){
190                 opc.getServer().removeGroup(group,true);
191             }
192             group = opc.getServer().addGroup(GROUP_NAME);
193
194             CollectionParamConfParam collectionParamConfParam = new CollectionParamConfParam();
195             //collectionParamConfParam.setProductCode(S_PRODUCT_CODE);
196             collectionParamConfParam.setLocationCode(LOCATION_CODE);
197             CollectionParamConfService collectionParamConfService = opc.collectionParamConfService;
198             list = collectionParamConfService.findListBySpec(collectionParamConfParam);
199
200
201             for(int i=0;i<list.size();i++){
202                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
203                 Item item = group.addItem(result.getGatherAddress());
204                 result.setItem(item);
205                 list.set(i,result);
206             }
207             //S_PRODUCT_CODE_OLD = S_PRODUCT_CODE;
208         }
209
210         if(null != list && list.size()>0) {
211             ParamCollectionService paramCollectionService = opc.paramCollectionService;
212             for (int i = 0; i < list.size(); i++) {
213                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
214                 Item item = result.getItem();
215                 String value = OPCUnit.read(item);//读参数值
216                 ParamCollectionParam paramCollectionParam = new ParamCollectionParam();
217                 paramCollectionParam.setParamCode(result.getParamCode());
218                 paramCollectionParam.setParamName(result.getParamName());
219                 paramCollectionParam.setWorkOrderNo(S_ORDER_CODE);
220                 paramCollectionParam.setSfcCode(S_SFC_CODE);
221                 paramCollectionParam.setProductCode(S_PRODUCT_CODE);
222                 paramCollectionParam.setProductionLine(result.getProductionLine());
223                 paramCollectionParam.setLocationCode(result.getLocationCode());
224                 paramCollectionParam.setParamValue(value);
225                 paramCollectionParam.setParamLower(result.getParamLower());
226                 paramCollectionParam.setParamUpper(result.getParamUpper());
227                 paramCollectionParam.setParamStandard(result.getParamCentral());
228                 paramCollectionParam.setCollectionTime(DateTool.getLocalTimeForDate());
229                 paramCollectionParam.setUnit(result.getUnit());
230                 paramCollectionService.add(paramCollectionParam);
231             }
232         }
233
234         logger.info("OP030工位,handleInfo END!");
235     }
236
237     //PC完成
238     public void handleFinsh() {
239         if("".equals(S_ORDER_CODE)){
240             IN_TIME = DateTool.getLocalTimeForDate();
241             S_ORDER_CODE = OPCUnit.read(opc.getOP030_S_ORDER_CODE());//工单编号
242             S_PRODUCT_TYPE = OPCUnit.read(opc.getOP030_S_PRODUCT_CODE());//产品编号
243             S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
244             S_SFC_CODE = OPCUnit.read(opc.getOP030_S_SFC_CODE());//总成编码
245         }
246         //处理过站信息
247         String S_PRODUCT_STATE_CODE = OPCUnit.read(opc.getOP030_S_PRODUCT_STATE_CODE());
cf6bff 248
C 249         //修改报工记录
250         if("false".equals(S_PRODUCT_STATE_CODE)){
251             ProductionOrderRecordsParam productionOrderRecordsParam = new ProductionOrderRecordsParam();
252             productionOrderRecordsParam.setWorkOrderNo(S_ORDER_CODE);
253             productionOrderRecordsParam.setProductNo(S_SFC_CODE);
254             ProductionOrderRecordsService productionOrderRecordsService = opc.productionOrderRecordsService;
255
256             List<ProductionOrderRecordsResult> list = productionOrderRecordsService.findListBySpec(productionOrderRecordsParam);
257             if(list.size()>0){
258                 ProductionOrderRecordsResult productionOrderRecordsResult = list.get(0);
259                 productionOrderRecordsParam.setId(productionOrderRecordsResult.getId());
260                 productionOrderRecordsParam.setWhetherPass(S_PRODUCT_STATE_CODE);
261                 productionOrderRecordsService.update(productionOrderRecordsParam);
262             }
263         }
264
265
71e81e 266         PassingStationCollectionParam param = new PassingStationCollectionParam();
267         param.setWorkOrderNo(S_ORDER_CODE);
268         param.setProductCode(S_PRODUCT_CODE);
269         param.setSfcCode(S_SFC_CODE);
270         param.setProductionLine(PRODUCTION_LINE);
271         param.setLocationCode(LOCATION_CODE);
272         param.setInboundTime(IN_TIME);
273         param.setOutboundTime(DateTool.getLocalTimeForDate());
274         param.setOutRsSign(S_PRODUCT_STATE_CODE);//出站是否合格
275         PassingStationCollectionService passingStationCollectionService = opc.passingStationCollectionService;
276         passingStationCollectionService.add(param);
277
278         OPCUnit.write(opc.getOP030_J_PC_FINISH(), "true"); //PC完成
279
280         logger.info("OP030工位,handleFinsh PC处理完成!");
281     }
282
283 }