分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-03-14 73a926018601d9a5a5a3d3f4c051537f45a8eff4
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Quartz;
using Quartz.Impl.Matchers;
using System.Diagnostics;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Core.Extensions;
using WIDESEA_Core.Extensions.AutofacManager;
using WIDESEA_Core.FreeDB;
using WIDESEA_Core.ManageUser;
using WIDESEA_Core.Services;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.IServices;
using WIDESEA_WCS.Services.WCS.Partial.Scheduler;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS.Services
{
    public partial class VV_DispatchService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IVV_DispatchRepository _repository;//访问数据库
 
        FreeDB freeDB = new FreeDB();
        SchedulerCenter scheduler = new SchedulerCenter();
 
        [ActivatorUtilitiesConstructor]
        public VV_DispatchService(
            IVV_DispatchRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码,其他情况可以不用
            //base.Init(dbRepository);
        }
 
        public VV_DispatchService(IVV_DispatchRepository repository)
        : base(repository)
        {
            Init(repository);
        }
 
        public static IVV_DispatchService Instance
        {
            get { return AutofacContainerModule.GetService<IVV_DispatchService>(); }
        }
 
 
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <returns></returns>
        public WebResponseContent StartServe()
        {
            WebResponseContent webResponse = new WebResponseContent();
            try
            {
                if (!scheduler.IsRun())
                {
                    var cooRes = CoonPLC();
                    var startRes = SrartScheduler();
                    if (startRes.Status)
                    {
                        if (cooRes.Status)
                        {
                            WriteDBLog.Success($"开启服务", new { 数据 = "服务启动成功!" }, "PCS", UserContext.Current.UserName);
                            return webResponse.OK("服务启动成功!");
                        }
                        else
                        {
                            //返回PLC异常
                            WriteDBLog.Error($"开启服务", new { 数据 = cooRes }, "PCS", UserContext.Current.UserName);
                            return cooRes;
                        }
                    }
                    else
                    {
                        //调度启动失败,关闭全部
                        //WriteLog.Write_Log("服务", "服务", "启动服务\n[调度异常]" + startRes.Message);
                        WriteDBLog.Error($"开启服务", new { 数据 = startRes.Message }, "PCS", UserContext.Current.UserName);
                        return startRes;
                    }
                }
                else
                {
                    return webResponse.Error("请勿重复启动!");
                }
            }
            catch (Exception ex)
            {
                //WriteLog.Write_Log("服务", "服务", "启动服务\n[异常]" + ex.Message);
                WriteDBLog.Error($"开启服务", new { 数据 = ex.Message }, "PCS", UserContext.Current.UserTrueName);
                return webResponse.Error($"异常,{ex.Message}");
            }
        }
 
        /// <summary>
        /// 启动调度
        /// </summary>
        private WebResponseContent SrartScheduler()
        {
            WebResponseContent webResponse = new WebResponseContent();
            var jobs = freeDB.Select<VV_Dispatch>().ToList();
            try
            {
                foreach (var job in jobs)
                {
                    scheduler.AddJob(job);
                }
                scheduler.Start();
            }
            catch (Exception ex)
            {
                return webResponse.Error(ex.Message);
            }
            Console.WriteLine("服务启动成功!");
            return webResponse.OK();
        }
 
        /// <summary>
        /// 连接PLC
        /// </summary>
        /// <returns></returns>
        private WebResponseContent CoonPLC()
        {
            WebResponseContent content = new WebResponseContent();
            var plcList = freeDB.Select<dt_plcinfohead>().Where("EXISTS (select * from dt_equipmentinfo where equipment_state='Enable' and equipment_name=a.plcinfo_name)").ToList();
            if (plcList.Count == 0)
            {
                return content.OK();
                //throw new Exception("未配置任何PLC连接对象!");
            }
 
            var detialList = freeDB.Select<dt_plcinfodetail>().ToList();
            PLCClient.Clients = new List<PLCClient>();
 
            List<string> coonMsgList = new List<string>();
            foreach (var plc in plcList)
            {
                try
                {
                    #region PLC实例化
                    PLCClient client = null;
                    if (plc.plcinfo_type == "Simene")
                    {
                        client = new SiemensPLCClient(plc.plcinfo_model)
                        {
                            PLCName = plc.plcinfo_name,
                            Ip = plc.plcinfo_ip,
                            Port = plc.plcinfo_port ?? 102,
                            Slot = plc.plcinfo_slot ?? 0
                        };
                    }
                    else if (plc.plcinfo_type == "SimeneS7_200smart")
                    {
                        client = new SiemensPLCClient(HslCommunication.Profinet.Siemens.SiemensPLCS.S200Smart)
                        {
                            PLCName = plc.plcinfo_name,
                            Ip = plc.plcinfo_ip,
                            Port = plc.plcinfo_port ?? 102,
                            Slot = plc.plcinfo_slot ?? 0
                        };
                    }
                    else if (plc.plcinfo_type == "ModelBus")
                    {
                        client = new ModelBusClient()
                        {
                            PLCName = plc.plcinfo_name,
                            Ip = plc.plcinfo_ip,
                            Port = plc.plcinfo_port ?? 102,
                            Slot = plc.plcinfo_slot ?? 0
                        };
                    }
                    #endregion
 
                    if (client != null)
                    {
                        client.itemGroups = GetItemData(detialList, plc.plcinfo_iotyep);
                        string coonMsg = client.Connect();
                        coonMsgList.Add(coonMsg);
 
                        PLCClient.Clients.Add(client);
                    }
                    else
                    {
                        coonMsgList.Add($"{plc.plcinfo_name},PLC类型{plc.plcinfo_type}不存在。");
                    }
                }
                catch (Exception ex)
                {
                    coonMsgList.Add($"{plc.plcinfo_name}异常,{ex.Message}");
                }
            }
 
            string msg = string.Empty;
            var coonErrorMsgList = coonMsgList.Where(t => !t.EndsWith("连接成功")).ToList();
            WriteLog.Write_Log("服务", "服务", "启动服务\n" + string.Join("\n", coonErrorMsgList) + "\n异常数:" + coonErrorMsgList.Count);
            if (coonErrorMsgList.Count == 0)
            {
                return content.OK();
            }
            else
            {
                msg = string.Join("\n", coonErrorMsgList);
                return content.Error(msg);
            }
        }
 
        /// <summary>
        /// 获取交互协议
        /// </summary>
        /// <param name="detialList"></param>
        /// <param name="plcinfo_iotyep"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private List<DBItemGroup> GetItemData(List<dt_plcinfodetail> detialList, string plcinfo_iotyep)
        {
            List<DBItemGroup> itmes = new List<DBItemGroup>();
            var dataList = detialList.Where(t => t.plcdetail_iotype == plcinfo_iotyep).ToList();
            foreach (var item in dataList)
            {
                var order = new DBItemGroup();
                order.name = item.plcdetail_name;
                order.dataType = item.plcdetail_valtype;
                if (string.IsNullOrEmpty(item.plcdetail_value))
                {
                    order.dbAddress = item.plcdetail_db;
                }
                else
                {
                    order.dbAddress = item.plcdetail_db + "." + item.plcdetail_value;
                }
                order.dataLen = item.plcdetail_len;
                order.Methods = item.plcdetail_number;//
                itmes.Add(order);
            }
            return itmes;
        }
 
        /// <summary>
        /// 关闭服务
        /// </summary>
        /// <returns></returns>
        public WebResponseContent CloseServe()
        {
            WebResponseContent webResponse = new WebResponseContent();
            try
            {
                foreach (var item in PLCClient.Clients)
                {
                    try
                    {
                        item.DisConnect();
                    }
                    catch { }
                }
                scheduler.Shutdown();
                Console.WriteLine("服务关闭成功!");
                WriteLog.Write_Log("服务", "服务", "服务关闭成功!");
                WriteDBLog.Success($"关闭服务", new { 数据 = "服务关闭成功!" }, "PCS", UserContext.Current.UserTrueName);
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("服务", "服务", "关闭服务\n[异常]" + ex.Message);
                WriteDBLog.Error($"关闭服务", new { 数据 = ex.Message }, "PCS", UserContext.Current.UserTrueName);
                return webResponse.Error(ex.Message);
            }
            return webResponse.OK("服务关闭成功!");
        }
 
        /// <summary>
        /// 开启指定任务
        /// </summary>
        /// <param name="jobName"></param>
        /// <returns></returns>
        public WebResponseContent StartJob(string jobName)
        {
            WebResponseContent webResponse = new WebResponseContent();
            try
            {
                if (!scheduler.IsRun())
                {
                    throw new Exception("请先启动服务!");
                    //return webResponse.Error("请先启动服务!");
                }
                var jobData = freeDB.Select<VV_Dispatch>().Where(t => t.JobName == jobName).First();
                if (jobData == null)
                {
                    throw new Exception($"调度{jobName}不存在!");
                    //webResponse.Error($"调度{jobName}不存在!");
                }
                else
                {
                    var job = scheduler.GetJobByJobName(jobName);
                    if (job != null)
                    {
                        throw new Exception($"调度{jobName}已是执行状态!");
                        //webResponse.Error($"调度{jobName}已是执行状态!");
                    }
                    else
                    {
                        scheduler.AddJob(jobData);
                        webResponse.OK($"调度{jobName}执行成功!");
                    }
                }
            }
            catch (Exception ex)
            {
                WriteDBLog.Error($"执行{jobName}调度", new { 数据 = ex.Message }, "PCS", UserContext.Current.UserTrueName);
                return webResponse.Error(ex.Message);
            }
            WriteDBLog.Success($"执行{jobName}调度", new { 数据 = jobName + "执行成功!" }, "PCS", UserContext.Current.UserTrueName);
            return webResponse;
        }
 
        /// <summary>
        /// 关闭指定任务
        /// </summary>
        /// <param name="jobName"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public WebResponseContent CloseJob(string jobName)
        {
            WebResponseContent webResponse = new WebResponseContent();
            try
            {
                if (!scheduler.IsRun())
                {
                    throw new Exception("请先启动服务!");
                    //return webResponse.Error("请先启动服务!");
                }
 
                var job = scheduler.GetJobByJobName(jobName);
                if (job == null)
                {
                    throw new Exception($"任务{jobName},不存在!");
                    //return webResponse.Error($"任务{jobName},不存在!");
                }
                var res = scheduler.RemoveJob(job);
                if (res)
                {
                    webResponse.OK($"调度{jobName}暂停成功!");
                }
                else
                {
                    throw new Exception($"调度{jobName}暂停失败!");
                    //webResponse.Error($"调度{jobName}关闭失败!");
                }
            }
            catch (Exception ex)
            {
                WriteDBLog.Error($"暂停{jobName}调度", new { 数据 = ex.Message }, "PCS", UserContext.Current.UserTrueName);
                return webResponse.Error(ex.Message);
            }
            WriteDBLog.Success($"暂停{jobName}调度", new { 数据 = jobName + "暂停成功!" }, "PCS", UserContext.Current.UserTrueName);
            return webResponse;
        }
 
        /// <summary>
        /// 检查服务状态
        /// </summary>
        /// <returns></returns>
        public WebResponseContent CheckServeState()
        {
            WebResponseContent webResponse = new WebResponseContent();
            var state = scheduler.IsRun();
            if (state)
            {
                return webResponse.OK("调度已启动!");
            }
            else
            {
                return webResponse.Error("调度未启动!");
            }
        }
 
        /// <summary>
        /// 获取调度详情
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public override PageGridData<VV_Dispatch> GetPageData(PageDataOptions options)
        {
            var data = base.GetPageData(options);
            var isRun = scheduler.IsRun();
            if (isRun)
            {
                var jobs = scheduler.center.GetJobKeys(GroupMatcher<JobKey>.AnyGroup()).Result;
                foreach (var item in data.rows)
                {
                    item.IsRun = jobs.Where(t => t.Name == item.JobName).Any();
                }
            }
            else
            {
                data.rows.ForEach(t => { t.IsRun = false; });
            }
            return data;
        }
 
        public override WebResponseContent Upload(List<IFormFile> files)
        {
            #region
            WebResponseContent Response = new WebResponseContent();
            if (files == null || files.Count == 0) return Response.Error("请上传文件");
 
            string msg = "";
            string IP = "\\\\" + "192.168.1.35" + "\\ipc$";
            string password = "971204";
            string User = "dell";
 
            int i = 0;
            string filePath = $"\\\\192.168.1.35\\TextTest";
            string fullPath = filePath.MapPath(true);
            Process process = new Process();
            try
            {
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;
                process.Start();
                string dosLine = "net use " + IP + " " + password + " /user:" + User;
                process.StandardInput.WriteLine(dosLine);
                process.StandardInput.WriteLine("exit");
                while (!process.HasExited)
                {
                    process.WaitForExit(1000);
                }
                msg = process.StandardError.ReadToEnd();
                process.StandardError.Close();
                if (msg != "")
                    throw new Exception(msg);
                if (!Directory.Exists(fullPath)) Directory.CreateDirectory(fullPath);
                for (i = 0; i < files.Count; i++)
                {
                    string fileName = files[i].FileName;
                    fullPath = fullPath + "/" + fileName;
                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        files[i].CopyTo(stream);
                    }
                    //FileStream outFileStream = new FileStream(fullPath, FileMode.Create);
                    //files[i].CopyTo(outFileStream);
                }
                return Response.OK("文件上传成功", filePath);
            }
            catch (Exception ex)
            {
                //return Response.Error(ex.Message);
                Logger.Error($"上传文件失败:{typeof(T).GetEntityTableCnName()},路径:{filePath},失败文件:{files[i]},{ex.Message + ex.StackTrace}");
                return Response.Error("文件上传失败");
            }
            finally
            {
                process.Close();
                process.Dispose();
            }
 
            #endregion
        }
    }
}