genielabs / HomeGenie

HomeGenie, the programmable automation intelligence
https://homegenie.it
GNU General Public License v3.0
393 stars 155 forks source link

Issue in precision handling of ExtractValueFromBytes in Generic/Sensor.cs #97

Closed hybridview closed 9 years ago

hybridview commented 9 years ago

Another user and I were having trouble with some inflated Generic sensor values that have precision > 0. I tracked down the issue as shown below.

In the source file: HomeGenie\MigFiles\SupportLibraries\ZWaveLib\Devices\ProductHandlers\Generic\Sensor.cs in function ExtractValueFromBytes, the code that handles precision has a flaw. It multiplies precision by 10 instead of using 10^precision power. In a case where precision is 3, the function multiplies the value by 10*3=30 instead of 10^3=1000. When I modified the code to fix the issue, all of my generics are perfect! The fix involves changing the line of code from:

result = ((double)value / (precision == 0 ? 1 : 10D * precision )); TO result = ((double)value / (precision == 0 ? 1 : Math.Pow(10D, precision) ));

This will fix values for parameters like: ZWaveNode.MultiInstance.SensorBinary.n Sensor.Generic