Compare commits

...

32 Commits

Author SHA1 Message Date
8e41d15e4e [1.2.6] Makeing Logging easyier 2022-01-25 23:28:11 +01:00
1862aa7da2 [1.2.5] Better linux handling 2022-01-20 19:20:04 +01:00
628db8db10 [1.2.4] Config enabled module loading 2022-01-18 22:00:43 +01:00
55425d2afa [1.2.3] Tiny Refactoring 2022-01-09 11:21:35 +01:00
7204003afb [1.2.2] Going to netcore 2021-08-22 20:20:23 +02:00
8811a949fe make mirror on github.com 2021-08-22 14:19:52 +02:00
3cda47aa0c newer c# lang 2021-04-10 01:01:56 +02:00
f3842c0b6e Make netcore as default, also using 3.1 now 2021-01-09 00:16:32 +01:00
0231d800ef Check if requests not null 2020-01-20 14:27:35 +01:00
1a33d2ef0a Stopping HTTPListener is in netcore different 2019-12-10 15:20:50 +01:00
3e79b425a5 Change netcore 2019-12-01 17:53:11 +01:00
9326ab8659 Change signalhandling netcore 2019-11-29 14:44:45 +01:00
ec93004c2b If using netcore you dont need mono 2019-11-26 18:33:58 +01:00
a13e03a808 Add netcore and refactoring 2019-11-26 11:31:19 +01:00
8fecb22620 Merge branch 'master' of http://git.blubbfish.net/vs_utils/Bot-Utils 2019-10-29 22:50:03 +01:00
d7f22d62b0 c# formating rules 2019-10-29 22:49:40 +01:00
1cc64de12b also mp4 as binary 2019-09-10 17:04:34 +02:00
9e82a3bc58 [1.2.1] When using Dispose, kill also mqtt connection and other tiny fixes 2019-08-30 15:30:00 +02:00
03849c999a [BF] When using Dispose, kill also mqtt connection 2019-08-02 19:03:25 +02:00
1b0fb91b08 A bit more debugging 2019-07-09 20:50:20 +02:00
37e185bceb Formating 2019-05-29 21:48:18 +02:00
9d2238caaf „README.md“ ändern 2019-05-29 20:11:46 +02:00
db25b03820 Add LICENSE, CONTRIBUTING.md and README.md 2019-05-29 20:08:33 +02:00
8932867ed9 [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. 2019-05-27 17:23:31 +02:00
cf3413a3d7 [1.1.9] Modify Output of SendFileResponse 2019-04-21 15:01:14 +02:00
9705663328 [1.1.8] Add logger to Webserver Class 2019-04-15 21:18:27 +02:00
6840fced1c [1.1.7] Restrucutre loading, so that all is init and after the listener is started, REQUEST_URL_HOST gives now host and port 2019-04-14 17:23:32 +02:00
39eac5222c forget changelog 2019-04-03 23:53:10 +02:00
41c8ccfee7 [1.1.6] rename functions and make SendFileResponse with a parameter for the folder (default resources), also put returntype boolean, add function that parse post params, if path is a dictionary try to load index.html 2019-04-03 20:53:10 +02:00
b3d60ad1ae SendFileResponse as now the default parameter folder = "resources"
add a function GetPostParams
2019-04-02 23:31:28 +02:00
b8fb0e7278 rename functions and make SendFileResponse with a parameter for the folder, not finished yet 2019-04-01 18:15:22 +02:00
031076b3ba [v1.1.5] add a function to send an object as json directly 2019-03-27 19:32:39 +01:00
24 changed files with 1203 additions and 661 deletions

49
Bot-Utils-4.7.1.sln Normal file
View File

@ -0,0 +1,49 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.136
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bot-Utils", "Bot-Utils\Bot-Utils-4.7.1.csproj", "{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "litjson_4.7.1", "..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj", "{91A14CD2-2940-4500-8193-56D37EDDDBAA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils-IoT", "..\Utils-IoT\Utils-IoT\Utils-IoT-4.7.1.csproj", "{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "..\Utils\Utils\Utils-4.7.1.csproj", "{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Posix", "..\..\Librarys\Mono.Posix\Mono.Posix\Mono.Posix.csproj", "{E2CA132E-E85C-40AD-BE94-B138AA68772B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Release|Any CPU.Build.0 = Release|Any CPU
{91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.Build.0 = Release|Any CPU
{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.Build.0 = Release|Any CPU
{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Any CPU.Build.0 = Release|Any CPU
{E2CA132E-E85C-40AD-BE94-B138AA68772B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E2CA132E-E85C-40AD-BE94-B138AA68772B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2CA132E-E85C-40AD-BE94-B138AA68772B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2CA132E-E85C-40AD-BE94-B138AA68772B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {99DB6F34-BD41-4AFF-82B7-E4B3EFDF56DC}
EndGlobalSection
EndGlobal

View File

@ -1,17 +1,15 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.136
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.87
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bot-Utils", "Bot-Utils\Bot-Utils.csproj", "{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bot-Utils", "Bot-Utils\Bot-Utils.csproj", "{CB48457C-C567-42BC-9E5B-DAE5A3383108}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "litjson_4.7.1", "..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj", "{91A14CD2-2940-4500-8193-56D37EDDDBAA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils-IoT", "..\Utils-IoT\Utils-IoT\Utils-IoT.csproj", "{86E9D6ED-EE31-4FE7-AEA6-555DBBAA69A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils-IoT", "..\Utils-IoT\Utils-IoT\Utils-IoT.csproj", "{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils", "..\Utils\Utils\Utils.csproj", "{110EB1ED-1567-4A7E-B8B5-E46F421E0684}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "..\Utils\Utils\Utils.csproj", "{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Posix", "..\..\Librarys\Mono.Posix\Mono.Posix\Mono.Posix.csproj", "{E2CA132E-E85C-40AD-BE94-B138AA68772B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "litjson", "..\..\Librarys\litjson\litjson\litjson.csproj", "{A6EB1FF2-A3F1-4C97-8E7F-0F2CE4A8A820}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -19,31 +17,27 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Release|Any CPU.Build.0 = Release|Any CPU
{91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.Build.0 = Release|Any CPU
{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.Build.0 = Release|Any CPU
{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}.Release|Any CPU.Build.0 = Release|Any CPU
{E2CA132E-E85C-40AD-BE94-B138AA68772B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E2CA132E-E85C-40AD-BE94-B138AA68772B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2CA132E-E85C-40AD-BE94-B138AA68772B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2CA132E-E85C-40AD-BE94-B138AA68772B}.Release|Any CPU.Build.0 = Release|Any CPU
{CB48457C-C567-42BC-9E5B-DAE5A3383108}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB48457C-C567-42BC-9E5B-DAE5A3383108}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB48457C-C567-42BC-9E5B-DAE5A3383108}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB48457C-C567-42BC-9E5B-DAE5A3383108}.Release|Any CPU.Build.0 = Release|Any CPU
{86E9D6ED-EE31-4FE7-AEA6-555DBBAA69A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{86E9D6ED-EE31-4FE7-AEA6-555DBBAA69A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86E9D6ED-EE31-4FE7-AEA6-555DBBAA69A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86E9D6ED-EE31-4FE7-AEA6-555DBBAA69A4}.Release|Any CPU.Build.0 = Release|Any CPU
{110EB1ED-1567-4A7E-B8B5-E46F421E0684}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{110EB1ED-1567-4A7E-B8B5-E46F421E0684}.Debug|Any CPU.Build.0 = Debug|Any CPU
{110EB1ED-1567-4A7E-B8B5-E46F421E0684}.Release|Any CPU.ActiveCfg = Release|Any CPU
{110EB1ED-1567-4A7E-B8B5-E46F421E0684}.Release|Any CPU.Build.0 = Release|Any CPU
{A6EB1FF2-A3F1-4C97-8E7F-0F2CE4A8A820}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A6EB1FF2-A3F1-4C97-8E7F-0F2CE4A8A820}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6EB1FF2-A3F1-4C97-8E7F-0F2CE4A8A820}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6EB1FF2-A3F1-4C97-8E7F-0F2CE4A8A820}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {99DB6F34-BD41-4AFF-82B7-E4B3EFDF56DC}
SolutionGuid = {0F0DF907-D03F-41A9-B0A2-CA229CC259EE}
EndGlobalSection
EndGlobal

58
Bot-Utils/ABot.cs Normal file
View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;
namespace BlubbFish.Utils.IoT.Bots {
public abstract class ABot {
private Boolean RunningProcess = true;
private readonly ProgramLogger logger = null;
public Boolean DebugLogging {
get;
}
public ABot(String[] _, Boolean fileLogging, String configSearchPath) {
InIReader.SetSearchPath(new List<String>() { "/etc/"+ configSearchPath, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\"+ configSearchPath });
if(fileLogging) {
this.logger = new ProgramLogger(InIReader.GetInstance("settings").GetValue("logging", "path", Assembly.GetEntryAssembly().GetName().Name + ".log"));
}
if(Boolean.TryParse(InIReader.GetInstance("settings").GetValue("logging", "debug", "true"), out Boolean debuglog)) {
this.DebugLogging = debuglog;
}
}
private void ConsoleCancelEvent(Object sender, ConsoleCancelEventArgs e) {
e.Cancel = true;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ConsoleCancelEvent()");
this.RunningProcess = false;
}
#if NETCOREAPP
private void Unloading(AssemblyLoadContext obj) => this.RunningProcess = false;
private void ProcessExit(Object sender, EventArgs e) => this.RunningProcess = false;
#endif
protected void WaitForShutdown() {
#if NETCOREAPP
AssemblyLoadContext.Default.Unloading += this.Unloading;
AppDomain.CurrentDomain.ProcessExit += this.ProcessExit;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Attach Unloading and ProcessExit.");
#endif
Console.CancelKeyPress += new ConsoleCancelEventHandler(this.ConsoleCancelEvent);
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Attach ConsoleCancelEvent.");
while(this.RunningProcess) {
Thread.Sleep(100);
}
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Shutdown.");
}
public virtual void Dispose() {
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.Dispose: Shutdown.");
this.RunningProcess = false;
this.logger?.Dispose();
}
}
}

155
Bot-Utils/AWebserver.cs Normal file
View File

@ -0,0 +1,155 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;
namespace BlubbFish.Utils.IoT.Bots {
public abstract class AWebserver : ABot {
protected Dictionary<String, String> config;
protected HttpListener httplistener;
public AWebserver(String[] args, Boolean fileLogging, String configSearchPath, Dictionary<String, String> settings) : base(args, fileLogging, configSearchPath) => this.config = settings;
protected void StartListen() {
this.httplistener = new HttpListener();
this.httplistener.Prefixes.Add(this.config["prefix"]);
this.httplistener.Start();
_ = ThreadPool.QueueUserWorkItem((o) => {
Console.WriteLine("Webserver is Running...");
try {
while(this.httplistener.IsListening) {
_ = ThreadPool.QueueUserWorkItem((state) => {
HttpListenerContext httplistenercontext = state as HttpListenerContext;
try {
_ = this.SendWebserverResponse(httplistenercontext);
} catch { } finally {
httplistenercontext.Response.OutputStream.Close();
}
}, this.httplistener.GetContext());
}
} catch { };
});
}
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;
if(restr.StartsWith("/")) {
restr = restr.IndexOf("?") != -1 ? restr[1..restr.IndexOf("?")] : restr[1..];
if(Directory.Exists(folder + "/" + restr)) {
restr += "/index.html";
}
String end = restr.IndexOf('.') != -1 ? restr[(restr.IndexOf('.') + 1)..] : "";
if(File.Exists(folder + "/" + restr)) {
try {
if(end == "png" || end == "jpg" || end == "jpeg" || end == "ico" || end == "woff" || end == "mp4") {
Byte[] output = File.ReadAllBytes(folder + "/" + restr);
switch(end) {
case "ico":
cont.Response.ContentType = "image/x-ico";
break;
case "woff":
cont.Response.ContentType = "font/woff";
break;
case "mp4":
cont.Response.ContentType = "video/mpeg";
break;
}
cont.Response.OutputStream.Write(output, 0, output.Length);
if(printOutput) {
Console.WriteLine("200 - " + cont.Request.Url.PathAndQuery);
}
return true;
} else {
String file = File.ReadAllText(folder + "/" + restr);
if(requests != null && requests.GetSections(false).Contains(restr)) {
Dictionary<String, String> vars = requests.GetSection(restr);
foreach(KeyValuePair<String, String> item in vars) {
file = file.Replace("\"{%" + item.Key.ToUpper() + "%}\"", item.Value);
}
}
file = file.Replace("{%REQUEST_URL_HOST%}", cont.Request.Url.Host + ":" + cont.Request.Url.Port);
Byte[] buf = Encoding.UTF8.GetBytes(file);
cont.Response.ContentLength64 = buf.Length;
switch(end) {
case "css":
cont.Response.ContentType = "text/css";
break;
}
cont.Response.OutputStream.Write(buf, 0, buf.Length);
if(printOutput) {
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;
}
}
}
if(printOutput) {
Helper.WriteError("404 - " + cont.Request.Url.PathAndQuery + " not found!");
}
cont.Response.StatusCode = 404;
return false;
}
public static void SetRequestsOverride(InIReader requestslookup) => requests = requestslookup;
}
#endregion
}

View 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(String[] args, Boolean fileLogging, String configSearchPath, ABackend backend, Dictionary<String, String> settings) : base(args, fileLogging, configSearchPath, 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();
}
}
}

View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BlubbFish.Utils.IoT.Bots</RootNamespace>
<AssemblyName>Bot-Utils</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ABot.cs" />
<Compile Include="Bot.cs" />
<Compile Include="Events\CronEvent.cs" />
<Compile Include="Events\ModulEventArgs.cs" />
<Compile Include="Events\SenmlEvent.cs" />
<Compile Include="Events\MqttEvent.cs" />
<Compile Include="Events\OvertakerEvent.cs" />
<Compile Include="Events\StatusPollingEvent.cs" />
<Compile Include="Interfaces\IForceLoad.cs" />
<Compile Include="Moduls\AModul.cs" />
<Compile Include="Moduls\CronJob.cs" />
<Compile Include="Moduls\Mqtt.cs" />
<Compile Include="Moduls\Overtaker.cs" />
<Compile Include="Moduls\Statuspolling.cs" />
<Compile Include="MultiSourceBot.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Webserver.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="..\CHANGELOG.md" />
<Content Include="..\CONTRIBUTING.md" />
<Content Include="..\LICENSE" />
<Content Include="..\README.md" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj">
<Project>{91a14cd2-2940-4500-8193-56d37edddbaa}</Project>
<Name>litjson_4.7.1</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Librarys\Mono.Posix\Mono.Posix\Mono.Posix.csproj">
<Project>{e2ca132e-e85c-40ad-be94-b138aa68772b}</Project>
<Name>Mono.Posix</Name>
</ProjectReference>
<ProjectReference Include="..\..\Utils-IoT\Utils-IoT\Utils-IoT.csproj">
<Project>{b870e4d5-6806-4a0b-b233-8907eedc5afc}</Project>
<Name>Utils-IoT</Name>
</ProjectReference>
<ProjectReference Include="..\..\Utils\Utils\Utils.csproj">
<Project>{fac8ce64-bf13-4ece-8097-aeb5dd060098}</Project>
<Name>Utils</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -1,83 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>BlubbFish.Utils.IoT.Bots</RootNamespace>
<AssemblyName>Bot-Utils</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
<PackageId>Bots.IoT.Utils.BlubbFish</PackageId>
<Version>1.2.6</Version>
<NeutralLanguage>de-DE</NeutralLanguage>
<Description>Bot-Utils are helpers for programming a bot</Description>
<Authors>BlubbFish</Authors>
<Company>BlubbFish</Company>
<Copyright>Copyright © BlubbFish 2018 - 25.01.2022</Copyright>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>http://git.blubbfish.net/vs_utils/Bot-Utils</PackageProjectUrl>
<RepositoryUrl>http://git.blubbfish.net/vs_utils/Bot-Utils.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>
1.2.6 - 2022-01-25 - Makeing Logging easyier
1.2.5 - 2022-01-20 - Better linux handling
1.2.4 - 2022-01-18 - Config enabled module loading
1.2.3 - 2022-01-09 - Tiny Refactoring
1.2.2 - 2021-08-22 - Going to netcore
1.2.1 - 2019-08-30 - When using Dispose, kill also mqtt connection and other tiny fixes
1.2.0 - 2019-05-27 - Refactor Bot to ABot and refere MultiSourceBot
1.1.9 - 2019-04-21 - Modify Output of SendFileResponse
1.1.8 - 2019-04-15 - Add logger to Webserver Class
1.1.7 - 2019-04-14 - REQUEST_URL_HOST
1.1.6 - 2019-04-03 - Refactoring and bugfixing
1.1.5 - 2019-03-27 - add a function to send an object as json directly
1.1.4 - 2019-03-13 - add Woff as Binary type
1.1.3 - 2019-03-10 - Variables parsing now as a String
1.1.2 - 2019-03-08 - Fixing bug for Contenttype
1.1.1 - 2019-02-17 - Update to local librarys
1.1.0 - 2019-02-14 - Remove Helper from Bot-Utils
</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<ProjectReference Include="..\..\..\Librarys\litjson\litjson\litjson.csproj" />
<ProjectReference Include="..\..\Utils-IoT\Utils-IoT\Utils-IoT.csproj" />
<ProjectReference Include="..\..\Utils\Utils\Utils.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="Bot.cs" />
<Compile Include="Events\CronEvent.cs" />
<Compile Include="Events\ModulEventArgs.cs" />
<Compile Include="Events\SenmlEvent.cs" />
<Compile Include="Events\MqttEvent.cs" />
<Compile Include="Events\OvertakerEvent.cs" />
<Compile Include="Events\StatusPollingEvent.cs" />
<Compile Include="Interfaces\IForceLoad.cs" />
<Compile Include="Moduls\AModul.cs" />
<Compile Include="Moduls\CronJob.cs" />
<Compile Include="Moduls\Mqtt.cs" />
<Compile Include="Moduls\Overtaker.cs" />
<Compile Include="Moduls\Statuspolling.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Webserver.cs" />
<Content Include="../CHANGELOG.md" />
<Content Include="../CONTRIBUTING.md" />
<Content Include="../LICENSE" />
<Content Include="../README.md" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj">
<Project>{91a14cd2-2940-4500-8193-56d37edddbaa}</Project>
<Name>litjson_4.7.1</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\Librarys\Mono.Posix\Mono.Posix\Mono.Posix.csproj">
<Project>{e2ca132e-e85c-40ad-be94-b138aa68772b}</Project>
<Name>Mono.Posix</Name>
</ProjectReference>
<ProjectReference Include="..\..\Utils-IoT\Utils-IoT\Utils-IoT.csproj">
<Project>{b870e4d5-6806-4a0b-b233-8907eedc5afc}</Project>
<Name>Utils-IoT</Name>
</ProjectReference>
<ProjectReference Include="..\..\Utils\Utils\Utils.csproj">
<Project>{fac8ce64-bf13-4ece-8097-aeb5dd060098}</Project>
<Name>Utils</Name>
</ProjectReference>
<None Include="..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>

View File

@ -1,99 +1,72 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using BlubbFish.Utils.IoT.Bots.Moduls;
using BlubbFish.Utils.IoT.Bots.Events;
using BlubbFish.Utils.IoT.Bots.Interfaces;
namespace BlubbFish.Utils.IoT.Bots {
public abstract class Bot<T> {
private Thread sig_thread;
private Boolean RunningProcess = true;
protected ProgramLogger logger = new ProgramLogger();
protected readonly Dictionary<String, AModul<T>> moduls = new Dictionary<String, AModul<T>>();
protected void WaitForShutdown() {
if (Type.GetType("Mono.Runtime") != null) {
this.sig_thread = new Thread(delegate () {
Mono.Unix.UnixSignal[] signals = new Mono.Unix.UnixSignal[] {
new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGTERM),
new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGINT)
};
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono attached.");
while (true) {
Int32 i = Mono.Unix.UnixSignal.WaitAny(signals, -1);
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono INT recieved " + i + ".");
this.RunningProcess = false;
break;
}
});
this.sig_thread.Start();
} else {
Console.CancelKeyPress += new ConsoleCancelEventHandler(this.SetupShutdown);
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Windows attached.");
}
while (this.RunningProcess) {
Thread.Sleep(100);
}
}
private void SetupShutdown(Object sender, ConsoleCancelEventArgs e) {
e.Cancel = true;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.SetupShutdown: Signalhandler Windows INT recieved.");
this.RunningProcess = false;
}
protected void ModulDispose() {
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
item.Value.Dispose();
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulDispose: Modul entladen: " + item.Key);
}
if (this.sig_thread != null && this.sig_thread.IsAlive) {
this.sig_thread.Abort();
}
}
protected void ModulLoader(String @namespace, Object library) {
Assembly asm = Assembly.GetEntryAssembly();
foreach (Type item in asm.GetTypes()) {
if (item.Namespace == @namespace) {
Type t = item;
String name = t.Name;
try {
if (InIReader.ConfigExist(name.ToLower())) {
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()) }));
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Loaded Modul " + name);
} else if (t.HasInterface(typeof(IForceLoad))) {
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 }));
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Loaded Modul Forced " + name);
}
} catch(Exception e) {
Helper.WriteError(e.InnerException.Message);
}
}
}
}
protected void ModulInterconnect() {
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
item.Value.Interconnect(this.moduls);
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulInterconnect: Interconnect Module " + item.Key);
}
}
protected void ModulEvents() {
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
item.Value.EventLibSetter();
item.Value.Update += this.ModulUpdate;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulEvents: Attach Event " + item.Key);
}
}
protected void ModulUpdate(Object sender, ModulEventArgs e) {
Console.WriteLine(e.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Reflection;
using BlubbFish.Utils.IoT.Bots.Events;
using BlubbFish.Utils.IoT.Bots.Interfaces;
using BlubbFish.Utils.IoT.Bots.Moduls;
namespace BlubbFish.Utils.IoT.Bots {
public abstract class Bot<T> : ABot {
protected readonly Dictionary<String, AModul<T>> moduls = new Dictionary<String, AModul<T>>();
public Bot(String[] args, Boolean fileLogging, String configSearchPath) : base(args, fileLogging, configSearchPath) { }
protected void ModulDispose() {
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulDispose: Entlade Modul: " + item.Key);
item.Value.Dispose();
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulDispose: Modul entladen: " + item.Key);
}
this.Dispose();
}
protected void ModulLoader(String @namespace, Object library) {
Assembly asm = Assembly.GetEntryAssembly();
foreach (Type item in asm.GetTypes()) {
if (item.Namespace == @namespace) {
Type t = item;
String name = t.Name;
try {
if (InIReader.ConfigExist(name.ToLower())) {
Dictionary<String, String> modulconfig = InIReader.GetInstance(name.ToLower()).GetSection("modul");
if(!(modulconfig.ContainsKey("enabled") && modulconfig["enabled"].ToLower() == "false")) {
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()) }));
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Loaded Modul " + name);
continue;
}
}
if (t.HasInterface(typeof(IForceLoad))) {
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulLoader: Forced Load Modul " + name);
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: Forced Loaded Modul " + name);
}
} catch(Exception e) {
Helper.WriteError(e.InnerException.Message);
}
}
}
}
protected void ModulInterconnect() {
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
item.Value.Interconnect(this.moduls);
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulInterconnect: Interconnect Module " + item.Key);
}
}
protected void ModulEvents() {
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
item.Value.EventLibSetter();
item.Value.Update += this.ModulUpdate;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulEvents: Attach Event " + item.Key);
}
}
protected void ModulUpdate(Object sender, ModulEventArgs e) {
if(this.DebugLogging) {
Console.WriteLine(e.ToString());
}
}
}
}

