Facebook
From ola, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 193
  1.  bool ret = true;
  2.  ConnectionInfo connectionInfo = new ConnectionInfo("addons.inpost24.com", 22323, "inrouting", new PasswordAuthenticationMethod("inrouting", "OFIT6jJbSZRF18HzW"));
  3.  
  4.  SftpClient sftp = null;
  5.  
  6.  try
  7.  {
  8.      SendMessage(MessageType.Info, "Pobieranie danych z InPost", false);
  9.  
  10.      sftp = new SftpClient(connectionInfo);
  11.      sftp.Connect();
  12.      int lncnt = 0;
  13.      using (MemoryStream ms = new MemoryStream())
  14.      {
  15.          SendMessage(MessageType.Info, "Wczytuję plik 'apm_micro_area.csv'", false);
  16.          sftp.DownloadFile("/pl-regions/apm_micro_area/apm_micro_area.csv", ms);
  17.          ms.Position = 0;
  18.          using (StreamReader reader = new StreamReader(ms))
  19.          {
  20.              string header = reader.ReadLine();
  21.              if (header == "box_machine_name;area_number;micro_area")
  22.              {
  23.                  while (!reader.EndOfStream)
  24.                  {
  25.                      lncnt++;
  26.                      SendMessage(MessageType.Count, $"Plik: apm_micro_area.csv; Linia: {lncnt}", false);
  27.                      string[] line = reader.ReadLine().Split(';').Select(t => t.Trim('"', '\'')).ToArray();
  28.                      Data.Inpost.SetMicroArea(line[0], line[1], line[2]);
  29.                  }
  30.              }
  31.              else
  32.                  SendMessage(MessageType.Warning, "Nieznany nagłówek", false);
  33.          }
  34.          SendMessage(MessageType.Info, $"Wczytano {lncnt} linii", false);
  35.      }
  36.      lncnt = 0;
  37.      using (MemoryStream ms = new MemoryStream())
  38.      {
  39.          SendMessage(MessageType.Info, "Wczytuję plik '4214456.csv'", false);
  40.          sftp.DownloadFile("/pl-regions/4214456/4214456.csv", ms);
  41.          ms.Position = 0;
  42.          using (StreamReader reader = new StreamReader(ms))
  43.          {
  44.              string header = reader.ReadLine();
  45.              if (header == "Postcode;Branch;BranchID;Area;SortingHubCode;LastModify")
  46.              {
  47.                  while (!reader.EndOfStream)
  48.                  {
  49.                      lncnt++;
  50.                      SendMessage(MessageType.Count, $"Plik: 4214456.csv; Linia: {lncnt}", false);
  51.                      string[] line = reader.ReadLine().Split(';').Select(t => t.Trim('"', '\'')).ToArray();
  52.                      Data.Inpost.SetRoutingCode(line[0], line[1], line[3], line[4], line[5]);
  53.                  }
  54.              }
  55.              else
  56.                  SendMessage(MessageType.Warning, "Nieznany nagłówek", false);
  57.          }
  58.          SendMessage(MessageType.Info, $"Wczytano {lncnt} linii", false);
  59.      }
  60.  }
  61.  catch (Exception ex)
  62.  {
  63.      ret = false;
  64.      SendMessage(MessageType.Error, ex.Message, false);
  65.  }
  66.  finally
  67.  {
  68.      if (sftp != null)
  69.      {
  70.          if (sftp.IsConnected)
  71.              sftp.Disconnect();
  72.          sftp.Dispose();
  73.      }
  74.  }
  75.  
  76.  return ret;