diff --git a/IoT-Bot/IoT-Bot/Condition/ACondition.cs b/IoT-Bot/IoT-Bot/Condition/ACondition.cs index d6aae87..6a737b8 100644 --- a/IoT-Bot/IoT-Bot/Condition/ACondition.cs +++ b/IoT-Bot/IoT-Bot/Condition/ACondition.cs @@ -7,9 +7,13 @@ namespace IoTBot.Condition { abstract class ACondition { protected ASensor sensor; protected Dictionary settings; + protected ADataBackend data; + protected AUserBackend user; - public ACondition(Dictionary settings, ADataBackend backend) { + public ACondition(Dictionary settings, ADataBackend data, AUserBackend user) { this.settings = settings; + this.data = data; + this.user = user; Dictionary l = new Dictionary { { "topic", this.settings["sensor_topic"] }, { "type", this.settings["sensor"] } @@ -17,13 +21,13 @@ namespace IoTBot.Condition { if(settings.ContainsKey("sensor_polling")) { l.Add("polling", this.settings["sensor_polling"]); } - this.sensor = ASensor.GetInstance(backend, l, "testSensor"); + this.sensor = ASensor.GetInstance(data, l, "testSensor"); this.sensor.Update += this.Sensor_Update; } protected abstract void Sensor_Update(Object sender, EventArgs e); - public static ACondition GetInstance(Dictionary settings, ADataBackend backend) { + public static ACondition GetInstance(Dictionary settings, ADataBackend data, AUserBackend user) { String object_condition = "IoTBot.Condition." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower(); Type t = null; try { @@ -31,7 +35,7 @@ namespace IoTBot.Condition { } catch(TypeLoadException) { throw new ArgumentException("condition.ini: " + settings["type"] + " is not a Sensor"); } - return (ACondition)t.GetConstructor(new Type[] { typeof(Dictionary), typeof(ADataBackend) }).Invoke(new Object[] { settings, backend }); + return (ACondition)t.GetConstructor(new Type[] { typeof(Dictionary), typeof(ADataBackend), typeof(AUserBackend) }).Invoke(new Object[] { settings, data, user }); } } } \ No newline at end of file diff --git a/IoT-Bot/IoT-Bot/Condition/Edge.cs b/IoT-Bot/IoT-Bot/Condition/Edge.cs index 32114cd..d7683af 100644 --- a/IoT-Bot/IoT-Bot/Condition/Edge.cs +++ b/IoT-Bot/IoT-Bot/Condition/Edge.cs @@ -7,15 +7,13 @@ namespace IoTBot.Condition { class Edge : ACondition { private Boolean histBool; - public Edge(Dictionary settings, ADataBackend backend) : base(settings, backend) { - - } + public Edge(Dictionary settings, ADataBackend data, AUserBackend user) : base(settings, data, user) { } protected override void Sensor_Update(Object sender, EventArgs e) { if(this.sensor.Datatypes == ASensor.Types.Bool) { if(this.sensor.GetBool == Boolean.Parse(this.settings["sensor_value"]) && this.histBool != this.sensor.GetBool) { this.histBool = this.sensor.GetBool; - Telegram.Instance.Send("Jemand ist DA!"); + this.user.Send("Jemand ist DA!"); } else { this.histBool = this.sensor.GetBool; } diff --git a/IoT-Bot/IoT-Bot/ConditionWorker.cs b/IoT-Bot/IoT-Bot/ConditionWorker.cs index fc53037..6eac744 100644 --- a/IoT-Bot/IoT-Bot/ConditionWorker.cs +++ b/IoT-Bot/IoT-Bot/ConditionWorker.cs @@ -9,25 +9,23 @@ namespace IoTBot { private InIReader ini; private static ConditionWorker instance; private List conditions; - private Telegram telegram; - public static ConditionWorker GetInstance(ADataBackend backend) { + public static ConditionWorker GetInstance(ADataBackend data, AUserBackend user) { if (instance == null) { - instance = new ConditionWorker(backend); + instance = new ConditionWorker(data, user); } return instance; } - internal void Run(Telegram telegram) { - this.telegram = telegram; + internal void Run() { } - private ConditionWorker(ADataBackend backend) { + private ConditionWorker(ADataBackend data, AUserBackend user) { this.ini = InIReader.GetInstance("condition.ini"); this.conditions = new List(); List sections = this.ini.GetSections(); foreach(String section in sections) { - this.conditions.Add(ACondition.GetInstance(this.ini.GetSection(section), backend)); + this.conditions.Add(ACondition.GetInstance(this.ini.GetSection(section), data, user)); } } } diff --git a/IoT-Bot/IoT-Bot/Program.cs b/IoT-Bot/IoT-Bot/Program.cs index e726dbb..d52d84b 100644 --- a/IoT-Bot/IoT-Bot/Program.cs +++ b/IoT-Bot/IoT-Bot/Program.cs @@ -6,11 +6,12 @@ namespace IoTBot { class Program { static void Main(String[] args) { ADataBackend mqtt = ADataBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("mqtt")); - ConditionWorker.GetInstance(mqtt).Run(Telegram.Instance); + AUserBackend telegram = AUserBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("user")); + ConditionWorker.GetInstance(mqtt, telegram).Run(); mqtt.MessageIncomming += Mqtt_MessageIncomming; mqtt.MessageSending += Mqtt_MessageSending; - Telegram.Instance.MessageIncomming += Telegram_MessageIncomming; - Telegram.Instance.MessageSending += Telegram_MessageSending; + telegram.MessageIncomming += Telegram_MessageIncomming; + telegram.MessageSending += Telegram_MessageSending; while(true) { System.Threading.Thread.Sleep(100); @@ -21,11 +22,11 @@ namespace IoTBot { Console.WriteLine("-> [" + DateTime.Now.ToUniversalTime() + "] MQTT: " + e.Message + " on " + e.Topic); } - private static void Telegram_MessageSending(Object sender, TelegramEventArgs e) { + private static void Telegram_MessageSending(Object sender, UserMessageEventArgs e) { Console.WriteLine("-> [" + e.Date.ToUniversalTime() + "] Telegram: " + e.Message + " on " + e.UserId); } - private static void Telegram_MessageIncomming(Object sender, TelegramEventArgs e) { + private static void Telegram_MessageIncomming(Object sender, UserMessageEventArgs e) { Console.WriteLine("<- [" + e.Date.ToUniversalTime() + "] Telegram: " + e.Message + " on " + e.UserId); } diff --git a/IoT-Bot/IoT-Bot/settings.ini.example b/IoT-Bot/IoT-Bot/settings.ini.example index c5a9de1..35cc5a2 100644 --- a/IoT-Bot/IoT-Bot/settings.ini.example +++ b/IoT-Bot/IoT-Bot/settings.ini.example @@ -1,4 +1,5 @@ -[general] +[user] +type=telegram telegram-key=ABCDEFGH chatid=1234 diff --git a/Mqtt-Dashboard/Mqtt-Dashboard/bin/Release/Dashboard.exe b/Mqtt-Dashboard/Mqtt-Dashboard/bin/Release/Dashboard.exe index 0bd647c..404f6d5 100644 Binary files a/Mqtt-Dashboard/Mqtt-Dashboard/bin/Release/Dashboard.exe and b/Mqtt-Dashboard/Mqtt-Dashboard/bin/Release/Dashboard.exe differ diff --git a/Mqtt-Dashboard/Mqtt-Dashboard/bin/Release/Utils-IoT.dll b/Mqtt-Dashboard/Mqtt-Dashboard/bin/Release/Utils-IoT.dll index e0255a5..3552837 100644 Binary files a/Mqtt-Dashboard/Mqtt-Dashboard/bin/Release/Utils-IoT.dll and b/Mqtt-Dashboard/Mqtt-Dashboard/bin/Release/Utils-IoT.dll differ diff --git a/Utils/IoT.sln b/Utils/IoT.sln index 55ba0d2..543f324 100644 --- a/Utils/IoT.sln +++ b/Utils/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/Utils/IoT/Connector/ADataBackend.cs b/Utils/IoT/Connector/ADataBackend.cs index 20bd68a..7889dd2 100644 --- a/Utils/IoT/Connector/ADataBackend.cs +++ b/Utils/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/Utils/IoT/Connector/AUserBackend.cs b/Utils/IoT/Connector/AUserBackend.cs new file mode 100644 index 0000000..18efaf0 --- /dev/null +++ b/Utils/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/Utils/IoT/Connector/Mosquitto.cs b/Utils/IoT/Connector/Data/Mosquitto.cs similarity index 98% rename from Utils/IoT/Connector/Mosquitto.cs rename to Utils/IoT/Connector/Data/Mosquitto.cs index a740999..faf674f 100644 --- a/Utils/IoT/Connector/Mosquitto.cs +++ b/Utils/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/Utils/IoT/Connector/Mqtt.cs b/Utils/IoT/Connector/Data/Mqtt.cs similarity index 97% rename from Utils/IoT/Connector/Mqtt.cs rename to Utils/IoT/Connector/Data/Mqtt.cs index 8eaca93..b9a75be 100644 --- a/Utils/IoT/Connector/Mqtt.cs +++ b/Utils/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/Utils/IoT/Connector/Helper.cs b/Utils/IoT/Connector/Helper.cs new file mode 100644 index 0000000..243bebe --- /dev/null +++ b/Utils/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/Utils/IoT/Connector/Telegram.cs b/Utils/IoT/Connector/Telegram.cs deleted file mode 100644 index 7df80fb..0000000 --- a/Utils/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/Utils/IoT/Connector/User/Telegram.cs b/Utils/IoT/Connector/User/Telegram.cs new file mode 100644 index 0000000..d8b92b8 --- /dev/null +++ b/Utils/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/Utils/IoT/Utils-IoT.csproj b/Utils/IoT/Utils-IoT.csproj index ca5ca5e..4630305 100644 --- a/Utils/IoT/Utils-IoT.csproj +++ b/Utils/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/Utils/IoT/bin/Release/Utils-IoT.dll b/Utils/IoT/bin/Release/Utils-IoT.dll index e0255a5..3552837 100644 Binary files a/Utils/IoT/bin/Release/Utils-IoT.dll and b/Utils/IoT/bin/Release/Utils-IoT.dll differ