Exemple #1
0
        public static EnergyValue Parse(byte[] message)
        {
            ZWaveValue  zvalue = ZWaveValue.ExtractValueFromBytes(message, 11);
            EnergyValue energy = new EnergyValue();

            //energy.Value = ((double)int.Parse(
            //                       message[12].ToString("X2") + message[13].ToString("X2") + message[14].ToString("X2"),
            //                       System.Globalization.NumberStyles.HexNumber
            //                   )) / 1000D;
            energy.Value = zvalue.Value;
            if (Enum.IsDefined(typeof(ZWaveEnergyScaleType), zvalue.Scale))
            {
                energy.Parameter = (ZWaveEnergyScaleType)zvalue.Scale;
            }
            switch (energy.Parameter)
            {
            // Accumulated power consumption kW/h
            case ZWaveEnergyScaleType.kWh:
                energy.EventType = EventParameter.MeterKwHour;
                break;

            // Accumulated power consumption kilo Volt Ampere / hours (kVA/h)
            case ZWaveEnergyScaleType.kVAh:
                energy.EventType = EventParameter.MeterKvaHour;
                break;

            // Instant power consumption Watt
            case ZWaveEnergyScaleType.Watt:
                energy.EventType = EventParameter.MeterWatt;
                break;

            // Pulses count
            case ZWaveEnergyScaleType.Pulses:
                energy.EventType = EventParameter.MeterPulses;
                break;

            // AC load Voltage
            case ZWaveEnergyScaleType.ACVolt:
                energy.EventType = EventParameter.MeterAcVolt;
                break;

            // AC load Current
            case ZWaveEnergyScaleType.ACCurrent:
                energy.EventType = EventParameter.MeterAcCurrent;
                break;

            // Power Factor
            case ZWaveEnergyScaleType.PowerFactor:
                energy.EventType = EventParameter.MeterPower;
                break;

            default:
                energy.EventType = EventParameter.MeterWatt;
                break;
            }
            return(energy);
        }
