123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
-
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- using Command;
- using InABox.Core;
- using InABox.Rpc;
- namespace Receiver;
- public class MainWindowViewModel : INotifyPropertyChanged
- {
- private bool _lightOn;
- private string _lightHex;
- public bool LightOn
- {
- get => _lightOn;
- set
- {
- _lightOn = value;
- if (LightOn == false) LightVisibility = "Hidden";
- else LightVisibility = "Visible";
- OnPropertyChanged(nameof(LightVisibility));
- }
- }
- public string LightVisibility { get; set; }
- public string LightHex
- {
- get => _lightHex;
- set
- {
- _lightHex = value;
- OnPropertyChanged(nameof(LightHex));
- }
- }
- RpcServerPipeTransport Transport { get; set; }
- public MainWindowViewModel()
- {
- Transport = new RpcServerPipeTransport("SwitchTransport");
- Transport.AddHandler<MainWindowViewModel, SwitchCommand, SwitchParameters, SwitchResult>(new SwitchHandler(this));
- Transport.AddHandler<MainWindowViewModel, ColourCommand, ColourParameters, ColourResult>(new ColourHandler(this));
- Transport.Start();
-
- LightOn = true;
- LightHex = "#0000FF";
-
- if (LightOn == false) LightVisibility = "Hidden";
- else LightVisibility = "Visible";
- }
- public event PropertyChangedEventHandler? PropertyChanged;
- protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
- {
- if (EqualityComparer<T>.Default.Equals(field, value)) return false;
- field = value;
- OnPropertyChanged(propertyName);
- return true;
- }
- }
- public class SwitchHandler : RpcCommandHandler<MainWindowViewModel, SwitchCommand, SwitchParameters, SwitchResult>
- {
- public SwitchHandler(MainWindowViewModel sender) : base(sender)
- {
- }
- protected override SwitchResult Execute(IRpcSession session, SwitchParameters parameters, Logger logger)
- {
- Sender.LightOn = parameters.Active;
- var result = new SwitchResult();
- result.LightOn = parameters.Active;
- return result;
- }
- }
- public class ColourHandler : RpcCommandHandler<MainWindowViewModel, ColourCommand, ColourParameters, ColourResult>
- {
- public ColourHandler(MainWindowViewModel sender) : base(sender)
- {
- }
- protected override ColourResult Execute(IRpcSession session, ColourParameters parameters, Logger logger)
- {
- Sender.LightHex = parameters.Hex;
- var result = new ColourResult();
- result.HexValue = parameters.Hex;
- return result;
- }
- }
|