@@ -5,8 +5,16 @@ Imports UnderAutomation.UniversalRobots
55Imports System.Linq
66Imports System.Text.RegularExpressions
77Imports System.Threading
8+ Imports UnderAutomation.UniversalRobots.SSH.Common
9+ Imports System.ComponentModel
810
9- Public Class MainForm
11+ Public Class MainForm
12+
13+ Shared Sub New ()
14+ TypeDescriptor.AddAttributes( GetType (SSH.SshCommand), New ReadOnlyAttribute( True ))
15+ TypeDescriptor.AddAttributes( GetType (SSH.Sftp.SftpFile), New ReadOnlyAttribute( True ))
16+ End Sub
17+
1018
1119 ' instance of the UR connection
1220 Private WithEvents _ur As New UR()
@@ -86,6 +94,11 @@ Public Class MainForm
8694 ' get IP of the local network interface connected to the robot
8795 txtLocalIP.Text = _ur.DataStreamingLocalEndPoint.Address.ToString()
8896 udXmlRpcPort.Value = 50000
97+
98+ _shell = _ur.SSH?.CreateShellStream( "UnderAutomation demo" , 40 , 100 , 40 , 100 , 1000 )
99+
100+ InitializeFtp()
101+
89102 Catch ex As Exception
90103 HandleEx(ex)
91104 End Try
@@ -115,7 +128,7 @@ Public Class MainForm
115128
116129 Private Sub linkDoc_LinkClicked(sender As Object , e As LinkLabelLinkClickedEventArgs) Handles linkDoc.LinkClicked
117130 Try
118- Process.Start( "https://underautomation.github.io /documentation?f" )
131+ Process.Start( "https://underautomation.com/universal-robots /documentation?f" )
119132 Catch
120133 End Try
121134 End Sub
@@ -599,6 +612,243 @@ Public Class MainForm
599612 End Sub
600613
601614
615+
616+ # End Region
617+
618+
619+ # Region "SSH"
620+ Private WithEvents _shell As SSH.ShellStream
621+
622+ Private Sub _shell_DataReceived(sender As Object , e As ShellDataEventArgs) Handles _shell.DataReceived
623+ If txtShellHistory.InvokeRequired Then
624+ txtShellHistory.Invoke( Sub () shell_DataReceived(e))
625+ Else
626+ shell_DataReceived(e)
627+ End If
628+ End Sub
629+
630+ ''' Display received SSH data
631+ Private Sub shell_DataReceived(e As ShellDataEventArgs)
632+ Dim rawData = Encoding.UTF8.GetString(e.Data)
633+
634+ ' remove ANSI data from answer (for example color infos)
635+ Dim data = Regex.Replace(rawData, "(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]" , "" )
636+
637+ txtShellHistory.AppendText(data)
638+
639+ txtShellHistory.ScrollToCaret()
640+ End Sub
641+
642+ Private Sub _shell_ErrorOccurred(sender As Object , e As ExceptionEventArgs) Handles _shell.ErrorOccurred
643+ If txtShellHistory.InvokeRequired Then
644+ txtShellHistory.Invoke( Sub () shell_ErrorOccurred(e.Exception.Message))
645+ Else
646+ shell_ErrorOccurred(e.Exception.Message)
647+ End If
648+ End Sub
649+
650+ Private Sub shell_ErrorOccurred(message As String )
651+ txtShellHistory.AppendText(vbNewLine & vbNewLine & " --- ERROR ---" & vbNewLine & message & vbNewLine & vbNewLine)
652+ End Sub
653+
654+ ' Send command line in shell
655+ Private Sub btnSendShell_Click(sender As Object , e As EventArgs) Handles btnSendShell.Click, txtShellCommand.KeyDown
656+ If TypeOf e Is KeyEventArgs AndAlso CType (e, KeyEventArgs).KeyCode <> Keys.Enter Then Return
657+
658+ If String .IsNullOrEmpty(txtShellCommand.Text) Then Return
659+
660+ _shell.WriteLine(txtShellCommand.Text)
661+
662+ txtShellCommand.Text = ""
663+ End Sub
664+
665+ Private Sub btnSSHSend_Click(sender As Object , e As EventArgs) Handles btnSSHSend.Click, txtSSHCommand.KeyDown
666+ If TypeOf e Is KeyEventArgs AndAlso CType (e, KeyEventArgs).KeyCode <> Keys.Enter Then Return
667+
668+ If String .IsNullOrEmpty(txtSSHCommand.Text) Then Return
669+
670+
671+ Dim command = _ur.SSH.CreateCommand(txtSSHCommand.Text)
672+
673+ command.Execute()
674+
675+ gridSSHResult.SelectedObject = command
676+ End Sub
677+
678+ # End Region
679+
680+
681+ # Region "SFTP"
682+
683+ Public Sub InitializeFtp()
684+ FillList( "/" )
685+ End Sub
686+
687+ Private Sub FillList(path As String )
688+ lstFolder.Items.Clear()
689+
690+
691+ If Not _ur.SftpEnabled Then
692+ Return
693+ End If
694+
695+ Try
696+ path = path.Replace( "\" , "/" )
697+ If Not path.EndsWith( "/" ) Then path = path & "/"
698+
699+ txtPath.Text = path
700+
701+ Dim files = _ur.SFTP.ListDirectory(path)
702+
703+ For Each file In files
704+
705+ ' do not display special folders
706+ If file.Name = "." OrElse file.Name = ".." Then Continue For
707+
708+ Dim itm = lstFolder.Items.Add(file.Name)
709+
710+ itm.Tag = file
711+
712+ If file.IsDirectory Then
713+ itm.ImageKey = "folder"
714+ ElseIf file.IsSymbolicLink Then
715+ itm.ImageKey = "symbolicLink"
716+ Else
717+ itm.ImageKey = "file"
718+ End If
719+ Next
720+ Catch ex As Exception
721+ HandleEx(ex)
722+ End Try
723+
724+ End Sub
725+
726+
727+ Private Sub lstFolder_ItemActivate(sender As Object , e As EventArgs) Handles lstFolder.ItemActivate
728+ Dim file = TryCast (lstFolder.SelectedItems?.OfType( Of ListViewItem)?.FirstOrDefault?.Tag, SSH.Sftp.SftpFile)
729+
730+ If file Is Nothing OrElse ( Not file.IsDirectory AndAlso Not file.IsSymbolicLink) Then Return
731+
732+ FillList(file.FullName)
733+ End Sub
734+
735+ Private Sub lstFolder_ItemSelectionChanged(sender As Object , e As ListViewItemSelectionChangedEventArgs) Handles lstFolder.ItemSelectionChanged
736+ gridFile.SelectedObject = e.Item?.Tag
737+ End Sub
738+ Private Sub lstFolder_SelectedIndexChanged(sender As Object , e As EventArgs) Handles lstFolder.SelectedIndexChanged
739+ If lstFolder.SelectedItems.Count = 0 Then gridFile.SelectedObject = Nothing
740+ End Sub
741+
742+ Private Sub btnPrevious_Click(sender As Object , e As EventArgs) Handles btnPrevious.Click
743+ Dim p = GetPath().TrimEnd( "/"c )
744+ If p = "" Then Return
745+ FillList(Path.GetDirectoryName(p))
746+ End Sub
747+
748+ Private Sub btnOpenPath_Click(sender As Object , e As EventArgs) Handles btnOpenPath.Click, btnRefresh.Click, txtPath.KeyDown
749+ If TypeOf e Is KeyEventArgs AndAlso CType (e, KeyEventArgs).KeyCode <> Keys.Enter Then Return
750+ ReloadList()
751+ End Sub
752+
753+ Private Sub ReloadList()
754+ FillList(GetPath())
755+ End Sub
756+
757+ Private Sub btnDelete_Click(sender As Object , e As EventArgs) Handles btnDelete.Click
758+ If Not _ur.SftpEnabled Then Return
759+
760+ Try
761+ For Each itm In lstFolder.SelectedItems.OfType( Of ListViewItem)
762+ Dim file = TryCast (itm.Tag, SSH.Sftp.SftpFile)
763+
764+ If file Is Nothing Then Return
765+
766+ _ur.SFTP.Delete(file.FullName)
767+ Thread.Sleep( 500 )
768+ ReloadList()
769+ Next
770+ Catch ex As Exception
771+ HandleEx(ex)
772+ End Try
773+ End Sub
774+
775+ Private Sub btnRename_Click(sender As Object , e As EventArgs) Handles btnRename.Click
776+ Try
777+ lstFolder.SelectedItems?.OfType( Of ListViewItem)?.FirstOrDefault?.BeginEdit()
778+ Catch ex As Exception
779+ HandleEx(ex)
780+ End Try
781+ End Sub
782+
783+ Private Sub btnUpload_Click(sender As Object , e As EventArgs) Handles btnUpload.Click
784+ If Not _ur.SftpEnabled Then Return
785+
786+ Try
787+ If dlgOpen.ShowDialog() <> DialogResult.OK Then Return
788+
789+
790+ Using selectedFile = File.OpenRead(dlgOpen.FileName)
791+ _ur.SFTP.UploadFile(selectedFile, GetPath() & Path.GetFileName(dlgOpen.FileName).Replace( "\" , "/" ))
792+ End Using
793+
794+ Thread.Sleep( 500 )
795+ ReloadList()
796+ SelectFile(Path.GetFileName(dlgOpen.FileName))
797+ Catch ex As Exception
798+ HandleEx(ex)
799+ End Try
800+ End Sub
801+
802+ Private Function GetPath() As String
803+ If Not txtPath.Text.EndsWith( "/" ) Then Return txtPath.Text & "/"
804+ Return txtPath.Text
805+ End Function
806+
807+ Private Sub SelectFile(name As String )
808+ Dim itm = lstFolder.Items.OfType( Of ListViewItem).FirstOrDefault( Function (x) String .Equals(x.Text, name, StringComparison.InvariantCultureIgnoreCase))
809+ If itm IsNot Nothing Then itm.Selected = True
810+ End Sub
811+
812+ Private Sub btnDownload_Click(sender As Object , e As EventArgs) Handles btnDownload.Click
813+ If Not _ur.SftpEnabled Then Return
814+ Try
815+ Dim file = TryCast (lstFolder.SelectedItems.OfType( Of ListViewItem)?.FirstOrDefault?.Tag, SSH.Sftp.SftpFile)
816+
817+ If file Is Nothing Then Return
818+
819+ dlgSave.FileName = Path.GetFileName(file.FullName).Replace( "\" , "/" )
820+
821+ If dlgSave.ShowDialog() <> DialogResult.OK Then Return
822+
823+ Using selectedFile = IO.File.Open(dlgSave.FileName, FileMode.OpenOrCreate)
824+ _ur.SFTP.DownloadFile(file.FullName, selectedFile)
825+ End Using
826+ Catch ex As Exception
827+ HandleEx(ex)
828+ End Try
829+
830+ End Sub
831+
832+ Private Sub lstFolder_AfterLabelEdit(sender As Object , e As LabelEditEventArgs) Handles lstFolder.AfterLabelEdit
833+ If Not _ur.SftpEnabled Then Return
834+
835+ Try
836+ Dim file = TryCast (lstFolder.Items(e.Item).Tag, SSH.Sftp.SftpFile)
837+
838+ If file Is Nothing Then Return
839+
840+ _ur.SFTP.RenameFile(file.FullName, Path.GetDirectoryName(file.FullName).Replace( "\" , "/" ) & "/" & e.Label)
841+
842+ Thread.Sleep( 500 )
843+ ReloadList()
844+ SelectFile(e.Label)
845+ Catch ex As Exception
846+ e.CancelEdit = True
847+ HandleEx(ex)
848+ End Try
849+ End Sub
850+
602851# End Region
603852
853+
604854End Class
0 commit comments