commit ffdbb6eaa46d459292efdfb5b9fbe41d3dde20b1 Author: BlubbFish Date: Sat Mar 31 20:42:28 2018 +0000 [DW] Flex4Grid deleted [NF] BosMon Mqtt Plugin created diff --git a/BosmonMqtt.sln b/BosmonMqtt.sln new file mode 100644 index 0000000..f654721 --- /dev/null +++ b/BosmonMqtt.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27004.2010 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BosmonMqtt", "BosmonMqtt\BosmonMqtt.csproj", "{EE2F31C4-64D7-4788-AC68-33C5CC8EFCCD}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "M2Mqtt", "..\Librarys\mqtt\M2Mqtt\M2Mqtt.csproj", "{A11AEF5A-B246-4FE8-8330-06DB73CC8074}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EE2F31C4-64D7-4788-AC68-33C5CC8EFCCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EE2F31C4-64D7-4788-AC68-33C5CC8EFCCD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EE2F31C4-64D7-4788-AC68-33C5CC8EFCCD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EE2F31C4-64D7-4788-AC68-33C5CC8EFCCD}.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 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {768F1E16-781A-45A6-B11D-6B2D5E0008B1} + EndGlobalSection +EndGlobal diff --git a/BosmonMqtt/BosmonMqtt.csproj b/BosmonMqtt/BosmonMqtt.csproj new file mode 100644 index 0000000..7d7219e --- /dev/null +++ b/BosmonMqtt/BosmonMqtt.csproj @@ -0,0 +1,80 @@ + + + + + Debug + AnyCPU + {EE2F31C4-64D7-4788-AC68-33C5CC8EFCCD} + Library + Properties + BlubbFish.BosmonMqtt + BosmonMqtt + v2.0 + 512 + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + x86 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + x86 + + + + D:\Programme\BosMon\BosMonClasses.dll + False + + + + + + + + D:\Programme\BosMon\plugins\TelegramFilter.dll + False + + + + + + + + + + UserControl + + + Config.cs + + + + + Config.cs + + + + + {a11aef5a-b246-4fe8-8330-06db73cc8074} + M2Mqtt + + + + + copy "$(TargetPath)" "D:\Programme\BosMon_dev\plugins\$(TargetFileName)" +copy "$(TargetDir)\M2Mqtt.dll" "D:\Programme\BosMon_dev\plugins\M2Mqtt.dll" +del "D:\Programme\BosMon_dev\config\bosmon.log" + + \ No newline at end of file diff --git a/BosmonMqtt/BosmonMqtt.csproj.user b/BosmonMqtt/BosmonMqtt.csproj.user new file mode 100644 index 0000000..1a284e1 --- /dev/null +++ b/BosmonMqtt/BosmonMqtt.csproj.user @@ -0,0 +1,9 @@ + + + + Program + D:\Programme\BosMon_dev\BosMon.exe + D:\Programme\BosMon_dev\ + -nosplashscreen -debug -configpath "D:\Programme\BosMon_dev\config" + + \ No newline at end of file diff --git a/BosmonMqtt/MqttEventProcessor.cs b/BosmonMqtt/MqttEventProcessor.cs new file mode 100644 index 0000000..ffc0c9e --- /dev/null +++ b/BosmonMqtt/MqttEventProcessor.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Text; +using BosMon.Data; +using BosMon.Data.Telegrams; +using BosMon.Prefs; +using TelegramFilter.Filter; +using uPLibrary.Networking.M2Mqtt; +using uPLibrary.Networking.M2Mqtt.Messages; + +namespace BlubbFish.BosmonMqtt { + class MqttEventProcessor : IDisposable { + private PluginConfiguration pluginconfig; + private MqttClient client; + private FilterList filter; + private List pfilter = new List(); + + public MqttEventProcessor(PluginConfiguration pluginconfiguration) { + this.pluginconfig = pluginconfiguration; + this.Connect(); + } + + public void Connect() { + try { + this.client = new MqttClient(this.pluginconfig.Server, this.pluginconfig.Port, false, null); + if (this.pluginconfig.User == "" || this.pluginconfig.Password == "") { + this.client.Connect("bosmon-" + Guid.NewGuid().ToString()); + } else { + this.client.Connect("bosmon-" + Guid.NewGuid().ToString(), this.pluginconfig.User, this.pluginconfig.Password); + } + this.client.Subscribe(new String[] { this.pluginconfig.Topic }, new Byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE }); + } catch (Exception) { + + } + this.filter = new FilterList(this.pluginconfig.PluginStorage.GetSection("filter")); + this.pfilter.Clear(); + foreach (FilterItem item in this.filter) { + if(item.GetType() == typeof(PocsagFilterItem)) { + this.pfilter.Add(item as PocsagFilterItem); + } + } + } + + internal void TelegramEvent(Object sender, TelegramEventArgs e) { + Telegram t = e.Telegram as Telegram; + if (this.client != null && this.client.IsConnected && this.filter != null) { + if (t.Type == PocsagTelegram.TYPE_POCSAG) { + PocsagTelegram p = t as PocsagTelegram; + foreach (PocsagFilterItem item in this.pfilter) { + if((item.IsMatching(p) && item.Negated)) { + return; + } + } + this.client.Publish(this.pluginconfig.Topic + p.Address+p.Func, Encoding.UTF8.GetBytes(p.Msg)); + } + } + } + + #region IDisposable Support + private Boolean disposedValue = false; + + + protected virtual void Dispose(Boolean disposing) { + if (!this.disposedValue) { + if (disposing) { + // TODO: verwalteten Zustand (verwaltete Objekte) entsorgen. + } + this.disposedValue = true; + } + } + + public void Dispose() { + Dispose(true); + } + #endregion + } +} diff --git a/BosmonMqtt/MqttPlugin.cs b/BosmonMqtt/MqttPlugin.cs new file mode 100644 index 0000000..0910c3a --- /dev/null +++ b/BosmonMqtt/MqttPlugin.cs @@ -0,0 +1,89 @@ +using System; +using BosMon.Exceptions; +using BosMon.Plugins; +using BosMon.Prefs; + +namespace BlubbFish.BosmonMqtt { + public class Class1 : IBosMonPlugin { + private Plugin plugin = new Plugin(); + #region Plugin Eigenschaften + String IBosMonPlugin.PluginName { + get { + return "MQTT-Bridge"; + } + } + + String IBosMonPlugin.PluginDescription { + get { + return "Sendet Telegramme nach MQTT"; + } + } + + Int32 IBosMonPlugin.PluginVersion { + get { + return 1; + } + } + + PluginType IBosMonPlugin.PluginType { + get { + return PluginType.Client; + } + } + + Int32 IBosMonPlugin.PluginFlags { + get { + return 0; + } + } + + String[] IBosMonPlugin.StartPluginAfter { + get { + return null; + } + } + #endregion + #region Plugin Connectoren + public IBosMonHost PluginHost { + get { + return this.plugin.PluginHost; + } + set { + this.plugin.PluginHost = value ?? throw new BosMonArgumentNullException(); + } + } + + IBosMonConfigurationStorage IBosMonPlugin.ConfigurationStorage { + get { + return this.plugin.ConfigurationStorage; + } + set { + if (value == null) { + throw new BosMonArgumentNullException(); + } else if (this.plugin.ConfigurationStorage != null) { + throw new BosMonInternalException("ConfigStorage already set"); + } + this.plugin.ConfigurationStorage = value; + } + } + #endregion + #region Plugin Einstiegspunkte + void IDisposable.Dispose() { + this.plugin.Dispose(true); + GC.SuppressFinalize(this); + } + + void IBosMonPlugin.InitializePlugin() { + this.plugin.Init(); + } + + void IBosMonPlugin.StartPlugin() { + this.plugin.Start(); + } + + void IBosMonPlugin.StopPlugin() { + this.plugin.Stop(); + } + #endregion + } +} diff --git a/BosmonMqtt/Plugin.cs b/BosmonMqtt/Plugin.cs new file mode 100644 index 0000000..f483c25 --- /dev/null +++ b/BosmonMqtt/Plugin.cs @@ -0,0 +1,85 @@ +using System; +using BlubbFish.BosmonMqtt.Views; +using BosMon.Data.Processors; +using BosMon.Exceptions; +using BosMon.Plugins; +using BosMon.Prefs; + +namespace BlubbFish.BosmonMqtt { + class Plugin { + private PluginConfiguration pluginconfiguration; + private Boolean isDisposed = false; + private Boolean isInitialized = false; + private ConfigView _configView; + private MqttEventProcessor _mqtteventprocessor; + private Boolean isStarted = false; + private ITelegramEventProcessor _events; + + internal IBosMonHost PluginHost { get; set; } + internal IBosMonConfigurationStorage ConfigurationStorage { get; set; } + + internal void Init() { + if (this.ConfigurationStorage == null || this.PluginHost == null) { + throw new BosMonArgumentNullException(); + } + if (this.isInitialized) { + throw new BosMonInternalException(); + } + this.pluginconfiguration = new PluginConfiguration(this.ConfigurationStorage); + this.isInitialized = true; + this._configView = new ConfigView(this.pluginconfiguration); + this._mqtteventprocessor = new MqttEventProcessor(this.pluginconfiguration); + } + + internal void Start() { + if (!this.isInitialized) { + throw new BosMonInternalException("Not initialized"); + } + try { + // ... Plugin starten ... + this.PluginHost.RegisterConfigPage("Plugins", "Mqtt", this._configView); + //this.PluginHost.RegisterEventProcessor("MqttEventProcessor", this._mqtteventprocessor); + + this._events = this.PluginHost.GetEventProcessor("TelegramEventProcessor") as ITelegramEventProcessor; + this._events.TelegramEvent += this._mqtteventprocessor.TelegramEvent; + + // Es muss unbedingt darauf geachtet werden, dass das Plugin in dieser + // Methode nicht blockiert, da sonst der Hauptthread von BosMon ebenfalls + // blockiert wird. + + this.isStarted = true; + } catch (Exception) { + + } + } + + internal void Stop() { + if (!this.isInitialized) { + throw new BosMonInternalException("Not initialized"); + } + + try { + if (this.isStarted) { + this._configView.Dispose(); + this.PluginHost.UnRegisterConfigPage(this._configView); + this._events.TelegramEvent -= this._mqtteventprocessor.TelegramEvent; + this._mqtteventprocessor.Dispose(); + this.isStarted = false; + } + } catch (Exception ex) { + BosMon.Gui.BosMonErrorBox.Show(ex, "Fehler"); + } + } + + internal void Dispose(Boolean dispose) { + if (!this.isDisposed) { + if (dispose) { + try { + //DeInitialize(); + } catch (Exception) { } + } + this.isDisposed = true; + } + } + } +} diff --git a/BosmonMqtt/PluginConfiguration.cs b/BosmonMqtt/PluginConfiguration.cs new file mode 100644 index 0000000..b17a422 --- /dev/null +++ b/BosmonMqtt/PluginConfiguration.cs @@ -0,0 +1,123 @@ +using BosMon.Prefs; +using System; +using System.Text; + +namespace BlubbFish.BosmonMqtt { + internal class PluginConfiguration : BosMonConfiguration, IBosMonConfigurationTemplate { + private String _server; + private String _user; + private String _pass; + private String _topic; + private Int32 _port; + + public PluginConfiguration(IBosMonConfigurationStorage storage) : base(storage) { + this.PluginStorage = storage; + } + + public IBosMonConfigurationStorage PluginStorage { get; private set; } + + public String Server { + get { + return this._server; + } + set { + this._server = value; + WriteConfiguration(); + } + } + public String User { + get { + return this._user; + } + set { + this._user = value; + WriteConfiguration(); + } + } + public String Password { + get { + return Encoding.UTF8.GetString(Convert.FromBase64String(this._pass)); + } + set { + this._pass = Convert.ToBase64String(Encoding.UTF8.GetBytes(value)); + ; + WriteConfiguration(); + } + } + public String Topic { + get { + return this._topic; + } + set { + this._topic = value; + if (this._topic.Length > 0 && this._topic[0] != '/') { + this._topic = "/" + this._topic; + } + if (this._topic.Length > 1 && this._topic[this._topic.Length-1] != '/') { + this._topic = this._topic + "/"; + } + WriteConfiguration(); + } + } + public Int32 Port { + get { + return this._port; + } + set { + this._port = value; + WriteConfiguration(); + } + } + + #region IBosMonConfigurationTemplate Config-Variablen + public override void ReadConfiguration() { + base.ReadConfiguration(); + this._server = this.Storage.ReadString("server", ""); + this._user = this.Storage.ReadString("user", ""); + this._pass = this.Storage.ReadString("pass", ""); + this._topic = this.Storage.ReadString("topic", ""); + this._port = this.Storage.ReadInteger("port", 1883); + } + public override void WriteConfiguration() { + this.Storage.Write("server", this._server); + this.Storage.Write("user", this._user); + this.Storage.Write("pass", this._pass); + this.Storage.Write("topic", this._topic); + this.Storage.Write("port", this._port); + base.WriteConfiguration(); + } + #endregion + #region IBosMonConfigurationTemplate Member + public override TKeyVal[] StringVariables { + get { + return Merge( + new TKeyVal[] { + new TKeyVal("server", "localhost"), + new TKeyVal("user", ""), + new TKeyVal("pass", ""), + new TKeyVal("topic", "/bosmon/messages/") + }, + base.StringVariables); + } + } + public override TKeyVal[] IntVariables { + get { + return Merge(new TKeyVal[] { + new TKeyVal("port",1883) + }, base.IntVariables); + } + } + public override TKeyVal[] Sections { + get { + return new TKeyVal[] { + new TKeyVal("filter", true) + }; + } + } + + + + + #endregion + } +} \ No newline at end of file diff --git a/BosmonMqtt/Properties/AssemblyInfo.cs b/BosmonMqtt/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..f551efa --- /dev/null +++ b/BosmonMqtt/Properties/AssemblyInfo.cs @@ -0,0 +1,39 @@ +using System.Resources; +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("BosmonMqtt")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("BosmonMqtt")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("BlubbFish")] +[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("ee2f31c4-64d7-4788-ac68-33c5cc8efccd")] + +// 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, +// indem Sie "*" wie unten gezeigt eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: NeutralResourcesLanguage("de")] + diff --git a/BosmonMqtt/Views/Config.Designer.cs b/BosmonMqtt/Views/Config.Designer.cs new file mode 100644 index 0000000..22d5817 --- /dev/null +++ b/BosmonMqtt/Views/Config.Designer.cs @@ -0,0 +1,232 @@ +namespace BlubbFish.BosmonMqtt.Views { + partial class ConfigView { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) { + if (disposing && (components != null)) { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() { + this.label1 = new System.Windows.Forms.Label(); + this.textServer = new System.Windows.Forms.TextBox(); + this.textUser = new System.Windows.Forms.TextBox(); + this.label3 = new System.Windows.Forms.Label(); + this.textPass = new System.Windows.Forms.TextBox(); + this.label4 = new System.Windows.Forms.Label(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.textPort = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.label7 = new System.Windows.Forms.Label(); + this.labelStatus = new System.Windows.Forms.Label(); + this.testButton = new System.Windows.Forms.Button(); + this.label5 = new System.Windows.Forms.Label(); + this.textTopic = new System.Windows.Forms.TextBox(); + this.filterButton = new System.Windows.Forms.Button(); + this.groupBox1.SuspendLayout(); + this.SuspendLayout(); + // + // label1 + // + this.label1.Location = new System.Drawing.Point(6, 16); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(55, 20); + this.label1.TabIndex = 0; + this.label1.Text = "Server:"; + this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // textServer + // + this.textServer.Location = new System.Drawing.Point(67, 16); + this.textServer.Name = "textServer"; + this.textServer.Size = new System.Drawing.Size(317, 20); + this.textServer.TabIndex = 1; + this.textServer.Tag = "server"; + this.textServer.Leave += new System.EventHandler(this.Settings_Changed); + // + // textUser + // + this.textUser.Location = new System.Drawing.Point(67, 68); + this.textUser.Name = "textUser"; + this.textUser.Size = new System.Drawing.Size(317, 20); + this.textUser.TabIndex = 4; + this.textUser.Tag = "user"; + this.textUser.Leave += new System.EventHandler(this.Settings_Changed); + // + // label3 + // + this.label3.Location = new System.Drawing.Point(6, 67); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(55, 20); + this.label3.TabIndex = 3; + this.label3.Text = "Benutzer:"; + this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // textPass + // + this.textPass.Location = new System.Drawing.Point(67, 94); + this.textPass.Name = "textPass"; + this.textPass.Size = new System.Drawing.Size(317, 20); + this.textPass.TabIndex = 6; + this.textPass.Tag = "pass"; + this.textPass.UseSystemPasswordChar = true; + this.textPass.Leave += new System.EventHandler(this.Settings_Changed); + // + // label4 + // + this.label4.Location = new System.Drawing.Point(6, 94); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(55, 20); + this.label4.TabIndex = 5; + this.label4.Text = "Passwort:"; + this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // groupBox1 + // + this.groupBox1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.groupBox1.Controls.Add(this.textPort); + this.groupBox1.Controls.Add(this.label2); + this.groupBox1.Controls.Add(this.label7); + this.groupBox1.Controls.Add(this.labelStatus); + this.groupBox1.Controls.Add(this.testButton); + this.groupBox1.Controls.Add(this.label5); + this.groupBox1.Controls.Add(this.textTopic); + this.groupBox1.Controls.Add(this.filterButton); + this.groupBox1.Controls.Add(this.textServer); + this.groupBox1.Controls.Add(this.label4); + this.groupBox1.Controls.Add(this.textPass); + this.groupBox1.Controls.Add(this.label1); + this.groupBox1.Controls.Add(this.textUser); + this.groupBox1.Controls.Add(this.label3); + this.groupBox1.Location = new System.Drawing.Point(3, 3); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(390, 250); + this.groupBox1.TabIndex = 7; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Einstellungen"; + // + // textPort + // + this.textPort.Location = new System.Drawing.Point(67, 42); + this.textPort.MaxLength = 5; + this.textPort.Name = "textPort"; + this.textPort.Size = new System.Drawing.Size(317, 20); + this.textPort.TabIndex = 15; + this.textPort.Tag = "port"; + this.textPort.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Check_Numbers); + this.textPort.Leave += new System.EventHandler(this.Settings_Changed); + // + // label2 + // + this.label2.Location = new System.Drawing.Point(6, 42); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(55, 20); + this.label2.TabIndex = 14; + this.label2.Text = "Port:"; + this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // label7 + // + this.label7.Location = new System.Drawing.Point(141, 151); + this.label7.Name = "label7"; + this.label7.Size = new System.Drawing.Size(243, 29); + this.label7.TabIndex = 13; + this.label7.Text = "Wurden Filter eingetragen, so werden nur Telegramme gesendet, die explizit erlaub" + + "t sind."; + // + // labelStatus + // + this.labelStatus.AutoSize = true; + this.labelStatus.Location = new System.Drawing.Point(67, 227); + this.labelStatus.Name = "labelStatus"; + this.labelStatus.Size = new System.Drawing.Size(131, 13); + this.labelStatus.TabIndex = 12; + this.labelStatus.Text = "Verbindung nicht getestet."; + // + // testButton + // + this.testButton.Location = new System.Drawing.Point(67, 197); + this.testButton.Name = "testButton"; + this.testButton.Size = new System.Drawing.Size(200, 23); + this.testButton.TabIndex = 11; + this.testButton.Text = "Einstellungen Testen"; + this.testButton.UseVisualStyleBackColor = true; + this.testButton.Click += new System.EventHandler(this.Setting_Test); + // + // label5 + // + this.label5.Location = new System.Drawing.Point(6, 120); + this.label5.Name = "label5"; + this.label5.Size = new System.Drawing.Size(55, 20); + this.label5.TabIndex = 9; + this.label5.Text = "Topic:"; + this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // textTopic + // + this.textTopic.Location = new System.Drawing.Point(67, 120); + this.textTopic.Name = "textTopic"; + this.textTopic.Size = new System.Drawing.Size(317, 20); + this.textTopic.TabIndex = 10; + this.textTopic.Tag = "topic"; + this.textTopic.Leave += new System.EventHandler(this.Settings_Changed); + // + // filterButton + // + this.filterButton.Location = new System.Drawing.Point(9, 153); + this.filterButton.Name = "filterButton"; + this.filterButton.Size = new System.Drawing.Size(125, 23); + this.filterButton.TabIndex = 8; + this.filterButton.Text = "Filter..."; + this.filterButton.UseVisualStyleBackColor = true; + this.filterButton.Click += new System.EventHandler(this.Filter_Click); + // + // ConfigView + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.AutoSize = true; + this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.Controls.Add(this.groupBox1); + this.Name = "ConfigView"; + this.Size = new System.Drawing.Size(396, 256); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Label label1; + private System.Windows.Forms.TextBox textServer; + private System.Windows.Forms.TextBox textUser; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.TextBox textPass; + private System.Windows.Forms.Label label4; + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.Button filterButton; + private System.Windows.Forms.Label label5; + private System.Windows.Forms.TextBox textTopic; + private System.Windows.Forms.Label labelStatus; + private System.Windows.Forms.Button testButton; + private System.Windows.Forms.Label label7; + private System.Windows.Forms.TextBox textPort; + private System.Windows.Forms.Label label2; + } +} \ No newline at end of file diff --git a/BosmonMqtt/Views/Config.cs b/BosmonMqtt/Views/Config.cs new file mode 100644 index 0000000..706e00a --- /dev/null +++ b/BosmonMqtt/Views/Config.cs @@ -0,0 +1,117 @@ +using System; +using System.Drawing; +using System.Text; +using System.Threading; +using System.Windows.Forms; +using BosMon.Prefs; +using BosMon.Utils; +using TelegramFilter.Filter; +using TelegramFilter.Gui; +using uPLibrary.Networking.M2Mqtt; +using uPLibrary.Networking.M2Mqtt.Messages; + +namespace BlubbFish.BosmonMqtt.Views { + internal partial class ConfigView : UserControl { + private PluginConfiguration pluginconfig; + private EditFilterListDlg filter; + + internal ConfigView(PluginConfiguration pluginconfiguration) { + InitializeComponent(); + this.pluginconfig = pluginconfiguration; + this.filter = new EditFilterListDlg(new FilterList(this.pluginconfig.PluginStorage.GetSection("filter"))); + this.textServer.Text = this.pluginconfig.Server; + this.textUser.Text = this.pluginconfig.User; + this.textPass.Text = this.pluginconfig.Password; + this.textTopic.Text = this.pluginconfig.Topic; + this.textPort.Text = this.pluginconfig.Port.ToString(); + } + + private void Filter_Click(Object sender, EventArgs e) { + this.filter.ShowDialog(); + } + + private void Settings_Changed(Object sender, EventArgs e) { + TextBox t = (TextBox)sender; + switch (t.Tag) { + case "server": + this.pluginconfig.Server = t.Text; + break; + case "user": + this.pluginconfig.User = t.Text; + break; + case "pass": + this.pluginconfig.Password = t.Text; + break; + case "topic": + this.pluginconfig.Topic = t.Text; + t.BeginInvoke((Action)(() => { + t.Text = this.pluginconfig.Topic; + })); + break; + case "port": + if (Int32.TryParse(t.Text, out Int32 port)) { + this.pluginconfig.Port = port; + } + break; + } + } + + private void Check_Numbers(Object sender, KeyPressEventArgs e) { + e.Handled = !Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar); + } + + private void Setting_Test(Object sender, EventArgs e) { + try { + this.labelStatus.BeginInvoke((Action)(() => { + this.labelStatus.Text = "Baue Verbindung auf."; + this.labelStatus.ForeColor = Color.DarkOrange; + })); + MqttClient client = new MqttClient(this.pluginconfig.Server, this.pluginconfig.Port, false, null); + if (this.pluginconfig.User == "" || this.pluginconfig.Password == "") { + client.Connect("bosmon-" + Guid.NewGuid().ToString()); + } else { + client.Connect("bosmon-" + Guid.NewGuid().ToString(), this.pluginconfig.User, this.pluginconfig.Password); + } + this.labelStatus.BeginInvoke((Action)(() => { + this.labelStatus.Text = "Subscribe Topic."; + this.labelStatus.ForeColor = Color.DarkOrange; + })); + client.Subscribe(new String[] { this.pluginconfig.Topic }, new Byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE }); + this.labelStatus.BeginInvoke((Action)(() => { + this.labelStatus.Text = "Schicke Testnachricht."; + this.labelStatus.ForeColor = Color.DarkOrange; + })); + client.MqttMsgPublishReceived += this.Client_MqttMsgPublishReceived; + client.Publish(this.pluginconfig.Topic, Encoding.UTF8.GetBytes("BosMon Connection Test")); + this.labelStatus.BeginInvoke((Action)(() => { + this.labelStatus.Text = "Warte auf Testnachricht."; + this.labelStatus.ForeColor = Color.DarkOrange; + })); + for(Int32 i=0;i<=20;i++) { + Thread.Sleep(100); + if (this._testMessageRecieved) { + break; + } + } + if(!this._testMessageRecieved) { + throw new Exception(); + } + this.labelStatus.BeginInvoke((Action)(() => { + this.labelStatus.Text = "Verbindung erfolgreich!"; + this.labelStatus.ForeColor = Color.DarkGreen; + })); + } catch(Exception) { + this.labelStatus.BeginInvoke((Action)(() => { + this.labelStatus.Text += " Fehler!"; + this.labelStatus.ForeColor = Color.Red; + })); + } + } + private Boolean _testMessageRecieved = false; + private void Client_MqttMsgPublishReceived(Object sender, MqttMsgPublishEventArgs e) { + if(Encoding.UTF8.GetString(e.Message) == "BosMon Connection Test") { + this._testMessageRecieved = true; + } + } + } +} diff --git a/BosmonMqtt/Views/Config.resx b/BosmonMqtt/Views/Config.resx new file mode 100644 index 0000000..5ea0895 --- /dev/null +++ b/BosmonMqtt/Views/Config.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BosmonMqtt/bin/Release/BosmonMqtt.dll b/BosmonMqtt/bin/Release/BosmonMqtt.dll new file mode 100644 index 0000000..79dd32e Binary files /dev/null and b/BosmonMqtt/bin/Release/BosmonMqtt.dll differ diff --git a/BosmonMqtt/bin/Release/M2Mqtt.dll b/BosmonMqtt/bin/Release/M2Mqtt.dll new file mode 100644 index 0000000..b8d32e3 Binary files /dev/null and b/BosmonMqtt/bin/Release/M2Mqtt.dll differ