whycxzp
2025-06-09 7fe1ea37e4044eb61374a51c5a2fe1ecf9e834be
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
package com.whyc.schedule;
 
import com.whyc.dto.Point;
import com.whyc.dto.Response;
import com.whyc.dto.TempPoint;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.db_alarm.BattStationTempAlarm;
import com.whyc.pojo.db_batt.BattCamera;
import com.whyc.pojo.db_param.PageParam;
import com.whyc.pojo.db_power_history.BattStationTempHistory;
import com.whyc.pojo.db_power_rt.BattStationTemp;
import com.whyc.service.BattStationTempAlarmService;
import com.whyc.service.BattStationTempHisService;
import com.whyc.service.BattStationTempService;
import com.whyc.service.PageParamService;
import com.whyc.util.DateUtil;
import com.whyc.util.HikTempUtil;
import com.whyc.util.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@EnableScheduling
@Component
@Slf4j
public class BattStationTempScheduleService {
 
    @Autowired
    private BattStationTempService service;
 
    @Autowired
    private BattStationTempHisService hisService;
 
    @Autowired
    private BattStationTempAlarmService alarmService;
 
    @Autowired
    private PageParamService paramService;
 
    /**
     * 温度实时
     * */
    @Scheduled(fixedRate = 4000,initialDelay = 2000)
    public void startRecordAndCheck(){
        List<BattCamera> ipInfoList = HikTempUtil.cameraInfoList;
        List<PageParam> tempParmList = paramService.getTempParmList();
        Integer level2AlarmTemp =  tempParmList.get(0).getValue();
        Integer level1AlarmTemp = tempParmList.get(1).getValue();
        for (int i = 0; i < ipInfoList.size(); i++) {
            BattCamera cameraInfo = ipInfoList.get(i);
            //ThreadPoolExecutorFactory.getPoolExecutor().execute(()-> {
                String ip = cameraInfo.getCameraIp();
                String cameraId = cameraInfo.getCameraId();
                Integer battGroupId = cameraInfo.getBattGroupId();
                Integer stationId = cameraInfo.getStationId();
 
                Response response = HikTempUtil.getTempPoint(ip);
                if ((Boolean) response.getData()){
                    Map<String, Object> tempMap = (Map<String, Object>) response.getData2();
                    int pixelX = (int) tempMap.get("pixelX");
                    int pixelY = (int) tempMap.get("pixelY");
                    TempPoint minTempPoint = (TempPoint) tempMap.get("minTempPoint");
                    TempPoint maxTempPoint = (TempPoint) tempMap.get("maxTempPoint");
 
                    BattStationTemp battStationTemp = new BattStationTemp();
                    battStationTemp.setPixelX(pixelX);
                    battStationTemp.setPixelY(pixelY);
                    //最大温度点位
                    battStationTemp.setMaxTemp(maxTempPoint.getTemp());
                    List<Point> maxTempPoints = maxTempPoint.getPoints();
                    //对points,里面的x,y进行x*y的字符串转换,并将所有元素用逗号隔开
                    List<String> maxPointsStr = maxTempPoints.stream().map(item -> item.getX() + "*" + item.getY()).collect(Collectors.toList());
                    String maxPoints = String.join(",", maxPointsStr);
                    battStationTemp.setMaxTempPoint(maxPoints);
                    //最小温度点位
                    battStationTemp.setMinTemp(minTempPoint.getTemp());
                    List<Point> minTempPoints = minTempPoint.getPoints();
                    //对points,里面的x,y进行x*y的字符串转换,并将所有元素用逗号隔开
                    List<String> minPointsStr = minTempPoints.stream().map(item -> item.getX() + "*" + item.getY()).collect(Collectors.toList());
                    String minPoints = String.join(",", minPointsStr);
                    battStationTemp.setMinTempPoint(minPoints);
 
                    battStationTemp.setCameraId(cameraId);
                    Date date = new Date();
                    battStationTemp.setRecordTime(date);
                    battStationTemp.setBattGroupId(battGroupId);
                    battStationTemp.setStationId(stationId);
 
                    service.addOrUpdate(battStationTemp);
                    String yearMonth = ThreadLocalUtil.format(date, 2);
                    //温度历史 - 记录到 按电池组id,月度表
                    boolean exist = hisService.existTempHisTableByMonth(battGroupId,yearMonth);
                    //不存在则创建.插入记录
                    if(!exist){
                        hisService.createTableByMonth(battGroupId,yearMonth);
                    }
                    BattStationTempHistory tempHistory = new BattStationTempHistory();
                    try {
                        BeanUtils.copyProperties(tempHistory,battStationTemp);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        throw new RuntimeException(e);
                    }
                    hisService.add(battGroupId,yearMonth,tempHistory);
                    //温度告警判断
                    //查询温度告警表,如果存在
                    BattStationTempAlarm alarmInDB = alarmService.getByBattGroupId(battGroupId);
                    if(alarmInDB != null){ // TODO 校验温度点位和逻辑,很重要!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                        //如果存在,同时当前温度大于2级,则更新温度和告警等级
                        if(battStationTemp.getMaxTemp()>=level2AlarmTemp){
                            //温度相同,查看下告警等级是否需要调整
                            //查看数据库内温度的告警等级和当前级别是否一致
                            if(alarmInDB.getTemp().floatValue() == battStationTemp.getMaxTemp()){
                                Float temp = alarmInDB.getTemp();
                                int levelInDB = alarmInDB.getLevel();
                                int levelNow;
                                if(temp>=level1AlarmTemp){
                                    levelNow = 1;
                                }else{
                                    levelNow = 2;
                                }
 
                               if(levelInDB != levelNow){ //告警等级不同,调整告警等级
                                    BattStationTempAlarm alarm = new BattStationTempAlarm();
                                    alarm.setLevel(alarmInDB.getLevel()==1?2:1);
                                    alarm.setId(alarmInDB.getId());
                                    if(!alarmInDB.getMaxTempPoint().equals(battStationTemp.getMaxTempPoint())){
                                        alarm.setMaxTempPoint(battStationTemp.getMaxTempPoint());
                                    }
                                    alarmService.updateById(alarm);
                                }else if(!alarmInDB.getMaxTempPoint().equals(battStationTemp.getMaxTempPoint())){ //告警等级相同,最大温度点位不同则更新点位
                                   BattStationTempAlarm alarm = new BattStationTempAlarm();
                                   alarm.setId(alarmInDB.getId());
                                   alarm.setMaxTempPoint(battStationTemp.getMaxTempPoint());
                                   alarmService.updateById(alarm);
                               }
 
                            }
                            //温度不同
                            else {
                                BattStationTempAlarm alarm = new BattStationTempAlarm();
                                alarm.setId(alarmInDB.getId());
                                alarm.setTemp(battStationTemp.getMaxTemp());
                                if (battStationTemp.getMaxTemp() >= level1AlarmTemp) { //温度大于1级报警温度
                                    alarm.setLevel(1);
                                } else {
                                    alarm.setLevel(2);
                                }
                                if(!alarmInDB.getMaxTempPoint().equals(battStationTemp.getMaxTempPoint())){
                                    alarm.setMaxTempPoint(battStationTemp.getMaxTempPoint());
                                }
                                alarmService.updateById(alarm);
                            }
                        }else {
                            //如果不存在了,表明告警结束,更新结束时间
                            BattStationTempAlarm alarm = new BattStationTempAlarm();
                            alarm.setId(alarmInDB.getId());
                            alarm.setEndTime(date);
                            alarmService.updateById(alarm);
                        }
                    }
 
                    // 告警表内不存在当前电池组的告警,如果超过报警温度,则生成告警
                    else if(battStationTemp.getMaxTemp()>=level2AlarmTemp){ //温度大于2级报警温度
                        BattStationTempAlarm alarm = new BattStationTempAlarm();
                        alarm.setBattGroupId(battGroupId);
                        alarm.setStationId(stationId);
                        alarm.setTemp(battStationTemp.getMaxTemp());
                        alarm.setStartTime(date);
                        if (battStationTemp.getMaxTemp()>=level1AlarmTemp){ //温度大于1级报警温度
                            alarm.setLevel(1);
                        }else{
                            alarm.setLevel(2);
                        }
                        //添加最高温度点位
                        alarm.setMaxTempPoint(battStationTemp.getMaxTempPoint());
                        alarmService.add(alarm);
                    }
 
                }else{
                    String msg = response.getMsg();
                    // TODO 报错日志记录
                }
            //});
        }
 
    }
 
 
}