| 
									
										
										
										
											2024-09-20 19:24:32 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 文件名:TimerClass.nut | 
					
						
							|  |  |  | 路径:Dps_A/BaseClass/TimerClass/TimerClass.nut | 
					
						
							|  |  |  | 创建日期:2024-09-19	12:39 | 
					
						
							|  |  |  | 文件用途:定时器类 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | class Timer { | 
					
						
							|  |  |  |     //执行任务队列树 | 
					
						
							|  |  |  |     Wait_Exec_Tree = null; | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |     //定时执行任务队列树 | 
					
						
							|  |  |  |     Date_Exec_Tree = null; | 
					
						
							| 
									
										
										
										
											2024-09-20 19:24:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     constructor() { | 
					
						
							|  |  |  |         Wait_Exec_Tree = RedBlackTree(); | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |         Date_Exec_Tree = RedBlackTree(); | 
					
						
							| 
									
										
										
										
											2024-09-20 19:24:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Cb_timer_dispatch_Func.rawset("__System__Timer__Event", Update.bindenv(this)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-27 15:32:44 +08:00
										 |  |  |     function ClockTime() { | 
					
						
							| 
									
										
										
										
											2025-04-05 22:03:40 +08:00
										 |  |  |         return Sq_GetTimestampString().slice(-9).tointeger(); | 
					
						
							| 
									
										
										
										
											2024-10-27 15:32:44 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |     //检测延时任务 | 
					
						
							|  |  |  |     function CheckTimeOut() { | 
					
						
							| 
									
										
										
										
											2024-10-27 15:32:44 +08:00
										 |  |  |         local Node = Wait_Exec_Tree.GetPop(); | 
					
						
							| 
									
										
										
										
											2024-09-20 19:24:32 +08:00
										 |  |  |         if (!Node) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         //取得函数体 | 
					
						
							|  |  |  |         local Info = Node.Info; | 
					
						
							|  |  |  |         //执行时间 | 
					
						
							|  |  |  |         local exec_time = Node.time; | 
					
						
							| 
									
										
										
										
											2024-10-27 15:32:44 +08:00
										 |  |  |         //如果没到执行时间,就不管 | 
					
						
							|  |  |  |         if (ClockTime() <= exec_time) { | 
					
						
							| 
									
										
										
										
											2024-09-20 19:24:32 +08:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-10-27 15:32:44 +08:00
										 |  |  |         //该执行了从树中删除这个元素 | 
					
						
							|  |  |  |         Wait_Exec_Tree.pop(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-20 19:24:32 +08:00
										 |  |  |         //函数 | 
					
						
							|  |  |  |         local func = Info[0]; | 
					
						
							|  |  |  |         //参数 | 
					
						
							|  |  |  |         local func_args = Info[1]; | 
					
						
							|  |  |  |         //执行函数 | 
					
						
							|  |  |  |         func.acall(func_args); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function SetTimeOut(target_func, delay_time, ...) { | 
					
						
							|  |  |  |         local target_arg_list = []; | 
					
						
							|  |  |  |         target_arg_list.push(getroottable()); | 
					
						
							|  |  |  |         for (local i = 0; i< vargv.len(); i++) { | 
					
						
							|  |  |  |             target_arg_list.push(vargv[i]); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |         //当前时间戳,单位:秒 | 
					
						
							| 
									
										
										
										
											2024-10-27 15:32:44 +08:00
										 |  |  |         local time_sec = ClockTime(); | 
					
						
							| 
									
										
										
										
											2024-09-20 19:24:32 +08:00
										 |  |  |         //计算下一次执行的时间 | 
					
						
							| 
									
										
										
										
											2024-10-27 15:32:44 +08:00
										 |  |  |         local exec_time_sec = time_sec + delay_time; | 
					
						
							| 
									
										
										
										
											2024-09-20 19:24:32 +08:00
										 |  |  |         //设置下一次执行 | 
					
						
							|  |  |  |         local func_info = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         func_info.push(target_func); | 
					
						
							|  |  |  |         func_info.push(target_arg_list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         _Timer_Object.Wait_Exec_Tree.insert(exec_time_sec, func_info); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     //检测定时任务 | 
					
						
							|  |  |  |     function CheckCronTask() { | 
					
						
							| 
									
										
										
										
											2024-10-27 15:32:44 +08:00
										 |  |  |         local Node = Date_Exec_Tree.GetPop(); | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |         if (!Node) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         //取得函数体 | 
					
						
							|  |  |  |         local Info = Node.Info; | 
					
						
							|  |  |  |         //执行时间 | 
					
						
							|  |  |  |         local exec_time = Node.time; | 
					
						
							|  |  |  |         //如果没到执行时间,放回去,等待下次扫描 | 
					
						
							|  |  |  |         if (time() <= exec_time) { | 
					
						
							| 
									
										
										
										
											2024-10-27 15:32:44 +08:00
										 |  |  |             // Date_Exec_Tree.insert(exec_time, Node.Info); | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-10-27 15:32:44 +08:00
										 |  |  |         Date_Exec_Tree.pop(); | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |         //函数 | 
					
						
							|  |  |  |         local func = Info[0]; | 
					
						
							|  |  |  |         //参数 | 
					
						
							|  |  |  |         local func_args = Info[1]; | 
					
						
							| 
									
										
										
										
											2024-10-11 23:56:27 +08:00
										 |  |  |         //Cron字符串 | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |         local NextTimestep = Info[2]; | 
					
						
							|  |  |  |         //执行函数 | 
					
						
							| 
									
										
										
										
											2025-04-21 13:36:35 +08:00
										 |  |  |         local Flag = func.acall(func_args); | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |         //继续构建下一次任务 | 
					
						
							| 
									
										
										
										
											2025-04-21 13:36:35 +08:00
										 |  |  |         if(Flag == null || Flag == true) Date_Exec_Tree.insert(Sq_Cron_Next(NextTimestep, time()), Info, Node.name); | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-21 13:36:35 +08:00
										 |  |  |     function SetCronTask(target_func, build_info, ...) { | 
					
						
							|  |  |  |         local CronString = ""; | 
					
						
							|  |  |  |         local TaskName = null; | 
					
						
							|  |  |  |         //如果是字符串直接就是Cron字符串 | 
					
						
							|  |  |  |         if (typeof build_info == "string") { | 
					
						
							|  |  |  |             CronString = build_info; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         //如果是Table 则有名字 | 
					
						
							|  |  |  |         else if (typeof build_info == "table") { | 
					
						
							|  |  |  |             CronString = build_info.Cron; | 
					
						
							|  |  |  |             TaskName = build_info.Name; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |         local NowTimestep = time(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //下一次执行的时间 | 
					
						
							| 
									
										
										
										
											2024-10-11 23:56:27 +08:00
										 |  |  |         local NextTimestep = Sq_Cron_Next(CronString, NowTimestep); | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         local target_arg_list = []; | 
					
						
							|  |  |  |         target_arg_list.push(getroottable()); | 
					
						
							|  |  |  |         for (local i = 0; i< vargv.len(); i++) { | 
					
						
							|  |  |  |             target_arg_list.push(vargv[i]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //设置下一次执行 | 
					
						
							|  |  |  |         local func_info = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //函数体 | 
					
						
							|  |  |  |         func_info.push(target_func); | 
					
						
							|  |  |  |         //参数列表 | 
					
						
							|  |  |  |         func_info.push(target_arg_list); | 
					
						
							|  |  |  |         //间隔时间戳时间 | 
					
						
							| 
									
										
										
										
											2024-10-11 23:56:27 +08:00
										 |  |  |         func_info.push(CronString); | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-21 13:36:35 +08:00
										 |  |  |         _Timer_Object.Date_Exec_Tree.insert(NextTimestep, func_info, TaskName); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function RemoveCronTask(name) { | 
					
						
							|  |  |  |         _Timer_Object.Date_Exec_Tree.removeNodeByName(name); | 
					
						
							| 
									
										
										
										
											2024-10-02 21:00:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function Update() { | 
					
						
							|  |  |  |         CheckTimeOut(); | 
					
						
							|  |  |  |         CheckCronTask(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-09-20 19:24:32 +08:00
										 |  |  | } |