/// <summary>
 /// This method updates or converts value to target type
 /// </summary>
 /// <param name="type">Frame item type</param>
 /// <param name="targetType">Target property type</param>
 /// <param name="value">Value to convert</param>
 /// <returns></returns>
 private static Object InternalUpdateValue(PackageItemTypes type, Type targetType, Object value)
 {
     //check value
     if (value != null)
     {
         //check type
         if (type == PackageItemTypes.String && targetType == typeof(Guid) && value is String)
         {
             Nullable <Guid> guidValue = (value as String).ToGuidWithoutDash();
             return(guidValue.Value);
         }
         else if (type == PackageItemTypes.Enum)
         {
             if (Utility.IsNullableType(targetType))
             {
                 targetType = Nullable.GetUnderlyingType(targetType);
             }
             if (!targetType.IsEnum)
             {
                 throw new Exception(String.Format("TargetType is not enum type. [{0}]", targetType));
             }
             return(Enum.ToObject(targetType, value));
         }
     }
     return(value);
 }
Example #2
0
        /// <summary>
        /// This function parse frame from data
        /// </summary>
        /// <param name="data">Data to parse</param>
        /// <param name="action">Function to get frame item type</param>
        private void InternalParseFrame(Byte[] data, Func <UInt16, UInt32, PackageItemTypes> action)
        {
            //vriables
            UInt32 address = 0x00;
            UInt16 length  = 0x00;

            Byte[]           dataItem = null;
            PackageItemTypes type     = PackageItemTypes.Unkown;
            int count = data.Length;

            //parse frame
            for (int i = 0; i < count; i++)
            {
                //read address
                address = (UInt32)(data[i + 3] << 24 | data[i + 2] << 16 | data[i + 1] << 8 | data[i]);
                length  = (UInt16)(data[i + 5] << 8 | data[i + 4]);

                //read frame item type
                type = action != null?action(this.Address, address) : PackageItemTypes.Unkown;

                //read data
                dataItem = new Byte[length];
                Buffer.BlockCopy(data, i + 6, dataItem, 0, length);

                //parse
                IPackageItem item = this.InternalParseFrame(type, address, length, dataItem);
                if (item != null)
                {
                    this.m_items.Add(item);
                }

                //next
                i += 5 + length;
            }
        }
Example #3
0
        /// <summary>
        /// This function initialize item of type
        /// </summary>
        /// <param name="type">Item type</param>
        /// <param name="address">Item address for value</param>
        /// <param name="value">Value</param>
        /// <returns>Frame item | null</returns>
        public static IPackageItem CreatePackageItem(PackageItemTypes type, UInt32 address, Object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            switch (type)
            {
            case PackageItemTypes.DateTime:
                return(new PackageItemDateTime(address, (DateTime)value));

            case PackageItemTypes.Guid:
                return(new PackageItemGuid(address, (Guid)value));

            case PackageItemTypes.String:
            {
                if (value is Guid)
                {
                    return(new PackageItemString(address, ((Guid)value).ToStringWithoutDash()));
                }
                else
                {
                    return(new PackageItemString(address, (String)value));
                }
            }

            case PackageItemTypes.Int16:
                return(new PackageItemInt16(address, (Int16)value));

            case PackageItemTypes.Int32:
                return(new PackageItemInt32(address, (Int32)value));

            case PackageItemTypes.Int64:
                return(new PackageItemInt64(address, (Int64)value));

            case PackageItemTypes.Byte:
                return(new PackageItemByte(address, (Byte)value));

            case PackageItemTypes.Boolean:
                return(new PackageItemBoolean(address, (Boolean)value));

            case PackageItemTypes.UInt16:
                return(new PackageItemUInt16(address, (UInt16)value));

            case PackageItemTypes.UInt32:
                return(new PackageItemUInt32(address, (UInt32)value));

            case PackageItemTypes.UInt64:
                return(new PackageItemUInt64(address, (UInt64)value));

            case PackageItemTypes.Enum:
                return(new PackageItemEnum(address, value));

            default:
                return(new PackageItemUnkown(address, (List <Byte>)value));
            }
        }
