Esempio n. 1
0
        public SocketResponse Disconnect()
        {
            string totalResponse = "";

            switch (EquipmentConnectionSetting.ConnectionType)
            {
            case ConnectionType.TcpIp:

                //Add logout sequence here
                if (EquipmentConnectionSetting.LogoutSequences != null && EquipmentConnectionSetting.LogoutSequences.Any())
                {
                    foreach (var sequence in EquipmentConnectionSetting.LogoutSequences.OrderBy(p => p.SequenceNumber))
                    {
                        if (!string.IsNullOrEmpty(sequence.ExpectedResponse))
                        {
                            var response = SendCommandAndWaitForResponse(sequence.Command.Unescape(), new List <string> {
                                sequence.ExpectedResponse
                            }, new TimeSpan(0, 0, 0, sequence.Timeout));
                            totalResponse += response + Environment.NewLine;
                            if (response.TimeoutOccurred)
                            {
                                throw new Exception("Unable to logout." + totalResponse);
                            }
                        }
                        else
                        {
                            SendCommand(sequence.Command);
                        }
                    }
                }

                _socketClient.Close();
                break;

            case ConnectionType.Ssh:

                //Add logout sequence here
                if (EquipmentConnectionSetting.LogoutSequences != null && EquipmentConnectionSetting.LogoutSequences.Any())
                {
                    foreach (var sequence in EquipmentConnectionSetting.LogoutSequences.OrderBy(p => p.SequenceNumber))
                    {
                        if (!string.IsNullOrEmpty(sequence.ExpectedResponse))
                        {
                            var response = SendCommandAndWaitForResponse(sequence.Command.Unescape(), new List <string> {
                                sequence.ExpectedResponse
                            }, new TimeSpan(0, 0, 0, sequence.Timeout));
                            totalResponse += response.Data + Environment.NewLine;
                            if (response.TimeoutOccurred)
                            {
                                throw new Exception("Unable to logout." + totalResponse);
                            }
                        }
                        else
                        {
                            SendCommand(sequence.Command);
                        }
                    }
                }

                _shellStream.Close();
                _sshClient.Disconnect();
                //_sshClient.Dispose();
                break;

            case ConnectionType.Telnet:

                //Add logout sequence here
                if (EquipmentConnectionSetting.LogoutSequences != null && EquipmentConnectionSetting.LogoutSequences.Any())
                {
                    foreach (var sequence in EquipmentConnectionSetting.LogoutSequences.OrderBy(p => p.SequenceNumber))
                    {
                        if (!string.IsNullOrEmpty(sequence.ExpectedResponse))
                        {
                            var response = SendCommandAndWaitForResponse(sequence.Command.Unescape(), new List <string> {
                                sequence.ExpectedResponse
                            }, new TimeSpan(0, 0, 0, sequence.Timeout));
                            totalResponse += response.Data + Environment.NewLine;
                            if (response.TimeoutOccurred)
                            {
                                throw new Exception("Unable to logout." + totalResponse);
                            }
                        }
                        else
                        {
                            SendCommand(sequence.Command);
                        }
                    }
                }

                _socketClient.Close();
                break;

            default:
                throw new NotImplementedException();
            }

            return(new SocketResponse
            {
                Data = totalResponse,
                TimeoutOccurred = false
            });
        }