123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- 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<ConfigNode> _ConfigNodes = new List<ConfigNode>();
- public List<ConfigNode> ConfigNodes
- {
- get { return _ConfigNodes; }
- set { _ConfigNodes = value; NotifyOfPropertyChange("ConfigNodes"); }
- }
- private List<ConfigItem> _configItems = null;
- public List<ConfigItem> 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<ConfigNode> TreeViewSelectedItemChangedCmd { private set; get; }
- #endregion
- #region Functions
- public SystemConfigViewModel()
- {
- this.DisplayName = "System Config";
- TreeViewSelectedItemChangedCmd = new BaseCommand<ConfigNode>(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<bool> 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
- }
- }
|