[NF] Some nice tricks

[NF] First working version
This commit is contained in:
BlubbFish 2017-09-07 15:00:28 +00:00
parent 59d1f15bc2
commit cfa9815d77
3 changed files with 109 additions and 25 deletions

View File

@ -12,9 +12,11 @@ namespace Dashboard.Connector {
public delegate void MqttMessage(Object sender, MqttMsgPublishEventArgs e); public delegate void MqttMessage(Object sender, MqttMsgPublishEventArgs e);
public event MqttMessage MessageIncomming; public event MqttMessage MessageIncomming;
public event MqttMessage MessageSending;
private Mqtt() { private Mqtt() {
this.client = new MqttClient(InIReader.GetInstance("settings.ini").GetValue("general", "mqtt-server")); this.client = new MqttClient(InIReader.GetInstance("settings.ini").GetValue("general", "mqtt-server"));
Connect();
} }
public static Mqtt Instance { public static Mqtt Instance {
get { get {
@ -24,7 +26,7 @@ namespace Dashboard.Connector {
return instance; return instance;
} }
} }
public void Connect() { private void Connect() {
this.client.MqttMsgPublishReceived += this.Client_MqttMsgPublishReceived; this.client.MqttMsgPublishReceived += this.Client_MqttMsgPublishReceived;
this.client.Connect(Guid.NewGuid().ToString()); this.client.Connect(Guid.NewGuid().ToString());
this.client.Subscribe(new String[] { "#" }, new Byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE }); 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) { public void Send(String topic, String data) {
this.client.Publish(topic, Encoding.UTF8.GetBytes(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 #region IDisposable Support

View File

@ -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;
}
}
}
}

View File

@ -3,20 +3,14 @@ using System;
using uPLibrary.Networking.M2Mqtt.Messages; using uPLibrary.Networking.M2Mqtt.Messages;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
namespace Dashboard.Sensor { namespace Dashboard.Sensor {
public abstract class ASensor : IDisposable { public abstract class ASensor : IDisposable {
private String topic; private String topic;
private Int32 pollcount; private Int32 pollcount;
private Dictionary<String, String> settings; private Dictionary<String, String> settings;
public enum Types { private Thread updateThread;
Bool,
Int,
Float
}
public delegate void UpdatedValue(Object sender, EventArgs e);
public event UpdatedValue Update;
public ASensor(Dictionary<String, String> settings) { public ASensor(Dictionary<String, String> settings) {
this.GetBool = true; this.GetBool = true;
@ -27,7 +21,17 @@ namespace Dashboard.Sensor {
this.pollcount = this.Polling; this.pollcount = this.Polling;
this.settings = settings; this.settings = settings;
Mqtt.Instance.MessageIncomming += this.IncommingMqttMessage; 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) { private void IncommingMqttMessage(Object sender, MqttMsgPublishEventArgs e) {
if(e.Topic == this.topic) { if(e.Topic == this.topic) {
this.UpdateValue(e); this.UpdateValue(e);
@ -35,8 +39,6 @@ namespace Dashboard.Sensor {
this.Update?.Invoke(this, e); this.Update?.Invoke(this, e);
} }
} }
protected abstract void UpdateValue(MqttMsgPublishEventArgs e);
public Int32 GetInt { get; protected set; }
internal static ASensor GetInstance(String v, Dictionary<String, String> dictionary) { internal static ASensor GetInstance(String v, Dictionary<String, String> dictionary) {
string object_sensor = "Dashboard.Sensor." + char.ToUpper(v[0]) + v.Substring(1); string object_sensor = "Dashboard.Sensor." + char.ToUpper(v[0]) + v.Substring(1);
@ -49,29 +51,54 @@ namespace Dashboard.Sensor {
return (ASensor)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>) }).Invoke(new object[] { dictionary }); return (ASensor)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>) }).Invoke(new object[] { dictionary });
} }
public Single GetFloat { get; protected set; } private void Poll() {
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() {
if(this.pollcount++ >= this.Polling) { if(this.pollcount++ >= this.Polling) {
this.pollcount = 1; this.pollcount = 1;
Mqtt.Instance.Send(this.topic + "/status",""); Mqtt.Instance.Send(this.topic + "/status","");
} }
} }
protected virtual void Dispose(Boolean disposing) {
if (disposing) { internal virtual void SetBool(Boolean v) {
Mqtt.Instance.MessageIncomming -= this.IncommingMqttMessage; 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() { public void Dispose() {
Dispose(true); Dispose(true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
#endregion
internal virtual void SetBool(Boolean v) {
Mqtt.Instance.Send(this.topic + "/set", v ? "on" : "off");
}
} }
} }