public void CreateAndSendAggregatedStatusRequestMessage(cRoadSideObject RoadSideObject)
        {
            RSMP_Messages.AggregatedStatusRequest AggregatedStatusRequest;

            string sSendBuffer;

            try
            {
                AggregatedStatusRequest = new RSMP_Messages.AggregatedStatusRequest();

                AggregatedStatusRequest.mType = "rSMsg";
                AggregatedStatusRequest.type  = "AggregatedStatusRequest";
                AggregatedStatusRequest.mId   = System.Guid.NewGuid().ToString();

                AggregatedStatusRequest.ntsOId = RoadSideObject.sNTSObjectId;
                AggregatedStatusRequest.xNId   = RoadSideObject.sExternalNTSId;
                AggregatedStatusRequest.cId    = RoadSideObject.sComponentId;

                sSendBuffer = JSonSerializer.SerializeObject(AggregatedStatusRequest);

                RSMPGS.JSon.SendJSonPacket(AggregatedStatusRequest.type, AggregatedStatusRequest.mId, sSendBuffer, true);

                if (RSMPGS.MainForm.checkBox_ViewOnlyFailedPackets.Checked == false)
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Sent AggregatedStatusRequest message, ntsOId: " + RoadSideObject.sNTSObjectId + ", cId: " + RoadSideObject.sComponentId + ", MsgId: " + AggregatedStatusRequest.mId);
                }
            }
            catch (Exception e)
            {
                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Failed to create AggregatedStatusRequest message: {0}", e.Message);
            }
        }
