/// <summary>
        /// Deserializes the Frame to a .NET object.
        /// </summary>
        /// <typeparam name="T">The type of the object to deserialize to.</typeparam>
        /// <param name="frame">The Frame to deserialize.</param>
        /// <param name="reflection">Reflection information for the Object to deserialize</param>
        /// <returns>The deserialized object from the Frame.</returns>
        private static List <T> InternalDeserializeV2 <T>(PackageV2 frame, ReflectionType reflection)
        {
            //get group attribute
            PackageGroupAttribute PackageGroupAttribute = reflection.GetCustomAttribute <PackageGroupAttribute>();

            if (PackageGroupAttribute == null)
            {
                throw new Exception(String.Format("Type {0} does not contain FrameGroup attribute!", reflection.Type.Name));
            }

            //create collection
            List <T> collection = new List <T>();

            //each objects
            foreach (PackageGroupItem group in frame.Groups)
            {
                //check group address for this type
                if (group.Address == PackageGroupAttribute.Address)
                {
                    //intiailize object
                    T result = PackageHelper.InternalDeserializeV2 <T>(group, reflection);

                    //create objet to collection
                    collection.Add(result);
                }
            }

            //return result
            return(collection);
        }
        /// <summary>
        /// This function constructs frame from data array
        /// </summary>
        /// <param name="array">Data array</param>
        /// <param name="index">Start frame index</param>
        /// <param name="length">Frame length</param>
        /// <param name="address">Command address from frame</param>
        /// <param name="state">State value</param>]
        /// <param name="action">Callback for parsing frame items</param>
        /// <returns>Frame | null</returns>
        private static PackageV2 ConstructPackageV2(List <Byte> array, int index, int length, UInt16 address, Byte state, Func <UInt16, UInt16, UInt32, PackageItemTypes> action)
        {
            //check data length available
            if ((array.Count - index) >= length)
            {
                //get checksum
                Byte checkSum        = PackageHelper.GetFrameDataCheckSum(array, index - 5, length + 5);
                Byte currentCheckSum = array[index + length + 0];
                if (checkSum != currentCheckSum)
                {
                    return(null);
                }

                //copy data block
                List <Byte> temp = array.GetRange(index, length);

                //get current action
                Func <UInt16, UInt16, UInt32, PackageItemTypes> currentAction = action == null ? PackageHelper.InternalGetPackageItemType : action;

                //initialize package
                PackageV2 frame = new PackageV2(address, state, temp, currentAction);

                //remove data
                array.RemoveRange(0, length + index + 1);

                //return package
                return(frame);
            }
            return(null);
        }
