diff --git a/HueLib2/Bridge/Bridge.cs b/HueLib2/Bridge/Bridge.cs index 79f4b791..f9a86a81 100644 --- a/HueLib2/Bridge/Bridge.cs +++ b/HueLib2/Bridge/Bridge.cs @@ -18,11 +18,11 @@ public partial class Bridge : INotifyPropertyChanged MessageCollection _lastmessages; private string _apiversion = string.Empty; private string _mac = string.Empty; - private readonly Error _bridgeNotResponding = new Error() { address = "none", description = "Bridge is not responding", type = -999 }; private string _swversion; private bool _isdefault = false; private IPAddress _ipAddress; private string _name; + private readonly Error _bridgeNotResponding; /// /// Api Key to access the bridge. If the application is not autorized the api key will not be set. @@ -97,7 +97,7 @@ public IPAddress IpAddress { _ipAddress = value; OnPropertyChanged(); - + _bridgeNotResponding.address = _ipAddress.ToString(); } } @@ -133,6 +133,7 @@ public Bridge() _ipAddress = IPAddress.None; _apiKey = string.Empty; _lastmessages = new MessageCollection(); + _bridgeNotResponding = new Error() { address = $"{_ipAddress}", description = "Bridge is not responding", type = -999 }; } /// @@ -153,6 +154,7 @@ public Bridge(IPAddress ip, string mac, string apiversion, string swversion,stri _name = name; OnPropertyChanged("Mac"); OnPropertyChanged("ApiVersion"); + _bridgeNotResponding = new Error() { address = $"{_ipAddress}", description = "Bridge is not responding", type = -999 }; } /// diff --git a/HueLib2/Bridge/Bridge_ObjectSetter.cs b/HueLib2/Bridge/Bridge_ObjectSetter.cs index ae0b61a1..46729012 100644 --- a/HueLib2/Bridge/Bridge_ObjectSetter.cs +++ b/HueLib2/Bridge/Bridge_ObjectSetter.cs @@ -274,6 +274,29 @@ public CommandResult ChangeSensorConfig(string id,SensorConfig newconfig) return bresult; } + /// + /// Change the sensor's state. + /// + /// id of the sensor + /// New state of the sensor + /// BridgeCommResult + public CommandResult ChangeSensorState(string id, SensorState newstate) + { + CommandResult bresult = new CommandResult(); + CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + $@"/sensors/{id}/state"),WebRequestType.PUT, Serializer.SerializeToJson(newstate)); + if (comres.status == WebExceptionStatus.Success) + { + lastMessages = new MessageCollection(Serializer.DeserializeToObject>(comres.data)); + bresult.Success = lastMessages.FailureCount == 0; + bresult.resultobject = lastMessages; + } + else + { + bresult.resultobject = comres.data; + } + + return bresult; + } /// /// Set to null all properties that are not allow to be set at modification. diff --git a/HueLib2/Communication/WebClientTimeout.cs b/HueLib2/Communication/WebClientTimeout.cs index 36b10f78..ea56d9ca 100644 --- a/HueLib2/Communication/WebClientTimeout.cs +++ b/HueLib2/Communication/WebClientTimeout.cs @@ -1,7 +1,10 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; @@ -27,5 +30,8 @@ protected override WebRequest GetWebRequest(Uri address) } return request; } + } + + } diff --git a/WinHue3/Addons/CpuTempMon/CpuTemp.cs b/WinHue3/Addons/CpuTempMon/CpuTemp.cs new file mode 100644 index 00000000..f18652f8 --- /dev/null +++ b/WinHue3/Addons/CpuTempMon/CpuTemp.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Management; +using OpenHardwareMonitor; +using OpenHardwareMonitor.Hardware; +using OpenHardwareMonitor.Collections; +using System.Windows.Threading; +using System.ComponentModel; + +namespace WinHue3 +{ + public class CpuTemp + { + public float? temperature + { + get; + private set; + } + + public string pollSensorName; + + public List cpuSensors + { + get; + private set; + } + + private readonly DispatcherTimer timer; + private readonly Computer comp; + private readonly BackgroundWorker bgwOpen; + private bool _notWorking = false; + + public bool Working + { + get{ return _notWorking;} + private set{ _notWorking = value;} + } + + public CpuTemp(int pollingInterval) + { + + comp = new Computer(); + timer = new DispatcherTimer(); + timer.Tick += timer_Tick; + timer.Interval = new TimeSpan(0, 0, pollingInterval); + comp.CPUEnabled = true; + + bgwOpen = new BackgroundWorker(); + bgwOpen.DoWork += bgwOpen_DoWork; + try + { + comp.Open(); + UpdateSensors(); + comp.Close(); + Working = true; + } + catch (Exception) + { + Working = false; + } + + pollSensorName = "CPU Package"; + } + + void bgwOpen_DoWork(object sender, DoWorkEventArgs e) + { + try + { + comp.Open(); + timer.Start(); + Working = true; + } + catch(Exception) + { + Working = false; + } + + + } + + public void Start() + { + if (!timer.IsEnabled && Working) + { + bgwOpen.RunWorkerAsync(); + } + } + + public void Stop() + { + if (!timer.IsEnabled || !Working) return; + timer.Stop(); + comp.Close(); + } + + private void timer_Tick(object sender, EventArgs e) + { + UpdateSensors(); + int cpuPackage = cpuSensors.FindIndex(sensor => sensor.Name == pollSensorName); + if (cpuPackage == -1) + cpuPackage = 0; + + temperature = cpuSensors[cpuPackage].Value; + OnTempUpdated?.Invoke(this, new CpuTempEventArgs() { currentTemp= temperature}); + } + + public event CpuTempEvent OnTempUpdated; + public delegate void CpuTempEvent(object sender, CpuTempEventArgs e); + public event CpuSensorEvent OnSensorUpdated; + public delegate void CpuSensorEvent(object sender, EventArgs e); + + private void UpdateSensors() + { + foreach (IHardware hardware in comp.Hardware) + { + if (hardware.HardwareType != HardwareType.CPU) continue; + + hardware.Update(); + + cpuSensors = hardware.Sensors.ToList(); + cpuSensors.RemoveAll(sens => sens.SensorType != SensorType.Temperature); + } + OnSensorUpdated?.Invoke(this, new EventArgs()); + } + } + + public class CpuTempEventArgs : EventArgs + { + public float? currentTemp; + } +} diff --git a/WinHue3/Addons/CpuTempMon/CpuTempMonitor.cs b/WinHue3/Addons/CpuTempMon/CpuTempMonitor.cs new file mode 100644 index 00000000..88345d8d --- /dev/null +++ b/WinHue3/Addons/CpuTempMon/CpuTempMonitor.cs @@ -0,0 +1,163 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using HueLib2; + +namespace WinHue3 +{ + + public class CpuTempMonitor : View + { + /// + /// Name of the plugin. + /// + private const string PluginName = "Cpu Temp Monitor"; + + /// + /// Description of the plugin. + /// + private const string PluginDesc = "Adjust a light or group of light depending on the temperature of your cpu."; + + /// + /// Author of the plugin. + /// + private const string PluginAuth = "Pascal Pharand"; + + CpuTemp _temp; + + double _gradientStartColor; + double _gradientStopColor; + double _gradientStopTemp; + double _gradientStartTemp; + string _objectId; + bool _objectType; + byte _userBri; + byte _userSat; + private bool _isrunning; + + public CpuTempMonitor() + { + _temp = new CpuTemp(1); + _temp.OnTempUpdated += temp_OnTempUpdated; + LoadSettings(); + } + + private void LoadSettings() + { + _gradientStartColor = Properties.Settings.Default.CPUTemp_gradientStartColor; + _gradientStartTemp = Properties.Settings.Default.CPUTemp_gradientStartTemp; + _gradientStopColor = Properties.Settings.Default.CPUTemp_gradientStopColor; + _gradientStopTemp = Properties.Settings.Default.CPUTemp_gradientStopTemp; + _objectId = Properties.Settings.Default.CPUTemp_ObjectID; + _objectType = Properties.Settings.Default.CPUTemp_ObjectType; + _userBri = Properties.Settings.Default.CpuTemp_Brightness; + _userSat = Properties.Settings.Default.CPUTemp_Saturation; + + } + + public bool IsRunning + { + get { return _isrunning;} + private set { _isrunning = value; OnPropertyChanged(); } + + } + + /// + /// Required for the interface to know what the plugin name is. + /// + public string pluginName => PluginName; + + /// + /// Required for the interface to know what the plugin description is. + /// + public string pluginDesc => PluginDesc; + + /// + /// Required for the interface to know what the plugin author is. + /// + public string pluginAuth => PluginAuth; + + /// + /// Required by the interface get the icon image. + /// + public Bitmap pluginIcon => Properties.Resources.cputemp; + + /// + /// Do the plugin work. + /// + public void Start() + { + _temp.Start(); + IsRunning = true; + } + + /// + /// Stop the plugin. + /// + public void Stop() + { + _temp.Stop(); + IsRunning = false; + } + + /// + /// Show the settings form for the plugin. + /// + /// True or false or null depending on what you want to return. + public bool? ShowSettingsForm() + { + _temp.Stop(); + Form_CpuTempMonitorSettings settings = new Form_CpuTempMonitorSettings(_temp) {Owner = Application.Current.MainWindow}; + _temp.OnTempUpdated -= temp_OnTempUpdated; + var result = settings.ShowDialog(); + _temp.OnTempUpdated += temp_OnTempUpdated; + if (result == true) + LoadSettings(); + return result; + } + + void temp_OnTempUpdated(object sender, CpuTempEventArgs e) + { + float? actualtemp = e.currentTemp; + ushort hueTemp = 0; + + if (actualtemp == null) + { + hueTemp = (ushort)_gradientStartColor; + } + else + { + + double gradientRange = _gradientStartColor - _gradientStopColor; + double tempRange = _gradientStopTemp - _gradientStartTemp; + + // Check for the order of the sliders. Is the first slider before the 2nd or the other way around. + if (gradientRange > 0) + { + double multiplier = _gradientStartColor / tempRange; + hueTemp = (ushort)(_gradientStartColor - (multiplier * (actualtemp - _gradientStartTemp))); + } + else + { + hueTemp = (ushort)((_gradientStopColor * actualtemp) / _gradientStopTemp); + } + + } + + if (_objectType == true) + { + BridgeStore.SelectedBridge.SetState(new State() { hue = hueTemp, bri = _userBri, sat = _userSat, @on = true, transitiontime = 9 },_objectId); + } + else + { + BridgeStore.SelectedBridge.SetState(new HueLib2.Action() { hue = hueTemp, bri = _userBri, sat = _userSat, @on = true, transitiontime = 9 },_objectId); + } + + } + } +} diff --git a/WinHue3/Addons/CpuTempMon/SettingsForm.xaml b/WinHue3/Addons/CpuTempMon/SettingsForm.xaml new file mode 100644 index 00000000..58dfb689 --- /dev/null +++ b/WinHue3/Addons/CpuTempMon/SettingsForm.xaml @@ -0,0 +1,73 @@ + + + + + + +