[NF] Aufgeräumt
This commit is contained in:
parent
4545c506fc
commit
00134d226c
@ -10,24 +10,20 @@ namespace IoTBot.Condition {
|
|||||||
protected ADataBackend data;
|
protected ADataBackend data;
|
||||||
protected AUserBackend user;
|
protected AUserBackend user;
|
||||||
|
|
||||||
public ACondition(Dictionary<String, String> settings, ADataBackend data, AUserBackend user) {
|
protected ACondition(String name, Dictionary<String, String> settings, ASensor sensor, ADataBackend data, AUserBackend user) {
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.user = user;
|
this.user = user;
|
||||||
Dictionary<String, String> l = new Dictionary<String, String> {
|
this.sensor = sensor;
|
||||||
{ "topic", this.settings["sensor_topic"] },
|
|
||||||
{ "type", this.settings["sensor"] }
|
|
||||||
};
|
|
||||||
if(settings.ContainsKey("sensor_polling")) {
|
|
||||||
l.Add("polling", this.settings["sensor_polling"]);
|
|
||||||
}
|
}
|
||||||
this.sensor = ASensor.GetInstance(data, l, "testSensor");
|
|
||||||
|
public void Attach() {
|
||||||
this.sensor.Update += this.Sensor_Update;
|
this.sensor.Update += this.Sensor_Update;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void Sensor_Update(Object sender, EventArgs e);
|
protected abstract void Sensor_Update(Object sender, EventArgs e);
|
||||||
|
|
||||||
public static ACondition GetInstance(Dictionary<String, String> settings, ADataBackend data, AUserBackend user) {
|
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();
|
String object_condition = "IoTBot.Condition." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower();
|
||||||
Type t = null;
|
Type t = null;
|
||||||
try {
|
try {
|
||||||
@ -35,7 +31,7 @@ namespace IoTBot.Condition {
|
|||||||
} catch(TypeLoadException) {
|
} catch(TypeLoadException) {
|
||||||
throw new ArgumentException("condition.ini: " + settings["type"] + " is not a Sensor");
|
throw new ArgumentException("condition.ini: " + settings["type"] + " is not a Sensor");
|
||||||
}
|
}
|
||||||
return (ACondition)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>), typeof(ADataBackend), typeof(AUserBackend) }).Invoke(new Object[] { settings, data, user });
|
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 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,7 +7,7 @@ namespace IoTBot.Condition {
|
|||||||
class Edge : ACondition {
|
class Edge : ACondition {
|
||||||
private Boolean histBool;
|
private Boolean histBool;
|
||||||
|
|
||||||
public Edge(Dictionary<String, String> settings, ADataBackend data, AUserBackend user) : base(settings, data, user) { }
|
public Edge(String name, Dictionary<String, String> settings, ASensor sensor, ADataBackend data, AUserBackend user) : base(name, settings, sensor, data, user) { }
|
||||||
|
|
||||||
protected override void Sensor_Update(Object sender, EventArgs e) {
|
protected override void Sensor_Update(Object sender, EventArgs e) {
|
||||||
if(this.sensor.Datatypes == ASensor.Types.Bool) {
|
if(this.sensor.Datatypes == ASensor.Types.Bool) {
|
||||||
|
@ -2,30 +2,34 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using BlubbFish.Utils;
|
using BlubbFish.Utils;
|
||||||
using BlubbFish.Utils.IoT.Connector;
|
using BlubbFish.Utils.IoT.Connector;
|
||||||
|
using BlubbFish.Utils.IoT.Sensor;
|
||||||
using IoTBot.Condition;
|
using IoTBot.Condition;
|
||||||
|
|
||||||
namespace IoTBot {
|
namespace IoTBot {
|
||||||
class ConditionWorker {
|
class ConditionWorker {
|
||||||
private InIReader ini;
|
private List<ACondition> conditions = new List<ACondition>();
|
||||||
private static ConditionWorker instance;
|
private readonly ADataBackend data;
|
||||||
private List<ACondition> conditions;
|
private readonly AUserBackend user;
|
||||||
|
|
||||||
public static ConditionWorker GetInstance(ADataBackend data, AUserBackend user) {
|
public ConditionWorker(ADataBackend data, AUserBackend user) {
|
||||||
if (instance == null) {
|
this.data = data;
|
||||||
instance = new ConditionWorker(data, user);
|
this.user = user;
|
||||||
}
|
|
||||||
return instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Run() {
|
public void SetCondition(String name, Dictionary<String, String> settings) {
|
||||||
|
Dictionary<String, String> sensor_settings = new Dictionary<String, String>();
|
||||||
|
foreach (KeyValuePair<String, String> item in settings) {
|
||||||
|
if(item.Key.StartsWith("sensor_")) {
|
||||||
|
sensor_settings.Add(item.Key.Substring(7), item.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ASensor sensor = ASensor.GetInstance(this.data, sensor_settings, name);
|
||||||
|
this.conditions.Add(ACondition.GetInstance(name, settings, sensor, this.data, this.user));
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConditionWorker(ADataBackend data, AUserBackend user) {
|
public void Run() {
|
||||||
this.ini = InIReader.GetInstance("condition.ini");
|
foreach (ACondition item in this.conditions) {
|
||||||
this.conditions = new List<ACondition>();
|
item.Attach();
|
||||||
List<String> sections = this.ini.GetSections();
|
|
||||||
foreach(String section in sections) {
|
|
||||||
this.conditions.Add(ACondition.GetInstance(this.ini.GetSection(section), data, user));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,12 +7,20 @@ namespace IoTBot {
|
|||||||
static void Main(String[] args) {
|
static void Main(String[] args) {
|
||||||
ADataBackend mqtt = ADataBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("mqtt"));
|
ADataBackend mqtt = ADataBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("mqtt"));
|
||||||
AUserBackend telegram = AUserBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("user"));
|
AUserBackend telegram = AUserBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("user"));
|
||||||
ConditionWorker.GetInstance(mqtt, telegram).Run();
|
InIReader condition_settings = InIReader.GetInstance("condition.ini");
|
||||||
|
|
||||||
|
ConditionWorker worker = new ConditionWorker(mqtt, telegram);
|
||||||
|
foreach (String section in condition_settings.GetSections()) {
|
||||||
|
worker.SetCondition(section, condition_settings.GetSection(section));
|
||||||
|
}
|
||||||
|
|
||||||
mqtt.MessageIncomming += Mqtt_MessageIncomming;
|
mqtt.MessageIncomming += Mqtt_MessageIncomming;
|
||||||
mqtt.MessageSending += Mqtt_MessageSending;
|
mqtt.MessageSending += Mqtt_MessageSending;
|
||||||
telegram.MessageIncomming += Telegram_MessageIncomming;
|
telegram.MessageIncomming += Telegram_MessageIncomming;
|
||||||
telegram.MessageSending += Telegram_MessageSending;
|
telegram.MessageSending += Telegram_MessageSending;
|
||||||
|
|
||||||
|
worker.Run();
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
System.Threading.Thread.Sleep(100);
|
System.Threading.Thread.Sleep(100);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user