MainParameterEditViewModel.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. using Aitex.Core.RT.SCCore;
  2. using Caliburn.Micro;
  3. using MECF.Framework.Common.DataCenter;
  4. using MECF.Framework.Common.OperationCenter;
  5. using MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig;
  6. using MECF.Framework.UI.Client.CenterViews.Dialogs;
  7. using MECF.Framework.UI.Client.CenterViews.Editors.Recipe;
  8. using MECF.Framework.UI.Client.ClientBase;
  9. using OpenSEMI.ClientBase;
  10. using SciChart.Core.Extensions;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.Globalization;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Data;
  21. using System.Windows.Input;
  22. using System.Windows.Threading;
  23. namespace MECF.Framework.UI.Client.CenterViews.Parameter
  24. {
  25. public class MainParameterEditViewModel : ModuleUiViewModelBase, ISupportMultipleSystem
  26. {
  27. public bool IsPermission { get => this.Permission == 3; }
  28. public List<ParamBase> ParameterTree { get; set; } = new List<ParamBase>();
  29. public ObservableCollection<TempSetData> HeadLst { get; set; } = new ObservableCollection<TempSetData>();
  30. public ObservableCollection<TempSetData> PIDHeadLst { get; set; } = new ObservableCollection<TempSetData>();
  31. public ObservableCollection<ConfigItem> StepNames { get; set; } = new ObservableCollection<ConfigItem>();
  32. private ConfigNode _currentNode;
  33. public ConfigNode CurrentNode
  34. {
  35. get { return _currentNode; }
  36. set { _currentNode = value; this.NotifyOfPropertyChange(nameof(CurrentNode)); }
  37. }
  38. private string _allZoneValue = "0";
  39. public string AllZoneValue
  40. {
  41. get { return _allZoneValue; }
  42. set { _allZoneValue = value; this.NotifyOfPropertyChange(nameof(AllZoneValue)); }
  43. }
  44. private ConfigNode _currentSubNode;
  45. public ConfigNode CurrentSubNode
  46. {
  47. get { return _currentSubNode; }
  48. set { _currentSubNode = value; this.NotifyOfPropertyChange(nameof(CurrentSubNode)); }
  49. }
  50. private ConfigNode _currentTwoSubNode;
  51. public ConfigNode CurrentTwoSubNode
  52. {
  53. get { return _currentTwoSubNode; }
  54. set { _currentTwoSubNode = value; this.NotifyOfPropertyChange(nameof(CurrentTwoSubNode)); }
  55. }
  56. private List<ConfigNode> _ConfigNodes = new List<ConfigNode>();
  57. public List<ConfigNode> ConfigNodes
  58. {
  59. get { return _ConfigNodes; }
  60. set { _ConfigNodes = value; NotifyOfPropertyChange("ConfigNodes"); }
  61. }
  62. private string _defaultUnit = "Pa";
  63. public string DefaultUnit
  64. {
  65. get
  66. {
  67. return _defaultUnit;
  68. }
  69. set
  70. {
  71. _defaultUnit = value;
  72. NotifyOfPropertyChange("DefaultUnit");
  73. }
  74. }
  75. private ConfigNode _rootNode;
  76. private string _CurrentNodeName = string.Empty;
  77. private string path;
  78. private string currentValue;
  79. private ObservableCollection<PageValue> valueList { get; set; } = new ObservableCollection<PageValue>();
  80. private ObservableCollection<PageValue> newValueList { get; set; } = new ObservableCollection<PageValue>();
  81. protected override void OnInitialize()
  82. {
  83. base.OnInitialize();
  84. this.SystemName = "System";
  85. ConfigNodes = SystemConfigProvider.Instance.GetConfigTree(SystemName).SubNodes;
  86. _rootNode = SystemConfigProvider.Instance.GetConfig(true);
  87. InitTree();
  88. }
  89. protected override void OnActivate()
  90. {
  91. base.OnActivate();
  92. }
  93. private void InitTree()
  94. {
  95. var node = SystemConfigProvider.Instance.GetConfigTree(SystemName).SubNodes;
  96. ParameterTree.Add(new ParamBase() { ID = 1, ParentID = 0, Name = "StepName", ConfigName = "StepName" });
  97. //ParameterTree.Add(new ParamBase() { ID = 2, ParentID = 0, Name = "TempSetting", ConfigName = "TempSetting" });
  98. //ParameterTree.Add(new ParamBase() { ID = 3, ParentID = 0, Name = "TempSetting", ConfigName = "TempSetting" });
  99. ParameterTree = this.LoadTreeView(0);
  100. foreach (var item in ParameterTree)
  101. {
  102. InitAllParameterTree(item);
  103. }
  104. var stepNameNode = FindNodeByName(_rootNode, $"{strHeader}.StepName");
  105. if (stepNameNode != null && stepNameNode.Items.Count > 0)
  106. {
  107. stepNameNode.Items.ForEach(x => StepNames.Add(x));
  108. CurrentNode = stepNameNode;
  109. }
  110. this.GetHeaderConfig();
  111. }
  112. string strHeader = "PM1.RecipeEditParameter";
  113. private void InitAllParameterTree(ParamBase para)
  114. {
  115. CurrentNode = new ConfigNode();
  116. CurrentNode = FindNodeByName(_rootNode, $"{strHeader}.{para.ConfigName}");
  117. InitItemsCurrentValue(CurrentNode, true);
  118. if (CurrentNode.SubNodes != null)
  119. {
  120. foreach (var item in CurrentNode.SubNodes)
  121. {
  122. InitItemsCurrentValue(item, true);
  123. }
  124. }
  125. }
  126. private Visibility _stepNameVisibility = Visibility.Visible;
  127. public Visibility StepNameVisibility
  128. {
  129. get => _stepNameVisibility;
  130. set
  131. {
  132. _stepNameVisibility = value;
  133. NotifyOfPropertyChange(nameof(StepNameVisibility));
  134. }
  135. }
  136. private Visibility _tempSettingeVisibility = Visibility.Hidden;
  137. public Visibility TempSettingVisibility
  138. {
  139. get => _tempSettingeVisibility;
  140. set
  141. {
  142. _tempSettingeVisibility = value;
  143. NotifyOfPropertyChange(nameof(TempSettingVisibility));
  144. }
  145. }
  146. private List<ParamBase> LoadTreeView(int id)
  147. {
  148. List<ParamBase> node = ParameterTree.FindAll(s => s.ParentID.Equals(id));
  149. foreach (var item in node)
  150. {
  151. item.Children = LoadTreeView(item.ID);
  152. }
  153. return node;
  154. }
  155. public void HideAllCanvas()
  156. {
  157. StepNameVisibility = Visibility.Hidden;
  158. TempSettingVisibility = Visibility.Hidden;
  159. }
  160. public void TreeViewSelectedItemChanged(object obj)
  161. {
  162. valueList.Clear();
  163. var view = GetView() as MainParameterEditView;
  164. HideAllCanvas();
  165. ParamBase para = obj as ParamBase;
  166. string strHeader = "PM1.RecipeEditParameter";
  167. switch (para.ConfigName)
  168. {
  169. case "StepName":
  170. StepNameVisibility = Visibility.Visible;
  171. break;
  172. case "TempSetting":
  173. CurrentNode = FindNodeByName(_rootNode, $"{strHeader}.{para.ConfigName}");
  174. TempSettingVisibility = Visibility.Visible;
  175. if (CurrentNode.SubNodes.Where(x => x.StabilizeBoolValue == true).Count() <= 0)
  176. {
  177. CurrentNode.SubNodes[0].StabilizeBoolValue = true;
  178. }
  179. CurrentSubNode = CurrentNode.SubNodes.FirstOrDefault(x => x.StabilizeBoolValue);
  180. break;
  181. }
  182. }
  183. private void InitItemsCurrentValue(ConfigNode node, bool initSubItems = true)
  184. {
  185. if (node == null) return;
  186. _CurrentNodeName = string.IsNullOrEmpty(node.Path) ? node.Name : $"{node.Path}.{node.Name}";
  187. if (node.Items == null || node.Items.Count <= 0)
  188. {
  189. if (!initSubItems) return;
  190. foreach (var item in node.SubNodes)
  191. {
  192. InitItemsCurrentValue(item);
  193. }
  194. }
  195. else
  196. {
  197. GetDataOfConfigItems(node);
  198. }
  199. }
  200. private void GetDataOfConfigItems(ConfigNode node)
  201. {
  202. if (node.Items == null)
  203. return;
  204. List<string> allItems = new List<string>();
  205. for (int i = 0; i < node.Items.Count; i++)
  206. {
  207. string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", node.Items[i].Name);
  208. allItems.Add(key);
  209. }
  210. List<string> newallItems = allItems.Distinct().ToList();
  211. var DictItems = QueryDataClient.Instance.Service.PollConfig(newallItems);
  212. for (int i = 0; i < node.Items.Count; i++)
  213. {
  214. string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", node.Items[i].Name);
  215. node.Items[i].CurrentValue = DictItems.ContainsKey(key) ? DictItems[key].ToString() : "";
  216. node.Items[i].Path = key;
  217. //if (CurrenItems[i].Path.Contains("PM1.RecipeEditParameter.PressureStabilizeTable"))
  218. // DefaultUnit = CurrenItems[i].Unit;
  219. if (node.Items[i].Type == DataType.Bool)
  220. {
  221. bool value;
  222. if (bool.TryParse(node.Items[i].CurrentValue, out value))
  223. {
  224. node.Items[i].BoolValue = value;
  225. node.Items[i].CurrentValue = value ? "Yes" : "No";
  226. }
  227. }
  228. else
  229. node.Items[i].StringValue = node.Items[i].CurrentValue;
  230. }
  231. }
  232. public void MenuCommand(object obj, object menuLevel)
  233. {
  234. RadioButton radioButton = obj as RadioButton;
  235. switch ((string)menuLevel)
  236. {
  237. case "LevelOne":
  238. CurrentSubNode = CurrentNode.SubNodes.Find((x) => x.Name == radioButton.ToolTip.ToString());
  239. break;
  240. case "LevelTwo":
  241. CurrentTwoSubNode = CurrentSubNode.SubNodes.Find((x) => x.Name == radioButton.ToolTip.ToString());
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. public void SetValue(object obj)
  248. {
  249. if (CurrentNode.Items == null || CurrentNode.Items.Count == 0) return;
  250. ConfigItem item = null;
  251. if (obj is Control)
  252. item = CurrentNode.Items.ToList().Find((x) => x.Name == (obj as Control).ToolTip.ToString());
  253. else
  254. item = obj as ConfigItem;
  255. if (item == null) return;
  256. if (item.Type == DataType.String)
  257. {
  258. currentValue = item.CurrentValue;
  259. path = item.Path;
  260. FullKeyboard fullKeyboard = new FullKeyboard("", currentValue);
  261. if ((bool)fullKeyboard.ShowDialog())
  262. {
  263. InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", path, fullKeyboard.ValueString);
  264. currentValue = fullKeyboard.ValueString;
  265. item.CurrentValue = fullKeyboard.ValueString;
  266. }
  267. valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue });
  268. }
  269. else
  270. {
  271. InputDialogViewModel dialog = new InputDialogViewModel();
  272. dialog.Item = item;
  273. dialog.DisplayName = "Set Value";
  274. WindowManager wm = new WindowManager();
  275. bool? bret = wm.ShowDialog(dialog);
  276. if ((bool)bret)
  277. {
  278. item.StringValue = dialog.DialogResult;
  279. //key :System.IsSimulatorMode
  280. //value: true or false 都是字符串
  281. //input check
  282. string value;
  283. if (item.Type == DataType.Bool)
  284. {
  285. if (item.StringValue.Equals("Yes", StringComparison.CurrentCultureIgnoreCase))
  286. item.BoolValue = true;
  287. else if (item.StringValue.Equals("No", StringComparison.CurrentCultureIgnoreCase))
  288. item.BoolValue = false;
  289. else
  290. {
  291. DialogBox.ShowWarning("The Value Should be Yes Or No.");
  292. return;
  293. }
  294. value = item.BoolValue.ToString().ToLower();
  295. }
  296. else
  297. {
  298. if (item.Type == DataType.Int)
  299. {
  300. int iValue;
  301. if (int.TryParse(item.StringValue, out iValue))
  302. {
  303. if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min))
  304. {
  305. if (iValue > item.Max || iValue < item.Min)
  306. {
  307. DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", ((int)item.Min).ToString(), ((int)item.Max).ToString()));
  308. return;
  309. }
  310. }
  311. }
  312. else
  313. {
  314. DialogBox.ShowWarning("Please input valid data.");
  315. return;
  316. }
  317. value = item.StringValue;
  318. }
  319. else if (item.Type == DataType.Double)
  320. {
  321. double fValue;
  322. if (double.TryParse(item.StringValue, out fValue))
  323. {
  324. if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min))
  325. {
  326. if (fValue > item.Max || fValue < item.Min)
  327. {
  328. DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", item.Min.ToString(), item.Max.ToString()));
  329. return;
  330. }
  331. string[] box = fValue.ToString().Split('.');
  332. if (box.Length > 1 && box[1].Length > 3)
  333. {
  334. DialogBox.ShowWarning(string.Format("The value should be more than three decimal places"));
  335. return;
  336. }
  337. }
  338. }
  339. else
  340. {
  341. DialogBox.ShowWarning("Please input valid data.");
  342. return;
  343. }
  344. value = item.StringValue;
  345. }
  346. else
  347. value = item.StringValue;
  348. }
  349. string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", item.Name);
  350. path = item.Path;
  351. currentValue = value;
  352. valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue });
  353. }
  354. }
  355. }
  356. public void AllZoneTextChanged()
  357. {
  358. if (CurrentSubNode.Items == null || CurrentSubNode.Items.Count == 0) return;
  359. newValueList = new ObservableCollection<PageValue>();
  360. valueList = new ObservableCollection<PageValue>();
  361. foreach (var item in CurrentSubNode.Items)
  362. {
  363. item.CurrentValue = AllZoneValue;
  364. newValueList.Add(new PageValue() { Path = item.Path, CurrentValue = AllZoneValue });
  365. }
  366. }
  367. public void SetCurrentSubNodeValue(object obj)
  368. {
  369. if (CurrentSubNode.Items == null || CurrentSubNode.Items.Count == 0) return;
  370. ConfigItem item = null;
  371. if (obj is Control)
  372. item = CurrentSubNode.Items.ToList().Find((x) => x.Name == (obj as Control).ToolTip.ToString());
  373. else
  374. item = obj as ConfigItem;
  375. if (item == null) return;
  376. if (item.Type == DataType.String)
  377. {
  378. currentValue = item.CurrentValue;
  379. path = item.Path;
  380. FullKeyboard fullKeyboard = new FullKeyboard("", currentValue);
  381. if ((bool)fullKeyboard.ShowDialog())
  382. {
  383. InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", path, fullKeyboard.ValueString);
  384. currentValue = fullKeyboard.ValueString;
  385. item.CurrentValue = fullKeyboard.ValueString;
  386. }
  387. valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue });
  388. }
  389. else
  390. {
  391. InputDialogViewModel dialog = new InputDialogViewModel();
  392. dialog.Item = item;
  393. dialog.DisplayName = "Set Value";
  394. WindowManager wm = new WindowManager();
  395. bool? bret = wm.ShowDialog(dialog);
  396. if ((bool)bret)
  397. {
  398. item.StringValue = dialog.DialogResult;
  399. //key :System.IsSimulatorMode
  400. //value: true or false 都是字符串
  401. //input check
  402. string value;
  403. if (item.Type == DataType.Bool)
  404. {
  405. if (item.StringValue.Equals("Yes", StringComparison.CurrentCultureIgnoreCase))
  406. item.BoolValue = true;
  407. else if (item.StringValue.Equals("No", StringComparison.CurrentCultureIgnoreCase))
  408. item.BoolValue = false;
  409. else
  410. {
  411. DialogBox.ShowWarning("The Value Should be Yes Or No.");
  412. return;
  413. }
  414. value = item.BoolValue.ToString().ToLower();
  415. }
  416. else
  417. {
  418. if (item.Type == DataType.Int)
  419. {
  420. int iValue;
  421. if (int.TryParse(item.StringValue, out iValue))
  422. {
  423. if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min))
  424. {
  425. if (iValue > item.Max || iValue < item.Min)
  426. {
  427. DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", ((int)item.Min).ToString(), ((int)item.Max).ToString()));
  428. return;
  429. }
  430. }
  431. }
  432. else
  433. {
  434. DialogBox.ShowWarning("Please input valid data.");
  435. return;
  436. }
  437. value = item.StringValue;
  438. }
  439. else if (item.Type == DataType.Double)
  440. {
  441. double fValue;
  442. if (double.TryParse(item.StringValue, out fValue))
  443. {
  444. if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min))
  445. {
  446. if (fValue > item.Max || fValue < item.Min)
  447. {
  448. DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", item.Min.ToString(), item.Max.ToString()));
  449. return;
  450. }
  451. string[] box = fValue.ToString().Split('.');
  452. if (box.Length > 1 && box[1].Length > 3)
  453. {
  454. DialogBox.ShowWarning(string.Format("The value should be more than three decimal places"));
  455. return;
  456. }
  457. }
  458. }
  459. else
  460. {
  461. DialogBox.ShowWarning("Please input valid data.");
  462. return;
  463. }
  464. value = item.StringValue;
  465. }
  466. else
  467. value = item.StringValue;
  468. }
  469. path = item.Path;
  470. currentValue = value;
  471. valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue });
  472. }
  473. }
  474. }
  475. public void SetStepNameValue(object obj, MouseButtonEventArgs e)
  476. {
  477. e.Handled = true;
  478. if (StepNames == null || StepNames.Count == 0) return;
  479. ConfigItem item = null;
  480. if (obj is Control)
  481. item = StepNames.ToList().Find((x) => x.Name == (obj as Control).ToolTip.ToString());
  482. else
  483. item = obj as ConfigItem;
  484. if (item == null) return;
  485. if (item.Type == DataType.String)
  486. {
  487. currentValue = item.CurrentValue;
  488. path = item.Path;
  489. FullKeyboard fullKeyboard = new FullKeyboard("", currentValue);
  490. if ((bool)fullKeyboard.ShowDialog())
  491. {
  492. InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", path, fullKeyboard.ValueString);
  493. currentValue = fullKeyboard.ValueString;
  494. item.CurrentValue = fullKeyboard.ValueString;
  495. }
  496. valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue });
  497. }
  498. else
  499. {
  500. InputDialogViewModel dialog = new InputDialogViewModel();
  501. dialog.Item = item;
  502. dialog.DisplayName = "Set Value";
  503. WindowManager wm = new WindowManager();
  504. bool? bret = wm.ShowDialog(dialog);
  505. if ((bool)bret)
  506. {
  507. item.StringValue = dialog.DialogResult;
  508. //key :System.IsSimulatorMode
  509. //value: true or false 都是字符串
  510. //input check
  511. string value;
  512. value = item.StringValue;
  513. path = item.Path;
  514. currentValue = value;
  515. valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue });
  516. }
  517. }
  518. }
  519. public void SaveParameter()
  520. {
  521. if (newValueList!=null&& newValueList.Count>0 )
  522. {
  523. var one = newValueList[0];
  524. if (one.Path.Contains("TempSetting"))
  525. {
  526. foreach (var item in valueList)
  527. {
  528. newValueList.Add(item);
  529. }
  530. valueList.Clear();
  531. valueList = newValueList;
  532. }
  533. }
  534. foreach (var item in valueList)
  535. {
  536. InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", item.Path, item.CurrentValue);
  537. }
  538. valueList = new ObservableCollection<PageValue>();
  539. newValueList = new ObservableCollection<PageValue>();
  540. }
  541. private string[] HeaterNameList = new string[5] { "HeaterU", "HeaterCU", "HeaterC", "HeaterCL", "HeaterL" };
  542. public void GetHeaderConfig()
  543. {
  544. HeadLst.Clear();
  545. for (int i = 0; i < 5; i++)
  546. {
  547. var configHeater = (string)QueryDataClient.Instance.Service.GetConfig($"PM1.Heater.{HeaterNameList[i]}.DisplayName");
  548. TempSetData item = new TempSetData(configHeater);
  549. item.SetValue.Value = configHeater;
  550. HeadLst.Add(item);
  551. }
  552. }
  553. private ConfigNode FindNodeByName(ConfigNode parentNode, string strName)
  554. {
  555. string strCates = strName.Split('.')[0];
  556. ConfigNode node = parentNode.SubNodes.Find((x) => x.Name == strCates);
  557. if (node == null)
  558. return parentNode;
  559. else
  560. return FindNodeByName(node, strName.Replace(strCates + ".", ""));
  561. }
  562. }
  563. public class ParamBase
  564. {
  565. public int ID { get; set; }
  566. public string Name { get; set; }
  567. public int ParentID { get; set; }
  568. public string ConfigName { get; set; }
  569. public List<ParamBase> Children { get; set; }
  570. public ParamBase()
  571. {
  572. Children = new List<ParamBase>();
  573. }
  574. }
  575. public class ParameterConvert : IValueConverter
  576. {
  577. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  578. {
  579. return value.ToString().Replace("Table", "").Replace("LineNo", "");
  580. }
  581. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  582. {
  583. throw new NotImplementedException();
  584. }
  585. }
  586. }