From cfa9815d774e337406641938157bc5894ec916b7 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Thu, 7 Sep 2017 15:00:28 +0000 Subject: [PATCH] [NF] Some nice tricks [NF] First working version --- Mqtt-Dashboard/Connector/Mqtt.cs | 5 +- Mqtt-Dashboard/Connector/Telegram.cs | 54 ++++++++++++++++++++ Mqtt-Dashboard/Sensor/ASensor.cs | 75 +++++++++++++++++++--------- 3 files changed, 109 insertions(+), 25 deletions(-) create mode 100644 Mqtt-Dashboard/Connector/Telegram.cs diff --git a/Mqtt-Dashboard/Connector/Mqtt.cs b/Mqtt-Dashboard/Connector/Mqtt.cs index f19f792..f0e0dcf 100644 --- a/Mqtt-Dashboard/Connector/Mqtt.cs +++ b/Mqtt-Dashboard/Connector/Mqtt.cs @@ -12,9 +12,11 @@ namespace Dashboard.Connector { public delegate void MqttMessage(Object sender, MqttMsgPublishEventArgs e); public event MqttMessage MessageIncomming; + public event MqttMessage MessageSending; private Mqtt() { this.client = new MqttClient(InIReader.GetInstance("settings.ini").GetValue("general", "mqtt-server")); + Connect(); } public static Mqtt Instance { get { @@ -24,7 +26,7 @@ namespace Dashboard.Connector { return instance; } } - public void Connect() { + private void Connect() { this.client.MqttMsgPublishReceived += this.Client_MqttMsgPublishReceived; this.client.Connect(Guid.NewGuid().ToString()); this.client.Subscribe(new String[] { "#" }, new Byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE }); @@ -36,6 +38,7 @@ namespace Dashboard.Connector { public void Send(String topic, String data) { this.client.Publish(topic, Encoding.UTF8.GetBytes(data)); + this.MessageSending?.Invoke(this, new MqttMsgPublishEventArgs(topic, Encoding.UTF8.GetBytes(data), false, 0, false)); } #region IDisposable Support diff --git a/Mqtt-Dashboard/Connector/Telegram.cs b/Mqtt-Dashboard/Connector/Telegram.cs new file mode 100644 index 0000000..d583272 --- /dev/null +++ b/Mqtt-Dashboard/Connector/Telegram.cs @@ -0,0 +1,54 @@ +using BlubbFish.Utils; +using System; +using Telegram.Bot; +using Telegram.Bot.Args; +using Telegram.Bot.Exceptions; +using Telegram.Bot.Types; + +namespace MqttToTelegram { + class Telegram { + private static Telegram instance; + private TelegramBotClient bot; + private ChatId chat; + + public delegate void TelegramMessage(Object sender, Message e); + + public event TelegramMessage MessageIncomming; + public event TelegramMessage MessageSending; + + private Telegram() { + bot = new TelegramBotClient(InIReader.GetInstance("settings.ini").GetValue("general", "telegram-key")); + bot.OnMessage += Bot_OnMessage; + this.Connect(); + } + + private void Bot_OnMessage(Object sender, MessageEventArgs e) { + this.MessageIncomming?.Invoke(this, e.Message); + } + + public static Telegram Instance { + get { + if(instance == null) { + instance = new Telegram(); + } + return instance; + } + } + + private void Connect() { + bot.StartReceiving(); + this.chat = new ChatId(InIReader.GetInstance("settings.ini").GetValue("general", "chatid")); + } + + public async void Send(String text) { + try { + Message x = await this.bot.SendTextMessageAsync(this.chat, text); + this.MessageSending?.Invoke(this, x); + } catch(ApiRequestException e) { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(e.Message+" "+e.ErrorCode+" "+e.Parameters); + Console.ForegroundColor = ConsoleColor.White; + } + } + } +} diff --git a/Mqtt-Dashboard/Sensor/ASensor.cs b/Mqtt-Dashboard/Sensor/ASensor.cs index 4cbe6ef..e5b9aa4 100644 --- a/Mqtt-Dashboard/Sensor/ASensor.cs +++ b/Mqtt-Dashboard/Sensor/ASensor.cs @@ -3,21 +3,15 @@ using System; using uPLibrary.Networking.M2Mqtt.Messages; using System.Collections.Generic; using System.Linq; +using System.Threading; namespace Dashboard.Sensor { public abstract class ASensor : IDisposable { private String topic; private Int32 pollcount; private Dictionary settings; - public enum Types { - Bool, - Int, - Float - } + private Thread updateThread; - public delegate void UpdatedValue(Object sender, EventArgs e); - public event UpdatedValue Update; - public ASensor(Dictionary settings) { this.GetBool = true; this.GetFloat = 0.0f; @@ -27,7 +21,17 @@ namespace Dashboard.Sensor { this.pollcount = this.Polling; this.settings = settings; Mqtt.Instance.MessageIncomming += this.IncommingMqttMessage; + this.updateThread = new Thread(this.SensorPolling); + this.updateThread.Start(); } + + private void SensorPolling() { + while(true) { + Thread.Sleep(1000); + this.Poll(); + } + } + private void IncommingMqttMessage(Object sender, MqttMsgPublishEventArgs e) { if(e.Topic == this.topic) { this.UpdateValue(e); @@ -35,8 +39,6 @@ namespace Dashboard.Sensor { this.Update?.Invoke(this, e); } } - protected abstract void UpdateValue(MqttMsgPublishEventArgs e); - public Int32 GetInt { get; protected set; } internal static ASensor GetInstance(String v, Dictionary dictionary) { string object_sensor = "Dashboard.Sensor." + char.ToUpper(v[0]) + v.Substring(1); @@ -48,30 +50,55 @@ namespace Dashboard.Sensor { } return (ASensor)t.GetConstructor(new Type[] { typeof(Dictionary) }).Invoke(new object[] { dictionary }); } - - public Single GetFloat { get; protected set; } - public Boolean GetBool { get; protected set; } - public Types Datatypes { get; protected set; } - public DateTime Timestamp { get; protected set; } - public Int32 Polling { get; private set; } - public void Poll() { + + private void Poll() { if(this.pollcount++ >= this.Polling) { this.pollcount = 1; Mqtt.Instance.Send(this.topic + "/status",""); } } - protected virtual void Dispose(Boolean disposing) { - if (disposing) { - Mqtt.Instance.MessageIncomming -= this.IncommingMqttMessage; + + internal virtual void SetBool(Boolean v) { + Mqtt.Instance.Send(this.topic + "/set", v ? "on" : "off"); + } + + protected abstract void UpdateValue(MqttMsgPublishEventArgs e); + + public Single GetFloat { get; protected set; } + public Boolean GetBool { get; protected set; } + public Int32 GetInt { get; protected set; } + public Types Datatypes { get; protected set; } + public DateTime Timestamp { get; protected set; } + public Int32 Polling { get; private set; } + public enum Types { + Bool, + Int, + Float + } + public delegate void UpdatedValue(Object sender, EventArgs e); + public event UpdatedValue Update; + + #region IDisposable Support + private bool disposedValue = false; + protected virtual void Dispose(bool disposing) { + if(!disposedValue) { + if(disposing) { + this.updateThread.Abort(); + while(this.updateThread.ThreadState != ThreadState.Aborted) { } + Mqtt.Instance.MessageIncomming -= this.IncommingMqttMessage; + } + this.settings = null; + this.updateThread = null; + disposedValue = true; } } + ~ASensor() { + Dispose(false); + } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } - - internal virtual void SetBool(Boolean v) { - Mqtt.Instance.Send(this.topic + "/set", v ? "on" : "off"); - } + #endregion } } \ No newline at end of file