浏览代码

采集定时任务改换定时组件,增加定时周期可配置,可动态修改接口

chenwen 1 年之前
父节点
当前提交
27e20e611e

+ 39 - 2
src/main/java/com/hb/proj/api/controller/APIController.java

@@ -7,6 +7,7 @@ import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import com.hb.proj.gather.model.GatherConfig;
 import com.hb.proj.gather.model.LiquidParam;
 import com.hb.proj.gather.process.DataTransConfig;
 import com.hb.proj.gather.protocol.ChannelGroupMgr;
@@ -16,6 +17,7 @@ import com.hb.proj.gather.scheduler.GatherLiquidTask;
 import com.hb.proj.gather.scheduler.GatherTaskExecutor;
 import com.hb.proj.gather.scheduler.ManualLiquidTask;
 import com.hb.proj.gather.utils.ByteUtils;
+import com.hb.proj.utils.ConfigUtils;
 import com.hb.proj.utils.RespVO;
 import com.hb.proj.utils.RespVOBuilder;
 import com.hb.xframework.util.MapUtils;
@@ -33,6 +35,11 @@ public class APIController {
 	@Autowired
 	private RedisRepComponent repRedis;
 
+	/**
+	 * 手动发送指令(只针对50188设备内部测试用)
+	 * @param cmd
+	 * @return
+	 */
 	@RequestMapping("/sendCommond")
 	public RespVO<Object> sendCommond(@NotBlank(message="指令不能为空") String cmd){
 		Channel channel=ChannelGroupMgr.get("50188");
@@ -118,7 +125,7 @@ public class APIController {
 	}
 	
 	/**
-	 * 启动液面测试
+	 * 手动启动液面测试
 	 * @param serial
 	 * @param param
 	 * @return
@@ -152,7 +159,13 @@ public class APIController {
 	
 	
 	
-	
+	/**
+	 * 动液面指令操作
+	 * @param serial
+	 * @param action
+	 * @param param
+	 * @return
+	 */
 	private RespVO<Object> liquidCtr(String serial,int action,LiquidParam param) {
 		Channel channel=ChannelGroupMgr.get(serial);
 		if(channel==null) {
@@ -165,4 +178,28 @@ public class APIController {
 		ChannelGroupMgr.addFuture(serial,GatherTaskExecutor.submit(task));
 		return RespVOBuilder.ok();
 	}
+	
+	
+	/**
+	 * 采集周期设置 单位秒
+	 * @param single
+	 * @param diagram
+	 * @param liquid
+	 * @return
+	 */
+	@RequestMapping("/setGatherConfig")
+	public RespVO<Object> setGatherConfig(Integer single,Integer diagram,Integer liquid){
+		GatherConfig config=ConfigUtils.get();
+		if(single!=null && single > 30) {
+			config.setSingle(single);
+		}
+		if(diagram!=null && diagram > 300) {
+			config.setDiagram(diagram);
+		}
+		if(liquid!=null && liquid > 1200) {
+			config.setLiquid(liquid);
+		}
+		ConfigUtils.save();
+		return RespVOBuilder.ok();
+	}
 }

+ 28 - 0
src/main/java/com/hb/proj/gather/model/GatherConfig.java

@@ -0,0 +1,28 @@
+package com.hb.proj.gather.model;
+
+import lombok.Data;
+
+@Data
+public class GatherConfig {
+
+	/**
+	 * 单值采集间隔,单位s,默认60s
+	 */
+	private Integer single;
+	
+	/**
+	 * 图形数据采集间隔,单位s,默认300s
+	 */
+	private Integer diagram;
+	
+	/**
+	 * 动液面采集间隔,单位s,默认1200s
+	 */
+	private Integer liquid;
+	
+	public GatherConfig() {
+		single=60;
+		diagram=300;
+		liquid=1200;
+	}
+}

+ 85 - 9
src/main/java/com/hb/proj/gather/scheduler/GatherScheduler.java

@@ -1,17 +1,20 @@
 package com.hb.proj.gather.scheduler;
 
 import java.util.Iterator;
+import java.util.Timer;
+import java.util.TimerTask;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.scheduling.annotation.EnableScheduling;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
+import com.hb.proj.gather.model.GatherConfig;
 import com.hb.proj.gather.process.DataTransConfig;
 import com.hb.proj.gather.protocol.ChannelGroupMgr;
+import com.hb.proj.utils.ConfigUtils;
 
 import io.netty.channel.Channel;
+import jakarta.annotation.PreDestroy;
 
 /**
  * 采集定时任务 
@@ -21,24 +24,88 @@ import io.netty.channel.Channel;
  */
 
 @Component
-@EnableScheduling
+//@EnableScheduling
 public class GatherScheduler {
 	
 	private final static  Logger logger = LoggerFactory.getLogger(GatherScheduler.class);
 	
-	private static int scheNum=0; //执行次数
+	//private static int scheNum=0; //执行次数
 	
+	private Timer timer=null;
 	
+	/*
+	 * public static int getScheNum() { return scheNum; }
+	 */
 	
-	public static int getScheNum() {
-		return scheNum;
+	/**
+	 * 采集启动入口
+	 */
+	public void start() {
+		logger.info("采集启动入口");
+		GatherConfig config=ConfigUtils.get();
+		timer=new Timer();
+		timer.schedule(new TimerTask() {
+
+			@Override
+			public void run() {
+				startGather(false);
+			}
+			
+		}, 60000, config.getSingle()*1000);  //单值1分钟后(系统启动等待设备连接)执行,每getSingle()秒重复执行
+		
+		timer.schedule(new TimerTask() {
+
+			@Override
+			public void run() {
+				startGather(true);
+			}
+			
+		}, 120000, config.getDiagram()*1000); //多值2分钟后执行,每config.getDiagram()秒重复执行
+		
+		
+		timer.schedule(new TimerTask() {
+
+			@Override
+			public void run() {
+				startLiquidGather();
+			}
+			
+		}, 180000, config.getLiquid()*1000); //动液面3分钟后执行,每config.getLiquid()秒重复执行
+		
+		
 	}
 	
+	/**
+	 * isMulti:true 多值采集,false:单值采集
+	 * @param isMulti
+	 */
+	public void startGather(boolean isMulti) {
+		logger.info("{}定时采集启动...",isMulti?"多值":"单值");
+		Iterator<Channel> iterator=ChannelGroupMgr.iterator();
+		Channel channel=null;
+		String serial=null;
+		while(iterator.hasNext()) {
+			channel=iterator.next();
+			serial=channel.attr(ChannelGroupMgr.ATTR_KEY_SERIAL).get();
+			if(DataTransConfig.isGatherDev(serial) && ChannelGroupMgr.isDone(serial)) {  //isDone 保证同一设备同一时间只有一个任务在执行
+				ChannelGroupMgr.addFuture(serial,GatherTaskExecutor.submit(new GatherTask(channel,isMulti)));  
+			}
+			else if(!ChannelGroupMgr.isDone(serial)) {
+				logger.warn("设备{}有任务未结束,本次采集忽略",serial);
+			}
+			
+		}
+	}
+	
+	
+	
+	
 
 	/**
 	 * 单值1分钟执行一次采集,首次等待1分钟,等待服务启动,设备连接上
 	 */
-	@Scheduled(fixedRate=60*1000,initialDelay= 60000)  //每分钟执行一次
+	//@Scheduled(fixedRate=60*1000,initialDelay= 60000)  //每分钟执行一次
+	/*
 	public void startGather() {
 		logger.info("定时采集启动...");
 		scheNum+=1;
@@ -54,13 +121,13 @@ public class GatherScheduler {
 		
 		scheNum=scheNum%5==0?0:scheNum;
 	}
-	
+	*/
 	
 	
 	/**
 	 * 动液面定时采集
 	 */
-	@Scheduled(fixedRate = 20 * 60 * 1000,initialDelay= 300000)  
+	//@Scheduled(fixedRate = 20 * 60 * 1000,initialDelay= 300000)  
 	public void startLiquidGather() {
 			logger.info("动液面定时采集启动...");
 			Iterator<Channel> iterator=ChannelGroupMgr.iterator();
@@ -76,4 +143,13 @@ public class GatherScheduler {
 			}
 	}
 	
+	
+	@PreDestroy
+	public void close() {
+		logger.info("即将清空采集定时器里的未执行的任务");
+		if(timer!=null) {
+			timer.cancel();
+		}
+	}
+	
 }

+ 13 - 2
src/main/java/com/hb/proj/gather/scheduler/GatherTask.java

@@ -18,12 +18,15 @@ public class GatherTask implements Runnable{
 
 	private Channel  channel;
 	
+	private Boolean isMulti=false; //是否为多值采集
+	
 	private long cmdTimeout=25*1000; //指令等待回复超时时间 2次超时=50s  不能超过采集周期60s
 	
 	private int timeoutCount=0;  //超时发送指令次数
 	
-	public GatherTask(Channel channel) {
+	public GatherTask(Channel channel,Boolean isMulti) {
 		this.channel=channel;
+		this.isMulti=isMulti;
 	}
 	
 	@Override
@@ -34,6 +37,7 @@ public class GatherTask implements Runnable{
 		
 		ByteBufAllocator alloc=channel.alloc(); 
 		synchronized(channel) {
+			/**
 			logger.info("单值采集开始...");
 			singleGahter(alloc);
 			if((GatherScheduler.getScheNum())%5!=0) {  //5次单值进行一次多值
@@ -41,7 +45,14 @@ public class GatherTask implements Runnable{
 			}
 			logger.info("多值采集开始...");
 			multiGather(alloc,channel);
+			**/
 			
+			if(isMulti) {
+				multiGather(alloc);
+			}
+			else {
+				singleGahter(alloc);
+			}
 		}
 	}
 	
@@ -79,7 +90,7 @@ public class GatherTask implements Runnable{
 	
 	
 	
-	private void multiGather(ByteBufAllocator alloc,Channel  channel) {
+	private void multiGather(ByteBufAllocator alloc) {
 		try {
 			checkDiagramPoint(alloc);
 			

+ 5 - 1
src/main/java/com/hb/proj/gather/server/NettyGatherRunner.java

@@ -10,6 +10,7 @@ import org.springframework.core.annotation.Order;
 import org.springframework.stereotype.Component;
 
 import com.hb.proj.gather.process.DataTransConfig;
+import com.hb.proj.gather.scheduler.GatherScheduler;
 
 @Component
 @Order(1)
@@ -23,13 +24,16 @@ public class NettyGatherRunner implements ApplicationRunner {
 	@Autowired
 	private NettyGatherServer  nettyGatherServer;
 	
+	@Autowired
+	private GatherScheduler gatherScheduler;
+	
 	@Override
 	public void run(ApplicationArguments args) throws Exception {
 		
 		new Thread(()-> {
 			
-			//(new DataAssembler()).start();
 			DataTransConfig.init();
+			gatherScheduler.start();
 			nettyGatherServer.start(gatherPort);
 			
 		}).start();

+ 42 - 0
src/main/java/com/hb/proj/utils/ConfigUtils.java

@@ -0,0 +1,42 @@
+package com.hb.proj.utils;
+
+import java.io.IOException;
+
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+
+import com.hb.proj.gather.model.GatherConfig;
+
+public class ConfigUtils {
+	
+	private static GatherConfig  config=null;
+	private static Resource resource=null;
+	
+	static {
+		resource = new ClassPathResource("config.json");
+		try {
+			config=JacksonUtils.readFile(resource.getFile(),GatherConfig.class);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	public static GatherConfig get() {
+		return config;
+	}
+	
+	public static void save() {
+		try {
+			JacksonUtils.writeFile(resource.getFile(), config);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	public static void main(String[] args) {
+		GatherConfig  config=get();
+		System.out.println(config);
+		config.setSingle(150);
+		save();
+	}
+}

+ 18 - 0
src/main/java/com/hb/proj/utils/JacksonUtils.java

@@ -1,5 +1,6 @@
 package com.hb.proj.utils;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -73,4 +74,21 @@ public class JacksonUtils {
     		throw new RuntimeException("jackson参数转换出错");
     	}
     }
+    
+    public static <T>  T  readFile(File file,Class<T> cls) {
+    	try{
+    		return mapper.readValue(file, cls);
+    	}
+    	catch(Exception e) {
+    		throw new RuntimeException("jackson参数json文件转换出错");
+    	}
+    }
+    
+    public static void writeFile(File file,Object datas) {
+    	try {
+			mapper.writeValue(file, datas);
+		} catch (Exception e) {
+			e.printStackTrace();
+		} 
+    }
 }

+ 6 - 0
src/main/resources/config.json

@@ -0,0 +1,6 @@
+{
+		"single":60,
+		"diagram":600,
+		"liquid":1200
+	 }	
+