分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-07-19 7a4c218909936721fe281737491d10efc7378e09
´úÂë¹ÜÀí/PCS/WCS_Server/WIDESEA_WCS/Services/WCS/Partial/VV_DispatchService.cs
@@ -1,11 +1,15 @@
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;
@@ -59,24 +63,29 @@
            {
                if (!scheduler.IsRun())
                {
                    WIDESEA.Helper.GetToken();
                    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);
                        //WriteLog.Write_Log("服务", "服务", "启动服务\n[调度异常]" + startRes.Message);
                        WriteDBLog.Error($"开启服务", new { æ•°æ® = startRes.Message }, "PCS", UserContext.Current.UserName);
                        return startRes;
                    }
                }
@@ -87,7 +96,8 @@
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("服务", "服务", "启动服务\n[异常]" + ex.Message);
                //WriteLog.Write_Log("服务", "服务", "启动服务\n[异常]" + ex.Message);
                WriteDBLog.Error($"开启服务", new { æ•°æ® = ex.Message }, "PCS", UserContext.Current.UserTrueName);
                return webResponse.Error($"异常,{ex.Message}");
            }
        }
@@ -125,7 +135,8 @@
            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)
            {
                throw new Exception("未配置任何PLC连接对象!");
                return content.OK();
                //throw new Exception("未配置任何PLC连接对象!");
            }
            var detialList = freeDB.Select<dt_plcinfodetail>().ToList();
@@ -140,7 +151,7 @@
                    PLCClient client = null;
                    if (plc.plcinfo_type == "Simene")
                    {
                        client = new SiemensPLCClient()
                        client = new SiemensPLCClient(plc.plcinfo_model)
                        {
                            PLCName = plc.plcinfo_name,
                            Ip = plc.plcinfo_ip,
@@ -227,6 +238,9 @@
                {
                    order.dbAddress = item.plcdetail_db + "." + item.plcdetail_value;
                }
                order.opratortype = item.plcdetail_opratortype;
                order.dataLen = item.plcdetail_len;
                order.Methods = item.plcdetail_number;//
                itmes.Add(order);
            }
            return itmes;
@@ -252,10 +266,13 @@
                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("服务关闭成功!");
        }
@@ -268,28 +285,40 @@
        public WebResponseContent StartJob(string jobName)
        {
            WebResponseContent webResponse = new WebResponseContent();
            if (!scheduler.IsRun())
            try
            {
                return webResponse.Error("请先启动服务!");
            }
            var jobData = freeDB.Select<VV_Dispatch>().Where(t => t.JobName == jobName).First();
            if (jobData == null)
            {
                webResponse.Error($"调度{jobName}不存在!");
            }
            else
            {
                var job = scheduler.GetJobByJobName(jobName);
                if (job != null)
                if (!scheduler.IsRun())
                {
                    webResponse.Error($"调度{jobName}已是执行状态!");
                    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
                {
                    scheduler.AddJob(jobData);
                    webResponse.OK("调度添加成功");
                    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;
        }
@@ -302,25 +331,37 @@
        public WebResponseContent CloseJob(string jobName)
        {
            WebResponseContent webResponse = new WebResponseContent();
            if (!scheduler.IsRun())
            try
            {
                return webResponse.Error("请先启动服务!");
            }
                if (!scheduler.IsRun())
                {
                    throw new Exception("请先启动服务!");
                    //return webResponse.Error("请先启动服务!");
                }
            var job = scheduler.GetJobByJobName(jobName);
            if (job == null)
            {
                return webResponse.Error($"任务{jobName},不存在!");
                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}关闭失败!");
                }
            }
            var res = scheduler.RemoveJob(job);
            if (res)
            catch (Exception ex)
            {
                webResponse.OK($"调度{jobName}关闭成功!");
                WriteDBLog.Error($"暂停{jobName}调度", new { æ•°æ® = ex.Message }, "PCS", UserContext.Current.UserTrueName);
                return webResponse.Error(ex.Message);
            }
            else
            {
                webResponse.Error($"调度{jobName}关闭失败!");
            }
            WriteDBLog.Success($"暂停{jobName}调度", new { æ•°æ® = jobName + "暂停成功!" }, "PCS", UserContext.Current.UserTrueName);
            return webResponse;
        }
@@ -365,5 +406,69 @@
            }
            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
        }
    }
}