Example #4
0
        /// <summary>
        /// This function parse frame item from item type
        /// </summary>
        /// <param name="type">Type of frame</param>
        /// <param name="address">Frame item address</param>
        /// <param name="length">Frame item length</param>
        /// <param name="data">Data frame item</param>
        /// <returns>Frame item or null</returns>
        private IPackageItem InternalParseFrame(PackageItemTypes type, UInt32 address, UInt16 length, Byte[] data)
        {
            switch (type)
            {
            case PackageItemTypes.DateTime:
                return(new PackageItemDateTime(address, data));

            case PackageItemTypes.Guid:
                return(new PackageItemGuid(address, data));

            case PackageItemTypes.String:
                return(new PackageItemString(address, data));

            case PackageItemTypes.Int16:
                return(new PackageItemInt16(address, data));

            case PackageItemTypes.Int32:
                return(new PackageItemInt32(address, data));

            case PackageItemTypes.Int64:
                return(new PackageItemInt64(address, data));

            case PackageItemTypes.Byte:
                return(new PackageItemByte(address, data));

            case PackageItemTypes.Boolean:
                return(new PackageItemBoolean(address, data));

            case PackageItemTypes.UInt16:
                return(new PackageItemUInt16(address, data));

            case PackageItemTypes.UInt32:
                return(new PackageItemUInt32(address, data));

            case PackageItemTypes.Enum:
                return(new PackageItemEnum(address, data));

            case PackageItemTypes.UInt64:
                return(new PackageItemUInt64(address, data));

            default:
                return(new PackageItemUnkown(address, data));
            }
        }
Example #5
0
        /// <summary>
        /// This function return NET object type from Frame Item type
        /// </summary>
        /// <param name="type">Frame item type</param>
        /// <returns>NET object type</returns>
        public static Type GetObjectTypeFromPackageItemType(PackageItemTypes type)
        {
            switch (type)
            {
            case PackageItemTypes.DateTime:
                return(typeof(DateTime));

            case PackageItemTypes.Guid:
                return(typeof(Guid));

            case PackageItemTypes.Int16:
                return(typeof(Int16));

            case PackageItemTypes.Int32:
                return(typeof(Int32));

            case PackageItemTypes.Int64:
                return(typeof(Int64));

            case PackageItemTypes.String:
                return(typeof(String));

            case PackageItemTypes.Byte:
                return(typeof(Byte));

            case PackageItemTypes.Boolean:
                return(typeof(Boolean));

            case PackageItemTypes.UInt64:
                return(typeof(UInt64));

            case PackageItemTypes.UInt32:
                return(typeof(UInt32));

            case PackageItemTypes.Enum:
                return(typeof(Enum));

            default:
                return(typeof(Object));
            }
        }
Example #6
0
        /// <summary>
        /// This function parse frame from data
        /// </summary>
        /// <param name="data">Data to parse</param>
        /// <param name="action">Function to get frame item type</param>
        private void InternalParseFrame(Byte[] data, Func <UInt16, UInt16, UInt32, PackageItemTypes> action)
        {
            //vriables
            UInt32 address = 0x00;
            UInt16 length  = 0x00;

            Byte[]           dataItem = null;
            PackageItemTypes type     = PackageItemTypes.Unkown;
            int    count        = data.Length;
            UInt16 itemCount    = 0x00;
            UInt16 dataAddress  = 0x00;
            int    currentIndex = 0x00;

            //parse data
            while (currentIndex < count)
            {
                //check group value
                if (data[currentIndex++] != 0x67)
                {
                    throw new Exception("Data are not valid!");
                }

                //get data address
                dataAddress = (UInt16)(data[currentIndex + 1] << 8 | data[currentIndex]);

                //get count+
                itemCount     = (UInt16)(data[currentIndex + 3] << 8 | data[currentIndex + 2]);
                currentIndex += 0x04;

                //create group
                PackageGroupItem group = new PackageGroupItem(dataAddress);

                //parse items
                for (int i = 0; i < itemCount; i++)
                {
                    //read address
                    address = (UInt32)(data[currentIndex + 3] << 24 | data[currentIndex + 2] << 16 | data[currentIndex + 1] << 8 | data[currentIndex]);
                    length  = (UInt16)(data[currentIndex + 5] << 8 | data[currentIndex + 4]);

                    //read frame item type
                    type = action != null?action(this.Address, dataAddress, address) : PackageItemTypes.Unkown;

                    //read data
                    dataItem = new Byte[length];
                    Buffer.BlockCopy(data, currentIndex + 6, dataItem, 0, length);

                    //parse
                    IPackageItem item = this.InternalParseFrame(type, address, length, dataItem);
                    if (item != null)
                    {
                        group.Add(item);
                    }

                    //next
                    currentIndex += 0x06 + length;
                }

                //add group to frame
                this.m_groups.Add(group);
            }
        }