using AutoMapper; using Microsoft.IdentityModel.Logging; using SqlSugar; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net.NetworkInformation; using System.Net.WebSockets; using System.Net; using System.Text; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Unicode; using System.Threading.Tasks; using Txgy.RBS.DbModel.Models; using Txgy.RBS.DTO; using Txgy.RBS.Framework.Api; using Txgy.RBS.IServices; using static System.Net.Mime.MediaTypeNames; using Txgy.RBS.Framework; namespace Txgy.RBS.Services { public class GlobalConfigService : BaseService, IGlobalConfigService { private readonly IMapper _iMapper; public GlobalConfigService(ISqlSugarClient client, IMapper iMapper) : base(client) { this._iMapper = iMapper; } public ApiResult AddGlobalConfig(GlobalConfigDTO globalConfigDTO) { int res = 0; global_config global_Config = _iMapper.Map(globalConfigDTO); res = _Client.Insertable(global_Config).ExecuteCommand(); if (res > 0) { return new ApiResult(); } return new ApiResult() { Message = "添加失败" }; } public List GetAllGlobalConfig() { var global_Config = _Client.Queryable().ToList(); return _iMapper.Map>(global_Config); } public GlobalConfigDTO GetGlobalConfig(int id) { return new GlobalConfigDTO(); } public ApiResult UpdateGlobalConfig(GlobalConfigDTO GlobalConfigDTO) { global_config global_Config = _iMapper.Map (GlobalConfigDTO); int res= _Client.Updateable(global_Config).ExecuteCommand(); if(res > 0) { return new ApiResult(); } return new ApiResult() { Message = "更新失败" }; } #region 方法 private bool CMDStartProcess(ProcessInfo proInfo) { //* Create your Process Process process = new Process(); process.Exited += Process_Exited; process.EnableRaisingEvents = true; process.StartInfo.FileName = Path.GetFullPath(proInfo.ProPath + proInfo.ProName + ".exe"); process.StartInfo.WorkingDirectory = Path.GetFullPath(proInfo.ProPath); process.StartInfo.Arguments = proInfo.ProParams; Debug.WriteLine($"*******name:{process.StartInfo.FileName}, arguments:{process.StartInfo.Arguments}*********"); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; //* Set your output and error (asynchronous) handlers { process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler); process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler); } //* Start process and handlers bool res = process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); // process.WaitForExit(); return res; } private void Process_Exited(object? sender, EventArgs e) { } void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) { Debug.WriteLine("output*************:{0},{1}", sendingProcess.ToString(), outLine.Data); if (string.IsNullOrEmpty(outLine.Data)) return; if (outLine.Data.Contains("ProcessSlice:")) { var pro = sendingProcess as Process; if (pro != null) { } } else if (outLine.Data.Contains("ML ")) { var pro = sendingProcess as Process; if (pro != null) { } } } public int StartProcess(ProcessInfo proInfo) { bool res = CMDStartProcess(proInfo); int seInd = res ? 0 : 37; return seInd; } /// /// 结束进程 /// /// /// 0=成功;1=未找到进程;-1=失败 private int KillProcess(ProcessInfo processInfo) { int ri = 0; if (processInfo != null) { if (processInfo.ProName != null) { try { Process[] localByName = Process.GetProcessesByName(processInfo.ProName); if (localByName.Length == 0) { ri = 1; return ri; } foreach (var item in localByName) { Console.WriteLine(item.Id); item.Kill(); } return ri; } catch (Exception) { return -1; } } else { return 0; } } else { return 0; } } /// /// 查找进程 /// /// /// 0=正在运行;1=未运行;-1=系统错误 private int FindProcess(ProcessInfo processInfo) { int ri = 0; try { Process[] localByName = Process.GetProcessesByName(processInfo.ProName); if (localByName.Length == 0) { ri = 1; return ri; } return ri; } catch (Exception) { return -1; } } public string ProcessStatus { get; set; } public void GetState(List< ProcessInfo> processes) { foreach (var item in processes) { var pro = item; if (pro != null ) { int sfp = FindProcess(pro); if (sfp == 0) { ProcessStatus = "正常运行"; } else { ProcessStatus = "重启中"; StartProcess(pro); Thread.Sleep(20); } } } } #endregion } }