[NF] Some nice tricks
[NF] First working version
This commit is contained in:
parent
59d1f15bc2
commit
cfa9815d77
@ -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
|
||||
|
54
Mqtt-Dashboard/Connector/Telegram.cs
Normal file
54
Mqtt-Dashboard/Connector/Telegram.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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<String, String> 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<String, String> 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<String, String> 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<String, String>) }).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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user