Skip to content

Commit

Permalink
Adding the House Mode (Get & Set)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienwarin committed Aug 27, 2018
1 parent 9d47b0f commit 69bfaeb
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 16 deletions.
21 changes: 21 additions & 0 deletions VeraNet/Objects/VeraHouseMode.cs
@@ -0,0 +1,21 @@
// -----------------------------------------------------------------------
// <copyright file="VeraHouseMode.cs" company="Sebastien.warin.Fr">
// Copyright 2018 - Sebastien.warin.fr
// </copyright>
// <author>Sebastien Warin</author>
// -----------------------------------------------------------------------

namespace VeraNet.Objects
{
/// <summary>
/// Represent the House's mode
/// </summary>
public enum VeraHouseMode
{
None = 0,
Home = 1,
Away = 2,
Night = 3,
Vacation = 4
}
}
2 changes: 1 addition & 1 deletion VeraNet/Properties/AssemblyInfo.cs
Expand Up @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Sebastien.warin.fr")]
[assembly: AssemblyProduct("VeraNet")]
[assembly: AssemblyCopyright("Copyright © 2012 Sebastien.warin.fr")]
[assembly: AssemblyCopyright("Copyright © 2012-2018 Sebastien.warin.fr")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down
69 changes: 56 additions & 13 deletions VeraNet/VeraController.cs
Expand Up @@ -22,6 +22,7 @@ namespace VeraNet
/// <summary>
/// Represent the Vera controller
/// </summary>
/// <seealso cref="System.IDisposable" />
public class VeraController : IDisposable
{
private const int REQUEST_INTERVAL_PAUSE_MS = 100;
Expand Down Expand Up @@ -53,6 +54,10 @@ public class VeraController : IDisposable
/// Occurs when scene is updated.
/// </summary>
public event EventHandler<SceneUpdatedEventArgs> SceneUpdated;
/// <summary>
/// Occurs when the house mode is changed.
/// </summary>
public event EventHandler<HouseModeChangedEventArgs> HouseModeChanged;

/// <summary>
/// Gets a value indicating whether this controller is listening for changes.
Expand Down Expand Up @@ -89,36 +94,36 @@ public class VeraController : IDisposable
/// <value>
/// The Vera serial number.
/// </value>
public string SerialNumber { get; set; }
public string SerialNumber { get; private set; }
/// <summary>
/// Gets or sets the Vera device version.
/// </summary>
/// <value>
/// The Vera device version.
/// </value>
public string Version { get; set; }
public string Version { get; private set; }
/// <summary>
/// Gets or sets the Vera model.
/// </summary>
/// <value>
/// The Vera model.
/// </value>
public string Model { get; set; }
public string Model { get; private set; }
/// <summary>
/// Gets or sets the temperature unit.
/// </summary>
/// <value>
/// The temperature unit.
/// </value>
public string TemperatureUnit { get; set; }
public string TemperatureUnit { get; private set; }

/// <summary>
/// Gets or sets the last update.
/// </summary>
/// <value>
/// The last update.
/// </value>
public DateTime LastUpdate { get; set; }
public DateTime LastUpdate { get; private set; }
/// <summary>
/// Gets the current load time.
/// </summary>
Expand All @@ -139,14 +144,21 @@ public class VeraController : IDisposable
/// <value>
/// The state of the current.
/// </value>
public VeraState CurrentState { get; set; }
public VeraState CurrentState { get; private set; }
/// <summary>
/// Gets or sets the current comment.
/// </summary>
/// <value>
/// The current comment.
/// </value>
public string CurrentComment { get; set; }
public string CurrentComment { get; private set; }
/// <summary>
/// Gets or sets the house's mode.
/// </summary>
/// <value>
/// The house's mode.
/// </value>
public VeraHouseMode HouseMode { get; private set; }

/// <summary>
/// Gets the sections.
Expand Down Expand Up @@ -312,7 +324,7 @@ public void WaitForFullRequest()
throw new Exception("Unable to perform a full request when the listener is running. Call StopListener() before !");
}
}

internal string GetWebResponse(string uri, bool throwException = false)
{
try
Expand Down Expand Up @@ -349,6 +361,7 @@ private void RequestVeraWorker()
try
{
this.RequestVera();
this.RequestHouseMode();
errorCount = 0;
Thread.Sleep(REQUEST_INTERVAL_PAUSE_MS);
}
Expand All @@ -370,6 +383,32 @@ private void RequestVeraWorker()
}
}