Beispiel #2
0
        public RSMP_Messages.AggregatedStatus CreateAndSendAggregatedStatusMessage(cRoadSideObject RoadSideObject, bool bCreateMessageOnly, out string sSendBuffer)
        {
            sSendBuffer = "";

            RSMP_Messages.AggregatedStatus AggregatedStatusMessage = new RSMP_Messages.AggregatedStatus();

            AggregatedStatusMessage.mType  = "rSMsg";
            AggregatedStatusMessage.type   = "AggregatedStatus";
            AggregatedStatusMessage.mId    = System.Guid.NewGuid().ToString();
            AggregatedStatusMessage.ntsOId = RoadSideObject.sNTSObjectId;
            AggregatedStatusMessage.xNId   = RoadSideObject.sExternalNTSId;
            AggregatedStatusMessage.cId    = RoadSideObject.sComponentId;

            if (RoadSideObject.dtLastChangedAggregatedStatus == DateTime.MinValue)
            {
                RoadSideObject.dtLastChangedAggregatedStatus = DateTime.Now;
            }

            AggregatedStatusMessage.aSTS = CreateISO8601UTCTimeStamp(RoadSideObject.dtLastChangedAggregatedStatus);

            AggregatedStatusMessage.fP = RoadSideObject.sFunctionalPosition.Length > 0 ? RoadSideObject.sFunctionalPosition : null;
            AggregatedStatusMessage.fS = RoadSideObject.sFunctionalState.Length > 0 ? RoadSideObject.sFunctionalState : null;

            AggregatedStatusMessage.se = (bool[])RoadSideObject.bBitStatus.Clone();

            for (int iIndex = 0; iIndex < RoadSideObject.bBitStatus.GetLength(0); iIndex++)
            {
                AggregatedStatusMessage.se[iIndex] = RoadSideObject.bBitStatus[iIndex];
            }

            sSendBuffer = JSonSerializer.SerializeObject(AggregatedStatusMessage);

            if (bCreateMessageOnly)
            {
                return(AggregatedStatusMessage);
            }

            if (RSMPGS.JSon.SendJSonPacket(AggregatedStatusMessage.type, AggregatedStatusMessage.mId, sSendBuffer, true))
            {
                if (RSMPGS.MainForm.checkBox_ViewOnlyFailedPackets.Checked == false)
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Sent aggregated status message, ntsOId: " + AggregatedStatusMessage.ntsOId + ", Type: " + AggregatedStatusMessage.type + ", MsgId: " + AggregatedStatusMessage.mId);
                }
            }
            else
            {
                if (cHelper.IsSettingChecked("BufferAndSendAggregatedStatusWhenConnect"))
                {
                    cBufferedMessage BufferedMessage = new cBufferedMessage(cBufferedMessage.eMessageType.AggregatedStatus, AggregatedStatusMessage.type, AggregatedStatusMessage.mId, sSendBuffer);
                    RSMPGS.MainForm.AddBufferedMessageToListAndListView(BufferedMessage);
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Warning, "Buffered aggregated status message, ntsOId: " + AggregatedStatusMessage.ntsOId + ", Type: " + AggregatedStatusMessage.type + ", MsgId: " + AggregatedStatusMessage.mId);
                }
            }

            return(AggregatedStatusMessage);
        }
        public void CreateAndSendAlarmMessage(cAlarmObject AlarmObject, AlarmSpecialisation alarmSpecialisation)
        {
            RSMP_Messages.AlarmHeader AlarmHeader;
            cAlarmEvent AlarmEvent = null;
            string      sSendBuffer;

            try
            {
                AlarmHeader = new RSMP_Messages.AlarmHeader();

                AlarmHeader.mType = "rSMsg";
                AlarmHeader.type  = "Alarm";
                AlarmHeader.mId   = System.Guid.NewGuid().ToString();

                AlarmHeader.ntsOId = AlarmObject.RoadSideObject.sNTSObjectId;
                AlarmHeader.xNId   = AlarmObject.RoadSideObject.sExternalNTSId;
                AlarmHeader.cId    = AlarmObject.RoadSideObject.sComponentId;
                AlarmHeader.aCId   = AlarmObject.sAlarmCodeId;
                AlarmHeader.xACId  = AlarmObject.sExternalAlarmCodeId;
                AlarmHeader.xNACId = AlarmObject.sExternalNTSAlarmCodeId;

                AlarmHeader.aSp = alarmSpecialisation.ToString();

                sSendBuffer = JSonSerializer.SerializeObject(AlarmHeader);

                AlarmEvent              = new cAlarmEvent();
                AlarmEvent.AlarmObject  = AlarmObject;
                AlarmEvent.sDirection   = "Sent";
                AlarmEvent.sTimeStamp   = CreateLocalTimeStamp();
                AlarmEvent.sMessageId   = AlarmHeader.mId;
                AlarmEvent.sAlarmCodeId = AlarmHeader.aCId;
                AlarmEvent.sEvent       = AlarmHeader.aSp;

                RSMPGS.MainForm.AddAlarmEventToAlarmObjectAndToList(AlarmObject, AlarmEvent);
                RSMPGS.MainForm.UpdateAlarmListView(AlarmObject);

                /*
                 * if (RSMPGS_Main.bWriteEventsContinous)
                 * {
                 * RSMPGS.SysLog.EventLog("Alarm;{0}\tMId: {1}\tComponentId: {2}\tAlarmCodeId: {3}\tEvent: {4}",
                 *    AlarmEvent.sTimeStamp, AlarmEvent.sMessageId, AlarmHeader.cId, AlarmEvent.sAlarmCodeId, AlarmEvent.sEvent);
                 * }
                 */
                RSMPGS.JSon.SendJSonPacket(AlarmHeader.type, AlarmHeader.mId, sSendBuffer, true);
                if (RSMPGS.MainForm.checkBox_ViewOnlyFailedPackets.Checked == false)
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Sent alarm message, AlarmCode: " + AlarmObject.sAlarmCodeId + ", Type: " + AlarmHeader.type + "/" + AlarmHeader.aSp + ", MsgId: " + AlarmHeader.mId);
                }
            }
            catch (Exception e)
            {
                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Failed to create alarm message: {0}", e.Message);
            }
        }
        public void CreateAndSendSubscriptionMessage_upto_3_1_4(cRoadSideObject RoadSideObject, List <RSMP_Messages.StatusSubscribe_Status_Over_3_1_4> StatusSubscribeValues)
        {
            RSMP_Messages.StatusSubscribe_UpTo_3_1_4 StatusSubscribe;

            string sSendBuffer;

            try
            {
                StatusSubscribe = new RSMP_Messages.StatusSubscribe_UpTo_3_1_4();

                StatusSubscribe.mType = "rSMsg";
                StatusSubscribe.type  = "StatusSubscribe";
                StatusSubscribe.mId   = System.Guid.NewGuid().ToString();

                StatusSubscribe.ntsOId = RoadSideObject.sNTSObjectId;
                StatusSubscribe.xNId   = RoadSideObject.sExternalNTSId;
                StatusSubscribe.cId    = RoadSideObject.sComponentId;
                StatusSubscribe.sS     = new List <RSMP_Messages.StatusSubscribe_Status_UpTo_3_1_4>();

                foreach (RSMP_Messages.StatusSubscribe_Status_Over_3_1_4 StatusSubscriptionValue in StatusSubscribeValues)
                {
                    RSMP_Messages.StatusSubscribe_Status_UpTo_3_1_4 StatusSubscribeValue = new RSMP_Messages.StatusSubscribe_Status_UpTo_3_1_4();

                    StatusSubscribeValue.n   = StatusSubscriptionValue.n;
                    StatusSubscribeValue.sCI = StatusSubscriptionValue.sCI;
                    StatusSubscribeValue.uRt = StatusSubscriptionValue.uRt;

                    StatusSubscribe.sS.Add(StatusSubscribeValue);

                    AddSubscriptionMessageEvent(RoadSideObject, StatusSubscribe.mId, StatusSubscribe.cId, StatusSubscriptionValue);
                }

                sSendBuffer = JSonSerializer.SerializeObject(StatusSubscribe);
                if (RSMPGS.JSon.SendJSonPacket(StatusSubscribe.type, StatusSubscribe.mId, sSendBuffer, true))
                {
                    if (RSMPGS.MainForm.checkBox_ViewOnlyFailedPackets.Checked == false)
                    {
                        RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Sent subscription message, ComponentId: " + StatusSubscribe.cId + " , MsgId: " + StatusSubscribe.mId);
                    }
                }
                else
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Failed to send subscription message, ComponentId: " + StatusSubscribe.cId + " , MsgId: " + StatusSubscribe.mId);
                }
            }
            catch (Exception e)
            {
                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Failed to create subscription message: {0}", e.Message);
            }
        }
