|
@@ -6,6 +6,7 @@ import com.huimv.eartag2.common.dao.repo.*;
|
|
|
//import com.huimv.eartag2.process.service.IDeviceService;
|
|
|
//import com.huimv.eartag2.process.utils.DateUtil;
|
|
|
//import com.huimv.eartag2.process.utils.MathUtil;
|
|
|
+import com.huimv.eartag2.common.service.IBaseConfigService;
|
|
|
import com.huimv.eartag2.common.utils.BizConst;
|
|
|
import com.huimv.eartag2.common.vo.FarmAllStatusVo;
|
|
|
import com.huimv.eartag2.device.service.ICacheService;
|
|
@@ -16,16 +17,14 @@ import com.huimv.eartag2.device.utils.MathUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.domain.Example;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.sql.Timestamp;
|
|
|
import java.text.ParseException;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @Project : huimv.shiwan
|
|
@@ -74,6 +73,16 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
private String serviceFarmAllStatusIp;
|
|
|
@Value("${service.farmAllStatus.url}")
|
|
|
private String serviceFarmAllStatusUrl;
|
|
|
+ @Autowired
|
|
|
+ private IBaseConfigService iBaseConfigService;
|
|
|
+ @Autowired
|
|
|
+ private BasePigpenEntityRepo basePigpenRepo;
|
|
|
+ @Autowired
|
|
|
+ private EartagAbnormalAlarmEntityRepo eartagAbnormalAlarmEntityRepo;
|
|
|
+ @Autowired
|
|
|
+ private EartagAbnormalCountEntityRepo eartagAbnormalCountEntityRepo;
|
|
|
+ @Autowired
|
|
|
+ private EartagDeviceRegisterRepo eartagDeviceRegisterRepo;
|
|
|
|
|
|
/**
|
|
|
* @Method : handleDeviceHeartbeat
|
|
@@ -376,7 +385,9 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
updateDeviceOnlineWithEnvtemp(farmId, deviceCode,envtemp,todayDate,nowTimestamp);
|
|
|
|
|
|
//{检查异常温度}
|
|
|
- checkAbnormalEnvTemp(farmId,envtemp);
|
|
|
+// checkAbnormalEnvTemp(farmId,envtemp);
|
|
|
+ //--处理异常环境温度报警
|
|
|
+ handleEnvtempAlarmAndCount(dataJo,todayDate,nowTimestamp,farmId);
|
|
|
|
|
|
//判断该设备编号是否存在牧场缓存在线集合当中;
|
|
|
Boolean isExist = cacheService.isExistDeviceOnlineSet(farmId, deviceCode,todayDateText);
|
|
@@ -394,6 +405,156 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
System.out.println("## END 环境数据。"+new com.huimv.eartag2.common.utils.DateUtil().getTodayMissionText());
|
|
|
}
|
|
|
|
|
|
+ //{处理环境温度报警}
|
|
|
+ private void handleEnvtempAlarmAndCount(JSONObject dataJo, java.sql.Date todayDate, Timestamp nowTimestamp, String farmId) {
|
|
|
+ String device = dataJo.getString("device");
|
|
|
+ System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+dataJo);
|
|
|
+ Map maxAndMinMap = getMaxAndMin2(farmId, "envMaxTem", "envMinTem");
|
|
|
+ BigDecimal configMaxTempBd = new BigDecimal(maxAndMinMap.get("envMaxTem").toString());
|
|
|
+ BigDecimal configMinTempBd = new BigDecimal(maxAndMinMap.get("envMinTem").toString());
|
|
|
+ BigDecimal envTemp1Bd = new BigDecimal(dataJo.getString("temp1"));
|
|
|
+ if (envTemp1Bd.compareTo(configMaxTempBd) >= 0) {
|
|
|
+ //{}
|
|
|
+ String warningObject = getWarningObject(device);
|
|
|
+ if(warningObject == null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 保存耳标高温报警
|
|
|
+ EartagAbnormalAlarmEntity eartagAbnormalAlarmEntity = new EartagAbnormalAlarmEntity();
|
|
|
+ eartagAbnormalAlarmEntity.setFarmCode(farmId);
|
|
|
+ eartagAbnormalAlarmEntity.setContent("高温警报:["+warningObject+"]的环境温度为" + envTemp1Bd.toString() + "°,高于等于高温阈值" + configMaxTempBd.toString() + "°");
|
|
|
+ //数据:2
|
|
|
+ eartagAbnormalAlarmEntity.setAlarmType(2);
|
|
|
+ //采集器:1
|
|
|
+ eartagAbnormalAlarmEntity.setAlarmFrom(1);
|
|
|
+ eartagAbnormalAlarmEntity.setAddTime(nowTimestamp);
|
|
|
+ eartagAbnormalAlarmEntity.setAddDate(todayDate);
|
|
|
+ //环境高温:3
|
|
|
+ eartagAbnormalAlarmEntity.setAlarmSort(3);
|
|
|
+ eartagAbnormalAlarmEntityRepo.saveAndFlush(eartagAbnormalAlarmEntity);
|
|
|
+ // 高温次数记录
|
|
|
+ saveEartagTempDataCount(todayDate, nowTimestamp, farmId);
|
|
|
+ } else if (configMinTempBd.compareTo(envTemp1Bd) >= 0) {
|
|
|
+ //{}
|
|
|
+ String warningObject = getWarningObject(device);
|
|
|
+ if(warningObject == null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 保存耳标高温报警
|
|
|
+ EartagAbnormalAlarmEntity eartagAbnormalAlarmEntity = new EartagAbnormalAlarmEntity();
|
|
|
+ eartagAbnormalAlarmEntity.setFarmCode(farmId);
|
|
|
+ eartagAbnormalAlarmEntity.setContent("低温警报:["+warningObject+"]的环境温度为" + envTemp1Bd.toString() + "°,低于等于低温阈值" + configMinTempBd.toString() + "°");
|
|
|
+ //数据:2
|
|
|
+ eartagAbnormalAlarmEntity.setAlarmType(2);
|
|
|
+ //采集器:1
|
|
|
+ eartagAbnormalAlarmEntity.setAlarmFrom(1);
|
|
|
+ eartagAbnormalAlarmEntity.setAddTime(nowTimestamp);
|
|
|
+ eartagAbnormalAlarmEntity.setAddDate(todayDate);
|
|
|
+ //环境低温:4
|
|
|
+ eartagAbnormalAlarmEntity.setAlarmSort(4);
|
|
|
+ eartagAbnormalAlarmEntityRepo.saveAndFlush(eartagAbnormalAlarmEntity);
|
|
|
+ //低温次数记录
|
|
|
+ saveEartagTempDataCount(todayDate, nowTimestamp, farmId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //--读取报警对象
|
|
|
+ public String getWarningObject(String device){
|
|
|
+ EartagDeviceRegisterEntity queryEartagDeviceRegisterEntity = eartagDeviceRegisterRepo.getByDeviceCode(device);
|
|
|
+ if(queryEartagDeviceRegisterEntity == null){
|
|
|
+ log.error("该采集器设备["+device+"]不存在.");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String warningObject = "";
|
|
|
+ if(queryEartagDeviceRegisterEntity.getPigpenId()!=null){
|
|
|
+ //{读取栋舍}
|
|
|
+ warningObject = getPigpenByDevice(device);
|
|
|
+ }else{
|
|
|
+ warningObject = queryEartagDeviceRegisterEntity.getFarmerName();
|
|
|
+ }
|
|
|
+ return warningObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存异常耳标温度计数
|
|
|
+ public void saveEartagTempDataCount(java.sql.Date todayDate, Timestamp nowTimestamp, String farmCode) {
|
|
|
+ EartagAbnormalCountEntity eartagAbnormalCountEntity = new EartagAbnormalCountEntity();
|
|
|
+ eartagAbnormalCountEntity.setFarmCode(farmCode);
|
|
|
+ eartagAbnormalCountEntity.setAddDate(todayDate);
|
|
|
+ Example<EartagAbnormalCountEntity> example = Example.of(eartagAbnormalCountEntity);
|
|
|
+ Optional<EartagAbnormalCountEntity> optional = eartagAbnormalCountEntityRepo.findOne(example);
|
|
|
+ if (!optional.isPresent()) {
|
|
|
+ eartagAbnormalCountEntity.setEartagDataCount(1);
|
|
|
+ eartagAbnormalCountEntity.setEartagDataUpateTime(nowTimestamp);
|
|
|
+ eartagAbnormalCountEntityRepo.saveAndFlush(eartagAbnormalCountEntity);
|
|
|
+ } else {
|
|
|
+ EartagAbnormalCountEntity existEartagAbnormalCountEntity = optional.get();
|
|
|
+ Integer count = existEartagAbnormalCountEntity.getEartagDataCount();
|
|
|
+ if(count == null){
|
|
|
+ count = 0;
|
|
|
+ }
|
|
|
+ existEartagAbnormalCountEntity.setEartagDataCount(count + 1);
|
|
|
+ existEartagAbnormalCountEntity.setEartagDataUpateTime(nowTimestamp);
|
|
|
+ eartagAbnormalCountEntityRepo.saveAndFlush(existEartagAbnormalCountEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //--通过设备获取栋舍
|
|
|
+ private String getPigpenByDevice(String device) {
|
|
|
+ //根据设备读取栋舍
|
|
|
+ List<Object[]> pigpenList = basePigpenRepo.getPigpenByDevice(device);
|
|
|
+ if(pigpenList.size()==0){
|
|
|
+ return "";
|
|
|
+ }else{
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for(int a=0;a<pigpenList.size();a++){
|
|
|
+ Object[] objArray = pigpenList.get(0);
|
|
|
+ if(sb.length()>0){
|
|
|
+ sb.append(",");
|
|
|
+ }
|
|
|
+ sb.append(objArray[0]).append(",").append(objArray[1]);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取最大最小数值
|
|
|
+ private Map getMaxAndMin2(String farmCode, String maxKeyName, String minKeyName) {
|
|
|
+ Map configMap = new HashMap();
|
|
|
+ String hashKey = BizConst.HASH_EARTEMP_TH_PREFIX + farmCode;
|
|
|
+ // 从属性配置表取值
|
|
|
+ String confMaxTem = getConfigValue(farmCode, maxKeyName);
|
|
|
+ if (confMaxTem == null) {
|
|
|
+ log.error("属性配置:"+maxKeyName+"未配置,请联系管理员进行配置.");
|
|
|
+ }
|
|
|
+ configMap.put(maxKeyName, confMaxTem);
|
|
|
+ //获取耳标温度阈值:最小值
|
|
|
+ // 从属性配置表取值
|
|
|
+ String configMinTem = getConfigValue(farmCode, minKeyName);
|
|
|
+ if (configMinTem == null) {
|
|
|
+ log.error("属性配置:"+minKeyName+"未配置,请联系管理员进行配置.");
|
|
|
+ }
|
|
|
+ configMap.put(minKeyName, configMinTem);
|
|
|
+ return configMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从属性配置表取值
|
|
|
+ public String getConfigValue(String farmCode, String configKey) {
|
|
|
+ //从数据库读取温度阈值属性
|
|
|
+// List<SysBaseConfigEntity> configList = sysBaseConfigEntityRepo.getConfigValue(configKey, farmCode);
|
|
|
+// if (configList.size() > 0) {
|
|
|
+// SysBaseConfigEntity sysBaseConfigEntity = configList.get(0);
|
|
|
+// return sysBaseConfigEntity.getConfigValue();
|
|
|
+// } else {
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+ Map configMap = iBaseConfigService.getConfigValue(configKey,farmCode,"0");
|
|
|
+ if(configMap == null){
|
|
|
+ log.error("采用默认配置参数 configKey="+configKey);
|
|
|
+ return null;
|
|
|
+ }else{
|
|
|
+ return configMap.get("configValue")+"";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @Method : checkAbnormalEnvTemp
|
|
|
* @Description : 检查异常环境温度
|
|
@@ -777,7 +938,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
public String getFarmIdByDeviceCode(String deviceCode) {
|
|
|
EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
if(deviceRegisterEntity == null){
|
|
|
- log.error("该设备编码["+deviceCode+"]的设备已经不存在。");
|
|
|
+ log.error("该采集器设备["+deviceCode+"]的已经不存在。");
|
|
|
return null;
|
|
|
}
|
|
|
return deviceRegisterEntity.getFarmId();
|
|
@@ -788,7 +949,6 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
String farmId = deviceRegisterJo.getString("farmId");
|
|
|
//{}
|
|
|
Integer effectiveDeviceTotal = getEffectiveDeviceSum(farmId);
|
|
|
- System.out.println("## effectiveDeviceTotal >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+effectiveDeviceTotal);
|
|
|
//{设置设备总数}
|
|
|
cacheService.putDeviceTotal(effectiveDeviceTotal,farmId);
|
|
|
}
|