diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee2be7a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.vs +/Snips/bin +/Snips/obj diff --git a/Snips.sln b/Snips.sln new file mode 100644 index 0000000..49f36f3 --- /dev/null +++ b/Snips.sln @@ -0,0 +1,61 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.645 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Snips", "Snips\Snips.csproj", "{E32A1FC4-CF86-4DEA-8868-AC14BA4D1A85}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M2Mqtt_4.7.1", "..\Librarys\mqtt\M2Mqtt\M2Mqtt_4.7.1.csproj", "{A11AEF5A-B246-4FE8-8330-06DB73CC8074}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "litjson_4.7.1", "..\Librarys\litjson\litjson\litjson_4.7.1.csproj", "{91A14CD2-2940-4500-8193-56D37EDDDBAA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bot-Utils", "..\Utils\Bot-Utils\Bot-Utils\Bot-Utils.csproj", "{BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Posix", "..\Librarys\Mono.Posix\Mono.Posix\Mono.Posix.csproj", "{E2CA132E-E85C-40AD-BE94-B138AA68772B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils-IoT", "..\Utils\Utils-IoT\Utils-IoT\Utils-IoT.csproj", "{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectorDataMqtt", "..\Utils\ConnectorDataMqtt\ConnectorDataMqtt\ConnectorDataMqtt.csproj", "{EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E32A1FC4-CF86-4DEA-8868-AC14BA4D1A85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E32A1FC4-CF86-4DEA-8868-AC14BA4D1A85}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E32A1FC4-CF86-4DEA-8868-AC14BA4D1A85}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E32A1FC4-CF86-4DEA-8868-AC14BA4D1A85}.Release|Any CPU.Build.0 = Release|Any CPU + {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A11AEF5A-B246-4FE8-8330-06DB73CC8074}.Release|Any CPU.Build.0 = Release|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.Build.0 = Release|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9}.Release|Any CPU.Build.0 = Release|Any CPU + {E2CA132E-E85C-40AD-BE94-B138AA68772B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E2CA132E-E85C-40AD-BE94-B138AA68772B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E2CA132E-E85C-40AD-BE94-B138AA68772B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E2CA132E-E85C-40AD-BE94-B138AA68772B}.Release|Any CPU.Build.0 = Release|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.Build.0 = Release|Any CPU + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EE6C8F68-ED46-4C1C-ABDD-CFCDF75104F2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3E7374AD-D5E2-4BD3-A37C-0BB057D34C25} + EndGlobalSection +EndGlobal diff --git a/Snips/DataInputs/EnvEsp8266.cs b/Snips/DataInputs/EnvEsp8266.cs new file mode 100644 index 0000000..ef08077 --- /dev/null +++ b/Snips/DataInputs/EnvEsp8266.cs @@ -0,0 +1,28 @@ +using System; +using LitJson; + +namespace BlubbFish.Iot.Snips.DataInputs { + public class EnvEsp8266 { + public Double Humidity { get; private set; } + public Double Lux { get; private set; } + public Double Pressure { get; private set; } + public Double Temperature { get; private set; } + public String Name { get; private set; } + + public EnvEsp8266(JsonData data) { + this.Name = GetID(data); + this.Update(data); + } + + internal static String GetID(JsonData data) => data["Name"].ToString(); + + internal void Update(JsonData data) { + this.Humidity = (Double)data["Humidity"]; + this.Lux = (Double)data["Lux"]; + this.Pressure = (Double)data["Pressure"]; + this.Temperature = (Double)data["Temperature"]; + } + + public override String ToString() => this.Name + " " + this.Temperature + "°C " + this.Humidity + "% " + this.Pressure + " mhP " + this.Lux + " Lux"; + } +} \ No newline at end of file diff --git a/Snips/Helper/Texthelper.cs b/Snips/Helper/Texthelper.cs new file mode 100644 index 0000000..c819f1a --- /dev/null +++ b/Snips/Helper/Texthelper.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlubbFish.Iot.Snips.Helper { + public class Texthelper { + public static Int32 ComputeLevenshteinDistance(String source, String target) { + if((source == null) || (target == null)) { + return 0; + } + + if((source.Length == 0) || (target.Length == 0)) { + return 0; + } + + if(source == target) { + return source.Length; + } + + Int32 sourceWordCount = source.Length; + Int32 targetWordCount = target.Length; + + // Step 1 + if(sourceWordCount == 0) { + return targetWordCount; + } + + if(targetWordCount == 0) { + return sourceWordCount; + } + + Int32[,] distance = new Int32[sourceWordCount + 1, targetWordCount + 1]; + + // Step 2 + for(Int32 i = 0; i <= sourceWordCount; distance[i, 0] = i++) { } + + for(Int32 j = 0; j <= targetWordCount; distance[0, j] = j++) { } + + for(Int32 i = 1; i <= sourceWordCount; i++) { + for(Int32 j = 1; j <= targetWordCount; j++) { + // Step 3 + Int32 cost = (target[j - 1] == source[i - 1]) ? 0 : 1; + + // Step 4 + distance[i, j] = Math.Min(Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1), distance[i - 1, j - 1] + cost); + } + } + + return distance[sourceWordCount, targetWordCount]; + } + + public static Double CalculateSimilarity(String source, String target) { + if((source == null) || (target == null)) { + return 0.0; + } + + if((source.Length == 0) || (target.Length == 0)) { + return 0.0; + } + + if(source == target) { + return 1.0; + } + + Int32 stepsToSame = ComputeLevenshteinDistance(source, target); + return (1.0 - (stepsToSame / (Double)Math.Max(source.Length, target.Length))); + } + } +} diff --git a/Snips/Intents/AEspIntent.cs b/Snips/Intents/AEspIntent.cs new file mode 100644 index 0000000..f5a84d6 --- /dev/null +++ b/Snips/Intents/AEspIntent.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using BlubbFish.Iot.Snips.DataInputs; +using BlubbFish.Utils.IoT.Connector; +using LitJson; + +namespace BlubbFish.Iot.Snips.Intents { + abstract public class AEspIntent : AIntent { + protected readonly Dictionary esps; + protected String espid = null; + + protected AEspIntent(JsonData data, ABackend snips, Dictionary esps) : base(data, snips) => this.esps = esps; + + public override void Parse() { + String ort = this.slots["ort"]; + String mort = "ESP8266-" + ort; + Dictionary goal = new Dictionary(); + foreach(KeyValuePair item in this.esps) { + goal.Add(item.Key, Helper.Texthelper.CalculateSimilarity(mort.ToLower(), item.Key.ToLower())); + } + if(goal.Count > 0) { + KeyValuePair t = goal.First(); + foreach(KeyValuePair move in goal) { + if(move.Value > t.Value) { + t = move; + } + } + this.espid = t.Key; + } + } + } +} diff --git a/Snips/Intents/AIntent.cs b/Snips/Intents/AIntent.cs new file mode 100644 index 0000000..1adc34b --- /dev/null +++ b/Snips/Intents/AIntent.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using BlubbFish.Utils.IoT.Connector; +using LitJson; + +namespace BlubbFish.Iot.Snips.Intents { + public abstract class AIntent { + protected readonly JsonData data; + private readonly ABackend snips; + protected readonly Dictionary slots = new Dictionary(); + + protected AIntent(JsonData data, ABackend snips) { + this.data = data; + this.snips = snips; + this.ParseSlots(); + } + + private void ParseSlots() { + foreach(JsonData item in this.data["slots"]) { + this.slots.Add(item["slotName"].ToString(), item["value"]["value"].ToString()); + } + } + + protected void SnipsSay(String text) => ((ADataBackend)this.snips).Send("hermes/tts/say", + JsonMapper.ToJson( + new Dictionary { + { "id",this.data["id"].ToString() }, + { "lang","de" }, + { "sessionId", this.data["sessionId"].ToString() }, + { "siteId","default" }, + { "text",text } + } + ) + ); + + protected void SnipsEndSpeak() => ((ADataBackend)this.snips).Send("hermes/tts/sayFinished", + JsonMapper.ToJson( + new Dictionary { + { "id", this.data["id"].ToString() }, + { "sessionId", this.data["sessionId"].ToString() } + } + ) + ); + + public abstract void Parse(); + + public abstract void Answer(); + } +} diff --git a/Snips/Intents/GetHum.cs b/Snips/Intents/GetHum.cs new file mode 100644 index 0000000..f16bd5d --- /dev/null +++ b/Snips/Intents/GetHum.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using BlubbFish.Iot.Snips.DataInputs; +using BlubbFish.Utils.IoT.Connector; +using LitJson; + +namespace BlubbFish.Iot.Snips.Intents { + public class GetHum : AEspIntent { + public GetHum(JsonData data, ABackend snips, Dictionary esps) : base(data, snips, esps) { + } + + public override void Answer() { + if(this.espid != null) { + this.SnipsSay("Die Luftfeuchtigkeit beträgt " + this.esps[this.espid].Humidity + " Prozent relative Luftfeuchtigkeit im " + this.slots["ort"] + " !"); + } else { + this.SnipsSay("Ich habe bisher keine Sensordaten erhalten!"); + } + this.SnipsEndSpeak(); + } + } +} diff --git a/Snips/Intents/GetTemp.cs b/Snips/Intents/GetTemp.cs new file mode 100644 index 0000000..6b58e2d --- /dev/null +++ b/Snips/Intents/GetTemp.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using BlubbFish.Iot.Snips.DataInputs; +using BlubbFish.Utils.IoT.Connector; +using LitJson; + +namespace BlubbFish.Iot.Snips.Intents { + class GetTemp : AEspIntent { + public GetTemp(JsonData data, ABackend snips, Dictionary esps) : base(data, snips, esps) { + } + + public override void Answer() { + if(this.espid != null) { + this.SnipsSay("Im " + this.slots["ort"] + " ist es " + this.esps[this.espid].Temperature + " Grad "+this.slots["feel"]+"!"); + } else { + this.SnipsSay("Ich habe bisher keine Sensordaten erhalten!"); + } + this.SnipsEndSpeak(); + } + } +} diff --git a/Snips/Intents/Wetter.cs b/Snips/Intents/Wetter.cs new file mode 100644 index 0000000..d3d4a3e --- /dev/null +++ b/Snips/Intents/Wetter.cs @@ -0,0 +1,65 @@ +using System; +using System.IO; +using System.Net; +using System.Text; +using System.Text.RegularExpressions; +using BlubbFish.Utils.IoT.Connector; +using LitJson; + +namespace BlubbFish.Iot.Snips.Intents { + public class Wetter : AIntent { + private String wetterText; + + public Wetter(JsonData data, ABackend snips) : base(data, snips) { + } + + public override void Answer() { + this.SnipsSay("Die Wettervorhersage für NRW Lautet:"); + this.SnipsSay(this.wetterText); + this.SnipsEndSpeak(); + } + public override void Parse() { + String html = null; + String url = "https://opendata.dwd.de/weather/text_forecasts/txt/"; + + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + + using(HttpWebResponse response = (HttpWebResponse)request.GetResponse()) + using(Stream stream = response.GetResponseStream()) + using(StreamReader reader = new StreamReader(stream)) { + html = reader.ReadToEnd(); + } + if(html != null) { + String file = null; + MatchCollection m = new Regex("\"(.*VHDL13_DWEH_([0-9]{6}).*)\"").Matches(html); + if(m.Count > 1) { + Int32 d = Int32.Parse(m[0].Groups[2].Value); + file = m[0].Groups[1].Value; + foreach(Match item in m) { + if(d < Int32.Parse(item.Groups[2].Value)) { + file = item.Groups[1].Value; + } + } + } else if(m.Count == 1) { + file = m[0].Groups[1].Value; + } + String wetter = null; + String urlw = "https://opendata.dwd.de/weather/text_forecasts/txt/"+ file; + + HttpWebRequest requestw = (HttpWebRequest)WebRequest.Create(urlw); + Byte[] buffer; + using(HttpWebResponse response = (HttpWebResponse)requestw.GetResponse()) + using(Stream stream = response.GetResponseStream()) + using(MemoryStream memstream = new MemoryStream()) { + stream.CopyTo(memstream); + buffer = memstream.ToArray(); + } + wetter = Encoding.UTF8.GetString(Encoding.Convert(Encoding.GetEncoding(1252), Encoding.UTF8, buffer)); + wetter = wetter.Replace("\r\r\n", "\n"); + Int32 start = wetter.IndexOf("\n\n")+2; + Int32 end = wetter.IndexOf("Detaillierter Wetterablauf") - start; + this.wetterText = wetter.Substring(start, end); + } + } + } +} diff --git a/Snips/Program.cs b/Snips/Program.cs new file mode 100644 index 0000000..80d8de3 --- /dev/null +++ b/Snips/Program.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using BlubbFish.Utils.IoT.Connector; + +namespace BlubbFish.Iot.Snips { + class Program { + public Program(String[] args) { + Dictionary sources = new Dictionary { + { "snips", ABackend.GetInstance(new Dictionary { { "type", "mqtt" }, { "server", "10.100.0.34" }, { "topic", "hermes/nlu/intentParsed" } }, ABackend.BackendType.Data) }, + { "iot", ABackend.GetInstance(new Dictionary { { "type", "mqtt" }, { "server", "iot.blubbfish.net" }, { "topic", "esp8266/sensor/+" } }, ABackend.BackendType.Data) } + }; + SnipsBot s = new SnipsBot(sources, new Dictionary { { "logger", "out.log" } }); + s.Dispose(); + } + + static void Main(String[] args) => new Program(args); + } +} diff --git a/Snips/Properties/AssemblyInfo.cs b/Snips/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..eda5a99 --- /dev/null +++ b/Snips/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("Snips")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Snips")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly +// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("e32a1fc4-cf86-4dea-8868-ac14ba4d1a85")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Snips/Snips.csproj b/Snips/Snips.csproj new file mode 100644 index 0000000..5d768b3 --- /dev/null +++ b/Snips/Snips.csproj @@ -0,0 +1,81 @@ + + + + + Debug + AnyCPU + {E32A1FC4-CF86-4DEA-8868-AC14BA4D1A85} + Exe + BlubbFish.Iot.Snips + Snips + v4.7.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + {91A14CD2-2940-4500-8193-56D37EDDDBAA} + litjson_4.7.1 + + + {A11AEF5A-B246-4FE8-8330-06DB73CC8074} + M2Mqtt_4.7.1 + + + {BB7BFCB5-3DB0-49E1-802A-3CE3EECC59F9} + Bot-Utils + + + {ee6c8f68-ed46-4c1c-abdd-cfcdf75104f2} + ConnectorDataMqtt + + + {B870E4D5-6806-4A0B-B233-8907EEDC5AFC} + Utils-IoT + + + + + \ No newline at end of file diff --git a/Snips/SnipsBot.cs b/Snips/SnipsBot.cs new file mode 100644 index 0000000..0f80b83 --- /dev/null +++ b/Snips/SnipsBot.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using BlubbFish.Iot.Snips.DataInputs; +using BlubbFish.Iot.Snips.Intents; +using BlubbFish.Utils.IoT.Bots; +using BlubbFish.Utils.IoT.Connector; +using BlubbFish.Utils.IoT.Events; +using LitJson; + +namespace BlubbFish.Iot.Snips { + class SnipsBot : MultiSourceBot { + private readonly Boolean debug = true; + private readonly Dictionary esps = new Dictionary(); + + public SnipsBot(Dictionary sources, Dictionary settings) : base(sources, settings) { + this.sources["snips"].MessageIncomming += this.IntentWorker; + this.sources["iot"].MessageIncomming += this.GetEspData; + this.WaitForShutdown(); + } + + private void IntentWorker(Object sender, BackendEvent e) { + try { + JsonData data = JsonMapper.ToObject(e.Message); + AIntent intent = null; + if(data["intent"]["intentName"].ToString() == "blubbfish:GetTemp") { + intent = new GetTemp(data, this.sources["snips"], this.esps); + } else if(data["intent"]["intentName"].ToString() == "blubbfish:GetHum") { + intent = new GetHum(data, this.sources["snips"], this.esps); + } else if(data["intent"]["intentName"].ToString() == "blubbfish:Wetter") { + intent = new Wetter(data, this.sources["snips"]); + } + if(intent != null) { + intent.Parse(); + intent.Answer(); + } + } catch { } + } + + private void GetEspData(Object sender, BackendEvent e) { + try { + JsonData data = JsonMapper.ToObject(e.Message); + String name = EnvEsp8266.GetID(data); + if(this.esps.ContainsKey(name)) { + this.esps[name].Update(data); + } else { + this.esps.Add(name, new EnvEsp8266(data)); + } + if(this.debug) { + Console.WriteLine("Get data: " + this.esps[name]); + } + } catch { } + } + + public override void Dispose() { + foreach(KeyValuePair item in this.sources) { + item.Value.Dispose(); + } + base.Dispose(); + } + } +}