コード例 #1
0
        /// <summary>
        /// Creates the message.
        /// </summary>
        /// <returns>Polled Event message.</returns>
        public static PolledEventMessage CreateMessage(params object[] values)
        {
            PolledEventMessage pe = new PolledEventMessage();

            pe.SetValues(values);
            return(pe);
        }
コード例 #2
0
        /// <summary>
        /// Creates the message.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns>Polled Event message.</returns>
        public static PolledEventMessage CreateMessage(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return(null);
            }
            ModuleProc         PROC    = new ModuleProc("PolledEventMessage", "CreateMessage");
            PolledEventMessage result  = default(PolledEventMessage);
            MemoryStream       ms      = null;
            Encoding           unicode = Encoding.Unicode;

            try
            {
                using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    StreamReader sr = new StreamReader(fs);

                    int actualLength  = (int)fs.Length;
                    int desiredLength = (int)fs.Length;

                    if (sr.CurrentEncoding != Encoding.Unicode)
                    {
                        desiredLength *= 2;
                    }

                    fs.Position = 0;
                    char[] originalData = sr.ReadToEnd().ToCharArray();
                    byte[] copiedData   = new byte[desiredLength];

                    fs.Position = 0;
                    unicode.GetBytes(originalData, 0, actualLength, copiedData, 0);

                    ms = new MemoryStream(copiedData);
                    sr.Close();
                }

                if (ms != null)
                {
                    result = ParseMessage(ms, Encoding.Unicode);
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
            finally
            {
                if (ms != null)
                {
                    ms.Close();
                    Extensions.DisposeObject(ref ms);
                }
            }

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Gets the message string.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns>Message stream.</returns>
        public static Stream GetMessageStream(this PolledEventMessage message, Encoding encoding)
        {
            ModuleProc PROC   = new ModuleProc("", "GetMessageString");
            Stream     result = default(Stream);

            try
            {
                string rawData = message.RawMessageData;
                byte[] bytes   = encoding.GetBytes(rawData);
                result = new MemoryStream(bytes);
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }

            return(result);
        }
コード例 #4
0
        /// <summary>
        /// When implemented in a class, serializes an object into the body of the message.
        /// </summary>
        /// <param name="message">The <see cref="T:System.Messaging.Message"/> that will contain the serialized object.</param>
        /// <param name="obj">The object to be serialized into the message.</param>
        public void Write(Message message, object obj)
        {
            ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, "Write");

            try
            {
                if ((obj == null) ||
                    (obj.GetType() != typeof(PolledEventMessage)))
                {
                    Log.Info(PROC, "Invalid object passed.");
                    return;
                }

                PolledEventMessage objPE = obj as PolledEventMessage;
                message.BodyStream = objPE.GetMessageStream(Encoding.Unicode);
                message.BodyType   = VT_LPWSTR;
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
        }
コード例 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PolledEventMessageMeter"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 /// <param name="type">The type.</param>
 internal PolledEventMessageMeter(PolledEventMessage parent, PolledEventMessageMeterType type)
     : base(string.Empty)
 {
     _parent = parent;
     this.Type = type;
 }
コード例 #6
0
        ///// <returns>Polled Event message.</returns>
        //public static PolledEventMessage ParseMessage(Stream messageStream, Encoding encoding)
        //{
        //}

        /// <summary>
        /// Parses the message.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <returns>Polled Event message.</returns>
        public static PolledEventMessage ParseMessage(Stream messageStream, Encoding encoding)
        {
            ModuleProc         PROC   = new ModuleProc("QueueMessageFactory", "ParseMessage");
            PolledEventMessage result = default(PolledEventMessage);

            try
            {
                if (messageStream != null)
                {
                    string rawMessage = string.Empty;
                    messageStream.Position = 0;
                    try
                    {
                        int    length = (int)messageStream.Length;
                        byte[] bytes  = new byte[length];
                        messageStream.Read(bytes, 0, length);
                        rawMessage = encoding.GetString(bytes);
                    }
                    catch
                    {
                        Log.Info(PROC, "Unable to copy the messageStream stream.");
                    }
                    finally
                    {
                        messageStream.Dispose();
                        messageStream.Close();
                    }

                    XElement xRoot = XElement.Parse(rawMessage);
                    if (xRoot != null &&
                        xRoot.Name.LocalName.IgnoreCaseCompare("Polled_Event"))
                    {
                        result = new PolledEventMessage(rawMessage)
                        {
                            SiteCode       = xRoot.GetElementValue("Site_Code"),
                            BarPosition    = xRoot.GetElementValue("Bar_Pos"),
                            FaultSource    = xRoot.GetElementValueInt("Fault_Source"),
                            FaultType      = xRoot.GetElementValueInt("Fault_Type"),
                            InstallationNo = xRoot.GetElementValueInt("Serial_No"), /*xRoot.GetElementValueInt("Installation"),*/
                            FaultMessage   = xRoot.GetElementValue("Fault_Message"),
                            Date           = xRoot.GetElementValueDateTime("Date"),
                            EventValue     = xRoot.GetElementValue("Event_Value"),
                            Description    = xRoot.GetElementValue("Description"),
                        };

                        IEnumerable <XElement> counters = xRoot.GetElement("sector").GetElements("counter");
                        if (counters != null)
                        {
                            foreach (XElement counter in counters)
                            {
                                int  meterId    = counter.GetAttributeValueInt("counterid");
                                long meterValue = counter.GetTextValueInt64(-99);

                                // special cases
                                if (result.FaultSource == 21 &&
                                    result.FaultType == 20)
                                {
                                    result.MetersReceived.Add(meterId, meterValue);
                                    meterValue = 0;
                                }
                                result.Meters.Add(meterId, meterValue);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }

            return(result);
        }
コード例 #7
0
 /// <summary>
 /// Creates the message.
 /// </summary>
 /// <returns>Polled Event message.</returns>
 public static PolledEventMessage CreateMessage(params object[] values)
 {
     PolledEventMessage pe = new PolledEventMessage();
     pe.SetValues(values);
     return pe;
 }
コード例 #8
0
        ///// <returns>Polled Event message.</returns>
        //public static PolledEventMessage ParseMessage(Stream messageStream, Encoding encoding)
        //{
        //}

        /// <summary>
        /// Parses the message.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <returns>Polled Event message.</returns>
        public static PolledEventMessage ParseMessage(Stream messageStream, Encoding encoding)
        {
            ModuleProc PROC = new ModuleProc("QueueMessageFactory", "ParseMessage");
            PolledEventMessage result = default(PolledEventMessage);

            try
            {
                if (messageStream != null)
                {
                    string rawMessage = string.Empty;
                    messageStream.Position = 0;
                    try
                    {
                        int length = (int)messageStream.Length;
                        byte[] bytes = new byte[length];
                        messageStream.Read(bytes, 0, length);
                        rawMessage = encoding.GetString(bytes);
                    }
                    catch
                    {
                        Log.Info(PROC, "Unable to copy the messageStream stream.");
                    }
                    finally
                    {
                        messageStream.Dispose();
                        messageStream.Close();
                    }
                    
                    XElement xRoot = XElement.Parse(rawMessage);
                    if (xRoot != null &&
                        xRoot.Name.LocalName.IgnoreCaseCompare("Polled_Event"))
                    {
                        result = new PolledEventMessage(rawMessage)
                        {
                            SiteCode = xRoot.GetElementValue("Site_Code"),
                            BarPosition = xRoot.GetElementValue("Bar_Pos"),
                            FaultSource = xRoot.GetElementValueInt("Fault_Source"),
                            FaultType = xRoot.GetElementValueInt("Fault_Type"),
                            InstallationNo = xRoot.GetElementValueInt("Serial_No"), /*xRoot.GetElementValueInt("Installation"),*/
                            FaultMessage = xRoot.GetElementValue("Fault_Message"),
                            Date = xRoot.GetElementValueDateTime("Date"),
                            EventValue = xRoot.GetElementValue("Event_Value"),
                            Description = xRoot.GetElementValue("Description"),
                        };

                        IEnumerable<XElement> counters = xRoot.GetElement("sector").GetElements("counter");
                        if (counters != null)
                        {
                            foreach (XElement counter in counters)
                            {
                                int meterId = counter.GetAttributeValueInt("counterid");
                                long meterValue = counter.GetTextValueInt64(-99);

                                // special cases
                                if (result.FaultSource == 21 && 
                                    result.FaultType == 20)
                                {
                                    result.MetersReceived.Add(meterId, meterValue);
                                    meterValue = 0;
                                }
                                result.Meters.Add(meterId, meterValue);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }

            return result;
        }
コード例 #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PolledEventMessageMeter"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 /// <param name="type">The type.</param>
 internal PolledEventMessageMeter(PolledEventMessage parent, PolledEventMessageMeterType type)
     : base(string.Empty)
 {
     _parent   = parent;
     this.Type = type;
 }
コード例 #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PolledEventMessageMeters"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 internal PolledEventMessageMeters(PolledEventMessage parent)
     : base(string.Empty)
 {
     _parent        = parent;
     _currentMeters = new SortedDictionary <PolledEventMessageMeterType, PolledEventMessageMeter>();
 }
コード例 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PolledEventMessageMeters"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 internal PolledEventMessageMeters(PolledEventMessage parent)
     : base(string.Empty)
 {
     _parent = parent;
     _currentMeters = new SortedDictionary<PolledEventMessageMeterType, PolledEventMessageMeter>();
 }