ProjectsOld/IoT-Bot/IoT-Bot/Condition/ACondition.cs
2017-12-18 23:30:48 +01:00

85 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Sensor;
namespace IoTBot.Condition {
abstract class ACondition {
protected ASensor sensor;
protected Dictionary<String, String> settings;
protected String name;
protected ADataBackend data;
protected AUserBackend user;
protected ACondition(String name, Dictionary<String, String> settings, ASensor sensor, ADataBackend data, AUserBackend user) {
this.settings = settings;
this.name = name;
this.data = data;
this.user = user;
this.sensor = sensor;
}
public void Attach() {
if(this.sensor != null) {
this.sensor.Update += this.Sensor_Update;
}
switch (this.settings["source"].ToLower()) {
case "user":
if(this.user != null) {
this.user.MessageIncomming += this.User_Update;
}
break;
case "data":
if(this.data != null) {
this.data.MessageIncomming += this.Data_Update;
}
break;
case "both":
if (this.user != null) {
this.user.MessageIncomming += this.User_Update;
}
if (this.data != null) {
this.data.MessageIncomming += this.Data_Update;
}
break;
}
}
protected void Send(String message = "", String topic = "") {
if(message == "") {
message = this.settings["target_message"];
}
if(topic == "") {
topic = this.settings["target_topic"];
}
switch (this.settings["target"].ToLower()) {
case "user":
this.user.Send(message);
break;
case "data":
this.data.Send(topic, message);
break;
case "sensor":
//this.sensor.Set(message);
break;
}
}
protected abstract void Data_Update(Object sender, MqttEventArgs e);
protected abstract void User_Update(Object sender, UserMessageEventArgs e);
protected abstract void Sensor_Update(Object sender, EventArgs e);
public static ACondition GetInstance(String name, Dictionary<String, String> settings, ASensor sensor, ADataBackend data, AUserBackend user) {
String object_condition = "IoTBot.Condition." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower();
Type t = null;
try {
t = Type.GetType(object_condition, true);
} catch(TypeLoadException) {
throw new ArgumentException("condition.ini: " + settings["type"] + " is not a Sensor");
}
return (ACondition)t.GetConstructor(new Type[] { typeof(String), typeof(Dictionary<String, String>), typeof(ASensor), typeof(ADataBackend), typeof(AUserBackend) }).Invoke(new Object[] { name, settings, sensor, data, user });
}
}
}