|
@@ -10,7 +10,7 @@ import com.huimv.eartag2.common.utils.BizConst;
|
|
|
import com.huimv.eartag2.common.utils.DateUtil;
|
|
|
import com.huimv.eartag2.process2.service.IDeviceService;
|
|
|
//import com.huimv.eartag2.process2.utils.DateUtil;
|
|
|
-import com.huimv.eartag2.process2.service.IRedisService;
|
|
|
+import com.huimv.eartag2.process2.service.ICacheService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
@@ -21,6 +21,7 @@ import java.sql.Timestamp;
|
|
|
import java.text.ParseException;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
@@ -47,13 +48,13 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
@Autowired
|
|
|
private EartagDeviceOnlineRepo eartagDeviceOnlineRepo;
|
|
|
@Autowired
|
|
|
- private IRedisService redisService;
|
|
|
+ private ICacheService cacheService;
|
|
|
@Autowired
|
|
|
private EartarFarmAllStatusRepo eartagFarmAllStatusRepo;
|
|
|
@Autowired
|
|
|
private EartagEartagRegisterRepo eartagRegisterRepo;
|
|
|
- @Autowired
|
|
|
- private EartagDeviceRegisterRepo eartagDeviceRegisterRepo;
|
|
|
+// @Autowired
|
|
|
+// private EartagDeviceRegisterRepo deviceRegisterRepo;
|
|
|
|
|
|
|
|
|
// 保存在线数据
|
|
@@ -172,11 +173,12 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
deviceAlias = deviceAliasObj.toString();
|
|
|
} else {
|
|
|
//从设备注册表读取设备别名
|
|
|
+
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
}
|
|
|
//# 查询牧场id #
|
|
|
// Object farmIdObj = redisTemplate.opsForHash().get(BizConst.DEVICE_REGISTER_PREFIX + deviceCode,"farmId");
|
|
|
- Object farmIdObj = redisService.getFarmIdByDeviceCode(deviceCode);
|
|
|
+ Object farmIdObj = cacheService.getFarmIdByDeviceCode(deviceCode);
|
|
|
System.out.println("TEST farmIdObj>>" + farmIdObj.toString());
|
|
|
//(1)
|
|
|
EartagDeviceOnlineEntity newEartagDeviceOnlineEntity = new EartagDeviceOnlineEntity();
|
|
@@ -188,17 +190,18 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
newEartagDeviceOnlineEntity.setFirstTime(new Timestamp(new Date().getTime()));
|
|
|
newEartagDeviceOnlineEntity.setLastTime(new Timestamp(new Date().getTime()));
|
|
|
newEartagDeviceOnlineEntity.setAddDate(new java.sql.Date(new Date().getTime()));
|
|
|
- newEartagDeviceOnlineEntity.setFarmId(Integer.parseInt(farmIdObj.toString()));
|
|
|
+ newEartagDeviceOnlineEntity.setFarmId(farmIdObj.toString());
|
|
|
eartagDeviceOnlineRepo.saveAndFlush(newEartagDeviceOnlineEntity);
|
|
|
//(2)
|
|
|
//将onlineEntity转为Map
|
|
|
Map onlineMap = fromOnlineEntityToMap(newEartagDeviceOnlineEntity);
|
|
|
redisTemplate.opsForHash().putAll(deviceOnlineHashKey, onlineMap);
|
|
|
} else {
|
|
|
- //# 修改今天在线记录(1),并将记录同步到redis(2) #
|
|
|
+ //# 修改今天数据库在线记录(1),并将记录同步到缓存中(2) #
|
|
|
//(1)
|
|
|
int total = eartagDeviceOnlineEntity.getHeartbeatTotal();
|
|
|
eartagDeviceOnlineEntity.setHeartbeatTotal(total + 1);
|
|
|
+ eartagDeviceOnlineEntity.setLastTime(new Timestamp(new Date().getTime()));
|
|
|
eartagDeviceOnlineRepo.saveAndFlush(eartagDeviceOnlineEntity);
|
|
|
//(2)
|
|
|
//将onlineEntity转为Map
|
|
@@ -242,19 +245,20 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
|
|
|
//(1)判断牧场总状态缓存是否存在
|
|
|
Object createDateObj = checkIsExistFarmAllStatusCache(deviceCode);
|
|
|
+ System.out.println("## createDateObj>>"+createDateObj);
|
|
|
if (createDateObj == null) {
|
|
|
log.error("这个设备(" + deviceCode + ")的注册数据不存在,请检查具体原因.");
|
|
|
System.out.println("1.");
|
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
} else if (createDateObj.toString().equalsIgnoreCase("0")) {
|
|
|
System.out.println("牧场总状态缓存记录不存在.");
|
|
|
- // B流程
|
|
|
+ // B流程(新建缓存)
|
|
|
handleDeviceStatus(deviceCode);
|
|
|
System.out.println("2.");
|
|
|
}else if (createDateObj.toString().trim().equalsIgnoreCase(todayDateText)) {
|
|
|
//计算在线状态,并同步到缓存
|
|
|
- Object farmIdObj = redisService.getFarmIdByDeviceCode(deviceCode);
|
|
|
- countDeviceStatus(farmIdObj.toString(),deviceCode,todayDateText);
|
|
|
+ Object farmIdObj = cacheService.getFarmIdByDeviceCode(deviceCode);
|
|
|
+ countAndFlashDeviceStatus(farmIdObj.toString(),deviceCode,todayDateText);
|
|
|
System.out.println("3.");
|
|
|
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
} else {
|
|
@@ -266,11 +270,175 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String getFarmIdByDeviceCode(String deviceCode) {
|
|
|
+ EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
+ return deviceRegisterEntity.getFarmId();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : updateDeviceRegister
|
|
|
+ * @Description :
|
|
|
+ * @Params : [farmId, deviceCode]
|
|
|
+ * @Return : void
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/1
|
|
|
+ * @Time : 10:56
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void updateDeviceRegister(String farmId, String deviceCode) {
|
|
|
+ System.out.println("## (2)/(B)");
|
|
|
+ //--更新mysql
|
|
|
+ EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
+ if (deviceRegisterEntity != null) {
|
|
|
+ deviceRegisterEntity.setActiveStatus(1);
|
|
|
+ deviceRegisterEntity.setActiveTime(new Timestamp(new Date().getTime()));
|
|
|
+ deviceRegisterRepo.saveAndFlush(deviceRegisterEntity);
|
|
|
+ }
|
|
|
+ //更新缓存中设备注册信息
|
|
|
+ cacheService.putDeviceRegister(deviceCode,deviceRegisterEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateDeviceOnline(String farmId, String deviceCode) throws ParseException {
|
|
|
+ //# 先判断今天在线记录是否存在 #
|
|
|
+ //# 更新数据表 #
|
|
|
+ //# 更新缓存 #
|
|
|
+ //
|
|
|
+ updateDeviceOnlineCount(deviceCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateDeviceAllStatus(String farmId, String deviceCode) throws ParseException {
|
|
|
+ String todayDateText = new DateUtil().getTodayDateText();
|
|
|
+ System.out.println("1.todayDateText>>" + todayDateText);
|
|
|
+
|
|
|
+ //(1)判断牧场总状态缓存是否存在
|
|
|
+ Object createDateObj = checkIsExistFarmAllStatusCache(deviceCode);
|
|
|
+ System.out.println("## createDateObj>>"+createDateObj);
|
|
|
+ if (createDateObj == null) {
|
|
|
+ log.error("这个设备(" + deviceCode + ")的注册数据不存在,请检查具体原因.");
|
|
|
+ System.out.println("1.");
|
|
|
+ } else if (createDateObj.toString().equalsIgnoreCase("0")) {
|
|
|
+ System.out.println("牧场总状态缓存记录不存在.");
|
|
|
+ // B流程(新建缓存)
|
|
|
+ handleDeviceStatus(deviceCode);
|
|
|
+ System.out.println("2.");
|
|
|
+ }else if (createDateObj.toString().trim().equalsIgnoreCase(todayDateText)) {
|
|
|
+ //计算在线状态,并同步到缓存
|
|
|
+ Object farmIdObj = cacheService.getFarmIdByDeviceCode(deviceCode);
|
|
|
+ countAndFlashDeviceStatus(farmIdObj.toString(),deviceCode,todayDateText);
|
|
|
+ System.out.println("3.");
|
|
|
+ } else {
|
|
|
+ //缓存日期非当日日期
|
|
|
+ // B流程
|
|
|
+ handleDeviceStatus(deviceCode);
|
|
|
+ System.out.println("4.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : updateDeviceOnlineWithEnvtemp
|
|
|
+ * @Description : 更新设备在线当中的环境温度参数
|
|
|
+ * @Params : [farmId, deviceCode]
|
|
|
+ * @Return : void
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/1
|
|
|
+ * @Time : 15:11
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void updateDeviceOnlineWithEnvtemp(String farmId, String deviceCode) throws ParseException {
|
|
|
+ System.out.println("## (1)/(C)");
|
|
|
+ //# 更新数据库
|
|
|
+ //# 更新在线缓存
|
|
|
+ String todayDateText = new DateUtil().getTodayDateText();
|
|
|
+ System.out.println("1.todayDateText>>" + todayDateText);
|
|
|
+ //读取在线日期
|
|
|
+ EartagDeviceOnlineEntity eartagDeviceOnlineEntity = eartagDeviceOnlineRepo.getDeviceOnline(deviceCode, todayDateText);
|
|
|
+ String deviceOnlineHashKey = BizConst.DEVICE_ONLINE_PREFIX + deviceCode;
|
|
|
+ if (eartagDeviceOnlineEntity == null) {
|
|
|
+ System.out.println("## (1.1)");
|
|
|
+ //不存在设备在线统计
|
|
|
+ //创建设备在线统计并创建缓存
|
|
|
+ createNewDeviceOnline(farmId,deviceCode);
|
|
|
+ }else{
|
|
|
+ System.out.println("## (1.2)");
|
|
|
+ int envtempTotal = eartagDeviceOnlineEntity.getEnvtempTotal();
|
|
|
+ System.out.println(">>>>>>>>>>> envtempTotal>>"+envtempTotal);
|
|
|
+ eartagDeviceOnlineEntity.setEnvtempTotal(++envtempTotal);
|
|
|
+ eartagDeviceOnlineEntity.setLastTime(new Timestamp(new Date().getTime()));
|
|
|
+ System.out.println(">>>>>>>>>>> eartagDeviceOnlineEntity>>"+eartagDeviceOnlineEntity);
|
|
|
+ eartagDeviceOnlineRepo.saveAndFlush(eartagDeviceOnlineEntity);
|
|
|
+ //更新设备在线缓存数据
|
|
|
+ cacheService.putDeviceInOnline(deviceOnlineHashKey,eartagDeviceOnlineEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateEnvtempOfDeviceAllStatus(String farmId, String deviceCode) throws ParseException {
|
|
|
+ System.out.println("## (A)");
|
|
|
+ String todayDateText = new DateUtil().getTodayDateText();
|
|
|
+ //
|
|
|
+ EartarFarmAllStatusEntity farmAllStatusEntity = eartagFarmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
|
|
|
+ if(farmAllStatusEntity == null){
|
|
|
+ System.out.println("## (A.1)");
|
|
|
+ //设备总状态表信息不存在
|
|
|
+ //创建设备状态记录并更新缓存cache()
|
|
|
+ createNewFarmAllStatus(farmId,deviceCode);
|
|
|
+ //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
+ }else{
|
|
|
+ System.out.println("## (A.2)");
|
|
|
+ //设备总状态表信息存在
|
|
|
+ //计算设备状态并更新缓存cache(在线数+1,离线数-1,计算设备在线率)
|
|
|
+ int onlineNum = farmAllStatusEntity.getDeviceOnline();
|
|
|
+ int offlineNum = farmAllStatusEntity.getDeviceOffline();
|
|
|
+ int total = farmAllStatusEntity.getDeviceTotal();
|
|
|
+ onlineNum++;
|
|
|
+ offlineNum--;
|
|
|
+ BigDecimal reateBd= new BigDecimal(onlineNum).divide(new BigDecimal(total),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ farmAllStatusEntity.setDeviceOnline(onlineNum);
|
|
|
+ farmAllStatusEntity.setDeviceOffline(offlineNum);
|
|
|
+ farmAllStatusEntity.setDeviceRate(Float.parseFloat(reateBd.toString()));
|
|
|
+ farmAllStatusEntity.setUpdateTime(new Timestamp(new Date().getTime()));
|
|
|
+ eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
+ //# 同步更新牧场总状态缓存相关字段 #
|
|
|
+ cacheService.flashAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : createNewDeviceOnline
|
|
|
+ * @Description :
|
|
|
+ * @Params : [farmId, deviceCode]
|
|
|
+ * @Return : void
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/1
|
|
|
+ * @Time : 16:33
|
|
|
+ */
|
|
|
+ private void createNewDeviceOnline(String farmId, String deviceCode) {
|
|
|
+ System.out.println("## (1.1.1)");
|
|
|
+ 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.setFarmId(farmId);
|
|
|
+ newEartagDeviceOnlineEntity.setEnvtempFirstTime(new Timestamp(new Date().getTime()));
|
|
|
+ newEartagDeviceOnlineEntity.setEnvtempLastTime(new Timestamp(new Date().getTime()));
|
|
|
+ eartagDeviceOnlineRepo.saveAndFlush(newEartagDeviceOnlineEntity);
|
|
|
+ //将新建的设备在线数据更新到设备在线缓存
|
|
|
+ cacheService.putDeviceInOnline(BizConst.DEVICE_ONLINE_PREFIX + deviceCode,newEartagDeviceOnlineEntity);
|
|
|
+ }
|
|
|
+
|
|
|
//# 处理设备状态 #
|
|
|
private void handleDeviceStatus(String deviceCode) throws ParseException {
|
|
|
//(1)判断今天的牧场总状态记录是否存在
|
|
|
//从设备注册表读取牧场id
|
|
|
- EartagDeviceRegisterEntity deviceRegisterEntity = eartagDeviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
+ EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
String farmId = "";
|
|
|
if(deviceRegisterEntity == null){
|
|
|
log.error("这个设备(" + deviceCode + ")的注册数据不存在,请检查具体原因.");
|
|
@@ -282,10 +450,9 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
if(farmAllStatusEntity == null){
|
|
|
//设备注册表信息不存在
|
|
|
//创建设备状态记录并更新缓存cache()
|
|
|
- createFarmAllStatus(deviceCode);
|
|
|
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
+ createNewFarmAllStatus(farmId,deviceCode);
|
|
|
}else{
|
|
|
- //设备注册表信息存在
|
|
|
+ //设备注册表信息存在()
|
|
|
//计算设备状态并更新缓存cache()
|
|
|
countDeviceStatus(farmId,deviceCode,todayDateText);
|
|
|
}
|
|
@@ -293,7 +460,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
|
|
|
//# 计算在线状态,并同步到缓存 #
|
|
|
private void countDeviceStatus(String farmId,String deviceCode,String todayDateText) throws ParseException {
|
|
|
- //读取牧场总状态数据
|
|
|
+ //读取牧场总状态数据(数据库记录确定存在)
|
|
|
EartarFarmAllStatusEntity farmAllStatusEntity = eartagFarmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
|
|
|
//在线数+1,离线数-1,计算设备在线率
|
|
|
int onlineNum = farmAllStatusEntity.getDeviceOnline();
|
|
@@ -305,17 +472,44 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
farmAllStatusEntity.setDeviceOnline(onlineNum);
|
|
|
farmAllStatusEntity.setDeviceOffline(offlineNum);
|
|
|
farmAllStatusEntity.setDeviceRate(Float.parseFloat(reateBd.toString()));
|
|
|
+ farmAllStatusEntity.setUpdateTime(new Timestamp(new Date().getTime()));
|
|
|
System.out.println("## farmAllStatusEntity>>"+farmAllStatusEntity);
|
|
|
eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
- //# 设置牧场总状态缓存相关字段 #
|
|
|
- redisService.putFarmAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
+ //# 同步更新牧场总状态缓存相关字段 #
|
|
|
+// cacheService.putAllFarmAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
+ cacheService.flashAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
}
|
|
|
|
|
|
+ //# 计算在线状态,并同步到缓存 #
|
|
|
+ private void countAndFlashDeviceStatus(String farmId,String deviceCode,String todayDateText) throws ParseException {
|
|
|
+ //读取牧场总状态数据(数据库记录确定存在)
|
|
|
+ EartarFarmAllStatusEntity farmAllStatusEntity = eartagFarmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
|
|
|
+ if(farmAllStatusEntity == null){
|
|
|
+ System.out.println("## farmAllStatusEntity == null");
|
|
|
+ }else{
|
|
|
+ System.out.println("## farmAllStatusEntity != null");
|
|
|
+ }
|
|
|
+ System.out.println("## farmAllStatusEntity>>"+farmAllStatusEntity.toString());
|
|
|
+ //在线数+1,离线数-1,计算设备在线率
|
|
|
+ int onlineNum = farmAllStatusEntity.getDeviceOnline();
|
|
|
+ int offlineNum = farmAllStatusEntity.getDeviceOffline();
|
|
|
+ int total = farmAllStatusEntity.getDeviceTotal();
|
|
|
+ onlineNum++;
|
|
|
+ offlineNum--;
|
|
|
+ BigDecimal reateBd= new BigDecimal(onlineNum).divide(new BigDecimal(total),1,BigDecimal.ROUND_HALF_UP);
|
|
|
+ farmAllStatusEntity.setDeviceOnline(onlineNum);
|
|
|
+ farmAllStatusEntity.setDeviceOffline(offlineNum);
|
|
|
+ farmAllStatusEntity.setDeviceRate(Float.parseFloat(reateBd.toString()));
|
|
|
+ System.out.println("## farmAllStatusEntity>>"+farmAllStatusEntity.toString());
|
|
|
+ eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
+ //# 同步更新牧场总状态缓存相关字段 #
|
|
|
+ cacheService.putFarmAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
+ }
|
|
|
|
|
|
//断牧场总状态缓存是否存在
|
|
|
private Object checkIsExistFarmAllStatusCache(String deviceCode) {
|
|
|
//从缓存中获取farmId
|
|
|
- Object farmIdObj = redisService.getFarmIdByDeviceCode(deviceCode);
|
|
|
+ Object farmIdObj = cacheService.getFarmIdByDeviceCode(deviceCode);
|
|
|
System.out.println("22.farmIdObj>>" + farmIdObj);
|
|
|
String farmId = "";
|
|
|
if (farmIdObj != null) {
|
|
@@ -323,7 +517,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
} else {
|
|
|
log.info("设备注册缓存里没有设备编号为" + deviceCode + "的设备注册信息");
|
|
|
//(2)
|
|
|
- EartagDeviceRegisterEntity eartagDeviceRegisterEntity = eartagDeviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
+ EartagDeviceRegisterEntity eartagDeviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
System.out.println("22.eartagDeviceRegisterEntity>>" + eartagDeviceRegisterEntity.toString());
|
|
|
if (eartagDeviceRegisterEntity == null) {
|
|
|
log.error("设备注册记录不存在设备编号为" + deviceCode + "的设备注册信息.");
|
|
@@ -334,7 +528,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
}
|
|
|
System.out.println("22.farmId>>" + farmId);
|
|
|
//从牧场总状态缓存中获取最近一次日期
|
|
|
- Object createDateObj = redisService.getCreateDateByFarmId(farmId);
|
|
|
+ Object createDateObj = cacheService.getCreateDateByFarmId(farmId);
|
|
|
System.out.println("22.createDateObj>>" + createDateObj);
|
|
|
if (createDateObj == null) {
|
|
|
return "0";
|
|
@@ -347,7 +541,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
// System.out.println("updateDeviceStatus 1.deviceCode>>" + deviceCode);
|
|
|
// //# 验证是否存在当天总状态记录
|
|
|
// //(1)
|
|
|
-// if (redisService.isExistTodayFarmAllStatus(deviceCode)) {
|
|
|
+// if (cacheService.isExistTodayFarmAllStatus(deviceCode)) {
|
|
|
// //缓存中存在该牧场当当天的总状态记录
|
|
|
// System.out.println("缓存中存在该记录:Yes");
|
|
|
// //更新总状态记录
|
|
@@ -360,23 +554,52 @@ public class DeviceServiceImpl implements IDeviceService {
|
|
|
// }
|
|
|
|
|
|
//# 创建总状态记录 #
|
|
|
- private void createFarmAllStatus(String deviceCode) {
|
|
|
- //#从farm_all_status读取最近一天的数据
|
|
|
- Map lastFarmAllStatusMap = getLastFarmAllStatus(deviceCode);
|
|
|
-
|
|
|
- //MySQL:farm_all_status
|
|
|
-
|
|
|
-
|
|
|
- //Redis:farm_all_status_farmId
|
|
|
-
|
|
|
-
|
|
|
+ private void createNewFarmAllStatus(String farmId,String deviceCode) {
|
|
|
+ System.out.println("## (A.1.1)");
|
|
|
+ //# 计算处于工作状态的设备总数(设备注册表)
|
|
|
+ List<Object[]> deviceRegList = deviceRegisterRepo.getDeviceCountByFarmId(farmId);
|
|
|
+ Object[] deviceRegObj = (Object[]) deviceRegList.get(0);
|
|
|
+ //设备总数
|
|
|
+ int deviceTotal = 0;
|
|
|
+ deviceTotal = Integer.parseInt(deviceRegObj[0].toString());
|
|
|
+ System.out.println("## total>>"+deviceTotal);
|
|
|
+ if(deviceTotal == 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();
|
|
|
+ farmAllStatusEntity.setDeviceTotal(deviceTotal);
|
|
|
+ farmAllStatusEntity.setDeviceOnline(onlineNum);
|
|
|
+ farmAllStatusEntity.setDeviceOffline(offlineNum);
|
|
|
+ farmAllStatusEntity.setDeviceRate(Float.parseFloat(reateBd.toString()));
|
|
|
+ farmAllStatusEntity.setEartagTotal(eartagTotal);
|
|
|
+ farmAllStatusEntity.setEartagOnline(1);
|
|
|
+ farmAllStatusEntity.setUpdateTime(new Timestamp(new Date().getTime()));
|
|
|
+ farmAllStatusEntity.setCreateDate(new java.sql.Date(new Date().getTime()));
|
|
|
+ eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
+
|
|
|
+ //Redis Cache:farm_all_status_+farmId
|
|
|
+ cacheService.flashAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//#
|
|
|
private Map getLastFarmAllStatus(String deviceCode) {
|
|
|
// farmAllStatusRepo.getLastFarmAllStatus();
|
|
|
//获取farmId,然后用farmId获取最近一天的数据
|
|
|
- Object farmIdObj = redisService.getFarmIdByDeviceCode(deviceCode);
|
|
|
+ Object farmIdObj = cacheService.getFarmIdByDeviceCode(deviceCode);
|
|
|
System.out.println("redis.farmIdObj>>" + farmIdObj);
|
|
|
if (farmIdObj == null) {
|
|
|
//# 初始化MySQL:farm_all_status
|