9 Commits
15 changed files with 156 additions and 42 deletions
+23
View File
@@ -1,9 +1,12 @@
using System;
using System.Runtime.Loader;
using System.Threading;
namespace BlubbFish.Utils.IoT.Bots {
public abstract class ABot {
#if !NETCOREAPP
private Thread sig_thread;
#endif
private Boolean RunningProcess = true;
protected ProgramLogger logger = new ProgramLogger();
@@ -14,8 +17,18 @@ namespace BlubbFish.Utils.IoT.Bots {
this.RunningProcess = false;
}
#if NETCOREAPP
private void Default_Unloading(AssemblyLoadContext obj) {
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.SetupShutdown: Signalhandler Windows NETCORE recieved.");
this.RunningProcess = false;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Shutdown.");
this.Dispose();
}
#endif
protected void WaitForShutdown() {
if(Type.GetType("Mono.Runtime") != null) {
#if !NETCOREAPP
this.sig_thread = new Thread(delegate () {
Mono.Unix.UnixSignal[] signals = new Mono.Unix.UnixSignal[] {
new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGTERM),
@@ -30,19 +43,29 @@ namespace BlubbFish.Utils.IoT.Bots {
}
});
this.sig_thread.Start();
#endif
} else {
#if NETCOREAPP
AssemblyLoadContext.Default.Unloading += this.Default_Unloading;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Netcore attached.");
#endif
Console.CancelKeyPress += new ConsoleCancelEventHandler(this.SetupShutdown);
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Windows attached.");
}
while(this.RunningProcess) {
Thread.Sleep(100);
}
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Shutdown.");
}
public virtual void Dispose() {
#if !NETCOREAPP
if(this.sig_thread != null && this.sig_thread.IsAlive) {
this.sig_thread.Abort();
}
#endif
}
}
}
+6
View File
@@ -64,6 +64,12 @@
<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>
+53
View File
@@ -0,0 +1,53 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<RootNamespace>BlubbFish.Utils.IoT.Bots</RootNamespace>
<AssemblyName>Bot-Utils</AssemblyName>
<PackageId>Bots.IoT.Utils.BlubbFish</PackageId>
<Version>1.2.1</Version>
<AssemblyVersion>1.2.1</AssemblyVersion>
<FileVersion>1.2.1</FileVersion>
<NeutralLanguage>de-DE</NeutralLanguage>
<Description>Bot-Utils are helpers for programming a bot</Description>
<Authors>BlubbFish</Authors>
<Company>BlubbFish</Company>
<Copyright>Copyright © BlubbFish 2018 - 30.08.2019</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.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.1.9 Modify Output of SendFileResponse
1.1.8 Add logger to Webserver Class
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.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.5 add a function to send an object as json directly
1.1.4 add Woff as Binary type
1.1.3 Variables parsing now as a String
1.1.2 Fixing bug for Contenttype
1.1.1 Update to local librarys
1.1.0 Remove Helper from Bot-Utils</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Librarys\litjson\litjson\litjson_Core.csproj" />
<ProjectReference Include="..\..\Utils-IoT\Utils-IoT\Utils-IoT_Core.csproj" />
<ProjectReference Include="..\..\Utils\Utils\Utils_Core.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="../CHANGELOG.md" />
<Content Include="../CONTRIBUTING.md" />
<Content Include="../LICENSE" />
<Content Include="../README.md" />
</ItemGroup>
<ItemGroup>
<None Include="..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>
+1 -3
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;
}
}
+1 -3
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;
}
}
+1 -3
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;
}
}
+1 -3
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;
}
}
+2 -2
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;
}
@@ -61,7 +61,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
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"]);
+3 -5
View File
@@ -30,7 +30,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
#region Cronjobrunner
protected void Runner() {
Thread.Sleep(DateTime.Now.AddMinutes(1).AddSeconds(DateTime.Now.Second * (-1)).AddMilliseconds(DateTime.Now.Millisecond * (-1)) - DateTime.Now);
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;
@@ -140,9 +140,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
#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));
}
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
@@ -164,7 +162,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
}
public override void Dispose() {
Dispose(true);
this.Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
+3 -7
View File
@@ -6,9 +6,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
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
@@ -63,9 +61,7 @@ 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();
#endregion
#region IDisposable Support
@@ -80,7 +76,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
}
public override void Dispose() {
Dispose(true);
this.Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
+1 -1
View File
@@ -45,7 +45,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
}
public override void Dispose() {
Dispose(true);
this.Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
+4 -3
View File
@@ -1,4 +1,5 @@
using System.Reflection;
#if !NETCOREAPP
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
@@ -10,7 +11,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("BlubbFish")]
[assembly: AssemblyProduct("Bot-Utils")]
[assembly: AssemblyCopyright("Copyright © 2018 - 30.08.2019")]
[assembly: AssemblyCopyright("Copyright © BlubbFish 2018 - 30.08.2019")]
[assembly: AssemblyTrademark("© BlubbFish")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("de-DE")]
@@ -35,7 +36,7 @@ using System.Runtime.InteropServices;
// [assembly: AssemblyVersion("1.0.*")]
[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
+11 -9
View File
@@ -32,10 +32,10 @@ namespace BlubbFish.Utils.IoT.Bots {
Console.WriteLine("Webserver is Running...");
try {
while(this.httplistener.IsListening) {
ThreadPool.QueueUserWorkItem((state) => {
_ = ThreadPool.QueueUserWorkItem((state) => {
HttpListenerContext httplistenercontext = state as HttpListenerContext;
try {
this.SendWebserverResponse(httplistenercontext);
_ = this.SendWebserverResponse(httplistenercontext);
} catch { } finally {
httplistenercontext.Response.OutputStream.Close();
}
@@ -48,18 +48,14 @@ namespace BlubbFish.Utils.IoT.Bots {
public static Boolean SendFileResponse(HttpListenerContext cont, String folder = "resources", Boolean printOutput = true) {
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);
}
restr = restr.IndexOf("?") != -1 ? restr[1..restr.IndexOf("?")] : restr.Substring(1);
if(Directory.Exists(folder + "/" + restr)) {
restr += "/index.html";
}
String end = restr.IndexOf('.') != -1 ? restr.Substring(restr.IndexOf('.') + 1) : "";
if(File.Exists(folder + "/" + restr)) {
try {
if(end == "png" || end == "jpg" || end == "jpeg" || end == "ico" || end == "woff") {
if(end == "png" || end == "jpg" || end == "jpeg" || end == "ico" || end == "woff" || end == "mp4") {
Byte[] output = File.ReadAllBytes(folder + "/" + restr);
switch(end) {
case "ico":
@@ -68,6 +64,9 @@ namespace BlubbFish.Utils.IoT.Bots {
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) {
@@ -76,7 +75,7 @@ namespace BlubbFish.Utils.IoT.Bots {
return true;
} else {
String file = File.ReadAllText(folder + "/" + restr);
if(requests.GetSections(false).Contains(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);
@@ -145,7 +144,10 @@ namespace BlubbFish.Utils.IoT.Bots {
}
public override void Dispose() {
if(this.httplistener.IsListening) {
this.httplistener.Stop();
}
this.httplistener.Close();
if(this.databackend != null) {
this.databackend.Dispose();
+43
View File
@@ -0,0 +1,43 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.87
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bot-Utils_Core", "Bot-Utils\Bot-Utils_Core.csproj", "{CB48457C-C567-42BC-9E5B-DAE5A3383108}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils-IoT_Core", "..\Utils-IoT\Utils-IoT\Utils-IoT_Core.csproj", "{86E9D6ED-EE31-4FE7-AEA6-555DBBAA69A4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils_Core", "..\Utils\Utils\Utils_Core.csproj", "{110EB1ED-1567-4A7E-B8B5-E46F421E0684}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "litjson_Core", "..\..\Librarys\litjson\litjson\litjson_Core.csproj", "{A6EB1FF2-A3F1-4C97-8E7F-0F2CE4A8A820}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{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 = {0F0DF907-D03F-41A9-B0A2-CA229CC259EE}
EndGlobalSection
EndGlobal
View File