using Microsoft.AspNetCore.Builder;
|
using Quartz.Impl;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using WIDESEA_Common;
|
using WIDESEA_Common.CutomerModel;
|
using WIDESEA_Core.EFDbContext;
|
using WIDESEA_Core.Utilities;
|
using WIDESEA_Entity.DomainModels;
|
using WIDESEA_Services.IRepositories;
|
using WIDESEA_Services.IServices;
|
using WIDESEA_Services.Repositories;
|
using WIDESEA_Services.Services;
|
using WIDESEA_WCS.WCSClient;
|
|
namespace WIDESEA_WCS
|
{
|
/// <summary>
|
/// Quartz 启动服务
|
/// </summary>
|
public static class QuartzJobMiddleWare
|
{
|
/// <summary>
|
/// 使用Quartz(任务调度库)Job(工作)模式
|
/// </summary>
|
/// <param name="app"></param>
|
/// <exception cref="ArgumentNullException"></exception>
|
public static void UseQuartzJobMildd(this IApplicationBuilder app)
|
{
|
if (app == null) throw new ArgumentNullException(nameof(app));
|
try
|
{
|
//WIDESEA.Helper.GetToken();
|
using (VOLContext context = new VOLContext())
|
{
|
IDt_DispatchManagementRepository dispatchManagementRepository = new Dt_DispatchManagementRepository(context);
|
List<Dt_DispatchManagement> dispatchManagementList = dispatchManagementRepository.Find(r => r.dm_state == "Enable");
|
if (null == dispatchManagementList)
|
{
|
Console.Out.WriteLine("WCS系统未配置或未启用任务计划,请检查。");
|
return;
|
}
|
WCSService.jobs = FindJobOptions(dispatchManagementList);
|
WCSService.Clients = new List<PLCClient>();
|
StdSchedulerFactory factory = new StdSchedulerFactory();
|
ISchedulerCenterServer schedulerCenter = new SchedulerCenterServer(factory);
|
IDt_PLCinfoHeadRepository pLcinfoHeadRepository = new Dt_PLCinfoHeadRepository(context);
|
IDt_PLCinfoDetailRepository plcinfoDetailRepository = new Dt_PLCinfoDetailRepository(context);
|
List<Dt_PLCinfoHead> pLcinfoHeadsList = pLcinfoHeadRepository.Find(x => true);
|
|
foreach (Dt_PLCinfoHead item in pLcinfoHeadsList)
|
{
|
PLCClient client = new PLCClient
|
{
|
PLCIPAddress = item.plcinfo_ip,
|
PLCName = item.plcinfo_name,
|
PLCDescription = item.plcinfo_state,
|
PLCDBItems = plcinfoDetailRepository.Find(x => x.plcdetail_headid == item.plcinfo_id
|
&& !string.IsNullOrEmpty(x.plcdetail_name)).Select(x => new DBItemGroup
|
{
|
ItemAddress = x.plcdetail_db.Trim() + "." + x.plcdetail_value.Trim(),
|
ItemDataType = x.plcdetail_valtype.Trim(),
|
ItemName = x.plcdetail_name.Trim(),
|
ItemOperatorType = x.plcdetail_opratortype?.Trim(),
|
EquipNum = x.plcdetail_number.Trim(),
|
Remark = x.plcdetail_remark.Trim()
|
}).ToList(),
|
plcinfo_equiptype = item.plcinfo_equiptype
|
};
|
string msg = client.Connect();
|
client.Start(() => { });
|
WCSService.Clients.Add(client);
|
}
|
|
WCSService.jobs.ForEach(x => { x.JobParams = WCSService.Clients.Where(y => y.plcinfo_equiptype == x.JobGroup).ToList(); });
|
if (!WCSService.jobs.Any())
|
return;
|
for (int i = 0; i < WCSService.jobs.Count; i++)
|
{
|
WebResponseContent content = schedulerCenter.AddScheduleJobAsync(WCSService.jobs[i]).Result;
|
if (!content.Status)
|
{
|
factory = null;
|
schedulerCenter = null;
|
return;
|
}
|
}
|
schedulerCenter.StartScheduleAsync();
|
}
|
}
|
catch (Exception ex)
|
{
|
Console.Out.WriteLine("启动WCS服务失败." + ex.Message);
|
}
|
}
|
|
/// <summary>
|
/// 查找任务计划
|
/// </summary>
|
/// <param name="dispatches"></param>
|
/// <returns></returns>
|
public static List<JobOptions> FindJobOptions(List<Dt_DispatchManagement> dispatches)
|
{
|
List<JobOptions> jobOptions = new List<JobOptions>();
|
foreach (var item in dispatches)
|
{
|
JobOptions options = new JobOptions()
|
{
|
AssemblyName = item.dm_assembly,
|
BeginTime = item.dm_begintime,
|
ClassName = item.dm_class,
|
Cron = item.dm_cron,
|
CycleRunTimes = item.dm_cycleruntimes ?? 0,
|
EndTime = item.dm_endtime,
|
IntervalSecond = item.dm_intervalsecond,
|
IsStart = false,
|
JobGroup = item.dm_group,
|
JobName = item.dm_group,
|
JobParams = null,
|
Remark = item.dm_remark,
|
TriggerType = item.dm_triggertype,
|
PLCConnectState = "未连接PLC"
|
};
|
jobOptions.Add(options);
|
}
|
return jobOptions;
|
}
|
|
|
}
|
}
|