admin
2024-01-10 7f291910791bdb9a545dc12ef36c3c359cfa4e59
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
package cn.stylefeng.guns.plcserver.server.state.impl;
 
 
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.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.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.plcserver.opc.GlobalVariable;
import cn.stylefeng.guns.plcserver.opc.OPCComm;
import cn.stylefeng.guns.plcserver.opc.unit.OPCUnit;
import cn.stylefeng.guns.plcserver.server.OP100.OP100ServerInterface;
import cn.stylefeng.guns.plcserver.server.state.StateServerInterface;
import cn.stylefeng.guns.plcserver.tool.DateTool;
import cn.stylefeng.guns.plcserver.tool.LogUtil;
import org.apache.log4j.Logger;
import org.openscada.opc.lib.da.Group;
import org.openscada.opc.lib.da.Item;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
public class StateServerInterfaceImpl implements StateServerInterface {
 
    private static Logger logger;
    LogUtil logUtil = new LogUtil();
    static {
        logger = Logger.getLogger(StateServerInterfaceImpl.class);
    }
 
    private OPCComm opc = OPCComm.getInstance();
 
    private static String GROUP_NAME  = "STATE_GROUP";//组
    private static List list = null;
    private static Group group = null;
 
    @Override
    public void monitor() {
        String ecpStr = "";//异常记录标记
        try{
            handleInfo();
 
        }catch (Exception e) {
            ecpStr ="出现异常:" + logUtil.getTrace(e);
        }finally {
            if(!"".equals(ecpStr)){
                logger.info(ecpStr+"\r\n");
            }
        }
    }
 
 
    //PC读参数信息
    public void handleInfo() throws Exception {
        logger.info("OP100工位,handleInfo START!");
 
        if(null == list /*|| !S_PRODUCT_CODE_OLD.equals(S_PRODUCT_CODE)*/){
            if(group != null){
                opc.getServer().removeGroup(group,true);
            }
            group = opc.getServer().addGroup(GROUP_NAME);
 
            EquipmentInfoParam equipmentInfoParam = new EquipmentInfoParam();
            EquipmentInfoService equipmentInfoService = opc.equipmentInfoService;
            list = equipmentInfoService.findListBySpec(equipmentInfoParam);
 
            for(int i=0;i<list.size();i++){
                EquipmentInfoResult result = (EquipmentInfoResult) list.get(i);
                Item item = group.addItem(result.getSpareField1());
                result.setItem(item);
                list.set(i,result);
            }
        }
        if(null != list && list.size()>0) {
            EquipmentStatusService equipmentStatusService = opc.equipmentStatusService;
 
            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 equipmentAlarmService = opc.equipmentAlarmService;
                            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 equipmentAlarmService = opc.equipmentAlarmService;
                        equipmentAlarmService.add(equipmentAlarmParam);
                    }
                }
 
            }
        }
 
    }
 
 
}