- bool ret = true;
- ConnectionInfo connectionInfo = new ConnectionInfo("gryf.dpd.com.pl", 22, "RDB-reader", new PasswordAuthenticationMethod("RDB-reader", "Wu9hsdwG8"));
- SftpClient sftp = null;
- //string filename = "zipcodes_depots_GW_20210629.txt";
- string filename_prefix = "zipcodes_depots_GW_";
- string filename = "";
- string filenameRoutes = "ROUTES";
- string filenameDepots = "DEPOTS";
- string zipname = "routesdb.zip";
- string workPath = Properties.Settings.Default.DPDRoutingWorkPath.Value;
- string donePath = Properties.Settings.Default.DPDRoutingDonePath.Value;
- try
- {
- SendMessage(MessageType.Info, "Pobieranie danych z DPD", false);
- sftp = new SftpClient(connectionInfo);
- sftp.Connect();
- PrepareDPDRoutingDirectories(donePath, workPath);
- var files = sftp.ListDirectory("/");
- for (int i = 0; i < files.Count<Renci.SshNet.Sftp.SftpFile>(); i++)
- {
- if (files.ElementAt<Renci.SshNet.Sftp.SftpFile>(i).Name.StartsWith(filename_prefix)
- && String.Compare(files.ElementAt<Renci.SshNet.Sftp.SftpFile>(i).Name, filename, StringComparison.Ordinal) > 0)
- {
- filename = files.ElementAt<Renci.SshNet.Sftp.SftpFile>(i).Name;
- }
- }
- if (filename != "")
- {
- using (Stream fileStream = File.Create($@"{workPath}{filename}"))
- {
- sftp.DownloadFile(filename, fileStream);
- }
- }
- using (Stream fileStream = File.Create($@"{workPath}{zipname}"))
- {
- sftp.DownloadFile(zipname, fileStream);
- }
- ZipFile.ExtractToDirectory($@"{workPath}" + zipname, $@"{workPath}");
- }
- catch (Exception ex)
- {
- ret = false;
- SendMessage(MessageType.Error, ex.Message, false);
- }
- finally
- {
- if (sftp != null)
- {
- if (sftp.IsConnected)
- sftp.Disconnect();
- sftp.Dispose();
- }
- }
- int lncnt = 0;
- List<string>line;
- List<string> line_r;
- List<string> line_d;
- StreamReader reader = null;
- ///////////////////////////////////////////////////////////////
- if (DPD_CheckFilechanged(donePath, workPath, filenameDepots) == true)
- {
- try
- {
- Data.DPD_OLD.ClearTMPTables("DEPOTS");
- SendMessage(MessageType.Info, $"Wczytuję plik '{filenameDepots}'", false);
- reader = new StreamReader(Path.Combine($@"{workPath}", filenameDepots), Encoding.GetEncoding("utf-8"));
- reader.ReadLine();
- reader.ReadLine();
- reader.ReadLine();
- reader.ReadLine();
- reader.ReadLine();
- reader.ReadLine();
- string header = reader.ReadLine();
- if (header == "#Key: GeoPostDepotNumber|")
- {
- while (!reader.EndOfStream)
- {
- lncnt++;
- SendMessage(MessageType.Count, $"Plik: {filenameDepots}; Linia: {lncnt}", false);
- line_d = reader.ReadLine().Split('|').Select(t => t.Trim('"', ''')).ToList();
- Data.DPD_OLD.insertDepotsDPD_TMP(line_d[0], line_d[1], line_d[2], line_d[3], line_d[4], line_d[5], line_d[6], line_d[7],
- line_d[8], line_d[9], line_d[10], line_d[11], line_d[12], line_d[13]);
- }
- }
- else
- SendMessage(MessageType.Warning, "Nieznany nagłówek", false);
- SendMessage(MessageType.Info, filenameDepots + $" Wczytano {lncnt} linii do tabeli tymczasowej", false);
- }
- catch (Exception ex)
- {
- SendMessage(MessageType.Error, ex.Message, false);
- }
- finally
- {
- if (reader != null)
- {
- reader.Close();
- reader.Dispose();
- }
- }
- try
- {
- Data.DPD_OLD.UpdateDepotsIData();
- DPDUpdateRoutingSPLMoveFile(filenameDepots, workPath, donePath);
- }
- catch (Exception ex)
- {
- SendMessage(MessageType.Error, ex.Message, false);
- }
- }
- else
- {
- SendMessage(MessageType.Info, $"Brak potrzeby aktualizacji pliku '{filenameDepots}'", false);
- }
- private void PrepareDPDRoutingDirectories(string donePath, string workPath)
- {
- try
- {
- DirectoryInfo di = new DirectoryInfo(workPath);
- if (!di.Exists) Directory.CreateDirectory(workPath);
- di = new DirectoryInfo(donePath);
- if (!di.Exists) Directory.CreateDirectory(donePath);
- string[] work_files = Directory.GetFiles(workPath, "*.*");
- foreach (string f in work_files)
- {
- File.Delete(f);
- }
- }
- catch (Exception ex)
- {
- SendMessage(MessageType.Error, ex.Message, false);
- }
- }