[NF] AUserBackend hinzugefügt
This commit is contained in:
parent
086bc89c2d
commit
4545c506fc
@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
[general]
|
||||
[user]
|
||||
type=telegram
|
||||
telegram-key=ABCDEFGH
|
||||
chatid=1234
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user