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){
|
//如果存在,同时当前温度大于2级,则更新温度和告警等级
|
if(battStationTemp.getMaxTemp()>=level2AlarmTemp){
|
//温度相同,查看下告警等级是否一样
|
if(alarmInDB.getTemp().floatValue() == battStationTemp.getMaxTemp()){
|
if((alarmInDB.getTemp().floatValue() == level2AlarmTemp
|
&& alarmInDB.getLevel() == 2)
|
||
|
(alarmInDB.getTemp().floatValue() == level1AlarmTemp
|
&& alarmInDB.getLevel() == 1)
|
){
|
|
}else{ //告警等级不同,调整告警等级
|
BattStationTempAlarm alarm = new BattStationTempAlarm();
|
alarm.setLevel(alarmInDB.getLevel()==1?2:1);
|
alarm.setId(alarmInDB.getId());
|
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);
|
}
|
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);
|
}
|
alarmService.add(alarm);
|
}
|
|
}else{
|
String msg = response.getMsg();
|
// TODO 报错日志记录
|
}
|
});
|
}
|
|
}
|
|
|
}
|