/// <summary>
/// Requests the current house mode.
/// </summary>
/// <returns></returns>
public VeraHouseMode RequestHouseMode()
{
VeraHouseMode houseMode = (VeraHouseMode)Convert.ToInt32(this.GetWebResponse(this.ConnectionInfo.ToString() + "/data_request?id=variableget&Variable=Mode"));
if (houseMode != this.HouseMode)
{
var eventArgs = new HouseModeChangedEventArgs { NewMode = houseMode, OldMode = this.HouseMode };
this.HouseMode = houseMode;
this.HouseModeChanged?.Invoke(this, eventArgs);
}
return houseMode;
}

/// <summary>
/// Sets the house mode.
/// </summary>
/// <param name="houseMode">The house mode.</param>
/// <returns></returns>
public bool SetHouseMode(VeraHouseMode houseMode)
{
return this.GetWebResponse(this.ConnectionInfo.ToString() + "/data_request?id=lu_action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=SetHouseMode&Mode=" + ((int)houseMode).ToString()).Contains("<OK>OK</OK>");
}

private void RequestVera()
{
// Generate URI and get HTTP response content
Expand Down Expand Up @@ -422,7 +461,7 @@ private void RequestVera()
}
if (jsonResponse.ContainsKey("state"))
{
this.CurrentState = StateUtils.GetStateFromCode(Convert.ToInt32(jsonResponse["state"]));
this.CurrentState = StateUtils.GetStateFromCode(Convert.ToInt32(jsonResponse["state"]));;
}
if (jsonResponse.ContainsKey("comment"))
{
Expand All @@ -436,6 +475,13 @@ private void RequestVera()
{
this.CurrentDataVersion = Convert.ToInt64(jsonResponse["dataversion"]);
}
if (jsonResponse.ContainsKey("mode"))
{
this.HouseMode = (VeraHouseMode)Convert.ToInt32(jsonResponse["mode"]);
}

// Update OK
this.LastUpdate = DateTime.Now;

// Raise DataReceived event
if (this.DataReceived != null)
Expand All @@ -448,11 +494,8 @@ private void RequestVera()
RawData = strResponse
});
}

// Update OK
this.LastUpdate = DateTime.Now;
}

private void LoadVeraObjects<TObject>(Dictionary<string, object> jsonValues, string jsonKey, ObservableCollection<TObject> listToLoad, Func<Dictionary<string, object>, TObject> createObject = null) where TObject : VeraBaseObject, new()
{
if (jsonValues.ContainsKey(jsonKey))
Expand Down
26 changes: 24 additions & 2 deletions VeraNet/VeraEventArgs.cs
Expand Up @@ -57,9 +57,31 @@ public SceneUpdatedEventArgs(Scene scene)
}

/// <summary>
/// Provides data when an error occcured.
/// Provides data when the house mode changes
/// </summary>
public class VeraErrorOccurredEventArgs : EventArgs
/// <seealso cref="System.EventArgs" />
public class HouseModeChangedEventArgs : EventArgs
{
/// <summary>
/// Gets or sets the new mode.
/// </summary>
/// <value>
/// The new mode.
/// </value>
public VeraHouseMode NewMode { get; set; }
/// <summary>
/// Gets or sets the old mode.
/// </summary>
/// <value>
/// The old mode.
/// </value>
public VeraHouseMode OldMode { get; set; }
}

/// <summary>
/// Provides data when an error occcured.
/// </summary>
public class VeraErrorOccurredEventArgs : EventArgs
{
/// <summary>
/// Gets or sets the exception.
Expand Down
1 change: 1 addition & 0 deletions VeraNet/VeraNet.csproj
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Objects\Devices\TemperatureSensor.cs" />
<Compile Include="Objects\Scene.cs" />
<Compile Include="Objects\Section.cs" />
<Compile Include="Objects\VeraHouseMode.cs" />
<Compile Include="Objects\VeraState.cs" />
<Compile Include="Objects\VeraBaseObject.cs" />
<Compile Include="Objects\VeraInteractiveObject.cs" />
Expand Down

0 comments on commit 69bfaeb

Please sign in to comment.