[1.2.2] Going to netcore
This commit is contained in:
parent
8811a949fe
commit
7204003afb
@ -5,26 +5,16 @@ using System.Net;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using BlubbFish.Utils.IoT.Connector;
|
|
||||||
using BlubbFish.Utils.IoT.Events;
|
|
||||||
using LitJson;
|
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots {
|
namespace BlubbFish.Utils.IoT.Bots {
|
||||||
public abstract class Webserver : ABot
|
public abstract class AWebserver : ABot {
|
||||||
{
|
|
||||||
protected Dictionary<String, String> config;
|
protected Dictionary<String, String> config;
|
||||||
protected static InIReader requests;
|
|
||||||
protected HttpListener httplistener;
|
protected HttpListener httplistener;
|
||||||
protected ABackend databackend;
|
|
||||||
|
|
||||||
public Webserver(ABackend backend, Dictionary<String, String> settings, InIReader requestslookup) {
|
public AWebserver(Dictionary<String, String> settings) => this.config = settings;
|
||||||
this.config = settings;
|
|
||||||
requests = requestslookup;
|
|
||||||
this.databackend = backend;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void StartListen() {
|
protected void StartListen() {
|
||||||
this.databackend.MessageIncomming += this.Backend_MessageIncomming;
|
|
||||||
this.httplistener = new HttpListener();
|
this.httplistener = new HttpListener();
|
||||||
this.httplistener.Prefixes.Add(this.config["prefix"]);
|
this.httplistener.Prefixes.Add(this.config["prefix"]);
|
||||||
this.httplistener.Start();
|
this.httplistener.Start();
|
||||||
@ -45,7 +35,57 @@ namespace BlubbFish.Utils.IoT.Bots {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Boolean SendFileResponse(HttpListenerContext cont, String folder = "resources", Boolean printOutput = true) {
|
public override void Dispose() {
|
||||||
|
if(this.httplistener.IsListening) {
|
||||||
|
this.httplistener.Stop();
|
||||||
|
}
|
||||||
|
this.httplistener.Close();
|
||||||
|
base.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract Boolean SendWebserverResponse(HttpListenerContext cont);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region HttpListener* Extensions
|
||||||
|
public static class HttpListenerHelper {
|
||||||
|
private static InIReader requests;
|
||||||
|
|
||||||
|
public static Dictionary<String, String> GetPostParams(this HttpListenerRequest request) {
|
||||||
|
if(request.HttpMethod == "POST") {
|
||||||
|
if(request.HasEntityBody) {
|
||||||
|
StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding);
|
||||||
|
String rawData = reader.ReadToEnd();
|
||||||
|
request.InputStream.Close();
|
||||||
|
reader.Close();
|
||||||
|
Dictionary<String, String> ret = new Dictionary<String, String>();
|
||||||
|
foreach(String param in rawData.Split('&')) {
|
||||||
|
String[] kvPair = param.Split('=');
|
||||||
|
if(!ret.ContainsKey(kvPair[0])) {
|
||||||
|
ret.Add(kvPair[0], HttpUtility.UrlDecode(kvPair[1]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Dictionary<String, String>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Boolean SendStringResponse(this HttpListenerContext cont, String obj) => cont.SendBinaryResponse(Encoding.UTF8.GetBytes(obj));
|
||||||
|
|
||||||
|
public static Boolean SendBinaryResponse(this HttpListenerContext cont, Byte[] buf) {
|
||||||
|
try {
|
||||||
|
cont.Response.ContentLength64 = buf.Length;
|
||||||
|
cont.Response.OutputStream.Write(buf, 0, buf.Length);
|
||||||
|
Console.WriteLine("200 - " + cont.Request.Url.PathAndQuery);
|
||||||
|
return true;
|
||||||
|
} catch(Exception e) {
|
||||||
|
Helper.WriteError("500 - " + e.Message + "\n\n" + e.StackTrace);
|
||||||
|
cont.Response.StatusCode = 500;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Boolean SendFileResponse(this HttpListenerContext cont, String folder = "resources", Boolean printOutput = true) {
|
||||||
String restr = cont.Request.Url.PathAndQuery;
|
String restr = cont.Request.Url.PathAndQuery;
|
||||||
if(restr.StartsWith("/")) {
|
if(restr.StartsWith("/")) {
|
||||||
restr = restr.IndexOf("?") != -1 ? restr[1..restr.IndexOf("?")] : restr[1..];
|
restr = restr.IndexOf("?") != -1 ? restr[1..restr.IndexOf("?")] : restr[1..];
|
||||||
@ -81,7 +121,7 @@ namespace BlubbFish.Utils.IoT.Bots {
|
|||||||
file = file.Replace("\"{%" + item.Key.ToUpper() + "%}\"", item.Value);
|
file = file.Replace("\"{%" + item.Key.ToUpper() + "%}\"", item.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file = file.Replace("{%REQUEST_URL_HOST%}", cont.Request.Url.Host+":"+cont.Request.Url.Port);
|
file = file.Replace("{%REQUEST_URL_HOST%}", cont.Request.Url.Host + ":" + cont.Request.Url.Port);
|
||||||
Byte[] buf = Encoding.UTF8.GetBytes(file);
|
Byte[] buf = Encoding.UTF8.GetBytes(file);
|
||||||
cont.Response.ContentLength64 = buf.Length;
|
cont.Response.ContentLength64 = buf.Length;
|
||||||
switch(end) {
|
switch(end) {
|
||||||
@ -109,53 +149,7 @@ namespace BlubbFish.Utils.IoT.Bots {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Boolean SendJsonResponse(Object data, HttpListenerContext cont) {
|
public static void SetRequestsOverride(InIReader requestslookup) => requests = requestslookup;
|
||||||
try {
|
|
||||||
Byte[] buf = Encoding.UTF8.GetBytes(JsonMapper.ToJson(data));
|
|
||||||
cont.Response.ContentLength64 = buf.Length;
|
|
||||||
cont.Response.OutputStream.Write(buf, 0, buf.Length);
|
|
||||||
Console.WriteLine("200 - " + cont.Request.Url.PathAndQuery);
|
|
||||||
return true;
|
|
||||||
} catch(Exception e) {
|
|
||||||
Helper.WriteError("500 - " + e.Message + "\n\n" + e.StackTrace);
|
|
||||||
cont.Response.StatusCode = 500;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Dictionary<String, String> GetPostParams(HttpListenerRequest req) {
|
|
||||||
if(req.HttpMethod == "POST") {
|
|
||||||
if(req.HasEntityBody) {
|
|
||||||
StreamReader reader = new StreamReader(req.InputStream, req.ContentEncoding);
|
|
||||||
String rawData = reader.ReadToEnd();
|
|
||||||
req.InputStream.Close();
|
|
||||||
reader.Close();
|
|
||||||
Dictionary<String, String> ret = new Dictionary<String, String>();
|
|
||||||
foreach(String param in rawData.Split('&')) {
|
|
||||||
String[] kvPair = param.Split('=');
|
|
||||||
if(!ret.ContainsKey(kvPair[0])) {
|
|
||||||
ret.Add(kvPair[0], HttpUtility.UrlDecode(kvPair[1]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new Dictionary<String, String>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Dispose() {
|
|
||||||
if(this.httplistener.IsListening) {
|
|
||||||
this.httplistener.Stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.httplistener.Close();
|
|
||||||
if(this.databackend != null) {
|
|
||||||
this.databackend.Dispose();
|
|
||||||
}
|
|
||||||
base.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract void Backend_MessageIncomming(Object sender, BackendEvent e);
|
|
||||||
protected abstract Boolean SendWebserverResponse(HttpListenerContext cont);
|
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
23
Bot-Utils/AWebserverDataBackend.cs
Normal file
23
Bot-Utils/AWebserverDataBackend.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
using BlubbFish.Utils.IoT.Connector;
|
||||||
|
using BlubbFish.Utils.IoT.Events;
|
||||||
|
|
||||||
|
namespace BlubbFish.Utils.IoT.Bots {
|
||||||
|
public abstract class AWebserverDataBackend : AWebserver {
|
||||||
|
protected ABackend databackend;
|
||||||
|
protected AWebserverDataBackend(ABackend backend, Dictionary<String, String> settings) : base(settings) => this.databackend = backend;
|
||||||
|
|
||||||
|
protected void StartDataBackend() => this.databackend.MessageIncomming += this.Backend_MessageIncomming;
|
||||||
|
|
||||||
|
protected abstract void Backend_MessageIncomming(Object sender, BackendEvent e);
|
||||||
|
|
||||||
|
public override void Dispose() {
|
||||||
|
if(this.databackend != null) {
|
||||||
|
this.databackend.Dispose();
|
||||||
|
}
|
||||||
|
base.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,19 +5,20 @@
|
|||||||
<RootNamespace>BlubbFish.Utils.IoT.Bots</RootNamespace>
|
<RootNamespace>BlubbFish.Utils.IoT.Bots</RootNamespace>
|
||||||
<AssemblyName>Bot-Utils</AssemblyName>
|
<AssemblyName>Bot-Utils</AssemblyName>
|
||||||
<PackageId>Bots.IoT.Utils.BlubbFish</PackageId>
|
<PackageId>Bots.IoT.Utils.BlubbFish</PackageId>
|
||||||
<Version>1.2.1</Version>
|
<Version>1.2.2</Version>
|
||||||
<AssemblyVersion>1.2.1</AssemblyVersion>
|
<AssemblyVersion>1.2.2</AssemblyVersion>
|
||||||
<FileVersion>1.2.1</FileVersion>
|
<FileVersion>1.2.2</FileVersion>
|
||||||
<NeutralLanguage>de-DE</NeutralLanguage>
|
<NeutralLanguage>de-DE</NeutralLanguage>
|
||||||
<Description>Bot-Utils are helpers for programming a bot</Description>
|
<Description>Bot-Utils are helpers for programming a bot</Description>
|
||||||
<Authors>BlubbFish</Authors>
|
<Authors>BlubbFish</Authors>
|
||||||
<Company>BlubbFish</Company>
|
<Company>BlubbFish</Company>
|
||||||
<Copyright>Copyright © BlubbFish 2018 - 30.08.2019</Copyright>
|
<Copyright>Copyright © BlubbFish 2018 - 22.08.2021</Copyright>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageProjectUrl>http://git.blubbfish.net/vs_utils/Bot-Utils</PackageProjectUrl>
|
<PackageProjectUrl>http://git.blubbfish.net/vs_utils/Bot-Utils</PackageProjectUrl>
|
||||||
<RepositoryUrl>http://git.blubbfish.net/vs_utils/Bot-Utils.git</RepositoryUrl>
|
<RepositoryUrl>http://git.blubbfish.net/vs_utils/Bot-Utils.git</RepositoryUrl>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
<PackageReleaseNotes>1.2.1 When using Dispose, kill also mqtt connection and other tiny fixes
|
<PackageReleaseNotes>1.2.2 Going to netcore
|
||||||
|
1.2.1 When using Dispose, kill also mqtt connection and other tiny fixes
|
||||||
1.2.0 Refactor Bot to ABot and refere MultiSourceBot, Webserver and Bot to it. Add MultiSourceBot. Rewrite Mqtt module so that it not need to watch the connection.
|
1.2.0 Refactor Bot to ABot and refere MultiSourceBot, Webserver and Bot to it. Add MultiSourceBot. Rewrite Mqtt module so that it not need to watch the connection.
|
||||||
1.1.9 Modify Output of SendFileResponse
|
1.1.9 Modify Output of SendFileResponse
|
||||||
1.1.8 Add logger to Webserver Class
|
1.1.8 Add logger to Webserver Class
|
||||||
@ -39,10 +40,11 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="../CHANGELOG.md" />
|
<Content Include="../CHANGELOG.md" />
|
||||||
<Content Include="../CONTRIBUTING.md" />
|
<Content Include="../CONTRIBUTING.md" />
|
||||||
<Content Include="../LICENSE" />
|
<Content Include="../LICENSE" />
|
||||||
<Content Include="../README.md" />
|
<Content Include="../README.md" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\LICENSE">
|
<None Include="..\LICENSE">
|
||||||
<Pack>True</Pack>
|
<Pack>True</Pack>
|
||||||
|
116
Bot-Utils/Bot.cs
116
Bot-Utils/Bot.cs
@ -1,60 +1,60 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using BlubbFish.Utils.IoT.Bots.Events;
|
using BlubbFish.Utils.IoT.Bots.Events;
|
||||||
using BlubbFish.Utils.IoT.Bots.Interfaces;
|
using BlubbFish.Utils.IoT.Bots.Interfaces;
|
||||||
using BlubbFish.Utils.IoT.Bots.Moduls;
|
using BlubbFish.Utils.IoT.Bots.Moduls;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots {
|
namespace BlubbFish.Utils.IoT.Bots {
|
||||||
public abstract class Bot<T> : ABot {
|
public abstract class Bot<T> : ABot {
|
||||||
protected readonly Dictionary<String, AModul<T>> moduls = new Dictionary<String, AModul<T>>();
|
protected readonly Dictionary<String, AModul<T>> moduls = new Dictionary<String, AModul<T>>();
|
||||||
|
|
||||||
protected void ModulDispose() {
|
protected void ModulDispose() {
|
||||||
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
|
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
|
||||||
item.Value.Dispose();
|
item.Value.Dispose();
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulDispose: Modul entladen: " + item.Key);
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulDispose: Modul entladen: " + item.Key);
|
||||||
}
|
}
|
||||||
this.Dispose();
|
this.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void ModulLoader(String @namespace, Object library) {
|
protected void ModulLoader(String @namespace, Object library) {
|
||||||
Assembly asm = Assembly.GetEntryAssembly();
|
Assembly asm = Assembly.GetEntryAssembly();
|
||||||
foreach (Type item in asm.GetTypes()) {
|
foreach (Type item in asm.GetTypes()) {
|
||||||
if (item.Namespace == @namespace) {
|
if (item.Namespace == @namespace) {
|
||||||
Type t = item;
|
Type t = item;
|
||||||
String name = t.Name;
|
String name = t.Name;
|
||||||
try {
|
try {
|
||||||
if (InIReader.ConfigExist(name.ToLower())) {
|
if (InIReader.ConfigExist(name.ToLower())) {
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Load Modul " + name);
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Load Modul " + name);
|
||||||
this.moduls.Add(name, (AModul<T>)t.GetConstructor(new Type[] { typeof(T), typeof(InIReader) }).Invoke(new Object[] { library, InIReader.GetInstance(name.ToLower()) }));
|
this.moduls.Add(name, (AModul<T>)t.GetConstructor(new Type[] { typeof(T), typeof(InIReader) }).Invoke(new Object[] { library, InIReader.GetInstance(name.ToLower()) }));
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Loaded Modul " + name);
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Loaded Modul " + name);
|
||||||
} else if (t.HasInterface(typeof(IForceLoad))) {
|
} else if (t.HasInterface(typeof(IForceLoad))) {
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Load Modul Forced " + name);
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Load Modul Forced " + name);
|
||||||
this.moduls.Add(name, (AModul<T>)t.GetConstructor(new Type[] { typeof(T), typeof(InIReader) }).Invoke(new Object[] { library, null }));
|
this.moduls.Add(name, (AModul<T>)t.GetConstructor(new Type[] { typeof(T), typeof(InIReader) }).Invoke(new Object[] { library, null }));
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Loaded Modul Forced " + name);
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Loaded Modul Forced " + name);
|
||||||
}
|
}
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
Helper.WriteError(e.InnerException.Message);
|
Helper.WriteError(e.InnerException.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void ModulInterconnect() {
|
protected void ModulInterconnect() {
|
||||||
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
|
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
|
||||||
item.Value.Interconnect(this.moduls);
|
item.Value.Interconnect(this.moduls);
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulInterconnect: Interconnect Module " + item.Key);
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulInterconnect: Interconnect Module " + item.Key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void ModulEvents() {
|
protected void ModulEvents() {
|
||||||
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
|
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
|
||||||
item.Value.EventLibSetter();
|
item.Value.EventLibSetter();
|
||||||
item.Value.Update += this.ModulUpdate;
|
item.Value.Update += this.ModulUpdate;
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulEvents: Attach Event " + item.Key);
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulEvents: Attach Event " + item.Key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void ModulUpdate(Object sender, ModulEventArgs e) => Console.WriteLine(e.ToString());
|
protected void ModulUpdate(Object sender, ModulEventArgs e) => Console.WriteLine(e.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
65
Changelog.md
65
Changelog.md
@ -0,0 +1,65 @@
|
|||||||
|
## 1.2.2 - Going to netcore
|
||||||
|
### New Features
|
||||||
|
* Split Wbserver to AWebserver and AWebserverDataBackend
|
||||||
|
* Add mp4 as binary content
|
||||||
|
* make a mirror of this repo on github
|
||||||
|
### Changes
|
||||||
|
* change to c+ newer coding style
|
||||||
|
* mograde to c# netcore 3.1
|
||||||
|
|
||||||
|
## 1.2.1
|
||||||
|
### New Features
|
||||||
|
* Add LICENSE, CONTRIBUTING.md and README.md
|
||||||
|
### Bugfixes
|
||||||
|
* When using Dispose, kill also mqtt connection
|
||||||
|
### Changes
|
||||||
|
* A bit more debugging
|
||||||
|
|
||||||
|
## 1.2.0
|
||||||
|
### New Features
|
||||||
|
* Add MultiSourceBot*
|
||||||
|
* Refere MultiSourceBot, Webserver and Bot to it.
|
||||||
|
### Changes
|
||||||
|
* Refactor Bot to ABot
|
||||||
|
* Rewrite Mqtt module so that it not need to watch the connection.
|
||||||
|
|
||||||
|
## 1.1.9
|
||||||
|
### New Features
|
||||||
|
* Modify Output of SendFileResponse
|
||||||
|
|
||||||
|
## 1.1.8
|
||||||
|
### New Features
|
||||||
|
* Add logger to Webserver Class
|
||||||
|
|
||||||
|
## 1.1.7
|
||||||
|
### Changes
|
||||||
|
* Restrucutre loading, so that all is init and after the listener is started, REQUEST_URL_HOST gives now host and port
|
||||||
|
|
||||||
|
## 1.1.6
|
||||||
|
### New Features
|
||||||
|
* SendFileResponse with a parameter for the folder
|
||||||
|
* add function that parse post params
|
||||||
|
|
||||||
|
## 1.1.5
|
||||||
|
### New Features
|
||||||
|
* add a function to send an object as json directly
|
||||||
|
|
||||||
|
## 1.1.4
|
||||||
|
### New Features
|
||||||
|
* add Woff as Binary type
|
||||||
|
|
||||||
|
## 1.1.3
|
||||||
|
### Changes
|
||||||
|
* Variables parsing now as a String
|
||||||
|
|
||||||
|
## 1.1.2
|
||||||
|
### Bugfixes
|
||||||
|
* Fixing bug for Contenttype
|
||||||
|
|
||||||
|
## 1.1.1
|
||||||
|
### Changes
|
||||||
|
* Update to local librarys
|
||||||
|
|
||||||
|
## 1.1.0
|
||||||
|
### New Features
|
||||||
|
* Remove Helper from Bot-Utils
|
Loading…
Reference in New Issue
Block a user