Admin
2025-12-02 9e42f0dafa019f5ecf6b0ff425ecb966b002171e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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;
        }
 
        
    }
}