Example #1
0
        internal void ProcessParameters(byte[] data, byte initialOffsetInData)
        {
            ParamValues.Clear();
            _cmdClassRef = null;
            _cmdRef      = null;
            int index = 0;

            if (CommandDefinition.Param == null)
            {
                return;
            }
            foreach (var item in CommandDefinition.Param)
            {
                ParamValue pv = ProcessParameter(data, ref index, item, null);
                if (item.Mode == ParamModes.VariantGroup && pv.InnerValues != null && pv.InnerValues.Count > 0)
                {
                    for (int i = 0; i < pv.InnerValues.Count; i++)
                    {
                        pv.InnerValues[i].ParamDefinitionTextSuffix = " " + (i + 1);
                        ParamValues.Add(pv.InnerValues[i]);
                    }
                }
                else
                {
                    ParamValues.Add(pv);
                }
            }
        }
        public static Command FindCommand(this ZWaveDefinition zWaveDefinition, CommandClass commandClass, byte key)
        {
            Command ret = null;

            if (commandClass.Command != null)
            {
                foreach (Command var in commandClass.Command)
                {
                    if (var.Bits > 0 && var.Bits < 8)
                    {
                        if (var.KeyId == (key & Tools.GetMaskFromBits(var.Bits, (byte)(8 - var.Bits))))
                        {
                            ret = var;
                            break;
                        }
                    }
                    else if (var.KeyId == key)
                    {
                        ret = var;
                        break;
                    }
                }
            }
            return(ret);
        }
Example #3
0
        public virtual CommandClass Clone(ISubscribeCollectionFactory iSubscribeCollectionFactory)
        {
            CommandClass ret = (CommandClass)MemberwiseClone();

            ret.Command = null;
            if (Command != null)
            {
                ret.Command = iSubscribeCollectionFactory.CreateCollection <Command>();
                foreach (var item in Command)
                {
                    Command c = item.Clone(iSubscribeCollectionFactory);
                    c.Parent = ret;
                    ret.Command.Add(c);
                }
            }

            ret.DefineSet = null;
            if (DefineSet != null)
            {
                ret.DefineSet = iSubscribeCollectionFactory.CreateCollection <DefineSet>();
                foreach (var item in DefineSet)
                {
                    DefineSet c = item.Clone(iSubscribeCollectionFactory);
                    c.Parent = ret;
                    ret.DefineSet.Add(c);
                }
            }
            return(ret);
        }