View File

@ -14,8 +14,6 @@ namespace BlubbFish.Utils.IoT.Bots.Events {
public String Property { get; protected set; }
public String Value { get; protected set; }
public String Source { get; protected set; }
public override String ToString() {
return this.Source + ": " + this.Address + " set " + this.Property + " to " + this.Value;
}
public override String ToString() => this.Source + ": " + this.Address + " set " + this.Property + " to " + this.Value;
}
}

View File

@ -9,8 +9,6 @@ namespace BlubbFish.Utils.IoT.Bots.Events {
this.Value = text;
this.Source = "MQTT";
}
public override String ToString() {
return this.Source + ": on " + this.Address + " set " + this.Value;
}
public override String ToString() => this.Source + ": on " + this.Address + " set " + this.Value;
}
}

View File

@ -9,8 +9,6 @@ namespace BlubbFish.Utils.IoT.Bots.Events {
this.Value = text;
this.Source = "Senml";
}
public override String ToString() {
return this.Source + ": on " + this.Address + " set " + this.Value;
}
public override String ToString() => this.Source + ": on " + this.Address + " set " + this.Value;
}
}

View File

@ -11,8 +11,6 @@ namespace BlubbFish.Utils.IoT.Bots.Events {
this.Source = "POLLING";
}
public override String ToString() {
return this.Source + ": " + this.Value + " on " + this.Address;
}
public override String ToString() => this.Source + ": " + this.Value + " on " + this.Address;
}
}

