管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2023-10-23 2fd93008d2ce0052353e5016f2c15d1890ffd2e6
ExportMap/cs/Tools.cs
@@ -6,14 +6,18 @@
using System.Configuration;
using System.Data.Common;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Web;
using System.Web.WebSockets;
namespace ExportMap.cs
{
@@ -24,8 +28,22 @@
        /// </summary>
        public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// 获取令牌
        /// </summary>
        public static string Token
        {
            get
            {
                return "c36e4f94-dfde-401e-9967-2c4a449f1300";
            }
        }
        private static string tempDir;
        /// <summary>
        /// 临时目录
        /// </summary>
        public static string TempDir
        {
            get
@@ -33,7 +51,10 @@
                if (string.IsNullOrWhiteSpace(tempDir))
                {
                    tempDir = Path.Combine(BaseDir, "temp");
                    if (!Directory.Exists(tempDir)) Directory.CreateDirectory(tempDir);
                    if (!Directory.Exists(tempDir))
                    {
                        Directory.CreateDirectory(tempDir);
                    }
                }
                return tempDir;
@@ -51,7 +72,7 @@
            {
                if (null == _dbHelper)
                {
                    _dbHelper = new PostgreHelper(DbEnum.langfang);
                    _dbHelper = new PostgreHelper();
                }
                return _dbHelper;
@@ -115,7 +136,7 @@
        }
        /// <summary>
        /// 创建目录
        /// 创建目录 *
        /// </summary>
        public static void CreateDirectory(string dir)
        {
@@ -129,7 +150,7 @@
        }
        /// <summary>
        /// 克隆对象
        /// 克隆对象 *
        /// </summary>
        public static T Clone<T>(T source) where T : new()
        {
@@ -157,9 +178,12 @@
        /// <summary>
        /// 执行CMD
        /// </summary>
        /// <param name="task">任务</param>
        /// <param name="cmd">命令行</param>
        /// <param name="isPy">是否为QGIS Py脚本</param>
        /// <param name="isOut">是否输出错误</param>
        /// <returns>执行结果或出错信息</returns>
        public static String ExecCmd(string cmd, bool isPy = false)
        public static string ExecCmd(SysTask task, string cmd, bool isPy = false, bool isOut = false)
        {
            List<string> list = new List<string>();
            if (isPy)
@@ -171,7 +195,7 @@
            }
            list.Add(cmd);
            string str = ExecCmd(list);
            string str = ExecCmd(task, list, isOut);
            return str;
        }
@@ -179,11 +203,13 @@
        /// <summary>
        /// 执行CMD
        /// </summary>
        /// <param name="task">任务</param>
        /// <param name="list">命令集合</param>
        /// <param name="isOut">是否输出错误</param>
        /// <returns>执行结果或出错信息</returns>
        public static string ExecCmd(List<string> list)
        public static string ExecCmd(SysTask task, List<string> list, bool isOut = false)
        {
            string str = null;
            string str;
            try
            {
                Process p = new Process();
@@ -192,12 +218,15 @@
                p.StartInfo.CreateNoWindow = true;
                //p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardOutput = isOut;
                p.StartInfo.RedirectStandardError = true;
                p.Start();
                task.pid = p.Id;
                task.id = TaskDBHelper.Insert(task);
                StreamWriter si = p.StandardInput; // 标准输入流 
                //StreamReader so = p.StandardOutput; // 标准输出流
                StreamReader so = isOut ? p.StandardOutput : null; // 标准输出流
                StreamReader se = p.StandardError; // 标准错误流
                LogOut.Info("cmd = " + string.Join(",", list));
@@ -208,15 +237,22 @@
                }
                si.WriteLine("exit");
                //string info = so.ReadToEnd();
                string info = null == so ? null : so.ReadToEnd();
                str = se.ReadToEnd();
                //if (!string.IsNullOrEmpty(info)) LogOut.Debug(info);
                if (!string.IsNullOrEmpty(str)) LogOut.Error(str);
                if (!string.IsNullOrEmpty(str) && !str.Contains("@jit(cache=True, nogil=True)")) LogOut.Error(str);
                if (p.HasExited == false) p.Kill();
                task = TaskDBHelper.SelectById(task.id);
                if (null != task && task.status < 2)
                {
                    task.status = 2;
                    TaskDBHelper.Update(task);
                }
                if (null != so) so.Close();
                se.Close();
                //so.Close();
                si.Close();
                p.Close();
            }
@@ -224,8 +260,165 @@
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                str = ex.Message;
                task = TaskDBHelper.SelectById(task.id);
                if (null != task && task.status < 2)
                {
                    task.err = ex.Message;
                    task.status = 4;
                    TaskDBHelper.Update(task);
                }
            }
            return str;
        }
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="list">命令集合</param>
        public static string ExecCmd(List<string> list)
        {
            string str = null;
            Process p = null;
            try
            {
                p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.Start();
                StreamWriter si = p.StandardInput;
                StreamReader se = p.StandardError;
                LogOut.Info("cmd = " + string.Join(",", list));
                si.AutoFlush = true;
                foreach (string cmd in list)
                {
                    si.WriteLine(cmd);
                }
                si.WriteLine("exit");
                str = se.ReadToEnd();
                se.Close();
                si.Close();
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                str = ex.Message;
            }
            finally
            {
                if (p != null)
                {
                    p.Close();
                }
            }
            return str;
        }
        /// <summary>
        /// 执行命令
        /// </summary>
        public static string ExecCmd(List<string> list, ref string rs)
        {
            string str = null;
            Process p = null;
            try
            {
                p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.Start();
                StreamWriter si = p.StandardInput;
                StreamReader so = p.StandardOutput;
                StreamReader se = p.StandardError;
                LogOut.Info("cmd = " + string.Join(",", list));
                si.AutoFlush = true;
                foreach (string cmd in list)
                {
                    si.WriteLine(cmd);
                }
                si.WriteLine("exit");
                rs = so.ReadToEnd();
                str = se.ReadToEnd();
                se.Close();
                so.Close();
                si.Close();
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                str = ex.Message;
            }
            finally
            {
                if (p != null)
                {
                    p.Close();
                }
            }
            return str;
        }
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="list">命令集合</param>
        public static string ExecCmdForWin(List<string> list, bool noWin = false)
        {
            string str = null;
            Process p = null;
            try
            {
                p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = noWin;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.Start();
                StreamWriter si = p.StandardInput;
                StreamReader se = p.StandardError;
                LogOut.Info("cmd = " + string.Join(",", list));
                si.AutoFlush = true;
                foreach (string cmd in list)
                {
                    si.WriteLine(cmd);
                }
                si.WriteLine("exit");
                str = se.ReadToEnd();
                se.Close();
                si.Close();
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                str = ex.Message;
            }
            finally
            {
                if (p != null)
                {
                    p.Close();
                }
            }
            return str;
        }