Beispiel #5
0
        public RSMP_Messages.StatusUpdate CreateAndSendStatusUpdateMessage(cRoadSideObject RoadSideObject, List <RSMP_Messages.Status_VTQ> sS, bool bCreateMessageOnly, out string sSendBuffer)
        {
            sSendBuffer = "";

            RSMP_Messages.StatusUpdate StatusUpdateMessage = new RSMP_Messages.StatusUpdate();

            StatusUpdateMessage.mType = "rSMsg";
            StatusUpdateMessage.type  = "StatusUpdate";
            StatusUpdateMessage.mId   = System.Guid.NewGuid().ToString();

            StatusUpdateMessage.ntsOId = RoadSideObject.sNTSObjectId;
            StatusUpdateMessage.xNId   = RoadSideObject.sExternalNTSId;
            StatusUpdateMessage.cId    = RoadSideObject.sComponentId;

            StatusUpdateMessage.sTs = CreateISO8601UTCTimeStamp();

            StatusUpdateMessage.sS = sS;

            sSendBuffer = JSonSerializer.SerializeObject(StatusUpdateMessage);

            if (bCreateMessageOnly)
            {
                return(StatusUpdateMessage);
            }

            if (RSMPGS.JSon.SendJSonPacket(StatusUpdateMessage.type, StatusUpdateMessage.mId, sSendBuffer, true))
            {
                if (RSMPGS.MainForm.checkBox_ViewOnlyFailedPackets.Checked == false)
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Sent Status Update message, Type: " + StatusUpdateMessage.type + ", MsgId: " + StatusUpdateMessage.mId);
                }
            }
            else
            {
                if (cHelper.IsSettingChecked("BufferAndSendStatusUpdatesWhenConnect"))
                {
                    cBufferedMessage BufferedMessage = new cBufferedMessage(cBufferedMessage.eMessageType.Status, StatusUpdateMessage.type, StatusUpdateMessage.mId, sSendBuffer);
                    RSMPGS.MainForm.AddBufferedMessageToListAndListView(BufferedMessage);
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Warning, "Buffered Status Update message, Type: " + StatusUpdateMessage.type + ", MsgId: " + StatusUpdateMessage.mId);
                }
            }

            return(StatusUpdateMessage);
        }
Beispiel #6
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);
        }
