|
@@ -1,768 +0,0 @@
|
|
|
-package com.huimv.eartag2.process2.service.impl;
|
|
|
-
|
|
|
-//import cn.hutool.core.date.DateUtil;
|
|
|
-
|
|
|
-import com.huimv.eartag2.common.dao.entity.EartagDeviceOnlineEntity;
|
|
|
-import com.huimv.eartag2.common.dao.entity.EartagDeviceRegisterEntity;
|
|
|
-import com.huimv.eartag2.common.dao.entity.EartarFarmAllStatusEntity;
|
|
|
-import com.huimv.eartag2.common.dao.repo.*;
|
|
|
-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.ICacheService;
|
|
|
-import com.huimv.eartag2.process2.service.IEartagService;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-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;
|
|
|
-
|
|
|
-/**
|
|
|
- * @Project : huimv.shiwan
|
|
|
- * @Package : com.huimv.biosafety.uface.controller
|
|
|
- * @Description : TODO
|
|
|
- * @Version : 1.0
|
|
|
- * @Author : ZhuoNing
|
|
|
- * @Create : 2020-12-25
|
|
|
- **/
|
|
|
-@Service
|
|
|
-@Slf4j
|
|
|
-public class DeviceServiceImpl implements IDeviceService {
|
|
|
- @Autowired
|
|
|
- private RedisTemplate redisTemplate;
|
|
|
- @Autowired
|
|
|
- private EartagDeviceRegisterRepo deviceRegisterRepo;
|
|
|
- @Autowired
|
|
|
- private EartagHeartbeatRepo heartbeatRepo;
|
|
|
- @Autowired
|
|
|
- private EartagEnvRepo envRepo;
|
|
|
- // @Value("#{device.register.prefix}")
|
|
|
-// private String deviceRegisterPrefix;
|
|
|
- @Autowired
|
|
|
- private EartagDeviceOnlineRepo eartagDeviceOnlineRepo;
|
|
|
- @Autowired
|
|
|
- private ICacheService cacheService;
|
|
|
- @Autowired
|
|
|
- private EartarFarmAllStatusRepo eartagFarmAllStatusRepo;
|
|
|
- @Autowired
|
|
|
- private EartagEartagRegisterRepo eartagRegisterRepo;
|
|
|
-// @Autowired
|
|
|
-// private EartagDeviceRegisterRepo deviceRegisterRepo;
|
|
|
- @Autowired
|
|
|
- private EartagDeviceOnlineRepo deviceOnlineRepo;
|
|
|
- @Autowired
|
|
|
- private EartarFarmAllStatusRepo farmAllStatusRepo;
|
|
|
- @Autowired
|
|
|
- private IEartagService eartagService;
|
|
|
-
|
|
|
-
|
|
|
- // 保存在线数据
|
|
|
- @Override
|
|
|
- public void saveDeviceOnline(Map deviceMap) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- //保存状态数据
|
|
|
- @Override
|
|
|
- public void saveDeviceStatus(Map deviceMap) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @Method : updateDeviceRegister
|
|
|
- * @Description : 更新设备注册数据
|
|
|
- * @Params : [deviceCode]
|
|
|
- * @Return : void
|
|
|
- * @Author : ZhuoNing
|
|
|
- * @Date : 2022/2/21
|
|
|
- * @Time : 15:13
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void updateDeviceRegister(String deviceCode) {
|
|
|
- System.out.println("updateDeviceRegister");
|
|
|
- //--更新设备注册表(eartag_device_register.active_status)同时更新对应的Redis
|
|
|
- updateRegisterActiveStatus(deviceCode);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @Method : updateRegisterActiveStatus
|
|
|
- * @Description : 更新注册表中的心跳状态
|
|
|
- * @Params : [deviceCode]
|
|
|
- * @Return : void
|
|
|
- * @Author : ZhuoNing
|
|
|
- * @Date : 2022/2/21
|
|
|
- * @Time : 15:13
|
|
|
- */
|
|
|
- void updateRegisterActiveStatus(String deviceCode) {
|
|
|
- //--更新mysql
|
|
|
- EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
- if (deviceRegisterEntity != null) {
|
|
|
-// if(deviceRegisterEntity.getActiveStatus() == 0){
|
|
|
- deviceRegisterEntity.setActiveStatus(1);
|
|
|
- deviceRegisterEntity.setActiveTime(new Timestamp(new Date().getTime()));
|
|
|
- deviceRegisterRepo.saveAndFlush(deviceRegisterEntity);
|
|
|
-// }
|
|
|
- }
|
|
|
- //--更新redis中注册表数据
|
|
|
- String deviceRegHashKey = BizConst.DEVICE_REGISTER_PREFIX + deviceCode;
|
|
|
- //将Entity转为Map
|
|
|
- Map registerMap = RegisterEntityToMap(deviceRegisterEntity);
|
|
|
- redisTemplate.opsForHash().putAll(deviceRegHashKey, registerMap);
|
|
|
-
|
|
|
-// Object nameObj = redisTemplate.opsForHash().get(deviceRegHashKey,"activeTime");
|
|
|
-// System.out.println("activeTime>>"+nameObj.toString());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @Method : RegisterEntityToMap
|
|
|
- * @Description :
|
|
|
- * @Params : [deviceRegisterEntity]
|
|
|
- * @Return : java.util.Map
|
|
|
- * @Author : ZhuoNing
|
|
|
- * @Date : 2022/2/21
|
|
|
- * @Time : 14:03
|
|
|
- */
|
|
|
- private Map RegisterEntityToMap(EartagDeviceRegisterEntity deviceRegisterEntity) {
|
|
|
- Map map = new HashMap();
|
|
|
- map.put("Id", deviceRegisterEntity.getId());
|
|
|
- map.put("deviceCode", deviceRegisterEntity.getDeviceCode());
|
|
|
- map.put("farmId", deviceRegisterEntity.getFarmId());
|
|
|
- map.put("farmName", deviceRegisterEntity.getFarmName());
|
|
|
- map.put("typeF", deviceRegisterEntity.getTypeF());
|
|
|
- map.put("stage", deviceRegisterEntity.getStage());
|
|
|
- map.put("pigpenId", deviceRegisterEntity.getPigpenId());
|
|
|
- map.put("unitId", deviceRegisterEntity.getUnitId());
|
|
|
- map.put("registerTime", deviceRegisterEntity.getRegisterTime());
|
|
|
- map.put("lastTime", deviceRegisterEntity.getLastTime());
|
|
|
- map.put("activeStatus", deviceRegisterEntity.getActiveStatus());
|
|
|
- map.put("activeTime", deviceRegisterEntity.getActiveTime());
|
|
|
- map.put("deviceStatus", deviceRegisterEntity.getDeviceStatus());
|
|
|
- map.put("deviceAlias", deviceRegisterEntity.getDeviceAlias());
|
|
|
- map.put("location", deviceRegisterEntity.getLocation());
|
|
|
- map.put("remark", deviceRegisterEntity.getRemark());
|
|
|
- return map;
|
|
|
- }
|
|
|
-
|
|
|
- //更新设备在线数据
|
|
|
- @Override
|
|
|
- public void updateDeviceOnline(String deviceCode) throws ParseException {
|
|
|
- //设备在线表 eartag_device_online.heartbeat_total
|
|
|
- //更新mysql
|
|
|
- //更新redis
|
|
|
- //# 更新设备在线数据 #
|
|
|
- updateDeviceOnlineCount(deviceCode);
|
|
|
-
|
|
|
- //# 更新设备注册表(eartag_device_register.active_status)同时更新对应的Redis #
|
|
|
-// updateRegisterActiveStatus(deviceCode);
|
|
|
- }
|
|
|
-
|
|
|
- //更新设备在线Static
|
|
|
- private void updateDeviceOnlineCount(String deviceCode) throws ParseException {
|
|
|
- String todayText = new DateUtil().getTodayDateText();
|
|
|
- //读取在线日期
|
|
|
- EartagDeviceOnlineEntity eartagDeviceOnlineEntity = eartagDeviceOnlineRepo.getDeviceOnline(deviceCode, todayText);
|
|
|
- String deviceOnlineHashKey = BizConst.DEVICE_ONLINE_PREFIX + deviceCode;
|
|
|
- if (eartagDeviceOnlineEntity == null) {
|
|
|
- //# 新建今天在线记录(1),并将记录同步到redis(2) #
|
|
|
- //查询别名
|
|
|
- Object deviceAliasObj = redisTemplate.opsForHash().get(BizConst.DEVICE_REGISTER_PREFIX + deviceCode, "deviceAlias");
|
|
|
- System.out.println("TEST deviceAliasObj>>" + deviceAliasObj.toString());
|
|
|
- String deviceAlias = "";
|
|
|
- if (deviceAliasObj != null) {
|
|
|
- deviceAlias = deviceAliasObj.toString();
|
|
|
- } else {
|
|
|
- //从设备注册表读取设备别名
|
|
|
-
|
|
|
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
- }
|
|
|
- //# 查询牧场id #
|
|
|
-// Object farmIdObj = redisTemplate.opsForHash().get(BizConst.DEVICE_REGISTER_PREFIX + deviceCode,"farmId");
|
|
|
- Object farmIdObj = cacheService.getFarmIdByDeviceCode(deviceCode);
|
|
|
- System.out.println("TEST farmIdObj>>" + farmIdObj.toString());
|
|
|
- //(1)
|
|
|
- EartagDeviceOnlineEntity newEartagDeviceOnlineEntity = new EartagDeviceOnlineEntity();
|
|
|
- newEartagDeviceOnlineEntity.setDeviceCode(deviceCode);
|
|
|
- newEartagDeviceOnlineEntity.setAlias(deviceAlias);
|
|
|
- newEartagDeviceOnlineEntity.setHeartbeatTotal(1);
|
|
|
- newEartagDeviceOnlineEntity.setEnvtempTotal(0);
|
|
|
- newEartagDeviceOnlineEntity.setEartagTotal(0);
|
|
|
- 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(farmIdObj.toString());
|
|
|
- eartagDeviceOnlineRepo.saveAndFlush(newEartagDeviceOnlineEntity);
|
|
|
- //(2)
|
|
|
- //将onlineEntity转为Map
|
|
|
- Map onlineMap = fromOnlineEntityToMap(newEartagDeviceOnlineEntity);
|
|
|
- redisTemplate.opsForHash().putAll(deviceOnlineHashKey, onlineMap);
|
|
|
- } else {
|
|
|
- //# 修改今天数据库在线记录(1),并将记录同步到缓存中(2) #
|
|
|
- //(1)
|
|
|
- int total = eartagDeviceOnlineEntity.getHeartbeatTotal();
|
|
|
- eartagDeviceOnlineEntity.setHeartbeatTotal(total + 1);
|
|
|
- eartagDeviceOnlineEntity.setLastTime(new Timestamp(new Date().getTime()));
|
|
|
- eartagDeviceOnlineRepo.saveAndFlush(eartagDeviceOnlineEntity);
|
|
|
- //(2)
|
|
|
- //将onlineEntity转为Map
|
|
|
- Map onlineMap = fromOnlineEntityToMap(eartagDeviceOnlineEntity);
|
|
|
- redisTemplate.opsForHash().putAll(deviceOnlineHashKey, onlineMap);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //--将onlineEntity转为Map
|
|
|
- private Map fromOnlineEntityToMap(EartagDeviceOnlineEntity newEartagDeviceOnlineEntity) {
|
|
|
- Map map = new HashMap();
|
|
|
- map.put("id", newEartagDeviceOnlineEntity.getId());
|
|
|
- map.put("deviceCode", newEartagDeviceOnlineEntity.getDeviceCode());
|
|
|
- map.put("alias", newEartagDeviceOnlineEntity.getAlias());
|
|
|
- map.put("heartbeatTotal", newEartagDeviceOnlineEntity.getHeartbeatTotal());
|
|
|
- map.put("envtempTotal", newEartagDeviceOnlineEntity.getEnvtempTotal());
|
|
|
- map.put("eartagTotal", newEartagDeviceOnlineEntity.getEartagTotal());
|
|
|
- map.put("firstTime", newEartagDeviceOnlineEntity.getFirstTime());
|
|
|
- map.put("lastTime", newEartagDeviceOnlineEntity.getLastTime());
|
|
|
- map.put("lastEnvtemp", newEartagDeviceOnlineEntity.getLastEnvtemp());
|
|
|
- map.put("lastEartag", newEartagDeviceOnlineEntity.getLastEartag());
|
|
|
- map.put("addDate", newEartagDeviceOnlineEntity.getAddDate());
|
|
|
- map.put("farmId", newEartagDeviceOnlineEntity.getFarmId());
|
|
|
- return map;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @Method : updateDeviceStatus
|
|
|
- * @Description : 更新设备状态数据
|
|
|
- * @Params : [deviceCode]
|
|
|
- * @Return : void
|
|
|
- * @Author : ZhuoNing
|
|
|
- * @Date : 2022/2/21
|
|
|
- * @Time : 16:04
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void updateFarmDeviceStatus(String deviceCode) throws ParseException {
|
|
|
- System.out.println("updateDeviceStatus 1.deviceCode>>" + deviceCode);
|
|
|
- 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.");
|
|
|
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @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,Timestamp nowTimestamp) {
|
|
|
- System.out.println("## 心跳 1.1/2.1");
|
|
|
- //--更新mysql
|
|
|
- EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
- if (deviceRegisterEntity != null) {
|
|
|
- System.out.println("## 心跳 1.1.1/2.1.1");
|
|
|
- //设备注册数据存在
|
|
|
- deviceRegisterEntity.setActiveStatus(1);
|
|
|
- deviceRegisterEntity.setActiveTime(new Timestamp(new Date().getTime()));
|
|
|
- deviceRegisterRepo.saveAndFlush(deviceRegisterEntity);
|
|
|
- //更新缓存中设备注册信息
|
|
|
- cacheService.putDeviceRegister(deviceCode,deviceRegisterEntity);
|
|
|
- }else{
|
|
|
- System.out.println("## 心跳 1.1.2/12.1.2");
|
|
|
- //设备注册数据不存在
|
|
|
- //新建设备注册记录
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void updateDeviceOnlineCount(String farmId, String deviceCode, String todayDateText, Timestamp nowTimestamp, java.sql.Date todayDate) throws ParseException {
|
|
|
- //# 先判断今天在线记录是否存在 #
|
|
|
- //# 更新数据表 #
|
|
|
- //# 更新缓存 #
|
|
|
- System.out.println("## 心跳 1.2/2.2");
|
|
|
- //
|
|
|
- EartagDeviceOnlineEntity deviceOnlineEntity = deviceOnlineRepo.getDeviceOnline(deviceCode,todayDateText);
|
|
|
- if(deviceOnlineEntity == null){
|
|
|
- System.out.println("## 心跳 1.2.1/2.2.1");
|
|
|
- //不存在;
|
|
|
- EartagDeviceOnlineEntity newDeviceOnlineEntity = new EartagDeviceOnlineEntity();
|
|
|
- newDeviceOnlineEntity.setDeviceCode(deviceCode);
|
|
|
- newDeviceOnlineEntity.setHeartbeatTotal(1);
|
|
|
- newDeviceOnlineEntity.setEnvtempTotal(0);
|
|
|
- newDeviceOnlineEntity.setEartagTotal(0);
|
|
|
- newDeviceOnlineEntity.setAddDate(todayDate);
|
|
|
- newDeviceOnlineEntity.setHeartbeatFirstTime(nowTimestamp);
|
|
|
- newDeviceOnlineEntity.setHeartbeatLastTime(nowTimestamp);
|
|
|
- newDeviceOnlineEntity.setFarmId(farmId);
|
|
|
- deviceOnlineRepo.saveAndFlush(newDeviceOnlineEntity);
|
|
|
- //更新每天在线统计缓存
|
|
|
- cacheService.putDeviceInOnline(deviceCode,newDeviceOnlineEntity);
|
|
|
- }else{
|
|
|
- System.out.println("## 心跳 1.2.2/2.2.2");
|
|
|
- //存在;
|
|
|
- int total = deviceOnlineEntity.getHeartbeatTotal();
|
|
|
- deviceOnlineEntity.setHeartbeatTotal(++total);
|
|
|
- deviceOnlineEntity.setHeartbeatLastTime(nowTimestamp);
|
|
|
- deviceOnlineRepo.saveAndFlush(deviceOnlineEntity);
|
|
|
- //更新每天在线统计缓存
|
|
|
- cacheService.putDeviceInOnline(deviceCode,deviceOnlineEntity);
|
|
|
- }
|
|
|
-
|
|
|
- //--(优化思路) --//
|
|
|
-// updateDeviceOnlineCount(deviceCode);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @Method : updateDeviceAllStatus
|
|
|
- * @Description : 更新总状态
|
|
|
- * @Params : [farmId, deviceCode]
|
|
|
- * @Return : void
|
|
|
- *
|
|
|
- * @Author : ZhuoNing
|
|
|
- * @Date : 2022/3/2
|
|
|
- * @Time : 20:51
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void updateDeviceAllStatus(String farmId, String deviceCode, String todayDateText, Timestamp nowTimestamp, java.sql.Date todayDate) throws ParseException {
|
|
|
- System.out.println("## 心跳 2.3");
|
|
|
- //读取一条总状态记录;
|
|
|
- EartarFarmAllStatusEntity farmAllStatusEntity = farmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
|
|
|
- if(farmAllStatusEntity == null){
|
|
|
- System.out.println("## 心跳 2.3.1");
|
|
|
- //总状态记录不存在;
|
|
|
- //#创建一条新总状态记录;
|
|
|
- //*计算设备在线数量、离线数量、在线率*
|
|
|
- int deviceTotal = getDeviceTotalByFarmIdFromDeviceRegister(farmId);
|
|
|
- int onlineNum = 1;
|
|
|
- int offlineNum = deviceTotal-1;
|
|
|
- BigDecimal reateBd= new BigDecimal(onlineNum).divide(new BigDecimal(deviceTotal),1,BigDecimal.ROUND_HALF_UP);
|
|
|
- //*计算设备注销数量*
|
|
|
- int cancelNum = getDeviceCancelTotalFromDeviceRegister(farmId);
|
|
|
- //#计算耳标总数
|
|
|
- int eartagTotal = eartagService.getEartagRegTotalByFarmId(farmId);
|
|
|
- //计算耳标在线数量
|
|
|
- int eartagOnlineTotal = eartagService.getEartagOnelineTotal(farmId,todayDateText);
|
|
|
- //
|
|
|
- EartarFarmAllStatusEntity newFarmAllStatusEntity = new EartarFarmAllStatusEntity();
|
|
|
- newFarmAllStatusEntity.setDeviceTotal(deviceTotal);
|
|
|
- newFarmAllStatusEntity.setDeviceOnline(onlineNum);
|
|
|
- newFarmAllStatusEntity.setDeviceOffline(offlineNum);
|
|
|
- newFarmAllStatusEntity.setDeviceRate(Float.parseFloat(reateBd.toString()));
|
|
|
- newFarmAllStatusEntity.setDeviceCancel(cancelNum);
|
|
|
- newFarmAllStatusEntity.setEartagTotal(eartagTotal);
|
|
|
- newFarmAllStatusEntity.setEartagOnline(eartagOnlineTotal);
|
|
|
- newFarmAllStatusEntity.setFarmId(farmId);
|
|
|
- newFarmAllStatusEntity.setUpdateTime(nowTimestamp);
|
|
|
- newFarmAllStatusEntity.setCreateDate(todayDate);
|
|
|
- farmAllStatusRepo.saveAndFlush(newFarmAllStatusEntity);
|
|
|
- //更新总状态表缓存
|
|
|
- cacheService.putAllFarmAllStatusCache(farmId,newFarmAllStatusEntity);
|
|
|
- }else{
|
|
|
- System.out.println("## 心跳 2.3.2");
|
|
|
- //总状态记录存在;
|
|
|
- //#计算设备的在线、离线、在线率,更新时间
|
|
|
- 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(nowTimestamp);
|
|
|
- System.out.println("## farmAllStatusEntity>>"+farmAllStatusEntity);
|
|
|
- eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
- //更新总状态表缓存
|
|
|
- cacheService.putAllFarmAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //计算注销设备数量
|
|
|
- private int getDeviceCancelTotalFromDeviceRegister(String farmId) {
|
|
|
- List<Object[]> deviceRegList = deviceRegisterRepo.getDeviceCountByFarmId(farmId);
|
|
|
- Object[] deviceRegObj = (Object[]) deviceRegList.get(0);
|
|
|
- //设备总数
|
|
|
- return Integer.parseInt(deviceRegObj[0].toString());
|
|
|
- }
|
|
|
-
|
|
|
- //计算设备总数
|
|
|
- public int getDeviceTotalByFarmIdFromDeviceRegister(String farmId){
|
|
|
- List<Object[]> deviceRegList = deviceRegisterRepo.getDeviceCountByFarmId(farmId);
|
|
|
- Object[] deviceRegObj = (Object[]) deviceRegList.get(0);
|
|
|
- //设备总数
|
|
|
- return Integer.parseInt(deviceRegObj[0].toString());
|
|
|
- }
|
|
|
-
|
|
|
-// @Override
|
|
|
- public void updateDeviceAllStatus_old1(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 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()));
|
|
|
- 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 = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
- String farmId = "";
|
|
|
- if(deviceRegisterEntity == null){
|
|
|
- log.error("这个设备(" + deviceCode + ")的注册数据不存在,请检查具体原因.");
|
|
|
- }else{
|
|
|
- farmId = deviceRegisterEntity.getFarmId();
|
|
|
- }
|
|
|
- String todayDateText = new DateUtil().getTodayDateText();
|
|
|
- EartarFarmAllStatusEntity farmAllStatusEntity = eartagFarmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
|
|
|
- if(farmAllStatusEntity == null){
|
|
|
- //设备注册表信息不存在
|
|
|
- //创建设备状态记录并更新缓存cache()
|
|
|
- createNewFarmAllStatus(farmId,deviceCode);
|
|
|
- }else{
|
|
|
- //设备注册表信息存在()
|
|
|
- //计算设备状态并更新缓存cache()
|
|
|
- countDeviceStatus(farmId,deviceCode,todayDateText);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //# 计算在线状态,并同步到缓存 #
|
|
|
- private void countDeviceStatus(String farmId,String deviceCode,String todayDateText) throws ParseException {
|
|
|
- //读取牧场总状态数据(数据库记录确定存在)
|
|
|
- EartarFarmAllStatusEntity farmAllStatusEntity = eartagFarmAllStatusRepo.getOneByFarmIdAndCreateDate(farmId,todayDateText);
|
|
|
- //在线数+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()));
|
|
|
- System.out.println("## farmAllStatusEntity>>"+farmAllStatusEntity);
|
|
|
- eartagFarmAllStatusRepo.saveAndFlush(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 = cacheService.getFarmIdByDeviceCode(deviceCode);
|
|
|
- System.out.println("22.farmIdObj>>" + farmIdObj);
|
|
|
- String farmId = "";
|
|
|
- if (farmIdObj != null) {
|
|
|
- farmId = farmIdObj.toString();
|
|
|
- } else {
|
|
|
- log.info("设备注册缓存里没有设备编号为" + deviceCode + "的设备注册信息");
|
|
|
- //(2)
|
|
|
- EartagDeviceRegisterEntity eartagDeviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
- System.out.println("22.eartagDeviceRegisterEntity>>" + eartagDeviceRegisterEntity.toString());
|
|
|
- if (eartagDeviceRegisterEntity == null) {
|
|
|
- log.error("设备注册记录不存在设备编号为" + deviceCode + "的设备注册信息.");
|
|
|
- return null;
|
|
|
- } else {
|
|
|
- farmId = eartagDeviceRegisterEntity.getFarmId();
|
|
|
- }
|
|
|
- }
|
|
|
- System.out.println("22.farmId>>" + farmId);
|
|
|
- //从牧场总状态缓存中获取最近一次日期
|
|
|
- Object createDateObj = cacheService.getCreateDateByFarmId(farmId);
|
|
|
- System.out.println("22.createDateObj>>" + createDateObj);
|
|
|
- if (createDateObj == null) {
|
|
|
- return "0";
|
|
|
- } else {
|
|
|
- return createDateObj;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-// public void updateDeviceStatus_old1(String deviceCode) throws ParseException {
|
|
|
-// System.out.println("updateDeviceStatus 1.deviceCode>>" + deviceCode);
|
|
|
-// //# 验证是否存在当天总状态记录
|
|
|
-// //(1)
|
|
|
-// if (cacheService.isExistTodayFarmAllStatus(deviceCode)) {
|
|
|
-// //缓存中存在该牧场当当天的总状态记录
|
|
|
-// System.out.println("缓存中存在该记录:Yes");
|
|
|
-// //更新总状态记录
|
|
|
-//// autoUpdateFarmAllStatus(deviceCode);
|
|
|
-// } else {
|
|
|
-// System.out.println("缓存中存在该记录:No");
|
|
|
-// //# 创建总状态记录 #(缓存中没有今天的记录)
|
|
|
-// createFarmAllStatus(deviceCode);
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
- //# 创建总状态记录 #
|
|
|
- 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 = cacheService.getFarmIdByDeviceCode(deviceCode);
|
|
|
- System.out.println("redis.farmIdObj>>" + farmIdObj);
|
|
|
- if (farmIdObj == null) {
|
|
|
- //# 初始化MySQL:farm_all_status
|
|
|
- initFarmAllStatus();
|
|
|
- }
|
|
|
-
|
|
|
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- //# 初始化总状态表
|
|
|
- private void initFarmAllStatus() {
|
|
|
- //# 计算设备总数(设备注册表)
|
|
|
- int deviceTotal = 0;
|
|
|
-// deviceRegisterRepo.countTotal();
|
|
|
-
|
|
|
- //# 计算耳标总数(耳标注册表)
|
|
|
- int eartagTotal = 0;
|
|
|
-
|
|
|
- EartarFarmAllStatusEntity farmAllStatusEntity = new EartarFarmAllStatusEntity();
|
|
|
- farmAllStatusEntity.setDeviceTotal(deviceTotal);
|
|
|
- farmAllStatusEntity.setEartagTotal(eartagTotal);
|
|
|
- eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
-
|
|
|
- //同步更新redis缓存
|
|
|
-
|
|
|
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
- }
|
|
|
-}
|