diff --git a/IoT.sln b/IoT.sln index 234c4e4..d3796fa 100644 --- a/IoT.sln +++ b/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 diff --git a/IoT/Connector/ADataBackend.cs b/IoT/Connector/ADataBackend.cs index 44f6996..7a840fd 100644 --- a/IoT/Connector/ADataBackend.cs +++ b/IoT/Connector/ADataBackend.cs @@ -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 dictionary) { - String object_sensor = "BlubbFish.Utils.IoT.Connector." + Char.ToUpper(dictionary["type"][0]) + dictionary["type"].Substring(1).ToLower(); + public static ADataBackend GetInstance(Dictionary 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) }).Invoke(new Object[] { dictionary }); + return (ADataBackend)t.GetConstructor(new Type[] { typeof(Dictionary) }).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; } - } } diff --git a/IoT/Connector/AUserBackend.cs b/IoT/Connector/AUserBackend.cs new file mode 100644 index 0000000..a90a384 --- /dev/null +++ b/IoT/Connector/AUserBackend.cs @@ -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 settings; + + public abstract event TelegramMessage MessageIncomming; + public abstract event TelegramMessage MessageSending; + public delegate void TelegramMessage(Object sender, UserMessageEventArgs e); + + public AUserBackend(Dictionary settings) { + this.settings = settings; + } + + public static AUserBackend GetInstance(Dictionary 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) }).Invoke(new Object[] { settings }); + } + + public abstract void Send(String message); + + public abstract void Dispose(); + } +} diff --git a/IoT/Connector/Mosquitto.cs b/IoT/Connector/Data/Mosquitto.cs similarity index 96% rename from IoT/Connector/Mosquitto.cs rename to IoT/Connector/Data/Mosquitto.cs index 6f856c5..2e13847 100644 --- a/IoT/Connector/Mosquitto.cs +++ b/IoT/Connector/Data/Mosquitto.cs @@ -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; diff --git a/IoT/Connector/Mqtt.cs b/IoT/Connector/Data/Mqtt.cs similarity index 94% rename from IoT/Connector/Mqtt.cs rename to IoT/Connector/Data/Mqtt.cs index c031382..309a0b9 100644 --- a/IoT/Connector/Mqtt.cs +++ b/IoT/Connector/Data/Mqtt.cs @@ -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; diff --git a/IoT/Connector/Helper.cs b/IoT/Connector/Helper.cs new file mode 100644 index 0000000..41e3b27 --- /dev/null +++ b/IoT/Connector/Helper.cs @@ -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; } + } +} diff --git a/IoT/Connector/Telegram.cs b/IoT/Connector/Telegram.cs deleted file mode 100644 index 98a0531..0000000 --- a/IoT/Connector/Telegram.cs +++ /dev/null @@ -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; } - } -} diff --git a/IoT/Connector/User/Telegram.cs b/IoT/Connector/User/Telegram.cs new file mode 100644 index 0000000..251ca50 --- /dev/null +++ b/IoT/Connector/User/Telegram.cs @@ -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 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; + } + } +} diff --git a/IoT/Utils-IoT.csproj b/IoT/Utils-IoT.csproj index 2e1426c..6b26fdf 100644 --- a/IoT/Utils-IoT.csproj +++ b/IoT/Utils-IoT.csproj @@ -109,9 +109,11 @@ - - - + + + + + @@ -126,11 +128,5 @@ - - - {fac8ce64-bf13-4ece-8097-aeb5dd060098} - Utils - - \ No newline at end of file diff --git a/IoT/bin/Release/Utils-IoT.dll b/IoT/bin/Release/Utils-IoT.dll index e0255a5..3552837 100644 Binary files a/IoT/bin/Release/Utils-IoT.dll and b/IoT/bin/Release/Utils-IoT.dll differ