|
@@ -0,0 +1,571 @@
|
|
|
|
+package com.huimv.eartag2.device.service.impl;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.huimv.eartag2.common.dao.entity.*;
|
|
|
|
+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.utils.BizConst;
|
|
|
|
+import com.huimv.eartag2.device.service.ICacheService;
|
|
|
|
+import com.huimv.eartag2.device.service.IDeviceService;
|
|
|
|
+import com.huimv.eartag2.device.service.IEartagService;
|
|
|
|
+import com.huimv.eartag2.device.utils.DateUtil;
|
|
|
|
+import com.huimv.eartag2.device.utils.MathUtil;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+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 MathUtil mathUtil;
|
|
|
|
+ @Autowired
|
|
|
|
+ private DateUtil dateUtil;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EartagRawRepo rawRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EartagHeartbeatRepo heartbeatRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EartagEnvRepo envRepo;
|
|
|
|
+// @Autowired
|
|
|
|
+// private IDeviceService deviceService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ICacheService cacheService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EartagDeviceRegisterRepo deviceRegisterRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EartagDeviceOnlineRepo deviceOnlineRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EartarFarmAllStatusRepo eartagFarmAllStatusRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EartarFarmAllStatusRepo farmAllStatusRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private IEartagService eartagService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EartagDeviceOnlineRepo eartagDeviceOnlineRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EartagEartagRegisterRepo eartagRegisterRepo;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @Method : handleDeviceHeartbeat
|
|
|
|
+ * @Description : 处理设备心跳数据
|
|
|
|
+ * @Params : [dataJo]
|
|
|
|
+ * @Return : void
|
|
|
|
+ *
|
|
|
|
+ * @Author : ZhuoNing
|
|
|
|
+ * @Date : 2022/3/12
|
|
|
|
+ * @Time : 19:16
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void handleDeviceHeartbeat(JSONObject dataJo) throws ParseException {
|
|
|
|
+ System.out.println("心跳数据 dataJo>>"+dataJo);
|
|
|
|
+ //逻辑处理:
|
|
|
|
+ //1、保存设备心跳流水数据;
|
|
|
|
+ //2、更新设备注册表(活动状态、活动更新时间,更新类型)
|
|
|
|
+ //3、更新在线统计表(心跳数,心跳最早上传时间,最后一次上传时间)
|
|
|
|
+ //4、更新总状态表表
|
|
|
|
+
|
|
|
|
+ //1、{保存设备心跳记录}
|
|
|
|
+ saveDeviceHeartbeat(dataJo);
|
|
|
|
+ //2、{更新设备注册表}
|
|
|
|
+// updateDeviceRegister(dataJo);
|
|
|
|
+ //3、{更新在线统计表}
|
|
|
|
+// updateDeviceOnline(dataJo);
|
|
|
|
+ //4、{更新总状态表}
|
|
|
|
+// updateAllStatus(dataJo);
|
|
|
|
+ //--------------------------------------------------------------------------------------------
|
|
|
|
+ System.out.println("dataJo>"+dataJo);
|
|
|
|
+ String deviceCode = dataJo.getString("device");
|
|
|
|
+ //{获取牧场id}
|
|
|
|
+ String farmId = getFarmIdByDeviceCode(deviceCode);
|
|
|
|
+ System.out.println("## farmId>>"+farmId);
|
|
|
|
+ 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());
|
|
|
|
+
|
|
|
|
+ //Step1:更新设备注册表[设置活动状态为1,设置活动状态更新时间]
|
|
|
|
+ //Step2:更新在线统计[心跳数+1,心跳第一次上传时间,心跳最后一次时间],预先判断该数据是否存在,如果不存在要先添加记录,如果存在就修改数据
|
|
|
|
+
|
|
|
|
+ //# 更新设备注册表 #
|
|
|
|
+ updateDeviceRegister(farmId, deviceCode,nowTimestamp);
|
|
|
|
+ //# 更新在线统计 #
|
|
|
|
+ updateDeviceOnlineCount(farmId, deviceCode,todayDateText,nowTimestamp,todayDate);
|
|
|
|
+
|
|
|
|
+ //判断该设备编号是否存在缓存在线集合当中;
|
|
|
|
+ Boolean isExist = cacheService.isExistDeviceOnlineSet(farmId, deviceCode, todayDateText);
|
|
|
|
+ System.out.println("# 处理心跳 isExist >>"+isExist);
|
|
|
|
+ if (!isExist) {
|
|
|
|
+ System.out.println("## 心跳 2");
|
|
|
|
+ //# 更新总状态表 #
|
|
|
|
+ updateDeviceAllStatus(farmId, deviceCode,todayDateText,nowTimestamp,todayDate);
|
|
|
|
+
|
|
|
|
+ //# 将新设备编号加入在线缓存 #
|
|
|
|
+ cacheService.putDeviceToOnline(farmId, deviceCode,todayDateText);
|
|
|
|
+ }
|
|
|
|
+ //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //更新总状态
|
|
|
|
+ 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);
|
|
|
|
+ newFarmAllStatusEntity.setFarmId(farmId);
|
|
|
|
+ farmAllStatusRepo.saveAndFlush(newFarmAllStatusEntity);
|
|
|
|
+ //更新总状态表缓存
|
|
|
|
+ cacheService.putAllFarmAllStatusCache(farmId,newFarmAllStatusEntity);
|
|
|
|
+ }else{
|
|
|
|
+ System.out.println("## 心跳 2.3.2");
|
|
|
|
+ //总状态记录存在;
|
|
|
|
+ //#计算设备的在线、离线、在线率,更新时间
|
|
|
|
+ int onlineNum = farmAllStatusEntity.getDeviceOnline();
|
|
|
|
+ onlineNum++;
|
|
|
|
+ //
|
|
|
|
+ int total = getDeviceTotalByFarmIdFromDeviceRegister(farmId);
|
|
|
|
+ int offlineNum = 0;
|
|
|
|
+ if(total > onlineNum){
|
|
|
|
+ offlineNum = total - onlineNum;
|
|
|
|
+ }
|
|
|
|
+ farmAllStatusEntity.setDeviceTotal(total);
|
|
|
|
+ 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);
|
|
|
|
+ eartagFarmAllStatusRepo.saveAndFlush(farmAllStatusEntity);
|
|
|
|
+ //更新总状态表缓存
|
|
|
|
+ cacheService.putAllFarmAllStatusCache(farmId,farmAllStatusEntity);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //计算设备总数
|
|
|
|
+ public int getDeviceTotalByFarmIdFromDeviceRegister(String farmId){
|
|
|
|
+ //从缓存钟读取设备总数
|
|
|
|
+ Integer deviceTotal = cacheService.getDeviceTotal(farmId);
|
|
|
|
+ if(deviceTotal != null) {
|
|
|
|
+ return deviceTotal;
|
|
|
|
+ }else{
|
|
|
|
+ List<Object[]> deviceRegList = deviceRegisterRepo.getDeviceCountByFarmId(farmId);
|
|
|
|
+ Object[] deviceRegObj = (Object[]) deviceRegList.get(0);
|
|
|
|
+ Integer total = Integer.parseInt(deviceRegObj[0].toString());
|
|
|
|
+ //设置缓存
|
|
|
|
+ cacheService.putDeviceCancel(total,farmId);
|
|
|
|
+ //设备总数
|
|
|
|
+ return total;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //计算注销设备数量
|
|
|
|
+ private int getDeviceCancelTotalFromDeviceRegister(String farmId) {
|
|
|
|
+ //从缓存钟读取设备总数
|
|
|
|
+ Integer deviceCancel = cacheService.getDeviceCancel(farmId);
|
|
|
|
+ if(deviceCancel != null) {
|
|
|
|
+ return deviceCancel;
|
|
|
|
+ }else {
|
|
|
|
+ List<Object[]> deviceRegList = deviceRegisterRepo.getDeviceCountByFarmId(farmId);
|
|
|
|
+ Object[] deviceRegObj = (Object[]) deviceRegList.get(0);
|
|
|
|
+ Integer total = Integer.parseInt(deviceRegObj[0].toString());
|
|
|
|
+ //设置缓存
|
|
|
|
+ cacheService.putDeviceCancel(total,farmId);
|
|
|
|
+ //设备总数
|
|
|
|
+ return total;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //更新在线状态
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //更新设备注册表
|
|
|
|
+ private void updateDeviceRegister(String farmId, String deviceCode, Timestamp nowTimestamp) {
|
|
|
|
+ //--更新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);
|
|
|
|
+ }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);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @Method : handleDeviceEnvtemp
|
|
|
|
+ * @Description : 处理设备环境温度数据
|
|
|
|
+ * @Params : [dataJo]
|
|
|
|
+ * @Return : void
|
|
|
|
+ *
|
|
|
|
+ * @Author : ZhuoNing
|
|
|
|
+ * @Date : 2022/3/12
|
|
|
|
+ * @Time : 19:16
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void handleDeviceEnvtemp(JSONObject dataJo) throws ParseException {
|
|
|
|
+ System.out.println("环境数据 dataJo>>"+dataJo);
|
|
|
|
+ //逻辑处理:
|
|
|
|
+ //1、保存环境温度流水数据;
|
|
|
|
+ //2、更新设备注册表(活动状态、活动更新时间,更新类型)
|
|
|
|
+ //3、更新在线统计表(环境温度计数,环境温度最早上传时间,最新环境温度,环境温度最后一次上传时间)
|
|
|
|
+ //4、更新总状态表
|
|
|
|
+ //保存设备心跳记录
|
|
|
|
+// saveDeviceHeartbeat(dataJo);
|
|
|
|
+ //{更新设备注册表}
|
|
|
|
+// updateDeviceRegister(dataJo);
|
|
|
|
+ //{更新在线统计表}
|
|
|
|
+// updateDeviceOnline(dataJo);
|
|
|
|
+ //{更新总状态表}
|
|
|
|
+// updateAllStatus(dataJo);
|
|
|
|
+ //------------------------------------------------------------------------------------
|
|
|
|
+ String deviceCode = dataJo.getString("device");
|
|
|
|
+ String farmId = getFarmIdByDeviceCode(deviceCode);
|
|
|
|
+
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ //判断该设备编号是否存在牧场缓存在线集合当中;
|
|
|
|
+ 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");
|
|
|
|
+ }
|
|
|
|
+ //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void updateEnvtempOfDeviceAllStatus(String farmId, String deviceCode) throws ParseException {
|
|
|
|
+// System.out.println("## (A)");
|
|
|
|
+ String todayDateText = new com.huimv.eartag2.common.utils.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);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //# 创建总状态记录 #
|
|
|
|
+ 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 void updateDeviceOnlineWithEnvtemp(String farmId, String deviceCode) throws ParseException {
|
|
|
|
+ System.out.println("## (1)/(C)");
|
|
|
|
+ //# 更新数据库
|
|
|
|
+ //# 更新在线缓存
|
|
|
|
+ String todayDateText = new com.huimv.eartag2.common.utils.DateUtil().getTodayDateText();
|
|
|
|
+// System.out.println("1.todayDateText>>" + todayDateText);
|
|
|
|
+ //读取在线日期
|
|
|
|
+ EartagDeviceOnlineEntity eartagDeviceOnlineEntity = eartagDeviceOnlineRepo.getDeviceOnline(deviceCode, todayDateText);
|
|
|
|
+ String deviceOnlineHashKey = BizConst.DEVICE_ONLINE_PREFIX + deviceCode;
|
|
|
|
+ if (eartagDeviceOnlineEntity == null) {
|
|
|
|
+ //不存在设备在线统计
|
|
|
|
+ //创建设备在线统计并创建缓存
|
|
|
|
+ createNewDeviceOnline(farmId,deviceCode);
|
|
|
|
+ }else{
|
|
|
|
+ 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);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //创建
|
|
|
|
+ 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 updateAllStatus(JSONObject dataJo) {
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ //更新在线统计表
|
|
|
|
+// private void updateDeviceOnline(JSONObject dataJo) {
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getFarmIdByDeviceCode(String deviceCode) {
|
|
|
|
+ EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
|
|
|
|
+ return deviceRegisterEntity.getFarmId();
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * @Method : saveDeviceHeartbeat
|
|
|
|
+ * @Description : 保存设备心跳数据
|
|
|
|
+ * @Params : [dataJo]
|
|
|
|
+ * @Return : void
|
|
|
|
+ *
|
|
|
|
+ * @Author : ZhuoNing
|
|
|
|
+ * @Date : 2022/2/15
|
|
|
|
+ * @Time : 14:39
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void saveDeviceHeartbeat(JSONObject dataJo) {
|
|
|
|
+ String deviceCode = dataJo.getString("device");
|
|
|
|
+ EartagHeartbeatEntity addHeartbeatEntity = new EartagHeartbeatEntity();
|
|
|
|
+ addHeartbeatEntity.setDeviceCode(deviceCode);
|
|
|
|
+ addHeartbeatEntity.setAddTime(new Timestamp(new Date().getTime()));
|
|
|
|
+ heartbeatRepo.saveAndFlush(addHeartbeatEntity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @Method : saveDeviceTemp
|
|
|
|
+ * @Description : 保存设备温度数据
|
|
|
|
+ * @Params : [dataJo]
|
|
|
|
+ * @Return : void
|
|
|
|
+ *
|
|
|
|
+ * @Author : ZhuoNing
|
|
|
|
+ * @Date : 2022/2/15
|
|
|
|
+ * @Time : 14:39
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void saveDeviceTemp(JSONObject dataJo) {
|
|
|
|
+ String device = dataJo.getString("device");
|
|
|
|
+ String temp = dataJo.getString("temp");
|
|
|
|
+ EartagEnvEntity envEntity = new EartagEnvEntity();
|
|
|
|
+ envEntity.setDeviceCode(device);
|
|
|
|
+ envEntity.setTemp1(Integer.parseInt(temp));
|
|
|
|
+ envEntity.setTemp2(0F);
|
|
|
|
+ envEntity.setAddTime(new Timestamp(new Date().getTime()));
|
|
|
|
+ envRepo.saveAndFlush(envEntity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @Method : saveRawdata
|
|
|
|
+ * @Description : 保存原始数据
|
|
|
|
+ * @Params : [askText]
|
|
|
|
+ * @Return : void
|
|
|
|
+ *
|
|
|
|
+ * @Author : ZhuoNing
|
|
|
|
+ * @Date : 2022/2/9
|
|
|
|
+ * @Time : 13:38
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public void saveRawdata(String askText) throws ParseException {
|
|
|
|
+// log.info("#开始解析Ask请求数据>>" + askText.trim().replace("\n", ""));
|
|
|
|
+ String[] dataArray = askText.split("\\+");
|
|
|
|
+ String cmdHeader = dataArray[0];
|
|
|
|
+ Map rawMap = new HashMap();
|
|
|
|
+ if (cmdHeader.trim().equalsIgnoreCase("hm")) {
|
|
|
|
+ //采集器原始数据
|
|
|
|
+ String device = dataArray[1];
|
|
|
|
+ String cmd = dataArray[2];
|
|
|
|
+ rawMap.put("cmdHeader", cmdHeader);
|
|
|
|
+ rawMap.put("device", device);
|
|
|
|
+ rawMap.put("cmd", cmd);
|
|
|
|
+ rawMap.put("ask", askText);
|
|
|
|
+ } else if (cmdHeader.trim().equalsIgnoreCase("zj")) {
|
|
|
|
+ //采集器原始数据
|
|
|
|
+ String device = dataArray[1];
|
|
|
|
+ rawMap.put("cmdHeader", cmdHeader);
|
|
|
|
+ rawMap.put("device", device);
|
|
|
|
+ rawMap.put("cmd", "");
|
|
|
|
+ rawMap.put("ask", askText);
|
|
|
|
+ rawMap.put("addTime", dateUtil.getTodayMissionText());
|
|
|
|
+ } else {
|
|
|
|
+ //采集器原始数据
|
|
|
|
+ String device = dataArray[1];
|
|
|
|
+ rawMap.put("cmdHeader", cmdHeader);
|
|
|
|
+ rawMap.put("device", device);
|
|
|
|
+ rawMap.put("cmd", "");
|
|
|
|
+ rawMap.put("ask", askText);
|
|
|
|
+ }
|
|
|
|
+ //保存原始数据
|
|
|
|
+ //类型、设备、命令、数据内容、日期
|
|
|
|
+ EartagRawEntity addRawEntity = new EartagRawEntity();
|
|
|
|
+ addRawEntity.setCmdHeader(rawMap.get("cmdHeader").toString());
|
|
|
|
+ addRawEntity.setDevice(rawMap.get("device").toString());
|
|
|
|
+ addRawEntity.setCmd(rawMap.get("cmd").toString());
|
|
|
|
+ addRawEntity.setAsk(rawMap.get("ask").toString());
|
|
|
|
+ addRawEntity.setAddTime(new Timestamp(new Date().getTime()));
|
|
|
|
+ rawRepo.saveAndFlush(addRawEntity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|