OPC
懒羊羊
2023-12-13 21fde820e495c08cfa0ba277685468baa5bf9118
提交 | 用户 | 时间
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;
10 import cn.stylefeng.guns.modular.cm.passingStationCollection.service.PassingStationCollectionService;
c64c9f 11 import cn.stylefeng.guns.modular.gm.greaseManage.model.params.GreaseManageParam;
C 12 import cn.stylefeng.guns.modular.gm.greaseManage.model.result.GreaseManageResult;
13 import cn.stylefeng.guns.modular.gm.greaseManage.service.GreaseManageService;
71e81e 14 import cn.stylefeng.guns.plcserver.opc.GlobalVariable;
15 import cn.stylefeng.guns.plcserver.opc.OPCComm;
16 import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit;
17
18 import cn.stylefeng.guns.plcserver.server.OP050.OP050ServerInterface;
19 import cn.stylefeng.guns.plcserver.tool.DateTool;
20 import cn.stylefeng.guns.plcserver.tool.LogUtil;
21 import org.apache.log4j.Logger;
22 import org.openscada.opc.lib.da.Group;
23 import org.openscada.opc.lib.da.Item;
24
21fde8 25 import java.text.SimpleDateFormat;
c64c9f 26 import java.time.LocalDate;
21fde8 27 import java.time.format.DateTimeFormatter;
c64c9f 28 import java.time.temporal.ChronoUnit;
71e81e 29 import java.util.Date;
30 import java.util.List;
31
32
33 public class OP050ServerInterfaceImpl implements OP050ServerInterface {
34
35     private static Logger logger;
36     LogUtil logUtil = new LogUtil();
37     static {
38         logger = Logger.getLogger(OP050ServerInterfaceImpl.class);
39     }
40
41     private OPCComm opc = OPCComm.getInstance();
42     private static Date IN_TIME = null;
43     private static String S_ORDER_CODE = "";//工单编号
44     private static String S_PRODUCT_CODE = "";//产品编号
45     private static String S_PRODUCT_TYPE= "";//产品类型
46     private static String S_PRODUCT_CODE_OLD = "";//产品编号
47     private static String S_SFC_CODE = "";//总成编码
48     private static String S_RIGHT_CODE  = "";//右壳体编码
49     private static String PRODUCTION_LINE  = "OP";//产线号
50     private static String LOCATION_CODE  = "OP050";//工位号
51     private static String GROUP_NAME  = "OP050_P";//组名
52     private static List list = null;
53     private static Group group = null;
54     @Override
55     public synchronized void monitor() {
56         String ecpStr = "";//异常记录标记
57         try{
58             if(GlobalVariable.OP050_F_HEART_BEAT) {
59                 //PC读工单
60                 String isPLCStart = OPCUnit.read(opc.getOP050_J_PLC_START());//PLC开始
61                 if(isPLCStart.equals("true")){
62                     String isFinish = OPCUnit.read(opc.getOP050_J_PC_WRITE_ORDER());//PC下发工单完成
63                     if(isFinish.equals("false")){
64                         handleOrder();
65                     }
66                 }
67
68
69                 //PC完成
70                 String isPLCFinish = OPCUnit.read(opc.getOP050_J_PLC_FINISH());//PLC完成
71                 if(isPLCFinish.equals("true")){
72                     String isFinish = OPCUnit.read(opc.getOP050_J_PC_FINISH());//PC操作完成
73                     if(isFinish.equals("false")){
74                         handleInfo();
75                         handleFinsh();
76                     }
77                 }
78
c64c9f 79                 handleOilState();
C 80
71e81e 81             }else{
82                 logger.info("OP050工位与PLC断开,请检查PLC或网络是否异常!");
83             }
84
85         }catch (Exception e) {
86             ecpStr ="出现异常:" + logUtil.getTrace(e);
87         }finally {
88             if(!"".equals(ecpStr)){
89                 logger.info(ecpStr+"\r\n");
90             }
91         }
92     }
93
c64c9f 94
C 95     public void handleOilState(){
96         GreaseManageParam param = new GreaseManageParam();
97         GreaseManageService greaseManageService = opc.greaseManageService;
98         param.setLocationCode(LOCATION_CODE);
99         List<GreaseManageResult> list = greaseManageService.findListBySpec(param);
100         if(list.size()>0) {
101             String duaDate = list.get(0).getDuaDate();
102
21fde8 103             SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
104             String currentDate = format.format(new Date());
105             long  daysBetween = getRemainder(currentDate,duaDate);
c64c9f 106             if(daysBetween>0){
C 107                 OPCUnit.write(opc.getOP050_OIL_STATE(), "false"); //写油脂状态
108             }else{
109                 OPCUnit.write(opc.getOP050_OIL_STATE(), "true"); //写油脂状态
110             }
21fde8 111             logger.info("到期日期:"+duaDate+"当前日期:"+currentDate+"剩余天数::"+daysBetween);
c64c9f 112         }
C 113     }
114
21fde8 115     public static long getRemainder(String currentDate,String tableDate){
116         DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
117         LocalDate localDate1 = LocalDate.parse(currentDate, dateFormatter);
118         LocalDate localDate2 = LocalDate.parse(tableDate, dateFormatter);
119         long days = ChronoUnit.DAYS.between(localDate1, localDate2);
120         return days;
121     }
71e81e 122     //PC读工单
123     public void handleOrder() {
124         IN_TIME = DateTool.getLocalTimeForDate();
125         S_ORDER_CODE = OPCUnit.read(opc.getOP050_S_ORDER_CODE());//工单编号
126         S_PRODUCT_TYPE = OPCUnit.read(opc.getOP050_S_PRODUCT_CODE());//产品编号
127         S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
128         S_SFC_CODE = OPCUnit.read(opc.getOP050_S_SFC_CODE());//总成编码
129         OPCUnit.write(opc.getOP050_J_PC_WRITE_ORDER(), "true"); //PC读工单完成
130         logger.info("OP050工位,handleOrder PC处理完成!");
131     }
132
133     //PC读参数信息
134     public void handleInfo() throws Exception {
135         logger.info("OP050工位,handleInfo START!");
136
137         if(null == list /*|| !S_PRODUCT_CODE_OLD.equals(S_PRODUCT_CODE)*/){
138             if(group != null){
139                 opc.getServer().removeGroup(group,true);
140             }
141             group = opc.getServer().addGroup(GROUP_NAME);
142
143             CollectionParamConfParam collectionParamConfParam = new CollectionParamConfParam();
144             //collectionParamConfParam.setProductCode(S_PRODUCT_CODE);
145             collectionParamConfParam.setLocationCode(LOCATION_CODE);
146             CollectionParamConfService collectionParamConfService = opc.collectionParamConfService;
147             list = collectionParamConfService.findListBySpec(collectionParamConfParam);
148
149
150             for(int i=0;i<list.size();i++){
151                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
152                 Item item = group.addItem(result.getGatherAddress());
153                 result.setItem(item);
154                 list.set(i,result);
155             }
156             //S_PRODUCT_CODE_OLD = S_PRODUCT_CODE;
157         }
158
159         if(null != list && list.size()>0) {
160             ParamCollectionService paramCollectionService = opc.paramCollectionService;
161             for (int i = 0; i < list.size(); i++) {
162                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
163                 Item item = result.getItem();
164                 String value = OPCUnit.read(item);//读参数值
165                 ParamCollectionParam paramCollectionParam = new ParamCollectionParam();
166                 paramCollectionParam.setParamCode(result.getParamCode());
167                 paramCollectionParam.setParamName(result.getParamName());
168                 paramCollectionParam.setWorkOrderNo(S_ORDER_CODE);
169                 paramCollectionParam.setSfcCode(S_SFC_CODE);
170                 paramCollectionParam.setProductCode(S_PRODUCT_CODE);
171                 paramCollectionParam.setProductionLine(result.getProductionLine());
172                 paramCollectionParam.setLocationCode(result.getLocationCode());
173                 paramCollectionParam.setParamValue(value);
174                 paramCollectionParam.setParamLower(result.getParamLower());
175                 paramCollectionParam.setParamUpper(result.getParamUpper());
176                 paramCollectionParam.setParamStandard(result.getParamCentral());
177                 paramCollectionParam.setCollectionTime(DateTool.getLocalTimeForDate());
178                 paramCollectionParam.setUnit(result.getUnit());
179                 paramCollectionService.add(paramCollectionParam);
180             }
181         }
182
183         logger.info("OP050工位,handleInfo END!");
184     }
185
186     //PC完成
187     public void handleFinsh() {
188
189         if("".equals(S_ORDER_CODE)){
190             IN_TIME = DateTool.getLocalTimeForDate();
191             S_ORDER_CODE = OPCUnit.read(opc.getOP050_S_ORDER_CODE());//工单编号
192             S_PRODUCT_TYPE = OPCUnit.read(opc.getOP050_S_PRODUCT_CODE());//产品编号
193             S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
194             S_SFC_CODE = OPCUnit.read(opc.getOP050_S_SFC_CODE());//总成编码
195         }
196
197         //处理过站信息
198         String S_PRODUCT_STATE_CODE = OPCUnit.read(opc.getOP050_S_PRODUCT_STATE_CODE());
199         PassingStationCollectionParam param = new PassingStationCollectionParam();
200         param.setWorkOrderNo(S_ORDER_CODE);
201         param.setProductCode(S_PRODUCT_CODE);
202         param.setSfcCode(S_SFC_CODE);
203         param.setProductionLine(PRODUCTION_LINE);
204         param.setLocationCode(LOCATION_CODE);
205         param.setInboundTime(IN_TIME);
206         param.setOutboundTime(DateTool.getLocalTimeForDate());
207         param.setOutRsSign(S_PRODUCT_STATE_CODE);//出站是否合格
208         PassingStationCollectionService passingStationCollectionService = opc.passingStationCollectionService;
209         passingStationCollectionService.add(param);
210
211         //根据分总成编码,将EOP过站数据绑定关系修改为总成编码
212         String  copSfcCode = OPCUnit.read(opc.getOP050_S_SFC_CODE_COP());//COP分总成
213         String  bopSfcCode = OPCUnit.read(opc.getOP050_S_SFC_CODE_BOP());//BOP分总成
214         passingStationCollectionService.updateSFC(S_SFC_CODE,copSfcCode);
215         passingStationCollectionService.updateSFC(S_SFC_CODE,bopSfcCode);
216
217
218         //根据分总成编码,将EOP采集数据绑定关系修改为总成编码
219         ParamCollectionService paramCollectionService = opc.paramCollectionService;
220         paramCollectionService.updateSFC(S_SFC_CODE,copSfcCode);
221         paramCollectionService.updateSFC(S_SFC_CODE,bopSfcCode);
222
223
224         OPCUnit.write(opc.getOP050_J_PC_FINISH(), "true"); //PC完成
225
226         logger.info("OP050工位,handleFinsh PC处理完成!");
227     }
228
229 }