|
@@ -1,10 +1,17 @@
|
|
|
package com.huimv.eartag2.service.impl;
|
|
|
|
|
|
+import com.huimv.eartag2.common.dao.entity.EartagDataEntity;
|
|
|
+import com.huimv.eartag2.common.dao.repo.EartagDataRepo;
|
|
|
import com.huimv.eartag2.service.IEartagService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
/**
|
|
|
* @Project : huimv.shiwan
|
|
|
* @Package : com.huimv.biosafety.uface.controller
|
|
@@ -14,9 +21,14 @@ import org.springframework.stereotype.Service;
|
|
|
* @Create : 2020-12-25
|
|
|
**/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class EartagServiceImpl implements IEartagService {
|
|
|
@Autowired
|
|
|
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+ @Autowired
|
|
|
+ private EartagDataRepo eartagDataRepo;
|
|
|
|
|
|
//处理耳标信息
|
|
|
@Override
|
|
@@ -43,4 +55,60 @@ public class EartagServiceImpl implements IEartagService {
|
|
|
private void handleDeviceRegister() {
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : countAct
|
|
|
+ * @Description : 计算act数值
|
|
|
+ * @Params : [deviceCode, nowAct0]
|
|
|
+ * @Return : java.lang.Integer
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/1/18
|
|
|
+ * @Time : 15:25
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Integer countAct(String deviceCode, String nowAct) {
|
|
|
+ //读取hash
|
|
|
+ Object actObj = redisTemplate.opsForHash().get(deviceCode, "act");
|
|
|
+ if (actObj == null) {
|
|
|
+ //--初始化最新的redis记录
|
|
|
+ initRedisObj(deviceCode);
|
|
|
+ Object lastAct = redisTemplate.opsForHash().get(deviceCode, "act");
|
|
|
+ if (lastAct == null) {
|
|
|
+ log.error("#--- redis数据库有问题,请检查redis是否能正常连接 ---# ");
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Object lastAct = redisTemplate.opsForHash().get(deviceCode, "act");
|
|
|
+ Integer act1 = Integer.parseInt(nowAct) - Integer.parseInt(lastAct.toString());
|
|
|
+// redisTemplate.opsForHash().put(deviceCode, "act1", act1);
|
|
|
+ return act1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化redis最新记录
|
|
|
+ */
|
|
|
+ private void initRedisObj(String deviceCode) {
|
|
|
+ EartagDataEntity lastEartagDataEntity = eartagDataRepo.getLastEartagData(deviceCode);
|
|
|
+ if (lastEartagDataEntity != null) {
|
|
|
+ System.out.println("## lastEartagDataEntity.toString>>" + lastEartagDataEntity.toString());
|
|
|
+ //--从数据库读取最新记录并覆盖redis
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("cmdHeader", lastEartagDataEntity.getCmdHeader());
|
|
|
+ map.put("device", lastEartagDataEntity.getDevice());
|
|
|
+ map.put("earmark", lastEartagDataEntity.getEarmark());
|
|
|
+ map.put("bat", lastEartagDataEntity.getBat().toString());
|
|
|
+ map.put("earTemp", lastEartagDataEntity.getEarTemp().toString());
|
|
|
+ map.put("earTemp1", lastEartagDataEntity.getEarTemp1().toString());
|
|
|
+ map.put("envTemp", lastEartagDataEntity.getEnvTemp().toString());
|
|
|
+ map.put("envTemp1", lastEartagDataEntity.getEnvTemp1().toString());
|
|
|
+ map.put("act", lastEartagDataEntity.getAct().toString());
|
|
|
+ map.put("act1", lastEartagDataEntity.getAct1().toString());
|
|
|
+ map.put("signal1", lastEartagDataEntity.getSignal1().toString());
|
|
|
+ map.put("askTime", lastEartagDataEntity.getAskTime());
|
|
|
+ map.put("other", lastEartagDataEntity.getOther());
|
|
|
+ map.put("addTime", lastEartagDataEntity.getAddTime().toString());
|
|
|
+ //为hash结构设置多个键值对(hmset)
|
|
|
+ redisTemplate.opsForHash().putAll(deviceCode, map);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|