View File

@ -39,7 +39,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
if (this.HasConfig && this.ConfigPublic) {
Dictionary<String, Dictionary<String, String>> ret = new Dictionary<String, Dictionary<String, String>>(this.config);
if (ret.ContainsKey("modul")) {
ret.Remove("modul");
_ = ret.Remove("modul");
}
return ret;
}
@ -54,16 +54,14 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
protected abstract void LibUpadteThread(Object state);
protected void HandleLibUpdate(Object sender, EventArgs e) {
ThreadPool.QueueUserWorkItem(this.LibUpadteThread, e);
}
protected void HandleLibUpdate(Object sender, EventArgs e) => ThreadPool.QueueUserWorkItem(this.LibUpadteThread, e);
public abstract void Dispose();
public void SetConfig(Dictionary<String, Dictionary<String, String>> newconf) {
if (this.HasConfig && this.ConfigPublic) {
if (newconf.ContainsKey("modul")) {
newconf.Remove("modul");
_ = newconf.Remove("modul");
}
if (this.config.ContainsKey("modul")) {
newconf.Add("modul", this.config["modul"]);

View File

@ -1,172 +1,162 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;
using BlubbFish.Utils.IoT.Bots.Interfaces;
namespace BlubbFish.Utils.IoT.Bots.Moduls {
public abstract class CronJob<T> : AModul<T>, IDisposable, IForceLoad {
protected readonly List<Tuple<String, Action<Object>, Object>> internalCron = new List<Tuple<String, Action<Object>, Object>>();
protected Thread thread;
protected DateTime crontime;
protected readonly Dictionary<String, String> cron_named = new Dictionary<String, String> {
{ "@yearly", "0 0 1 1 *" },
{ "@annually", "0 0 1 1 *" },
{ "@monthly", "0 0 1 * *" },
{ "@weekly", "0 0 * * 0" },
{ "@daily", "0 0 * * *" },
{ "@hourly", "0 * * * *" }
};
#region Constructor
public CronJob(T lib, InIReader settings) : base(lib, settings) {
this.crontime = DateTime.Now;
this.thread = new Thread(this.Runner);
this.thread.Start();
}
#endregion
#region Cronjobrunner
protected void Runner() {
Thread.Sleep(DateTime.Now.AddMinutes(1).AddSeconds(DateTime.Now.Second * (-1)).AddMilliseconds(DateTime.Now.Millisecond * (-1)) - DateTime.Now);
while (true) {
if (this.crontime.Minute != DateTime.Now.Minute) {
this.crontime = DateTime.Now;
if (this.config.Count != 0) {
foreach (KeyValuePair<String, Dictionary<String, String>> item in this.config) {
if (item.Value.ContainsKey("cron") && item.Value.ContainsKey("set") && this.ParseCronString(item.Value["cron"])) {
this.SetValues(item.Value["set"]);
}
}
}
foreach (Tuple<String, Action<Object>, Object> item in this.internalCron) {
if (this.ParseCronString(item.Item1)) {
item.Item2?.Invoke(item.Item3);
}
}
}
Thread.Sleep(100);
}
}
protected abstract void SetValues(String value);
#endregion
#region CronFunctions
protected Boolean ParseCronString(String cronstring) {
cronstring = cronstring.Trim();
if (this.cron_named.ContainsKey(cronstring)) {
cronstring = this.cron_named[cronstring];
}
String[] value = cronstring.Split(' ');
if (value.Length != 5) {
return false;
}
if (!this.CheckDateStr(this.crontime.ToString("mm"), value[0], "0-59")) {
return false;
}
if (!this.CheckDateStr(this.crontime.ToString("HH"), value[1], "0-23")) {
return false;
}
if (!this.CheckDateStr(this.crontime.ToString("MM"), value[3], "1-12")) {
return false;
}
if (value[2] != "*" && value[4] != "*") {
if (!this.CheckDateStr(this.crontime.ToString("dd"), value[2], "1-31") && !this.CheckDateStr(((Int32)this.crontime.DayOfWeek).ToString(), value[4], "0-7")) {
return false;
}
} else {
if (!this.CheckDateStr(this.crontime.ToString("dd"), value[2], "1-31")) {
return false;
}
if (!this.CheckDateStr(((Int32)this.crontime.DayOfWeek).ToString(), value[4], "0-7")) {
return false;
}
}
return true;
}
protected Boolean CheckDateStr(String date, String cron, String limit) {
cron = cron.ToLower();
for (Int32 i = 0; i <= 6; i++) {
cron = cron.Replace(DateTime.Parse("2015-01-" + (4 + i) + "T00:00:00").ToString("ddd", CultureInfo.CreateSpecificCulture("en-US")), i.ToString());
cron = cron.Replace(DateTime.Parse("2015-01-" + (4 + i) + "T00:00:00").ToString("dddd", CultureInfo.CreateSpecificCulture("en-US")), i.ToString());
}
for (Int32 i = 1; i <= 12; i++) {
cron = cron.Replace(DateTime.Parse("2015-" + i + "-01T00:00:00").ToString("MMM", CultureInfo.CreateSpecificCulture("en-US")), i.ToString());
cron = cron.Replace(DateTime.Parse("2015-" + i + "-01T00:00:00").ToString("MMMM", CultureInfo.CreateSpecificCulture("en-US")), i.ToString());
}
if (cron.Contains("*")) {
cron = cron.Replace("*", limit);
}
if (cron.Contains("-")) {
MatchCollection m = new Regex("(\\d+)-(\\d+)").Matches(cron);
foreach (Match p in m) {
List<String> s = new List<String>();
for (Int32 i = Math.Min(Int32.Parse(p.Groups[1].Value), Int32.Parse(p.Groups[2].Value)); i <= Math.Max(Int32.Parse(p.Groups[1].Value), Int32.Parse(p.Groups[2].Value)); i++) {
s.Add(i.ToString());
}
cron = cron.Replace(p.Groups[0].Value, String.Join(",", s));
}
}
Int32 match = 0;
if (cron.Contains("/")) {
Match m = new Regex("/(\\d+)").Match(cron);
cron = cron.Replace(m.Groups[0].Value, "");
match = Int32.Parse(m.Groups[1].Value);
}
Dictionary<Int32, String> ret = new Dictionary<Int32, String>();
if (!cron.Contains(",")) {
ret.Add(Int32.Parse(cron), "");
} else {
foreach (String item in cron.Split(',')) {
if (!ret.ContainsKey(Int32.Parse(item))) {
ret.Add(Int32.Parse(item), "");
}
}
}
if (match != 0) {
Dictionary<Int32, String> r = new Dictionary<Int32, String>();
foreach (KeyValuePair<Int32, String> item in ret) {
if (item.Key % match == 0) {
r.Add(item.Key, "");
}
}
ret = r;
}
return ret.ContainsKey(Int32.Parse(date));
}
#endregion
#region AModul
public override void SetInterconnection(String cron, Action<Object> hook, Object data) {
this.internalCron.Add(new Tuple<String, Action<Object>, Object>(cron, hook, data));
}
protected override void UpdateConfig() { }
#endregion
#region IDisposable Support
private Boolean disposedValue = false;
protected virtual void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
if (this.thread != null) {
this.thread.Abort();
while (this.thread.ThreadState == ThreadState.Running) { Thread.Sleep(100); }
}
}
this.thread = null;
this.disposedValue = true;
}
}
public override void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;
using BlubbFish.Utils.IoT.Bots.Interfaces;
namespace BlubbFish.Utils.IoT.Bots.Moduls {
public abstract class CronJob<T> : AModul<T>, IForceLoad {
protected readonly List<Tuple<String, Action<Object>, Object>> internalCron = new List<Tuple<String, Action<Object>, Object>>();
protected Thread thread;
protected Boolean threadRunning = false;
protected DateTime crontime;
protected readonly Dictionary<String, String> cron_named = new Dictionary<String, String> {
{ "@yearly", "0 0 1 1 *" },
{ "@annually", "0 0 1 1 *" },
{ "@monthly", "0 0 1 * *" },
{ "@weekly", "0 0 * * 0" },
{ "@daily", "0 0 * * *" },
{ "@hourly", "0 * * * *" }
};
#region Constructor
public CronJob(T lib, InIReader settings) : base(lib, settings) {
this.crontime = DateTime.Now;
this.thread = new Thread(this.Runner);
this.threadRunning = true;
this.thread.Start();
}
#endregion
#region Cronjobrunner
protected void Runner() {
DateTime nextminute = DateTime.Now.AddMinutes(1).AddSeconds(DateTime.Now.Second * -1).AddMilliseconds(DateTime.Now.Millisecond * -1);
while(nextminute > DateTime.Now && this.threadRunning) {
Thread.Sleep(100);
}
while (this.threadRunning) {
if (this.crontime.Minute != DateTime.Now.Minute) {
this.crontime = DateTime.Now;
if (this.config.Count != 0) {
foreach (KeyValuePair<String, Dictionary<String, String>> item in this.config) {
if (item.Value.ContainsKey("cron") && item.Value.ContainsKey("set") && this.ParseCronString(item.Value["cron"])) {
this.SetValues(item.Value["set"]);
}
}
}
foreach (Tuple<String, Action<Object>, Object> item in this.internalCron) {
if (this.ParseCronString(item.Item1)) {
item.Item2?.Invoke(item.Item3);
}
}
}
Thread.Sleep(100);
}
}
protected abstract void SetValues(String value);
#endregion
#region CronFunctions
protected Boolean ParseCronString(String cronstring) {
cronstring = cronstring.Trim();
if (this.cron_named.ContainsKey(cronstring)) {
cronstring = this.cron_named[cronstring];
}
String[] value = cronstring.Split(' ');
if (value.Length != 5) {
return false;
}
if (!this.CheckDateStr(this.crontime.ToString("mm"), value[0], "0-59")) {
return false;
}
if (!this.CheckDateStr(this.crontime.ToString("HH"), value[1], "0-23")) {
return false;
}
if (!this.CheckDateStr(this.crontime.ToString("MM"), value[3], "1-12")) {
return false;
}
if (value[2] != "*" && value[4] != "*") {
if (!this.CheckDateStr(this.crontime.ToString("dd"), value[2], "1-31") && !this.CheckDateStr(((Int32)this.crontime.DayOfWeek).ToString(), value[4], "0-7")) {
return false;
}
} else {
if (!this.CheckDateStr(this.crontime.ToString("dd"), value[2], "1-31")) {
return false;
}
if (!this.CheckDateStr(((Int32)this.crontime.DayOfWeek).ToString(), value[4], "0-7")) {
return false;
}
}
return true;
}
protected Boolean CheckDateStr(String date, String cron, String limit) {
cron = cron.ToLower();
for (Int32 i = 0; i <= 6; i++) {
cron = cron.Replace(DateTime.Parse("2015-01-" + (4 + i) + "T00:00:00").ToString("ddd", CultureInfo.CreateSpecificCulture("en-US")), i.ToString());
cron = cron.Replace(DateTime.Parse("2015-01-" + (4 + i) + "T00:00:00").ToString("dddd", CultureInfo.CreateSpecificCulture("en-US")), i.ToString());
}
for (Int32 i = 1; i <= 12; i++) {
cron = cron.Replace(DateTime.Parse("2015-" + i + "-01T00:00:00").ToString("MMM", CultureInfo.CreateSpecificCulture("en-US")), i.ToString());
cron = cron.Replace(DateTime.Parse("2015-" + i + "-01T00:00:00").ToString("MMMM", CultureInfo.CreateSpecificCulture("en-US")), i.ToString());
}
if (cron.Contains("*")) {
cron = cron.Replace("*", limit);
}
if (cron.Contains("-")) {
MatchCollection m = new Regex("(\\d+)-(\\d+)").Matches(cron);
foreach (Match p in m) {
List<String> s = new List<String>();
for (Int32 i = Math.Min(Int32.Parse(p.Groups[1].Value), Int32.Parse(p.Groups[2].Value)); i <= Math.Max(Int32.Parse(p.Groups[1].Value), Int32.Parse(p.Groups[2].Value)); i++) {
s.Add(i.ToString());
}
cron = cron.Replace(p.Groups[0].Value, String.Join(",", s));
}
}
Int32 match = 0;
if (cron.Contains("/")) {
Match m = new Regex("/(\\d+)").Match(cron);
cron = cron.Replace(m.Groups[0].Value, "");
match = Int32.Parse(m.Groups[1].Value);
}
Dictionary<Int32, String> ret = new Dictionary<Int32, String>();
if (!cron.Contains(",")) {
ret.Add(Int32.Parse(cron), "");
} else {
foreach (String item in cron.Split(',')) {
if (!ret.ContainsKey(Int32.Parse(item))) {
ret.Add(Int32.Parse(item), "");
}
}
}
if (match != 0) {
Dictionary<Int32, String> r = new Dictionary<Int32, String>();
foreach (KeyValuePair<Int32, String> item in ret) {
if (item.Key % match == 0) {
r.Add(item.Key, "");
}
}
ret = r;
}
return ret.ContainsKey(Int32.Parse(date));
}
#endregion
#region AModul
public override void SetInterconnection(String cron, Action<Object> hook, Object data) => this.internalCron.Add(new Tuple<String, Action<Object>, Object>(cron, hook, data));
protected override void UpdateConfig() { }
public override void Dispose() {
this.threadRunning = false;
while(this.thread != null && this.thread.IsAlive) {
Thread.Sleep(10);
}
this.thread = null;
}
#endregion
}
}

View File

@ -1,119 +1,80 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using BlubbFish.Utils.IoT.Bots.Events;
using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
using LitJson;
namespace BlubbFish.Utils.IoT.Bots.Moduls {
public abstract class Mqtt<T> : AModul<T>, IDisposable {
protected readonly Thread connectionWatcher;
protected ABackend mqtt;
protected Dictionary<String, AModul<T>> modules;
#region Constructor
public Mqtt(T lib, InIReader settings) : base(lib, settings) {
if (this.config.ContainsKey("settings")) {
this.connectionWatcher = new Thread(this.ConnectionWatcherRunner);
this.connectionWatcher.Start();
} else {
throw new ArgumentException("Setting section [settings] is missing!");
}
}
#endregion
#region Watcher
protected void ConnectionWatcherRunner() {
while (true) {
try {
if (this.mqtt == null || !this.mqtt.IsConnected) {
this.Reconnect();
}
Thread.Sleep(10000);
} catch (Exception) { }
}
}
protected void Reconnect() {
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Moduls.Mqtt.Reconnect()");
this.Disconnect();
this.Connect();
}
protected abstract void Connect();
protected abstract void Disconnect();
#endregion
#region AModul
public override void Interconnect(Dictionary<String, AModul<T>> moduls) {
this.modules = moduls;
}
protected override void UpdateConfig() {
this.Reconnect();
}
#endregion
protected Tuple<Boolean, MqttEvent> ChangeConfig(BackendEvent e, String topic) {
if (e.From.ToString().StartsWith(topic) && (e.From.ToString().EndsWith("/set") || e.From.ToString().EndsWith("/get"))) {
Match m = new Regex("^"+ topic + "(\\w+)/[gs]et$|").Match(e.From.ToString());
if (!m.Groups[1].Success) {
return new Tuple<Boolean, MqttEvent>(false, null);
}
AModul<T> modul = null;
foreach (KeyValuePair<String, AModul<T>> item in this.modules) {
if (item.Key.ToLower() == m.Groups[1].Value) {
modul = item.Value;
}
}
if (modul == null) {
return new Tuple<Boolean, MqttEvent>(false, null);
}
if (e.From.ToString().EndsWith("/get") && modul.HasConfig && modul.ConfigPublic) {
String t = topic + m.Groups[1].Value;
String d = JsonMapper.ToJson(modul.GetConfig()).ToString();
((ADataBackend)this.mqtt).Send(t, d);
return new Tuple<Boolean, MqttEvent>(true, new MqttEvent(t, d));
} else if (e.From.ToString().EndsWith("/set") && modul.HasConfig && modul.ConfigPublic) {
try {
JsonData a = JsonMapper.ToObject(e.Message);
Dictionary<String, Dictionary<String, String>> newconf = new Dictionary<String, Dictionary<String, String>>();
foreach (String section in a.Keys) {
Dictionary<String, String> sectiondata = new Dictionary<String, String>();
foreach (String item in a[section].Keys) {
sectiondata.Add(item, a[section][item].ToString());
}
newconf.Add(section, sectiondata);
}
modul.SetConfig(newconf);
return new Tuple<Boolean, MqttEvent>(true, new MqttEvent("New Config", "Write"));
} catch (Exception) { }
}
}
return new Tuple<Boolean, MqttEvent>(false, null);
}
#region IDisposable Support
private Boolean disposedValue = false;
protected void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
this.connectionWatcher.Abort();
while (this.connectionWatcher.ThreadState == ThreadState.Running) { Thread.Sleep(10); }
this.Disconnect();
}
this.disposedValue = true;
}
}
public override void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using BlubbFish.Utils.IoT.Bots.Events;
using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
using LitJson;
namespace BlubbFish.Utils.IoT.Bots.Moduls {
public abstract class Mqtt<T> : AModul<T> {
protected ABackend mqtt;
protected Dictionary<String, AModul<T>> modules;
#region Constructor
public Mqtt(T lib, InIReader settings) : base(lib, settings) => this.Connect();
#endregion
#region Connection
protected void Reconnect() {
if(!this.config.ContainsKey("settings")) {
throw new ArgumentException("Setting section [settings] is missing!");
} else {
this.Disconnect();
this.Connect();
}
}
protected void Connect() => this.mqtt = !this.config.ContainsKey("settings") ? throw new ArgumentException("Setting section [settings] is missing!") : ABackend.GetInstance(this.config["settings"], ABackend.BackendType.Data);
protected void Disconnect() => this.mqtt.Dispose();
#endregion
#region AModul
public override void Interconnect(Dictionary<String, AModul<T>> moduls) => this.modules = moduls;
protected override void UpdateConfig() => this.Reconnect();
public override void Dispose() => this.Disconnect();
#endregion
protected Tuple<Boolean, MqttEvent> ChangeConfig(BackendEvent e, String topic) {
if (e.From.ToString().StartsWith(topic) && (e.From.ToString().EndsWith("/set") || e.From.ToString().EndsWith("/get"))) {
Match m = new Regex("^"+ topic + "(\\w+)/[gs]et$|").Match(e.From.ToString());
if (!m.Groups[1].Success) {
return new Tuple<Boolean, MqttEvent>(false, null);
}
AModul<T> modul = null;
foreach (KeyValuePair<String, AModul<T>> item in this.modules) {
if (item.Key.ToLower() == m.Groups[1].Value) {
modul = item.Value;
}
}
if (modul == null) {
return new Tuple<Boolean, MqttEvent>(false, null);
}
if (e.From.ToString().EndsWith("/get") && modul.HasConfig && modul.ConfigPublic) {
String t = topic + m.Groups[1].Value;
String d = JsonMapper.ToJson(modul.GetConfig()).ToString();
((ADataBackend)this.mqtt).Send(t, d);
return new Tuple<Boolean, MqttEvent>(true, new MqttEvent(t, d));
} else if (e.From.ToString().EndsWith("/set") && modul.HasConfig && modul.ConfigPublic) {
try {
JsonData a = JsonMapper.ToObject(e.Message);
Dictionary<String, Dictionary<String, String>> newconf = new Dictionary<String, Dictionary<String, String>>();
foreach (String section in a.Keys) {
Dictionary<String, String> sectiondata = new Dictionary<String, String>();
foreach (String item in a[section].Keys) {
sectiondata.Add(item, a[section][item].ToString());
}
newconf.Add(section, sectiondata);
}
modul.SetConfig(newconf);
return new Tuple<Boolean, MqttEvent>(true, new MqttEvent("New Config", "Write"));
} catch { }
}
}
return new Tuple<Boolean, MqttEvent>(false, null);
}
}
}

View File

@ -2,13 +2,11 @@
using System.Collections.Generic;
namespace BlubbFish.Utils.IoT.Bots.Moduls {
public abstract class Overtaker<T> : AModul<T>, IDisposable {
public abstract class Overtaker<T> : AModul<T> {
protected readonly Dictionary<String, Dictionary<String, String>> events = new Dictionary<String, Dictionary<String, String>>();
#region Constructor
public Overtaker(T lib, InIReader settings) : base(lib, settings) {
this.ParseIni();
}
public Overtaker(T lib, InIReader settings) : base(lib, settings) => this.ParseIni();
#endregion
#region Overtakerfunctions
@ -24,7 +22,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
}
}
protected void SetValues(Object sender, String name, Dictionary<String, String> dictionary) {
protected void SetValues(Object sender, String _, Dictionary<String, String> dictionary) {
String from = dictionary["from"];
String[] source = from.Split(':');
if (source.Length != 2) {
@ -63,26 +61,9 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
#region AModul
public override void Interconnect(Dictionary<String, AModul<T>> moduls) { }
protected override void UpdateConfig() {
this.ParseIni();
protected override void UpdateConfig() => this.ParseIni();
public override void Dispose() {
}
#endregion
#region IDisposable Support
private Boolean disposedValue = false;
protected virtual void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
}
this.disposedValue = true;
}
}
public override void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#endregion
}
}

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using BlubbFish.Utils.IoT.Bots.Interfaces;
namespace BlubbFish.Utils.IoT.Bots.Moduls {
public abstract class Statuspolling<T> : AModul<T>, IDisposable, IForceLoad {
public abstract class Statuspolling<T> : AModul<T>, IForceLoad {
#region Constructor
public Statuspolling(T lib, InIReader settings) : base(lib, settings) { }
@ -31,22 +31,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
}
}
protected override void UpdateConfig() { }
#endregion
#region IDisposable Support
private Boolean disposedValue = false;
protected virtual void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
}
this.disposedValue = true;
}
}
public override void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
public override void Dispose() {
}
#endregion
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using BlubbFish.Utils.IoT.Connector;
namespace BlubbFish.Utils.IoT.Bots {
public abstract class MultiSourceBot : ABot {
protected Dictionary<String, ABackend> sources;
protected Dictionary<String, String> settings;
protected MultiSourceBot(String[] args, Boolean fileLogging, String configSearchPath, Dictionary<String, ABackend> sources, Dictionary<String, String> settings) : base(args, fileLogging, configSearchPath) {
this.sources = sources;
this.settings = settings;
}
}
}

