Newer
Older
GHFX_REFACTOR / Utility.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using GeoScene.Data;
using GeoScene.Engine;

namespace Cyberpipe
{
    public class Utility
    {

        public static string dbdatabase;
        public static string userID;

//      public static Hashtable Pipe_QueryFields;
        public static string DBPassword;
        public static string DBServer;
        public static string DBBackUp;
        public static string serverip;
        public static int serverport;

        public static GSODataSource dataSource;

        public static ArrayList TerrainsName;
        public static ArrayList LayerNamesList;

        public static Hashtable Query_Fields = new Hashtable();
        public static Hashtable Query_Roads = new Hashtable();
        public static Hashtable Pipe_Code = new Hashtable();
        public static List<PipelineType> listPipelineType;
        public static string PicRootURL;
        public static string PicDefaultURL;
//        public static double dFlyAboveLine = 2;
        public static string userName ="游客";
        public static string projectStr;
        public static string userRole;

        public static Boolean isNeedLogin = false;//设定是否需要登录的开关

        //数据配置
        public static List<databaseClass> listDb;
        public static string backupurl;
        public static string backuppath;
        //上传下载配置
        public static string udserviceurl;
        public static string uploadpath;

        //传感器配置
        public static string sensorConnectString;
        public static string sensorDatabase;
        public static List<MarkerLayer> sensorMarkerLayers;

        //管线类型
        public static string pipelinetype;
        //施工库配置
        public static string sgdbip;
        public static string sgdbname;
        public static string sgdbuser;
        public static string sgdbpwd;
        //规划库配置
        public static string ghdbip;
        public static string ghdbname;
        public static string ghdbuser;
        public static string ghdbpwd;
        public static List<Location> locationList;

        public static string filename = Application.StartupPath + "\\Config.xml";
        public static string filenameSensor = Application.StartupPath + "\\sensorConfig.xml";
        public static string filenamePipelineType = Application.StartupPath + "\\PipelineType.xml";
        public static string filenameDbconfig = Application.StartupPath + "\\databaseConfig.xml";
        public static string filenameLocation = Application.StartupPath + "\\Location.xml";
        public static string layerConfigFile = Application.StartupPath + "\\layerConfig.xml";

        #region wxl 从MainFrm重构来,读取一遍config.xml
        public static List<string> m_PipelineLayerNames = new List<string>();//线图层名称
        public static List<string> workwellLayerNames = new List<string>();//工井图层名称
        public static List<string> valueLayerNames = new List<string>();//阀门图层名称
        public static List<string> instrumenLayerNames = new List<string>();//附属物图层名称
        public static List<string> pipefittingLayerNames = new List<string>();//管件图层名称
        public static List<string> sgPipeLayersNames = new List<string>();//施工管线图层名称

        public static HashSet<string> layerNameSet = new HashSet<string>();//记录有哪些图层要显示
        public static string roadLayerName = "";
        #endregion wxl

        public static LayerConfig LayerConfig;

        public static string LoginImgPath;
        public static string RoleServer;
        public static string TitleImagePath;
        public static string SysnameVersion;
        public static string Copyright;
        public static string LegendImgPath;
        public static string AppId;

        public static void SetBallons(GSOBalloon featureTooltip, GSOBalloonEx balloonEx)
        {
            featureTooltip.CacheFilePath = Application.StartupPath + "/GeoScene/Globe/Temp";

            featureTooltip.SetSize(EnumSizeIndex.ROUNDED_CX, 0);
            featureTooltip.SetSize(EnumSizeIndex.ROUNDED_CY, 0);
            featureTooltip.SetSize(EnumSizeIndex.MARGIN_CX, 3);
            featureTooltip.SetSize(EnumSizeIndex.MARGIN_CY, 3);
            featureTooltip.SetSize(EnumSizeIndex.ANCHOR_HEIGHT, 0);
            featureTooltip.SetSize(EnumSizeIndex.ANCHOR_WIDTH, 0);
            featureTooltip.SetSize(EnumSizeIndex.ANCHOR_MARGIN, 0);
            featureTooltip.SetSize(EnumSizeIndex.ANCHOR_OFFSET_CX, 1);
            featureTooltip.SetSize(EnumSizeIndex.ANCHOR_OFFSET_CY, -1);
            featureTooltip.SetDirection(EnumToolTipDirection.TD_BOTTOMEDGE_LEFT);

            featureTooltip.EscapeSequencesEnabled = true;
            featureTooltip.HyperlinkEnabled = true;
            featureTooltip.Opaque = 30;
            featureTooltip.MaxWidth = 300;
            featureTooltip.SetShadow(0, 0, 50, true, 0, 0);

            balloonEx.SetSize(EnumSizeIndexEx.CONTENT_CX, 500);
            balloonEx.SetSize(EnumSizeIndexEx.CONTENT_CY, 300);
            balloonEx.Opaque = 5;
            balloonEx.SetBorder(Color.White, 1, 1);
            balloonEx.SetColorBkType(EnumBkColorTypeEx.SKY);
        }
        
