using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Entity; using System.Drawing; using System.Linq; using System.Net.Sockets; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Documents; using System.Windows.Forms; using XdCxRhDW.Repostory; using System.IO; using System.Data.Entity.Migrations; using ExtensionsDev; using DevExpress.Utils; using DevExpress.XtraMap; using XdCxRhDW.WebApi; using DevExpress.XtraPrinting; using XdCxRhDW.Entity; using DevExpress.XtraEditors.Controls; using System.Security.Policy; using DevExpress.Utils.About; using XdCxRhDW.Dto; namespace XdCxRhDW.App.UserControl { public partial class CtrlSysSettings : DevExpress.XtraEditors.XtraUserControl { public CtrlSysSettings() { InitializeComponent(); //this.layoutControl1.UseDefault(); } private async void CtrlSysSettings_Load(object sender, EventArgs e) { try { txtWmtsSource.Properties.Items.AddEnum(); txtLayer.Items.AddEnum(); txtWmtsSource.SelectedIndex = 0; txtLayer.SelectedIndex = 0; using (RHDWContext db = new RHDWContext()) { var res = await db.SysSetings.FirstOrDefaultAsync(); if (res != null) { this.txtHttpPort.Text = res.HttpPort.ToString(); if (!string.IsNullOrEmpty(res.XLDirectory)) this.txtXLDirectory.Text = res.XLDirectory.ToString(); if (res.YDPZThreshold > 0) this.txtYDPZThreshold.Text = res.YDPZThreshold.ToString(); if (this.txtMapType.SelectedIndex == res.MapType) txtMapType_SelectedIndexChanged(null, null); else this.txtMapType.SelectedIndex = res.MapType; if (txtMapType.SelectedIndex != 0) { this.txtWmtsSource.EditValue = res.WmtsSource; for (int i = 0; i < txtLayer.Items.Count; i++) { if (res.MapLayerType.HasFlag(EnumMapLayerType.SatMap)) { this.txtLayer.Items[EnumMapLayerType.SatMap].CheckState = CheckState.Checked; } if (res.MapLayerType.HasFlag(EnumMapLayerType.RoadMap)) { this.txtLayer.Items[EnumMapLayerType.RoadMap].CheckState = CheckState.Checked; } if (res.MapLayerType.HasFlag(EnumMapLayerType.XZQH_Map)) { this.txtLayer.Items[EnumMapLayerType.XZQH_Map].CheckState = CheckState.Checked; } } } } } } catch (Exception ex) { Serilog.Log.Error(ex, "加载配置信息异常"); DxHelper.MsgBoxHelper.ShowError("加载配置信息异常"); } layoutControl1.BestFit(); } private async void btnSave_Click(object sender, EventArgs e) { bool needStartHttpSvr = false; try { if (!int.TryParse(txtHttpPort.Text, out int port)) { DxHelper.MsgBoxHelper.ShowError("Http端口错误!"); return; } //if (txtMapType.SelectedIndex != 0 && txtWmtsSource.EditValue == null) //{ // DxHelper.MsgBoxHelper.ShowError("请选择!"); // return; //} if (txtMapType.SelectedIndex != 0 && txtLayer.CheckedItemsCount<=0) { DxHelper.MsgBoxHelper.ShowError("请选择图层类型!"); return; } using (RHDWContext db = new RHDWContext()) { var res = await db.SysSetings.FirstOrDefaultAsync(); if (res == null) res = new SysSetings(); res.HttpPort = port; res.XLDirectory = txtXLDirectory.Text; res.YDPZThreshold = string.IsNullOrWhiteSpace(txtYDPZThreshold.Text) ? 0 : Convert.ToInt32(txtYDPZThreshold.Text); res.MapType = txtMapType.SelectedIndex; if (txtMapType.SelectedIndex != 0) { res.WmtsSource = (EnumWmtsSource)txtWmtsSource.EditValue; EnumMapLayerType layerType = EnumMapLayerType.SatMap; for (int i = 0; i < txtLayer.Items.Count; i++) { if (txtLayer.GetItemChecked(i)) { layerType |= (EnumMapLayerType)txtLayer.Items[i].Value; } } layerType = layerType & ~EnumMapLayerType.SatMap;//移除 res.MapLayerType = layerType; } db.SysSetings.AddOrUpdate(res); needStartHttpSvr = true; await db.SaveChangesAsync(); DxHelper.MsgBoxHelper.ShowInfo("配置信息保存成功!"); Messenger.Defalut.Pub("系统配置改变", await db.SysSetings.FirstOrDefaultAsync()); } } catch (Exception ex) { Serilog.Log.Error(ex, "配置信息保存异常"); DxHelper.MsgBoxHelper.ShowError("配置信息保存异常"); } if (needStartHttpSvr) { try { using (RHDWContext db = new RHDWContext()) { var settings = await db.SysSetings.FirstOrDefaultAsync(); Startup.Start(settings.HttpPort, "多模式融合定位平台.Xml", "XdCxRhDW.Dto.xml"); } } catch (System.Reflection.TargetInvocationException ex) { Serilog.Log.Error(ex, $"启动Http服务失败!"); if (ex.InnerException is HttpListenerException) { DxHelper.MsgBoxHelper.ShowWarning($"{ex.InnerException.Message}"); } else { DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!"); } } catch (Exception ex) { Serilog.Log.Error(ex, $"启动Http服务失败!"); DxHelper.MsgBoxHelper.ShowWarning($"启动Http服务失败!"); } } } private void txtMapType_SelectedIndexChanged(object sender, EventArgs e) { if (txtMapType.SelectedIndex == 0) { itemWmtsSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never; itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never; } else { itemWmtsSource.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always; itemMapLayer.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always; } layoutControl1.BestFit(); } } }