private void button_BufferedMessages_CreateRandom_Click(object sender, EventArgs e)
        {
            if (comboBox_BufferedMessages_CreateRandom_Type.SelectedIndex < 0)
            {
                return;
            }

            int iMessageCount;

            if (Int32.TryParse(textBox_CreateRandomMessages_Count.Text, out iMessageCount) == false || iMessageCount > 30000)
            {
                MessageBox.Show("Invalid number or too many messages (>30000)", "Create buffered messages", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (iMessageCount <= 0)
            {
                return;
            }

            Cursor.Current = Cursors.WaitCursor;
            Application.DoEvents();

            List <cAlarmObject>       AlarmObjects                    = new List <cAlarmObject>();
            List <cStatusReturnValue> StatusReturnValues              = new List <cStatusReturnValue>();
            List <cStatusReturnValue> ValidStatusReturnValues         = new List <cStatusReturnValue>();
            List <cRoadSideObject>    AggregatedStatusRoadSideObjects = new List <nsRSMPGS.cRoadSideObject>();

            foreach (cRoadSideObject RoadSideObject in RSMPGS.ProcessImage.RoadSideObjects.Values)
            {
                AlarmObjects.AddRange(RoadSideObject.AlarmObjects);
                foreach (cStatusObject StatusObject in RoadSideObject.StatusObjects)
                {
                    StatusReturnValues.AddRange(StatusObject.StatusReturnValues);
                    ValidStatusReturnValues.AddRange(StatusObject.StatusReturnValues.FindAll(srv => srv.Value.Quality == cValue.eQuality.recent));
                }
                if (RoadSideObject.bIsComponentGroup)
                {
                    AggregatedStatusRoadSideObjects.Add(RoadSideObject);
                }
            }

            Random rnd = new Random();

            ListView_BufferedMessages.StopSorting();
            ListView_BufferedMessages.BeginUpdate();

            //  RSMPGS.ProcessImage.AggregatedStatusObjects
            string sSendBuffer;

            for (int iIndex = 0; iIndex < iMessageCount; iIndex++)
            {
                switch (comboBox_BufferedMessages_CreateRandom_Type.SelectedIndex)
                {
                case 0:

                    if (AlarmObjects.Count > 0)
                    {
                        cAlarmObject AlarmObject = AlarmObjects[rnd.Next(0, AlarmObjects.Count - 1)];
                        cJSon.AlarmSpecialisation        alarmSpecialisation = new cJSon.AlarmSpecialisation[] { cJSon.AlarmSpecialisation.Acknowledge, cJSon.AlarmSpecialisation.Issue, cJSon.AlarmSpecialisation.Suspend }[rnd.Next(0, 2)];
                        RSMP_Messages.AlarmHeaderAndBody alarmHeaderAndBody  = RSMPGS.JSon.CreateAndSendAlarmMessage(AlarmObject, alarmSpecialisation, true, out sSendBuffer);
                        cBufferedMessage BufferedMessage = new cBufferedMessage(cBufferedMessage.eMessageType.Alarm, alarmHeaderAndBody.type, alarmHeaderAndBody.mId, sSendBuffer);
                        AddBufferedMessageToListAndListView(BufferedMessage);
                    }
                    break;

                case 1:

                    if (AggregatedStatusRoadSideObjects.Count > 0)
                    {
                        cRoadSideObject RoadSideObject = AggregatedStatusRoadSideObjects[rnd.Next(0, AggregatedStatusRoadSideObjects.Count - 1)];
                        RSMP_Messages.AggregatedStatus aggregatedStatus = RSMPGS.JSon.CreateAndSendAggregatedStatusMessage(RoadSideObject, true, out sSendBuffer);
                        cBufferedMessage BufferedMessage = new cBufferedMessage(cBufferedMessage.eMessageType.AggregatedStatus, aggregatedStatus.type, aggregatedStatus.mId, sSendBuffer);
                        AddBufferedMessageToListAndListView(BufferedMessage);
                    }
                    break;

                case 2:

                    if (StatusReturnValues.Count > 0)
                    {
                        cStatusReturnValue StatusReturnValue = StatusReturnValues[rnd.Next(0, StatusReturnValues.Count - 1)];
                        List <RSMP_Messages.Status_VTQ> sS   = new List <RSMP_Messages.Status_VTQ>();
                        RSMP_Messages.Status_VTQ        s    = new RSMP_Messages.Status_VTQ();
                        s.sCI = StatusReturnValue.StatusObject.sStatusCodeId;
                        s.n   = StatusReturnValue.sName;
                        s.q   = StatusReturnValue.Value.Quality.ToString();
                        s.s   = StatusReturnValue.Value.Quality == cValue.eQuality.unknown ? null : StatusReturnValue.Value.GetValue();
                        sS.Add(s);

                        RSMP_Messages.StatusUpdate statusUpdate    = RSMPGS.JSon.CreateAndSendStatusUpdateMessage(StatusReturnValue.StatusObject.RoadSideObject, sS, true, out sSendBuffer);
                        cBufferedMessage           BufferedMessage = new cBufferedMessage(cBufferedMessage.eMessageType.Status, statusUpdate.type, statusUpdate.mId, sSendBuffer);
                        AddBufferedMessageToListAndListView(BufferedMessage);
                    }
                    break;

                case 3:

                    if (ValidStatusReturnValues.Count > 0)
                    {
                        cStatusReturnValue StatusReturnValue = ValidStatusReturnValues[rnd.Next(0, ValidStatusReturnValues.Count - 1)];
                        List <RSMP_Messages.Status_VTQ> sS   = new List <RSMP_Messages.Status_VTQ>();
                        RSMP_Messages.Status_VTQ        s    = new RSMP_Messages.Status_VTQ();
                        s.sCI = StatusReturnValue.StatusObject.sStatusCodeId;
                        s.n   = StatusReturnValue.sName;
                        s.q   = StatusReturnValue.Value.Quality.ToString();
                        s.s   = StatusReturnValue.Value.Quality == cValue.eQuality.unknown ? null : StatusReturnValue.Value.GetValue();
                        sS.Add(s);

                        RSMP_Messages.StatusUpdate statusUpdate    = RSMPGS.JSon.CreateAndSendStatusUpdateMessage(StatusReturnValue.StatusObject.RoadSideObject, sS, true, out sSendBuffer);
                        cBufferedMessage           BufferedMessage = new cBufferedMessage(cBufferedMessage.eMessageType.Status, statusUpdate.type, statusUpdate.mId, sSendBuffer);
                        AddBufferedMessageToListAndListView(BufferedMessage);
                    }
                    break;
                }

                //cBufferedMessage
                //AddBufferedMessageToListAndListView(cBufferedMessage BufferedMessage)
            }

            ListView_BufferedMessages.EndUpdate();
            ListView_BufferedMessages.ResumeSorting();

            Cursor.Current = Cursors.Default;
        }
示例#2
0
        public RSMP_Messages.AlarmHeaderAndBody CreateAndSendAlarmMessage(cAlarmObject AlarmObject, AlarmSpecialisation alarmSpecialisation, bool bCreateMessageOnly, out string sSendBuffer, out cAlarmEvent AlarmEvent)
        {
            sSendBuffer = "";

            RSMP_Messages.AlarmHeaderAndBody AlarmHeaderAndBody;
            //      RSMP_Messages.AlarmHeader AlarmHeader;


            AlarmHeaderAndBody = new RSMP_Messages.AlarmHeaderAndBody();

            AlarmEvent = new cAlarmEvent();

            try
            {
                AlarmHeaderAndBody.mType  = "rSMsg";
                AlarmHeaderAndBody.type   = "Alarm";
                AlarmHeaderAndBody.mId    = System.Guid.NewGuid().ToString();
                AlarmHeaderAndBody.ntsOId = AlarmObject.RoadSideObject.sNTSObjectId;
                AlarmHeaderAndBody.xNId   = AlarmObject.RoadSideObject.sExternalNTSId;
                AlarmHeaderAndBody.cId    = AlarmObject.RoadSideObject.sComponentId;
                AlarmHeaderAndBody.aCId   = AlarmObject.sAlarmCodeId;
                AlarmHeaderAndBody.xACId  = AlarmObject.sExternalAlarmCodeId;
                AlarmHeaderAndBody.xNACId = AlarmObject.sExternalNTSAlarmCodeId;
                AlarmHeaderAndBody.rvs    = new List <RSMP_Messages.AlarmReturnValue>();


                AlarmEvent.AlarmObject = AlarmObject;

                AlarmHeaderAndBody.ack = AlarmObject.bAcknowledged ? "Acknowledged" : "notAcknowledged";
                AlarmHeaderAndBody.aS  = AlarmObject.bActive ? "Active" : "inActive";
                AlarmHeaderAndBody.sS  = AlarmObject.bSuspended ? "Suspended" : "notSuspended";

                switch (alarmSpecialisation)
                {
                case AlarmSpecialisation.Issue:
                    AlarmHeaderAndBody.aSp = "Issue";
                    AlarmEvent.sEvent      = AlarmHeaderAndBody.aSp + " / " + AlarmHeaderAndBody.aS;
                    break;

                case AlarmSpecialisation.Acknowledge:
                    AlarmHeaderAndBody.aSp = "Acknowledge";
                    AlarmEvent.sEvent      = AlarmHeaderAndBody.aSp + " / " + AlarmHeaderAndBody.ack;
                    break;

                case AlarmSpecialisation.Suspend:
                    AlarmHeaderAndBody.aSp = "Suspend";
                    AlarmEvent.sEvent      = AlarmHeaderAndBody.aSp + " / " + AlarmHeaderAndBody.sS;
                    break;
                }

                if (AlarmObject.bActive == false && AlarmObject.bAcknowledged)
                {
                    AlarmObject.AlarmCount = 0;
                }

                if (AlarmObject.dtLastChangedAlarmStatus == DateTime.MinValue)
                {
                    AlarmObject.dtLastChangedAlarmStatus = DateTime.Now;
                }

                AlarmHeaderAndBody.aTs = CreateISO8601UTCTimeStamp(AlarmObject.dtLastChangedAlarmStatus);
                AlarmHeaderAndBody.cat = AlarmObject.sCategory;
                AlarmHeaderAndBody.pri = AlarmObject.sPriority;

                AlarmEvent.sAlarmCodeId = AlarmObject.sAlarmCodeId;
                AlarmEvent.sDirection   = "Sent";
                AlarmEvent.sTimeStamp   = UnpackISO8601UTCTimeStamp(AlarmHeaderAndBody.aTs); // String.Format("{0:yyyy-MM-dd}T{0:HH:mm:ss.fff}", UnpackISO8601UTCTimeStamp(AlarmHeaderAndBody.aTs));
                AlarmEvent.sMessageId   = AlarmHeaderAndBody.mId;

                foreach (cAlarmReturnValue AlarmReturnValue in AlarmObject.AlarmReturnValues)
                {
                    RSMP_Messages.AlarmReturnValue rv = new RSMP_Messages.AlarmReturnValue();
                    rv.n = AlarmReturnValue.sName;
                    rv.v = AlarmReturnValue.Value.GetValue();
                    AlarmHeaderAndBody.rvs.Add(rv);
                    AlarmEvent.AlarmEventReturnValues.Add(new nsRSMPGS.cAlarmEventReturnValue(rv.n, rv.v));
                }

                sSendBuffer = JSonSerializer.SerializeObject(AlarmHeaderAndBody);

                if (bCreateMessageOnly)
                {
                    return(AlarmHeaderAndBody);
                }

                if (RSMPGS.JSon.SendJSonPacket(AlarmHeaderAndBody.type, AlarmHeaderAndBody.mId, sSendBuffer, true))
                {
                    if (RSMPGS.MainForm.checkBox_ViewOnlyFailedPackets.Checked == false)
                    {
                        RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Sent alarm message, AlarmCode: " + AlarmObject.sAlarmCodeId + ", Type: " + AlarmHeaderAndBody.type + "/" + AlarmHeaderAndBody.aSp + "/" + AlarmHeaderAndBody.aS + ", MsgId: " + AlarmHeaderAndBody.mId);//, SequenceNumber: " + AlarmHeaderAndBody.sNr);
                    }
                }
                else
                {
                    if (cHelper.IsSettingChecked("BufferAndSendAlarmsWhenConnect"))
                    {
                        cBufferedMessage BufferedMessage = new cBufferedMessage(cBufferedMessage.eMessageType.Alarm, AlarmHeaderAndBody.type, AlarmHeaderAndBody.mId, sSendBuffer);
                        RSMPGS.MainForm.AddBufferedMessageToListAndListView(BufferedMessage);
                        RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Warning, "Buffered alarm message, AlarmCode: " + AlarmObject.sAlarmCodeId + ", Type: " + AlarmHeaderAndBody.type + "/" + AlarmHeaderAndBody.aSp + "/" + AlarmHeaderAndBody.aS + ", MsgId: " + AlarmHeaderAndBody.mId);// + ", SequenceNumber: " + AlarmHeaderAndBody.sNr);
                    }
                }

                //        RSMPGS.ProcessImage.SequenceNumber_Alarm++;
            }
            catch (Exception e)
            {
                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Failed to create alarm message: {0}", e.Message);
                AlarmEvent = null;
            }

            return(AlarmHeaderAndBody);
        }
        private bool DecodeAndParseAlarmMessage(RSMP_Messages.Header packetHeader, string sJSon, bool bUseStrictProtocolAnalysis, bool bUseCaseSensitiveIds, ref bool bHasSentAckOrNack, ref string sError)
        {
            StringComparison sc = bUseCaseSensitiveIds ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

            bool bPacketWasProperlyHandled = false;

            try
            {
                RSMP_Messages.AlarmHeaderAndBody AlarmHeader = JSonSerializer.Deserialize <RSMP_Messages.AlarmHeaderAndBody>(sJSon);

                if (AlarmHeader.cat != null && AlarmHeader.cat != "")
                {
                    cRoadSideObject RoadSideObject = cHelper.FindRoadSideObject(AlarmHeader.ntsOId, AlarmHeader.cId, bUseCaseSensitiveIds);
                    if (RoadSideObject != null)
                    {
                        foreach (cAlarmObject AlarmObject in RoadSideObject.AlarmObjects)
                        {
                            if (AlarmObject.sAlarmCodeId.Equals(AlarmHeader.aCId, sc))
                            {
                                cAlarmEvent AlarmEvent = new cAlarmEvent();
                                AlarmEvent.AlarmObject  = AlarmObject;
                                AlarmEvent.sDirection   = "Received";
                                AlarmEvent.sTimeStamp   = UnpackISO8601UTCTimeStamp(AlarmHeader.aTs);
                                AlarmEvent.sMessageId   = AlarmHeader.mId;
                                AlarmEvent.sAlarmCodeId = AlarmHeader.aCId;

                                //foreach (cAlarmReturnValue AlarmReturnValue in AlarmHeader.rvs)
                                if (AlarmHeader.rvs != null)
                                {
                                    foreach (RSMP_Messages.AlarmReturnValue AlarmReturnValue in AlarmHeader.rvs)
                                    {
                                        AlarmEvent.AlarmEventReturnValues.Add(new cAlarmEventReturnValue(AlarmReturnValue.n, AlarmReturnValue.v));
                                    }
                                }
                                switch (AlarmHeader.aSp.ToLower())
                                {
                                case "issue":
                                    AlarmEvent.sEvent = AlarmHeader.aSp + " / " + AlarmHeader.aS;
                                    if (AlarmHeader.aS.Equals("active", StringComparison.OrdinalIgnoreCase))
                                    {
                                        AlarmObject.AlarmCount++;
                                    }
                                    bPacketWasProperlyHandled = true;
                                    break;

                                case "acknowledge":
                                    AlarmEvent.sEvent         = AlarmHeader.aSp + " / " + AlarmHeader.ack;
                                    bPacketWasProperlyHandled = true;
                                    break;

                                case "suspend":
                                    AlarmEvent.sEvent         = AlarmHeader.aSp + " / " + AlarmHeader.sS;
                                    bPacketWasProperlyHandled = true;
                                    break;

                                default:
                                    AlarmEvent.sEvent = "(unknown: " + AlarmHeader.aSp + ")";
                                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Warning, "Could not parse correct alarm state {0} (corresponding MsgId {1}) ", AlarmHeader.aSp, AlarmHeader.mId);
                                    break;
                                }

                                if (bPacketWasProperlyHandled)
                                {
                                    AlarmObject.bActive = AlarmHeader.aS.Equals("active", StringComparison.OrdinalIgnoreCase)
                      ? true : false;
                                    AlarmObject.bAcknowledged = AlarmHeader.ack.Equals("acknowledged", StringComparison.OrdinalIgnoreCase)
                      ? true : false;
                                    AlarmObject.bSuspended = AlarmHeader.sS.Equals("suspended", StringComparison.OrdinalIgnoreCase)
                      ? true : false;
                                    if (AlarmObject.bActive == false && AlarmObject.bAcknowledged)
                                    {
                                        AlarmObject.AlarmCount = 0;
                                    }
                                }
                                RSMPGS.MainForm.AddAlarmEventToAlarmObjectAndToList(AlarmObject, AlarmEvent);
                                RSMPGS.MainForm.UpdateAlarmListView(AlarmObject);
                                break;
                            }
                        }
                    }
                    if (bPacketWasProperlyHandled == false)
                    {
                        sError = "Failed to handle Alarm message, could not find object, ntsOId: '" + AlarmHeader.ntsOId + "', cId: '" + AlarmHeader.cId + "', aCId: '" + AlarmHeader.aCId + "'";
                    }
                }
                else
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Got alarm message from SCADA, aSp: {0} (corresponding MsgId {1}) ", AlarmHeader.aSp, AlarmHeader.mId);
                }
            }
            catch (Exception e)
            {
                sError = "Failed to deserialize packet: " + e.Message;
                bPacketWasProperlyHandled = false;
            }

            return(bPacketWasProperlyHandled);
        }