Beispiel #7
0
        private bool DecodeAndParseStatusMessage(RSMP_Messages.Header packetHeader, StatusMsgType statusMsgType, string sJSon, bool bUseStrictProtocolAnalysis, bool bUseCaseSensitiveIds, ref bool bHasSentAckOrNack, ref string sError)
        {
            StringComparison sc = bUseCaseSensitiveIds ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

            bool bSuccess = true;

            // Values to return
            List <RSMP_Messages.Status_VTQ> sS = new List <RSMP_Messages.Status_VTQ>();

            try
            {
                // StatusSubscribe, StatusUnsubscribe and StatusRequest are very much alike, differns by the uRt property only
                RSMP_Messages.StatusSubscribe_Over_3_1_4 StatusSubscribe = JSonSerializer.Deserialize <RSMP_Messages.StatusSubscribe_Over_3_1_4>(sJSon);

                foreach (RSMP_Messages.StatusSubscribe_Status_Over_3_1_4 StatusSubscribe_Status in StatusSubscribe.sS)
                {
                    if (StatusSubscribe_Status.sCI == null)
                    {
                        sError = "StatusCode Id (sCI) in " + packetHeader.type + " is missing";
                        return(false);
                    }
                }

                cRoadSideObject RoadSideObject = cHelper.FindRoadSideObject(StatusSubscribe.ntsOId, StatusSubscribe.cId, bUseCaseSensitiveIds);

                if (RoadSideObject != null)
                {
                    foreach (RSMP_Messages.StatusSubscribe_Status_Over_3_1_4 StatusSubscribe_Status in StatusSubscribe.sS)
                    {
                        RSMP_Messages.Status_VTQ s = new RSMP_Messages.Status_VTQ();
                        s.sCI = StatusSubscribe_Status.sCI;
                        s.n   = StatusSubscribe_Status.n;
                        s.s   = null;
                        // 3.1.1 = unknown
                        //s.q = "unknown";
                        // 3.1.2 = undefined ??
                        s.q = "undefined";
                        // Find status in object
                        cStatusObject      StatusObject      = RoadSideObject.StatusObjects.Find(x => x.sStatusCodeId.Equals(StatusSubscribe_Status.sCI, sc));
                        cStatusReturnValue StatusReturnValue = null;
                        if (StatusObject != null)
                        {
                            StatusReturnValue = StatusObject.StatusReturnValues.Find(x => x.sName.Equals(StatusSubscribe_Status.n, sc));
                        }
                        if (StatusReturnValue != null)
                        {
                            RSMPGS.ProcessImage.UpdateStatusValue(ref s, StatusReturnValue.Value.GetValueType(), StatusReturnValue.Value.GetValue());
                            switch (statusMsgType)
                            {
                            case StatusMsgType.Request:
                                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Got status request (NTSObjectId: {0}, ComponentId: {1}, StatusCodeId: {2}, Name: {3}, Status: {4})", StatusSubscribe.ntsOId, StatusSubscribe.cId, StatusObject.sStatusCodeId, StatusReturnValue.sName, StatusReturnValue.Value.GetValue());
                                break;

                            case StatusMsgType.UnSubscribe:
                            case StatusMsgType.Subscribe:
                                // Delete subscription if it already exists
                                foreach (cSubscription Subscription in RoadSideObject.Subscriptions)
                                {
                                    if (Subscription.StatusReturnValue == StatusReturnValue)
                                    {
                                        RoadSideObject.Subscriptions.Remove(Subscription);
                                        break;
                                    }
                                }
                                if (statusMsgType == StatusMsgType.Subscribe)
                                {
                                    string sUpdateRate = StatusSubscribe_Status.uRt;
                                    float  fUpdateRate = 0;
                                    float.TryParse(StatusSubscribe_Status.uRt, out fUpdateRate);
                                    if (fUpdateRate == 0)
                                    {
                                        float.TryParse(StatusSubscribe_Status.uRt.Replace('.', ','), out fUpdateRate);
                                    }
                                    bool bAlwaysSendOnChange = StatusSubscribe_Status.sOc;
                                    RoadSideObject.Subscriptions.Add(new cSubscription(StatusObject, StatusReturnValue, fUpdateRate, bAlwaysSendOnChange));
                                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Got status subscribe (NTSObjectId: {0}, ComponentId: {1}. StatusCodeId: {2}, Name: {3}, Status: {4})", StatusSubscribe.ntsOId, StatusSubscribe.cId, StatusObject.sStatusCodeId, StatusReturnValue.sName, StatusReturnValue.Value.GetValue());
                                }
                                else
                                {
                                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Got status unsubscribe, removed subscription (NTSObjectId: {0}, ComponentId: {1}. StatusCodeId: {2}, Name: {3}, Status: {4})", StatusSubscribe.ntsOId, StatusSubscribe.cId, StatusObject.sStatusCodeId, StatusReturnValue.sName, StatusReturnValue.Value.GetValue());
                                }
                                break;
                            }
                        }
                        if (s.s == null)
                        {
                            RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Got status request/subscribe, failed to update StatusCodeId or Object (could be unknown value) (NTSObjectId: {0}, ComponentId: {1}, StatusCodeId: {2}))", StatusSubscribe.ntsOId, StatusSubscribe.cId, StatusSubscribe_Status.sCI);
                        }

                        sS.Add(s);
                    }
                }
                else
                {
                    // Failed, fill return list with 'unknown'
                    foreach (RSMP_Messages.StatusSubscribe_Status_Over_3_1_4 StatusSubscribe_Status in StatusSubscribe.sS)
                    {
                        RSMP_Messages.Status_VTQ s = new RSMP_Messages.Status_VTQ();
                        s.sCI = StatusSubscribe_Status.sCI;
                        s.n   = StatusSubscribe_Status.n;
                        s.s   = null;
                        // 3.1.1 = unknown
                        //s.q = "unknown";
                        // 3.1.2 = undefined ??
                        s.q = "undefined";
                        sS.Add(s);
                    }
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Got status message, failed to find object (NTSObjectId: {0}, ComponentId: {1})", StatusSubscribe.ntsOId, StatusSubscribe.cId);
                }

                if (statusMsgType != StatusMsgType.UnSubscribe)
                {
                    // Response message
                    RSMP_Messages.StatusResponse StatusResponse = new RSMP_Messages.StatusResponse();
                    // Send response to client
                    StatusResponse.mType  = "rSMsg";
                    StatusResponse.type   = (statusMsgType == StatusMsgType.Subscribe) ? "StatusUpdate" : "StatusResponse";
                    StatusResponse.mId    = System.Guid.NewGuid().ToString();
                    StatusResponse.ntsOId = StatusSubscribe.ntsOId;
                    StatusResponse.xNId   = StatusSubscribe.xNId;
                    StatusResponse.cId    = StatusSubscribe.cId;
                    StatusResponse.sTs    = CreateISO8601UTCTimeStamp();
                    StatusResponse.sS     = sS;
                    string sSendBuffer = JSonSerializer.SerializeObject(StatusResponse);
                    if (bHasSentAckOrNack == false)
                    {
                        bHasSentAckOrNack = SendPacketAck(true, packetHeader.mId, "");
                    }
                    RSMPGS.JSon.SendJSonPacket(StatusResponse.type, StatusResponse.mId, sSendBuffer, true);
                    if (RSMPGS.MainForm.checkBox_ViewOnlyFailedPackets.Checked == false)
                    {
                        RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Sent StatusResponse message, Type: " + StatusResponse.type + ", MsgId: " + StatusResponse.mId);
                    }
                }
            }
            catch (Exception e)
            {
                sError   = "Failed to deserialize packet: " + e.Message;
                bSuccess = false;
            }

            return(bSuccess);
        }
