[NF] Verbindungsüberwachung eingebaut

[NF] Globale Aktivierung des Plugins hinzugefügt
This commit is contained in:
BlubbFish 2018-04-01 11:38:33 +00:00
parent ffdbb6eaa4
commit 01140f1700
8 changed files with 271 additions and 63 deletions

View File

@ -51,7 +51,7 @@
<Compile Include="MqttEventProcessor.cs" />
<Compile Include="MqttPlugin.cs" />
<Compile Include="Plugin.cs" />
<Compile Include="PluginConfiguration.cs" />
<Compile Include="Models\PluginConfiguration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Views\Config.cs">
<SubType>UserControl</SubType>

View File

@ -2,18 +2,22 @@
using System;
using System.Text;
namespace BlubbFish.BosmonMqtt {
namespace BlubbFish.BosmonMqtt.Models {
internal class PluginConfiguration : BosMonConfiguration, IBosMonConfigurationTemplate {
private String _server;
private String _user;
private String _pass;
private String _topic;
private Int32 _port;
private Boolean _enable;
public delegate void Change(Object sender, EventArgs e);
public event Change ConfigChanged;
public PluginConfiguration(IBosMonConfigurationStorage storage) : base(storage) {
this.PluginStorage = storage;
}
#region öffentliche Eigenschaften
public IBosMonConfigurationStorage PluginStorage { get; private set; }
public String Server {
@ -69,6 +73,17 @@ namespace BlubbFish.BosmonMqtt {
}
}
public Boolean Enable {
get {
return this._enable;
}
set {
this._enable = value;
WriteConfiguration();
}
}
#endregion
#region IBosMonConfigurationTemplate Config-Variablen
public override void ReadConfiguration() {
base.ReadConfiguration();
@ -77,6 +92,7 @@ namespace BlubbFish.BosmonMqtt {
this._pass = this.Storage.ReadString("pass", "");
this._topic = this.Storage.ReadString("topic", "");
this._port = this.Storage.ReadInteger("port", 1883);
this._enable = this.Storage.ReadBoolean("enable", false);
}
public override void WriteConfiguration() {
this.Storage.Write("server", this._server);
@ -84,9 +100,11 @@ namespace BlubbFish.BosmonMqtt {
this.Storage.Write("pass", this._pass);
this.Storage.Write("topic", this._topic);
this.Storage.Write("port", this._port);
this.Storage.Write("enable", this._enable);
base.WriteConfiguration();
}
#endregion
#region IBosMonConfigurationTemplate Member
public override TKeyVal<String>[] StringVariables {
get {
@ -103,10 +121,17 @@ namespace BlubbFish.BosmonMqtt {
public override TKeyVal<Int32>[] IntVariables {
get {
return Merge(new TKeyVal<Int32>[] {
new TKeyVal<Int32>("port",1883)
new TKeyVal<Int32>("port", 1883)
}, base.IntVariables);
}
}
public override TKeyVal<Boolean>[] BoolVariables {
get {
return Merge(new TKeyVal<Boolean>[] {
new TKeyVal<Boolean>("enable", false)
}, base.BoolVariables);
}
}
public override TKeyVal<Boolean>[] Sections {
get {
return new TKeyVal<Boolean>[] {
@ -115,6 +140,10 @@ namespace BlubbFish.BosmonMqtt {
}
}
public void Saved() {
this.ConfigChanged?.Invoke(this, new EventArgs());
}

View File

@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using BlubbFish.BosmonMqtt.Models;
using BosMon.Data;
using BosMon.Data.Telegrams;
using BosMon.Prefs;
using BosMon.Plugins;
using BosMon.Utils;
using TelegramFilter.Filter;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
@ -11,16 +14,78 @@ using uPLibrary.Networking.M2Mqtt.Messages;
namespace BlubbFish.BosmonMqtt {
class MqttEventProcessor : IDisposable {
private PluginConfiguration pluginconfig;
private IBosMonHost pluginhost;
private MqttClient client;
private FilterList filter;
private List<PocsagFilterItem> pfilter = new List<PocsagFilterItem>();
Thread connectionWatcher;
public MqttEventProcessor(PluginConfiguration pluginconfiguration) {
public MqttEventProcessor(PluginConfiguration pluginconfiguration, IBosMonHost pluginHost) {
this.pluginconfig = pluginconfiguration;
this.pluginhost = pluginHost;
this.Connect();
if(this.pluginconfig.Enable) {
this.ConnectionWatcherStart();
}
this.pluginconfig.ConfigChanged += this.Pluginconfig_ConfigChanged;
}
private void ConnectionWatcherRunner() {
while(true) {
try {
Thread.Sleep(10000);
if(!this.client.IsConnected) {
this.Reconnect();
}
} catch(Exception) { }
}
}
private void ConnectionWatcherStart() {
if(this.connectionWatcher == null) {
this.connectionWatcher = new Thread(this.ConnectionWatcherRunner);
}
if(this.connectionWatcher.ThreadState == ThreadState.Running || this.connectionWatcher.ThreadState == ThreadState.WaitSleepJoin) {
return;
}
this.connectionWatcher.Start();
this.Log("ConnectionWatcherStart", "Überwachungsthread gestartet.");
}
private void ConnectionWatcherStop() {
if(this.connectionWatcher == null) {
return;
}
try {
this.connectionWatcher.Abort();
Thread.Sleep(100);
this.connectionWatcher = null;
} catch (Exception) { }
this.Log("ConnectionWatcherStop", "Überwachungsthread gestoppt.");
}
private void Pluginconfig_ConfigChanged(Object sender, EventArgs e) {
this.Reconnect();
if (this.pluginconfig.Enable) {
this.ConnectionWatcherStart();
}
if(!this.pluginconfig.Enable) {
this.ConnectionWatcherStop();
}
}
private void Reconnect() {
if (this.pluginconfig.Enable) {
this.Log("Reconnect", "Reconnect ausgelößt.");
}
this.Disconnect();
this.Connect();
}
public void Connect() {
private void Connect() {
if (this.pluginconfig.Enable) {
try {
this.client = new MqttClient(this.pluginconfig.Server, this.pluginconfig.Port, false, null);
if (this.pluginconfig.User == "" || this.pluginconfig.Password == "") {
@ -30,30 +95,50 @@ namespace BlubbFish.BosmonMqtt {
}
this.client.Subscribe(new String[] { this.pluginconfig.Topic }, new Byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
} catch (Exception) {
return;
}
this.filter = new FilterList(this.pluginconfig.PluginStorage.GetSection("filter"));
this.pfilter.Clear();
foreach (FilterItem item in this.filter) {
if(item.GetType() == typeof(PocsagFilterItem)) {
if (item.GetType() == typeof(PocsagFilterItem)) {
this.pfilter.Add(item as PocsagFilterItem);
}
}
this.Log("Connect", "Erfolgreich verbunden.");
}
}
private void Disconnect() {
this.pfilter.Clear();
if (this.client != null && this.client.IsConnected) {
this.client.Unsubscribe(new String[] { this.pluginconfig.Topic });
this.client.Disconnect();
this.Log("Disconnect", "Verbindung getrennt.");
}
this.client = null;
}
internal void TelegramEvent(Object sender, TelegramEventArgs e) {
if (this.pluginconfig.Enable) {
try {
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)) {
if ((item.IsMatching(p) && item.Negated)) {
return;
}
}
this.client.Publish(this.pluginconfig.Topic + p.Address+p.Func, Encoding.UTF8.GetBytes(p.Msg));
this.client.Publish(this.pluginconfig.Topic + p.Address + p.Func, Encoding.UTF8.GetBytes(p.Msg));
}
}
} catch (Exception) { }
}
}
private void Log(String method, String text) {
BosMonLog.Logger.WriteLine(LogSeverity.Info, "BosmonMqtt.MqttEventProcessor", method, text);
this.pluginhost.AddMessage(DateTime.Now.Ticks, MessageType.Message, "BosmonMqtt " + text);
}
#region IDisposable Support
@ -63,7 +148,8 @@ namespace BlubbFish.BosmonMqtt {
protected virtual void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
// TODO: verwalteten Zustand (verwaltete Objekte) entsorgen.
this.ConnectionWatcherStop();
this.Disconnect();
}
this.disposedValue = true;
}

View File

@ -1,4 +1,5 @@
using System;
using BlubbFish.BosmonMqtt.Models;
using BlubbFish.BosmonMqtt.Views;
using BosMon.Data.Processors;
using BosMon.Exceptions;
@ -28,7 +29,7 @@ namespace BlubbFish.BosmonMqtt {
this.pluginconfiguration = new PluginConfiguration(this.ConfigurationStorage);
this.isInitialized = true;
this._configView = new ConfigView(this.pluginconfiguration);
this._mqtteventprocessor = new MqttEventProcessor(this.pluginconfiguration);
this._mqtteventprocessor = new MqttEventProcessor(this.pluginconfiguration, this.PluginHost);
}
internal void Start() {

View File

@ -38,6 +38,7 @@
this.label5 = new System.Windows.Forms.Label();
this.textTopic = new System.Windows.Forms.TextBox();
this.filterButton = new System.Windows.Forms.Button();
this.boxEnable = new System.Windows.Forms.CheckBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
@ -113,7 +114,7 @@
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.Location = new System.Drawing.Point(3, 30);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(390, 250);
this.groupBox1.TabIndex = 7;
@ -196,18 +197,31 @@
this.filterButton.UseVisualStyleBackColor = true;
this.filterButton.Click += new System.EventHandler(this.Filter_Click);
//
// boxEnable
//
this.boxEnable.AutoSize = true;
this.boxEnable.Location = new System.Drawing.Point(3, 7);
this.boxEnable.Name = "boxEnable";
this.boxEnable.Size = new System.Drawing.Size(218, 17);
this.boxEnable.TabIndex = 8;
this.boxEnable.Text = "Aktiviert oder Deaktiviert das Mqtt-Plugin";
this.boxEnable.UseVisualStyleBackColor = true;
this.boxEnable.CheckedChanged += new System.EventHandler(this.Enabled_Changed);
//
// 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.boxEnable);
this.Controls.Add(this.groupBox1);
this.Name = "ConfigView";
this.Size = new System.Drawing.Size(396, 256);
this.Size = new System.Drawing.Size(396, 283);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
@ -228,5 +242,6 @@
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox textPort;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.CheckBox boxEnable;
}
}

View File

@ -3,7 +3,8 @@ using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using BosMon.Prefs;
using BlubbFish.BosmonMqtt.Models;
using BosMon.Gui;
using BosMon.Utils;
using TelegramFilter.Filter;
using TelegramFilter.Gui;
@ -11,19 +12,19 @@ using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace BlubbFish.BosmonMqtt.Views {
internal partial class ConfigView : UserControl {
internal partial class ConfigView : UserControl, IPreferencesUserControl {
private PluginConfiguration pluginconfig;
private EditFilterListDlg filter;
private String _server;
private String _port;
private String _user;
private String _pass;
private String _topic;
private Boolean _enable;
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) {
@ -31,30 +32,47 @@ namespace BlubbFish.BosmonMqtt.Views {
}
private void Settings_Changed(Object sender, EventArgs e) {
TextBox t = (TextBox)sender;
TextBox t = sender as TextBox;
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;
}));
this._server = t.Text;
break;
case "port":
if (Int32.TryParse(t.Text, out Int32 port)) {
this.pluginconfig.Port = port;
this._port = t.Text;
break;
case "user":
this._user = t.Text;
break;
case "pass":
this._pass = t.Text;
break;
case "topic":
this._topic = t.Text;
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 + "/";
}
t.BeginInvoke((Action)(() => {
t.Text = this._topic;
}));
break;
}
}
private void Enabled_Changed(Object sender, EventArgs e) {
CheckBox t = sender as CheckBox;
this._enable = t.Checked;
this.boxEnable.BeginInvoke((Action)(() => {
this.textServer.Enabled = this._enable;
this.textPort.Enabled = this._enable;
this.textUser.Enabled = this._enable;
this.textPass.Enabled = this._enable;
this.textTopic.Enabled = this._enable;
this.filterButton.Enabled = this._enable;
this.testButton.Enabled = this._enable;
}));
}
private void Check_Numbers(Object sender, KeyPressEventArgs e) {
e.Handled = !Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar);
@ -66,23 +84,29 @@ namespace BlubbFish.BosmonMqtt.Views {
this.labelStatus.Text = "Baue Verbindung auf.";
this.labelStatus.ForeColor = Color.DarkOrange;
}));
MqttClient client = new MqttClient(this.pluginconfig.Server, this.pluginconfig.Port, false, null);
if (!Int32.TryParse(this._port, out Int32 port)) {
this.labelStatus.BeginInvoke((Action)(() => {
this.labelStatus.Text = "Fehler beim Parsen des Ports.";
}));
throw new Exception();
}
MqttClient client = new MqttClient(this._server, 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);
client.Connect("bosmon-" + Guid.NewGuid().ToString(), this._user, this._pass);
}
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 });
client.Subscribe(new String[] { this._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"));
client.Publish(this._topic, Encoding.UTF8.GetBytes("BosMon Connection Test"));
this.labelStatus.BeginInvoke((Action)(() => {
this.labelStatus.Text = "Warte auf Testnachricht.";
this.labelStatus.ForeColor = Color.DarkOrange;
@ -113,5 +137,58 @@ namespace BlubbFish.BosmonMqtt.Views {
this._testMessageRecieved = true;
}
}
public void LoadSettings() {
this.filter = new EditFilterListDlg(new FilterList(this.pluginconfig.PluginStorage.GetSection("filter")));
this._server = this.pluginconfig.Server;
this._port = this.pluginconfig.Port.ToString();
this._user = this.pluginconfig.User;
this._pass = this.pluginconfig.Password;
this._topic = this.pluginconfig.Topic;
this._enable = this.pluginconfig.Enable;
this.textServer.BeginInvoke((Action)(() => {
this.textServer.Text = this._server;
}));
this.textPort.BeginInvoke((Action)(() => {
this.textPort.Text = this._port;
}));
this.textUser.BeginInvoke((Action)(() => {
this.textUser.Text = this._user;
}));
this.textPass.BeginInvoke((Action)(() => {
this.textPass.Text = this._pass;
}));
this.textTopic.BeginInvoke((Action)(() => {
this.textTopic.Text = this._topic;
}));
this.boxEnable.BeginInvoke((Action)(() => {
this.boxEnable.Checked = this._enable;
this.textServer.Enabled = this._enable;
this.textPort.Enabled = this._enable;
this.textUser.Enabled = this._enable;
this.textPass.Enabled = this._enable;
this.textTopic.Enabled = this._enable;
this.filterButton.Enabled = this._enable;
this.testButton.Enabled = this._enable;
}));
}
public Boolean SaveSettings() {
if (Int32.TryParse(this._port, out Int32 port)) {
this.pluginconfig.Port = port;
} else {
return false;
}
this.pluginconfig.Server = this._server;
this.pluginconfig.User = this._user;
this.pluginconfig.Password = this._pass;
this.pluginconfig.Topic = this._topic;
this.pluginconfig.Enable = this._enable;
this.pluginconfig.Saved();
return true;
}
public void ResetSettings() { }
}
}

Binary file not shown.