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