GasPanelTIN.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using DryIoc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace ProximaAnalizer.Controls.GasPanel
  19. {
  20. /// <summary>
  21. /// Interaction logic for GasPanelTIN.xaml
  22. /// </summary>
  23. public partial class GasPanelTIN : UserControl
  24. {
  25. public GasPanelTIN()
  26. {
  27. InitializeComponent();
  28. foreach (var item in this.GetChildObjects<Button>(this.CirButton))
  29. CRBs.TryAdd(item.Name, item);
  30. }
  31. public readonly Dictionary<string, Button> CRBs = [];
  32. public readonly Dictionary<string, Button> RTBs = [];
  33. private static Brush Open = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#93C572"));
  34. private static Brush Close = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00ffffff"));
  35. private void BTN_Click(object sender, RoutedEventArgs e)
  36. {
  37. if (sender is not Button btn || btn is null)
  38. return;
  39. //btn.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#90ff3724"));
  40. MessageBox.Show(btn.Name);
  41. }
  42. public List<T> GetChildObjects<T>(DependencyObject obj, bool deeper = false) where T : FrameworkElement
  43. {
  44. List<T> childList = [];
  45. for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
  46. {
  47. if (VisualTreeHelper.GetChild(obj, i) is not T child)
  48. continue;
  49. childList.Add(child);
  50. if (deeper)
  51. childList.AddRange(GetChildObjects<T>(child));
  52. }
  53. return childList;
  54. }
  55. public object? DataSource
  56. {
  57. get { return (object?)GetValue(DataSourceProperty); }
  58. set { SetValue(DataSourceProperty, value); }
  59. }
  60. // Using a DependencyProperty as the backing store for DataSource. This enables animation, styling, binding, etc...
  61. public static readonly DependencyProperty DataSourceProperty =
  62. DependencyProperty.Register("DataSource", typeof(object), typeof(GasPanelTIN), new System.Windows.PropertyMetadata(default, DataSourceChangedCallback));
  63. private static void DataSourceChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  64. {
  65. if (d is not GasPanelTIN gasPanel)
  66. return;
  67. if (e.NewValue is not KeyValuePair<string, object> data)
  68. return;
  69. if (data.Value is not ObservableDictionary<string, ObservableDictionary<string, object>> doubleData)
  70. return;
  71. switch (data.Key)
  72. {
  73. case "MFC":
  74. foreach (var item in doubleData)
  75. {
  76. if (!item.Value.TryGetValue("Feedback", out object? value) || value is not float feedback)
  77. continue;
  78. if (gasPanel.RecButton.FindName($"{item.Key}_Value") is not TextBlock textBlock)
  79. continue;
  80. textBlock.Text = feedback.ToString();
  81. }
  82. break;
  83. case "Valve":
  84. foreach (var item in doubleData)
  85. {
  86. if (!item.Value.TryGetValue("Status", out object? open) || open is not bool isOpen)
  87. continue;
  88. if (!gasPanel.CRBs.TryGetValue(item.Key, out Button? button) || button is null)
  89. continue;
  90. button.Background = isOpen switch
  91. {
  92. true => Open,
  93. false => Close,
  94. };
  95. }
  96. break;
  97. case "Sensor":
  98. foreach (var item in doubleData)
  99. {
  100. if (!item.Value.TryGetValue("Value", out object? value) || value is not bool enable)
  101. continue;
  102. if (gasPanel.CirBorder.FindName($"{item.Key}") is Border border && border is not null)
  103. {
  104. border.Background = enable switch
  105. {
  106. true => Open,
  107. false => Close,
  108. };
  109. continue;
  110. }
  111. if (gasPanel.RecButton.FindName($"{item.Key}") is Button button && button is not null)
  112. {
  113. button.Background = enable switch
  114. {
  115. true => Open,
  116. false => Close,
  117. };
  118. continue;
  119. }
  120. //Debug.WriteLine(item.Key);
  121. }
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. }
  128. }