コード例 #1
0
        /// <summary>
        /// This function serializes object to frame group
        /// </summary>
        /// <typeparam name="T">Data type</typeparam>
        /// <param name="data">Data object</param>
        /// <param name="reflection">reflection info for this data type</param>
        /// <returns>Group collection</returns>
        private static List <PackageGroupItem> InternalSerializeV2ToGroup <T>(List <T> data, ReflectionType reflection)
        {
            //get group attribute
            PackageGroupAttribute PackageGroupAttribute = reflection.GetCustomAttribute <PackageGroupAttribute>();

            //check group attribute
            if (PackageGroupAttribute != null)
            {
                //get collection
                List <PackageGroupItem> collection = new List <PackageGroupItem>();

                //loop all object
                foreach (T obj in data)
                {
                    //serialize object
                    PackageGroupItem group = InternalSerializeV2ToGroup <T>(obj, reflection, PackageGroupAttribute);
                    if (group != null)
                    {
                        collection.Add(group);
                    }
                }

                //return groups
                return(collection);
            }

            //no data
            return(null);
        }
コード例 #2
0
        /// <summary>
        /// Returns a string that represents the current object.
        /// </summary>
        /// <returns>A string that represents the current object.</returns>
        public override string ToString()
        {
            //initialize builder
            StringBuilder builder = new StringBuilder();

            //base information
            builder.Append("------------------");
            builder.AppendFormat("Address: {0}, Groups: {1}", this.Address, this.Groups.Count);
            builder.Append(Environment.NewLine);

            //items
            int groupCount = this.m_groups.Count;

            for (int i = 0; i < groupCount; i++)
            {
                PackageGroupItem group = this.m_groups[i];
                builder.Append("---------");
                builder.Append(Environment.NewLine);
                builder.AppendFormat("Group: {0}, Address: 0x{1:x4}, Items: {2}", (i + 1), group.Address, group.Count);
                builder.Append(Environment.NewLine);
                foreach (IPackageItem item in group.Items)
                {
                    builder.AppendFormat("[{0}] - [{1} - 0x{1:x4}] - {2}", item.GetType(), item.Address, item.GetValue());
                    builder.Append(Environment.NewLine);
                }
            }

            //create end line
            builder.Append("------------------");

            //return string
            return(builder.ToString());
        }
コード例 #3
0
        /// <summary>
        /// Deserializes the Group to a .NET object.
        /// </summary>
        /// <typeparam name="T">The type of the object to deserialize to.</typeparam>
        /// <param name="group">The Group to deserialize.</param>
        /// <returns>The deserialized object from the Group.</returns>
        public static T DeserializeV2 <T>(PackageGroupItem group)
        {
            if (group == null)
            {
                throw new ArgumentNullException("group");
            }

            return(PackageHelper.InternalDeserializeV2 <T>(group));
        }
コード例 #4
0
        /// <summary>
        /// Deserializes the Group to a .NET object.
        /// </summary>
        /// <typeparam name="T">The type of the object to deserialize to.</typeparam>
        /// <param name="group">The Group to deserialize.</param>
        /// <returns>The deserialized object from the Group.</returns>
        private static T InternalDeserializeV2 <T>(PackageGroupItem group)
        {
            //get the object reglection
            ReflectionType reflection = ReflectionHelper.GetType(typeof(T));

            if (reflection != null)
            {
                return(PackageHelper.InternalDeserializeV2 <T>(group, reflection));
            }
            return(default(T));
        }
コード例 #5
0
        /// <summary>
        /// This method returns first froup with required address
        /// </summary>
        /// <param name="address">Required address</param>
        /// <returns></returns>
        public PackageGroupItem FindGroup(UInt16 address)
        {
            int groupCount = this.m_groups.Count;

            for (int i = 0; i < groupCount; i++)
            {
                PackageGroupItem group = this.m_groups[i];
                if (group.Address == address)
                {
                    return(group);
                }
            }
            return(null);
        }