View File

@ -1,5 +1,6 @@
using System.Reflection;
using System.Runtime.CompilerServices;
#if !NETCOREAPP
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
@ -10,9 +11,10 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("BlubbFish")]
[assembly: AssemblyProduct("Bot-Utils")]
[assembly: AssemblyCopyright("Copyright © 2018 - 12.03.2019")]
[assembly: AssemblyCopyright("Copyright © BlubbFish 2018 - 30.08.2019")]
[assembly: AssemblyTrademark("© BlubbFish")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("de-DE")]
// 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
@ -32,13 +34,21 @@ using System.Runtime.InteropServices;
// 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.1.4")]
[assembly: AssemblyFileVersion("1.1.4")]
[assembly: AssemblyVersion("1.2.1")]
[assembly: AssemblyFileVersion("1.2.1")]
#endif
/*
* 1.1.0 Remove Helper from Bot-Utils
* 1.1.1 Update to local librarys
* 1.1.2 Fixing bug for Contenttype
* 1.1.3 Variables parsing now as a String
* 1.1.4 add Woff as Binary type
* 1.1.5 add a function to send an object as json directly
* 1.1.6 rename functions and make SendFileResponse with a parameter for the folder (default resources),
* also put returntype boolean, add function that parse post params, if path is a dictionary try to load index.html
* 1.1.7 Restrucutre loading, so that all is init and after the listener is started, REQUEST_URL_HOST gives now host and port
* 1.1.8 Add logger to Webserver Class
* 1.1.9 Modify Output of SendFileResponse
* 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.1 When using Dispose, kill also mqtt connection and other tiny fixes
*/

