Skip to content
This repository has been archived by the owner on Jul 18, 2020. It is now read-only.

Commit

Permalink
Fixed WHC to Huelib 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyrules committed Sep 18, 2016
1 parent 5b28de7 commit 143ab18
Show file tree
Hide file tree
Showing 36 changed files with 2,483 additions and 220 deletions.
6 changes: 4 additions & 2 deletions HueLib2/Bridge/Bridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Api Key to access the bridge. If the application is not autorized the api key will not be set.
Expand Down Expand Up @@ -97,7 +97,7 @@ public IPAddress IpAddress
{
_ipAddress = value;
OnPropertyChanged();

_bridgeNotResponding.address = _ipAddress.ToString();
}
}

Expand Down Expand Up @@ -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 };
}

/// <summary>
Expand All @@ -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 };
}

/// <summary>
Expand Down
23 changes: 23 additions & 0 deletions HueLib2/Bridge/Bridge_ObjectSetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,29 @@ public CommandResult ChangeSensorConfig(string id,SensorConfig newconfig)
return bresult;
}

/// <summary>
/// Change the sensor's state.
/// </summary>
/// <param name="id">id of the sensor</param>
/// <param name="newstate">New state of the sensor</param>
/// <returns>BridgeCommResult</returns>
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<List<Message>>(comres.data));
bresult.Success = lastMessages.FailureCount == 0;
bresult.resultobject = lastMessages;
}
else
{
bresult.resultobject = comres.data;
}

return bresult;
}

/// <summary>
/// Set to null all properties that are not allow to be set at modification.
Expand Down
6 changes: 6 additions & 0 deletions HueLib2/Communication/WebClientTimeout.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -27,5 +30,8 @@ protected override WebRequest GetWebRequest(Uri address)
}
return request;
}

}


}
134 changes: 134 additions & 0 deletions WinHue3/Addons/CpuTempMon/CpuTemp.cs
Original file line number Diff line number Diff line change
@@ -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<ISensor> 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<ISensor>();
cpuSensors.RemoveAll(sens => sens.SensorType != SensorType.Temperature);
}
OnSensorUpdated?.Invoke(this, new EventArgs());
}
}

public class CpuTempEventArgs : EventArgs
{
public float? currentTemp;
}
}
163 changes: 163 additions & 0 deletions WinHue3/Addons/CpuTempMon/CpuTempMonitor.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Name of the plugin.
/// </summary>
private const string PluginName = "Cpu Temp Monitor";

/// <summary>
/// Description of the plugin.
/// </summary>
private const string PluginDesc = "Adjust a light or group of light depending on the temperature of your cpu.";

/// <summary>
/// Author of the plugin.
/// </summary>
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(); }

}

/// <summary>
/// Required for the interface to know what the plugin name is.
/// </summary>
public string pluginName => PluginName;

/// <summary>
/// Required for the interface to know what the plugin description is.
/// </summary>
public string pluginDesc => PluginDesc;

/// <summary>
/// Required for the interface to know what the plugin author is.
/// </summary>
public string pluginAuth => PluginAuth;

/// <summary>
/// Required by the interface get the icon image.
/// </summary>
public Bitmap pluginIcon => Properties.Resources.cputemp;

/// <summary>
/// Do the plugin work.
/// </summary>
public void Start()
{
_temp.Start();
IsRunning = true;
}

/// <summary>
/// Stop the plugin.
/// </summary>
public void Stop()
{
_temp.Stop();
IsRunning = false;
}

/// <summary>
/// Show the settings form for the plugin.
/// </summary>
/// <returns>True or false or null depending on what you want to return.</returns>
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<Light>(new State() { hue = hueTemp, bri = _userBri, sat = _userSat, @on = true, transitiontime = 9 },_objectId);
}
else
{
BridgeStore.SelectedBridge.SetState<Group>(new HueLib2.Action() { hue = hueTemp, bri = _userBri, sat = _userSat, @on = true, transitiontime = 9 },_objectId);
}

}
}
}
Loading

0 comments on commit 143ab18

Please sign in to comment.