using System; using System.Collections.Generic; using System.IO; using Aitex.Core.RT.Log; using ExcelLibrary.SpreadSheet; using MECF.Framework.Common.OperationCenter; using OpenSEMI.ClientBase; using OpenSEMI.ClientBase.Command; namespace VirgoUI.Client.Models.Utility.SystemConfig { public class SystemConfigViewModel : BaseModel { private int MenuPermission; #region Properties private List _ConfigNodes = new List(); public List ConfigNodes { get { return _ConfigNodes; } set { _ConfigNodes = value; NotifyOfPropertyChange("ConfigNodes"); } } private List _configItems = null; public List ConfigItems { get { return _configItems; } set { _configItems = value; NotifyOfPropertyChange("ConfigItems"); } } string _CurrentNodeName = string.Empty; public string CurrentNodeName { get { return _CurrentNodeName; } set { _CurrentNodeName = value; NotifyOfPropertyChange("CurrentNodeName"); } } public BaseCommand TreeViewSelectedItemChangedCmd { private set; get; } #endregion #region Functions public SystemConfigViewModel() { this.DisplayName = "System Config"; TreeViewSelectedItemChangedCmd = new BaseCommand(TreeViewSelectedItemChanged); } protected override void OnInitialize() { MenuPermission = ClientApp.Instance.GetPermission("Config"); base.OnInitialize(); ConfigNodes = SystemConfigProvider.Instance.GetConfigTree().SubNodes; } private void TreeViewSelectedItemChanged(ConfigNode node) { CurrentNodeName = string.IsNullOrEmpty(node.Path) ? node.Name : $"{node.Path}.{node.Name}"; ConfigItems = node.Items; GetDataOfConfigItems(); } private void GetDataOfConfigItems() { if (ConfigItems == null) return; for (int i = 0; i < ConfigItems.Count; i++) { string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", ConfigItems[i].Name); ConfigItems[i].CurrentValue = SystemConfigProvider.Instance.GetValueByName(key); if (ConfigItems[i].Type == DataType.Bool) { bool value; if (bool.TryParse(ConfigItems[i].CurrentValue, out value)) ConfigItems[i].BoolValue = value; } else ConfigItems[i].StringValue = ConfigItems[i].CurrentValue; } } public void SetValue(ConfigItem item) { if (MenuPermission != 3) return; //key :System.IsSimulatorMode //value: true or false 都是字符串 //input check string value; if (item.Type == DataType.Bool) { value = item.BoolValue.ToString().ToLower(); } else { if (item.TextSaved) return; if (item.Type == DataType.Int) { int iValue; if (int.TryParse(item.StringValue, out iValue)) { if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min)) { if (iValue > item.Max || iValue < item.Min) { DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", ((int)item.Min).ToString(), ((int)item.Max).ToString())); return; } } } else { DialogBox.ShowWarning("Please input valid data."); return; } value = item.StringValue; } else if (item.Type == DataType.Double) { double fValue; if (double.TryParse(item.StringValue, out fValue)) { if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min)) { if (fValue > item.Max || fValue < item.Min) { DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", item.Min.ToString(), item.Max.ToString())); return; } } } else { DialogBox.ShowWarning("Please input valid data."); return; } value = item.StringValue; } else value = item.StringValue; } string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", item.Name); InvokeClient.Instance.Service.DoOperation("System.SetConfig", key, value); item.TextSaved = true; Reload(); } public void Reload() { if (MenuPermission != 3) return; GetDataOfConfigItems(); } public void SaveAll() { if (MenuPermission != 3) return; if (ConfigItems == null) return; ConfigItems.ForEach(item => SetValue(item)); } public void ExportData() { if (MenuPermission != 3) return; try { if (ConfigNodes == null || ConfigNodes.Count == 0) return; Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.DefaultExt = ".xls"; // Default file extension dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension dlg.FileName = string.Format("ConfigList{0}", DateTime.Now.ToString("yyyyMMdd")); Nullable result = dlg.ShowDialog();// Show open file dialog box if (result != true) // Process open file dialog box results return; if (File.Exists(dlg.FileName)) { File.Delete(dlg.FileName); } Workbook workbook = new Workbook(); Worksheet worksheet = new Worksheet(DateTime.Now.ToString("yyyyMMdd")); int colCount = 1; worksheet.Cells[0, 1] = new Cell("NameView"); worksheet.Cells[0, 2] = new Cell("Unit"); worksheet.Cells[0, 3] = new Cell("Min"); worksheet.Cells[0, 4] = new Cell("Max"); worksheet.Cells[0, 5] = new Cell("DefaultValue"); worksheet.Cells[0, 6] = new Cell("CurrentValue"); for (int i = 0; i < ConfigNodes.Count; i++) { for (int j = 0; j < ConfigNodes[i].Items.Count; j++) { worksheet.Cells[colCount, 0] = new Cell(ConfigNodes[i].NameView); worksheet.Cells[colCount, 1] = new Cell(ConfigNodes[i].Items[j].NameView); worksheet.Cells[colCount, 2] = new Cell(ConfigNodes[i].Items[j].Unit); worksheet.Cells[colCount, 3] = new Cell(ConfigNodes[i].Items[j].Min); worksheet.Cells[colCount, 4] = new Cell(ConfigNodes[i].Items[j].Max); worksheet.Cells[colCount, 5] = new Cell(ConfigNodes[i].Items[j].DefaultValue); worksheet.Cells[colCount, 6] = new Cell(SystemConfigProvider.Instance.GetValueByName(String.Format("{0}{1}{2}", ConfigNodes[i].Name, ".", ConfigNodes[i].Items[j].Name))); colCount++; } if (ConfigNodes[i].SubNodes.Count != 0) { for (int k = 0; k < ConfigNodes[i].SubNodes.Count; k++) { for (int l = 0; l < ConfigNodes[i].SubNodes[k].Items.Count; l++) { worksheet.Cells[colCount, 0] = new Cell(ConfigNodes[i].SubNodes[k].NameView); worksheet.Cells[colCount, 1] = new Cell(ConfigNodes[i].SubNodes[k].Items[l].NameView); worksheet.Cells[colCount, 2] = new Cell(ConfigNodes[i].SubNodes[k].Items[l].Unit); worksheet.Cells[colCount, 3] = new Cell(ConfigNodes[i].SubNodes[k].Items[l].Min); worksheet.Cells[colCount, 4] = new Cell(ConfigNodes[i].SubNodes[k].Items[l].Max); worksheet.Cells[colCount, 5] = new Cell(ConfigNodes[i].SubNodes[k].Items[l].DefaultValue); worksheet.Cells[colCount, 6] = new Cell(SystemConfigProvider.Instance.GetValueByName(String.Format("{0}{1}{2}{3}{4}", ConfigNodes[i].Name, ".", ConfigNodes[i].SubNodes[k].Name, ".", ConfigNodes[i].SubNodes[k].Items[l].Name))); colCount++; } } } } workbook.Worksheets.Add(worksheet); workbook.Save(dlg.FileName); } catch (Exception ex) { LOG.Error(ex.Message, ex); } } #endregion } }