OPC
懒羊羊
2023-12-13 21fde820e495c08cfa0ba277685468baa5bf9118
提交 | 用户 | 时间
71e81e 1 package cn.stylefeng.guns.plcserver.server.BOP030.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;
21fde8 11 import cn.stylefeng.guns.modular.gm.greaseManage.model.params.GreaseManageParam;
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.BOP030.BOP030ServerInterface;
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;
26 import java.time.LocalDate;
27 import java.time.format.DateTimeFormatter;
28 import java.time.temporal.ChronoUnit;
71e81e 29 import java.util.Date;
30 import java.util.List;
31
32
33 public class BOP030ServerInterfaceImpl implements BOP030ServerInterface {
34
35     private static Logger logger;
36     LogUtil logUtil = new LogUtil();
37     static {
38         logger = Logger.getLogger(BOP030ServerInterfaceImpl.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  = "BOP";//产线号
50     private static String LOCATION_CODE  = "BOP030";//工位号
51     private static String GROUP_NAME  = "BOP030_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.BOP030_F_HEART_BEAT) {
59                 //PC读工单
60                 String isPLCStart = OPCUnit.read(opc.getBOP030_J_PLC_START());//PLC开始
61                 if(isPLCStart.equals("true")){
62                     String isFinish = OPCUnit.read(opc.getBOP030_J_PC_WRITE_ORDER());//PC下发工单完成
63                     if(isFinish.equals("false")){
64                         handleOrder();
65                     }
66                 }
67
68
69                 //PC完成
70                 String isPLCFinish = OPCUnit.read(opc.getBOP030_J_PLC_FINISH());//PLC完成
71                 if(isPLCFinish.equals("true")){
72                     String isFinish = OPCUnit.read(opc.getBOP030_J_PC_FINISH());//PC操作完成
73                     if(isFinish.equals("false")){
74                         handleInfo();
75                         handleFinsh();
76                     }
77                 }
21fde8 78                 handleOilState();
71e81e 79             }else{
80                 logger.info("BOP030工位与PLC断开,请检查PLC或网络是否异常!");
81             }
82
83         }catch (Exception e) {
84             ecpStr ="出现异常:" + logUtil.getTrace(e);
85         }finally {
86             if(!"".equals(ecpStr)){
87                 logger.info(ecpStr+"\r\n");
88             }
89         }
90     }
91
21fde8 92     public void handleOilState(){
93         GreaseManageParam param = new GreaseManageParam();
94         GreaseManageService greaseManageService = opc.greaseManageService;
95         param.setLocationCode(LOCATION_CODE);
96         List<GreaseManageResult> list = greaseManageService.findListBySpec(param);
97         if(list.size()>0) {
98             String duaDate = list.get(0).getDuaDate();
99
100             SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
101             String currentDate = format.format(new Date());
102             long  daysBetween = getRemainder(currentDate,duaDate);
103             if(daysBetween>0){
104                 OPCUnit.write(opc.getBOP030_OIL_STATE(), "false"); //写油脂状态
105             }else{
106                 OPCUnit.write(opc.getBOP030_OIL_STATE(), "true"); //写油脂状态
107             }
108             logger.info("到期日期:"+duaDate+"当前日期:"+currentDate+"剩余天数::"+daysBetween);
109         }
110     }
111
112     public static long getRemainder(String currentDate,String tableDate){
113         DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
114         LocalDate localDate1 = LocalDate.parse(currentDate, dateFormatter);
115         LocalDate localDate2 = LocalDate.parse(tableDate, dateFormatter);
116         long days = ChronoUnit.DAYS.between(localDate1, localDate2);
117         return days;
118     }
119
71e81e 120     //PC读工单
121     public void handleOrder() {
122         IN_TIME = DateTool.getLocalTimeForDate();
123         S_ORDER_CODE = OPCUnit.read(opc.getBOP030_S_ORDER_CODE());//工单编号
124         S_PRODUCT_TYPE = OPCUnit.read(opc.getBOP030_S_PRODUCT_CODE());//产品编号
125         S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
126         S_SFC_CODE = OPCUnit.read(opc.getBOP030_S_SFC_CODE());//总成编码
127         OPCUnit.write(opc.getBOP030_J_PC_WRITE_ORDER(), "true"); //PC读工单完成
128         logger.info("BOP030工位,handleOrder PC处理完成!");
129     }
130
131     //PC读参数信息
132     public void handleInfo() throws Exception {
133         logger.info("BOP030工位,handleInfo START!");
134
135         if(null == list /*|| !S_PRODUCT_CODE_OLD.equals(S_PRODUCT_CODE)*/){
136             if(group != null){
137                 opc.getServer().removeGroup(group,true);
138             }
139             group = opc.getServer().addGroup(GROUP_NAME);
140
141             CollectionParamConfParam collectionParamConfParam = new CollectionParamConfParam();
142             //collectionParamConfParam.setProductCode(S_PRODUCT_CODE);
143             collectionParamConfParam.setLocationCode(LOCATION_CODE);
144             CollectionParamConfService collectionParamConfService = opc.collectionParamConfService;
145             list = collectionParamConfService.findListBySpec(collectionParamConfParam);
146
147
148             for(int i=0;i<list.size();i++){
149                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
150                 Item item = group.addItem(result.getGatherAddress());
151                 result.setItem(item);
152                 list.set(i,result);
153             }
154             //S_PRODUCT_CODE_OLD = S_PRODUCT_CODE;
155         }
156
157         if(null != list && list.size()>0) {
158             ParamCollectionService paramCollectionService = opc.paramCollectionService;
159             for (int i = 0; i < list.size(); i++) {
160                 CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
161                 Item item = result.getItem();
162                 String value = OPCUnit.read(item);//读参数值
163                 ParamCollectionParam paramCollectionParam = new ParamCollectionParam();
164                 paramCollectionParam.setParamCode(result.getParamCode());
165                 paramCollectionParam.setParamName(result.getParamName());
166                 paramCollectionParam.setWorkOrderNo(S_ORDER_CODE);
167                 paramCollectionParam.setSfcCode(S_SFC_CODE);
168                 paramCollectionParam.setProductCode(S_PRODUCT_CODE);
169                 paramCollectionParam.setProductionLine(result.getProductionLine());
170                 paramCollectionParam.setLocationCode(result.getLocationCode());
171                 paramCollectionParam.setParamValue(value);
172                 paramCollectionParam.setParamLower(result.getParamLower());
173                 paramCollectionParam.setParamUpper(result.getParamUpper());
174                 paramCollectionParam.setParamStandard(result.getParamCentral());
175                 paramCollectionParam.setCollectionTime(DateTool.getLocalTimeForDate());
176                 paramCollectionParam.setUnit(result.getUnit());
177                 paramCollectionService.add(paramCollectionParam);
178             }
179         }
180
181         logger.info("BOP030工位,handleInfo END!");
182     }
183
184     //PC完成
185     public void handleFinsh() {
186         if("".equals(S_ORDER_CODE)){
187             IN_TIME = DateTool.getLocalTimeForDate();
188             S_ORDER_CODE = OPCUnit.read(opc.getBOP030_S_ORDER_CODE());//工单编号
189             S_PRODUCT_TYPE = OPCUnit.read(opc.getBOP030_S_PRODUCT_CODE());//产品编号
190             S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
191             S_SFC_CODE = OPCUnit.read(opc.getBOP030_S_SFC_CODE());//总成编码
192         }
193         //处理过站信息
194         String S_PRODUCT_STATE_CODE = OPCUnit.read(opc.getBOP030_S_PRODUCT_STATE_CODE());
195         PassingStationCollectionParam param = new PassingStationCollectionParam();
196         param.setWorkOrderNo(S_ORDER_CODE);
197         param.setProductCode(S_PRODUCT_CODE);
198         param.setSfcCode(S_SFC_CODE);
199         param.setProductionLine(PRODUCTION_LINE);
200         param.setLocationCode(LOCATION_CODE);
201         param.setInboundTime(IN_TIME);
202         param.setOutboundTime(DateTool.getLocalTimeForDate());
203         param.setOutRsSign(S_PRODUCT_STATE_CODE);//出站是否合格
204         PassingStationCollectionService passingStationCollectionService = opc.passingStationCollectionService;
205         passingStationCollectionService.add(param);
206
207         OPCUnit.write(opc.getBOP030_J_PC_FINISH(), "true"); //PC完成
208
209         logger.info("BOP030工位,handleFinsh PC处理完成!");
210     }
211
212 }