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