View File

@ -1,104 +0,0 @@
using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace BlubbFish.Utils.IoT.Bots
{
public abstract class Webserver
{
protected Dictionary<String, String> config;
protected InIReader requests;
protected HttpListener httplistener;
public Webserver(ABackend backend, Dictionary<String, String> settings, InIReader requests) {
this.config = settings;
this.requests = requests;
backend.MessageIncomming += this.Backend_MessageIncomming;
this.httplistener = new HttpListener();
this.httplistener.Prefixes.Add(this.config["prefix"]);
this.httplistener.Start();
ThreadPool.QueueUserWorkItem((o) => {
Console.WriteLine("Webserver is Running...");
try {
while (this.httplistener.IsListening) {
ThreadPool.QueueUserWorkItem((state) => {
HttpListenerContext httplistenercontext = state as HttpListenerContext;
try {
this.SendResponse(httplistenercontext);
} catch { } finally {
httplistenercontext.Response.OutputStream.Close();
}
}, this.httplistener.GetContext());
}
} catch { };
});
}
protected virtual void SendResponse(HttpListenerContext cont) {
String restr = cont.Request.Url.PathAndQuery;
if (restr.StartsWith("/")) {
if(restr.IndexOf("?") != -1) {
restr = restr.Substring(1, restr.IndexOf("?")-1);
} else {
restr = restr.Substring(1);
}
if(restr == "") {
restr = "index.html";
}
String end = restr.IndexOf('.') != -1 ? restr.Substring(restr.IndexOf('.')+1) : "";
if (File.Exists("resources/"+ restr)) {
try {
if (end == "png" || end == "jpg" || end == "jpeg" || end == "ico" || end == "woff") {
Byte[] output = File.ReadAllBytes("resources/" + restr);
switch(end) {
case "ico": cont.Response.ContentType = "image/x-ico"; break;
case "woff": cont.Response.ContentType = "font/woff"; break;
}
cont.Response.OutputStream.Write(output, 0, output.Length);
return;
} else {
String file = File.ReadAllText("resources/" + restr);
if (this.requests.GetSections(false).Contains(restr)) {
Dictionary<String, String> vars = this.requests.GetSection(restr);
foreach (KeyValuePair<String, String> item in vars) {
file = file.Replace("\"{%" + item.Key.ToUpper() + "%}\"", item.Value);
}
}
file = file.Replace("{%REQUEST_URL_HOST%}", cont.Request.Url.Host);
Byte[] buf = Encoding.UTF8.GetBytes(file);
cont.Response.ContentLength64 = buf.Length;
switch(end) {
case "css": cont.Response.ContentType = "text/css"; break;
}
cont.Response.OutputStream.Write(buf, 0, buf.Length);
Console.WriteLine("200 - " + cont.Request.Url.PathAndQuery);
return;
}
} catch(Exception e) {
Helper.WriteError("500 - " + e.Message);
cont.Response.StatusCode = 500;
return;
}
}
Helper.WriteError("404 - " + cont.Request.Url.PathAndQuery + " not found!");
cont.Response.StatusCode = 404;
return;
}
return;
}
public void Dispose() {
this.httplistener.Stop();
this.httplistener.Close();
}
protected abstract void Backend_MessageIncomming(Object sender, BackendEvent e);
}
}

