123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- using System.Windows.Input;
- using System.Xml;
- using Command;
- using GenHTTP.Engine;
- using InABox.Rpc;
- using Microsoft.Xaml.Behaviors.Core;
- using Org.BouncyCastle.Crypto.Engines;
- namespace DataLogistics;
- public class MainWindowViewModel : INotifyPropertyChanged
- {
- private float _greenValue;
- private float _redValue;
- private float _blueValue;
- private float _dimmerValue;
- public ICommand OnOffCommand { get; set; }
- public float DimmerValue
- {
- get => _dimmerValue;
- set
- {
- _dimmerValue = value;
- HexValue = ComputeHex(DimmerValue, RedValue, GreenValue, BlueValue);
- OnPropertyChanged(nameof(HexValue));
- }
- }
- public bool LightOn { get; set; }
- public float RedValue
- {
- get => _redValue;
- set
- {
- _redValue = value;
- HexValue = ComputeHex(DimmerValue, RedValue, GreenValue, BlueValue);
- OnPropertyChanged(nameof(HexValue));
- }
- }
- public float GreenValue
- {
- get => _greenValue;
- set
- {
- _greenValue = value;
- HexValue = ComputeHex(DimmerValue, RedValue, GreenValue, BlueValue);
- OnPropertyChanged(nameof(HexValue));
- }
- }
- public float BlueValue
- {
- get => _blueValue;
- set
- {
- _blueValue = value;
- HexValue = ComputeHex(DimmerValue, RedValue, GreenValue, BlueValue);
- OnPropertyChanged(nameof(HexValue));
- }
- }
- public string HexValue { get; set; }
- private RpcClientPipeTransport Transport;
- public MainWindowViewModel()
- {
- OnOffCommand = new ActionCommand(OnOffSwitch);
-
- DimmerValue = 100;
-
- RedValue = 100;
- GreenValue = 0;
- BlueValue = 180;
-
- HexValue = ComputeHex(DimmerValue, RedValue, GreenValue, BlueValue);
-
- Transport = new RpcClientPipeTransport("SwitchTransport");
- Transport.OnOpen += TransportOpen;
- Transport.OnClose += TransportClose;
- Transport.OnException += TransportException;
- Transport.OnMessage += TransportMessage;
- Transport.Connect();
- }
- private void TransportMessage(IRpcTransport transport, RpcTransportMessageArgs e)
- {
- }
- private void TransportException(IRpcTransport transport, RpcTransportExceptionArgs e)
- {
- }
- private void TransportClose(IRpcTransport transport, RpcTransportCloseArgs e)
- {
- }
- private void TransportOpen(IRpcTransport transport, RpcTransportOpenArgs e)
- {
- }
- public void OnOffSwitch()
- {
- try
- {
- var p = new SwitchParameters();
- p.Active = !LightOn;
- var result = Transport.Send<SwitchCommand, SwitchParameters, SwitchResult>(p);
- LightOn = result.LightOn;
- }
- catch (Exception e)
- {
-
- }
- }
-
- public string ComputeHex(float value, float red, float green, float blue)
- {
- int Red = Convert.ToInt32(red * (1 - (value / 255)));
- int Green = Convert.ToInt32(green * (1 - (value / 255)));
- int Blue = Convert.ToInt32(blue * (1 - (value / 255)));
-
- string RedHexFinal = Red.ToString("X");
- string GreenHexFinal = Green.ToString("X");
- string BlueHexFinal = Blue.ToString("X");
-
- if (RedHexFinal.Length == 1) RedHexFinal = "0" + RedHexFinal;
- if (GreenHexFinal.Length == 1) GreenHexFinal = "0" + GreenHexFinal;
- if (BlueHexFinal.Length == 1) BlueHexFinal = "0" + BlueHexFinal;
-
- return "#" + RedHexFinal + GreenHexFinal + BlueHexFinal;
- }
- 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;
- }
- }
|