Exemple #2
0
        public static SensorValue Parse(byte[] message)
        {
            SensorValue sensor = new SensorValue();
            //
            ZWaveValue zvalue = ZWaveValue.ExtractValueFromBytes(message, 11);
            //
            byte key = message[9];

            if (key == (byte)ZWaveSensorParameter.Temperature)
            {
                sensor.Parameter = ZWaveSensorParameter.Temperature;
                // convert from Fahrenheit to Celsius if needed
                sensor.Value     = (zvalue.Scale == (int)ZWaveTemperatureScaleType.Fahrenheit ? SensorValue.FahrenheitToCelsius(zvalue.Value) : zvalue.Value);
                sensor.EventType = EventParameter.SensorTemperature;
            }
            else if (key == (byte)ZWaveSensorParameter.GeneralPurposeValue)
            {
                sensor.Parameter = ZWaveSensorParameter.GeneralPurposeValue;
                sensor.Value     = zvalue.Value;
                sensor.EventType = EventParameter.Generic;
            }
            else if (key == (byte)ZWaveSensorParameter.Luminance)
            {
                sensor.Parameter = ZWaveSensorParameter.Luminance;
                sensor.Value     = zvalue.Value;
                sensor.EventType = EventParameter.SensorLuminance;
            }
            else if (key == (byte)ZWaveSensorParameter.RelativeHumidity)
            {
                sensor.Parameter = ZWaveSensorParameter.RelativeHumidity;
                sensor.Value     = zvalue.Value;
                sensor.EventType = EventParameter.SensorHumidity;
            }
            else if (key == (byte)ZWaveSensorParameter.Power)
            {
                //sensor.Value = BitConverter.ToUInt16(new byte[2] { message[12], message[11] }, 0) / 10D;
                //sensor.Value = ((double)int.Parse(
                //    message[12].ToString("X2") + message[13].ToString("X2") + message[14].ToString("X2"),
                //    System.Globalization.NumberStyles.HexNumber
                //    )) / 1000D;
                // TODO: this might be very buggy.... to be tested
                EnergyValue energy = EnergyValue.Parse(message);
                sensor.Parameter = ZWaveSensorParameter.Power;
                sensor.Value     = energy.Value;
                sensor.EventType = EventParameter.MeterPower;
            }
            else
            {
                sensor.Value = zvalue.Value;
            }
            //
            return(sensor);
        }
        // adapted from:
        // https://github.com/dcuddeback/open-zwave/blob/master/cpp/src/command_classes/CommandClass.cpp#L289
        public static ZWaveValue ExtractValueFromBytes(byte[] message, int valueOffset)
        {
            ZWaveValue result = new ZWaveValue();

            try
            {
                byte size      = (byte)(message[valueOffset - 1] & SizeMask);
                byte precision = (byte)((message[valueOffset - 1] & PrecisionMask) >> PrecisionShift);
                int  scale     = (int)((message[valueOffset - 1] & ScaleMask) >> ScaleShift);

                result.Size      = size;
                result.Precision = precision;
                result.Scale     = scale;

                int  value = 0;
                byte i;
                for (i = 0; i < size; ++i)
                {
                    value <<= 8;
                    value  |= (int)message[i + (int)valueOffset];
                }
                // Deal with sign extension. All values are signed
                if ((message[valueOffset] & 0x80) > 0)
                {
                    // MSB is signed
                    if (size == 1)
                    {
                        value = (int)((uint)value | 0xffffff00);
                    }
                    else if (size == 2)
                    {
                        value = (int)((uint)value | 0xffff0000);
                    }
                }
                //
                result.Value = ((double)value / (precision == 0 ? 1 : Math.Pow(10D, precision)));
            }
            catch
            {
                // TODO: report/handle exception
            }
            return(result);
        }
 // adapted from:
 // https://github.com/dcuddeback/open-zwave/blob/master/cpp/src/command_classes/CommandClass.cpp#L289
 public static ZWaveValue ExtractValueFromBytes(byte[] message, int valueOffset)
 {
     ZWaveValue result = new ZWaveValue();
     try
     {
         byte size = (byte)(message[valueOffset - 1] & sizeMask);
         byte precision = (byte)((message[valueOffset - 1] & precisionMask) >> precisionShift);
         int scale = (int)((message[valueOffset - 1] & scaleMask) >> scaleShift);
         //
         result.Size = size;
         result.Precision = precision;
         result.Scale = scale;
         //
         int value = 0;
         byte i;
         for (i = 0; i < size; ++i)
         {
             value <<= 8;
             value |= (int)message[i + (int)valueOffset];
         }
         // Deal with sign extension. All values are signed
         if ((message[valueOffset] & 0x80) > 0)
         {
             // MSB is signed
             if (size == 1)
             {
                 value = (int)((uint)value | 0xffffff00);
             }
             else if (size == 2)
             {
                 value = (int)((uint)value | 0xffff0000);
             }
         }
         //
         result.Value = ((double)value / (precision == 0 ? 1 : Math.Pow(10D, precision)));
     }
     catch
     {
         // TODO: report/handle exception
     }
     return result;
 }
        public static EnergyValue Parse(byte[] message)
        {
            ZWaveValue zvalue = ZWaveValue.ExtractValueFromBytes(message, 4);
            //int meterType = (message[1] & 0x1f);
            //Utility.logger.Warn("METER TYPE ({0})!", meterType);
            EnergyValue energy = new EnergyValue();

            energy.Value = zvalue.Value;
            if (Enum.IsDefined(typeof(ZWaveEnergyScaleType), zvalue.Scale))
            {
                energy.Parameter = (ZWaveEnergyScaleType)zvalue.Scale;
            }
            else
            {
                Utility.logger.Warn("Undefined Energy Meter Scale Type {0}!", zvalue.Scale);
            }


            // sometimes it appears this odd extra bit 0x80 (128) in message[2]
            // when scale type should be power factor it is interpreted as watt
            // (it happens with Qbino smart meter)
            // the following is a temporary work-around for that
            // TODO: find out the meaning of message[2] = 0xA1 (in place of 0x21)
            if ((message[2] & 0x80) == 0x80 && energy.Parameter == ZWaveEnergyScaleType.Watt)
            {
                energy.Parameter = ZWaveEnergyScaleType.PowerFactor;
            }


            switch (energy.Parameter)
            {
            // Accumulated power consumption kW/h
            case ZWaveEnergyScaleType.kWh:
                energy.EventType = EventParameter.MeterKwHour;
                break;

            // Accumulated power consumption kilo Volt Ampere / hours (kVA/h)
            case ZWaveEnergyScaleType.kVAh:
                energy.EventType = EventParameter.MeterKvaHour;
                break;

            // Instant power consumption Watt
            case ZWaveEnergyScaleType.Watt:
                energy.EventType = EventParameter.MeterWatt;
                break;

            // Pulses count
            case ZWaveEnergyScaleType.Pulses:
                energy.EventType = EventParameter.MeterPulses;
                break;

            // AC load Voltage
            case ZWaveEnergyScaleType.ACVolt:
                energy.EventType = EventParameter.MeterAcVolt;
                break;

            // AC load Current
            case ZWaveEnergyScaleType.ACCurrent:
                energy.EventType = EventParameter.MeterAcCurrent;
                break;

            // Power Factor
            case ZWaveEnergyScaleType.PowerFactor:
                energy.EventType = EventParameter.MeterPower;
                break;

            default:
                // Unknown value ?
                energy = null;
                break;
            }
            return(energy);
        }
