MainWindowViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. OnOffCommand = new ActionCommand(OnOffSwitch);
  65. DimmerValue = 100;
  66. RedValue = 100;
  67. GreenValue = 0;
  68. BlueValue = 180;
  69. HexValue = ComputeHex(DimmerValue, RedValue, GreenValue, BlueValue);
  70. Transport = new RpcClientPipeTransport("SwitchTransport");
  71. Transport.OnOpen += TransportOpen;
  72. Transport.OnClose += TransportClose;
  73. Transport.OnException += TransportException;
  74. Transport.OnMessage += TransportMessage;
  75. Transport.Connect();
  76. }
  77. private void TransportMessage(IRpcTransport transport, RpcTransportMessageArgs e)
  78. {
  79. }
  80. private void TransportException(IRpcTransport transport, RpcTransportExceptionArgs e)
  81. {
  82. }
  83. private void TransportClose(IRpcTransport transport, RpcTransportCloseArgs e)
  84. {
  85. }
  86. private void TransportOpen(IRpcTransport transport, RpcTransportOpenArgs e)
  87. {
  88. }
  89. public void OnOffSwitch()
  90. {
  91. try
  92. {
  93. var p = new SwitchParameters();
  94. p.Active = !LightOn;
  95. var result = Transport.Send<SwitchCommand, SwitchParameters, SwitchResult>(p);
  96. LightOn = result.LightOn;
  97. }
  98. catch (Exception e)
  99. {
  100. }
  101. }
  102. public string ComputeHex(float value, float red, float green, float blue)
  103. {
  104. int Red = Convert.ToInt32(red * (1 - (value / 255)));
  105. int Green = Convert.ToInt32(green * (1 - (value / 255)));
  106. int Blue = Convert.ToInt32(blue * (1 - (value / 255)));
  107. string RedHexFinal = Red.ToString("X");
  108. string GreenHexFinal = Green.ToString("X");
  109. string BlueHexFinal = Blue.ToString("X");
  110. if (RedHexFinal.Length == 1) RedHexFinal = "0" + RedHexFinal;
  111. if (GreenHexFinal.Length == 1) GreenHexFinal = "0" + GreenHexFinal;
  112. if (BlueHexFinal.Length == 1) BlueHexFinal = "0" + BlueHexFinal;
  113. return "#" + RedHexFinal + GreenHexFinal + BlueHexFinal;
  114. }
  115. public event PropertyChangedEventHandler? PropertyChanged;
  116. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  117. {
  118. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  119. }
  120. protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
  121. {
  122. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  123. field = value;
  124. OnPropertyChanged(propertyName);
  125. return true;
  126. }
  127. }