コード例 #6
0
        /// <summary>
        /// This function serializes object to frame group
        /// </summary>
        /// <typeparam name="T">Data type</typeparam>
        /// <param name="data">Data object</param>
        /// <param name="reflection">reflection info for this data type</param>
        /// <param name="PackageGroupAttribute">Current group attribute</param>
        /// <returns>Group | null</returns>
        private static PackageGroupItem InternalSerializeV2ToGroup <T>(T data, ReflectionType reflection, PackageGroupAttribute PackageGroupAttribute)
        {
            //variables
            Object value = null;

            //create group
            PackageGroupItem group = new PackageGroupItem(PackageGroupAttribute.Address);

            //set property
            foreach (ReflectionProperty item in reflection.PropertyCollection.Values)
            {
                //check property type
                if (item.Property.CanRead)
                {
                    //get current attribute
                    PackageItemAttribute attribute = item.GetCustomAttribute <PackageItemAttribute>();
                    if (attribute != null)
                    {
                        try
                        {
                            value = item.Property.GetValue(data, null);
                            if (value != null)
                            {
                                IPackageItem frameItem = Package.CreatePackageItem(attribute.Type, attribute.Address, value);
                                if (frameItem != null)
                                {
                                    group.Add(frameItem);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(String.Format("Error type: {0} -> {1} [{2}]", attribute.Type, item.Property.Name, value), ex);
                        }
                    }
                }
            }

            //return current group
            return(group);
        }
コード例 #7
0
        /// <summary>
        /// Serializes the specified Object to Frame
        /// </summary>
        /// <typeparam name="T">The type of the object to serialize to.</typeparam>
        /// <param name="data">The object to serialize.</param>
        /// <param name="frame">Frame</param>
        /// <param name="reflection">Reflection information for the Object to deserialize</param>
        /// <returns>Frame | null</returns>
        private static PackageV2 InternalSerializeV2 <T>(List <T> data, PackageV2 frame, ReflectionType reflection)
        {
            //get group attribute
            PackageGroupAttribute PackageGroupAttribute = reflection.GetCustomAttribute <PackageGroupAttribute>();

            //each object
            foreach (T obj in data)
            {
                //check attribute
                if (PackageGroupAttribute != null)
                {
                    //serialize group
                    PackageGroupItem group = InternalSerializeV2ToGroup <T>(obj, reflection, PackageGroupAttribute);

                    //create group to frame
                    frame.Add(group);
                }
            }

            //return result
            return(frame);
        }
コード例 #8
0
        /// <summary>
        /// Deserializes the Group to a .NET object.
        /// </summary>
        /// <typeparam name="T">The type of the object to deserialize to.</typeparam>
        /// <param name="group">The Group to deserialize.</param>
        /// <param name="reflection">Reflection information for the Object to deserialize</param>
        /// <returns>The deserialized object from the Group.</returns>
        private static T InternalDeserializeV2 <T>(PackageGroupItem group, ReflectionType reflection)
        {
            //intiailize object
            T result = (T)Activator.CreateInstance(typeof(T));

            //set property
            foreach (ReflectionProperty item in reflection.PropertyCollection.Values)
            {
                //check property type
                if (item.Property.CanWrite)
                {
                    //get current attribute
                    PackageItemAttribute attribute = item.GetCustomAttribute <PackageItemAttribute>();
                    if (attribute != null)
                    {
                        Object value = null;
                        try
                        {
                            //get vale from frame
                            value = group.GetValue <Object>(attribute.Address);

                            //update value
                            value = PackageHelper.InternalUpdateValue(attribute.Type, item.Property.PropertyType, value);

                            //set value to property
                            item.Property.SetValue(result, value, null);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(String.Format("Error type: {0} -> {1}", (value == null ? "NULL" : value.ToString()), item.Property.Name), ex);
                        }
                    }
                }
            }

            return(result);
        }
コード例 #9
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);
            }
        }
コード例 #10
0
 /// <summary>
 /// Inserts an item into the Frame at the specified index.
 /// </summary>
 /// <param name="group">The object to insert. The value can be null for reference types.</param>
 /// <param name="index">The zero-based index at which item should be inserted.</param>
 public void Add(PackageGroupItem group, int index)
 {
     this.m_groups.Insert(index, group);
 }
コード例 #11
0
 /// <summary>
 /// Adds an item to the end of the Frame
 /// </summary>
 /// <param name="group">The item to be added to the end of the Frame</param>
 public void Add(PackageGroupItem group)
 {
     this.m_groups.Add(group);
 }
コード例 #12
0
 /// <summary>
 ///  Removes the first occurrence of a specific object
 /// </summary>
 /// <param name="group">The object to remove</param>
 public void Remove(PackageGroupItem group)
 {
     this.m_groups.Remove(group);
 }