cl
2024-01-23 6dadf36ef118fbb3b2cd5aca94cde88e29fb55dc
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
294
295
296
297
298
299
300
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.service.EquipmentInfoService;
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.service.EquipmentAlarmService;
import cn.stylefeng.guns.modular.em.equipmentStatus.service.EquipmentStatusService;
import cn.stylefeng.guns.modular.gm.greaseManage.service.GreaseManageService;
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.model.params.ProductionOrderRecordsParam;
import cn.stylefeng.guns.modular.om.productionOrderRecords.model.result.ProductionOrderRecordsResult;
import cn.stylefeng.guns.modular.om.productionOrderRecords.service.ProductionOrderRecordsService;
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.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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Component
public class OP130Callback 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(OP130Callback.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  = "OP";//产线号
    private static String LOCATION_CODE  = "OP130";//工位号
    private static List list = null;
 
    public OP130Callback(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 {
            //心跳
            if(null != value && identifier.equals(OPCElement.OP130_F_HEART_BEAT)){
                if( "false".equals(value.toString()))
                {
                    ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP130_F_HEART_BEAT,true);
                    miloService.writeToOpcUa(entity);
                }
            }
            //PLC进站
            else if(null != value && identifier.equals(OPCElement.OP130_J_PLC_START)){
                if("true".equals(value.toString())){
                    handleOrder();
                }
            }
            //PLC出站
            else if(null != value && identifier.equals(OPCElement.OP130_J_PLC_FINISH)){
                if("true".equals(value.toString())){
                    handleFinsh();
                    handleInfo();
                    handleMaterial();
                }
            }else{
                System.out.println("值没有变");
            }
        } catch (Exception e) {
            ecpStr ="出现异常:" + logUtil.getTrace(e);
        }finally {
            if(!"".equals(ecpStr)){
                logger.info(ecpStr+"\r\n");
            }
        }
    }
 
    //PC读工单
    public void handleOrder() throws Exception {
        logger.info("handleOrder PC处理开始!");
        IN_TIME = DateTool.getLocalTimeForDate();
        List<String> lists = new ArrayList<>();
        lists.add(OPCElement.OP130_S_ORDER_CODE);
        lists.add(OPCElement.OP130_S_PRODUCT_CODE);
        lists.add(OPCElement.OP130_S_SFC_CODE);
        List<ReadWriteEntity> returnLists= miloService.readFromOpcUa(lists);
        returnLists.forEach(element->{
            switch(element.getIdentifier()){
                case OPCElement.OP130_S_ORDER_CODE:
                    S_ORDER_CODE = element.getValue().toString();
                    break;
                case OPCElement.OP130_S_PRODUCT_CODE:
                    S_PRODUCT_TYPE = element.getValue().toString();
                    S_PRODUCT_CODE = GlobalVariable.PRODUCT_CODE_MAP.get(S_PRODUCT_TYPE);
                    break;
                case OPCElement.OP130_S_SFC_CODE:
                    S_SFC_CODE = element.getValue().toString();
                    break;
                default:
                    break;
            }
        });
        ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP130_J_PC_WRITE_ORDER,true);
        miloService.writeToOpcUa(entity);
        logger.info("handleOrder PC处理完成!");
    }
 
    public void handleFinsh() throws Exception {
        logger.info("handleFinsh PC处理开始!");
        String S_PRODUCT_STATE_CODE = miloService.
                readFromOpcUa(OPCElement.OP130_S_PRODUCT_STATE_CODE)
                .getValue().toString();
 
        if("2".equals(S_PRODUCT_STATE_CODE)){
            ProductionOrderRecordsParam productionOrderRecordsParam = new ProductionOrderRecordsParam();
            productionOrderRecordsParam.setWorkOrderNo(S_ORDER_CODE);
            productionOrderRecordsParam.setProductNo(S_SFC_CODE);
            List<ProductionOrderRecordsResult> list = productionOrderRecordsService.findListBySpec(productionOrderRecordsParam);
            if(!list.isEmpty()){
                ProductionOrderRecordsResult productionOrderRecordsResult = list.get(0);
                productionOrderRecordsParam.setId(productionOrderRecordsResult.getId());
                productionOrderRecordsParam.setWhetherPass(S_PRODUCT_STATE_CODE);
                productionOrderRecordsService.update(productionOrderRecordsParam);
            }
        }
        //处理过站信息
        PassingStationCollectionParam param = new PassingStationCollectionParam();
        param.setWorkOrderNo(S_ORDER_CODE);
        param.setProductCode(S_PRODUCT_CODE);
        param.setSfcCode(S_SFC_CODE);
        param.setProductionLine(PRODUCTION_LINE);
        param.setLocationCode(LOCATION_CODE);
        param.setInboundTime(IN_TIME);
        param.setOutboundTime(DateTool.getLocalTimeForDate());
        param.setOutRsSign(S_PRODUCT_STATE_CODE);//出站是否合格
        passingStationCollectionService.add(param);
 
        ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP130_J_PC_FINISH,true);
        miloService.writeToOpcUa(entity);//写PC完成
        logger.info("handleFinsh PC处理完成!");
    }
 
    public void handleMaterial() {
 
        //查询批次上料信息
        ProductionOrderBatchInfoParam productionOrderBatchInfoParam = new ProductionOrderBatchInfoParam();
        productionOrderBatchInfoParam.setWorkOrderNo(S_ORDER_CODE);
        productionOrderBatchInfoParam.setLocationCode(LOCATION_CODE);
        List<ProductionOrderBatchInfoResult> list = productionOrderBatchInfoService.findListBySpec(productionOrderBatchInfoParam);
        for(int i=0;i<list.size();i++){
            ProductionOrderBatchInfoResult productionOrderBatchInfoResult = list.get(i);
            int remainingQuantity =  productionOrderBatchInfoResult.getResidueQuantity();//剩余数量
            if(remainingQuantity>0 ){
                //新增物料追溯
                MaterialTraceabilityParam materialTraceabilityParam = new MaterialTraceabilityParam();
                materialTraceabilityParam.setWorkOrderNo(S_ORDER_CODE);
                materialTraceabilityParam.setProductCode(S_PRODUCT_CODE);
                materialTraceabilityParam.setProductNo(S_SFC_CODE);
                materialTraceabilityParam.setLineCode(PRODUCTION_LINE);
                materialTraceabilityParam.setLocationCode(LOCATION_CODE);
                materialTraceabilityParam.setAssemblyQty(1);
                materialTraceabilityParam.setAssemblyTime(DateTool.getLocalTimeForDate());
                materialTraceabilityParam.setMaterialCode(productionOrderBatchInfoResult.getMaterialCode());//物料编码
                materialTraceabilityParam.setMaterialBatchNo(productionOrderBatchInfoResult.getBatch());//物料批次
 
                materialTraceabilityService.add(materialTraceabilityParam);
                //更新剩余数量
                productionOrderBatchInfoParam.setId(productionOrderBatchInfoResult.getId());
                productionOrderBatchInfoParam.setResidueQuantity(remainingQuantity-1);//剩余数量
                productionOrderBatchInfoService.update(productionOrderBatchInfoParam);
            }
        }
    }
 
    //PC读参数信息
    public void handleInfo() throws Exception {
        logger.info("handleInfo START!");
 
        if(null == list){
            CollectionParamConfParam collectionParamConfParam = new CollectionParamConfParam();
            collectionParamConfParam.setLocationCode(LOCATION_CODE);
            list = collectionParamConfService.findListBySpec(collectionParamConfParam);
        }
        if(null != list && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                CollectionParamConfResult result = (CollectionParamConfResult) list.get(i);
                Object value = miloService.
                        readFromOpcUa(result.getGatherAddress())
                        .getValue();//读参数值
                if(null != value) {
                    ParamCollectionParam paramCollectionParam = new ParamCollectionParam();
                    paramCollectionParam.setParamCode(result.getParamCode());
                    paramCollectionParam.setParamName(result.getParamName());
                    paramCollectionParam.setWorkOrderNo(S_ORDER_CODE);
                    paramCollectionParam.setSfcCode(S_SFC_CODE);
                    paramCollectionParam.setProductCode(S_PRODUCT_CODE);
                    paramCollectionParam.setProductionLine(result.getProductionLine());
                    paramCollectionParam.setLocationCode(result.getLocationCode());
                    paramCollectionParam.setParamValue(value.toString());
                    paramCollectionParam.setParamLower(result.getParamLower());
                    paramCollectionParam.setParamUpper(result.getParamUpper());
                    paramCollectionParam.setParamStandard(result.getParamCentral());
                    paramCollectionParam.setCollectionTime(DateTool.getLocalTimeForDate());
                    paramCollectionParam.setUnit(result.getUnit());
                    paramCollectionService.add(paramCollectionParam);
                }
            }
        }
 
        logger.info("handleInfo END!");
    }
 
}