Exemple #6
0
        public static SensorValue Parse(byte[] message)
        {
            var zvalue     = ZWaveValue.ExtractValueFromBytes(message, 4);
            var sensorType = ZWaveSensorParameter.Unknown;

            if (Enum.IsDefined(typeof(ZWaveSensorParameter), (int)message[2]))
            {
                sensorType = (ZWaveSensorParameter)message[2];
            }

            var sensorValue = new SensorValue
            {
                Parameter = sensorType,
                Value     = zvalue.Value
            };

            switch (sensorType)
            {
            case ZWaveSensorParameter.Temperature:
                // convert from Fahrenheit to Celsius if needed
                sensorValue.Value = zvalue.Scale == (int)ZWaveTemperatureScaleType.Fahrenheit
                        ? FahrenheitToCelsius(zvalue.Value)
                        : zvalue.Value;
                sensorValue.EventType = EventParameter.SensorTemperature;
                break;

            case ZWaveSensorParameter.GeneralPurposeValue:
                sensorValue.EventType = EventParameter.SensorGeneric;
                break;

            case ZWaveSensorParameter.Luminance:
                sensorValue.EventType = EventParameter.SensorLuminance;
                break;

            case ZWaveSensorParameter.RelativeHumidity:
                sensorValue.EventType = EventParameter.SensorHumidity;
                break;

            case ZWaveSensorParameter.Power:
                //sensor.Value = BitConverter.ToUInt16(new byte[2] { message[12], message[11] }, 0) / 10D;
                //sensor.Value = ((double)int.Parse(
                //    message[12].ToString("X2") + message[13].ToString("X2") + message[14].ToString("X2"),
                //    System.Globalization.NumberStyles.HexNumber
                //    )) / 1000D;
                // TODO: this might be very buggy.... to be tested
                EnergyValue energy = EnergyValue.Parse(message);
                sensorValue.Value     = energy.Value;
                sensorValue.EventType = EventParameter.MeterPower;
                break;

            case ZWaveSensorParameter.WaterFlow:
                sensorValue.EventType = EventParameter.WaterFlow;
                break;

            case ZWaveSensorParameter.WaterPressure:
                sensorValue.EventType = EventParameter.WaterPressure;
                break;

            case ZWaveSensorParameter.Ultraviolet:
                sensorValue.EventType = EventParameter.Ultraviolet;
                break;

                // TODO: implement other Sensor Types
            }

            return(sensorValue);
        }
Exemple #7
0
        public static EnergyValue Parse(byte[] message)
        {
            ZWaveValue  zvalue = ZWaveValue.ExtractValueFromBytes(message, 4);
            EnergyValue energy = new EnergyValue();

            energy.Value = zvalue.Value;
            if (Enum.IsDefined(typeof(ZWaveEnergyScaleType), zvalue.Scale))
            {
                energy.Parameter = (ZWaveEnergyScaleType)zvalue.Scale;
            }
            switch (energy.Parameter)
            {
            // Accumulated power consumption kW/h
            case ZWaveEnergyScaleType.kWh:
                //energy.EventType = EventParameter.MeterKwHour;
                // TODO: The following is fix for for AeonLabs HEM G2
                // TODO: https://github.com/genielabs/HomeGenie/pull/186
                // TODO: possibly move this fix to ZWaveValue.ExtractValueFromBytes method
                if ((message[2] & 0x80) == 0x80)
                {
                    energy.EventType = EventParameter.MeterAcVolt;
                }
                else
                {
                    energy.EventType = EventParameter.MeterKwHour;
                }
                break;

            // Accumulated power consumption kilo Volt Ampere / hours (kVA/h)
            case ZWaveEnergyScaleType.kVAh:
                energy.EventType = EventParameter.MeterKvaHour;
                break;

            // Instant power consumption Watt
            case ZWaveEnergyScaleType.Watt:
                energy.EventType = EventParameter.MeterWatt;
                break;

            // Pulses count
            case ZWaveEnergyScaleType.Pulses:
                energy.EventType = EventParameter.MeterPulses;
                break;

            // AC load Voltage
            case ZWaveEnergyScaleType.ACVolt:
                energy.EventType = EventParameter.MeterAcVolt;
                break;

            // AC load Current
            case ZWaveEnergyScaleType.ACCurrent:
                energy.EventType = EventParameter.MeterAcCurrent;
                break;

            // Power Factor
            case ZWaveEnergyScaleType.PowerFactor:
                energy.EventType = EventParameter.MeterPower;
                break;

            default:
                energy.EventType = EventParameter.MeterWatt;
                break;
            }
            return(energy);
        }