From 2f2524a5c4eaf95f8ef2fe5c46561da124febd73 Mon Sep 17 00:00:00 2001 From: Damian Wessels Date: Tue, 1 Aug 2023 14:20:27 +0200 Subject: [PATCH] public list fertig gestellt --- ConsoleApp3/CardMarketBot.csproj | 6 -- ConsoleApp3/CheckSevDeskPublicList.cs | 118 +++++++++++++++++++++++++ ConsoleApp3/Contracts/ICheckSevDesk.cs | 8 ++ ConsoleApp3/DataContracts/Invoice.cs | 2 +- ConsoleApp3/InvoiceParser.cs | 2 +- ConsoleApp3/Program.cs | 63 ++++--------- ConsoleApp3/usedList.csv | 1 - 7 files changed, 145 insertions(+), 55 deletions(-) create mode 100644 ConsoleApp3/CheckSevDeskPublicList.cs create mode 100644 ConsoleApp3/Contracts/ICheckSevDesk.cs delete mode 100644 ConsoleApp3/usedList.csv diff --git a/ConsoleApp3/CardMarketBot.csproj b/ConsoleApp3/CardMarketBot.csproj index 7fd7555..fad1b3f 100644 --- a/ConsoleApp3/CardMarketBot.csproj +++ b/ConsoleApp3/CardMarketBot.csproj @@ -11,10 +11,4 @@ - - - PreserveNewest - - - diff --git a/ConsoleApp3/CheckSevDeskPublicList.cs b/ConsoleApp3/CheckSevDeskPublicList.cs new file mode 100644 index 0000000..711388a --- /dev/null +++ b/ConsoleApp3/CheckSevDeskPublicList.cs @@ -0,0 +1,118 @@ +using ConsoleApp3.Contracts; +using OpenQA.Selenium.DevTools; +using OpenQA.Selenium.Support.UI; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace CardMarketBot +{ + internal class CheckSevDeskPublicList : ICheckSevDesk + { + private static readonly object _lock = new object(); + private static bool _closed; + + enum EResult + { + FAILED, + AVAIBLE, + OK, + WRITTEN + } + private static void Write(TcpClient client, string str) + { + try + { + SocketShutdown reason = SocketShutdown.Send; + lock (_lock) + { + BinaryWriter writer = new BinaryWriter(client.GetStream()); + writer.Write(str); + if (_closed) + { + reason = SocketShutdown.Both; + } + } + //client.Client.Shutdown(reason); + } + catch (IOException e) + { + Console.WriteLine($"IOException writing to socket: {e.Message}"); + } + } + + private static EResult Read(TcpClient client) + { + try + { + //while(true) + //{ + try + { + if (!client.Connected) + { + lock (_lock) + { + _closed = true; + //break; + } + } + BinaryReader reader = new BinaryReader(client.GetStream()); + string res = reader.ReadString(); + if (res.Contains("OK")) + return EResult.OK; + return EResult.FAILED; + } + catch (EndOfStreamException) + { + lock (_lock) + { + _closed = true; + } + return EResult.FAILED; + } + //client.Client.Shutdown(SocketShutdown.Receive); + //} + } + catch (IOException e) + { + Console.WriteLine(e.Message); + return EResult.FAILED; + } + } + + + public bool AlreadyKnown(string verkaufsnummer) + { + try + { + TcpClient client = new TcpClient("nas.cosysda.de", 4000); + Write(client, string.Format("#TEST#{0}", verkaufsnummer)); + EResult result = Read(client); + + client.Client.Shutdown(SocketShutdown.Both); + client.Close(); + + return result == EResult.OK; + } + catch(SocketException e) + { + throw new SocketException(); + } + } + + public bool Write(string verkaufsnummer) + { + TcpClient client = new TcpClient("nas.cosysda.de", 4000); + Write(client, string.Format("#WRITE#{0}", verkaufsnummer)); + EResult result = Read(client); + client.Client.Shutdown(SocketShutdown.Both); + client.Close(); + return result == EResult.OK; + } + } +} diff --git a/ConsoleApp3/Contracts/ICheckSevDesk.cs b/ConsoleApp3/Contracts/ICheckSevDesk.cs new file mode 100644 index 0000000..e5ad611 --- /dev/null +++ b/ConsoleApp3/Contracts/ICheckSevDesk.cs @@ -0,0 +1,8 @@ +namespace ConsoleApp3.Contracts +{ + internal interface ICheckSevDesk + { + bool AlreadyKnown(string verkaufsnummer); + bool Write(string verkaufsnummer); + } +} diff --git a/ConsoleApp3/DataContracts/Invoice.cs b/ConsoleApp3/DataContracts/Invoice.cs index f47efd3..a9ac9f3 100644 --- a/ConsoleApp3/DataContracts/Invoice.cs +++ b/ConsoleApp3/DataContracts/Invoice.cs @@ -86,7 +86,7 @@ namespace ConsoleApp3.DataContracts /// time left for paying the invoice, use format dd.MM.yyyy or number for number of days left [DataMember(Name = "timeToPay", EmitDefaultValue = false)] [JsonProperty(PropertyName = "timeToPay")] - public DateTime? TimeToPay { get; set; } + public int? TimeToPay { get; set; } /// /// diff --git a/ConsoleApp3/InvoiceParser.cs b/ConsoleApp3/InvoiceParser.cs index d736ae6..db683b1 100644 --- a/ConsoleApp3/InvoiceParser.cs +++ b/ConsoleApp3/InvoiceParser.cs @@ -62,7 +62,7 @@ namespace CardmarketBot "wir stellen Ihnen für ihre Bestellung folgende Rechnung. " + "Bitte Beachte, dass das Lieferdatum dem Bestelldatum entspricht."; rechnung.FootText = "Ihre Rechnung ist bereits über Cardmarket beglichen worden."; - rechnung.TimeToPay = new DateTime().AddDays(14); + rechnung.TimeToPay = 14; rechnung.Discount = 0; rechnung.Address = string.Format("{0}\n{1} {2}\n{3} {4}", kunde.Name, kunde.Strasse, kunde.Hausnummer, kunde.Plz, kunde.Ort); //"Damian Wessels\nDät Haartje 27A\n26683 Saterland"; rechnung.AddressCountry = new ModelStaticCountry() diff --git a/ConsoleApp3/Program.cs b/ConsoleApp3/Program.cs index 0f484a4..c0ac8f2 100644 --- a/ConsoleApp3/Program.cs +++ b/ConsoleApp3/Program.cs @@ -6,17 +6,22 @@ using System.Diagnostics; using System.Net; using System.Net.Sockets; using System.Text; +using OpenQA.Selenium.DevTools.V112.Network; +using OpenQA.Selenium.DevTools.V112.Debugger; +using CardMarketBot; namespace CardmarketBot { internal class Programm { + + static void Main(string[] args) { - CheckVerkauf("120202"); - //IUsedRepository usedRepository = new UsedRepository(); + ICheckSevDesk checkSevDesk = new CheckSevDeskPublicList(); + // Kunden aus Cardmarket erstellen List kunden = new List(); CardMarketParser cardMarketParser = new CardMarketParser("More-Tcg", "Magnatpower310!!"); @@ -29,8 +34,8 @@ namespace CardmarketBot InvoiceParser invoiceParser = new InvoiceParser(kunden); rechnungen = invoiceParser.GetInvoices(); - - + + foreach (ModelRechnung item in rechnungen) { var str_header = item.Invoice.Header; @@ -39,61 +44,27 @@ namespace CardmarketBot Regex regex = new Regex("[0-9]*$"); Match matched = regex.Match(str_header); string verkaufnummer = str_header.Substring(matched.Index); - if(!CheckVerkauf(verkaufnummer)) + if (checkSevDesk.AlreadyKnown(verkaufnummer)) { - Debugger.Break(); SevdeskService sevdeskService = new SevdeskService("7251554968610b78ca865b2b774b4134"); sevdeskService.Create(item); + checkSevDesk.Write(verkaufnummer); } } } - + // Post CSV Erstellen DeutschePost deutschePost = new DeutschePost(kunden); deutschePost.GenerateCSV(); - + Console.WriteLine("Fertig"); Console.ReadLine(); - + } - private static bool CheckVerkauf(string verkaufnummer) - { - byte[] bytes = new byte[1024]; + + - IPHostEntry host = Dns.GetHostEntry("server"); - IPAddress iPAddress = host.AddressList[0]; - - - - IPEndPoint remoteEP = new IPEndPoint(iPAddress, 4000); - - Socket sender = new Socket(iPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); - try - { - sender.Connect(remoteEP); - Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString()); - byte[] msg = Encoding.ASCII.GetBytes(string.Format("TEST #{0}", verkaufnummer)); - int bytesSent = sender.Send(msg); - int bytesRec = sender.Receive(bytes); - Console.WriteLine("Replying = {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec)); - sender.Shutdown(SocketShutdown.Both); - sender.Close(); - } - catch (ArgumentNullException ane) - { - Console.WriteLine("ArgumentNullException : {0}", ane.ToString()); - } - catch (SocketException se) - { - Console.WriteLine("SocketException : {0}", se.ToString()); - } - catch (Exception e) - { - Console.WriteLine("Unexpected exception : {0}", e.ToString()); - } - - return true; - } + } } \ No newline at end of file diff --git a/ConsoleApp3/usedList.csv b/ConsoleApp3/usedList.csv deleted file mode 100644 index 5f28270..0000000 --- a/ConsoleApp3/usedList.csv +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file