[NF] AUserBackend hinzugefügt
This commit is contained in:
parent
57856a4d8f
commit
6369fe4b46
6
IoT.sln
6
IoT.sln
@ -5,8 +5,6 @@ VisualStudioVersion = 15.0.26730.16
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils-IoT", "IoT\Utils-IoT.csproj", "{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "Utils\Utils.csproj", "{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -17,10 +15,6 @@ Global
|
||||
{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
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -8,31 +8,19 @@ namespace BlubbFish.Utils.IoT.Connector {
|
||||
public abstract event MqttMessage MessageSending;
|
||||
public delegate void MqttMessage(Object sender, MqttEventArgs e);
|
||||
|
||||
public static ADataBackend GetInstance(Dictionary<String, String> dictionary) {
|
||||
String object_sensor = "BlubbFish.Utils.IoT.Connector." + Char.ToUpper(dictionary["type"][0]) + dictionary["type"].Substring(1).ToLower();
|
||||
public static ADataBackend GetInstance(Dictionary<String, String> settings) {
|
||||
String object_sensor = "BlubbFish.Utils.IoT.Connector.Data." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower();
|
||||
Type t = null;
|
||||
try {
|
||||
t = Type.GetType(object_sensor, true);
|
||||
} catch (TypeLoadException) {
|
||||
throw new ArgumentException("settings.ini: " + dictionary["type"] + " is not a Connector");
|
||||
throw new ArgumentException("settings.ini: " + settings["type"] + " is not a DataBackend");
|
||||
}
|
||||
return (ADataBackend)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>) }).Invoke(new Object[] { dictionary });
|
||||
return (ADataBackend)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>) }).Invoke(new Object[] { settings });
|
||||
}
|
||||
|
||||
public abstract void Send(String topic, String data);
|
||||
|
||||
public abstract void Dispose();
|
||||
}
|
||||
public class MqttEventArgs : EventArgs {
|
||||
public MqttEventArgs() : base() { }
|
||||
public MqttEventArgs(String message, String topic) {
|
||||
this.Topic = topic;
|
||||
this.Message = message;
|
||||
this.Date = DateTime.Now;
|
||||
}
|
||||
|
||||
public String Topic { get; private set; }
|
||||
public String Message { get; private set; }
|
||||
public DateTime Date { get; private set; }
|
||||
}
|
||||
}
|
||||
|
34
IoT/Connector/AUserBackend.cs
Normal file
34
IoT/Connector/AUserBackend.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Connector {
|
||||
public abstract class AUserBackend {
|
||||
protected Dictionary<String, String> settings;
|
||||
|
||||
public abstract event TelegramMessage MessageIncomming;
|
||||
public abstract event TelegramMessage MessageSending;
|
||||
public delegate void TelegramMessage(Object sender, UserMessageEventArgs e);
|
||||
|
||||
public AUserBackend(Dictionary<String, String> settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
public static AUserBackend GetInstance(Dictionary<String, String> settings) {
|
||||
String object_sensor = "BlubbFish.Utils.IoT.Connector.User." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower();
|
||||
Type t = null;
|
||||
try {
|
||||
t = Type.GetType(object_sensor, true);
|
||||
} catch (TypeLoadException) {
|
||||
throw new ArgumentException("settings.ini: " + settings["type"] + " is not a UserBackend");
|
||||
}
|
||||
return (AUserBackend)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>) }).Invoke(new Object[] { settings });
|
||||
}
|
||||
|
||||
public abstract void Send(String message);
|
||||
|
||||
public abstract void Dispose();
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Connector {
|
||||
namespace BlubbFish.Utils.IoT.Connector.Data {
|
||||
class Mosquitto : ADataBackend, IDisposable {
|
||||
private Process p;
|
||||
private String message;
|
@ -4,7 +4,7 @@ using System.Text;
|
||||
using uPLibrary.Networking.M2Mqtt;
|
||||
using uPLibrary.Networking.M2Mqtt.Messages;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Connector {
|
||||
namespace BlubbFish.Utils.IoT.Connector.Data {
|
||||
class Mqtt : ADataBackend, IDisposable {
|
||||
private MqttClient client;
|
||||
|
30
IoT/Connector/Helper.cs
Normal file
30
IoT/Connector/Helper.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Connector {
|
||||
public class UserMessageEventArgs : EventArgs {
|
||||
public UserMessageEventArgs() : base() { }
|
||||
public UserMessageEventArgs(String message, Int64 UserId, DateTime date) {
|
||||
this.UserId = UserId;
|
||||
this.Message = message;
|
||||
this.Date = date;
|
||||
}
|
||||
public Int64 UserId { get; private set; }
|
||||
public String Message { get; private set; }
|
||||
public DateTime Date { get; private set; }
|
||||
}
|
||||
public class MqttEventArgs : EventArgs {
|
||||
public MqttEventArgs() : base() { }
|
||||
public MqttEventArgs(String message, String topic) {
|
||||
this.Topic = topic;
|
||||
this.Message = message;
|
||||
this.Date = DateTime.Now;
|
||||
}
|
||||
public String Topic { get; private set; }
|
||||
public String Message { get; private set; }
|
||||
public DateTime Date { get; private set; }
|
||||
}
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
using System;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Args;
|
||||
using Telegram.Bot.Exceptions;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Connector {
|
||||
public class Telegram {
|
||||
private static Telegram instance;
|
||||
private TelegramBotClient bot;
|
||||
private ChatId chat;
|
||||
|
||||
public delegate void TelegramMessage(Object sender, TelegramEventArgs e);
|
||||
|
||||
public event TelegramMessage MessageIncomming;
|
||||
public event TelegramMessage MessageSending;
|
||||
|
||||
private Telegram() {
|
||||
this.bot = new TelegramBotClient(InIReader.GetInstance("settings.ini").GetValue("general", "telegram-key"));
|
||||
this.bot.OnMessage += this.Bot_OnMessage;
|
||||
this.Connect();
|
||||
}
|
||||
|
||||
private void Bot_OnMessage(Object sender, MessageEventArgs e) {
|
||||
this.MessageIncomming?.Invoke(this, new TelegramEventArgs(e.Message.Text, e.Message.Chat.Id, e.Message.Date));
|
||||
}
|
||||
|
||||
public static Telegram Instance {
|
||||
get {
|
||||
if(instance == null) {
|
||||
instance = new Telegram();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
private void Connect() {
|
||||
this.bot.StartReceiving();
|
||||
this.chat = new ChatId(InIReader.GetInstance("settings.ini").GetValue("general", "chatid"));
|
||||
}
|
||||
|
||||
public async void Send(String text) {
|
||||
try {
|
||||
Message x = await this.bot.SendTextMessageAsync(this.chat, text);
|
||||
this.MessageSending?.Invoke(this, new TelegramEventArgs(x.Text, x.Chat.Id, x.Date));
|
||||
} catch(ApiRequestException e) {
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(e.Message+" "+e.ErrorCode+" "+e.Parameters);
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
}
|
||||
}
|
||||
}
|
||||
public class TelegramEventArgs : EventArgs {
|
||||
public TelegramEventArgs() : base() { }
|
||||
public TelegramEventArgs(String message, Int64 UserId, DateTime date) {
|
||||
this.UserId = UserId;
|
||||
this.Message = message;
|
||||
this.Date = date;
|
||||
}
|
||||
|
||||
public Int64 UserId { get; private set; }
|
||||
public String Message { get; private set; }
|
||||
public DateTime Date { get; private set; }
|
||||
}
|
||||
}
|
47
IoT/Connector/User/Telegram.cs
Normal file
47
IoT/Connector/User/Telegram.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Args;
|
||||
using Telegram.Bot.Exceptions;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Connector.User {
|
||||
public class Telegram : AUserBackend {
|
||||
private TelegramBotClient bot;
|
||||
private ChatId chat;
|
||||
|
||||
public override event TelegramMessage MessageIncomming;
|
||||
public override event TelegramMessage MessageSending;
|
||||
|
||||
public Telegram(Dictionary<String, String> settings) : base(settings) {
|
||||
this.bot = new TelegramBotClient(settings["telegram-key"]);
|
||||
this.bot.OnMessage += this.Bot_OnMessage;
|
||||
this.Connect();
|
||||
}
|
||||
|
||||
private void Bot_OnMessage(Object sender, MessageEventArgs e) {
|
||||
this.MessageIncomming?.Invoke(this, new UserMessageEventArgs(e.Message.Text, e.Message.Chat.Id, e.Message.Date));
|
||||
}
|
||||
|
||||
private void Connect() {
|
||||
this.bot.StartReceiving();
|
||||
this.chat = new ChatId(this.settings["chatid"]);
|
||||
}
|
||||
|
||||
public async override void Send(String message) {
|
||||
try {
|
||||
Message x = await this.bot.SendTextMessageAsync(this.chat, message);
|
||||
this.MessageSending?.Invoke(this, new UserMessageEventArgs(x.Text, x.Chat.Id, x.Date));
|
||||
} catch(ApiRequestException e) {
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(e.Message+" "+e.ErrorCode+" "+e.Parameters);
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Dispose() {
|
||||
this.bot.StopReceiving();
|
||||
this.bot = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -109,9 +109,11 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Connector\ADataBackend.cs" />
|
||||
<Compile Include="Connector\Mosquitto.cs" />
|
||||
<Compile Include="Connector\Mqtt.cs" />
|
||||
<Compile Include="Connector\Telegram.cs" />
|
||||
<Compile Include="Connector\AUserBackend.cs" />
|
||||
<Compile Include="Connector\Helper.cs" />
|
||||
<Compile Include="Connector\Data\Mosquitto.cs" />
|
||||
<Compile Include="Connector\Data\Mqtt.cs" />
|
||||
<Compile Include="Connector\User\Telegram.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Sensor\ASensor.cs" />
|
||||
<Compile Include="Sensor\Flex4GridPower.cs" />
|
||||
@ -126,11 +128,5 @@
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Utils\Utils.csproj">
|
||||
<Project>{fac8ce64-bf13-4ece-8097-aeb5dd060098}</Project>
|
||||
<Name>Utils</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user