MainWindowViewModel.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 
  2. using System.ComponentModel;
  3. using System.Runtime.CompilerServices;
  4. using Command;
  5. using InABox.Core;
  6. using InABox.Rpc;
  7. namespace Receiver;
  8. public class MainWindowViewModel : INotifyPropertyChanged
  9. {
  10. private bool _lightOn;
  11. private string _lightHex;
  12. public bool LightOn
  13. {
  14. get => _lightOn;
  15. set
  16. {
  17. _lightOn = value;
  18. if (LightOn == false) LightVisibility = "Hidden";
  19. else LightVisibility = "Visible";
  20. OnPropertyChanged(nameof(LightVisibility));
  21. }
  22. }
  23. public string LightVisibility { get; set; }
  24. public string LightHex
  25. {
  26. get => _lightHex;
  27. set
  28. {
  29. _lightHex = value;
  30. OnPropertyChanged(nameof(LightHex));
  31. }
  32. }
  33. RpcServerPipeTransport Transport { get; set; }
  34. public MainWindowViewModel()
  35. {
  36. Transport = new RpcServerPipeTransport("SwitchTransport");
  37. Transport.AddHandler<MainWindowViewModel, SwitchCommand, SwitchParameters, SwitchResult>(new SwitchHandler(this));
  38. Transport.AddHandler<MainWindowViewModel, ColourCommand, ColourParameters, ColourResult>(new ColourHandler(this));
  39. Transport.Start();
  40. LightOn = true;
  41. LightHex = "#0000FF";
  42. if (LightOn == false) LightVisibility = "Hidden";
  43. else LightVisibility = "Visible";
  44. }
  45. public event PropertyChangedEventHandler? PropertyChanged;
  46. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  47. {
  48. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  49. }
  50. protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
  51. {
  52. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  53. field = value;
  54. OnPropertyChanged(propertyName);
  55. return true;
  56. }
  57. }
  58. public class SwitchHandler : RpcCommandHandler<MainWindowViewModel, SwitchCommand, SwitchParameters, SwitchResult>
  59. {
  60. public SwitchHandler(MainWindowViewModel sender) : base(sender)
  61. {
  62. }
  63. protected override SwitchResult Execute(IRpcSession session, SwitchParameters parameters, Logger logger)
  64. {
  65. Sender.LightOn = parameters.Active;
  66. var result = new SwitchResult();
  67. result.LightOn = parameters.Active;
  68. return result;
  69. }
  70. }
  71. public class ColourHandler : RpcCommandHandler<MainWindowViewModel, ColourCommand, ColourParameters, ColourResult>
  72. {
  73. public ColourHandler(MainWindowViewModel sender) : base(sender)
  74. {
  75. }
  76. protected override ColourResult Execute(IRpcSession session, ColourParameters parameters, Logger logger)
  77. {
  78. Sender.LightHex = parameters.Hex;
  79. var result = new ColourResult();
  80. result.HexValue = parameters.Hex;
  81. return result;
  82. }
  83. }