private bool DecodeAndParseAggregatedStatusMessage(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; try { RSMP_Messages.AggregatedStatus AggregatedStatus = JSonSerializer.Deserialize <RSMP_Messages.AggregatedStatus>(sJSon); cRoadSideObject RoadSideObject = cHelper.FindRoadSideObject(AggregatedStatus.ntsOId, AggregatedStatus.cId, bUseCaseSensitiveIds); if (RoadSideObject != null) { RoadSideObject.bBitStatus = AggregatedStatus.se; RoadSideObject.sFunctionalPosition = AggregatedStatus.fP; RoadSideObject.sFunctionalState = AggregatedStatus.fS; cAggregatedStatusEvent AggregatedStatusEvent = new cAggregatedStatusEvent(); AggregatedStatusEvent.sTimeStamp = UnpackISO8601UTCTimeStamp(AggregatedStatus.aSTS); AggregatedStatusEvent.sMessageId = AggregatedStatus.mId; AggregatedStatusEvent.sFunctionalPosition = AggregatedStatus.fP; AggregatedStatusEvent.sFunctionalState = AggregatedStatus.fS; for (int i = 1; i < AggregatedStatus.se.Length + 1; i++) { AggregatedStatusEvent.sBitStatus += "B" + i + ": " + AggregatedStatus.se[i - 1] + " | "; } AggregatedStatusEvent.sBitStatus.Trim(); if (RSMPGS_Main.bWriteEventsContinous) { RSMPGS.SysLog.EventLog("AggregatedStatus;{0}\tMId: {1}\tComponentId: {2}\tBitStatus: {3}\tFuncPos: {4}\tFunkState: {5}", AggregatedStatusEvent.sTimeStamp, AggregatedStatusEvent.sMessageId, AggregatedStatus.cId, AggregatedStatusEvent.sBitStatus, AggregatedStatusEvent.sFunctionalPosition, AggregatedStatusEvent.sFunctionalState); } RoadSideObject.AggregatedStatusEvents.Add(AggregatedStatusEvent); RSMPGS.MainForm.HandleAggregatedStatusListUpdate(RoadSideObject, AggregatedStatusEvent); bSuccess = true; } if (bSuccess == false) { sError = "Failed to update AggregatedStatus, could not find object, ntsOId: '" + AggregatedStatus.ntsOId + "', cId: '" + AggregatedStatus.cId + "'"; } } catch (Exception e) { sError = "Failed to deserialize packet: " + e.Message; bSuccess = false; } return(bSuccess); }
public override bool DecodeAndParseGSSpecificJSonPacket(RSMP_Messages.Header Header, string sJSon, bool bUseStrictProtocolAnalysis, bool bUseCaseSensitiveIds, ref bool bHasSentAckOrNack, ref string sError) { bool bSuccess = false; lock (this) { try { switch (Header.type.ToLower()) { case "alarm": bSuccess = DecodeAndParseAlarmMessage(Header, sJSon, bUseStrictProtocolAnalysis, bUseCaseSensitiveIds, ref bHasSentAckOrNack, ref sError); break; case "commandrequest": bSuccess = DecodeAndParseCommandMessage(Header, sJSon, bUseStrictProtocolAnalysis, bUseCaseSensitiveIds, ref bHasSentAckOrNack, ref sError); break; case "statusrequest": bSuccess = DecodeAndParseStatusMessage(Header, StatusMsgType.Request, sJSon, bUseStrictProtocolAnalysis, bUseCaseSensitiveIds, ref bHasSentAckOrNack, ref sError); break; case "statussubscribe": bSuccess = DecodeAndParseStatusMessage(Header, StatusMsgType.Subscribe, sJSon, bUseStrictProtocolAnalysis, bUseCaseSensitiveIds, ref bHasSentAckOrNack, ref sError); break; case "statusunsubscribe": bSuccess = DecodeAndParseStatusMessage(Header, StatusMsgType.UnSubscribe, sJSon, bUseStrictProtocolAnalysis, bUseCaseSensitiveIds, ref bHasSentAckOrNack, ref sError); break; case "aggregatedstatusrequest": bSuccess = DecodeAndParseAggregatedStatusRequestMessage(Header, sJSon, bUseStrictProtocolAnalysis, bUseCaseSensitiveIds, ref bHasSentAckOrNack, ref sError); break; default: sError = "Illegal packet type: '" + Header.type + "', MsgId: '" + Header.mId + "'"; break; } } catch (Exception e) { sError = "Failed to deserialize packet type: '" + Header.type + "', MsgId: '" + Header.mId + "': " + e.Message; } } return(bSuccess); }
private bool DecodeAndParseAggregatedStatusRequestMessage(RSMP_Messages.Header packetHeader, string sJSon, bool bUseStrictProtocolAnalysis, bool bUseCaseSensitiveIds, ref bool bHasSentAckOrNack, ref string sError) { bool bPacketWasProperlyHandled = false; StringComparison sc = bUseCaseSensitiveIds ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; try { RSMP_Messages.AggregatedStatusRequest AggregatedStatusRequest = JSonSerializer.Deserialize <RSMP_Messages.AggregatedStatusRequest>(sJSon); cRoadSideObject RoadSideObject = cHelper.FindRoadSideObject(AggregatedStatusRequest.ntsOId, AggregatedStatusRequest.cId, bUseStrictProtocolAnalysis); if (RoadSideObject != null) { if (cHelper.IsSettingChecked("AllowRequestsOfAlarmsAndAggStatus")) { RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Request of aggregated status, ntsOId: {0}, cId: {1}", AggregatedStatusRequest.ntsOId, AggregatedStatusRequest.cId); bHasSentAckOrNack = SendPacketAck(true, packetHeader.mId, ""); CreateAndSendAggregatedStatusMessage(RoadSideObject); } else { RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Warning, "Request of aggregated status, ntsOId: {0}, cId: {1}. RSMP settings prevent us from answering.", AggregatedStatusRequest.ntsOId, AggregatedStatusRequest.cId); bHasSentAckOrNack = SendPacketAck(false, packetHeader.mId, "Invalid packet type: " + packetHeader.type); } bPacketWasProperlyHandled = true; } else { sError = "Failed to handle AggregatedStatusRequest message, ntsOId: " + AggregatedStatusRequest.ntsOId + "/ cId: " + AggregatedStatusRequest.cId + " could not be found)"; RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "{0}", sError); } } catch (Exception e) { RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Failed to deserialize packet: {0}", e.Message); } return(bPacketWasProperlyHandled); }
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); }
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); }
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.AlarmHeader AlarmHeader = JSonSerializer.Deserialize <RSMP_Messages.AlarmHeader>(sJSon); cRoadSideObject RoadSideObject = cHelper.FindRoadSideObject(AlarmHeader.ntsOId, AlarmHeader.cId, bUseStrictProtocolAnalysis); 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 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); AlarmEvent.sMessageId = AlarmHeader.mId; AlarmEvent.sAlarmCodeId = AlarmHeader.aCId; AlarmEvent.sEvent = AlarmHeader.aSp; AlarmSpecialisation alarmSpecialisation = cJSon.AlarmSpecialisation.Unknown; switch (AlarmHeader.aSp.ToLower()) { case "acknowledge": RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Ack of alarm, AlarmCodeId: {0}", AlarmHeader.aCId); AlarmObject.bAcknowledged = true; alarmSpecialisation = cJSon.AlarmSpecialisation.Acknowledge; break; case "suspend": RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Suspend of alarm, AlarmCodeId: {0}", AlarmHeader.aCId); AlarmObject.bSuspended = true; alarmSpecialisation = cJSon.AlarmSpecialisation.Suspend; break; case "resume": RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Resume of alarm, AlarmCodeId: {0}", AlarmHeader.aCId); AlarmObject.bSuspended = false; alarmSpecialisation = cJSon.AlarmSpecialisation.Suspend; break; case "request": if (cHelper.IsSettingChecked("AllowRequestsOfAlarmsAndAggStatus")) { RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Request of alarm status, AlarmCodeId: {0}", AlarmHeader.aCId); alarmSpecialisation = cJSon.AlarmSpecialisation.Issue; } else { RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Warning, "Request of alarm status, AlarmCodeId: {0}. RSMP settings prevent us from answering.", AlarmHeader.aCId); } break; } if (bHasSentAckOrNack == false) { if (alarmSpecialisation == AlarmSpecialisation.Unknown) { bHasSentAckOrNack = SendPacketAck(false, packetHeader.mId, "Unknown Alarm Specialisation: " + AlarmHeader.aSp); } else { bHasSentAckOrNack = SendPacketAck(true, packetHeader.mId, ""); } } bPacketWasProperlyHandled = true; if (alarmSpecialisation != AlarmSpecialisation.Unknown) { if (AlarmObject.bActive == false && AlarmObject.bAcknowledged && alarmSpecialisation != AlarmSpecialisation.Unknown) { AlarmObject.AlarmCount = 0; } if (AlarmHeader.aSp.ToLower() != "request") { AlarmObject.dtLastChangedAlarmStatus = DateTime.Now; } CreateAndSendAlarmMessage(AlarmObject, alarmSpecialisation); RSMPGS.MainForm.AddAlarmEventToAlarmObjectAndToList(AlarmObject, AlarmEvent); RSMPGS.MainForm.UpdateAlarmListView(AlarmObject); } } } } if (bPacketWasProperlyHandled == false) { sError = "Failed to handle alarm message, AlarmCodeId " + AlarmHeader.aCId + " could not be found)"; RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "{0}", sError); } } catch (Exception e) { RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Failed to deserialize packet: {0}", e.Message); } return(bPacketWasProperlyHandled); }
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); }
private bool DecodeAndParseStatusMessage(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; try { RSMP_Messages.StatusResponse StatusResponse = JSonSerializer.Deserialize <RSMP_Messages.StatusResponse>(sJSon); cRoadSideObject RoadSideObject = cHelper.FindRoadSideObject(StatusResponse.ntsOId, StatusResponse.cId, bUseCaseSensitiveIds); foreach (RSMP_Messages.Status_VTQ Reply in StatusResponse.sS) { cStatusObject StatusObject = RoadSideObject.StatusObjects.Find(x => x.sStatusCodeId.Equals(Reply.sCI, sc)); if (StatusObject == null) { continue; } cStatusReturnValue StatusReturnValue = StatusObject.StatusReturnValues.Find(x => x.sName.Equals(Reply.n, sc)); if (StatusReturnValue == null) { continue; } if (StatusReturnValue.Value.GetValueType().Equals("base64", StringComparison.OrdinalIgnoreCase)) { if (RSMPGS.MainForm.ToolStripMenuItem_StoreBase64Updates.Checked) { StatusReturnValue.Value.SetValue(RSMPGS.SysLog.StoreBase64DebugData(Reply.s)); } else { StatusReturnValue.Value.SetValue("base64"); } } else { StatusReturnValue.Value.SetValue(Reply.s); } StatusReturnValue.sQuality = Reply.q; if (ValidateTypeAndRange(StatusReturnValue.Value.GetValueType(), Reply.s)) { bSuccess = true; } else { string sStatusValue; if (Reply.s == null) { sStatusValue = "(null)"; } else { sStatusValue = (Reply.s.Length < 10) ? Reply.s : Reply.s.Substring(0, 9) + "..."; } sError = "Value and/or type is out of range or invalid for this RSMP protocol version, type: " + StatusReturnValue.Value.GetValueType() + ", quality: " + StatusReturnValue.sQuality + ", statusvalue: " + sStatusValue; RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, sError); } cStatusEvent StatusEvent = new cStatusEvent(); StatusEvent.sTimeStamp = UnpackISO8601UTCTimeStamp(StatusResponse.sTs); StatusEvent.sMessageId = StatusResponse.mId; StatusEvent.sEvent = "Received status"; StatusEvent.sStatusCommandId = Reply.sCI; StatusEvent.sName = Reply.n; if (StatusReturnValue.Value.GetValueType().Equals("base64", StringComparison.OrdinalIgnoreCase)) { StatusEvent.sStatus = "base64"; } else { StatusEvent.sStatus = Reply.s; } StatusEvent.sQuality = Reply.q; if (RSMPGS_Main.bWriteEventsContinous) { RSMPGS.SysLog.EventLog("Status;{0}\tMId: {1}\tComponentId: {2}\tStatusCommandId: {3}\tName: {4}\tStatus: {5}\tQuality: {6}\tUpdateRate: {7}\tUpdateOnChange: {8}\tEvent: {9}", StatusEvent.sTimeStamp, StatusEvent.sMessageId, StatusResponse.cId, StatusEvent.sStatusCommandId, StatusEvent.sName, StatusEvent.sStatus, StatusEvent.sQuality, StatusEvent.sUpdateRate, StatusEvent.bUpdateOnChange, StatusEvent.sEvent); } RoadSideObject.StatusEvents.Add(StatusEvent); RSMPGS.MainForm.HandleStatusListUpdate(RoadSideObject, StatusEvent, false); } } catch (Exception e) { sError = "Failed to deserialize packet: " + e.Message; bSuccess = false; } return(bSuccess); }
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; try { RSMP_Messages.CommandResponse CommandResponse = JSonSerializer.Deserialize <RSMP_Messages.CommandResponse>(sJSon); if (CommandResponse.type.Equals("commandresponse", StringComparison.OrdinalIgnoreCase)) { cRoadSideObject RoadSideObject = cHelper.FindRoadSideObject(CommandResponse.ntsOId, CommandResponse.cId, bUseCaseSensitiveIds); if (RoadSideObject != null) { foreach (RSMP_Messages.CommandResponse_Value Reply in CommandResponse.rvs) { foreach (cCommandObject CommandObject in RoadSideObject.CommandObjects) { bool bDone = false; foreach (cCommandReturnValue CommandReturnValue in CommandObject.CommandReturnValues) { if (CommandReturnValue.sName.Equals(Reply.n, sc) && CommandObject.sCommandCodeId.Equals(Reply.cCI, sc)) { cCommandEvent CommandEvent = new cCommandEvent(); CommandEvent.sTimeStamp = UnpackISO8601UTCTimeStamp(CommandResponse.cTS); CommandEvent.sMessageId = CommandResponse.mId; CommandEvent.sEvent = "Received command"; CommandEvent.sCommandCodeId = Reply.cCI; CommandEvent.sName = Reply.n; if (CommandReturnValue.Value.GetValueType().Equals("base64", StringComparison.OrdinalIgnoreCase)) { if (RSMPGS.MainForm.ToolStripMenuItem_StoreBase64Updates.Checked) { RSMPGS.SysLog.StoreBase64DebugData(Reply.v); } CommandEvent.sValue = "base64"; } else { CommandEvent.sValue = Reply.v; } CommandEvent.sAge = Reply.age; CommandReturnValue.sLastRecValue = Reply.v; CommandReturnValue.sLastRecAge = Reply.age; if (ValidateTypeAndRange(CommandReturnValue.Value.GetValueType(), Reply.v)) { bSuccess = true; } else { sError = "Value and/or type is out of range or invalid for this RSMP protocol version, type: " + CommandReturnValue.Value.GetValueType() + ", value: " + ((Reply.v.Length < 10) ? Reply.v : Reply.v.Substring(0, 9) + "..."); RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, sError); } 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, CommandResponse.cId, CommandEvent.sCommandCodeId, CommandEvent.sName, CommandEvent.sCommand, CommandEvent.sValue, CommandEvent.sAge, CommandEvent.sEvent); } RoadSideObject.CommandEvents.Add(CommandEvent); RSMPGS.MainForm.HandleCommandListUpdate(RoadSideObject, CommandResponse.ntsOId, CommandResponse.cId, CommandEvent, false, bUseCaseSensitiveIds); bDone = true; break; } } if (bDone) { break; } } } } } else { RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Got commandrequest message from SCADA, (corresponding MsgId {0}) ", CommandResponse.mId); } } catch (Exception e) { sError = "Failed to deserialize packet: " + e.Message; bSuccess = false; } return(bSuccess); }