92
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,92 @@
# Contributing
When contributing to this repository, please first discuss the change you wish to make via issue,
email, or any other method with the owners of this repository before making a change.
Please note we have a code of conduct, please follow it in all your interactions with the project.
## Pull Request Process
1. Ensure any install or build dependencies are removed before the end of the layer when doing a
build.
2. Update the README.md with details of changes to the interface, this includes new environment
variables, exposed ports, useful file locations and container parameters.
3. Increase the version numbers in any examples files and the README.md to the new version that this
Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/).
4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you
do not have permission to do that, you may request the second reviewer to merge it for you.
## Code of Conduct
### Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
nationality, personal appearance, race, religion, or sexual identity and
orientation.
### Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
### Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
### Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
### Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at git ATTTT blubbfish.net. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
### Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [http://contributor-covenant.org/version/1/4][version]
[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/

124
Changelog.md Normal file
View File

@ -0,0 +1,124 @@
# Changelog
## 1.2.6 - 2022-01-25 - Makeing Logging easyier
### New Features
* Construct the Programm logger only if enabled
* Set Searchpath by default for a Bot
* Add an option that allows to debug logging (by default on)
### Bugfixes
### Changes
* Deconstruct Programmlogger
* Change all classes that extend ABot
* Module messages also uses debug logging flag
* Codingstyles
## 1.2.5 - 2022-01-20 - Better linux handling
### New Features
* Add ProcessExit Handler in ABot
* Add Output in ModulDispose
### Bugfixes
* Eleminate Hangs in Cronjob, when in startup phase it not blocks shutdown
### Changes
* Reweite ABot, remove Mono Code
* Codingstyle
## 1.2.4 - 2022-01-18 - Config enabled module loading
### New Features
* Modules can have an enabled=true|false in config, so that also enables or disables moduleloading.
### Bugfixes
### Changes
## 1.2.3 - 2022-01-09 - Tiny Refactoring
### New Features
### Bugfixes
### Changes
* Cleanup Changelog
* Tiny Refactorings
## 1.2.2 - 2021-08-22 - Going to netcore
### New Features
* Split Wbserver to AWebserver and AWebserverDataBackend
* Add mp4 as binary content
* make a mirror of this repo on github
### Bugfixes
### Changes
* change to c+ newer coding style
* mograde to c# netcore 3.1
## 1.2.1 - 2019-08-30 - When using Dispose, kill also mqtt connection and other tiny fixes
### 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 - 2019-05-27 - Refactor Bot to ABot and refere MultiSourceBot
### New Features
* Add MultiSourceBot*
* Refere MultiSourceBot, Webserver and Bot to it.
### Bugfixes
### Changes
* Refactor Bot to ABot
* Rewrite Mqtt module so that it not need to watch the connection.
## 1.1.9 - 2019-04-21 - Modify Output of SendFileResponse
### New Features
* Modify Output of SendFileResponse
### Bugfixes
### Changes
## 1.1.8 - 2019-04-15 - Add logger to Webserver Class
### New Features
* Add logger to Webserver Class
### Bugfixes
### Changes
## 1.1.7 - 2019-04-14 - REQUEST_URL_HOST
### New Features
### Bugfixes
### 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 - 2019-04-03 - Refactoring and bugfixing
### New Features
* SendFileResponse with a parameter for the folder
* add function that parse post params
### Bugfixes
### Changes
## 1.1.5 - 2019-03-27 - add a function to send an object as json directly
### New Features
* add a function to send an object as json directly
### Bugfixes
### Changes
## 1.1.4 - 2019-03-13 - add Woff as Binary type
### New Features
* add Woff as Binary type
### Bugfixes
### Changes
## 1.1.3 - 2019-03-10 - Variables parsing now as a String
### New Features
### Bugfixes
### Changes
* Variables parsing now as a String
## 1.1.2 - 2019-03-08 - Fixing bug for Contenttype
### New Features
### Bugfixes
* Fixing bug for Contenttype
### Changes
## 1.1.1 - 2019-02-17 - Update to local librarys
### New Features
### Bugfixes
### Changes
* Update to local librarys
## 1.1.0 - 2019-02-14 - Remove Helper from Bot-Utils
### New Features
* Remove Helper from Bot-Utils
### Bugfixes
### Changes

165
LICENSE Normal file
View File

@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

12
README.md Normal file
View File

@ -0,0 +1,12 @@
# BlubbFish.Utils.IoT.Bots (Bot-Utils)
Library that makes it easier to create bots.
Maybe you find this Repo on Github. This is a mirror from [here](https://git.blubbfish.net/vs_utils/Bot-Utils).
## Linking to
### Internal
* BlubbFish.Utils ([Utils](http://git.blubbfish.net/vs_utils/Utils))
* BlubbFish.Utils.IoT ([Utils-IoT](http://git.blubbfish.net/vs_utils/Utils-IoT))
### External
* litjson ([Used Fork](https://github.com/blubbfish/litjson), [Original](https://github.com/LitJSON/litjson))