BosmonMqtt/BosmonMqtt/Views/Config.cs
2018-09-06 20:17:26 +00:00

189 lines
6.7 KiB
C#

using System;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using BlubbFish.BosmonMqtt.Models;
using BosMon.Gui;
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, 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;
}
private void Filter_Click(Object sender, EventArgs e) {
this.filter.ShowDialog();
}
private void Settings_Changed(Object sender, EventArgs e) {
TextBox t = sender as TextBox;
switch (t.Tag) {
case "server":
this._server = t.Text;
break;
case "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;
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);
}
private void Setting_Test(Object sender, EventArgs e) {
try {
this.labelStatus.BeginInvoke((Action)(() => {
this.labelStatus.Text = "Baue Verbindung auf.";
this.labelStatus.ForeColor = Color.DarkOrange;
}));
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, null, MqttSslProtocols.None);
if (this.pluginconfig.User == "" || this.pluginconfig.Password == "") {
client.Connect("bosmon-" + Guid.NewGuid().ToString());
} else {
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._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._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;
}
}
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() { }
}
}