@@ -251,5 +444,79 @@
            return sys;
        }
        /// <summary>
        /// 删除路径
        /// </summary>
        public static string DelPath(string path)
        {
            List<string> list = new List<string>();
            list.Add(string.Format("rd \"{0}\" /s /q", path));
            return ExecCmd(list);
        }
        /// <summary>
        /// 获取EPSG代码
        /// </summary>
        public static string GetEPSG(string file)
        {
            string gdalPath = GetSetting("gdalPath");
            string cmd = string.Format("{0}\\gdalsrsinfo.exe \"{1}\" -o epsg", gdalPath, file);
            string rs = null;
            string ostr = ExecCmd(new List<string> { cmd }, ref rs);
            if (string.IsNullOrEmpty(rs)) return null;
            string[] strs = rs.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string str in strs)
            {
                if (str.Contains("EPSG:"))
                {
                    return str;
                }
            }
            return null;
        }
        /// <summary>
        /// 设置单体模型参数
        /// </summary>
        public static void SetIsModel(XYZArgs args, List<SysMeta> list)
        {
            if (null == args.models || args.models.Count != args.ids.Count)
            {
                foreach (SysMeta meta in list) meta.ismeta = 1;
                return;
            }
            foreach (SysMeta meta in list)
            {
                int idx = args.ids.IndexOf(meta.id);
                meta.ismeta = idx == -1 ? 1 : args.models[idx];
            }
        }
        /// <summary>
        /// 获取本机IP
        /// </summary>
        /// <returns></returns>
        public static string GetLocalIP()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
            return GetSetting("localIP");
        }
        [DllImport("ReadLas.dll")]
        public extern static int get_las_cs(string file_name);
    }
}