Facebook
From ola, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 218
  1.  bool ret = true;
  2.  ConnectionInfo connectionInfo = new ConnectionInfo("gryf.dpd.com.pl", 22, "RDB-reader", new PasswordAuthenticationMethod("RDB-reader", "Wu9hsdwG8"));
  3.  
  4.  SftpClient sftp = null;
  5.  //string filename = "zipcodes_depots_GW_20210629.txt";
  6.  string filename_prefix = "zipcodes_depots_GW_";
  7.  string filename = "";
  8.  string filenameRoutes = "ROUTES";
  9.  string filenameDepots = "DEPOTS";
  10.  string zipname = "routesdb.zip";
  11.  string workPath = Properties.Settings.Default.DPDRoutingWorkPath.Value;
  12.  string donePath = Properties.Settings.Default.DPDRoutingDonePath.Value;
  13.  
  14.  try
  15.  {
  16.      SendMessage(MessageType.Info, "Pobieranie danych z DPD", false);
  17.  
  18.      sftp = new SftpClient(connectionInfo);
  19.      sftp.Connect();
  20.  
  21.      PrepareDPDRoutingDirectories(donePath, workPath);
  22.  
  23.      var files = sftp.ListDirectory("/");
  24.      for (int i = 0; i < files.Count<Renci.SshNet.Sftp.SftpFile>(); i++)
  25.      {
  26.          if (files.ElementAt<Renci.SshNet.Sftp.SftpFile>(i).Name.StartsWith(filename_prefix)
  27.                  && String.Compare(files.ElementAt<Renci.SshNet.Sftp.SftpFile>(i).Name, filename, StringComparison.Ordinal) > 0)
  28.          {
  29.              filename = files.ElementAt<Renci.SshNet.Sftp.SftpFile>(i).Name;
  30.          }
  31.      }
  32.  
  33.      if (filename != "")
  34.      {
  35.          using (Stream fileStream = File.Create($@"{workPath}{filename}"))
  36.          {
  37.              sftp.DownloadFile(filename, fileStream);
  38.          }
  39.      }
  40.  
  41.      using (Stream fileStream = File.Create($@"{workPath}{zipname}"))
  42.      {
  43.          sftp.DownloadFile(zipname, fileStream);
  44.        
  45.      }
  46.  
  47.      ZipFile.ExtractToDirectory($@"{workPath}" + zipname, $@"{workPath}");
  48.  
  49.  }
  50.  catch (Exception ex)
  51.  {
  52.      ret = false;
  53.      SendMessage(MessageType.Error, ex.Message, false);
  54.  }
  55.  finally
  56.  {
  57.      if (sftp != null)
  58.      {
  59.          if (sftp.IsConnected)
  60.              sftp.Disconnect();
  61.          sftp.Dispose();
  62.      }
  63.  }
  64.  int lncnt = 0;
  65.  List<string>line;
  66.  List<string> line_r;
  67.  List<string> line_d;
  68.  StreamReader reader = null;
  69.  
  70.  ///////////////////////////////////////////////////////////////
  71.          
  72.  if (DPD_CheckFilechanged(donePath, workPath, filenameDepots) == true)
  73.  {
  74.      try
  75.      {
  76.          Data.DPD_OLD.ClearTMPTables("DEPOTS");
  77.          SendMessage(MessageType.Info, $"Wczytuję plik '{filenameDepots}'", false);
  78.  
  79.          reader = new StreamReader(Path.Combine($@"{workPath}", filenameDepots), Encoding.GetEncoding("utf-8"));
  80.          reader.ReadLine();
  81.          reader.ReadLine();
  82.          reader.ReadLine();
  83.          reader.ReadLine();
  84.          reader.ReadLine();
  85.          reader.ReadLine();
  86.          string header = reader.ReadLine();
  87.          if (header == "#Key: GeoPostDepotNumber|")
  88.          {
  89.              while (!reader.EndOfStream)
  90.              {
  91.                  lncnt++;
  92.                  SendMessage(MessageType.Count, $"Plik: {filenameDepots}; Linia: {lncnt}", false);
  93.                  line_d = reader.ReadLine().Split('|').Select(t => t.Trim('"', ''')).ToList();
  94.                  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],
  95.                  line_d[8], line_d[9], line_d[10], line_d[11], line_d[12], line_d[13]);
  96.              }
  97.          }
  98.          else
  99.              SendMessage(MessageType.Warning, "Nieznany nagłówek", false);
  100.  
  101.          SendMessage(MessageType.Info, filenameDepots + $" Wczytano {lncnt} linii do tabeli tymczasowej", false);
  102.      }
  103.  
  104.      catch (Exception ex)
  105.      {
  106.          SendMessage(MessageType.Error, ex.Message, false);
  107.      }
  108.      finally
  109.      {
  110.          if (reader != null)
  111.          {
  112.              reader.Close();
  113.              reader.Dispose();
  114.          }
  115.      }
  116.      try
  117.      {
  118.          Data.DPD_OLD.UpdateDepotsIData();
  119.          DPDUpdateRoutingSPLMoveFile(filenameDepots, workPath, donePath);
  120.      }
  121.      catch (Exception ex)
  122.      {
  123.          SendMessage(MessageType.Error, ex.Message, false);
  124.      }
  125.  
  126.  }
  127.  else
  128.  {
  129.      SendMessage(MessageType.Info, $"Brak potrzeby aktualizacji pliku '{filenameDepots}'", false);
  130.  }
  131.            
  132.            
  133.             private void PrepareDPDRoutingDirectories(string donePath, string workPath)
  134.  {
  135.      try
  136.      {
  137.          DirectoryInfo di = new DirectoryInfo(workPath);
  138.          if (!di.Exists) Directory.CreateDirectory(workPath);
  139.  
  140.          di = new DirectoryInfo(donePath);
  141.          if (!di.Exists) Directory.CreateDirectory(donePath);
  142.  
  143.          string[] work_files = Directory.GetFiles(workPath, "*.*");
  144.          foreach (string f in work_files)
  145.          {
  146.              File.Delete(f);
  147.          }
  148.      }
  149.      catch (Exception ex)
  150.      {
  151.          SendMessage(MessageType.Error, ex.Message, false);
  152.      }
  153.  }