Beispiel #8
0
        private bool DecodeAndParseCommandMessage(RSMP_Messages.Header packetHeader, string sJSon, bool bUseStrictProtocolAnalysis, bool bUseCaseSensitiveIds, ref bool bHasSentAckOrNack, ref string sError)
        {
            StringComparison sc = bUseCaseSensitiveIds ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

            bool bSuccess = false;

            // Values to return
            List <RSMP_Messages.CommandResponse_Value> rvs = new List <RSMP_Messages.CommandResponse_Value>();

            //Dictionary<cRoadSideObject, cRoadSideObject> UpdatedRoadSideObjects = new Dictionary<cRoadSideObject, cRoadSideObject>();

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

                // Response message
                RSMP_Messages.CommandResponse CommandResponse = new RSMP_Messages.CommandResponse();

                bool bSomeValueWasBad = false;

                // Scan through each value to set
                foreach (RSMP_Messages.CommandRequest_Value CommandRequest_Value in CommandRequest.arg)
                {
                    // Create return value for each value to be set
                    RSMP_Messages.CommandResponse_Value rv = new RSMP_Messages.CommandResponse_Value();
                    rv.v   = null;
                    rv.n   = CommandRequest_Value.n;
                    rv.age = "undefined";

                    bool bFoundCommand = false;

                    cRoadSideObject RoadSideObject = cHelper.FindRoadSideObject(CommandRequest.ntsOId, CommandRequest.cId, bUseStrictProtocolAnalysis);

                    if (RoadSideObject != null)
                    {
                        // Find command in object
                        foreach (cCommandObject CommandObject in RoadSideObject.CommandObjects)
                        {
                            bool bDone = false;
                            // Find command name in command
                            foreach (cCommandReturnValue CommandReturnValue in CommandObject.CommandReturnValues)
                            {
                                if (CommandReturnValue.sName.Equals(CommandRequest_Value.n, sc) &&
                                    CommandReturnValue.sCommand.Equals(CommandRequest_Value.cO, sc))
                                {
                                    // Do some validation
                                    if (ValidateTypeAndRange(CommandReturnValue.Value.GetValueType(), CommandRequest_Value.v))
                                    {
                                        if (CommandReturnValue.Value.GetValueType().Equals("base64", StringComparison.OrdinalIgnoreCase))
                                        {
                                            if (RSMPGS.MainForm.ToolStripMenuItem_StoreBase64Updates.Checked)
                                            {
                                                CommandReturnValue.Value.SetValue(RSMPGS.SysLog.StoreBase64DebugData(CommandRequest_Value.v));
                                            }
                                        }
                                        else
                                        {
                                            CommandReturnValue.Value.SetValue(CommandRequest_Value.v);
                                        }
                                        rv.v   = CommandRequest_Value.v;
                                        rv.cCI = CommandRequest_Value.cCI;
                                        rv.age = "recent";
                                        RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Got Command, updated NTSObjectId: {0}, ComponentId: {1}, CommandCodeId: {2}, Name: {3}, Command: {4}, Value: {5}", CommandRequest.ntsOId, CommandRequest.cId, CommandRequest_Value.cCI, CommandRequest_Value.n, CommandRequest_Value.cO, CommandRequest_Value.v);
                                        RSMPGS.MainForm.HandleCommandListUpdate(RoadSideObject, CommandObject, CommandReturnValue);
                                    }
                                    else
                                    {
                                        rv.v   = null;
                                        rv.cCI = CommandRequest_Value.cCI;
                                        rv.age = "unknown";
                                        sError = "Value and/or type is out of range or invalid for this RSMP protocol version, type: " + CommandReturnValue.Value.GetValueType() + ", value: " + ((CommandRequest_Value.v.Length < 10) ? CommandRequest_Value.v : CommandRequest_Value.v.Substring(0, 9) + "...");
                                        RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "{0}", sError);
                                        bSomeValueWasBad = true;
                                    }

                                    /*
                                     * // Found at least one value
                                     * if (UpdatedRoadSideObjects.ContainsKey(RoadSideObject) == false)
                                     * {
                                     * UpdatedRoadSideObjects.Add(RoadSideObject, RoadSideObject);
                                     * }
                                     */
                                    bDone         = true;
                                    bFoundCommand = true;
                                    break;
                                }
                            }
                            if (bDone)
                            {
                                break;
                            }
                        }
                    }
                    rvs.Add(rv);
                    if (bFoundCommand == false)
                    {
                        sError = "Got Command, failed to find object/command/name (NTSObjectId: " + CommandRequest.ntsOId + ", ComponentId: " + CommandRequest.cId + ", CommandCodeId: " + CommandRequest_Value.cCI + ", Name: " + CommandRequest_Value.n + ", Command: " + CommandRequest_Value.cO + ", Value: " + CommandRequest_Value.v + ")";
                        RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "{0}", sError);
                        bSomeValueWasBad = true;
                    }
                }

                //cRoadSideObject UpdatedRoadSideObject = null;

                /*
                 * foreach (cRoadSideObject UpdatedRoadSideObject in UpdatedRoadSideObjects.Values)
                 * {
                 * RSMPGS.MainForm.HandleCommandListUpdate(UpdatedRoadSideObject);
                 * }
                 */

                bSuccess = bSomeValueWasBad == false ? true : false;

                // Send response to client
                CommandResponse.mType  = "rSMsg";
                CommandResponse.type   = "CommandResponse";
                CommandResponse.mId    = System.Guid.NewGuid().ToString();
                CommandResponse.ntsOId = CommandRequest.ntsOId;
                CommandResponse.xNId   = CommandRequest.xNId;
                CommandResponse.cId    = CommandRequest.cId;
                //CommandResponse.cCI = CommandRequest.cCI;
                CommandResponse.cTS = CreateISO8601UTCTimeStamp();
                CommandResponse.rvs = rvs;

                if (bHasSentAckOrNack == false)
                {
                    bHasSentAckOrNack = SendPacketAck(bSuccess, packetHeader.mId, "");
                }

                string sSendBuffer = JSonSerializer.SerializeObject(CommandResponse);
                RSMPGS.JSon.SendJSonPacket(CommandResponse.type, CommandResponse.mId, sSendBuffer, true);

                if (RSMPGS.MainForm.checkBox_ViewOnlyFailedPackets.Checked == false)
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Sent CommandResponse message, Type: " + CommandResponse.type + ", MsgId: " + CommandResponse.mId);
                }
            }
            catch (Exception e)
            {
                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Failed to deserialize packet: {0}", e.Message);
            }

            return(bSuccess);
        }
        public void CreateAndSendStatusMessage(cRoadSideObject RoadSideObject, List <RSMP_Messages.StatusSubscribe_Status_Over_3_1_4> StatusSubscribeValues, string statusType)
        {
            RSMP_Messages.StatusRequest        StatusRequest;
            RSMP_Messages.StatusRequest_Status StatusRequest_Status;
            cStatusEvent StatusEvent = null;
            string       sSendBuffer;

            try
            {
                StatusRequest = new RSMP_Messages.StatusRequest();

                StatusRequest.mType = "rSMsg";
                StatusRequest.type  = statusType;
                StatusRequest.mId   = System.Guid.NewGuid().ToString();

                StatusRequest.ntsOId = RoadSideObject.sNTSObjectId;
                StatusRequest.xNId   = RoadSideObject.sExternalNTSId;
                StatusRequest.cId    = RoadSideObject.sComponentId;
                StatusRequest.sS     = new List <RSMP_Messages.StatusRequest_Status>();

                foreach (RSMP_Messages.StatusSubscribe_Status_Over_3_1_4 StatusSubscribeValue in StatusSubscribeValues)
                {
                    StatusRequest_Status     = new RSMP_Messages.StatusRequest_Status();
                    StatusRequest_Status.sCI = StatusSubscribeValue.sCI;
                    StatusRequest_Status.n   = StatusSubscribeValue.n;
                    StatusRequest.sS.Add(StatusRequest_Status);

                    StatusEvent                  = new cStatusEvent();
                    StatusEvent.sTimeStamp       = CreateLocalTimeStamp();
                    StatusEvent.sMessageId       = StatusRequest.mId;
                    StatusEvent.sStatusCommandId = StatusRequest_Status.sCI;
                    StatusEvent.sName            = StatusRequest_Status.n;
                    if (statusType.ToLower() == "statusunsubscribe")
                    {
                        StatusEvent.sEvent = "Sent unsubscription";
                        //StatusReturnValue.sLastUpdateRate = null;
                    }
                    else
                    {
                        StatusEvent.sEvent = "Sent status request";
                    }

                    if (RSMPGS_Main.bWriteEventsContinous)
                    {
                        RSMPGS.SysLog.EventLog("Status;{0}\tMId: {1}\tComponentId: {2}\tStatusCommandId: {3}\tName: {4}\tStatus: {5}\tQuality: {6}\tUpdateRate: {7}\tEvent: {8}",
                                               StatusEvent.sTimeStamp, StatusEvent.sMessageId, StatusRequest.cId, StatusEvent.sStatusCommandId,
                                               StatusEvent.sName, StatusEvent.sStatus, StatusEvent.sQuality, StatusEvent.sUpdateRate, StatusEvent.sEvent);
                    }

                    RoadSideObject.StatusEvents.Add(StatusEvent);
                    RSMPGS.MainForm.HandleStatusListUpdate(RoadSideObject, StatusEvent, true);
                }

                sSendBuffer = JSonSerializer.SerializeObject(StatusRequest);

                RSMPGS.JSon.SendJSonPacket(StatusRequest.type, StatusRequest.mId, sSendBuffer, true);
                if (RSMPGS.MainForm.checkBox_ViewOnlyFailedPackets.Checked == false)
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Sent status message, ComponentId: " + StatusRequest.cId + " , MsgId: " + StatusRequest.mId);
                }
            }
            catch (Exception e)
            {
                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Failed to create status message: {0}", e.Message);
            }
        }
        public void CreateAndSendCommandMessage(cRoadSideObject RoadSideObject, List <cCommandReturnValue> ReturnValues, bool bUseCaseSensitiveIds)
        {
            StringComparison sc = bUseCaseSensitiveIds ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

            RSMP_Messages.CommandRequest       CommandRequest;
            RSMP_Messages.CommandRequest_Value CommandRequest_Value;
            string sSendBuffer;

            try
            {
                CommandRequest = new RSMP_Messages.CommandRequest();

                CommandRequest.mType = "rSMsg";
                CommandRequest.type  = "CommandRequest";
                CommandRequest.mId   = System.Guid.NewGuid().ToString();

                CommandRequest.ntsOId = RoadSideObject.sNTSObjectId;
                CommandRequest.xNId   = RoadSideObject.sExternalNTSId;
                CommandRequest.cId    = RoadSideObject.sComponentId;
                CommandRequest.arg    = new List <RSMP_Messages.CommandRequest_Value>();
                foreach (cCommandReturnValue CommandReturnValue in ReturnValues)
                {
                    CommandRequest_Value     = new RSMP_Messages.CommandRequest_Value();
                    CommandRequest_Value.cCI = CommandReturnValue.CommandObject.sCommandCodeId;
                    CommandRequest_Value.n   = CommandReturnValue.sName;
                    CommandRequest_Value.cO  = CommandReturnValue.sCommand;

                    if (CommandReturnValue.Value.GetValueType().Equals("base64", StringComparison.OrdinalIgnoreCase))
                    {
                        // Path?
                        if (CommandReturnValue.Value.GetValue().Contains("\\"))
                        {
                            try
                            {
                                byte[] Base64Bytes = null;
                                // Open file for reading
                                System.IO.FileStream   fsBase64 = new System.IO.FileStream(CommandReturnValue.Value.GetValue(), System.IO.FileMode.Open, System.IO.FileAccess.Read);
                                System.IO.BinaryReader brBase64 = new System.IO.BinaryReader(fsBase64);
                                long lBytes = new System.IO.FileInfo(CommandReturnValue.Value.GetValue()).Length;
                                Base64Bytes = brBase64.ReadBytes((Int32)lBytes);
                                fsBase64.Close();
                                fsBase64.Dispose();
                                brBase64.Close();
                                CommandRequest_Value.v = Convert.ToBase64String(Base64Bytes);
                                if (CommandRequest_Value.v.Length > (cTcpSocketClientThread.BUFLENGTH - 100))
                                {
                                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Base64 encoded packet is too big (" + Base64Bytes.GetLength(0).ToString() + " bytes), max buffer length is " + cTcpSocketClientThread.BUFLENGTH.ToString() + " bytes");
                                    CommandRequest_Value.v = null;
                                }
                            }
                            catch (Exception e)
                            {
                                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Could not base64-encode and send file '{0}', error {1}", CommandReturnValue.Value.GetValue(), e.Message);
                                CommandRequest_Value.v = null;
                            }
                        }
                    }
                    else
                    {
                        CommandRequest_Value.v = CommandReturnValue.Value.GetValue();
                    }
                    CommandRequest.arg.Add(CommandRequest_Value);

                    cCommandEvent CommandEvent = new cCommandEvent();
                    CommandEvent.sTimeStamp     = CreateLocalTimeStamp();
                    CommandEvent.sMessageId     = CommandRequest.mId;
                    CommandEvent.sEvent         = "Sent Command";
                    CommandEvent.sCommandCodeId = CommandReturnValue.CommandObject.sCommandCodeId;
                    CommandEvent.sName          = CommandReturnValue.sName;
                    CommandEvent.sCommand       = CommandReturnValue.sCommand;
                    CommandEvent.sValue         = CommandReturnValue.Value.GetValue();
                    RoadSideObject.CommandEvents.Add(CommandEvent);
                    RSMPGS.MainForm.HandleCommandListUpdate(RoadSideObject, CommandRequest.ntsOId, CommandRequest.cId, CommandEvent, true, bUseCaseSensitiveIds);

                    if (RSMPGS_Main.bWriteEventsContinous)
                    {
                        RSMPGS.SysLog.EventLog("Command;{0}\tMId: {1}\tComponentId: {2}\tCommandCodeId: {3}\tName: {4}\tCommand: {5}\tValue: {6}\t Age: {7}\tEvent: {8}",
                                               CommandEvent.sTimeStamp, CommandEvent.sMessageId, CommandRequest.cId, CommandEvent.sCommandCodeId,
                                               CommandEvent.sName, CommandEvent.sCommand, CommandEvent.sValue, CommandEvent.sAge, CommandEvent.sEvent);
                    }
                }

                sSendBuffer = JSonSerializer.SerializeObject(CommandRequest);

                if (RSMPGS.JSon.SendJSonPacket(CommandRequest.type, CommandRequest.mId, sSendBuffer, true))
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Sent command message, MsgId: " + CommandRequest.mId);
                }
                else
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Failed to send command message, MsgId: " + CommandRequest.mId);
                }
            }
            catch (Exception e)
            {
                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Failed to create command message: {0}", e.Message);
            }
        }