TempConfigComparision.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. namespace HistoryView.Controls.Configs;
  2. public partial class TempConfigComparision : UserControl
  3. {
  4. public TempConfigComparision()
  5. {
  6. InitializeComponent();
  7. this.Comparision = [];
  8. }
  9. private void Button_Click(object sender, RoutedEventArgs e)
  10. {
  11. FindVisualChildren.Find<Expander>(this.Items).ForEach(t => t.IsExpanded = false);
  12. }
  13. private void Button_Click_1(object sender, RoutedEventArgs e)
  14. {
  15. FindVisualChildren.Find<Expander>(this.Items).ForEach(t => t.IsExpanded = true);
  16. }
  17. public TempConfig Source
  18. {
  19. get { return (TempConfig)GetValue(SourceProperty); }
  20. set { SetValue(SourceProperty, value); }
  21. }
  22. public static readonly DependencyProperty SourceProperty =
  23. DependencyProperty.Register("Source", typeof(TempConfig), typeof(TempConfigComparision), new PropertyMetadata(default, SourceChangedCallback));
  24. public TempConfig Target
  25. {
  26. get { return (TempConfig)GetValue(TargetProperty); }
  27. set { SetValue(TargetProperty, value); }
  28. }
  29. public static readonly DependencyProperty TargetProperty =
  30. DependencyProperty.Register("Target", typeof(TempConfig), typeof(TempConfigComparision), new PropertyMetadata(default, TargetChangedCallback));
  31. private static void SourceChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  32. {
  33. if (d is not TempConfigComparision act)
  34. return;
  35. if (e.NewValue is not TempConfig config)
  36. return;
  37. if (config.Mini8sConfig is null)
  38. return;
  39. foreach (var mini8 in config.Mini8sConfig)
  40. {
  41. act.Comparision[mini8.Key] ??= new();
  42. act.Comparision[mini8.Key].Index = mini8.Key;
  43. act.Comparision[mini8.Key].Source = mini8.Value;
  44. }
  45. Compare(act);
  46. }
  47. private static void TargetChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  48. {
  49. if (d is not TempConfigComparision act)
  50. return;
  51. if (e.NewValue is not TempConfig config)
  52. return;
  53. if (config.Mini8sConfig is null)
  54. return;
  55. foreach (var mini8 in config.Mini8sConfig)
  56. {
  57. act.Comparision[mini8.Key] ??= new();
  58. act.Comparision[mini8.Key].Index = mini8.Key;
  59. act.Comparision[mini8.Key].Target = mini8.Value;
  60. }
  61. Compare(act);
  62. }
  63. public ObservableDictionary<byte, TempConfigComparisonData> Comparision
  64. {
  65. get { return (ObservableDictionary<byte, TempConfigComparisonData>)GetValue(ComparisionProperty); }
  66. set { SetValue(ComparisionProperty, value); }
  67. }
  68. public static readonly DependencyProperty ComparisionProperty =
  69. DependencyProperty.Register("Comparision", typeof(ObservableDictionary<byte, TempConfigComparisonData>), typeof(TempConfigComparision), new PropertyMetadata(default));
  70. private static void Compare(TempConfigComparision act)
  71. {
  72. foreach (TempConfigComparisonData tempConfigComparisonData in act.Comparision.Values)
  73. {
  74. if (tempConfigComparisonData.Source is null || tempConfigComparisonData.Target is null)
  75. {
  76. tempConfigComparisonData.IsSame = null;
  77. return;
  78. }
  79. if (tempConfigComparisonData.Source.ChannelConfig.Count != tempConfigComparisonData.Target.ChannelConfig.Count)
  80. {
  81. tempConfigComparisonData.IsSame = false;
  82. continue;
  83. }
  84. foreach (var tempConfigSubMini8Source in tempConfigComparisonData.Source.ChannelConfig)
  85. {
  86. if (!tempConfigComparisonData.Target.ChannelConfig.TryGetValue(tempConfigSubMini8Source.Key, out var tempConfigSubMini8Target) || tempConfigSubMini8Target is null)
  87. {
  88. tempConfigComparisonData.IsSame = false;
  89. continue;
  90. }
  91. if (!CompareChannel(tempConfigSubMini8Source.Value, tempConfigSubMini8Target))
  92. {
  93. tempConfigComparisonData.IsSame = false;
  94. continue;
  95. }
  96. tempConfigComparisonData.IsSame = true;
  97. }
  98. }
  99. }
  100. private static bool CompareChannel(TempConfigSubChannel source, TempConfigSubChannel target)
  101. {
  102. if (source.SetPoint != target.SetPoint)
  103. return false;
  104. if (source.Caps != target.Caps)
  105. return false;
  106. if (source.Floor != target.Floor)
  107. return false;
  108. if (source.Running_P != target.Running_P)
  109. return false;
  110. if (source.Running_I != target.Running_I)
  111. return false;
  112. if (source.Running_D != target.Running_D)
  113. return false;
  114. if (source.SetpointUpRate != target.SetpointUpRate)
  115. return false;
  116. if (source.SetpointDownRate != target.SetpointDownRate)
  117. return false;
  118. return true;
  119. }
  120. }
  121. public partial class TempConfigComparisonData : ObservableObject
  122. {
  123. [ObservableProperty]
  124. private byte _Index;
  125. [ObservableProperty]
  126. private bool? _IsSame;
  127. [ObservableProperty]
  128. private TempConfigSubMini8? _Source;
  129. [ObservableProperty]
  130. private TempConfigSubMini8? _Target;
  131. }