唐山轨道机控制端TCP服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
14 KiB

using Quartz.Impl;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace Emergency_platform
{
internal class TimedTask:IJob
{
public TimedTask()
{
}
public void pp()
{
HashSet<DayOfWeek> hashSet2 = new HashSet<DayOfWeek>();
foreach (object value in Enum.GetValues(typeof(DayOfWeek)))
{
var allDaysOfTheWeek = new HashSet<DayOfWeek>();
foreach (var d in Enum.GetValues(typeof(DayOfWeek)))
{
var dayOfWeek = (DayOfWeek)d;
allDaysOfTheWeek.Add(dayOfWeek);
if (dayOfWeek >= DayOfWeek.Monday && dayOfWeek <= DayOfWeek.Friday)
{
//mondayThroughFriday.Add(dayOfWeek);
}
}
}
//DayOfWeek dayOfWeek = (DayOfWeek)value;
//hashSet.Add(dayOfWeek);
//if (dayOfWeek >= DayOfWeek.Monday && dayOfWeek <= DayOfWeek.Friday)
//{
// hashSet2.Add(dayOfWeek);
//}
}
public virtual Task Execute(IJobExecutionContext context)
{
//执行控制轨道机巡逻任务
//取出巡逻点位集合
string[] sPatrols = context.JobDetail.JobDataMap.GetString("parameter").Split(';');
for (int i = 0; i < sPatrols.Length; i++)
{
string[] sPatrol = sPatrols[i].Split(',');
//下达巡逻指令
//GuiDaoJiMove(sPatrol[0], sPatrol[1]);
}
Console.WriteLine("业务逻辑" + DateTime.Now);
PrintInfo("业务逻辑" + DateTime.Now);
Thread.Sleep(60000);
return Task.CompletedTask;
}
/////// <summary>
/////// 每天HH:mm:ss进行任务
/////// </summary>
/////// <param name="sTime">时间:格式必须为“HH:mm:ss”</param>
/////// <param name="sPatrols">巡逻点位计集合:格式必须为“x1,y1;x2,y2;......”</param>
////public static void DailyJob(string sTime, string sPatrols)
////{
//// int iHour = Convert.ToInt32(sTime.Substring(0, 2));
//// int iMinute = Convert.ToInt32(sTime.Substring(3, 2));
//// int iSecond = Convert.ToInt32(sTime.Substring(6, 2));
//// //创建一个调度器
//// IScheduler _temp_schheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
//// // 创建作业
//// IJobDetail job = JobBuilder.Create<TimedTask>()
//// .WithIdentity("dailyTaskJob", "group1")
//// .Build();
//// // 创建触发器,每天九点
//// ITrigger trigger = TriggerBuilder.Create()
//// .WithIdentity("dailyTaskTrigger", "group1")
//// .StartNow()
//// .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(iHour, iMinute)) // 每天9:00执行
//// .Build();
//// // 将作业和触发器加入调度器
//// _temp_schheduler.ScheduleJob(job, trigger);
//// _temp_schheduler.Start();
////}
/// <summary>
/// 每天HH:mm:ss进行任务
/// </summary>
/// <param name="sTime">时间:格式必须为“HH:mm:ss”</param>
/// <param name="sPatrols">巡逻点位计集合:格式必须为“x1,y1;x2,y2;......”</param>
public static async Task DailyJob(string sTime, string sPatrols)
{
if (sTime == null)
{
return;
}
int iHour = Convert.ToInt32(sTime.Substring(0, 2));
int iMinute = Convert.ToInt32(sTime.Substring(3, 2));
int iSecond = Convert.ToInt32(sTime.Substring(6, 2));
string sJob = "job" + iHour + iMinute + iSecond;
string sGroup = "group" + iHour + iMinute + iSecond;
string sTrigger = "trigger" + iHour + iMinute + iSecond;
//每天每天HH:mm:ss进行任务
string cronExpression = string.Format("{0} {1} {2} ? * *", iSecond, iMinute, iHour);
//创建一个调度器
IScheduler _temp_schheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
// 创建作业
IJobDetail job = JobBuilder.Create<TimedTask>()
.WithIdentity(sJob, sGroup)
.UsingJobData("parameter", sPatrols)
.Build();
// 创建触发器,每天上午sTime执行
ITrigger trigger = TriggerBuilder.Create().WithIdentity(sTrigger, sGroup)
.WithCronSchedule(cronExpression)
.WithPriority(10) // 设置优先级,范围是0到255,数字越高,优先级越高
.Build();
// 将作业和触发器加入调度器
await _temp_schheduler.ScheduleJob(job, trigger);
await _temp_schheduler.Start();
}
///// <summary>
///// 每天HH:mm:ss进行任务
///// </summary>
///// <param name="sTime">时间:格式必须为“HH:mm:ss”</param>
///// <param name="sPatrols">巡逻点位计集合:格式必须为“x1,y1;x2,y2;......”</param>
//public static void DailyJob(string sTime, string sPatrols)
//{
// if (sTime == null)
// {
// return;
// }
// int iHour = Convert.ToInt32(sTime.Substring(0, 2));
// int iMinute = Convert.ToInt32(sTime.Substring(3, 2));
// int iSecond = Convert.ToInt32(sTime.Substring(6, 2));
// string sJob = "job" + iHour + iMinute + iSecond;
// string sGroup = "group" + iHour + iMinute + iSecond;
// string sTrigger = "trigger" + iHour + iMinute + iSecond;
// //创建一个调度器
// IScheduler _temp_schheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
// // 创建作业
// IJobDetail job = JobBuilder.Create<TimedTask>()
// .WithIdentity(sJob, sGroup)
// //.UsingJobData("parameter", sPatrols)
// .Build();
// // 创建触发器,每天上午sTime执行
// ITrigger trigger = TriggerBuilder.Create()
// .WithIdentity(sTrigger, sGroup)
// .WithDailyTimeIntervalSchedule(a =>
// a.WithIntervalInHours(24)
// .OnEveryDay()
// .StartingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(iHour, iMinute, iSecond)))
// //.WithPriority(10) // 设置优先级,范围是0到255,数字越高,优先级越高
// .Build();
// // 将作业和触发器加入调度器
// _temp_schheduler.ScheduleJob(job, trigger);
// _temp_schheduler.Start();
//}
/// <summary>
/// 每周中的某几天进行任务
/// </summary>
/// <param name="onDaysOfWeek">周中某几天</param>
/// <param name="sTime">时间:格式必须为“HH:mm:ss”</param>
/// <param name="iNo">编号(不能重复)</param>
/// <param name="sPatrols">巡逻点位计集合:格式必须为“x1,y1;x2,y2;......”</param>
public static async Task WeeklyJob(IReadOnlyCollection<DayOfWeek> onDaysOfWeek, string sTime, int iNo, string sPatrols)
{
int iHour = Convert.ToInt32(sTime.Substring(0, 2));
int iMinute = Convert.ToInt32(sTime.Substring(3, 2));
int iSecond = Convert.ToInt32(sTime.Substring(6, 2));
string sJob = "jobWeek" + iNo;
string sGroup = "groupWeek" + iNo;
string sTrigger = "triggerWeek" + iNo;
//创建一个调度器
IScheduler _temp_schheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
// 创建作业
IJobDetail job = JobBuilder.Create<TimedTask>()
.WithIdentity(sJob, sGroup)
.UsingJobData("parameter", sPatrols)
.Build();
// 创建触发器,每天上午sTime执行
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(sTrigger, sGroup)
.WithDailyTimeIntervalSchedule(a =>
a.OnDaysOfTheWeek(onDaysOfWeek)
.StartingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(iHour, iMinute, iSecond)))
//.WithPriority(10) // 设置优先级,范围是0到255,数字越高,优先级越高
.Build();
// 将作业和触发器加入调度器
await _temp_schheduler.ScheduleJob(job, trigger);
await _temp_schheduler.Start();
}
///// <summary>
///// 每周中的某几天进行任务(暂时无法实现)
///// </summary>
///// <param name="sDaysOfWeek">周中某几天,如"1,2,3"即每周的周一周二周三,用半角逗号隔开</param>
///// <param name="sTime">时间:格式必须为“HH:mm:ss”</param>
///// <param name="iNo">编号(不能重复)</param>
///// <param name="sPatrols">巡逻点位计集合:格式必须为“x1,y1;x2,y2;......”</param>
//public static void WeeklyJob(string sDaysOfWeek, string sTime, int iNo, string sPatrols)
//{
// if (sTime == null)
// {
// return;
// }
// int iHour = Convert.ToInt32(sTime.Substring(0, 2));
// int iMinute = Convert.ToInt32(sTime.Substring(3, 2));
// int iSecond = Convert.ToInt32(sTime.Substring(6, 2));
// string sJob = "jobWeek" + iNo;
// string sGroup = "groupWeek" + iNo;
// string sTrigger = "triggerWeek" + iNo;
// //每周中的某几天某点进行任务
// string cronExpression = string.Format("{0} {1} {2} ? * {3}", iSecond, iMinute, iHour, sDaysOfWeek);
// //创建一个调度器
// IScheduler _temp_schheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
// // 创建作业
// IJobDetail job = JobBuilder.Create<TimedTask>().WithIdentity(sJob, sGroup).Build();
// //给任务传参
// job.JobDataMap.Add("parameter", sPatrols);
// // 创建触发器,每周某几天某点进行作业
// ITrigger trigger = TriggerBuilder.Create().WithIdentity(sTrigger, sGroup)
// .WithCronSchedule(cronExpression)
// .WithPriority(10) // 设置优先级,范围是0到255,数字越高,优先级越高
// .Build();
// // 将作业和触发器加入调度器
// _temp_schheduler.ScheduleJob(job, trigger);
// _temp_schheduler.Start();
//}
/// <summary>
/// 每月中的某几天进行任务
/// </summary>
/// <param name="sDays">每月的某几天,如"1,5,10"即每月的1号,5号,10号执行。用半角逗号隔开</param>
/// <param name="sTime">时间:格式必须为“HH:mm:ss”</param>
/// <param name="iNo">编号(不能重复)</param>
/// <param name="sPatrols">巡逻点位计集合:格式必须为“x1,y1;x2,y2;......”</param>
public static async Task CertainDaysJob(string sDays, string sTime, int iNo, string sPatrols)
{
int iHour = Convert.ToInt32(sTime.Substring(0, 2));
int iMinute = Convert.ToInt32(sTime.Substring(3, 2));
int iSecond = Convert.ToInt32(sTime.Substring(6, 2));
string sJob = "jobDays" + iNo;
string sGroup = "groupDays" + iNo;
string sTrigger = "triggerDays" + iNo;
// 在Quartz.NET中使用Cron表达式
//string cronExpression = "0 15 10 5,15 * ?"; // 每月的第5号和第15号上午10点15分
string cronExpression = string.Format("{0} {1} {2} {3} * ?", iSecond, iMinute, iHour, sDays);
//创建一个调度器
IScheduler _temp_schheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
// 创建作业
IJobDetail job = JobBuilder.Create<TimedTask>().WithIdentity(sJob, sGroup).Build();
//给任务传参
job.JobDataMap.Add("parameter", sPatrols);
// 创建触发器,每周某几天0点进行作业
ITrigger trigger = TriggerBuilder.Create().WithIdentity(sTrigger, sGroup)
.WithCronSchedule(cronExpression)
.WithPriority(10) // 设置优先级,范围是0到255,数字越高,优先级越高
.Build();
// 将作业和触发器加入调度器
await _temp_schheduler.ScheduleJob(job, trigger);
await _temp_schheduler.Start();
}
public static void PrintInfo(string str)
{
string Folder = Environment.CurrentDirectory + @"\Logs";
string FileNamme = Environment.CurrentDirectory + @"\Logs\Info_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
if (!System.IO.Directory.Exists(Folder))
{
System.IO.Directory.CreateDirectory(Folder);
}
using (TextWriter fw = new StreamWriter(FileNamme, true))
{
try
{
FileInfo fi = new FileInfo(FileNamme);
fw.WriteLine(str);
}
catch (Exception ex)
{
fw.WriteLine(ex.Message);
}
finally
{
fw.Close();
fw.Dispose();
}
}
}
}
}