public static class SchedulerHelper { private static List<SchedulerInfo> jobs; private static IScheduler sched; public static void InitalScheduler() { var sched = GetScheduler(); var list = GetJobs(); foreach (var s in list) { sched.ScheduleJob(s.Job, s.Trigger); } sched.Start(); } public static void Restart() { var sched = GetScheduler(); sched.Start(); } public static List<SchedulerInfo> GetJobs() { if (jobs == null) { jobs = new List<SchedulerInfo>(); //从数据库读定时任务信息 TimerService ts = new TimerService(); var timers = ts.GetAllTimers(); foreach (var t in timers) { AddJob(t); } } return jobs; } public static IScheduler GetScheduler() { if (sched == null) { ISchedulerFactory sf = new StdSchedulerFactory(); sched = sf.GetScheduler(); } return sched; } public static void AddJob(Timer t) { IJobDetail job; switch (t.Action) { case TimerAction.MeterRecord: job = JobBuilder.Create<MeterJob>().WithIdentity(Guid.NewGuid().ToString("n"), "group").Build(); break; case TimerAction.MultiPowerOn: job = JobBuilder.Create<PowerOnJob>().UsingJobData("lampIds", t.Parameter).WithIdentity(Guid.NewGuid().ToString("n"), "group").Build(); break; case TimerAction.MultiPowerOff: job = JobBuilder.Create<PowerOffJob>().UsingJobData("lampIds", t.Parameter).WithIdentity(Guid.NewGuid().ToString("n"), "group").Build(); break; default: job = JobBuilder.Create<OtherJob>().WithIdentity(Guid.NewGuid().ToString("n"), "group").Build(); break; } ITrigger trigger = TriggerBuilder.Create() .WithIdentity(Guid.NewGuid().ToString("n"), "group").StartNow() .WithCronSchedule("0/30 * * * * ?") .Build(); if (job != null) { jobs.Add(new SchedulerInfo() { Job = job, Trigger = trigger, TimerId = t.TimerId }); Restart(); } } public static void DelJob(Timer t) { var sched = GetScheduler(); var list = GetJobs(); SchedulerInfo si = list.Where(a => a.TimerId == t.TimerId).FirstOrDefault(); JobKey key = si.Job.Key; sched.DeleteJob(si.Job.Key); } } class SchedulerInfo { public IJobDetail Job { get; set; } public ITrigger Trigger { get; set; } public int TimerId { get; set; } } public class PowerOnJob : IJob { public void Execute(IJobExecutionContext context) { string lampIds = context.JobDetail.JobDataMap.Values.FirstOrDefault().ToString(); List<int> ids = lampIds.Split(',').Select(i => int.Parse(i)).ToList(); //调用业务层的断电方法 Business.PowerService powerService = new Business.PowerService(); var isSuccess = powerService.PowerOffDevices(ids); IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<BaseHub>(); foreach (var item in ids) { hubContext.Clients.All.PowerOn(new { lampId = item, isSuccess = isSuccess }); } } } public class PowerOffJob : IJob { public void Execute(IJobExecutionContext context) { string lampIds = context.JobDetail.JobDataMap.Values.FirstOrDefault().ToString(); List<int> ids = lampIds.Split(',').Select(i => int.Parse(i)).ToList(); //调用业务层的断电方法 Business.PowerService powerService = new Business.PowerService(); var isSuccess = powerService.PowerOffDevices(ids); IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<BaseHub>(); foreach (var item in ids) { hubContext.Clients.All.PowerOff(new { lampId = item, isSuccess = isSuccess }); } } } public class MeterJob : IJob { public void Execute(IJobExecutionContext context) { List<string> areas = new List<string>(); AreaService aService = new AreaService(); aService.GetGlobalAreas(1).Where(a => a.AreaId > 1).ToList().ForEach(a => areas.Add(a.AreaId.ToString())); //调用ExportService的导出方法 string path = System.Web.HttpContext.Current.Server.MapPath("~/Files/"); ExportService service = new ExportService(); service.ExportMeters(path, DateTime.Now.ToShortDateString() + ".xls", areas.ToList()); } } public class OtherJob : IJob { public void Execute(IJobExecutionContext context) { } }