Esempio n. 1
0
        private static IDataBlock CreateDummyBlock(XElement datablock, ref int autoAddr)
        {
            XAttribute addressNode = datablock.Attribute("address");
            XAttribute keyNode     = datablock.Attribute("key");
            XAttribute nameNode    = datablock.Attribute("displayname");

            XAttribute dataLenghtNode = datablock.Attribute("datasize");

            if (!addressNode.Value.Equals("auto", StringComparison.OrdinalIgnoreCase))
            {
                autoAddr = addressNode.Value.ToInt32();
            }

            bool ignore = keyNode == null;

            string key = keyNode == null
                ? String.Empty
                : keyNode.Value;

            string name = nameNode == null
                ? key
                : nameNode.Value;

            IDataBlock block = new DummyDataBlock
            {
                Address = autoAddr,
                Key     = key,
                Name    = name,
                Size    = dataLenghtNode.Value.ToInt32(),
            };

            autoAddr += block.Size;
            return(!ignore
                ? block
                : null);
        }
Esempio n. 2
0
        private static IEnumerable <IDataBlock> CreateArrayDataBlock(
            byte[] scanData,
            int scanOffset,
            List <XElement> enumerables,
            List <XElement> structs,
            XElement datablock,
            ref int autoAddr)
        {
            XAttribute keyNode     = datablock.Attribute("key");
            XAttribute addressNode = datablock.Attribute("address");
            XAttribute nameNode    = datablock.Attribute("displayname");

            XAttribute copyaddressNode = datablock.Attribute("copyaddress");
            XAttribute readOnlyNode    = datablock.Attribute("readonly");
            XAttribute dataLenghtNode  = datablock.Attribute("datasize");

            XAttribute minvalNode  = datablock.Attribute("minvalue");
            XAttribute maxvalNode  = datablock.Attribute("maxvalue");
            XAttribute warnvalNode = datablock.Attribute("warnvalue");

            XAttribute innerTypeNode = datablock.Attribute("innertype");
            XAttribute headTypeNode  = datablock.Attribute("headtype");
            XAttribute arraySizeNode = datablock.Attribute("arraysize");

            XAttribute metaKeyNode = datablock.Attribute("metakey");

            if (!addressNode.Value.Equals("auto", StringComparison.OrdinalIgnoreCase))
            {
                autoAddr = addressNode.Value.ToInt32();
            }

            bool ignore = keyNode == null;

            string key = keyNode == null
                ? String.Empty
                : keyNode.Value;

            if (innerTypeNode == null)
            {
                throw new Exception(string.Format("Inner Type not set for Array: {0}", key));
            }

            string innerType = innerTypeNode.Value;

            string name = nameNode == null
                ? key
                : nameNode.Value;

            var copyAddresses = copyaddressNode == null
                ? new int[0]
                : copyaddressNode.Value.Split(';')
                                .Select(Extensions.ToInt32)
                                .ToArray();

            IDataBlock baseBlock;
            string     arraySizeStr = arraySizeNode.Value;
            string     headKey      = key;

            if (arraySizeStr.Equals("auto", StringComparison.OrdinalIgnoreCase))
            {
                string headType = headTypeNode.Value;
                int    dataLenght;
                switch (headType)
                {
                default:
                case "byte":
                    dataLenght = 1;
                    break;

                case "int16":
                    dataLenght = 2;
                    break;

                case "int32":
                    dataLenght = 4;
                    break;
                }

                switch (dataLenght)
                {
                default:
                case 1:
                    baseBlock = new ByteDataBlock
                    {
                        Address       = autoAddr,
                        CopyAddresses = copyAddresses,
                        Key           = headKey,
                        Name          = name,
                        ReadOnly      = true
                    };
                    break;

                case 2:
                    baseBlock = new Int16DataBlock
                    {
                        Address       = autoAddr,
                        CopyAddresses = copyAddresses,
                        Key           = headKey,
                        Name          = name,
                        ReadOnly      = true
                    };
                    break;

                case 4:
                    baseBlock = new Int32DataBlock
                    {
                        Address       = autoAddr,
                        CopyAddresses = copyAddresses,
                        Key           = headKey,
                        Name          = name,
                        ReadOnly      = true
                    };
                    break;
                }
            }
            else
            {
                baseBlock = new DummyDataBlock
                {
                    Address       = autoAddr,
                    CopyAddresses = new int[0],
                    Key           = headKey,
                    Name          = name,
                    ReadOnly      = true,
                    DummyValue    = arraySizeStr.ToInt32()
                };
            }

            var outBlocks = new List <IDataBlock>();

            autoAddr += baseBlock.Size;
            outBlocks.Add(baseBlock);

            int elCount = Convert.ToInt32(baseBlock.Read(scanData, scanOffset));

            for (int i = 0; i < elCount; i++)
            {
                IDataBlock block      = null;
                XElement   innerBlock = new XElement("datablock",
                                                     new XAttribute("type", innerType),
                                                     //
                                                     new XAttribute("key", string.Format("{0}[{1}]", key, i)),
                                                     new XAttribute("address", "auto"),
                                                     new XAttribute("displayname",
                                                                    string.Format("{0} Element {1}", name, i)
                                                                    .Trim()),
                                                     //
                                                     readOnlyNode,
                                                     metaKeyNode,
                                                     dataLenghtNode,
                                                     //
                                                     minvalNode,
                                                     maxvalNode,
                                                     warnvalNode);

                switch (innerType)
                {
                case "dummy":
                    block = CreateDummyBlock(innerBlock, ref autoAddr);
                    break;

                case "bool":
                case "byte":
                case "int16":
                case "int32":
                case "color":
                case "string":
                case "encodedstring":
                    block = CreateValueDataBlock(innerType, innerBlock, ref autoAddr);
                    break;

                case "enum":
                    block = CreateEnumDataBlock(enumerables, innerBlock, ref autoAddr);
                    break;

                case "array":
                    throw new Exception("Arrays of Arrays are not supported, use a struct datatype");
                    break;

                case "struct":
                    var structBlocks = CreateStructDataBlock(scanData, scanOffset, enumerables, structs, innerBlock, ref autoAddr);
                    outBlocks.AddRange(structBlocks);
                    break;
                }
                if (block != null)
                {
                    outBlocks.Add(block);
                }
            }

            return(!ignore
                ? outBlocks
                : Enumerable.Empty <IDataBlock>());
        }