Example #4
0
        private ParamValue ProcessParameter(byte[] data, ref int index, Param item, IList <ParamValue> vgParams)
        {
            ParamValue ret = ParamValue.CreateParamValue(item);

            if (string.IsNullOrEmpty(item.OptionalReference) || IsIncluded(item.OptionalReference))
            {
                switch (item.Mode)
                {
                case ParamModes.VariantGroup:
                    ret.InnerValues = new List <ParamValue>();
                    if (item.Size > 1)
                    {
                        for (int i = 0; i < item.Size; i++)
                        {
                            ParamValue vg = ParamValue.CreateParamValue(item);
                            vg.InnerValues = new List <ParamValue>();
                            foreach (var prm1 in item.Param1)
                            {
                                ParamValue innerParam = ProcessParameter(data, ref index, prm1, vg.InnerValues);
                                vg.InnerValues.Add(innerParam);
                                vg.ByteValueList.AddRange(innerParam.ByteValueList);
                            }
                            ret.InnerValues.Add(vg);
                        }
                    }
                    else if (item.SizeReference != null && item.SizeReference == MsgLength)
                    {
                        while (data.Length - GetTailSize(CommandDefinition) > index)
                        {
                            ParamValue vg = ParamValue.CreateParamValue(item);
                            vg.InnerValues = new List <ParamValue>();
                            foreach (var prm1 in item.Param1)
                            {
                                ParamValue innerParam = ProcessParameter(data, ref index, prm1, vg.InnerValues);
                                vg.InnerValues.Add(innerParam);
                                vg.ByteValueList.AddRange(innerParam.ByteValueList);
                            }
                            ret.InnerValues.Add(vg);
                            if (!string.IsNullOrEmpty(item.MoreToFollowReference) && !vg.IsIncluded(item.MoreToFollowReference))
                            {
                                break;
                            }
                        }
                    }
                    else if (item.SizeReference != null && item.SizeReference == MsgMarker)
                    {
                        byte[] marker = GetMarkerValue(item);
                        while (data.Length - GetTailSize(CommandDefinition) > index && !IsMarker(data, index, marker))
                        {
                            ParamValue vg = ParamValue.CreateParamValue(item);
                            vg.InnerValues = new List <ParamValue>();
                            foreach (var prm1 in item.Param1)
                            {
                                ParamValue innerParam = ProcessParameter(data, ref index, prm1, vg.InnerValues);
                                vg.InnerValues.Add(innerParam);
                                vg.ByteValueList.AddRange(innerParam.ByteValueList);
                            }
                            ret.InnerValues.Add(vg);
                        }
                    }
                    else if (item.SizeReference != null)
                    {
                        for (int i = 0; i < GetSize(item.SizeReference, vgParams); i++)
                        {
                            ParamValue vg = ParamValue.CreateParamValue(item);
                            vg.InnerValues = new List <ParamValue>();
                            foreach (var prm1 in item.Param1)
                            {
                                ParamValue innerParam = ProcessParameter(data, ref index, prm1, vg.InnerValues);
                                vg.InnerValues.Add(innerParam);
                                vg.ByteValueList.AddRange(innerParam.ByteValueList);
                            }
                            ret.InnerValues.Add(vg);
                        }
                    }
                    break;

                case ParamModes.Property:
                    ret.InnerValues = new List <ParamValue>();
                    byte bvalue = data.Length > index ? data[index++] : (byte)0;
                    ret.ByteValueList.Add(bvalue);
                    byte totalbits = 0;
                    foreach (var prm1 in item.Param1)
                    {
                        ParamValue sp = ParamValue.CreateParamValue(prm1);
                        sp.ByteValueList.Add((byte)(bvalue >> totalbits & Tools.GetMaskFromBits(prm1.Bits, 0)));
                        ret.InnerValues.Add(sp);
                        totalbits += prm1.Bits;
                    }
                    break;

                default:
                    byte bytesCount = GetParamValueBytes(item);
                    if (item.Size > 1)
                    {
                        for (int i = 0; i < item.Size; i++)
                        {
                            for (int j = 0; j < bytesCount; j++)
                            {
                                if (data.Length > index)
                                {
                                    ret.ByteValueList.Add(data[index++]);
                                }
                            }
                        }
                    }
                    else if (item.SizeReference != null && item.SizeReference == MsgLength)
                    {
                        if (data.Length > 1000)
                        {
                        }
                        while (data.Length - GetTailSize(CommandDefinition) > index)
                        {
                            for (int j = 0; j < bytesCount; j++)
                            {
                                if (data.Length > index)
                                {
                                    ret.ByteValueList.Add(data[index++]);
                                }
                            }
                        }
                    }
                    else if (item.SizeReference != null && item.SizeReference == MsgMarker)
                    {
                        byte[] marker = GetMarkerValue(item);
                        while (data.Length - GetTailSize(CommandDefinition) > index && !IsMarker(data, index, marker))
                        {
                            for (int j = 0; j < bytesCount; j++)
                            {
                                if (data.Length > index)
                                {
                                    ret.ByteValueList.Add(data[index++]);
                                }
                            }
                        }
                    }
                    else if (item.SizeReference != null)
                    {
                        for (int i = 0; i < GetSize(item.SizeReference, vgParams) + item.SizeChange; i++)
                        {
                            for (int j = 0; j < bytesCount; j++)
                            {
                                if (data.Length > index)
                                {
                                    ret.ByteValueList.Add(data[index++]);
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int j = 0; j < bytesCount; j++)
                        {
                            if (data.Length > index)
                            {
                                ret.ByteValueList.Add(data[index++]);
                            }
                        }
                    }

                    if (item.Type == zwParamType.CMD_CLASS_REF && ret.ByteValueList != null && ret.ByteValueList.Count > 0)
                    {
                        _cmdClassRef = ZWaveDefinition.CommandClasses.FirstOrDefault(x => x.KeyId == ret.ByteValueList[0]);
                    }
                    else if (item.Type == zwParamType.CMD_REF && _cmdClassRef != null)
                    {
                        if (_cmdClassRef.Command != null && ret.ByteValueList != null && ret.ByteValueList.Count > 0)
                        {
                            _cmdRef = _cmdClassRef.Command.FirstOrDefault(x => x.KeyId == ret.ByteValueList[0]);
                        }
                    }
                    if (item.Type == zwParamType.CMD_DATA && _cmdClassRef != null && _cmdRef != null && ret.ByteValueList != null && ret.ByteValueList.Count > 0)
                    {
                        byte[] cmddata = new byte[ret.ByteValueList.Count + 2];
                        cmddata[0] = _cmdClassRef.KeyId;
                        cmddata[1] = _cmdRef.KeyId;
                        Array.Copy(ret.ByteValueList.ToArray(), 0, cmddata, 2, cmddata.Length - 2);
                        CommandClassValue[] encapCmds;

                        ZWaveDefinition.ParseApplicationObject(cmddata, out encapCmds);
                        ret.InnerValues = new List <ParamValue>();
                        if (encapCmds != null)
                        {
                            foreach (var ec in encapCmds)
                            {
                                if (encapCmds.Length > 1)
                                {
                                    ParamValue pv = new ParamValue
                                    {
                                        ParamDefinition           = item,
                                        ParamDefinitionTextSuffix = ", ver." + ec.CommandClassDefinition.Version,
                                        InnerValues = ec.CommandValue.ParamValues
                                    };
                                    ret.InnerValues.Add(pv);
                                }
                                else
                                {
                                    ret.InnerValues = ec.CommandValue.ParamValues;
                                }
                            }
                        }
                    }
                    if (item.Type == zwParamType.CMD_ENCAP && ret.ByteValueList != null && ret.ByteValueList.Count > 0)
                    {
                        byte[] cmddata = new byte[ret.ByteValueList.Count];
                        Array.Copy(ret.ByteValueList.ToArray(), 0, cmddata, 0, cmddata.Length);
                        CommandClassValue[] encapCmds;

                        ZWaveDefinition.ParseApplicationObject(cmddata, out encapCmds);
                        ret.InnerValues = new List <ParamValue>();
                        if (encapCmds != null)
                        {
                            foreach (var ec in encapCmds)
                            {
                                if (encapCmds.Length > 1)
                                {
                                    ParamValue pv = new ParamValue
                                    {
                                        ParamDefinitionTextPrefix = ec.CommandValue.CommandDefinition.Name,
                                        ParamDefinitionTextSuffix = ", ver." + ec.CommandClassDefinition.Version,
                                        InnerValues = ec.CommandValue.ParamValues
                                    };
                                    ret.InnerValues.Add(pv);
                                }
                                else
                                {
                                    ret.InnerValues = ec.CommandValue.ParamValues;
                                }
                            }
                        }
                    }
                    break;
                }
            }
            return(ret);
        }
        public static Command FindCommand(this ZWaveDefinition zWaveDefinition, string commandClassName, byte commandClassVersion, string name)
        {
            CommandClass cmdClass = FindCommandClass(zWaveDefinition, commandClassName, commandClassVersion);

            return(cmdClass.Command.FirstOrDefault(var => var.Name == name));
        }
 public static Command FindCommand(this ZWaveDefinition zWaveDefinition, CommandClass commandClass, string name)
 {
     return(commandClass.Command.FirstOrDefault(var => var.Name == name));
 }
Example #7
0
        public static cmd_class DowngradeCommandClass(CommandClass cmdClass)
        {
            DefinitionConverter conv = new DefinitionConverter(null, null);

            return((cmd_class)conv.DowngradeCommandClass(cmdClass, null));
        }
Example #8
0
        private IEnumerable <string> ParameterToString(IList <byte> list, zwParamType type, Collection <Define> defines)
        {
            IList <string> ret   = new List <string>();
            bool           first = true;
            StringBuilder  sb;

            switch (type)
            {
            case zwParamType.CHAR:

                try
                {
                    ret.Add(Encoding.ASCII.GetString(list.ToArray(), 0, list.Count));
                }
                catch (Exception dex)
                {
                    dex.Message._EXLOG();
                }
                break;

            case zwParamType.HEX:
                if (defines == null)
                {
                    ret.Add(Tools.GetHex(list));
                }
                else
                {
                    sb = new StringBuilder();
                    bool isFirst = true;
                    foreach (var itemByte in list)
                    {
                        string str = null;
                        foreach (var dItem in defines)
                        {
                            if (dItem.KeyId == itemByte)
                            {
                                str = dItem.Text;
                            }
                        }
                        if (!isFirst)
                        {
                            sb.Append(" ");
                        }
                        if (str != null)
                        {
                            sb.Append(str);
                            sb.Append("=");
                        }
                        sb.Append(Tools.GetHex(itemByte, true));
                        isFirst = false;
                    }
                    ret.Add(sb.ToString());
                }
                break;

            case zwParamType.NUMBER:
                if (list.Count <= 4)     //int
                {
                    byte[] val = { 0, 0, 0, 0 };
                    Array.Copy(list.Reverse().ToArray(), val, list.Count);
                    int tmp = BitConverter.ToInt32(val, 0);
                    sb = new StringBuilder();
                    if (defines != null)
                    {
                        string str = null;
                        foreach (var dItem in defines)
                        {
                            if (dItem.KeyId == tmp)
                            {
                                str = dItem.Text;
                            }
                        }
                        sb.Append(str ?? tmp.ToString());
                    }
                    else
                    {
                        if (tmp < 100)
                        {
                            sb.Append(tmp.ToString(""));
                        }
                        else
                        {
                            sb.Append(tmp.ToString());
                        }
                    }
                    ret.Add(sb.ToString());
                }
                else     // as HEX
                {
                    ret.Add(Tools.GetHex(list));
                }
                break;

            case zwParamType.NODE_NUMBER:
                sb = new StringBuilder();
                foreach (var item in list)
                {
                    if (!first)
                    {
                        sb.Append(", ");
                    }
                    first = false;
                    sb.Append(item.ToString(""));
                }
                ret.Add(sb.ToString());
                break;

            case zwParamType.BITMASK:
                sb = new StringBuilder();
                for (int i = 0; i < list.Count; i++)
                {
                    byte maskByte = list[i];
                    if (maskByte == 0)
                    {
                        continue;
                    }
                    byte bitMask = 0x01;
                    for (int j = 0; j < 8; j++)
                    {
                        if ((bitMask & maskByte) != 0)
                        {
                            byte bitNumber = (byte)(i * 8 + j);
                            if (!first)
                            {
                                sb.Append(", ");
                            }
                            first = false;
                            if (defines != null)
                            {
                                string str = null;
                                foreach (var dItem in defines)
                                {
                                    if (dItem.KeyId == bitNumber)
                                    {
                                        str = dItem.Text;
                                    }
                                }

                                if (str != null)
                                {
                                    sb.Append(str);
                                    sb.Append("=");
                                    sb.Append(Tools.GetHex(bitNumber, true));
                                }
                                else
                                {
                                    sb.Append(Tools.GetHex(bitNumber, true));
                                }
                            }
                            else
                            {
                                sb.Append((bitNumber + 1).ToString(""));     //nodes starting from 1 in mask bytes array
                            }
                        }
                        bitMask <<= 1;
                    }
                }
                ret.Add(sb.ToString());
                break;

            case zwParamType.BOOLEAN:
                if (list.Count > 0)
                {
                    if (defines == null)
                    {
                        ret.Add(list[0] > 0 ? "true" : "false");
                    }
                    else
                    {
                        string str = null;
                        foreach (var dItem in defines)
                        {
                            if (dItem.KeyId == list[0])
                            {
                                str = dItem.Text;
                            }
                        }
                        if (str != null)
                        {
                            ret.Add(str);
                        }
                        else
                        {
                            ret.Add(list[0] > 0 ? "true" : "false");
                        }
                    }
                }
                else
                {
                    ret.Add("false");
                }
                break;

            case zwParamType.MARKER:
                ret.Add(Tools.GetHex(list));
                break;

            case zwParamType.BAS_DEV_REF:
                foreach (var item in list)
                {
                    BasicDevice bd = BasicDevices.FirstOrDefault(x => x.Key == Tools.GetHex(item, true));
                    if (bd != null)
                    {
                        ret.Add(string.Format("{0} - {1}", Tools.GetHex(item, true), bd.Name));
                    }
                    else
                    {
                        ret.Add(Tools.GetHex(item, true));
                    }
                }
                break;

            case zwParamType.GEN_DEV_REF:
                foreach (var item in list)
                {
                    _genericDevice = GenericDevices.FirstOrDefault(x => x.Key == Tools.GetHex(item, true));
                    if (_genericDevice != null)
                    {
                        ret.Add(string.Format("{0} - {1}", Tools.GetHex(item, true), _genericDevice.Name));
                    }
                    else
                    {
                        ret.Add(Tools.GetHex(item, true));
                    }
                }
                break;

            case zwParamType.SPEC_DEV_REF:
                if (_genericDevice != null)
                {
                    foreach (var item in list)
                    {
                        SpecificDevice bd =
                            _genericDevice.SpecificDevice.FirstOrDefault(x => x.Key == Tools.GetHex(item, true));
                        if (bd != null)
                        {
                            ret.Add(string.Format("{0} - {1}", Tools.GetHex(item, true), bd.Name));
                        }
                        else
                        {
                            ret.Add(Tools.GetHex(item, true));
                        }
                    }
                }
                else
                {
                    ret.Add(Tools.GetHex(list));
                }
                break;

            case zwParamType.CMD_CLASS_REF:
                foreach (var item in list)
                {
                    _commandClass = CommandClasses.FirstOrDefault(x => x.Key == Tools.GetHex(item, true));
                    if (_commandClass != null)
                    {
                        ret.Add(string.Format("{0} - {1}", Tools.GetHex(item, true), _commandClass.Name));
                    }
                    else
                    {
                        ret.Add(Tools.GetHex(item, true));
                    }
                }
                break;

            case zwParamType.CMD_REF:
                if (_commandClass != null)
                {
                    foreach (var item in list)
                    {
                        if (_commandClass.Command != null)
                        {
                            _command = _commandClass.Command.FirstOrDefault(x => x.Key == Tools.GetHex(item, true));
                            if (_command != null)
                            {
                                ret.Add(string.Format("{0} - {1}", Tools.GetHex(item, true), _command.Name));
                            }
                            else
                            {
                                ret.Add(Tools.GetHex(item, true));
                            }
                        }
                        else
                        {
                            ret.Add(Tools.GetHex(item, true));
                        }
                    }
                }
                else
                {
                    ret.Add(Tools.GetHex(list));
                }
                break;

            case zwParamType.CMD_DATA:
                ret.Add(Tools.GetHex(list));
                break;

            case zwParamType.CMD_ENCAP:
                ret.Add(Tools.GetHex(list));
                break;
            }
            return(ret);
        }
Example #9
0
        private void ProcessParametersTextValue(IEnumerable <ParamValue> paramValues, CommandClass cmdClass)
        {
            ParamValue prevParamValue = null;

            foreach (var item in paramValues)
            {
                if (item.HasTextValue)
                {
                    break;
                }
                if (item.InnerValues != null)
                {
                    ProcessParametersTextValue(item.InnerValues, cmdClass);
                }
                if (item.ParamDefinition != null)
                {
                    if (item.ParamDefinition.Defines != null && cmdClass.DefineSet != null)
                    {
                        Collection <Define> defines = null;
                        DefineSet           dSet    = cmdClass.DefineSet.FirstOrDefault(x => x.Name == item.ParamDefinition.Defines);
                        if (dSet != null)
                        {
                            defines = dSet.Define;
                            bool isMultiarray =
                                dSet.Define.Aggregate(true, (current, d) => current & d.Define1 != null);
                            if (isMultiarray && prevParamValue != null)
                            {
                                if (prevParamValue.ByteValueList != null && prevParamValue.ByteValueList.Count > 0)
                                {
                                    var d = dSet.Define.FirstOrDefault(x => x.KeyId == prevParamValue.ByteValueList[0]);
                                    if (d != null)
                                    {
                                        defines = d.Define1;
                                    }
                                }
                            }
                        }
                        IEnumerable <string> range =
                            ParameterToString(item.ByteValueList, item.ParamDefinition.Type, defines);
                        foreach (var str in range)
                        {
                            item.TextValueList.Add(str);
                            item.HasTextValue = true;
                        }
                    }
                    else
                    {
                        var paramType = item.ParamDefinition != null ? item.ParamDefinition.Type : zwParamType.HEX;
                        IEnumerable <string> range = ParameterToString(item.ByteValueList, paramType, null);
                        foreach (var str in range)
                        {
                            item.TextValueList.Add(str);
                            item.HasTextValue = true;
                        }
                    }
                }
                else
                {
                    var paramType = item.ParamDefinition != null ? item.ParamDefinition.Type : zwParamType.HEX;
                    IEnumerable <string> range = ParameterToString(item.ByteValueList, paramType, null);
                    foreach (var str in range)
                    {
                        item.TextValueList.Add(str);
                        item.HasTextValue = true;
                    }
                }
                prevParamValue = item;
            }
        }