'Make this sub resumable and call with
'Wait For (ftpGo(param1, param2, param3, param4)) complete (success as Boolean)
'Note: Replace param? with appropriate values
Private Sub ftpGo(Server As String, Login As String, Password As String, Path As String) As ResumableSub
Dim retVal As Boolean 'default to False
'Let's make sftp local to here
Dim sftp As SFtp
Try
sftp.Initialize("sftp", Login, Password, Server, 22)
sftp.SetKnownHostsStore(File.DirData("cmSFTP"), "hosts.txt")
sftp.List(Path)
'Doing it as per https://www.b4x.com/android/forum/threads/sftp-and-wait-for.107518/post-672207
'Also as per @Erel, you need to make sure to serialize the calls to the SFtp methods
Wait For sftp_ListCompleted (ServerPath As String, Success As Boolean, Folders() As SFtpEntry, Files() As SFtpEntry)
If Files.Length> 0 Then
'Using some smart string literals
Log($"There are ${Files.Length} items in this folder"$)
Log($"${Files(Files.Length-1).Name} date is ${DateTime.Date(Files(Files.Length-1).Timestamp)}"$)
If ServerPath.CharAt(ServerPath.Length - 1) <> "/" Then
Log("Adding final forward slash")
ServerPath=$"${ServerPath}/"$
End If
lblFile.Text = $"File: ${Files(Files.Length-1).Name}"$
'Didn't see BackupLocation in the postes code, so must be a Public variable
sftp.DownloadFile(ServerPath & Files(Files.Length-1).Name, BackupLocation, Files(Files.Length-1).Name)
Wait For sftp_DownloadCompleted (ServerPath As String, Success As Boolean)
Log($"${ServerPath}, Success=${Success}"$)
If Success = False Then Log($"Download Error: ${LastException.Message}"$)
retVal = Success
Else
Log("No Files found in " & ServerPath)
End If
Catch
Log($"ftpGo Try failure: ${LastException}"$)
End Try
'Since SFtp does not have an IsInitialized, wrap Close method in a try/catch block
Try
sftp.Close
Catch
Log(LastException)
End Try
Return retVal
End Sub