cl
2024-01-22 314bc7a1733856bd82c9d99bdfa27b34a4a762c3
提交 | 用户 | 时间
d43481 1 package cn.stylefeng.guns.plcserver.callback;
C 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.bs.equipmentInfo.model.params.EquipmentInfoParam;
8 import cn.stylefeng.guns.modular.bs.equipmentInfo.model.result.EquipmentInfoResult;
9 import cn.stylefeng.guns.modular.bs.equipmentInfo.service.EquipmentInfoService;
10 import cn.stylefeng.guns.modular.bs.locationInfo.model.params.LocationInfoParam;
11 import cn.stylefeng.guns.modular.bs.locationInfo.model.result.LocationInfoResult;
12 import cn.stylefeng.guns.modular.bs.locationInfo.service.LocationInfoService;
13 import cn.stylefeng.guns.modular.cm.paramCollection.model.params.ParamCollectionParam;
14 import cn.stylefeng.guns.modular.cm.paramCollection.service.ParamCollectionService;
15 import cn.stylefeng.guns.modular.cm.passingStationCollection.model.params.PassingStationCollectionParam;
16 import cn.stylefeng.guns.modular.cm.passingStationCollection.service.PassingStationCollectionService;
17 import cn.stylefeng.guns.modular.dq.materialTraceability.model.params.MaterialTraceabilityParam;
18 import cn.stylefeng.guns.modular.dq.materialTraceability.service.MaterialTraceabilityService;
19 import cn.stylefeng.guns.modular.em.equipmentAlarm.model.params.EquipmentAlarmParam;
20 import cn.stylefeng.guns.modular.em.equipmentAlarm.service.EquipmentAlarmService;
21 import cn.stylefeng.guns.modular.em.equipmentStatus.model.params.EquipmentStatusParam;
22 import cn.stylefeng.guns.modular.em.equipmentStatus.model.result.EquipmentStatusResult;
23 import cn.stylefeng.guns.modular.em.equipmentStatus.service.EquipmentStatusService;
24 import cn.stylefeng.guns.modular.gm.greaseManage.model.params.GreaseManageParam;
25 import cn.stylefeng.guns.modular.gm.greaseManage.model.result.GreaseManageResult;
26 import cn.stylefeng.guns.modular.gm.greaseManage.service.GreaseManageService;
27 import cn.stylefeng.guns.modular.om.productionOrdeInfo.model.params.ProductionOrdeInfoParam;
28 import cn.stylefeng.guns.modular.om.productionOrdeInfo.model.result.ProductionOrdeInfoResult;
29 import cn.stylefeng.guns.modular.om.productionOrdeInfo.service.ProductionOrdeInfoService;
30 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.params.ProductionOrderBatchInfoParam;
31 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.model.result.ProductionOrderBatchInfoResult;
32 import cn.stylefeng.guns.modular.om.productionOrderBatchInfo.service.ProductionOrderBatchInfoService;
33 import cn.stylefeng.guns.modular.om.productionOrderRecords.service.ProductionOrderRecordsService;
34 import cn.stylefeng.guns.modular.sc.repairManageInfo.model.params.RepairManageInfoParam;
35 import cn.stylefeng.guns.modular.sc.repairManageInfo.model.result.RepairManageInfoResult;
36 import cn.stylefeng.guns.modular.sc.repairManageInfo.service.RepairManageInfoService;
37 import cn.stylefeng.guns.modular.sc.serialNumbersConf.service.SerialNumbersConfService;
38 import cn.stylefeng.guns.plcserver.opc.GlobalVariable;
39 import cn.stylefeng.guns.plcserver.opc.OPCElement;
40 import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit;
41 import cn.stylefeng.guns.plcserver.tool.DateTool;
42 import cn.stylefeng.guns.plcserver.tool.LogUtil;
43 import com.kangaroohy.milo.model.ReadWriteEntity;
44 import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
45 import com.kangaroohy.milo.service.MiloService;
46 import org.apache.log4j.Logger;
47 import org.openscada.opc.lib.da.Item;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.stereotype.Component;
50
51 import java.text.SimpleDateFormat;
52 import java.time.LocalDate;
53 import java.time.format.DateTimeFormatter;
54 import java.time.temporal.ChronoUnit;
55 import java.util.ArrayList;
56 import java.util.Date;
57 import java.util.List;
58 import java.util.Random;
59
60 @Component
61 public class StateCallback implements SubscriptionCallback {
62
63     public MiloService miloService;
64
65     @Autowired
66     public ProductionOrdeInfoService productionOrdeInfoService;
67     @Autowired
68     public PassingStationCollectionService passingStationCollectionService;
69     @Autowired
70     public ParamCollectionService paramCollectionService;
71     @Autowired
72     public CollectionParamConfService collectionParamConfService;
73     @Autowired
74     public SerialNumbersConfService serialNumbersConfService;
75     @Autowired
76     private EquipmentStatusService equipmentStatusService;
77
78     @Autowired
79     private EquipmentAlarmService equipmentAlarmService;
80     @Autowired
81     private MaterialTraceabilityService materialTraceabilityService;
82     @Autowired
83     private EquipmentInfoService equipmentInfoService;
84     @Autowired
85     private RepairManageInfoService repairManageInfoService;
86
87     @Autowired
88     private LocationInfoService locationInfoService;
89
90     @Autowired
91     private GreaseManageService greaseManageService;
92
93     @Autowired
94     private ProductionOrderRecordsService productionOrderRecordsService;
95
96     @Autowired
97     private ProductionOrderBatchInfoService productionOrderBatchInfoService;
98
99     private static Logger logger;
100     LogUtil logUtil = new LogUtil();
101     static {
102         logger = Logger.getLogger(StateCallback.class);
103     }
104     private static Date IN_TIME = null;
105     private static String S_ORDER_CODE = "";//工单编号
106     private static String S_PRODUCT_CODE = "";//产品编号
107     private static String S_PRODUCT_TYPE= "";//产品类型
108     private static String S_SFC_CODE = "";//总成编码
109     private static String PRODUCTION_LINE  = "BOP";//产线号
110     private static String LOCATION_CODE  = "BOP010";//工位号
111     private static List list = null;
112
113     public StateCallback(MiloService miloService,
114                          ProductionOrdeInfoService productionOrdeInfoService,
115                          PassingStationCollectionService passingStationCollectionService,
116                          ParamCollectionService paramCollectionService,
117                          CollectionParamConfService collectionParamConfService,
118                          SerialNumbersConfService serialNumbersConfService,
119                          EquipmentStatusService equipmentStatusService,
120                          EquipmentAlarmService equipmentAlarmService,
121                          MaterialTraceabilityService materialTraceabilityService,
122                          EquipmentInfoService equipmentInfoService,
123                          RepairManageInfoService repairManageInfoService,
124                          LocationInfoService locationInfoService,
125                          GreaseManageService greaseManageService,
126                          ProductionOrderRecordsService productionOrderRecordsService,
127                          ProductionOrderBatchInfoService productionOrderBatchInfoService) {
128         this.miloService = miloService;
129         this.productionOrdeInfoService = productionOrdeInfoService;
130         this.passingStationCollectionService = passingStationCollectionService;
131         this.paramCollectionService = paramCollectionService;
132         this.collectionParamConfService = collectionParamConfService;
133         this.serialNumbersConfService = serialNumbersConfService;
134         this.equipmentStatusService = equipmentStatusService;
135         this.equipmentAlarmService = equipmentAlarmService;
136         this.materialTraceabilityService = materialTraceabilityService;
137         this.equipmentInfoService = equipmentInfoService;
138         this.repairManageInfoService = repairManageInfoService;
139         this.locationInfoService = locationInfoService;
140         this.greaseManageService = greaseManageService;
141         this.productionOrderRecordsService = productionOrderRecordsService;
142         this.productionOrderBatchInfoService = productionOrderBatchInfoService;
143     }
144     @Override
145     public void onSubscribe(String identifier, Object value) {
146         String ecpStr = "";//异常记录标记
147         try {
148             handleInfo();
149         } catch (Exception e) {
150             ecpStr ="出现异常:" + logUtil.getTrace(e);
151         }finally {
152             if(!"".equals(ecpStr)){
153                 logger.info(ecpStr+"\r\n");
154             }
155         }
156     }
157     public void handleInfo() throws Exception {
158         logger.info("OP100工位,handleInfo START!");
159
160         if(null == list ){
161             EquipmentInfoParam equipmentInfoParam = new EquipmentInfoParam();
162             list = equipmentInfoService.findListBySpec(equipmentInfoParam);
163         }
164         if(null != list && list.size()>0) {
165             for (int i = 0; i < list.size(); i++) {
166                 EquipmentInfoResult result = (EquipmentInfoResult) list.get(i);
167                 Item item = result.getItem();
168                 String value = OPCUnit.read(item);//读参数值  设备状态
169                 String equipmentNo = result.getEquipmentNo();
170
171                 EquipmentStatusParam equipmentStatusParam = new EquipmentStatusParam();
172                 equipmentStatusParam.setEquipmentNo(equipmentNo);
173                 List statuslist = equipmentStatusService.findListBySpec(equipmentStatusParam);
174                 if(statuslist.size()>0){
175                     EquipmentStatusResult equipmentStatusResult = (EquipmentStatusResult) statuslist.get(0);
176                     if(!value.equals(equipmentStatusResult.getStatus())){
177                         equipmentStatusParam.setId(equipmentStatusResult.getId());
178                         equipmentStatusParam.setStatus(value);
179                         equipmentStatusParam.setUpdateTime(DateTool.getLocalTimeForDate());
180                         equipmentStatusService.update(equipmentStatusParam);//更新设备状态
181
182                         //添加设备报警
183                         if("3".equals(value)){
184                             EquipmentAlarmParam equipmentAlarmParam = new EquipmentAlarmParam();
185                             equipmentAlarmParam.setEquipmentNo(equipmentNo);
186                             equipmentAlarmParam.setEquipmentName(result.getEquipmentName());
187                             equipmentAlarmParam.setWorkshopCode(result.getWorkshopCode());
188                             equipmentAlarmParam.setProductionLineCode(result.getProductionLineCode());
189                             equipmentAlarmParam.setLocationCode(result.getLocationCode());
190                             equipmentAlarmParam.setErrorDesc(result.getEquipmentName()+"报警!");
191                             equipmentAlarmParam.setAlarmTime(DateTool.getLocalTimeForDate());
192                             equipmentAlarmService.add(equipmentAlarmParam);
193                         }
194                     }
195
196                 }else{
197                     equipmentStatusParam.setEquipmentName(result.getEquipmentName());
198                     equipmentStatusParam.setWorkshopCode(result.getWorkshopCode());
199                     equipmentStatusParam.setProductionLineCode(result.getProductionLineCode());
200                     equipmentStatusParam.setLocationCode(result.getLocationCode());
201                     equipmentStatusParam.setStatus(value);
202                     equipmentStatusParam.setUpdateTime(DateTool.getLocalTimeForDate());
203                     equipmentStatusService.add(equipmentStatusParam);
204
205                     //添加设备报警
206                     if("3".equals(value)){
207                         EquipmentAlarmParam equipmentAlarmParam = new EquipmentAlarmParam();
208                         equipmentAlarmParam.setEquipmentNo(equipmentNo);
209                         equipmentAlarmParam.setEquipmentName(result.getEquipmentName());
210                         equipmentAlarmParam.setWorkshopCode(result.getWorkshopCode());
211                         equipmentAlarmParam.setProductionLineCode(result.getProductionLineCode());
212                         equipmentAlarmParam.setLocationCode(result.getLocationCode());
213                         equipmentAlarmParam.setErrorDesc(result.getEquipmentName()+"报警!");
214                         equipmentAlarmParam.setAlarmTime(DateTool.getLocalTimeForDate());
215                         equipmentAlarmService.add(equipmentAlarmParam);
216                     }
217                 }
218
219             }
220         }
221
222
223         Thread.sleep(60*1000);
224     }
225
226     public void handleOilStateDOP020(String LOCATION_CODE) throws Exception {
227         GreaseManageParam param = new GreaseManageParam();
228         param.setLocationCode(LOCATION_CODE);
229         List<GreaseManageResult> list = greaseManageService.findListBySpec(param);
230         if(!list.isEmpty()) {
231             String duaDate = list.get(0).getDuaDate();
232
233             SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
234             String currentDate = format.format(new Date());
235             long  daysBetween = getRemainder(currentDate,duaDate);
236             if(daysBetween>0){
237                 ReadWriteEntity entity = new ReadWriteEntity(OPCElement.DOP020_OIL_STATE,false);
238                 miloService.writeToOpcUa(entity);//写PC完成
239             }else{
240                 ReadWriteEntity entity = new ReadWriteEntity(OPCElement.DOP020_OIL_STATE,true);
241                 miloService.writeToOpcUa(entity);//写PC完成
242             }
243         }
244     }
245
246     public  void handleOilStateOP050(String LOCATION_CODE) throws Exception {
247         GreaseManageParam param = new GreaseManageParam();
248         param.setLocationCode(LOCATION_CODE);
249         List<GreaseManageResult> list = greaseManageService.findListBySpec(param);
250         if(!list.isEmpty()) {
251             String duaDate = list.get(0).getDuaDate();
252
253             SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
254             String currentDate = format.format(new Date());
255             long  daysBetween = getRemainder(currentDate,duaDate);
256             if(daysBetween>0){
257                 ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP050_OIL_STATE,false);
258                 miloService.writeToOpcUa(entity);//写PC完成
259             }else{
260                 ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP050_OIL_STATE,true);
261                 miloService.writeToOpcUa(entity);//写PC完成
262             }
263         }
264     }
265
266     public  void handleOilStateBOP030(String LOCATION_CODE) throws Exception {
267         GreaseManageParam param = new GreaseManageParam();
268         param.setLocationCode(LOCATION_CODE);
269         List<GreaseManageResult> list = greaseManageService.findListBySpec(param);
270         if(!list.isEmpty()) {
271             String duaDate = list.get(0).getDuaDate();
272
273             SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
274             String currentDate = format.format(new Date());
275             long  daysBetween = getRemainder(currentDate,duaDate);
276             if(daysBetween>0){
277                 ReadWriteEntity entity = new ReadWriteEntity(OPCElement.BOP030_OIL_STATE,false);
278                 miloService.writeToOpcUa(entity);//写PC完成
279             }else{
280                 ReadWriteEntity entity = new ReadWriteEntity(OPCElement.BOP030_OIL_STATE,true);
281                 miloService.writeToOpcUa(entity);//写PC完成
282             }
283         }
284     }
285
286     public static long getRemainder(String currentDate,String tableDate){
287         DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
288         LocalDate localDate1 = LocalDate.parse(currentDate, dateFormatter);
289         LocalDate localDate2 = LocalDate.parse(tableDate, dateFormatter);
290         long days = ChronoUnit.DAYS.between(localDate1, localDate2);
291         return days;
292     }
293 }