move Bot-Utils to Subfolder
This commit is contained in:
parent
225de0b9a2
commit
925df66e49
@ -1,82 +1,82 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<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')" />
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
<ProjectGuid>{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}</ProjectGuid>
|
<ProjectGuid>{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}</ProjectGuid>
|
||||||
<OutputType>Library</OutputType>
|
<OutputType>Library</OutputType>
|
||||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
<RootNamespace>BlubbFish.Utils.IoT.Bots</RootNamespace>
|
<RootNamespace>BlubbFish.Utils.IoT.Bots</RootNamespace>
|
||||||
<AssemblyName>Bot-Utils</AssemblyName>
|
<AssemblyName>Bot-Utils</AssemblyName>
|
||||||
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
|
||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
<NuGetPackageImportStamp>
|
<NuGetPackageImportStamp>
|
||||||
</NuGetPackageImportStamp>
|
</NuGetPackageImportStamp>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
<DebugType>full</DebugType>
|
<DebugType>full</DebugType>
|
||||||
<Optimize>false</Optimize>
|
<Optimize>false</Optimize>
|
||||||
<OutputPath>bin\Debug\</OutputPath>
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
<DebugType>pdbonly</DebugType>
|
<DebugType>pdbonly</DebugType>
|
||||||
<Optimize>true</Optimize>
|
<Optimize>true</Optimize>
|
||||||
<OutputPath>bin\Release\</OutputPath>
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
<DefineConstants>TRACE</DefineConstants>
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<StartupObject />
|
<StartupObject />
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="Mono.Posix">
|
<Reference Include="Mono.Posix">
|
||||||
<HintPath>..\..\Zway-Bot\packages\Mono.Posix.dll</HintPath>
|
<HintPath>..\..\Zway-Bot\packages\Mono.Posix.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
<Reference Include="System.Data" />
|
<Reference Include="System.Data" />
|
||||||
<Reference Include="System.Net.Http" />
|
<Reference Include="System.Net.Http" />
|
||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Bot.cs" />
|
<Compile Include="Bot.cs" />
|
||||||
<Compile Include="Events\CronEvent.cs" />
|
<Compile Include="Events\CronEvent.cs" />
|
||||||
<Compile Include="Events\ModulEventArgs.cs" />
|
<Compile Include="Events\ModulEventArgs.cs" />
|
||||||
<Compile Include="Events\SenmlEvent.cs" />
|
<Compile Include="Events\SenmlEvent.cs" />
|
||||||
<Compile Include="Events\MqttEvent.cs" />
|
<Compile Include="Events\MqttEvent.cs" />
|
||||||
<Compile Include="Events\OvertakerEvent.cs" />
|
<Compile Include="Events\OvertakerEvent.cs" />
|
||||||
<Compile Include="Events\StatusPollingEvent.cs" />
|
<Compile Include="Events\StatusPollingEvent.cs" />
|
||||||
<Compile Include="Interfaces\IForceLoad.cs" />
|
<Compile Include="Interfaces\IForceLoad.cs" />
|
||||||
<Compile Include="Moduls\AModul.cs" />
|
<Compile Include="Moduls\AModul.cs" />
|
||||||
<Compile Include="Moduls\CronJob.cs" />
|
<Compile Include="Moduls\CronJob.cs" />
|
||||||
<Compile Include="Moduls\Mqtt.cs" />
|
<Compile Include="Moduls\Mqtt.cs" />
|
||||||
<Compile Include="Moduls\Overtaker.cs" />
|
<Compile Include="Moduls\Overtaker.cs" />
|
||||||
<Compile Include="Moduls\Statuspolling.cs" />
|
<Compile Include="Moduls\Statuspolling.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Webserver.cs" />
|
<Compile Include="Webserver.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj">
|
<ProjectReference Include="..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj">
|
||||||
<Project>{91a14cd2-2940-4500-8193-56d37edddbaa}</Project>
|
<Project>{91a14cd2-2940-4500-8193-56d37edddbaa}</Project>
|
||||||
<Name>litjson_4.7.1</Name>
|
<Name>litjson_4.7.1</Name>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
<ProjectReference Include="..\IoT\Utils-IoT.csproj">
|
<ProjectReference Include="..\IoT\Utils-IoT.csproj">
|
||||||
<Project>{b870e4d5-6806-4a0b-b233-8907eedc5afc}</Project>
|
<Project>{b870e4d5-6806-4a0b-b233-8907eedc5afc}</Project>
|
||||||
<Name>Utils-IoT</Name>
|
<Name>Utils-IoT</Name>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
<ProjectReference Include="..\Utils\Utils.csproj">
|
<ProjectReference Include="..\Utils\Utils.csproj">
|
||||||
<Project>{fac8ce64-bf13-4ece-8097-aeb5dd060098}</Project>
|
<Project>{fac8ce64-bf13-4ece-8097-aeb5dd060098}</Project>
|
||||||
<Name>Utils</Name>
|
<Name>Utils</Name>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
@ -1,99 +1,99 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using BlubbFish.Utils.IoT.Bots.Moduls;
|
using BlubbFish.Utils.IoT.Bots.Moduls;
|
||||||
using BlubbFish.Utils.IoT.Bots.Events;
|
using BlubbFish.Utils.IoT.Bots.Events;
|
||||||
using BlubbFish.Utils.IoT.Bots.Interfaces;
|
using BlubbFish.Utils.IoT.Bots.Interfaces;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots {
|
namespace BlubbFish.Utils.IoT.Bots {
|
||||||
public abstract class Bot<T> {
|
public abstract class Bot<T> {
|
||||||
private Thread sig_thread;
|
private Thread sig_thread;
|
||||||
private Boolean RunningProcess = true;
|
private Boolean RunningProcess = true;
|
||||||
protected ProgramLogger logger = new ProgramLogger();
|
protected ProgramLogger logger = new ProgramLogger();
|
||||||
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 WaitForShutdown() {
|
protected void WaitForShutdown() {
|
||||||
if (Type.GetType("Mono.Runtime") != null) {
|
if (Type.GetType("Mono.Runtime") != null) {
|
||||||
this.sig_thread = new Thread(delegate () {
|
this.sig_thread = new Thread(delegate () {
|
||||||
Mono.Unix.UnixSignal[] signals = new Mono.Unix.UnixSignal[] {
|
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.SIGTERM),
|
||||||
new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGINT)
|
new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGINT)
|
||||||
};
|
};
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono attached.");
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono attached.");
|
||||||
while (true) {
|
while (true) {
|
||||||
Int32 i = Mono.Unix.UnixSignal.WaitAny(signals, -1);
|
Int32 i = Mono.Unix.UnixSignal.WaitAny(signals, -1);
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono INT recieved " + i + ".");
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono INT recieved " + i + ".");
|
||||||
this.RunningProcess = false;
|
this.RunningProcess = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.sig_thread.Start();
|
this.sig_thread.Start();
|
||||||
} else {
|
} else {
|
||||||
Console.CancelKeyPress += new ConsoleCancelEventHandler(this.SetupShutdown);
|
Console.CancelKeyPress += new ConsoleCancelEventHandler(this.SetupShutdown);
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Windows attached.");
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Windows attached.");
|
||||||
}
|
}
|
||||||
while (this.RunningProcess) {
|
while (this.RunningProcess) {
|
||||||
Thread.Sleep(100);
|
Thread.Sleep(100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetupShutdown(Object sender, ConsoleCancelEventArgs e) {
|
private void SetupShutdown(Object sender, ConsoleCancelEventArgs e) {
|
||||||
e.Cancel = true;
|
e.Cancel = true;
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.SetupShutdown: Signalhandler Windows INT recieved.");
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.SetupShutdown: Signalhandler Windows INT recieved.");
|
||||||
this.RunningProcess = false;
|
this.RunningProcess = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
if (this.sig_thread != null && this.sig_thread.IsAlive) {
|
if (this.sig_thread != null && this.sig_thread.IsAlive) {
|
||||||
this.sig_thread.Abort();
|
this.sig_thread.Abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
protected void ModulUpdate(Object sender, ModulEventArgs e) {
|
||||||
Console.WriteLine(e.ToString());
|
Console.WriteLine(e.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,16 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots.Events {
|
namespace BlubbFish.Utils.IoT.Bots.Events {
|
||||||
public class CronEvent : ModulEventArgs {
|
public class CronEvent : ModulEventArgs {
|
||||||
|
|
||||||
public CronEvent() {
|
public CronEvent() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public CronEvent(String addr, String prop, String value) {
|
public CronEvent(String addr, String prop, String value) {
|
||||||
this.Address = addr;
|
this.Address = addr;
|
||||||
this.Property = prop;
|
this.Property = prop;
|
||||||
this.Value = value;
|
this.Value = value;
|
||||||
this.Source = "Cronjob";
|
this.Source = "Cronjob";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,21 +1,21 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots.Events {
|
namespace BlubbFish.Utils.IoT.Bots.Events {
|
||||||
public class ModulEventArgs : EventArgs {
|
public class ModulEventArgs : EventArgs {
|
||||||
public ModulEventArgs() {
|
public ModulEventArgs() {
|
||||||
}
|
}
|
||||||
public ModulEventArgs(String addr, String prop, String val, String src) {
|
public ModulEventArgs(String addr, String prop, String val, String src) {
|
||||||
this.Address = addr;
|
this.Address = addr;
|
||||||
this.Property = prop;
|
this.Property = prop;
|
||||||
this.Value = val;
|
this.Value = val;
|
||||||
this.Source = src;
|
this.Source = src;
|
||||||
}
|
}
|
||||||
public String Address { get; protected set; }
|
public String Address { get; protected set; }
|
||||||
public String Property { get; protected set; }
|
public String Property { get; protected set; }
|
||||||
public String Value { get; protected set; }
|
public String Value { get; protected set; }
|
||||||
public String Source { get; protected set; }
|
public String Source { get; protected set; }
|
||||||
public override String ToString() {
|
public override String ToString() {
|
||||||
return this.Source + ": " + this.Address + " set " + this.Property + " to " + this.Value;
|
return this.Source + ": " + this.Address + " set " + this.Property + " to " + this.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,16 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots.Events {
|
namespace BlubbFish.Utils.IoT.Bots.Events {
|
||||||
public class MqttEvent : ModulEventArgs {
|
public class MqttEvent : ModulEventArgs {
|
||||||
public MqttEvent() {
|
public MqttEvent() {
|
||||||
}
|
}
|
||||||
public MqttEvent(String topic, String text) {
|
public MqttEvent(String topic, String text) {
|
||||||
this.Address = topic;
|
this.Address = topic;
|
||||||
this.Value = text;
|
this.Value = text;
|
||||||
this.Source = "MQTT";
|
this.Source = "MQTT";
|
||||||
}
|
}
|
||||||
public override String ToString() {
|
public override String ToString() {
|
||||||
return this.Source + ": on " + this.Address + " set " + this.Value;
|
return this.Source + ": on " + this.Address + " set " + this.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,16 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots.Events {
|
namespace BlubbFish.Utils.IoT.Bots.Events {
|
||||||
public class OvertakerEvent : ModulEventArgs {
|
public class OvertakerEvent : ModulEventArgs {
|
||||||
|
|
||||||
public OvertakerEvent() {
|
public OvertakerEvent() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public OvertakerEvent(String addr, String prop, String value) {
|
public OvertakerEvent(String addr, String prop, String value) {
|
||||||
this.Address = addr;
|
this.Address = addr;
|
||||||
this.Property = prop;
|
this.Property = prop;
|
||||||
this.Value = value;
|
this.Value = value;
|
||||||
this.Source = "Overtaker";
|
this.Source = "Overtaker";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,16 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots.Events {
|
namespace BlubbFish.Utils.IoT.Bots.Events {
|
||||||
public class SenmlEvent : ModulEventArgs {
|
public class SenmlEvent : ModulEventArgs {
|
||||||
public SenmlEvent() {
|
public SenmlEvent() {
|
||||||
}
|
}
|
||||||
public SenmlEvent(String topic, String text) {
|
public SenmlEvent(String topic, String text) {
|
||||||
this.Address = topic;
|
this.Address = topic;
|
||||||
this.Value = text;
|
this.Value = text;
|
||||||
this.Source = "Senml";
|
this.Source = "Senml";
|
||||||
}
|
}
|
||||||
public override String ToString() {
|
public override String ToString() {
|
||||||
return this.Source + ": on " + this.Address + " set " + this.Value;
|
return this.Source + ": on " + this.Address + " set " + this.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,18 +1,18 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots.Events {
|
namespace BlubbFish.Utils.IoT.Bots.Events {
|
||||||
public class StatusPollingEvent : ModulEventArgs {
|
public class StatusPollingEvent : ModulEventArgs {
|
||||||
public StatusPollingEvent() {
|
public StatusPollingEvent() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public StatusPollingEvent(String text, String node) {
|
public StatusPollingEvent(String text, String node) {
|
||||||
this.Value = text;
|
this.Value = text;
|
||||||
this.Address = node;
|
this.Address = node;
|
||||||
this.Source = "POLLING";
|
this.Source = "POLLING";
|
||||||
}
|
}
|
||||||
|
|
||||||
public override String ToString() {
|
public override String ToString() {
|
||||||
return this.Source + ": " + this.Value + " on " + this.Address;
|
return this.Source + ": " + this.Value + " on " + this.Address;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
namespace BlubbFish.Utils.IoT.Bots.Interfaces {
|
namespace BlubbFish.Utils.IoT.Bots.Interfaces {
|
||||||
public interface IForceLoad {
|
public interface IForceLoad {
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,79 +1,79 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using BlubbFish.Utils.IoT.Bots.Events;
|
using BlubbFish.Utils.IoT.Bots.Events;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots.Moduls {
|
namespace BlubbFish.Utils.IoT.Bots.Moduls {
|
||||||
public abstract class AModul<T> {
|
public abstract class AModul<T> {
|
||||||
protected T library;
|
protected T library;
|
||||||
private readonly InIReader settings;
|
private readonly InIReader settings;
|
||||||
protected Dictionary<String, Dictionary<String, String>> config = new Dictionary<String, Dictionary<String, String>>();
|
protected Dictionary<String, Dictionary<String, String>> config = new Dictionary<String, Dictionary<String, String>>();
|
||||||
|
|
||||||
public Boolean HasConfig { get; private set; }
|
public Boolean HasConfig { get; private set; }
|
||||||
public Boolean ConfigPublic { get; private set; }
|
public Boolean ConfigPublic { get; private set; }
|
||||||
|
|
||||||
public delegate void ModulEvent(Object sender, ModulEventArgs e);
|
public delegate void ModulEvent(Object sender, ModulEventArgs e);
|
||||||
public abstract event ModulEvent Update;
|
public abstract event ModulEvent Update;
|
||||||
|
|
||||||
public AModul(T lib, InIReader settings) {
|
public AModul(T lib, InIReader settings) {
|
||||||
this.HasConfig = false;
|
this.HasConfig = false;
|
||||||
this.ConfigPublic = false;
|
this.ConfigPublic = false;
|
||||||
this.library = lib;
|
this.library = lib;
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
this.ParseConfig();
|
this.ParseConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ParseConfig() {
|
private void ParseConfig() {
|
||||||
if (this.settings != null) {
|
if (this.settings != null) {
|
||||||
this.HasConfig = true;
|
this.HasConfig = true;
|
||||||
foreach (String item in this.settings.GetSections(false)) {
|
foreach (String item in this.settings.GetSections(false)) {
|
||||||
this.config.Add(item, this.settings.GetSection(item));
|
this.config.Add(item, this.settings.GetSection(item));
|
||||||
}
|
}
|
||||||
if (this.config.ContainsKey("modul")) {
|
if (this.config.ContainsKey("modul")) {
|
||||||
this.ConfigPublic = this.config["modul"].ContainsKey("config") && this.config["modul"]["config"].ToLower() == "public";
|
this.ConfigPublic = this.config["modul"].ContainsKey("config") && this.config["modul"]["config"].ToLower() == "public";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dictionary<String, Dictionary<String, String>> GetConfig() {
|
public Dictionary<String, Dictionary<String, String>> GetConfig() {
|
||||||
if (this.HasConfig && this.ConfigPublic) {
|
if (this.HasConfig && this.ConfigPublic) {
|
||||||
Dictionary<String, Dictionary<String, String>> ret = new Dictionary<String, Dictionary<String, String>>(this.config);
|
Dictionary<String, Dictionary<String, String>> ret = new Dictionary<String, Dictionary<String, String>>(this.config);
|
||||||
if (ret.ContainsKey("modul")) {
|
if (ret.ContainsKey("modul")) {
|
||||||
ret.Remove("modul");
|
ret.Remove("modul");
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
return new Dictionary<String, Dictionary<String, String>>();
|
return new Dictionary<String, Dictionary<String, String>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Interconnect(Dictionary<String, AModul<T>> moduls) { }
|
public virtual void Interconnect(Dictionary<String, AModul<T>> moduls) { }
|
||||||
|
|
||||||
public virtual void SetInterconnection(String param, Action<Object> hook, Object data) { }
|
public virtual void SetInterconnection(String param, Action<Object> hook, Object data) { }
|
||||||
|
|
||||||
public abstract void EventLibSetter();
|
public abstract void EventLibSetter();
|
||||||
|
|
||||||
protected abstract void LibUpadteThread(Object state);
|
protected abstract void LibUpadteThread(Object state);
|
||||||
|
|
||||||
protected void HandleLibUpdate(Object sender, EventArgs e) {
|
protected void HandleLibUpdate(Object sender, EventArgs e) {
|
||||||
ThreadPool.QueueUserWorkItem(this.LibUpadteThread, e);
|
ThreadPool.QueueUserWorkItem(this.LibUpadteThread, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void Dispose();
|
public abstract void Dispose();
|
||||||
|
|
||||||
public void SetConfig(Dictionary<String, Dictionary<String, String>> newconf) {
|
public void SetConfig(Dictionary<String, Dictionary<String, String>> newconf) {
|
||||||
if (this.HasConfig && this.ConfigPublic) {
|
if (this.HasConfig && this.ConfigPublic) {
|
||||||
if (newconf.ContainsKey("modul")) {
|
if (newconf.ContainsKey("modul")) {
|
||||||
newconf.Remove("modul");
|
newconf.Remove("modul");
|
||||||
}
|
}
|
||||||
if (this.config.ContainsKey("modul")) {
|
if (this.config.ContainsKey("modul")) {
|
||||||
newconf.Add("modul", this.config["modul"]);
|
newconf.Add("modul", this.config["modul"]);
|
||||||
}
|
}
|
||||||
this.config = newconf;
|
this.config = newconf;
|
||||||
this.settings.SetSections(this.config);
|
this.settings.SetSections(this.config);
|
||||||
this.UpdateConfig();
|
this.UpdateConfig();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void UpdateConfig();
|
protected abstract void UpdateConfig();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,172 +1,172 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using BlubbFish.Utils.IoT.Bots.Interfaces;
|
using BlubbFish.Utils.IoT.Bots.Interfaces;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots.Moduls {
|
namespace BlubbFish.Utils.IoT.Bots.Moduls {
|
||||||
public abstract class CronJob<T> : AModul<T>, IDisposable, IForceLoad {
|
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 readonly List<Tuple<String, Action<Object>, Object>> internalCron = new List<Tuple<String, Action<Object>, Object>>();
|
||||||
protected Thread thread;
|
protected Thread thread;
|
||||||
protected DateTime crontime;
|
protected DateTime crontime;
|
||||||
|
|
||||||
protected readonly Dictionary<String, String> cron_named = new Dictionary<String, String> {
|
protected readonly Dictionary<String, String> cron_named = new Dictionary<String, String> {
|
||||||
{ "@yearly", "0 0 1 1 *" },
|
{ "@yearly", "0 0 1 1 *" },
|
||||||
{ "@annually", "0 0 1 1 *" },
|
{ "@annually", "0 0 1 1 *" },
|
||||||
{ "@monthly", "0 0 1 * *" },
|
{ "@monthly", "0 0 1 * *" },
|
||||||
{ "@weekly", "0 0 * * 0" },
|
{ "@weekly", "0 0 * * 0" },
|
||||||
{ "@daily", "0 0 * * *" },
|
{ "@daily", "0 0 * * *" },
|
||||||
{ "@hourly", "0 * * * *" }
|
{ "@hourly", "0 * * * *" }
|
||||||
};
|
};
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
public CronJob(T lib, InIReader settings) : base(lib, settings) {
|
public CronJob(T lib, InIReader settings) : base(lib, settings) {
|
||||||
this.crontime = DateTime.Now;
|
this.crontime = DateTime.Now;
|
||||||
this.thread = new Thread(this.Runner);
|
this.thread = new Thread(this.Runner);
|
||||||
this.thread.Start();
|
this.thread.Start();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Cronjobrunner
|
#region Cronjobrunner
|
||||||
protected void Runner() {
|
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) {
|
while (true) {
|
||||||
if (this.crontime.Minute != DateTime.Now.Minute) {
|
if (this.crontime.Minute != DateTime.Now.Minute) {
|
||||||
this.crontime = DateTime.Now;
|
this.crontime = DateTime.Now;
|
||||||
if (this.config.Count != 0) {
|
if (this.config.Count != 0) {
|
||||||
foreach (KeyValuePair<String, Dictionary<String, String>> item in this.config) {
|
foreach (KeyValuePair<String, Dictionary<String, String>> item in this.config) {
|
||||||
if (item.Value.ContainsKey("cron") && item.Value.ContainsKey("set") && this.ParseCronString(item.Value["cron"])) {
|
if (item.Value.ContainsKey("cron") && item.Value.ContainsKey("set") && this.ParseCronString(item.Value["cron"])) {
|
||||||
this.SetValues(item.Value["set"]);
|
this.SetValues(item.Value["set"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach (Tuple<String, Action<Object>, Object> item in this.internalCron) {
|
foreach (Tuple<String, Action<Object>, Object> item in this.internalCron) {
|
||||||
if (this.ParseCronString(item.Item1)) {
|
if (this.ParseCronString(item.Item1)) {
|
||||||
item.Item2?.Invoke(item.Item3);
|
item.Item2?.Invoke(item.Item3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Thread.Sleep(100);
|
Thread.Sleep(100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void SetValues(String value);
|
protected abstract void SetValues(String value);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region CronFunctions
|
#region CronFunctions
|
||||||
protected Boolean ParseCronString(String cronstring) {
|
protected Boolean ParseCronString(String cronstring) {
|
||||||
cronstring = cronstring.Trim();
|
cronstring = cronstring.Trim();
|
||||||
if (this.cron_named.ContainsKey(cronstring)) {
|
if (this.cron_named.ContainsKey(cronstring)) {
|
||||||
cronstring = this.cron_named[cronstring];
|
cronstring = this.cron_named[cronstring];
|
||||||
}
|
}
|
||||||
String[] value = cronstring.Split(' ');
|
String[] value = cronstring.Split(' ');
|
||||||
if (value.Length != 5) {
|
if (value.Length != 5) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!this.CheckDateStr(this.crontime.ToString("mm"), value[0], "0-59")) {
|
if (!this.CheckDateStr(this.crontime.ToString("mm"), value[0], "0-59")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!this.CheckDateStr(this.crontime.ToString("HH"), value[1], "0-23")) {
|
if (!this.CheckDateStr(this.crontime.ToString("HH"), value[1], "0-23")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!this.CheckDateStr(this.crontime.ToString("MM"), value[3], "1-12")) {
|
if (!this.CheckDateStr(this.crontime.ToString("MM"), value[3], "1-12")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (value[2] != "*" && value[4] != "*") {
|
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")) {
|
if (!this.CheckDateStr(this.crontime.ToString("dd"), value[2], "1-31") && !this.CheckDateStr(((Int32)this.crontime.DayOfWeek).ToString(), value[4], "0-7")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!this.CheckDateStr(this.crontime.ToString("dd"), value[2], "1-31")) {
|
if (!this.CheckDateStr(this.crontime.ToString("dd"), value[2], "1-31")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!this.CheckDateStr(((Int32)this.crontime.DayOfWeek).ToString(), value[4], "0-7")) {
|
if (!this.CheckDateStr(((Int32)this.crontime.DayOfWeek).ToString(), value[4], "0-7")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
protected Boolean CheckDateStr(String date, String cron, String limit) {
|
protected Boolean CheckDateStr(String date, String cron, String limit) {
|
||||||
cron = cron.ToLower();
|
cron = cron.ToLower();
|
||||||
for (Int32 i = 0; i <= 6; i++) {
|
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("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());
|
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++) {
|
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("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());
|
cron = cron.Replace(DateTime.Parse("2015-" + i + "-01T00:00:00").ToString("MMMM", CultureInfo.CreateSpecificCulture("en-US")), i.ToString());
|
||||||
}
|
}
|
||||||
if (cron.Contains("*")) {
|
if (cron.Contains("*")) {
|
||||||
cron = cron.Replace("*", limit);
|
cron = cron.Replace("*", limit);
|
||||||
}
|
}
|
||||||
if (cron.Contains("-")) {
|
if (cron.Contains("-")) {
|
||||||
MatchCollection m = new Regex("(\\d+)-(\\d+)").Matches(cron);
|
MatchCollection m = new Regex("(\\d+)-(\\d+)").Matches(cron);
|
||||||
foreach (Match p in m) {
|
foreach (Match p in m) {
|
||||||
List<String> s = new List<String>();
|
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++) {
|
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());
|
s.Add(i.ToString());
|
||||||
}
|
}
|
||||||
cron = cron.Replace(p.Groups[0].Value, String.Join(",", s));
|
cron = cron.Replace(p.Groups[0].Value, String.Join(",", s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Int32 match = 0;
|
Int32 match = 0;
|
||||||
if (cron.Contains("/")) {
|
if (cron.Contains("/")) {
|
||||||
Match m = new Regex("/(\\d+)").Match(cron);
|
Match m = new Regex("/(\\d+)").Match(cron);
|
||||||
cron = cron.Replace(m.Groups[0].Value, "");
|
cron = cron.Replace(m.Groups[0].Value, "");
|
||||||
match = Int32.Parse(m.Groups[1].Value);
|
match = Int32.Parse(m.Groups[1].Value);
|
||||||
}
|
}
|
||||||
Dictionary<Int32, String> ret = new Dictionary<Int32, String>();
|
Dictionary<Int32, String> ret = new Dictionary<Int32, String>();
|
||||||
if (!cron.Contains(",")) {
|
if (!cron.Contains(",")) {
|
||||||
ret.Add(Int32.Parse(cron), "");
|
ret.Add(Int32.Parse(cron), "");
|
||||||
} else {
|
} else {
|
||||||
foreach (String item in cron.Split(',')) {
|
foreach (String item in cron.Split(',')) {
|
||||||
if (!ret.ContainsKey(Int32.Parse(item))) {
|
if (!ret.ContainsKey(Int32.Parse(item))) {
|
||||||
ret.Add(Int32.Parse(item), "");
|
ret.Add(Int32.Parse(item), "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (match != 0) {
|
if (match != 0) {
|
||||||
Dictionary<Int32, String> r = new Dictionary<Int32, String>();
|
Dictionary<Int32, String> r = new Dictionary<Int32, String>();
|
||||||
foreach (KeyValuePair<Int32, String> item in ret) {
|
foreach (KeyValuePair<Int32, String> item in ret) {
|
||||||
if (item.Key % match == 0) {
|
if (item.Key % match == 0) {
|
||||||
r.Add(item.Key, "");
|
r.Add(item.Key, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ret = r;
|
ret = r;
|
||||||
}
|
}
|
||||||
return ret.ContainsKey(Int32.Parse(date));
|
return ret.ContainsKey(Int32.Parse(date));
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region AModul
|
#region AModul
|
||||||
public override void SetInterconnection(String cron, Action<Object> hook, Object data) {
|
public override void SetInterconnection(String cron, Action<Object> hook, Object data) {
|
||||||
this.internalCron.Add(new Tuple<String, Action<Object>, Object>(cron, hook, data));
|
this.internalCron.Add(new Tuple<String, Action<Object>, Object>(cron, hook, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void UpdateConfig() { }
|
protected override void UpdateConfig() { }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region IDisposable Support
|
#region IDisposable Support
|
||||||
private Boolean disposedValue = false;
|
private Boolean disposedValue = false;
|
||||||
|
|
||||||
protected virtual void Dispose(Boolean disposing) {
|
protected virtual void Dispose(Boolean disposing) {
|
||||||
if (!this.disposedValue) {
|
if (!this.disposedValue) {
|
||||||
if (disposing) {
|
if (disposing) {
|
||||||
if (this.thread != null) {
|
if (this.thread != null) {
|
||||||
this.thread.Abort();
|
this.thread.Abort();
|
||||||
while (this.thread.ThreadState == ThreadState.Running) { Thread.Sleep(100); }
|
while (this.thread.ThreadState == ThreadState.Running) { Thread.Sleep(100); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.thread = null;
|
this.thread = null;
|
||||||
this.disposedValue = true;
|
this.disposedValue = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Dispose() {
|
public override void Dispose() {
|
||||||
Dispose(true);
|
Dispose(true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,119 +1,119 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using BlubbFish.Utils.IoT.Bots.Events;
|
using BlubbFish.Utils.IoT.Bots.Events;
|
||||||
using BlubbFish.Utils.IoT.Connector;
|
using BlubbFish.Utils.IoT.Connector;
|
||||||
using BlubbFish.Utils.IoT.Events;
|
using BlubbFish.Utils.IoT.Events;
|
||||||
using LitJson;
|
using LitJson;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots.Moduls {
|
namespace BlubbFish.Utils.IoT.Bots.Moduls {
|
||||||
public abstract class Mqtt<T> : AModul<T>, IDisposable {
|
public abstract class Mqtt<T> : AModul<T>, IDisposable {
|
||||||
protected readonly Thread connectionWatcher;
|
protected readonly Thread connectionWatcher;
|
||||||
protected ABackend mqtt;
|
protected ABackend mqtt;
|
||||||
protected Dictionary<String, AModul<T>> modules;
|
protected Dictionary<String, AModul<T>> modules;
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
public Mqtt(T lib, InIReader settings) : base(lib, settings) {
|
public Mqtt(T lib, InIReader settings) : base(lib, settings) {
|
||||||
if (this.config.ContainsKey("settings")) {
|
if (this.config.ContainsKey("settings")) {
|
||||||
this.connectionWatcher = new Thread(this.ConnectionWatcherRunner);
|
this.connectionWatcher = new Thread(this.ConnectionWatcherRunner);
|
||||||
this.connectionWatcher.Start();
|
this.connectionWatcher.Start();
|
||||||
} else {
|
} else {
|
||||||
throw new ArgumentException("Setting section [settings] is missing!");
|
throw new ArgumentException("Setting section [settings] is missing!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Watcher
|
#region Watcher
|
||||||
protected void ConnectionWatcherRunner() {
|
protected void ConnectionWatcherRunner() {
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
if (this.mqtt == null || !this.mqtt.IsConnected) {
|
if (this.mqtt == null || !this.mqtt.IsConnected) {
|
||||||
this.Reconnect();
|
this.Reconnect();
|
||||||
}
|
}
|
||||||
Thread.Sleep(10000);
|
Thread.Sleep(10000);
|
||||||
} catch (Exception) { }
|
} catch (Exception) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void Reconnect() {
|
protected void Reconnect() {
|
||||||
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Moduls.Mqtt.Reconnect()");
|
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Moduls.Mqtt.Reconnect()");
|
||||||
this.Disconnect();
|
this.Disconnect();
|
||||||
this.Connect();
|
this.Connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void Connect();
|
protected abstract void Connect();
|
||||||
|
|
||||||
protected abstract void Disconnect();
|
protected abstract void Disconnect();
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region AModul
|
#region AModul
|
||||||
public override void Interconnect(Dictionary<String, AModul<T>> moduls) {
|
public override void Interconnect(Dictionary<String, AModul<T>> moduls) {
|
||||||
this.modules = moduls;
|
this.modules = moduls;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void UpdateConfig() {
|
protected override void UpdateConfig() {
|
||||||
this.Reconnect();
|
this.Reconnect();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
protected Tuple<Boolean, MqttEvent> ChangeConfig(BackendEvent e, String topic) {
|
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"))) {
|
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());
|
Match m = new Regex("^"+ topic + "(\\w+)/[gs]et$|").Match(e.From.ToString());
|
||||||
if (!m.Groups[1].Success) {
|
if (!m.Groups[1].Success) {
|
||||||
return new Tuple<Boolean, MqttEvent>(false, null);
|
return new Tuple<Boolean, MqttEvent>(false, null);
|
||||||
}
|
}
|
||||||
AModul<T> modul = null;
|
AModul<T> modul = null;
|
||||||
foreach (KeyValuePair<String, AModul<T>> item in this.modules) {
|
foreach (KeyValuePair<String, AModul<T>> item in this.modules) {
|
||||||
if (item.Key.ToLower() == m.Groups[1].Value) {
|
if (item.Key.ToLower() == m.Groups[1].Value) {
|
||||||
modul = item.Value;
|
modul = item.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (modul == null) {
|
if (modul == null) {
|
||||||
return new Tuple<Boolean, MqttEvent>(false, null);
|
return new Tuple<Boolean, MqttEvent>(false, null);
|
||||||
}
|
}
|
||||||
if (e.From.ToString().EndsWith("/get") && modul.HasConfig && modul.ConfigPublic) {
|
if (e.From.ToString().EndsWith("/get") && modul.HasConfig && modul.ConfigPublic) {
|
||||||
String t = topic + m.Groups[1].Value;
|
String t = topic + m.Groups[1].Value;
|
||||||
String d = JsonMapper.ToJson(modul.GetConfig()).ToString();
|
String d = JsonMapper.ToJson(modul.GetConfig()).ToString();
|
||||||
((ADataBackend)this.mqtt).Send(t, d);
|
((ADataBackend)this.mqtt).Send(t, d);
|
||||||
return new Tuple<Boolean, MqttEvent>(true, new MqttEvent(t, d));
|
return new Tuple<Boolean, MqttEvent>(true, new MqttEvent(t, d));
|
||||||
} else if (e.From.ToString().EndsWith("/set") && modul.HasConfig && modul.ConfigPublic) {
|
} else if (e.From.ToString().EndsWith("/set") && modul.HasConfig && modul.ConfigPublic) {
|
||||||
try {
|
try {
|
||||||
JsonData a = JsonMapper.ToObject(e.Message);
|
JsonData a = JsonMapper.ToObject(e.Message);
|
||||||
Dictionary<String, Dictionary<String, String>> newconf = new Dictionary<String, Dictionary<String, String>>();
|
Dictionary<String, Dictionary<String, String>> newconf = new Dictionary<String, Dictionary<String, String>>();
|
||||||
foreach (String section in a.Keys) {
|
foreach (String section in a.Keys) {
|
||||||
Dictionary<String, String> sectiondata = new Dictionary<String, String>();
|
Dictionary<String, String> sectiondata = new Dictionary<String, String>();
|
||||||
foreach (String item in a[section].Keys) {
|
foreach (String item in a[section].Keys) {
|
||||||
sectiondata.Add(item, a[section][item].ToString());
|
sectiondata.Add(item, a[section][item].ToString());
|
||||||
}
|
}
|
||||||
newconf.Add(section, sectiondata);
|
newconf.Add(section, sectiondata);
|
||||||
}
|
}
|
||||||
modul.SetConfig(newconf);
|
modul.SetConfig(newconf);
|
||||||
return new Tuple<Boolean, MqttEvent>(true, new MqttEvent("New Config", "Write"));
|
return new Tuple<Boolean, MqttEvent>(true, new MqttEvent("New Config", "Write"));
|
||||||
} catch (Exception) { }
|
} catch (Exception) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new Tuple<Boolean, MqttEvent>(false, null);
|
return new Tuple<Boolean, MqttEvent>(false, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region IDisposable Support
|
#region IDisposable Support
|
||||||
private Boolean disposedValue = false;
|
private Boolean disposedValue = false;
|
||||||
|
|
||||||
protected void Dispose(Boolean disposing) {
|
protected void Dispose(Boolean disposing) {
|
||||||
if (!this.disposedValue) {
|
if (!this.disposedValue) {
|
||||||
if (disposing) {
|
if (disposing) {
|
||||||
this.connectionWatcher.Abort();
|
this.connectionWatcher.Abort();
|
||||||
while (this.connectionWatcher.ThreadState == ThreadState.Running) { Thread.Sleep(10); }
|
while (this.connectionWatcher.ThreadState == ThreadState.Running) { Thread.Sleep(10); }
|
||||||
this.Disconnect();
|
this.Disconnect();
|
||||||
}
|
}
|
||||||
this.disposedValue = true;
|
this.disposedValue = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Dispose() {
|
public override void Dispose() {
|
||||||
Dispose(true);
|
Dispose(true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,88 +1,88 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots.Moduls {
|
namespace BlubbFish.Utils.IoT.Bots.Moduls {
|
||||||
public abstract class Overtaker<T> : AModul<T>, IDisposable {
|
public abstract class Overtaker<T> : AModul<T>, IDisposable {
|
||||||
protected readonly Dictionary<String, Dictionary<String, String>> events = new Dictionary<String, Dictionary<String, String>>();
|
protected readonly Dictionary<String, Dictionary<String, String>> events = new Dictionary<String, Dictionary<String, String>>();
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
public Overtaker(T lib, InIReader settings) : base(lib, settings) {
|
public Overtaker(T lib, InIReader settings) : base(lib, settings) {
|
||||||
this.ParseIni();
|
this.ParseIni();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Overtakerfunctions
|
#region Overtakerfunctions
|
||||||
protected void ParseIni() {
|
protected void ParseIni() {
|
||||||
this.RemoveLibraryUpdateHooks();
|
this.RemoveLibraryUpdateHooks();
|
||||||
foreach (KeyValuePair<String, Dictionary<String, String>> item in this.config) {
|
foreach (KeyValuePair<String, Dictionary<String, String>> item in this.config) {
|
||||||
if (item.Value.ContainsKey("from")) {
|
if (item.Value.ContainsKey("from")) {
|
||||||
String from = item.Value["from"];
|
String from = item.Value["from"];
|
||||||
String[] source = from.Split(':');
|
String[] source = from.Split(':');
|
||||||
this.events.Add(source[0], item.Value);
|
this.events.Add(source[0], item.Value);
|
||||||
this.AddLibraryUpdateHook(source[0]);
|
this.AddLibraryUpdateHook(source[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void SetValues(Object sender, String name, Dictionary<String, String> dictionary) {
|
protected void SetValues(Object sender, String name, Dictionary<String, String> dictionary) {
|
||||||
String from = dictionary["from"];
|
String from = dictionary["from"];
|
||||||
String[] source = from.Split(':');
|
String[] source = from.Split(':');
|
||||||
if (source.Length != 2) {
|
if (source.Length != 2) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String source_value;
|
String source_value;
|
||||||
if (sender.HasProperty(source[1])) {
|
if (sender.HasProperty(source[1])) {
|
||||||
source_value = sender.GetProperty(source[1]).ToString();
|
source_value = sender.GetProperty(source[1]).ToString();
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (dictionary.ContainsKey("convert")) {
|
if (dictionary.ContainsKey("convert")) {
|
||||||
foreach (String tuple in dictionary["convert"].Split(';')) {
|
foreach (String tuple in dictionary["convert"].Split(';')) {
|
||||||
String[] item = tuple.Split('-');
|
String[] item = tuple.Split('-');
|
||||||
if (source_value == item[0]) {
|
if (source_value == item[0]) {
|
||||||
source_value = item[1];
|
source_value = item[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (dictionary.ContainsKey("to")) {
|
if (dictionary.ContainsKey("to")) {
|
||||||
foreach (String to in dictionary["to"].Split(';')) {
|
foreach (String to in dictionary["to"].Split(';')) {
|
||||||
String[] target = to.Split(':');
|
String[] target = to.Split(':');
|
||||||
if (target.Length == 2) {
|
if (target.Length == 2) {
|
||||||
this.SetValueHook(target[0], target[1], source_value);
|
this.SetValueHook(target[0], target[1], source_value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void AddLibraryUpdateHook(String id);
|
protected abstract void AddLibraryUpdateHook(String id);
|
||||||
|
|
||||||
protected abstract void RemoveLibraryUpdateHooks();
|
protected abstract void RemoveLibraryUpdateHooks();
|
||||||
|
|
||||||
protected abstract void SetValueHook(String id, String prop, String value);
|
protected abstract void SetValueHook(String id, String prop, String value);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region AModul
|
#region AModul
|
||||||
public override void Interconnect(Dictionary<String, AModul<T>> moduls) { }
|
public override void Interconnect(Dictionary<String, AModul<T>> moduls) { }
|
||||||
protected override void UpdateConfig() {
|
protected override void UpdateConfig() {
|
||||||
this.ParseIni();
|
this.ParseIni();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region IDisposable Support
|
#region IDisposable Support
|
||||||
private Boolean disposedValue = false;
|
private Boolean disposedValue = false;
|
||||||
|
|
||||||
protected virtual void Dispose(Boolean disposing) {
|
protected virtual void Dispose(Boolean disposing) {
|
||||||
if (!this.disposedValue) {
|
if (!this.disposedValue) {
|
||||||
if (disposing) {
|
if (disposing) {
|
||||||
}
|
}
|
||||||
this.disposedValue = true;
|
this.disposedValue = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Dispose() {
|
public override void Dispose() {
|
||||||
Dispose(true);
|
Dispose(true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,53 +1,53 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using BlubbFish.Utils.IoT.Bots.Interfaces;
|
using BlubbFish.Utils.IoT.Bots.Interfaces;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots.Moduls {
|
namespace BlubbFish.Utils.IoT.Bots.Moduls {
|
||||||
public abstract class Statuspolling<T> : AModul<T>, IDisposable, IForceLoad {
|
public abstract class Statuspolling<T> : AModul<T>, IDisposable, IForceLoad {
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
public Statuspolling(T lib, InIReader settings) : base(lib, settings) { }
|
public Statuspolling(T lib, InIReader settings) : base(lib, settings) { }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Statuspollingfunctions
|
#region Statuspollingfunctions
|
||||||
protected abstract void PollSpecific(Object obj);
|
protected abstract void PollSpecific(Object obj);
|
||||||
|
|
||||||
protected abstract void PollAll(Object obj);
|
protected abstract void PollAll(Object obj);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region AModul
|
#region AModul
|
||||||
public override void Interconnect(Dictionary<String, AModul<T>> moduls) {
|
public override void Interconnect(Dictionary<String, AModul<T>> moduls) {
|
||||||
foreach (KeyValuePair<String, AModul<T>> item in moduls) {
|
foreach (KeyValuePair<String, AModul<T>> item in moduls) {
|
||||||
if (item.Value is CronJob<T>) {
|
if (item.Value is CronJob<T>) {
|
||||||
item.Value.SetInterconnection("0 0 * * *", new Action<Object>(this.PollAll), null);
|
item.Value.SetInterconnection("0 0 * * *", new Action<Object>(this.PollAll), null);
|
||||||
if (this.config.Count != 0) {
|
if (this.config.Count != 0) {
|
||||||
foreach (KeyValuePair<String, Dictionary<String, String>> section in this.config) {
|
foreach (KeyValuePair<String, Dictionary<String, String>> section in this.config) {
|
||||||
if (section.Value.ContainsKey("cron") && section.Value.ContainsKey("devices")) {
|
if (section.Value.ContainsKey("cron") && section.Value.ContainsKey("devices")) {
|
||||||
item.Value.SetInterconnection(section.Value["cron"], new Action<Object>(this.PollSpecific), section.Value["devices"]);
|
item.Value.SetInterconnection(section.Value["cron"], new Action<Object>(this.PollSpecific), section.Value["devices"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected override void UpdateConfig() { }
|
protected override void UpdateConfig() { }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region IDisposable Support
|
#region IDisposable Support
|
||||||
private Boolean disposedValue = false;
|
private Boolean disposedValue = false;
|
||||||
|
|
||||||
protected virtual void Dispose(Boolean disposing) {
|
protected virtual void Dispose(Boolean disposing) {
|
||||||
if (!this.disposedValue) {
|
if (!this.disposedValue) {
|
||||||
if (disposing) {
|
if (disposing) {
|
||||||
}
|
}
|
||||||
this.disposedValue = true;
|
this.disposedValue = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Dispose() {
|
public override void Dispose() {
|
||||||
Dispose(true);
|
Dispose(true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,40 +1,40 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
// Allgemeine Informationen über eine Assembly werden über die folgenden
|
// Allgemeine Informationen über eine Assembly werden über die folgenden
|
||||||
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
|
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
|
||||||
// die einer Assembly zugeordnet sind.
|
// die einer Assembly zugeordnet sind.
|
||||||
[assembly: AssemblyTitle("Bot-Utils")]
|
[assembly: AssemblyTitle("Bot-Utils")]
|
||||||
[assembly: AssemblyDescription("")]
|
[assembly: AssemblyDescription("")]
|
||||||
[assembly: AssemblyConfiguration("")]
|
[assembly: AssemblyConfiguration("")]
|
||||||
[assembly: AssemblyCompany("")]
|
[assembly: AssemblyCompany("")]
|
||||||
[assembly: AssemblyProduct("Bot-Utils")]
|
[assembly: AssemblyProduct("Bot-Utils")]
|
||||||
[assembly: AssemblyCopyright("Copyright © 2018 - 02.10.2018")]
|
[assembly: AssemblyCopyright("Copyright © 2018 - 02.10.2018")]
|
||||||
[assembly: AssemblyTrademark("")]
|
[assembly: AssemblyTrademark("")]
|
||||||
[assembly: AssemblyCulture("")]
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
|
// 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
|
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
|
||||||
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
|
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
|
||||||
[assembly: ComVisible(false)]
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
|
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
|
||||||
[assembly: Guid("bb7bfcb5-3db0-49e1-802a-3ce3eecc59f9")]
|
[assembly: Guid("bb7bfcb5-3db0-49e1-802a-3ce3eecc59f9")]
|
||||||
|
|
||||||
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
|
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
|
||||||
//
|
//
|
||||||
// Hauptversion
|
// Hauptversion
|
||||||
// Nebenversion
|
// Nebenversion
|
||||||
// Buildnummer
|
// Buildnummer
|
||||||
// Revision
|
// Revision
|
||||||
//
|
//
|
||||||
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
|
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
|
||||||
// indem Sie "*" wie unten gezeigt eingeben:
|
// indem Sie "*" wie unten gezeigt eingeben:
|
||||||
// [assembly: AssemblyVersion("1.0.*")]
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
[assembly: AssemblyVersion("1.1.0")]
|
[assembly: AssemblyVersion("1.1.0")]
|
||||||
[assembly: AssemblyFileVersion("1.1.0")]
|
[assembly: AssemblyFileVersion("1.1.0")]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 1.1.0 Remove Helper from Bot-Utils
|
* 1.1.0 Remove Helper from Bot-Utils
|
||||||
*/
|
*/
|
@ -1,98 +1,98 @@
|
|||||||
using BlubbFish.Utils.IoT.Connector;
|
using BlubbFish.Utils.IoT.Connector;
|
||||||
using BlubbFish.Utils.IoT.Events;
|
using BlubbFish.Utils.IoT.Events;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace BlubbFish.Utils.IoT.Bots
|
namespace BlubbFish.Utils.IoT.Bots
|
||||||
{
|
{
|
||||||
public abstract class Webserver
|
public abstract class Webserver
|
||||||
{
|
{
|
||||||
protected Dictionary<String, String> config;
|
protected Dictionary<String, String> config;
|
||||||
protected InIReader requests;
|
protected InIReader requests;
|
||||||
protected HttpListener httplistener;
|
protected HttpListener httplistener;
|
||||||
|
|
||||||
public Webserver(ABackend backend, Dictionary<String, String> settings, InIReader requests) {
|
public Webserver(ABackend backend, Dictionary<String, String> settings, InIReader requests) {
|
||||||
this.config = settings;
|
this.config = settings;
|
||||||
this.requests = requests;
|
this.requests = requests;
|
||||||
backend.MessageIncomming += this.Backend_MessageIncomming;
|
backend.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();
|
||||||
ThreadPool.QueueUserWorkItem((o) => {
|
ThreadPool.QueueUserWorkItem((o) => {
|
||||||
Console.WriteLine("Webserver is Running...");
|
Console.WriteLine("Webserver is Running...");
|
||||||
try {
|
try {
|
||||||
while (this.httplistener.IsListening) {
|
while (this.httplistener.IsListening) {
|
||||||
ThreadPool.QueueUserWorkItem((state) => {
|
ThreadPool.QueueUserWorkItem((state) => {
|
||||||
HttpListenerContext httplistenercontext = state as HttpListenerContext;
|
HttpListenerContext httplistenercontext = state as HttpListenerContext;
|
||||||
try {
|
try {
|
||||||
this.SendResponse(httplistenercontext);
|
this.SendResponse(httplistenercontext);
|
||||||
} catch { } finally {
|
} catch { } finally {
|
||||||
httplistenercontext.Response.OutputStream.Close();
|
httplistenercontext.Response.OutputStream.Close();
|
||||||
}
|
}
|
||||||
}, this.httplistener.GetContext());
|
}, this.httplistener.GetContext());
|
||||||
}
|
}
|
||||||
} catch { };
|
} catch { };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void SendResponse(HttpListenerContext cont) {
|
protected virtual void SendResponse(HttpListenerContext cont) {
|
||||||
String restr = cont.Request.Url.PathAndQuery;
|
String restr = cont.Request.Url.PathAndQuery;
|
||||||
if (restr.StartsWith("/")) {
|
if (restr.StartsWith("/")) {
|
||||||
if(restr.IndexOf("?") != -1) {
|
if(restr.IndexOf("?") != -1) {
|
||||||
restr = restr.Substring(1, restr.IndexOf("?")-1);
|
restr = restr.Substring(1, restr.IndexOf("?")-1);
|
||||||
} else {
|
} else {
|
||||||
restr = restr.Substring(1);
|
restr = restr.Substring(1);
|
||||||
}
|
}
|
||||||
if(restr == "") {
|
if(restr == "") {
|
||||||
restr = "index.html";
|
restr = "index.html";
|
||||||
}
|
}
|
||||||
String end = restr.IndexOf('.') != -1 ? restr.Substring(restr.IndexOf('.')+1) : "";
|
String end = restr.IndexOf('.') != -1 ? restr.Substring(restr.IndexOf('.')+1) : "";
|
||||||
if (File.Exists("resources/"+ restr)) {
|
if (File.Exists("resources/"+ restr)) {
|
||||||
try {
|
try {
|
||||||
if (end == "png" || end == ".jpg" || end == ".jpeg" || end == ".ico") {
|
if (end == "png" || end == ".jpg" || end == ".jpeg" || end == ".ico") {
|
||||||
Byte[] output = File.ReadAllBytes("resources/" + restr);
|
Byte[] output = File.ReadAllBytes("resources/" + restr);
|
||||||
cont.Response.OutputStream.Write(output, 0, output.Length);
|
cont.Response.OutputStream.Write(output, 0, output.Length);
|
||||||
cont.Response.ContentType = "image/"+end;
|
cont.Response.ContentType = "image/"+end;
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
String file = File.ReadAllText("resources/" + restr);
|
String file = File.ReadAllText("resources/" + restr);
|
||||||
if (this.requests.GetSections(false).Contains(restr)) {
|
if (this.requests.GetSections(false).Contains(restr)) {
|
||||||
Dictionary<String, String> vars = this.requests.GetSection(restr);
|
Dictionary<String, String> vars = this.requests.GetSection(restr);
|
||||||
foreach (KeyValuePair<String, String> item in vars) {
|
foreach (KeyValuePair<String, String> item in vars) {
|
||||||
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);
|
file = file.Replace("{%REQUEST_URL_HOST%}", cont.Request.Url.Host);
|
||||||
Byte[] buf = Encoding.UTF8.GetBytes(file);
|
Byte[] buf = Encoding.UTF8.GetBytes(file);
|
||||||
cont.Response.ContentLength64 = buf.Length;
|
cont.Response.ContentLength64 = buf.Length;
|
||||||
cont.Response.OutputStream.Write(buf, 0, buf.Length);
|
cont.Response.OutputStream.Write(buf, 0, buf.Length);
|
||||||
Console.WriteLine("200 - " + cont.Request.Url.PathAndQuery);
|
Console.WriteLine("200 - " + cont.Request.Url.PathAndQuery);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
Helper.WriteError("500 - " + e.Message);
|
Helper.WriteError("500 - " + e.Message);
|
||||||
cont.Response.StatusCode = 500;
|
cont.Response.StatusCode = 500;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Helper.WriteError("404 - " + cont.Request.Url.PathAndQuery + " not found!");
|
Helper.WriteError("404 - " + cont.Request.Url.PathAndQuery + " not found!");
|
||||||
cont.Response.StatusCode = 404;
|
cont.Response.StatusCode = 404;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
this.httplistener.Stop();
|
this.httplistener.Stop();
|
||||||
this.httplistener.Close();
|
this.httplistener.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void Backend_MessageIncomming(Object sender, BackendEvent e);
|
protected abstract void Backend_MessageIncomming(Object sender, BackendEvent e);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user