Skip to content

Commit

Permalink
- Fixed temperature reading Fahrenheit/Celsius conversion bug in ZWav…
Browse files Browse the repository at this point in the history
…eLib when value precision == 2
  • Loading branch information
genemars committed Feb 6, 2015
1 parent 7da0eab commit 98e335b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
3 changes: 2 additions & 1 deletion HISTORY.TXT
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
RELEASE HISTORY

06/02/2015 1.00 beta rev 476-git1
06/02/2015 1.00 beta rev 476-git2
- Fixed temperature reading Fahrenheit/Celsius conversion bug in ZWaveLib when value precision == 2
- Trying to fix Thermostat.SetPointGet and WakeUp app

05/02/2015 1.00 beta rev 476
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,53 +129,52 @@ public virtual bool HandleMultiInstanceReport(byte[] message)
paramType = ParameterType.MULTIINSTANCE_SENSOR_MULTILEVEL;
}
// we assume its a COMMAND_MULTIINSTANCE_REPORT
int scale = 0;
byte key = message[12];
double val = Utility.ExtractValueFromBytes(message, 14, out scale);
ZWaveValue zvalue = Utility.ExtractValueFromBytes(message, 14);

// if it's a COMMAND_MULTIINSTANCEV2_ENCAP we shift key and val +1 byte
if (cmdType == (byte)Command.MultiInstaceV2Encapsulated)
{
key = message[13];
val = Utility.ExtractValueFromBytes(message, 15, out scale);
zvalue = Utility.ExtractValueFromBytes(message, 15);
}
//
if (key == (byte)ZWaveSensorParameter.TEMPERATURE && message.Length > 16)
{
if (cmdType == (byte)Command.MultiInstaceV2Encapsulated && message.Length > 18)
{
val = BitConverter.ToUInt16(new byte[2] { message[18], message[17] }, 0) / 100D;
zvalue.Value = BitConverter.ToUInt16(new byte[2] { message[18], message[17] }, 0) / 100D;
}
else
{
val = Utility.ExtractTemperatureFromBytes(message);
zvalue.Value = Utility.ExtractTemperatureFromBytes(message);
}
//
nodeHost.RaiseUpdateParameterEvent(nodeHost, instance, paramType, val);
nodeHost.RaiseUpdateParameterEvent(nodeHost, instance, paramType, zvalue);
nodeHost.RaiseUpdateParameterEvent(
nodeHost,
key,
ParameterType.SENSOR_TEMPERATURE,
val
zvalue
);
processed = true;
}
else if (key == (byte)ZWaveSensorParameter.GENERAL_PURPOSE_VALUE)
{
nodeHost.RaiseUpdateParameterEvent(nodeHost, instance, paramType, (double)val);
nodeHost.RaiseUpdateParameterEvent(nodeHost, key, ParameterType.GENERIC, val);
nodeHost.RaiseUpdateParameterEvent(nodeHost, instance, paramType, zvalue.Value);
nodeHost.RaiseUpdateParameterEvent(nodeHost, key, ParameterType.GENERIC, zvalue);
processed = true;
}
else if (key == (byte)ZWaveSensorParameter.LUMINANCE)
{
nodeHost.RaiseUpdateParameterEvent(nodeHost, instance, paramType, (double)val);
nodeHost.RaiseUpdateParameterEvent(nodeHost, key, ParameterType.SENSOR_LUMINANCE, val);
nodeHost.RaiseUpdateParameterEvent(nodeHost, instance, paramType, zvalue.Value);
nodeHost.RaiseUpdateParameterEvent(nodeHost, key, ParameterType.SENSOR_LUMINANCE, zvalue);
processed = true;
}
else if (key == (byte)ZWaveSensorParameter.RELATIVE_HUMIDITY)
{
nodeHost.RaiseUpdateParameterEvent(nodeHost, instance, paramType, (double)val);
nodeHost.RaiseUpdateParameterEvent(nodeHost, key, ParameterType.SENSOR_HUMIDITY, val);
nodeHost.RaiseUpdateParameterEvent(nodeHost, instance, paramType, zvalue.Value);
nodeHost.RaiseUpdateParameterEvent(nodeHost, key, ParameterType.SENSOR_HUMIDITY, zvalue);
processed = true;
}
else if (key == (byte)ZWaveSensorParameter.POWER)
Expand Down Expand Up @@ -205,8 +204,8 @@ public virtual bool HandleMultiInstanceReport(byte[] message)
}
else
{
nodeHost.RaiseUpdateParameterEvent(nodeHost, instance, paramType, (double)val);
nodeHost.RaiseUpdateParameterEvent(nodeHost, key, ParameterType.GENERIC, val);
nodeHost.RaiseUpdateParameterEvent(nodeHost, instance, paramType, zvalue.Value);
nodeHost.RaiseUpdateParameterEvent(nodeHost, key, ParameterType.GENERIC, zvalue);
Console.WriteLine("\nUNHANDLED SENSOR PARAMETER TYPE => " + key + "\n");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static EnergyValue Parse(byte[] message)
// message[12].ToString("X2") + message[13].ToString("X2") + message[14].ToString("X2"),
// System.Globalization.NumberStyles.HexNumber
// )) / 1000D;
energy.Value = Utility.ExtractValueFromBytes(message, 11, out scale);
energy.Value = Utility.ExtractValueFromBytes(message, 11).Value;
if (Enum.IsDefined(typeof(ZWaveEnergyParameter), scale))
{
energy.Parameter = (ZWaveEnergyParameter)scale;
Expand Down
11 changes: 5 additions & 6 deletions MigFiles/SupportLibraries/ZWaveLib/Devices/Values/SensorValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public static SensorValue Parse(byte[] message)
{
SensorValue sensor = new SensorValue();
//
int scale = 0;
double val = Utility.ExtractValueFromBytes(message, 11, out scale);
ZWaveValue zvalue = Utility.ExtractValueFromBytes(message, 11);
//
byte key = message[9];
if (key == (byte)ZWaveSensorParameter.TEMPERATURE)
Expand All @@ -74,19 +73,19 @@ public static SensorValue Parse(byte[] message)
else if (key == (byte)ZWaveSensorParameter.GENERAL_PURPOSE_VALUE)
{
sensor.Parameter = ZWaveSensorParameter.GENERAL_PURPOSE_VALUE;
sensor.Value = val;
sensor.Value = zvalue.Value;
sensor.EventType = ParameterType.GENERIC;
}
else if (key == (byte)ZWaveSensorParameter.LUMINANCE)
{
sensor.Parameter = ZWaveSensorParameter.LUMINANCE;
sensor.Value = val;
sensor.Value = zvalue.Value;
sensor.EventType = ParameterType.SENSOR_LUMINANCE;
}
else if (key == (byte)ZWaveSensorParameter.RELATIVE_HUMIDITY)
{
sensor.Parameter = ZWaveSensorParameter.RELATIVE_HUMIDITY;
sensor.Value = val;
sensor.Value = zvalue.Value;
sensor.EventType = ParameterType.SENSOR_HUMIDITY;
}
else if (key == (byte)ZWaveSensorParameter.POWER)
Expand All @@ -104,7 +103,7 @@ public static SensorValue Parse(byte[] message)
}
else
{
sensor.Value = val;
sensor.Value = zvalue.Value;
}
//
return sensor;
Expand Down
35 changes: 21 additions & 14 deletions MigFiles/SupportLibraries/ZWaveLib/Devices/Values/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,39 @@ You should have received a copy of the GNU General Public License

namespace ZWaveLib.Devices.Values
{
public class ZWaveValue
{
public double Value;
public int Scale;
public int Precision;
public int Size;
}

public class Utility
{

public static double ExtractTemperatureFromBytes(byte[] message)
{
double temperature = 0;
int scale = 0;
byte[] tmp = new byte[4];
System.Array.Copy(message, message.Length - 4, tmp, 0, 4);
message = tmp;

temperature = ExtractValueFromBytes(message, 1, out scale);

// TODO: should use "scale" value returned from ExtractValueFromBytes
// 0x2A = Fahrenheit
// 0x22 = Celius
byte precisionScaleSize = message[0];
ZWaveValue zvalue = ExtractValueFromBytes(message, 1);
// zvalue.Scale == 1 -> Fahrenheit
// zvalue.Scale == 0 -> Celsius

// convert from Fahrenheit to Celsius
if (precisionScaleSize != 0x22) temperature = ((5.0 / 9.0) * (temperature - 32.0));
if (zvalue.Scale == 1) temperature = ((5.0 / 9.0) * (temperature - 32.0));

return temperature;
}

// adapted from:
// https://github.com/dcuddeback/open-zwave/blob/master/cpp/src/command_classes/CommandClass.cpp#L289
public static double ExtractValueFromBytes(byte[] message, int valueOffset, out int scale)
public static ZWaveValue ExtractValueFromBytes(byte[] message, int valueOffset)
{
double result = 0;
scale = 0;
ZWaveValue result = new ZWaveValue();
try
{
byte sizeMask = 0x07,
Expand All @@ -62,7 +65,11 @@ public static double ExtractValueFromBytes(byte[] message, int valueOffset, out
//
byte size = (byte)(message[valueOffset-1] & sizeMask);
byte precision = (byte)((message[valueOffset-1] & precisionMask) >> precisionShift);
scale = (int)((message[valueOffset-1] & scaleMask) >> scaleShift);
int scale = (int)((message[valueOffset-1] & scaleMask) >> scaleShift);
//
result.Size = size;
result.Precision = precision;
result.Scale = scale;
//
int value = 0;
byte i;
Expand All @@ -85,7 +92,7 @@ public static double ExtractValueFromBytes(byte[] message, int valueOffset, out
}
}
//
result = ((double)value / (precision == 0 ? 1 : Math.Pow(10D, precision) ));
result.Value = ((double)value / (precision == 0 ? 1 : Math.Pow(10D, precision) ));
} catch {
// TODO: report/handle exception
}
Expand Down

0 comments on commit 98e335b

Please sign in to comment.