[NF] AUserBackend hinzugefügt

This commit is contained in:
BlubbFish 2017-10-03 00:55:04 +02:00
parent b1efd55025
commit e533a5232a
17 changed files with 145 additions and 119 deletions

View File

@ -7,9 +7,13 @@ namespace IoTBot.Condition {
abstract class ACondition {
protected ASensor sensor;
protected Dictionary<String, String> settings;
protected ADataBackend data;
protected AUserBackend user;
public ACondition(Dictionary<String, String> settings, ADataBackend backend) {
public ACondition(Dictionary<String, String> settings, ADataBackend data, AUserBackend user) {
this.settings = settings;
this.data = data;
this.user = user;
Dictionary<String, String> l = new Dictionary<String, String> {
{ "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<String, String> settings, ADataBackend backend) {
public static ACondition GetInstance(Dictionary<String, String> 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<String, String>), typeof(ADataBackend) }).Invoke(new Object[] { settings, backend });
return (ACondition)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>), typeof(ADataBackend), typeof(AUserBackend) }).Invoke(new Object[] { settings, data, user });
}
}
}

View File

@ -7,15 +7,13 @@ namespace IoTBot.Condition {
class Edge : ACondition {
private Boolean histBool;
public Edge(Dictionary<String, String> settings, ADataBackend backend) : base(settings, backend) {
}
public Edge(Dictionary<String, String> 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;
}

View File

@ -9,25 +9,23 @@ namespace IoTBot {
private InIReader ini;
private static ConditionWorker instance;
private List<ACondition> 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<ACondition>();
List<String> 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));
}
}
}

View File

@ -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);
}

View File

@ -1,4 +1,5 @@
[general]
[user]
type=telegram
telegram-key=ABCDEFGH
chatid=1234

View File

@ -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

View File

@ -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; }
}
}

View 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();
}
}

View File

@ -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;

View File

@ -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;

View 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; }
}
}

View File

@ -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; }
}
}

View 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;
}
}
}

View File

@ -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.