|
@@ -103,9 +103,9 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
|
|
|
//Step1:更新设备注册表[设置活动状态为1,设置活动状态更新时间]
|
|
|
//Step2:更新在线统计[心跳数+1,心跳第一次上传时间,心跳最后一次时间],预先判断该数据是否存在,如果不存在要先添加记录,如果存在就修改数据
|
|
|
-
|
|
|
+ String updateType="设备心跳";
|
|
|
//# 更新设备注册表 #
|
|
|
- updateDeviceRegister(farmId, deviceCode,nowTimestamp);
|
|
|
+ updateDeviceRegister(farmId, deviceCode,nowTimestamp,updateType);
|
|
|
//# 更新在线统计 #
|
|
|
updateDeviceOnlineCount(farmId, deviceCode,todayDateText,nowTimestamp,todayDate);
|
|
|
|
|
@@ -118,8 +118,9 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
updateDeviceAllStatus(farmId, deviceCode,todayDateText,nowTimestamp,todayDate);
|
|
|
|
|
|
//# 将新设备编号加入在线缓存 #
|
|
|
- cacheService.putDeviceToOnline(farmId, deviceCode,todayDateText);
|
|
|
+ cacheService.putDeviceToOnlineSet(farmId, deviceCode,todayDateText);
|
|
|
}
|
|
|
+ System.out.println("## END 心跳.");
|
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
}
|
|
|
|
|
@@ -194,7 +195,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
Object[] deviceRegObj = (Object[]) deviceRegList.get(0);
|
|
|
Integer total = Integer.parseInt(deviceRegObj[0].toString());
|
|
|
//设置缓存
|
|
|
- cacheService.putDeviceCancel(total,farmId);
|
|
|
+ cacheService.putDeviceTotal(total,farmId);
|
|
|
//设备总数
|
|
|
return total;
|
|
|
}
|
|
@@ -207,10 +208,12 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
if(deviceCancel != null) {
|
|
|
return deviceCancel;
|
|
|
}else {
|
|
|
- List<Object[]> deviceRegList = deviceRegisterRepo.getDeviceCountByFarmId(farmId);
|
|
|
- Object[] deviceRegObj = (Object[]) deviceRegList.get(0);
|
|
|
+ //注销状态
|
|
|
+ Integer liveStatus = 2;
|
|
|
+ List<Object[]> deviceCancelList = deviceRegisterRepo.getDeviceCountByFarmIdAndLiveStatus(farmId,liveStatus);
|
|
|
+ Object[] deviceRegObj = (Object[]) deviceCancelList.get(0);
|
|
|
Integer total = Integer.parseInt(deviceRegObj[0].toString());
|
|
|
- //设置缓存
|
|
|
+ //设置注销缓存
|
|
|
cacheService.putDeviceCancel(total,farmId);
|
|
|
//设备总数
|
|
|
return total;
|
|
@@ -257,32 +260,39 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
}
|
|
|
|
|
|
//更新设备注册表
|
|
|
- private void updateDeviceRegister(String farmId, String deviceCode, Timestamp nowTimestamp) {
|
|
|
+ private void updateDeviceRegister(String farmId, String deviceCode, Timestamp nowTimestamp,String updateType) {
|
|
|
//--更新mysql
|
|
|
EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
if (deviceRegisterEntity != null) {
|
|
|
//设备注册数据存在
|
|
|
deviceRegisterEntity.setActiveStatus(1);
|
|
|
- deviceRegisterEntity.setActiveTime(new Timestamp(new Date().getTime()));
|
|
|
+ deviceRegisterEntity.setActiveTime(nowTimestamp);
|
|
|
+ deviceRegisterEntity.setUpdateType(updateType);
|
|
|
deviceRegisterRepo.saveAndFlush(deviceRegisterEntity);
|
|
|
//更新缓存中设备注册信息
|
|
|
cacheService.putDeviceRegister(deviceCode,deviceRegisterEntity);
|
|
|
}else{
|
|
|
//设备注册数据不存在
|
|
|
//新建设备注册记录
|
|
|
- EartagDeviceRegisterEntity newDeviceRegisterEntity = new EartagDeviceRegisterEntity();
|
|
|
- newDeviceRegisterEntity.setDeviceCode(deviceCode);
|
|
|
- newDeviceRegisterEntity.setFarmId(farmId);
|
|
|
- newDeviceRegisterEntity.setRegisterTime(nowTimestamp);
|
|
|
- newDeviceRegisterEntity.setActiveStatus(1);
|
|
|
- newDeviceRegisterEntity.setActiveTime(nowTimestamp);
|
|
|
- newDeviceRegisterEntity.setDeviceStatus(1);
|
|
|
- deviceRegisterRepo.saveAndFlush(newDeviceRegisterEntity);
|
|
|
- //更新缓存中设备注册信息
|
|
|
- cacheService.putDeviceRegister(deviceCode,newDeviceRegisterEntity);
|
|
|
+ newDeviceRegister(farmId,deviceCode,nowTimestamp,updateType);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ //新建设备注册记录
|
|
|
+ public void newDeviceRegister(String farmId,String deviceCode,Timestamp nowTimestamp,String updateType){
|
|
|
+ EartagDeviceRegisterEntity newDeviceRegisterEntity = new EartagDeviceRegisterEntity();
|
|
|
+ newDeviceRegisterEntity.setDeviceCode(deviceCode);
|
|
|
+ newDeviceRegisterEntity.setFarmId(farmId);
|
|
|
+ newDeviceRegisterEntity.setRegisterTime(nowTimestamp);
|
|
|
+ newDeviceRegisterEntity.setActiveStatus(1);
|
|
|
+ newDeviceRegisterEntity.setActiveTime(nowTimestamp);
|
|
|
+ newDeviceRegisterEntity.setDeviceStatus(1);
|
|
|
+ newDeviceRegisterEntity.setUpdateType(updateType);
|
|
|
+ deviceRegisterRepo.saveAndFlush(newDeviceRegisterEntity);
|
|
|
+ //更新缓存中设备注册信息
|
|
|
+ cacheService.putDeviceRegister(deviceCode,newDeviceRegisterEntity);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @Method : handleDeviceEnvtemp
|
|
|
* @Description : 处理设备环境温度数据
|
|
@@ -301,165 +311,247 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
//2、更新设备注册表(活动状态、活动更新时间,更新类型)
|
|
|
//3、更新在线统计表(环境温度计数,环境温度最早上传时间,最新环境温度,环境温度最后一次上传时间)
|
|
|
//4、更新总状态表
|
|
|
- //保存设备心跳记录
|
|
|
-// saveDeviceHeartbeat(dataJo);
|
|
|
- //{更新设备注册表}
|
|
|
-// updateDeviceRegister(dataJo);
|
|
|
- //{更新在线统计表}
|
|
|
-// updateDeviceOnline(dataJo);
|
|
|
- //{更新总状态表}
|
|
|
-// updateAllStatus(dataJo);
|
|
|
- //------------------------------------------------------------------------------------
|
|
|
String deviceCode = dataJo.getString("device");
|
|
|
String farmId = getFarmIdByDeviceCode(deviceCode);
|
|
|
-
|
|
|
+ String temp = dataJo.getString("temp");
|
|
|
+ String envtemp = com.huimv.eartag2.common.utils.MathUtil.countEnvtemp(temp,2).toString();
|
|
|
String todayDateText = new com.huimv.eartag2.common.utils.DateUtil().getTodayDateText();
|
|
|
java.sql.Date todayDate = new java.sql.Date(new java.util.Date().getTime());
|
|
|
Timestamp nowTimestamp = new Timestamp(new java.util.Date().getTime());
|
|
|
|
|
|
- //# 更新在线统计 #
|
|
|
- updateDeviceOnlineWithEnvtemp(farmId, deviceCode);
|
|
|
-// System.out.println("## ++++1");
|
|
|
- //# 更新设备注册表 #
|
|
|
- updateDeviceRegister(farmId, deviceCode,nowTimestamp);
|
|
|
-
|
|
|
- // //# 更新设备注册表 #
|
|
|
-// updateDeviceRegister(farmId, deviceCode,nowTimestamp);
|
|
|
-// //# 更新在线统计 #
|
|
|
-// updateDeviceOnlineWithEnvtemp(farmId, deviceCode);
|
|
|
-
|
|
|
+ //1.保存环境温度流水
|
|
|
+ saveDeviceTemp(dataJo);
|
|
|
+ String updateType = "环境温度";
|
|
|
+ //2.{更新设备注册表}
|
|
|
+ updateDeviceRegister(farmId, deviceCode,nowTimestamp,updateType);
|
|
|
+ //3.{更新在线统计表}
|
|
|
+ updateDeviceOnlineWithEnvtemp(farmId, deviceCode,envtemp,todayDate,nowTimestamp);
|
|
|
//判断该设备编号是否存在牧场缓存在线集合当中;
|
|
|
Boolean isExist = cacheService.isExistDeviceOnlineSet(farmId, deviceCode,todayDateText);
|
|
|
- System.out.println("## 处理环境温度 isExist>>"+isExist);
|
|
|
if (!isExist) {
|
|
|
- //Step1:更新总状态表[device_online+1,device_offline-1,device_rate],预先判断该数据是否存在,如果不存在要先添加记录,如果存在就修改数据
|
|
|
- //Step2:更新设备注册表[active_status=1,active_time=当前时间]
|
|
|
- //Step3:更新在线统计[],预先判断该数据是否存在,如果不存在要先添加记录,如果存在就修改数据
|
|
|
- //Step4:将新设备编号加入缓存
|
|
|
- System.out.println("## ----");
|
|
|
- //# 更新总状态表 #
|
|
|
- updateEnvtempOfDeviceAllStatus(farmId, deviceCode);
|
|
|
- //# 将新设备编号加入在线缓存 #
|
|
|
- cacheService.putDeviceToOnline(farmId, deviceCode, todayDateText);
|
|
|
- System.out.println("## END");
|
|
|
+ //4.{更新总状态表}
|
|
|
+ updateEnvtempOfDeviceAllStatus(farmId, todayDateText,todayDate,nowTimestamp);
|
|
|
+ //{将新设备编号加入在线缓存}
|
|
|
+ cacheService.putDeviceToOnlineSet(farmId, deviceCode, todayDateText);
|
|
|
}
|
|
|
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
+ System.out.println("## END 环境数据");
|
|
|
}
|
|
|
|
|
|
- public void updateEnvtempOfDeviceAllStatus(String farmId, String deviceCode) throws ParseException {
|
|
|
-// System.out.println("## (A)");
|
|
|
- String todayDateText = new com.huimv.eartag2.common.utils.DateUtil().getTodayDateText();
|
|
|
+ //更新设备环境信息
|
|
|
+ public void updateEnvtempOfDeviceAllStatus(String farmId, String todayDateText, java.sql.Date todayDate, Timestamp nowTimestamp) throws ParseException {
|
|
|
+ //{获取处于工作状态的设备数量}
|
|
|
+ Integer deviceTotal = getDeviceTotalByFarmIdFromDeviceRegister(farmId);
|
|
|
+ //设备在线数量
|
|
|
+ Integer deviceOnlineSum = cacheService.getDeviceOnlineSum(farmId,todayDateText);
|
|
|
+ deviceOnlineSum++;
|
|
|
+ //设备离线数量
|
|
|
+ Integer deviceOfflineSum = 0;
|
|
|
+ if(deviceTotal > deviceOnlineSum){
|
|
|
+ deviceOfflineSum = deviceTotal - deviceOnlineSum;
|
|
|
+ }
|
|
|
+ //设备在线率
|
|
|
+ BigDecimal deviceRateBd= new BigDecimal(deviceOnlineSum).divide(new BigDecimal(deviceTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ Float deviceOnlineRate = Float.parseFloat(deviceRateBd.toString());
|
|
|
+ //{获取注销设备数量}
|
|
|
+ Integer deviceCancelSum = getDeviceCancelTotalFromDeviceRegister(farmId);
|
|
|
+ //耳标总数量
|
|
|
+ Integer eartagTotal = eartagService.getEartagRegTotalByFarmId(farmId);
|
|
|
+ //耳标在线数量
|
|
|
+ Integer eartagOnlineSum = eartagService.getEartagOnelineTotal(farmId,todayDateText);
|
|
|
+ //耳标在线率
|
|
|
+ BigDecimal eartagRateBd= new BigDecimal(eartagOnlineSum).divide(new BigDecimal(eartagTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ Float eartagOnlineRate = Float.parseFloat(eartagRateBd.toString());
|
|
|
//
|
|
|
EartarFarmAllStatusEntity farmAllStatusEntity = eartagFarmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
|
|
|
if(farmAllStatusEntity == null){
|
|
|
-// System.out.println("## (A.1)");
|
|
|
- //设备总状态表信息不存在
|
|
|
- //创建设备状态记录并更新缓存cache()
|
|
|
- createNewFarmAllStatus(farmId,deviceCode);
|
|
|
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
+ //{新建设备状态记录并更新缓存cache}
|
|
|
+ createNewFarmAllStatus(farmId,todayDate,nowTimestamp,deviceTotal,deviceOnlineSum,deviceOfflineSum,deviceCancelSum,deviceOnlineRate,eartagTotal,eartagOnlineSum,eartagOnlineRate);
|
|
|
}else{
|
|
|
-// System.out.println("## (A.2)");
|
|
|
- //设备总状态表信息存在
|
|
|
//计算设备状态并更新缓存cache(在线数+1,离线数-1,计算设备在线率)
|
|
|
- int onlineNum = farmAllStatusEntity.getDeviceOnline();
|
|
|
- int offlineNum = farmAllStatusEntity.getDeviceOffline();
|
|
|
- int total = farmAllStatusEntity.getDeviceTotal();
|
|
|
- onlineNum++;
|
|
|
- offlineNum--;
|
|
|
- BigDecimal rateBd= new BigDecimal(onlineNum).divide(new BigDecimal(total),1,BigDecimal.ROUND_HALF_UP);
|
|
|
- farmAllStatusEntity.setDeviceOnline(onlineNum);
|
|
|
- farmAllStatusEntity.setDeviceOffline(offlineNum);
|
|
|
- farmAllStatusEntity.setDeviceRate(Float.parseFloat(rateBd.toString()));
|
|
|
- farmAllStatusEntity.setUpdateTime(new Timestamp(new Date().getTime()));
|
|
|
+ farmAllStatusEntity.setDeviceTotal(deviceTotal);
|
|
|
+ farmAllStatusEntity.setDeviceOnline(deviceOnlineSum);
|
|
|
+ farmAllStatusEntity.setDeviceOffline(deviceOfflineSum);
|
|
|
+ farmAllStatusEntity.setDeviceRate(deviceOnlineRate);
|
|
|
+ farmAllStatusEntity.setDeviceCancel(deviceCancelSum);
|
|
|
+ farmAllStatusEntity.setEartagTotal(eartagTotal);
|
|
|
+ farmAllStatusEntity.setEartagOnline(eartagOnlineSum);
|
|
|
+ farmAllStatusEntity.setEartagRate(eartagOnlineRate);
|
|
|
+ farmAllStatusEntity.setUpdateTime(nowTimestamp);
|
|
|
eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
//# 同步更新牧场总状态缓存相关字段 #
|
|
|
cacheService.flashAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //# 创建总状态记录 #
|
|
|
- private void createNewFarmAllStatus(String farmId,String deviceCode) {
|
|
|
- System.out.println("## (A.1.1)");
|
|
|
+ //计算处于不同生存状态的设备数量
|
|
|
+ private Integer getDeviceSum(String farmId,Integer liveStatus){
|
|
|
//# 计算处于工作状态的设备总数(设备注册表)
|
|
|
- List<Object[]> deviceRegList = deviceRegisterRepo.getDeviceCountByFarmId(farmId);
|
|
|
+ List<Object[]> deviceRegList = deviceRegisterRepo.getDeviceCountByFarmIdAndLiveStatus(farmId,liveStatus);
|
|
|
Object[] deviceRegObj = (Object[]) deviceRegList.get(0);
|
|
|
//设备总数
|
|
|
- int deviceTotal = 0;
|
|
|
- deviceTotal = Integer.parseInt(deviceRegObj[0].toString());
|
|
|
- System.out.println("## total>>"+deviceTotal);
|
|
|
- if(deviceTotal == 0){
|
|
|
+ return Integer.parseInt(deviceRegObj[0].toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ //# 创建总状态记录 #
|
|
|
+ private void createNewFarmAllStatus(String farmId, java.sql.Date todayDate, Timestamp nowTimestamp, Integer deviceTotal, Integer deviceOnlineSum, Integer deviceOfflineSum, Integer deviceCancelSum, Float deviceOnlineRate, Integer eartagTotal, Integer eartagOnlineSum, Float eartagOnlineRate) {
|
|
|
+ Integer liveStatus = 1;
|
|
|
+ if(getDeviceSum(farmId,liveStatus) == 0){
|
|
|
log.error("该牧场没有任何处于工作状态的采集器设备.");
|
|
|
- System.out.println("## (A.1.1.1)");
|
|
|
}else{
|
|
|
- System.out.println("## (A.1.1.2)");
|
|
|
//# 计算耳标总数(耳标注册表)
|
|
|
- int eartagTotal = 0;
|
|
|
- List<Object[]> eartagRegList = eartagRegisterRepo.getEartagCountByFarmId(farmId);
|
|
|
- Object[] eartagRegObj = (Object[]) eartagRegList.get(0);
|
|
|
- //处于工作状态的耳标总数
|
|
|
- eartagTotal = Integer.parseInt(eartagRegObj[0].toString());
|
|
|
-
|
|
|
- int onlineNum = 1;
|
|
|
- int offlineNum = deviceTotal-1;
|
|
|
- BigDecimal reateBd= new BigDecimal(onlineNum).divide(new BigDecimal(deviceTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
- //MySQL:farm_all_status
|
|
|
EartarFarmAllStatusEntity farmAllStatusEntity = new EartarFarmAllStatusEntity();
|
|
|
+ //{获取处于工作状态的设备数量}
|
|
|
+// Integer deviceTotal = getDeviceTotalByFarmIdFromDeviceRegister(farmId);
|
|
|
+ //设备在线数量
|
|
|
+// Integer deviceOnlineSum = cacheService.getDeviceOnlineSum(farmId,todayDateText);
|
|
|
+// deviceOnlineSum++;
|
|
|
+ //设备离线数量
|
|
|
+// Integer deviceOfflineSum = 0;
|
|
|
+// if(deviceTotal > deviceOnlineSum){
|
|
|
+// deviceOfflineSum = deviceTotal - deviceOnlineSum;
|
|
|
+// }
|
|
|
+ //{获取注销设备数量}
|
|
|
+// Integer deviceCancelSum = getDeviceCancelTotalFromDeviceRegister(farmId);
|
|
|
+// //耳标总数量
|
|
|
+// Integer eartagTotal = eartagService.getEartagRegTotalByFarmId(farmId);
|
|
|
+// //耳标在线数量
|
|
|
+// Integer eartagOnlineSum = eartagService.getEartagOnelineTotal(farmId,todayDateText);
|
|
|
+ //设备在线率
|
|
|
+// BigDecimal deviceRateBd= new BigDecimal(deviceOnlineSum).divide(new BigDecimal(deviceTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ //耳标在线率
|
|
|
+// BigDecimal eartagRateBd= new BigDecimal(eartagOnlineSum).divide(new BigDecimal(eartagTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
farmAllStatusEntity.setDeviceTotal(deviceTotal);
|
|
|
- farmAllStatusEntity.setDeviceOnline(onlineNum);
|
|
|
- farmAllStatusEntity.setDeviceOffline(offlineNum);
|
|
|
- farmAllStatusEntity.setDeviceRate(Float.parseFloat(reateBd.toString()));
|
|
|
+ farmAllStatusEntity.setDeviceOnline(deviceOnlineSum);
|
|
|
+ farmAllStatusEntity.setDeviceOffline(deviceOfflineSum);
|
|
|
+ farmAllStatusEntity.setDeviceRate(deviceOnlineRate);
|
|
|
+ farmAllStatusEntity.setDeviceCancel(deviceCancelSum);
|
|
|
farmAllStatusEntity.setEartagTotal(eartagTotal);
|
|
|
- farmAllStatusEntity.setEartagOnline(1);
|
|
|
- farmAllStatusEntity.setUpdateTime(new Timestamp(new Date().getTime()));
|
|
|
- farmAllStatusEntity.setCreateDate(new java.sql.Date(new Date().getTime()));
|
|
|
+ farmAllStatusEntity.setEartagOnline(eartagOnlineSum);
|
|
|
+ farmAllStatusEntity.setEartagRate(eartagOnlineRate);
|
|
|
+ farmAllStatusEntity.setFarmId(farmId);
|
|
|
+ farmAllStatusEntity.setUpdateTime(nowTimestamp);
|
|
|
+ farmAllStatusEntity.setCreateDate(todayDate);
|
|
|
eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
+ //设置总状态缓存
|
|
|
+ cacheService.flashAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- //Redis Cache:farm_all_status_+farmId
|
|
|
+ //# 创建总状态记录 #
|
|
|
+ private void createNewFarmAllStatus(String farmId,String todayDateText, java.sql.Date todayDate, Timestamp nowTimestamp) {
|
|
|
+ Integer liveStatus = 1;
|
|
|
+ if(getDeviceSum(farmId,liveStatus) == 0){
|
|
|
+ log.error("该牧场没有任何处于工作状态的采集器设备.");
|
|
|
+ }else{
|
|
|
+ //# 计算耳标总数(耳标注册表)
|
|
|
+ EartarFarmAllStatusEntity farmAllStatusEntity = new EartarFarmAllStatusEntity();
|
|
|
+ //{获取处于工作状态的设备数量}
|
|
|
+ Integer deviceTotal = getDeviceTotalByFarmIdFromDeviceRegister(farmId);
|
|
|
+ //设备在线数量
|
|
|
+ Integer deviceOnlineSum = cacheService.getDeviceOnlineSum(farmId,todayDateText);
|
|
|
+ deviceOnlineSum++;
|
|
|
+ //设备离线数量
|
|
|
+ Integer deviceOfflineSum = 0;
|
|
|
+ if(deviceTotal > deviceOnlineSum){
|
|
|
+ deviceOfflineSum = deviceTotal - deviceOnlineSum;
|
|
|
+ }
|
|
|
+ BigDecimal deviceRateBd= new BigDecimal(deviceOnlineSum).divide(new BigDecimal(deviceTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ //{获取注销设备数量}
|
|
|
+ Integer deviceCancelSum = getDeviceCancelTotalFromDeviceRegister(farmId);
|
|
|
+ //耳标总数量
|
|
|
+ Integer eartagTotal = eartagService.getEartagRegTotalByFarmId(farmId);
|
|
|
+ //耳标在线数量
|
|
|
+ Integer eartagOnlineSum = eartagService.getEartagOnelineTotal(farmId,todayDateText);
|
|
|
+ //耳标在线率
|
|
|
+ BigDecimal eartagRateBd= new BigDecimal(eartagOnlineSum).divide(new BigDecimal(eartagTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ farmAllStatusEntity.setDeviceTotal(deviceTotal);
|
|
|
+ farmAllStatusEntity.setDeviceOnline(deviceOnlineSum);
|
|
|
+ farmAllStatusEntity.setDeviceOffline(deviceOfflineSum);
|
|
|
+ farmAllStatusEntity.setDeviceRate(Float.parseFloat(deviceRateBd.toString()));
|
|
|
+ farmAllStatusEntity.setDeviceCancel(deviceCancelSum);
|
|
|
+ farmAllStatusEntity.setEartagTotal(eartagTotal);
|
|
|
+ farmAllStatusEntity.setEartagOnline(eartagOnlineSum);
|
|
|
+ farmAllStatusEntity.setEartagRate(Float.parseFloat(eartagRateBd.toString()));
|
|
|
+ farmAllStatusEntity.setFarmId(farmId);
|
|
|
+ farmAllStatusEntity.setUpdateTime(nowTimestamp);
|
|
|
+ farmAllStatusEntity.setCreateDate(todayDate);
|
|
|
+ eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
+ //设置总状态缓存
|
|
|
cacheService.flashAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //更新在线统计
|
|
|
- private void updateDeviceOnlineWithEnvtemp(String farmId, String deviceCode) throws ParseException {
|
|
|
- System.out.println("## (1)/(C)");
|
|
|
- //# 更新数据库
|
|
|
- //# 更新在线缓存
|
|
|
+ /**
|
|
|
+ * @Method : updateDeviceOnlineWithEnvtemp
|
|
|
+ * @Description : 更新在线统计
|
|
|
+ * @Params :
|
|
|
+ * @Return :
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/14
|
|
|
+ * @Time : 14:27
|
|
|
+ */
|
|
|
+ private void updateDeviceOnlineWithEnvtemp(String farmId, String deviceCode,String envtemp,java.sql.Date todayDate,Timestamp nowTimestamp) throws ParseException {
|
|
|
+ ///////////////////////////////////////////////
|
|
|
+ //Step1:更新数据库
|
|
|
+ //Step2:更新在线缓存
|
|
|
+ ///////////////////////////////////////////////
|
|
|
String todayDateText = new com.huimv.eartag2.common.utils.DateUtil().getTodayDateText();
|
|
|
-// System.out.println("1.todayDateText>>" + todayDateText);
|
|
|
+ String hashKey = BizConst.DEVICE_REGISTER_PREFIX+deviceCode;
|
|
|
+ //计算设备别名
|
|
|
+ String alias = (String)cacheService.getHashProperty(hashKey,"deviceAlias");
|
|
|
//读取在线日期
|
|
|
EartagDeviceOnlineEntity eartagDeviceOnlineEntity = eartagDeviceOnlineRepo.getDeviceOnline(deviceCode, todayDateText);
|
|
|
- String deviceOnlineHashKey = BizConst.DEVICE_ONLINE_PREFIX + deviceCode;
|
|
|
if (eartagDeviceOnlineEntity == null) {
|
|
|
- //不存在设备在线统计
|
|
|
- //创建设备在线统计并创建缓存
|
|
|
- createNewDeviceOnline(farmId,deviceCode);
|
|
|
+ Integer heartbeatTotal = 0;
|
|
|
+ Integer envtempTotal = 1;
|
|
|
+ Timestamp heartbeatFirstTime = null;
|
|
|
+ Timestamp heartbeatLastTime = null;
|
|
|
+ Timestamp envtempFirstTime = nowTimestamp;
|
|
|
+ Timestamp envtempLastTime = nowTimestamp;
|
|
|
+ //{计算耳标总数}
|
|
|
+ Integer eartagTotal = eartagService.getEartagRegTotalByFarmId(farmId);
|
|
|
+ //{新建设备在线统计并创建缓存}
|
|
|
+ newDeviceOnline(deviceCode,alias,heartbeatTotal,envtempTotal,eartagTotal,envtemp,todayDate,farmId,heartbeatFirstTime,heartbeatLastTime,envtempFirstTime,envtempLastTime);
|
|
|
}else{
|
|
|
int envtempTotal = eartagDeviceOnlineEntity.getEnvtempTotal();
|
|
|
-// System.out.println(">>>>>>>>>>> envtempTotal>>"+envtempTotal);
|
|
|
+ eartagDeviceOnlineEntity.setAlias(alias);
|
|
|
eartagDeviceOnlineEntity.setEnvtempTotal(++envtempTotal);
|
|
|
- eartagDeviceOnlineEntity.setLastTime(new Timestamp(new Date().getTime()));
|
|
|
-// System.out.println(">>>>>>>>>>> eartagDeviceOnlineEntity>>"+eartagDeviceOnlineEntity);
|
|
|
+ eartagDeviceOnlineEntity.setLastEnvtemp(envtemp);
|
|
|
+ eartagDeviceOnlineEntity.setEnvtempLastTime(nowTimestamp);
|
|
|
eartagDeviceOnlineRepo.saveAndFlush(eartagDeviceOnlineEntity);
|
|
|
//更新设备在线缓存数据
|
|
|
- cacheService.putDeviceInOnline(deviceOnlineHashKey,eartagDeviceOnlineEntity);
|
|
|
+ cacheService.putDeviceInOnline(deviceCode,eartagDeviceOnlineEntity);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //创建
|
|
|
- private void createNewDeviceOnline(String farmId, String deviceCode) {
|
|
|
-// System.out.println("## (1.1.1)");
|
|
|
+ /**
|
|
|
+ * @Method : newDeviceOnline
|
|
|
+ * @Description : 创建设备在线
|
|
|
+ * @Params : [farmId, deviceCode]
|
|
|
+ * @Return : void
|
|
|
+ *newDeviceOnline(deviceCode,alias,heartbeatTotal,envtempTotal,temp,todayDate,farmId,heartbeatFirstTime,heartbeatLastTime,nowTimestamp);
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/14
|
|
|
+ * @Time : 14:27
|
|
|
+ */
|
|
|
+ private void newDeviceOnline(String deviceCode, String alias, Integer heartbeatTotal, Integer envtempTotal, Integer eartagTotal, String temp, java.sql.Date todayDate, String farmId, Timestamp heartbeatFirstTime, Timestamp heartbeatLastTime, Timestamp envtempFirstTime, Timestamp envtempLastTime) {
|
|
|
EartagDeviceOnlineEntity newEartagDeviceOnlineEntity = new EartagDeviceOnlineEntity();
|
|
|
newEartagDeviceOnlineEntity.setDeviceCode(deviceCode);
|
|
|
- newEartagDeviceOnlineEntity.setHeartbeatTotal(0);;
|
|
|
- newEartagDeviceOnlineEntity.setEnvtempTotal(1);
|
|
|
- newEartagDeviceOnlineEntity.setEartagTotal(0);
|
|
|
- newEartagDeviceOnlineEntity.setAddDate(new java.sql.Date(new Date().getTime()));
|
|
|
+ newEartagDeviceOnlineEntity.setAlias(alias);
|
|
|
+ newEartagDeviceOnlineEntity.setHeartbeatTotal(heartbeatTotal);;
|
|
|
+ newEartagDeviceOnlineEntity.setEnvtempTotal(envtempTotal);
|
|
|
+ newEartagDeviceOnlineEntity.setEartagTotal(eartagTotal);
|
|
|
+ newEartagDeviceOnlineEntity.setLastEnvtemp(temp);
|
|
|
+ newEartagDeviceOnlineEntity.setAddDate(todayDate);
|
|
|
newEartagDeviceOnlineEntity.setFarmId(farmId);
|
|
|
- newEartagDeviceOnlineEntity.setEnvtempFirstTime(new Timestamp(new Date().getTime()));
|
|
|
- newEartagDeviceOnlineEntity.setEnvtempLastTime(new Timestamp(new Date().getTime()));
|
|
|
+ newEartagDeviceOnlineEntity.setHeartbeatFirstTime(heartbeatFirstTime);
|
|
|
+ newEartagDeviceOnlineEntity.setHeartbeatLastTime(heartbeatLastTime);
|
|
|
+ newEartagDeviceOnlineEntity.setEnvtempFirstTime(envtempFirstTime);
|
|
|
+ newEartagDeviceOnlineEntity.setEnvtempLastTime(envtempLastTime);
|
|
|
eartagDeviceOnlineRepo.saveAndFlush(newEartagDeviceOnlineEntity);
|
|
|
//将新建的设备在线数据更新到设备在线缓存
|
|
|
- cacheService.putDeviceInOnline(BizConst.DEVICE_ONLINE_PREFIX + deviceCode,newEartagDeviceOnlineEntity);
|
|
|
+ cacheService.putDeviceInOnline(deviceCode,newEartagDeviceOnlineEntity);
|
|
|
}
|
|
|
|
|
|
//更新总状态表
|