SystemConfigViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Input;
  9. using Aitex.Core.RT.SCCore;
  10. using ControlzEx.Standard;
  11. using FabConnect.SecsGemInterface.Application.Objects.ObjectService;
  12. using MECF.Framework.Common.DataCenter;
  13. using MECF.Framework.Common.Equipment;
  14. using MECF.Framework.Common.OperationCenter;
  15. using MECF.Framework.Common.Schedulers;
  16. using Prism.Commands;
  17. using Prism.Mvvm;
  18. using Venus_MainPages.Unity;
  19. using Venus_MainPages.Views;
  20. //using Venus_MainPages.Unity.SystemConfig;
  21. namespace Venus_MainPages.ViewModels
  22. {
  23. public class SystemConfigViewModel : BindableBase
  24. {
  25. #region 私有字段
  26. private List<ConfigNode> _ConfigNodes = new List<ConfigNode>();
  27. private List<ConfigItem> _configItems = null;
  28. private List<String> _SearchResultCollection = null;
  29. string _CurrentNodeName = string.Empty;
  30. private Visibility _Visibility = new Visibility();
  31. private string[] _InstalledModules = null;
  32. private string _SearchText;
  33. List<string> searchResultListHistory = new List<string>();
  34. public string SearchText
  35. {
  36. get { return _SearchText; }
  37. set { _SearchText = value; RaisePropertyChanged("SearchText"); }
  38. }
  39. #endregion
  40. #region 属性
  41. public List<ConfigNode> ConfigNodes
  42. {
  43. get { return _ConfigNodes; }
  44. set { _ConfigNodes = value; RaisePropertyChanged("ConfigNodes"); }
  45. }
  46. public List<String> SearchResultCollection
  47. {
  48. get { return _SearchResultCollection; }
  49. set { _SearchResultCollection = value; RaisePropertyChanged("SearchResultCollection"); }
  50. }
  51. public List<ConfigItem> ConfigItems
  52. {
  53. get { return _configItems; }
  54. set { _configItems = value; RaisePropertyChanged("ConfigItems"); }
  55. }
  56. public Visibility SearchListBoxShow
  57. {
  58. get { return _Visibility; }
  59. set { _Visibility = value; RaisePropertyChanged("SearchListBoxShow"); }
  60. }
  61. #region 命令
  62. private DelegateCommand<object> _TreeViewSelectedItemChangedCmd;
  63. public DelegateCommand<object> TreeViewSelectedItemChangedCmd =>
  64. _TreeViewSelectedItemChangedCmd ?? (_TreeViewSelectedItemChangedCmd = new DelegateCommand<object>(TreeViewSelectedItemChanged));
  65. #endregion
  66. #endregion
  67. #region 命令
  68. private DelegateCommand<object> _SetValueCommand;
  69. public DelegateCommand<object> SetValueCommand =>
  70. _SetValueCommand ?? (_SetValueCommand = new DelegateCommand<object>(SetValue));
  71. private DelegateCommand<object> _SearchTextChangedCommand;
  72. public DelegateCommand<object> SearchTextChangedCommand =>
  73. _SearchTextChangedCommand ?? (_SearchTextChangedCommand = new DelegateCommand<object>(SearchTextChanged));
  74. private DelegateCommand<object> _ListBoxSelectSearchResultCommand;
  75. public DelegateCommand<object> ListBoxSelectSearchResultCommand =>
  76. _ListBoxSelectSearchResultCommand ?? (_ListBoxSelectSearchResultCommand = new DelegateCommand<object>(SelectSearchResult));
  77. #endregion
  78. public SystemConfigViewModel()
  79. {
  80. SearchListBoxShow = Visibility.Hidden;
  81. ConfigNodes = SystemConfigProvider.Instance.GetConfigTree().SubNodes;
  82. }
  83. //protected override void OnInitialize()
  84. //{
  85. // base.OnInitialize();
  86. // ConfigNodes = SystemConfigProvider.Instance.GetConfigTree().SubNodes;
  87. //}
  88. //输入关键字
  89. private void SearchTextChanged(object searchConfigItem)
  90. {
  91. List<string> searchResultList = new List<string>();
  92. //如果输入框为空,不显示提示词,并展开左侧栏
  93. if (string.IsNullOrEmpty((string)searchConfigItem) || searchConfigItem.ToString().Length<3)
  94. {
  95. ConfigNodes = SystemConfigProvider.Instance.GetConfigTree().SubNodes;
  96. SearchListBoxShow = Visibility.Hidden;
  97. }
  98. else
  99. {
  100. ConfigNodes = SystemConfigProvider.Instance.GetConfigTree().SubNodes;
  101. var itemslist = ConfigNodes.SelectMany(m => m.Items).ToList();
  102. if (SearchText != null)
  103. {
  104. SearchListBoxShow = Visibility.Visible;
  105. //根节点
  106. string searchResult = null;
  107. _InstalledModules = QueryDataClient.Instance.Service.GetConfig($"System.InstalledModules").ToString().Split(',');
  108. foreach (var ConfigNodesItems in ConfigNodes)
  109. {
  110. if (!_InstalledModules.Contains(ConfigNodesItems.Name) && ConfigNodesItems.Name != "System" && ConfigNodesItems.Name != "Recipe" && ConfigNodesItems.Name != "Scheduler")
  111. {
  112. ConfigNodesItems.IsShow = false;
  113. continue;
  114. }
  115. //根节点搜索
  116. if (ConfigNodesItems.Name.Contains(SearchText))
  117. {
  118. ConfigNodesItems.IsShow = true;//展开根节点
  119. _CurrentNodeName = SearchText;//TreeView节点
  120. searchResult = _CurrentNodeName;
  121. GetDataOfConfigItems();
  122. }
  123. else
  124. {
  125. ConfigNodesItems.IsShow = false;
  126. //二级节点搜索
  127. foreach (var SubNodesItems in ConfigNodesItems.SubNodes)
  128. {
  129. if (SubNodesItems.Name.ToLower().Contains(SearchText.ToLower()))
  130. {
  131. ConfigNodesItems.IsShow = true;
  132. SubNodesItems.IsShow = true;
  133. _CurrentNodeName = String.Format("{0}{1}{2}", ConfigNodesItems.Name, ".", SubNodesItems.Name);
  134. searchResult = _CurrentNodeName;
  135. GetDataOfConfigItems();
  136. }
  137. else
  138. {
  139. SubNodesItems.IsShow = false;
  140. //叶子节点搜索
  141. foreach (var Items in SubNodesItems.Items)
  142. {
  143. var searchResult1 = SubNodesItems.Items.FindAll(e => e.Name.ToLower().Contains(SearchText.ToLower()));
  144. if (Items.Name.ToLower().Contains(SearchText.ToLower()))
  145. {
  146. ConfigNodesItems.IsShow = true;
  147. SubNodesItems.IsShow = true;
  148. _CurrentNodeName = String.Format("{0}{1}{2}{3}{4}", ConfigNodesItems.Name, ".", SubNodesItems.Name, ".", Items.Name);
  149. searchResult = _CurrentNodeName;
  150. GetDataOfConfigItems();
  151. searchResultList.Add(_CurrentNodeName);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. var searchResult2 = ConfigNodesItems.Items.FindAll(e => e.Name.ToLower().Contains(SearchText.ToLower()));
  158. if (searchResult2.Count != 0)
  159. {
  160. ConfigNodesItems.IsShow = true;
  161. _CurrentNodeName = SearchText;//TreeView节点
  162. searchResultList.Add(ConfigNodesItems.Name + "." + searchResult2[0].Name);
  163. searchResult = _CurrentNodeName;
  164. GetDataOfConfigItems();
  165. }
  166. //查询为空
  167. if (searchResult == null)
  168. {
  169. ConfigItems = null;
  170. }
  171. }
  172. }
  173. SearchResultCollection = searchResultList;
  174. if (searchResultList.Count > 0)
  175. {
  176. searchResultListHistory = searchResultList;
  177. }
  178. }
  179. }
  180. //选择选中项
  181. private void SelectSearchResult(object slectItem)
  182. {
  183. SearchResultCollection = searchResultListHistory;
  184. ConfigNodes = SystemConfigProvider.Instance.GetConfigTree().SubNodes;
  185. if (slectItem != null)
  186. {
  187. SearchText = slectItem.ToString();
  188. string[] searchResult = SearchText.Split('.');
  189. List<ConfigItem> ConfigItem = new List<ConfigItem>();
  190. //根节点下ConfigItems
  191. if (searchResult.Length == 2)
  192. {
  193. ConfigNodes.ForEach(e => e.IsShow = false);
  194. ConfigNode targetmodule = ConfigNodes.Find(e => e.Name.ToLower() == searchResult[0].ToLower());
  195. targetmodule.IsShow = true;
  196. targetmodule.SubNodes.ForEach(e => e.IsShow = false);
  197. _CurrentNodeName = string.IsNullOrEmpty(targetmodule.Path) ? targetmodule.Name : $"{targetmodule.Path}.{targetmodule.Name}";
  198. ConfigItems = targetmodule.Items.FindAll(e => e.Name.ToLower() == searchResult[1].ToLower());
  199. GetDataOfConfigItems();
  200. }
  201. //二级节点下ConfigItems
  202. if (searchResult.Length == 3)
  203. {
  204. ConfigNodes.ForEach(e => e.IsShow = false);
  205. ConfigNode targetmodule = ConfigNodes.Find(e => e.Name.ToLower() == searchResult[0].ToLower());
  206. targetmodule.IsShow = true;
  207. targetmodule.SubNodes.ForEach(e => e.IsShow = false);
  208. ConfigNode submodule = targetmodule.SubNodes.Find(e => e.Name.ToLower() == searchResult[1].ToLower());
  209. submodule.IsShow = true;
  210. _CurrentNodeName = string.IsNullOrEmpty(submodule.Path) ? submodule.Name : $"{submodule.Path}.{submodule.Name}";
  211. ConfigItems = submodule.Items.FindAll(e => e.Name.ToLower() == searchResult[2].ToLower());
  212. submodule.Items.FindAll(e => e.Name.ToLower() != searchResult[2].ToLower()).ForEach(e => e.Visible = false);
  213. GetDataOfConfigItems();
  214. }
  215. }
  216. }
  217. private void TreeViewSelectedItemChanged(object node)
  218. {
  219. var node2 = (ConfigNode)node;
  220. _CurrentNodeName = string.IsNullOrEmpty(node2.Path) ? node2.Name : $"{node2.Path}.{node2.Name}";
  221. string[] searchResult = null;
  222. if (SearchText == "" || SearchText == null)
  223. {
  224. ConfigItems = node2.Items;
  225. }
  226. else
  227. {
  228. searchResult = SearchText.Split('.');
  229. if (searchResult.Length == 2)
  230. {
  231. ConfigItems = node2.Items.FindAll(e => e.Name.ToLower() == searchResult[1].ToLower());
  232. }
  233. if (searchResult.Length == 3)
  234. {
  235. ConfigItems = node2.Items.FindAll(e => e.Name.ToLower() == searchResult[2].ToLower());
  236. }
  237. }
  238. GetDataOfConfigItems();
  239. }
  240. private void GetDataOfConfigItems()
  241. {
  242. if (ConfigItems == null)
  243. return;
  244. for (int i = 0; i < ConfigItems.Count; i++)
  245. {
  246. string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", ConfigItems[i].Name);
  247. ConfigItems[i].CurrentValue = SystemConfigProvider.Instance.GetValueByName(key);
  248. if (ConfigItems[i].Type == DataType.Bool)
  249. {
  250. bool value;
  251. if (bool.TryParse(ConfigItems[i].CurrentValue, out value))
  252. ConfigItems[i].BoolValue = value;
  253. }
  254. else
  255. ConfigItems[i].StringValue = ConfigItems[i].CurrentValue;
  256. }
  257. }
  258. private void SetValue(Object myitem)
  259. {
  260. //key :System.IsSimulatorMode
  261. //value: true or false 都是字符串
  262. //input check
  263. var item = (ConfigItem)myitem;
  264. string value;
  265. if (item.Type == DataType.Bool)
  266. {
  267. value = item.BoolValue.ToString().ToLower();
  268. }
  269. else
  270. {
  271. if (item.TextSaved)
  272. return;
  273. if (item.Type == DataType.Int)
  274. {
  275. int iValue;
  276. if (int.TryParse(item.StringValue, out iValue))
  277. {
  278. if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min))
  279. {
  280. if (iValue > item.Max || iValue < item.Min)
  281. {
  282. //DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", ((int)item.Min).ToString(), ((int)item.Max).ToString()));
  283. return;
  284. }
  285. }
  286. }
  287. else
  288. {
  289. //DialogBox.ShowWarning("Please input valid data.");
  290. return;
  291. }
  292. value = item.StringValue;
  293. }
  294. else if (item.Type == DataType.Double)
  295. {
  296. double fValue;
  297. if (double.TryParse(item.StringValue, out fValue))
  298. {
  299. if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min))
  300. {
  301. if (fValue > item.Max || fValue < item.Min)
  302. {
  303. //DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", item.Min.ToString(), item.Max.ToString()));
  304. return;
  305. }
  306. }
  307. }
  308. else
  309. {
  310. //DialogBox.ShowWarning("Please input valid data.");
  311. return;
  312. }
  313. value = item.StringValue;
  314. }
  315. else
  316. value = item.StringValue;
  317. }
  318. string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", item.Name);
  319. InvokeClient.Instance.Service.DoOperation("System.SetConfig", key, value);
  320. item.TextSaved = true;
  321. Reload();
  322. }
  323. public void Reload()
  324. {
  325. GetDataOfConfigItems();
  326. }
  327. public void SaveAll()
  328. {
  329. if (ConfigItems == null)
  330. return;
  331. ConfigItems.ForEach(item => SetValue(item));
  332. }
  333. }
  334. }