Example #1
0
        private void AddCommandEventToList(cRoadSideObject RoadSideObject, cCommandEvent CommandEvent)
        {
            ListViewItem lvItem = listView_CommandEvents.Items.Add(CommandEvent.sTimeStamp.ToString());

            lvItem.SubItems.Add(CommandEvent.sMessageId);
            lvItem.SubItems.Add(CommandEvent.sEvent);
            lvItem.SubItems.Add(CommandEvent.sCommandCodeId);
            lvItem.SubItems.Add(CommandEvent.sName);
            lvItem.SubItems.Add(CommandEvent.sCommand);
            lvItem.SubItems.Add(CommandEvent.sValue);
            lvItem.SubItems.Add(CommandEvent.sAge);
        }
Example #2
0
        public void HandleCommandListUpdate(cRoadSideObject RoadSideObject, string sntsOId, string scId, cCommandEvent CommandEvent, bool bSend, bool bUseCaseSensitiveIds)
        {
            if (SelectedRoadSideObject == null)
            {
                cRoadSideObject ScanRoadSideObject = cHelper.FindRoadSideObject(sntsOId, scId, bUseCaseSensitiveIds);

                if (ScanRoadSideObject != null)
                {
                    if (!bSend)
                    {
                        for (int i = 0; i < listView_Commands.Items.Count; i++)
                        {
                            ListViewItem lvItem = listView_Commands.Items[i];
                            if (lvItem.SubItems[3].Text.Equals(CommandEvent.sCommandCodeId, StringComparison.OrdinalIgnoreCase) &&
                                lvItem.SubItems[4].Text.Equals(CommandEvent.sName, StringComparison.OrdinalIgnoreCase))
                            {
                                lvItem.SubItems[7].Text = CommandEvent.sValue;
                                lvItem.SubItems[8].Text = CommandEvent.sAge;

                                break;
                            }
                        }
                    }
                    AddCommandEventToList(RoadSideObject, CommandEvent);
                }
            }
            else
            {
                if (SelectedRoadSideObject == RoadSideObject)
                {
                    if (!bSend)
                    {
                        for (int i = 0; i < listView_Commands.Items.Count; i++)
                        {
                            ListViewItem lvItem = listView_Commands.Items[i];
                            if (lvItem.SubItems[0].Text.Equals(CommandEvent.sCommandCodeId, StringComparison.OrdinalIgnoreCase) &&
                                lvItem.SubItems[2].Text.Equals(CommandEvent.sName, StringComparison.OrdinalIgnoreCase))
                            {
                                lvItem.SubItems[5].Text = CommandEvent.sValue;
                                lvItem.SubItems[6].Text = CommandEvent.sAge;

                                break;
                            }
                        }
                    }
                    AddCommandEventToList(RoadSideObject, CommandEvent);
                }
            }
        }
        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);
            }
        }
        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);
        }