MainWindowViewModel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections.ObjectModel;
  2. using System.ComponentModel;
  3. using System.Runtime.CompilerServices;
  4. using System.Windows.Input;
  5. using System.Xml;
  6. using Command;
  7. using GenHTTP.Engine;
  8. using InABox.Rpc;
  9. using Microsoft.Xaml.Behaviors.Core;
  10. using Org.BouncyCastle.Crypto.Engines;
  11. namespace DataLogistics;
  12. public class MainWindowViewModel : INotifyPropertyChanged
  13. {
  14. private float _greenValue;
  15. private float _redValue;
  16. private float _blueValue;
  17. private float _dimmerValue;
  18. public ICommand OnOffCommand { get; set; }
  19. public float DimmerValue
  20. {
  21. get => _dimmerValue;
  22. set
  23. {
  24. _dimmerValue = value;
  25. HexValue = ComputeHex(DimmerValue, RedValue, GreenValue, BlueValue);
  26. OnPropertyChanged(nameof(HexValue));
  27. }
  28. }
  29. public bool LightOn { get; set; }
  30. public float RedValue
  31. {
  32. get => _redValue;
  33. set
  34. {
  35. _redValue = value;
  36. HexValue = ComputeHex(DimmerValue, RedValue, GreenValue, BlueValue);
  37. OnPropertyChanged(nameof(HexValue));
  38. }
  39. }
  40. public float GreenValue
  41. {
  42. get => _greenValue;
  43. set
  44. {
  45. _greenValue = value;
  46. HexValue = ComputeHex(DimmerValue, RedValue, GreenValue, BlueValue);
  47. OnPropertyChanged(nameof(HexValue));
  48. }
  49. }
  50. public float BlueValue
  51. {
  52. get => _blueValue;
  53. set
  54. {
  55. _blueValue = value;
  56. HexValue = ComputeHex(DimmerValue, RedValue, GreenValue, BlueValue);
  57. OnPropertyChanged(nameof(HexValue));
  58. }
  59. }
  60. public string HexValue { get; set; }
  61. private RpcClientPipeTransport Transport;
  62. public MainWindowViewModel()
  63. {
  64. Transport = new RpcClientPipeTransport("SwitchTransport");
  65. Transport.Connect();
  66. OnOffCommand = new ActionCommand(OnOffSwitch);
  67. DimmerValue = 100;
  68. RedValue = 100;
  69. GreenValue = 0;
  70. BlueValue = 180;
  71. HexValue = ComputeHex(DimmerValue, RedValue, GreenValue, BlueValue);
  72. }
  73. public void OnOffSwitch()
  74. {
  75. try
  76. {
  77. var p = new SwitchParameters();
  78. p.Active = !LightOn;
  79. var result = Transport.Send<SwitchCommand, SwitchParameters, SwitchResult>(p);
  80. LightOn = result.LightOn;
  81. }
  82. catch (Exception e)
  83. {
  84. }
  85. }
  86. public string ComputeHex(float value, float red, float green, float blue)
  87. {
  88. int Red = Convert.ToInt32(red * (1 - (value / 255)));
  89. int Green = Convert.ToInt32(green * (1 - (value / 255)));
  90. int Blue = Convert.ToInt32(blue * (1 - (value / 255)));
  91. string RedHexFinal = Red.ToString("X");
  92. string GreenHexFinal = Green.ToString("X");
  93. string BlueHexFinal = Blue.ToString("X");
  94. if (RedHexFinal.Length == 1) RedHexFinal = "0" + RedHexFinal;
  95. if (GreenHexFinal.Length == 1) GreenHexFinal = "0" + GreenHexFinal;
  96. if (BlueHexFinal.Length == 1) BlueHexFinal = "0" + BlueHexFinal;
  97. string FinalHex = "#" + RedHexFinal + GreenHexFinal + BlueHexFinal;
  98. var p = new ColourParameters();
  99. p.Hex = FinalHex;
  100. var result = Transport.Send<ColourCommand, ColourParameters, ColourResult>(p);
  101. FinalHex = result.HexValue;
  102. return FinalHex;
  103. }
  104. public event PropertyChangedEventHandler? PropertyChanged;
  105. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  106. {
  107. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  108. }
  109. protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
  110. {
  111. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  112. field = value;
  113. OnPropertyChanged(propertyName);
  114. return true;
  115. }
  116. }