Exemple #3
0
        /// <summary>
        /// Create frame from Byte value
        /// </summary>
        /// <param name="frameAddress">Frame address</param>
        /// <param name="state">Frate state</param>
        /// <param name="value">Value as Byte</param>
        /// <returns>New frame</returns>
        public static PackageV2 CreateFrame(UInt16 frameAddress, Byte state, Byte value)
        {
            PackageV2 frame = new PackageV2(frameAddress, state);

            frame.State = state;
            return(frame);
        }
        /// <summary>
        /// Deserializes the Frame to a .NET object.
        /// </summary>
        /// <typeparam name="T">The type of the object to deserialize to.</typeparam>
        /// <param name="frame">The Frame to deserialize.</param>
        /// <returns>The deserialized object from the Frame.</returns>
        public static List <T> DeserializeV2 <T>(PackageV2 frame)
        {
            if (frame == null)
            {
                throw new ArgumentNullException("frame");
            }

            return(PackageHelper.InternalDeserializeV2 <T>(frame));
        }
        /// <summary>
        /// Deserializes the Frame to a .NET object.
        /// </summary>
        /// <typeparam name="T">The type of the object to deserialize to.</typeparam>
        /// <param name="frame">The Frame to deserialize.</param>
        /// <returns>The deserialized object from the Frame.</returns>
        private static List <T> InternalDeserializeV2 <T>(PackageV2 frame)
        {
            //get the object reglection
            ReflectionType reflection = ReflectionHelper.GetType(typeof(T));

            if (reflection != null)
            {
                return(PackageHelper.InternalDeserializeV2 <T>(frame, reflection));
            }
            return(default(List <T>));
        }
        /// <summary>
        /// Serializes the specified Object to Frame
        /// </summary>
        /// <typeparam name="T">The type of the object to serialize to.</typeparam>
        /// <param name="obj">The object to serialize.</param>
        /// <param name="frame">Frame</param>
        /// <returns>Frame | null</returns>
        private static PackageV2 InternalSerializeV2 <T>(List <T> obj, PackageV2 frame)
        {
            //get the object reglection
            ReflectionType reflection = ReflectionHelper.GetType(typeof(T));

            if (reflection != null)
            {
                return(PackageHelper.InternalSerializeV2 <T>(obj, frame, reflection));
            }
            return(null);
        }
        /// <summary>
        /// Serializes the specified object to a Frame.
        /// </summary>
        /// <typeparam name="T">The type of the object to serialize to.</typeparam>
        /// <param name="obj">The object to serialize.</param>
        /// <param name="commandAddress">Frame command address</param>
        /// <returns>Frame | null</returns>
        public static PackageV2 SerializeV2 <T>(List <T> obj, UInt16 commandAddress)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            //intiailize object
            PackageV2 result = new PackageV2(commandAddress, 0x01);

            //serialize
            return(SerializeV2 <T>(obj, result));
        }
        /// <summary>
        /// This function finds frame in array
        /// </summary>
        /// <param name="array">Input array data</param>
        /// <param name="action">Callback for parsing frame items</param>
        /// <returns>Frame | null</returns>
        public static PackageV2 FindPackageV2(List <Byte> array, Func <UInt16, UInt16, UInt32, PackageItemTypes> action)
        {
            //variables
            int count = array.Count;

            //find start byte
            for (int index = 0; index < count; index++)
            {
                //check start byte and length
                if (array[index] == 0x70 && (count - (index + 2)) >= 2)
                {
                    //get length from array
                    UInt16 length = (UInt16)(array[index + 2] << 8 | array[index + 1]);

                    //get command from array
                    UInt16 address = (UInt16)(array[index + 4] << 8 | array[index + 3]);

                    //get state
                    Byte state = array[index + 5];

                    //overime ci je dostatok dat na vytvorenie package
                    if (count >= (length - 1))
                    {
                        PackageV2 frame = PackageHelper.ConstructPackageV2(array, index + 6, length - 6, address, state, action);
                        if (frame != null)
                        {
                            //return package
                            return(frame);
                        }
                    }
                    else
                    {
                        //remove first data
                        if (index > 0)
                        {
                            array.RemoveRange(0, index + 1);
                        }

                        //nedostatok dat
                        return(null);
                    }
                }
            }

            //clear all data from buffer
            array.Clear();

            //any package
            return(null);
        }
        /// <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);
        }
Exemple #10
0
        /// <summary>
        /// Vytvori asynchronne volanie na metodu zabezpecujucu vytvorenie eventu
        /// oznamujuceho prijatie dat
        /// </summary>
        /// <param name="e">DataEventArgs</param>
        protected override void OnReceivedData(DataEventArgs e)
        {
            //base event
            base.OnReceivedData(e);

            //lock
            lock (this.m_lockObject)
            {
                //check buffer
                if (this.m_buffer == null)
                {
                    this.m_buffer = new List <Byte>();
                }

                //zalogujeme prijate dat
                this.InternalTrace(TraceTypes.Verbose, "Receiving data: [{0}]", e.Data.ToHexaString());

                //add data to buffer
                this.m_buffer.AddRange(e.Data);

                //loop
                while (true)
                {
                    //find frame
                    PackageV2 frame = PackageHelper.FindPackageV2(this.m_buffer, this.InternalGetPackageItemType);
                    if (frame != null)
                    {
                        //send receive event
                        this.OnReceivedPackage(new PackageEventArgs(frame, e.RemoteEndPoint));
                    }
                    else
                    {
                        //any data for frame
                        break;
                    }
                }
            }
        }
 /// <summary>
 /// Serializes the specified object to a Frame.
 /// </summary>
 /// <typeparam name="T">The type of the object to serialize to.</typeparam>
 /// <param name="obj">The object to serialize.</param>
 /// <param name="frame">Frame</param>
 /// <returns>Frame | null</returns>
 public static PackageV2 SerializeV2 <T>(List <T> obj, PackageV2 frame)
 {
     //intiailize object
     return(InternalSerializeV2 <T>(obj, frame));
 }
Exemple #12
0
 /// <summary>
 /// This function sends frame to transport layer
 /// </summary>
 /// <param name="package">Frame to send</param>
 /// <returns>True | false</returns>
 public virtual Boolean Send(PackageV2 package)
 {
     return(this.Write(package.ToByteArray()));
 }