[Nf] Aufgeräumt
This commit is contained in:
parent
e533a5232a
commit
11019488d7
@ -10,24 +10,20 @@ namespace IoTBot.Condition {
|
||||
protected ADataBackend data;
|
||||
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.data = data;
|
||||
this.user = user;
|
||||
Dictionary<String, String> l = new Dictionary<String, String> {
|
||||
{ "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");
|
||||
this.sensor = sensor;
|
||||
}
|
||||
|
||||
public void Attach() {
|
||||
this.sensor.Update += this.Sensor_Update;
|
||||
}
|
||||
|
||||
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();
|
||||
Type t = null;
|
||||
try {
|
||||
@ -35,7 +31,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), 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 {
|
||||
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) {
|
||||
if(this.sensor.Datatypes == ASensor.Types.Bool) {
|
||||
|
@ -2,30 +2,34 @@
|
||||
using System.Collections.Generic;
|
||||
using BlubbFish.Utils;
|
||||
using BlubbFish.Utils.IoT.Connector;
|
||||
using BlubbFish.Utils.IoT.Sensor;
|
||||
using IoTBot.Condition;
|
||||
|
||||
namespace IoTBot {
|
||||
class ConditionWorker {
|
||||
private InIReader ini;
|
||||
private static ConditionWorker instance;
|
||||
private List<ACondition> conditions;
|
||||
private List<ACondition> conditions = new List<ACondition>();
|
||||
private readonly ADataBackend data;
|
||||
private readonly AUserBackend user;
|
||||
|
||||
public static ConditionWorker GetInstance(ADataBackend data, AUserBackend user) {
|
||||
if (instance == null) {
|
||||
instance = new ConditionWorker(data, user);
|
||||
public ConditionWorker(ADataBackend data, AUserBackend user) {
|
||||
this.data = data;
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
ASensor sensor = ASensor.GetInstance(this.data, sensor_settings, name);
|
||||
this.conditions.Add(ACondition.GetInstance(name, settings, sensor, this.data, this.user));
|
||||
}
|
||||
|
||||
internal void Run() {
|
||||
}
|
||||
|
||||
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), data, user));
|
||||
public void Run() {
|
||||
foreach (ACondition item in this.conditions) {
|
||||
item.Attach();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,12 +7,20 @@ namespace IoTBot {
|
||||
static void Main(String[] args) {
|
||||
ADataBackend mqtt = ADataBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("mqtt"));
|
||||
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.MessageSending += Mqtt_MessageSending;
|
||||
telegram.MessageIncomming += Telegram_MessageIncomming;
|
||||
telegram.MessageSending += Telegram_MessageSending;
|
||||
|
||||
worker.Run();
|
||||
|
||||
while(true) {
|
||||
System.Threading.Thread.Sleep(100);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user