Skip to content

Commit

Permalink
Updated all CommadClasses to support new message handling pipeline.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bounz committed Mar 15, 2015
1 parent 270e055 commit 4c91a38
Show file tree
Hide file tree
Showing 33 changed files with 863 additions and 531 deletions.
20 changes: 10 additions & 10 deletions MigFiles/MIG/Interfaces/HomeAutomation/ZWave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ public object InterfaceControl(MIGInterfaceCommand request)
else if (command == Command.THERMOSTAT_MODESET)
{
var node = controller.GetDevice((byte)int.Parse(nodeId));
Thermostat.Mode mode = (Thermostat.Mode)Enum.Parse(typeof(Thermostat.Mode), request.GetOption(0));
Mode mode = (Mode)Enum.Parse(typeof(Mode), request.GetOption(0));
//
raiseEvent = true;
eventParameter = "Thermostat.Mode";
Expand All @@ -609,13 +609,13 @@ public object InterfaceControl(MIGInterfaceCommand request)
else if (command == Command.THERMOSTAT_SETPOINTGET)
{
var node = controller.GetDevice((byte)int.Parse(nodeId));
Thermostat.SetPointType mode = (Thermostat.SetPointType)Enum.Parse(typeof(Thermostat.SetPointType), request.GetOption(0));
SetPointType mode = (SetPointType)Enum.Parse(typeof(SetPointType), request.GetOption(0));
Thermostat.GetSetPoint(node, mode);
}
else if (command == Command.THERMOSTAT_SETPOINTSET)
{
var node = controller.GetDevice((byte)int.Parse(nodeId));
Thermostat.SetPointType mode = (Thermostat.SetPointType)Enum.Parse(typeof(Thermostat.SetPointType), request.GetOption(0));
SetPointType mode = (SetPointType)Enum.Parse(typeof(SetPointType), request.GetOption(0));
double temperature = double.Parse(request.GetOption(1).Replace(',', '.'), CultureInfo.InvariantCulture);
//
raiseEvent = true;
Expand All @@ -632,7 +632,7 @@ public object InterfaceControl(MIGInterfaceCommand request)
else if (command == Command.THERMOSTAT_FANMODESET)
{
var node = controller.GetDevice((byte)int.Parse(nodeId));
Thermostat.FanMode mode = (Thermostat.FanMode)Enum.Parse(typeof(Thermostat.FanMode), request.GetOption(0));
FanMode mode = (FanMode)Enum.Parse(typeof(FanMode), request.GetOption(0));
//
raiseEvent = true;
eventParameter = "Thermostat.FanMode";
Expand All @@ -648,7 +648,7 @@ public object InterfaceControl(MIGInterfaceCommand request)
else if (command == Command.THERMOSTAT_OPERATINGSTATE_GET)
{
var node = controller.GetDevice((byte)int.Parse(nodeId));
Thermostat.GetOperatingState(node);
ThermostatOperatingState.GetOperatingState(node);
}
else if(command==Command.USERCODE_SET)
{
Expand Down Expand Up @@ -1067,19 +1067,19 @@ private void controller_UpdateNodeParameter(object sender, UpdateNodeParameterEv
break;
case EventParameter.ThermostatMode:
path = "Thermostat.Mode";
value = ((Thermostat.Mode)value).ToString();
value = ((Mode)value).ToString();
break;
case EventParameter.ThermostatOperatingState:
path = "Thermostat.OperatingState";
value = ((Thermostat.OperatingState)value).ToString();
value = ((OperatingState)value).ToString();
break;
case EventParameter.ThermostatFanMode:
path = "Thermostat.FanMode";
value = ((Thermostat.FanMode)value).ToString();
value = ((FanMode)value).ToString();
break;
case EventParameter.ThermostatFanState:
path = "Thermostat.FanState";
value = ((Thermostat.FanState)value).ToString();
value = ((FanState)value).ToString();
break;
case EventParameter.ThermostatHeating:
path = "Thermostat.Heating";
Expand All @@ -1088,7 +1088,7 @@ private void controller_UpdateNodeParameter(object sender, UpdateNodeParameterEv
path = "Thermostat.SetBack";
break;
case EventParameter.ThermostatSetPoint:
path = "Thermostat.SetPoint." + ((Thermostat.SetPointType)((dynamic)value).Type).ToString();
path = "Thermostat.SetPoint." + ((SetPointType)((dynamic)value).Type).ToString();
value = ((dynamic)value).Value;
break;
case EventParameter.UserCode:
Expand Down
15 changes: 7 additions & 8 deletions MigFiles/SupportLibraries/ZWaveLib/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

namespace ZWaveLib
{
public delegate void ControllerEventHandler(object source, ControllerEventArgs e);
public class ControllerEventArgs : EventArgs
{
public readonly byte NodeId;
Expand Down Expand Up @@ -626,13 +625,13 @@ private ZWaveNode CreateDevice(byte nodeId, byte genericClass)
ZWaveNode node;
switch (genericClass)
{
case (byte)GenericType.StaticController:
// TODO: this is very untested...
node = (ZWaveNode)new Controller(zwavePort);
break;
default: // generic node
node = new ZWaveNode(nodeId, zwavePort, genericClass);
break;
case (byte) GenericType.StaticController:
// TODO: this is very untested...
node = (ZWaveNode) new Controller(zwavePort);
break;
default: // generic node
node = new ZWaveNode(nodeId, zwavePort, genericClass);
break;
}
node.UpdateNodeParameter += znode_UpdateNodeParameter;
node.ManufacturerSpecificResponse += znode_ManufacturerSpecificResponse;
Expand Down
29 changes: 24 additions & 5 deletions MigFiles/SupportLibraries/ZWaveLib/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public enum CommandClass : byte
ThermostatOperatingState = 0x42,
ThermostatSetPoint = 0x43,
ThermostatFanMode = 0x44,
ThermostatFanState = 0x45,
ThermostatFanState = 0x45,
ThermostatSetBack = 0x47,
//
MultiInstance = 0x60,
Expand Down Expand Up @@ -181,14 +181,33 @@ public enum Command : byte
//
AlarmGet = 0x04,
AlarmReport = 0x05,
//

// MultiInstance commands
/// <summary>
/// 0x01
/// </summary>
MultiInstanceSet = 0x01,
/// <summary>
/// 0x02
/// </summary>
MultiInstanceGet = 0x02,
/// <summary>
/// 0x04
/// </summary>
MultiInstanceCountGet = 0x04,
/// <summary>
/// 0x05
/// </summary>
MultiInstanceCountReport = 0x05,
MultiInstanceReport = 0x06,
//
MultiInstaceV2Encapsulated = 0x0D,
/// <summary>
/// 0x06
/// </summary>
MultiInstanceEncapsulated = 0x06,
//
/// <summary>
/// 0x0D
/// </summary>
MultiChannelEncapsulated = 0x0D,
//
AssociationSet = 0x01,
AssociationGet = 0x02,
Expand Down
11 changes: 7 additions & 4 deletions MigFiles/SupportLibraries/ZWaveLib/Handlers/Alarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
* Project Homepage: http://homegenie.it
*/

using System;
using ZWaveLib.Values;

namespace ZWaveLib.Handlers
{
public static class Alarm
public class Alarm : ICommandClass
{
public byte GetCommandClassId()
{
return 0x71;
}

public static ZWaveEvent GetEvent(ZWaveNode node, byte[] message)
public ZWaveEvent GetEvent(ZWaveNode node, byte[] message)
{
ZWaveEvent nodeEvent = null;
byte cmdType = message[8];
byte cmdType = message[1];
if (cmdType == (byte)Command.AlarmReport)
{
var alarm = AlarmValue.Parse(message);
Expand Down
23 changes: 13 additions & 10 deletions MigFiles/SupportLibraries/ZWaveLib/Handlers/Association.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
* Project Homepage: http://homegenie.it
*/

using System;

namespace ZWaveLib.Handlers
{
public static class Association
public class Association : ICommandClass
{
public class AssociationResponse
{
Expand All @@ -34,19 +32,24 @@ public class AssociationResponse
public string NodeList = "";
}

public static ZWaveEvent GetEvent(ZWaveNode node, byte[] message)
public byte GetCommandClassId()
{
return 0x85;
}

public ZWaveEvent GetEvent(ZWaveNode node, byte[] message)
{
ZWaveEvent nodeEvent = null;
byte cmdType = message[8];
if (message.Length > 12 && cmdType == (byte)Command.AssociationReport)
byte cmdType = message[1];
if (message.Length > 5 && cmdType == (byte)Command.AssociationReport)
{
byte groupId = message[9];
byte maxAssociations = message[10];
byte numAssociations = message[11]; // it is always zero ?!?
byte maxAssociations = message[3];
byte numAssociations = message[4]; // it is always zero ?!?
string assocNodes = "";
if (message.Length > 13)
if (message.Length > 6)
{
for (int a = 12; a < message.Length - 1; a++)
for (int a = 5; a < message.Length - 1; a++)
{
assocNodes += message[a] + ",";
}
Expand Down
14 changes: 8 additions & 6 deletions MigFiles/SupportLibraries/ZWaveLib/Handlers/Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@
* Project Homepage: http://homegenie.it
*/

using System;

namespace ZWaveLib.Handlers
{
public static class Basic
public class Basic : ICommandClass
{
public byte GetCommandClassId()
{
return 0x20;
}

public static ZWaveEvent GetEvent(ZWaveNode node, byte[] message)
public ZWaveEvent GetEvent(ZWaveNode node, byte[] message)
{
ZWaveEvent nodeEvent = null;
byte cmdType = message[8];
byte cmdType = message[1];
if (cmdType == (byte)Command.BasicReport || cmdType == (byte)Command.BasicSet)
{
int levelValue = (int)message[9];
int levelValue = (int)message[2];
nodeEvent = new ZWaveEvent(node, EventParameter.Level, (double)levelValue, 0);
}
return nodeEvent;
Expand Down
17 changes: 10 additions & 7 deletions MigFiles/SupportLibraries/ZWaveLib/Handlers/Battery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@
* Project Homepage: http://homegenie.it
*/

using System;

namespace ZWaveLib.Handlers
{
public static class Battery
public class Battery : ICommandClass
{
public static ZWaveEvent GetEvent(ZWaveNode node, byte[] message)
public byte GetCommandClassId()
{
return 0x80;
}

public ZWaveEvent GetEvent(ZWaveNode node, byte[] message)
{
ZWaveEvent nodeEvent = null;
byte cmdType = message[8];
if (message.Length > 7 && cmdType == (byte)Command.BatteryReport) // Battery Report
byte cmdType = message[1];
if (message.Length > 0 && cmdType == (byte)Command.BatteryReport) // Battery Report
{
int batteryLevel = message[9];
int batteryLevel = message[2];
nodeEvent = new ZWaveEvent(node, EventParameter.Battery, batteryLevel, 0);
}
return nodeEvent;
Expand Down
97 changes: 55 additions & 42 deletions MigFiles/SupportLibraries/ZWaveLib/Handlers/CommandClassFactory.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ZWaveLib.Handlers
{
class CommandClassFactory
{
public static ICommandClass GetCommandClass(byte ccId)
{
if(_commandClasses == null)
_commandClasses = CollectCommandClasses();

Type type;
if (!_commandClasses.TryGetValue(ccId, out type))
return null;

var cc = (ICommandClass)Activator.CreateInstance(type);
return cc;
}

private static Dictionary<byte, Type> _commandClasses;

private static Dictionary<byte, Type> CollectCommandClasses()
{
var iCommandClassTypes = new Dictionary<byte, Type>();
var iType = typeof(ICommandClass);
var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => iType.IsAssignableFrom(t));

foreach (var type in types)
{
var cc = (ICommandClass)Activator.CreateInstance(type);
var id = cc.GetCommandClassId();
_commandClasses.Add(id, type);
}

return iCommandClassTypes;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace ZWaveLib.Handlers
{
class CommandClassFactory
{
static readonly object lck = new object();

public static ICommandClass GetCommandClass(byte ccId)
{
if (_commandClasses == null)
{
lock (lck)
{
if (_commandClasses == null)
{
_commandClasses = CollectCommandClasses();
}
}
}

Type type;
if (!_commandClasses.TryGetValue(ccId, out type))
return null;

var cc = (ICommandClass)Activator.CreateInstance(type);
return cc;
}

private static Dictionary<byte, Type> _commandClasses;

private static Dictionary<byte, Type> CollectCommandClasses()
{
var iCommandClassTypes = new Dictionary<byte, Type>();
var iType = typeof (ICommandClass);

var assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();
var types = assemblyTypes.Where(t => iType.IsAssignableFrom(t)).ToList();

foreach (var type in types)
{
if (type == iType)
continue; // we are not going to use interface itself
var cc = (ICommandClass) Activator.CreateInstance(type);
var id = cc.GetCommandClassId();
iCommandClassTypes.Add(id, type);
}

return iCommandClassTypes;
}
}
}

0 comments on commit 4c91a38

Please sign in to comment.