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