From dea926e3d44716d506f68f900fa3979d75b3eb2d Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Sun, 29 Oct 2017 19:32:18 +0000 Subject: [PATCH] [NF] Telegram Condition [NF] Zway Libarary --- Zway.sln | 25 ++++++++++++++++ Zway/Exceptions/ZwayExceptions.cs | 15 ++++++++++ Zway/Properties/AssemblyInfo.cs | 36 ++++++++++++++++++++++ Zway/Zway.csproj | 50 +++++++++++++++++++++++++++++++ Zway/ZwayController.cs | 24 +++++++++++++++ Zway/lib/HttpClient.cs | 40 +++++++++++++++++++++++++ 6 files changed, 190 insertions(+) create mode 100644 Zway.sln create mode 100644 Zway/Exceptions/ZwayExceptions.cs create mode 100644 Zway/Properties/AssemblyInfo.cs create mode 100644 Zway/Zway.csproj create mode 100644 Zway/ZwayController.cs create mode 100644 Zway/lib/HttpClient.cs diff --git a/Zway.sln b/Zway.sln new file mode 100644 index 0000000..ac0358d --- /dev/null +++ b/Zway.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26730.16 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zway", "Zway\Zway.csproj", "{166258ED-CB3D-43F5-8E8D-3A993B64D022}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {166258ED-CB3D-43F5-8E8D-3A993B64D022}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {166258ED-CB3D-43F5-8E8D-3A993B64D022}.Debug|Any CPU.Build.0 = Debug|Any CPU + {166258ED-CB3D-43F5-8E8D-3A993B64D022}.Release|Any CPU.ActiveCfg = Release|Any CPU + {166258ED-CB3D-43F5-8E8D-3A993B64D022}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2F3A172A-4D52-4191-A84A-675D35B4A292} + EndGlobalSection +EndGlobal diff --git a/Zway/Exceptions/ZwayExceptions.cs b/Zway/Exceptions/ZwayExceptions.cs new file mode 100644 index 0000000..861305e --- /dev/null +++ b/Zway/Exceptions/ZwayExceptions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace BlubbFish.IoT.Zway.Exceptions { + public class ConnectionException : Exception { + public ConnectionException() { } + public ConnectionException(String message) : base(message) { } + public ConnectionException(String message, Exception innerException) : base(message, innerException) { } + protected ConnectionException(SerializationInfo info, StreamingContext context) : base(info, context) { } + } +} diff --git a/Zway/Properties/AssemblyInfo.cs b/Zway/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7c83d8c --- /dev/null +++ b/Zway/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("Zway")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Zway")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly +// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("166258ed-cb3d-43f5-8e8d-3a993b64d022")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, +// indem Sie "*" wie unten gezeigt eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Zway/Zway.csproj b/Zway/Zway.csproj new file mode 100644 index 0000000..0599ad0 --- /dev/null +++ b/Zway/Zway.csproj @@ -0,0 +1,50 @@ + + + + + Debug + AnyCPU + {166258ED-CB3D-43F5-8E8D-3A993B64D022} + Library + Properties + BlubbFish.IoT.Zway + Zway + v4.6.1 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Zway/ZwayController.cs b/Zway/ZwayController.cs new file mode 100644 index 0000000..86aaf9e --- /dev/null +++ b/Zway/ZwayController.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BlubbFish.IoT.Zway.lib; + +namespace BlubbFish.IoT.Zway { + public class ZwayController { + private HttpConnection http; + + public ZwayController(String server, String user, String pass) { + this.Connect(server, user, pass); + } + + public ZwayController(Dictionary settings) { + this.Connect(settings["server"], settings["user"], settings["pass"]); + } + + private void Connect(String server, String user, String pass) { + this.http = new HttpConnection(server, user, pass); + } + } +} diff --git a/Zway/lib/HttpClient.cs b/Zway/lib/HttpClient.cs new file mode 100644 index 0000000..5a46ac6 --- /dev/null +++ b/Zway/lib/HttpClient.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; + +namespace BlubbFish.IoT.Zway.lib { + internal class HttpConnection { + private HttpClient http; + private String server; + + internal HttpConnection(String server, String user, String pass) { + this.http = new HttpClient(); + //this.http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(user + ":" + pass))); + this.server = "http://" + server + ":8083"; + this.Init(); + } + + private async void Init() { + await this.GetString("/ZAutomation/api/v1/status"); + } + + private async Task GetString(String v) { + try { + HttpResponseMessage response = await this.http.GetAsync(this.server + v); + if(response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { + Console.Error.WriteLine("Benutzer oder Passwort falsch!"); + throw new Exceptions.ConnectionException("Benutzer oder Passwort falsch!"); + } + HttpContent content = response.Content; + return await content.ReadAsStringAsync(); + } catch(HttpRequestException e) { + Console.Error.WriteLine("Konnte keine Verbindung zum Razzbery Server herstellen: " + e.Message); + throw new Exceptions.ConnectionException("Konnte keine Verbindung zum Razzbery Server herstellen: " + e.Message); + } + } + } +}