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(); } } /// /// 启动服务 /// /// 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}"); } } /// /// 启动调度 /// private WebResponseContent SrartScheduler() { WebResponseContent webResponse = new WebResponseContent(); var jobs = freeDB.Select().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(); } /// /// 连接PLC /// /// private WebResponseContent CoonPLC() { WebResponseContent content = new WebResponseContent(); var plcList = freeDB.Select().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().ToList(); PLCClient.Clients = new List(); List coonMsgList = new List(); 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); } } /// /// 获取交互协议 /// /// /// /// /// private List GetItemData(List detialList, string plcinfo_iotyep) { List itmes = new List(); 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; } /// /// 关闭服务 /// /// 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("服务关闭成功!"); } /// /// 开启指定任务 /// /// /// public WebResponseContent StartJob(string jobName) { WebResponseContent webResponse = new WebResponseContent(); try { if (!scheduler.IsRun()) { throw new Exception("请先启动服务!"); //return webResponse.Error("请先启动服务!"); } var jobData = freeDB.Select().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; } /// /// 关闭指定任务 /// /// /// /// 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; } /// /// 检查服务状态 /// /// public WebResponseContent CheckServeState() { WebResponseContent webResponse = new WebResponseContent(); var state = scheduler.IsRun(); if (state) { return webResponse.OK("调度已启动!"); } else { return webResponse.Error("调度未启动!"); } } /// /// 获取调度详情 /// /// /// public override PageGridData GetPageData(PageDataOptions options) { var data = base.GetPageData(options); var isRun = scheduler.IsRun(); if (isRun) { var jobs = scheduler.center.GetJobKeys(GroupMatcher.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 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 } } }