[NF] Telegram Condition
[NF] Zway Libarary
This commit is contained in:
parent
ed37a2a14e
commit
b65b2e50df
@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils-IoT", "..\Utils\IoT\U
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "..\Utils\Utils\Utils.csproj", "{FAC8CE64-BF13-4ECE-8097-AEB5DD060098}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zway", "..\Zway\Zway\Zway.csproj", "{166258ED-CB3D-43F5-8E8D-3A993B64D022}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -27,6 +29,10 @@ Global
|
||||
{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
|
||||
{166258ED-CB3D-43F5-8E8D-3A993B64D022}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{166258ED-CB3D-43F5-8E8D-3A993B64D022}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{166258ED-CB3D-43F5-8E8D-3A993B64D022}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{166258ED-CB3D-43F5-8E8D-3A993B64D022}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -7,20 +7,68 @@ 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() {
|
||||
this.sensor.Update += this.Sensor_Update;
|
||||
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) {
|
||||
|
@ -9,6 +9,10 @@ namespace IoTBot.Condition {
|
||||
|
||||
public Edge(String name, Dictionary<String, String> settings, ASensor sensor, ADataBackend data, AUserBackend user) : base(name, settings, sensor, data, user) { }
|
||||
|
||||
protected override void Data_Update(Object sender, MqttEventArgs e) {
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -19,5 +23,9 @@ namespace IoTBot.Condition {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void User_Update(Object sender, UserMessageEventArgs e) {
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
44
IoT-Bot/Condition/TelegramBot.cs
Normal file
44
IoT-Bot/Condition/TelegramBot.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using BlubbFish.IoT.Zway;
|
||||
using BlubbFish.Utils.IoT.Connector;
|
||||
using BlubbFish.Utils.IoT.Sensor;
|
||||
|
||||
namespace IoTBot.Condition {
|
||||
class Telegrambot : ACondition {
|
||||
private ZwayController zw;
|
||||
|
||||
public Telegrambot(String name, Dictionary<String, String> settings, ASensor sensor, ADataBackend data, AUserBackend user) : base(name, settings, sensor, data, user) {
|
||||
this.zw = new ZwayController("10.100.0.214", "admin", "");
|
||||
}
|
||||
|
||||
protected override void Data_Update(Object sender, MqttEventArgs e) {
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override void Sensor_Update(Object sender, EventArgs e) {
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override void User_Update(Object sender, UserMessageEventArgs e) {
|
||||
if(e.Message == "/start") {
|
||||
this.Send("Hallo zurück! Ich kann aktuell die Befehle /licht");
|
||||
}
|
||||
if (e.Message.StartsWith("/licht")) {
|
||||
this.BotLicht(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private void BotLicht(String message) {
|
||||
if (message == "/licht") {
|
||||
this.user.Send("Was soll ich tun?", new String[] { "/licht einschalten", "/licht ausschalten", "/licht aufzaehlen" });
|
||||
}
|
||||
if(message == "/licht aufzaehlen") {
|
||||
this.user.Send("Ich habe folgende Lampen im Angebot:");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
27
IoT-Bot/Condition/Trigger.cs
Normal file
27
IoT-Bot/Condition/Trigger.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using BlubbFish.Utils.IoT.Connector;
|
||||
using BlubbFish.Utils.IoT.Sensor;
|
||||
|
||||
namespace IoTBot.Condition {
|
||||
class Trigger : ACondition {
|
||||
public Trigger(String name, Dictionary<String, String> settings, ASensor sensor, ADataBackend data, AUserBackend user) : base(name, settings, sensor, data, user) { }
|
||||
|
||||
protected override void Data_Update(Object sender, MqttEventArgs e) {
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override void Sensor_Update(Object sender, EventArgs e) {
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override void User_Update(Object sender, UserMessageEventArgs e) {
|
||||
if (e.Message == this.settings["trigger"]) {
|
||||
this.Send();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -16,13 +16,16 @@ namespace IoTBot {
|
||||
}
|
||||
|
||||
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 = null;
|
||||
if (settings.ContainsKey("sensor_type")) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
sensor = ASensor.GetInstance(this.data, sensor_settings, name);
|
||||
}
|
||||
ASensor sensor = ASensor.GetInstance(this.data, sensor_settings, name);
|
||||
this.conditions.Add(ACondition.GetInstance(name, settings, sensor, this.data, this.user));
|
||||
}
|
||||
|
||||
|
@ -7,8 +7,8 @@
|
||||
<ProjectGuid>{89077643-B472-419F-8EAB-56B9E2D13ABC}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>MqttToTelegram</RootNamespace>
|
||||
<AssemblyName>MqttToTelegram</AssemblyName>
|
||||
<RootNamespace>IoTBot</RootNamespace>
|
||||
<AssemblyName>IoTBot</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
@ -37,6 +37,8 @@
|
||||
<Compile Include="ConditionWorker.cs" />
|
||||
<Compile Include="Condition\ACondition.cs" />
|
||||
<Compile Include="Condition\Edge.cs" />
|
||||
<Compile Include="Condition\TelegramBot.cs" />
|
||||
<Compile Include="Condition\Trigger.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
@ -55,6 +57,10 @@
|
||||
<Project>{fac8ce64-bf13-4ece-8097-aeb5dd060098}</Project>
|
||||
<Name>Utils</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\Zway\Zway\Zway.csproj">
|
||||
<Project>{166258ed-cb3d-43f5-8e8d-3a993b64d022}</Project>
|
||||
<Name>Zway</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
@ -13,9 +13,11 @@ namespace IoTBot {
|
||||
foreach (String section in condition_settings.GetSections()) {
|
||||
worker.SetCondition(section, condition_settings.GetSection(section));
|
||||
}
|
||||
|
||||
mqtt.MessageIncomming += Mqtt_MessageIncomming;
|
||||
mqtt.MessageSending += Mqtt_MessageSending;
|
||||
|
||||
if (mqtt != null) {
|
||||
mqtt.MessageIncomming += Mqtt_MessageIncomming;
|
||||
mqtt.MessageSending += Mqtt_MessageSending;
|
||||
}
|
||||
telegram.MessageIncomming += Telegram_MessageIncomming;
|
||||
telegram.MessageSending += Telegram_MessageSending;
|
||||
|
||||
@ -23,6 +25,12 @@ namespace IoTBot {
|
||||
|
||||
while(true) {
|
||||
System.Threading.Thread.Sleep(100);
|
||||
if(Console.KeyAvailable) {
|
||||
ConsoleKeyInfo key = Console.ReadKey(false);
|
||||
if(key.Key == ConsoleKey.Escape) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user