Browse Source

单值实时数据 同步到redis

chenwen 1 year ago
parent
commit
c01d277246

+ 16 - 1
src/main/java/com/hb/proj/gather/business/DataTransRepSingleTask.java

@@ -1,6 +1,7 @@
 package com.hb.proj.gather.business;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -11,6 +12,7 @@ import com.hb.proj.gather.model.SingleCombPO;
 import com.hb.proj.gather.model.SingleInsertPO;
 import com.hb.proj.gather.model.WellParamVO;
 import com.hb.proj.gather.rep.GatherDataRepService;
+import com.hb.proj.gather.rep.RedisRepComponent;
 import com.hb.xframework.util.ApplicationContextUtils;
 
 /**
@@ -42,7 +44,7 @@ public class DataTransRepSingleTask implements Runnable{
 				logger.info("未找到参数配置{}_{}",singleCombPO.getDevSerial(),pcode);
 				continue;
 			}
-			insPOItm=new SingleInsertPO(paramConfig.getParamId(),gatherDatas.get(pcode),singleCombPO.getGatherTime());
+			insPOItm=new SingleInsertPO(pcode,paramConfig.getParamId(),gatherDatas.get(pcode),singleCombPO.getGatherTime());
 			DataTransUtils.transSingle(insPOItm, paramConfig);
 			insPOs.add(insPOItm);
 		
@@ -50,9 +52,22 @@ public class DataTransRepSingleTask implements Runnable{
 		
 		logger.info("单值数据转换完:{}",insPOs.size());
 		
+		RedisRepComponent repRedis=ApplicationContextUtils.getBean("redisRepComponent", RedisRepComponent.class);
+		
+		repRedis.put(paramConfig.getWellId(), buildRedisDatas(insPOs));
+		
 		GatherDataRepService repService=ApplicationContextUtils.getBean("gatherDataRepService", GatherDataRepService.class);
 		
 		repService.save(insPOs);  //入库
 	}
+	
+	
+	private Map<String,String> buildRedisDatas(List<SingleInsertPO> insPOs) {
+		Map<String,String> rtn=new HashMap<>(insPOs.size());
+		for(SingleInsertPO po : insPOs) {
+			rtn.put(po.getParamCode(), String.valueOf(po.getDataVal()));
+		}
+		return rtn;
+	}
 
 }

+ 12 - 1
src/main/java/com/hb/proj/gather/model/SingleInsertPO.java

@@ -8,6 +8,8 @@ import java.util.Date;
  *
  */
 public class SingleInsertPO {
+	
+	private String paramCode;
 
 	private String wellParam;
 	
@@ -19,7 +21,8 @@ public class SingleInsertPO {
 		
 	}
 	
-	public SingleInsertPO(String wellParam,Float  dataVal,Date gatherTime) {
+	public SingleInsertPO(String paramCode,String wellParam,Float  dataVal,Date gatherTime) {
+		this.paramCode=paramCode;
 		this.wellParam=wellParam;
 		this.dataVal=dataVal;
 		this.gatherTime=gatherTime;
@@ -49,4 +52,12 @@ public class SingleInsertPO {
 	public void setGatherTime(Date gatherTime) {
 		this.gatherTime = gatherTime;
 	}
+
+	public String getParamCode() {
+		return paramCode;
+	}
+
+	public void setParamCode(String paramCode) {
+		this.paramCode = paramCode;
+	}
 }

+ 29 - 0
src/main/java/com/hb/proj/gather/rep/RedisRepComponent.java

@@ -0,0 +1,29 @@
+package com.hb.proj.gather.rep;
+
+import java.util.Map;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.HashOperations;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+/**
+ * 采集数据在redis中的持久化  
+ * @author cwen
+ *
+ */
+@Component
+public class RedisRepComponent {
+
+	@Autowired
+	private RedisTemplate<String,String> redisTemplate;
+	
+	/**
+	 * 实时数据入redis
+	 * @param wellId
+	 * @param datas
+	 */
+	public void put(String wellId,Map<String,String> datas) {
+		HashOperations<String, String, String>  ops=redisTemplate.opsForHash();
+		ops.putAll(wellId, datas);
+	}
+}