        public static GSOPoint2d XYZ_2_Latlon(double x, double y)
        {
            int id = GSOProjectManager.AddProject(projectStr);
            //  GeoScene.Data.GSOProjectManager.SetCurProject(id);

            GSOPoint2d pt2D = new GSOPoint2d(x, y);
            GSOPoint2d result = GSOProjectManager.Inverse(pt2D, id);
            return result;
        }

        public static GSOPoint2d Latlon_2_XYZ(double lon, double lat)
        {
            int id = GSOProjectManager.AddProject(projectStr);

            GSOPoint2d pt2D = new GSOPoint2d(lon, lat);
            GSOPoint2d result = GSOProjectManager.Forward(pt2D, id);
            return result;
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="pToEncrypt">要加密字符串</param>
        /// <returns></returns>
        public static string MD5Encrypt(string pToEncrypt)
        {
            string sKey = "12345678";//默认8位加密密匙
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            //ret.ToString();
            return ret.ToString();
        }
        //
        //MD5加密
        //
        public static string MD5Encrypt2(string pToEncrypt)
        {
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);

            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(inputByteArray);
            
            string ret = BitConverter.ToString(output).Replace("-", "");
            ret = ret.ToLower();
            return ret;
        }
        public static string MD5Encrypt3(string pToEncrypt)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bs = Encoding.UTF8.GetBytes(pToEncrypt);
            bs = md5.ComputeHash(bs);
            StringBuilder s = new StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2"));
            }
            return s.ToString();
        }

        //程序启动的时候读取配置文件
        public static void SetParams()
        {
            #region 读取 filename "Config.xml"
            TerrainsName = new ArrayList();
            LayerNamesList = new ArrayList();
            if (File.Exists(filename))
            {
                XmlTextReader xmlReader = new XmlTextReader(filename);
                try
                {
                    while (xmlReader.Read())
                    {
                        if (xmlReader.Name == "database")
                        {
                            dbdatabase = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "dbpassword")
                        {
                            DBPassword = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "user")
                        {
                            userID = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "locaserverip")
                        {
                            serverip = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "locaserverport")
                        {
                            serverport = Convert.ToInt32(xmlReader.ReadElementString());
                        }
                        //else if (XmlReader.Name == "localicenseserverip")
                        //{
                        //    Utility.localicenseserverip = XmlReader.ReadElementString();
                        //}
                        //else if (XmlReader.Name == "localicenseserverport")
                        //{
                        //    Utility.localicenseserverport = Convert.ToInt32(XmlReader.ReadElementString());
                        //}
                        else if (xmlReader.Name == "dbbackuppath")
                        {
                            DBBackUp = xmlReader.ReadElementString();
                        }

                        else if (xmlReader.Name == "Terrain")
                        {
                            TerrainsName.Add(xmlReader.ReadElementString());
                        }
                        else if (xmlReader.Name == "dbserver")
                        {
                            DBServer = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "rooturl")
                        {
                            PicRootURL = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "defaultimgurl")
                        {
                            PicDefaultURL = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "queryfield")
                        {
                            Query_Fields.Add(xmlReader["label"], xmlReader.ReadElementString());
                        }
                        else if (xmlReader.Name == "location")
                        {
                            Query_Roads.Add(xmlReader["label"], xmlReader.ReadElementString());
                        }
                        else if (xmlReader.Name == "pipelineCode")
                        {
                            Pipe_Code.Add(xmlReader["label"], xmlReader.ReadElementString());
                            
                        }
                        else if (xmlReader.Name == "projectStr")
                        {
                            projectStr = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "backupserverurl")
                        {
                            backupurl = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "backuppath")
                        {
                            backuppath = xmlReader.ReadElementString();
                        }
                        //else if (XmlReader.Name == "uploadpath")
                        //{
                        //    Utility.uploadpath = XmlReader.ReadElementString();
                        //}
                        //else if (XmlReader.Name == "udserviceurl")
                        //{
                        //    Utility.udserviceurl = XmlReader.ReadElementString();
                        //}
                        else if (xmlReader.Name == "pipelinetype")
                        {
                            pipelinetype = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "shdbip")
                        {
                            sgdbip = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "shdbname")
                        {
                            sgdbname = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "shdbuser")
                        {
                            sgdbuser = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "shdbpwd")
                        {
                            sgdbpwd = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "ghdbip")
                        {
                            ghdbip = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "ghdbname")
                        {
                            ghdbname = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "ghdbuser")
                        {
                            ghdbuser = xmlReader.ReadElementString();
                        }
                        else if (xmlReader.Name == "ghdbpwd")
                        {
                            ghdbpwd = xmlReader.ReadElementString();
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogError.PublishError(ex);
                }
                finally
                {
                    xmlReader.Close();
                }
            }
            else
            {
                MessageBox.Show("配置文件" + filename + "不存在!", "提示");
            }
            #endregion

            #region wxl 从layerconfig.xml中读取

            LayerConfig = XmlTools.DeserializeFromXml<LayerConfig>(layerConfigFile);
            List<DataSrc> dataSrcs = LayerConfig.datasrcs;
            
            foreach (var dataSrc in dataSrcs)
            {
               List<LayerType>  layerTypes = dataSrc.layertypes;
                foreach (var layerType in layerTypes)
                {
                    List<Layer> layers = layerType.layers;
                    foreach (var layer in layers)
                    {
//                        if(!dictionaryNetLayerNameAndCaption.ContainsKey(layer.layer))
//                             dictionaryNetLayerNameAndCaption.Add(layer.layer, layer.label);
                        if (!layerNameSet.Contains(layer.layer))
                            layerNameSet.Add(layer.layer);
                        if (layer.layer.EndsWith("管线"))
                        {
                            LayerNamesList.Add(layer.layer.Substring(0, layer.layer.Length - 2));
                        }
                        if (layer.flag == null||layer.flag.Trim().Equals("")) continue;
                        if (!layer.layersrc.Equals("db")&&layer.flag.Equals("road"))
                        {
                            roadLayerName = layer.layer;
                        }
                        else if (layer.flag.Equals("pipeLine"))
                        {
                            m_PipelineLayerNames.Add(layer.label);
                        }
                        else if (layer.flag.Equals("access"))
                        {
                            instrumenLayerNames.Add(layer.label);
                        }
                        else if (layer.flag.Equals("character"))
                        {
                            pipefittingLayerNames.Add(layer.label);
                        }
                        else if (layer.flag.Equals("sgData"))
                        {
                            sgPipeLayersNames.Add(layer.label);
                        }
                        //配置文件中不存在workwell和valve的,不起作用
//                        else if (layer.flag.Equals("workwell"))
//                        {
//                            workwellLayerNames.Add(layer.label);
//                        }
//                        else if (layer.flag.Equals("valve"))
//                        {
//                            valueLayerNames.Add(layer.label);
//                        }
                       
                    }
                }
            }
            #endregion

            #region 读取 filenameSensor "sensorConfig.xml"
            sensorMarkerLayers = new List<MarkerLayer>();
            if (File.Exists(filenameSensor))
            {
                XmlTextReader XmlReader = new XmlTextReader(filenameSensor);
                try
                {
                    while (XmlReader.Read())
                    {
                        if (XmlReader.Name == "ConnectString")
                        {
                            string dbserver = XmlReader["dbserver"].Trim();
                            string dbserverport = XmlReader["dbserverport"].Trim();
                            string database = XmlReader["database"].Trim();
                            string username = XmlReader["username"].Trim();
                            string password = XmlReader["password"].Trim();
                            sensorDatabase = database;
                            sensorConnectString = "Server=" + dbserver + ";Port=" + dbserverport + ";Database=" + database + ";Uid=" + username + ";Pwd=" + password + "";
                        }
                        else if (XmlReader.Name == "MarkerLayer")
                        {
                            MarkerLayer markerLayer = new MarkerLayer();
                            string layerName = XmlReader["layerName"].Trim();
                            string tableName = XmlReader["tableName"].Trim();
                            markerLayer.layerName = layerName;
                            markerLayer.tableName = tableName;
                            Dictionary<string, string> fields = new Dictionary<string, string>();
                            XmlReader.ReadSubtree();
                            while (XmlReader.Read())
                            {
                                if (XmlReader.Name == "field")
                                {
                                    string name = XmlReader["name"].Trim();
                                    string label = XmlReader["label"].Trim();
                                    fields.Add(name, label);
                                }
                                else if (XmlReader.Name == "MarkerLayer")
                                {
                                    break;
                                }
                            }
                            markerLayer.fields = fields;
                            sensorMarkerLayers.Add(markerLayer);
                        }                       
                    }
                }
                catch (Exception ex)
                {
                    //LogError.PublishError(ex);
                }
                finally
                {
                    XmlReader.Close();
                }
            }
            else
            {
                MessageBox.Show("配置文件" + filenameSensor + "不存在!", "提示");
            }
            #endregion

            #region 读取 filenamePipelineType "PipelineType.xml"
            listPipelineType = new List<PipelineType>();
            if (File.Exists(filenamePipelineType))
            {
                XmlTextReader XmlReader = new XmlTextReader(filenamePipelineType);
                try
                {
                    while (XmlReader.Read())
                    {
                        if (XmlReader.Name == "PipelineCode")
                        {
                            string pipelinecode = XmlReader["code"] == null ? null : XmlReader["code"].Trim();
                            string pipelinename = XmlReader["name"] == null ? null : XmlReader["name"].Trim();
                            string pipelinetype = XmlReader["type"] == null ? null : XmlReader["type"].Trim();
                            if (pipelinecode != null && pipelinename != null && pipelinetype != null)
                            {
                                PipelineType pipelineType = new PipelineType(pipelinecode, pipelinename, pipelinetype);
                                if (listPipelineType.Contains(pipelineType) == false)
                                {
                                    listPipelineType.Add(pipelineType);
                                }
                            }
                        }                       
                    }
                }
                catch (Exception ex)
                {
                    //LogError.PublishError(ex);
                }
                finally
                {
                    XmlReader.Close();
                }
            }
            else
            {
                MessageBox.Show("配置文件" + filenamePipelineType + "不存在!", "提示");
            }
            #endregion

            #region 读取 database "databaseConfig.xml"
            listDb = new List<databaseClass>();
            if (File.Exists(filenameDbconfig))
            { 
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(filenameDbconfig);

                    XmlNode xn = doc.SelectSingleNode("Database");
                    // 得到根节点的所有子节点
                    XmlNodeList xnl = xn.ChildNodes;
                    foreach (XmlNode xn1 in xnl)
                    {
                        databaseClass model = new databaseClass();
                        XmlElement xe = (XmlElement)xn1;// 将节点转换为元素,便于得到节点的属性值
                        model.title = xe.GetAttribute("title");
                        // 得到database节点的所有子节点
                        XmlNodeList xnl0 = xe.ChildNodes;
                        model.dbip = xnl0.Item(0).InnerText;
                        model.database = xnl0.Item(1).InnerText;
                        model.dbuser = xnl0.Item(2).InnerText;
                        model.dbpassword = xnl0.Item(3).InnerText;

                        listDb.Add(model);
                    }
                }
                catch (Exception ex)
                {
                    //LogError.PublishError(ex);
                }
                //finally
                //{
                //    doc.Close();
                //}
            }
            else
            {
                MessageBox.Show("配置文件" + filenameDbconfig + "不存在!", "提示");
            }
            #endregion

            #region
            XmlTextReader XmlReaderLocation = null;
            try
            {
                if (File.Exists(filenameLocation))
                {
                    locationList = new List<Location>();
                    locationList.Add(new Location("", "", "", ""));
                    XmlReaderLocation = new XmlTextReader(filenameLocation);
                    while (XmlReaderLocation.Read())
                    {
                        if (XmlReaderLocation.Name == "location")
                        {
                            string name = XmlReaderLocation["name"] == null ? null : XmlReaderLocation["name"].Trim();
                            string x = XmlReaderLocation["x"] == null ? null : XmlReaderLocation["x"].Trim();
                            string y = XmlReaderLocation["y"] == null ? null : XmlReaderLocation["y"].Trim();
                            string z = XmlReaderLocation["z"] == null ? null : XmlReaderLocation["z"].Trim();

                            if (name != null)
                            {
                                locationList.Add(new Location(name, x, y, z));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //LogError.PublishError(ex);
            }
            finally
            {
                if (null != XmlReaderLocation)
                {
                    XmlReaderLocation.Close();
                }
            }


            #endregion
        }

        /// <summary>
        /// 获取Config.xml配置文件中的roleurl、titleimgpath、sysname、copyright配置
        /// </summary>
        /// <returns></returns>
        public static void SysInfoConfig()
        {
            if (!File.Exists(filename)) return;

            XmlTextReader xmlReader = new XmlTextReader(filename);
            try
            {
                while (xmlReader.Read())
                {
                    if (xmlReader.Name.Equals("roleurl"))
                    {
                        RoleServer = xmlReader.ReadElementString();
                    }
                    else if (xmlReader.Name.Equals("titleimgpath"))
                    {
                        TitleImagePath = xmlReader.ReadElementString();
                    }
                    else if (xmlReader.Name.Equals("sysname"))
                    {
                        SysnameVersion = xmlReader.ReadElementString();
                    }
                    else if (xmlReader.Name.Equals("copyright"))
                    {
                        Copyright = xmlReader.ReadElementString();
                    }
                    else if (xmlReader.Name.Equals("legendimg"))
                    {
                        LegendImgPath = xmlReader.ReadElementString();
                    }
                    else if (xmlReader.Name.Equals("appid"))
                    {
                        AppId = xmlReader.ReadElementString();
                    }
                    else if (xmlReader.Name.Equals("loginimgpath"))
                    {
                        LoginImgPath = xmlReader.ReadElementString();
                    }
                }

            }
            catch (Exception ex)
            {
           
                LogHelper.Error("获取系统配置信息出错!",ex);
            }
            finally
            {
                xmlReader.Close();
            }
        }
     
        
    }
}