From 2e6f2f464bc783d371e5f63cacb4609700f58485 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Sat, 15 Jan 2022 21:52:41 +0100 Subject: [PATCH] [1.3.0] Netcore default --- Changelog.md | 33 ++++ Utils-IoT/Connector/ABackend.cs | 9 +- Utils-IoT/JsonSensor/AJsonSensor.cs | 263 ++++++++++++++-------------- Utils-IoT/JsonSensor/Pir.cs | 2 +- Utils-IoT/JsonSensor/Switch.cs | 2 +- Utils-IoT/Utils-IoT.csproj | 13 +- 6 files changed, 179 insertions(+), 143 deletions(-) diff --git a/Changelog.md b/Changelog.md index e69de29..8effd26 100644 --- a/Changelog.md +++ b/Changelog.md @@ -0,0 +1,33 @@ +# Changelog + +## 1.3.0 - 2022-01-15 - Netcore default +### New Features +* Netcore is now default +* ABackend thows exeption if loading failed +### Bugfixes +### Changes +* Make ref for github in readme +* Refactoring +* Codingstyles + +## 1.2.0 - 2019-12.01 - Refactoring +### New Features +* Add netcore +* ABackend now return null if type is not loadable +### Bugfixes +### Changes +* Refactoring + +## 1.1.0 - 2019-05-29 - Readme +### New Features +* Add Readme, Contribution and Licence +### Bugfixes +### Changes +* Coding Styles + +## 1.0.0.0 - 2019-02-14 - Init +### New Features +* First Version +### Bugfixes +### Changes + diff --git a/Utils-IoT/Connector/ABackend.cs b/Utils-IoT/Connector/ABackend.cs index 87981fd..cd68fe0 100644 --- a/Utils-IoT/Connector/ABackend.cs +++ b/Utils-IoT/Connector/ABackend.cs @@ -27,15 +27,12 @@ namespace BlubbFish.Utils.IoT.Connector { Type t = Type.GetType(object_sensor, true); return (ABackend)t.GetConstructor(new Type[] { typeof(Dictionary) }).Invoke(new Object[] { settings }); } catch (TypeLoadException) { - Console.Error.WriteLine("Configuration: " + settings["type"] + " is not a " + ty.ToString() + "Backend"); - return null; + throw new TypeLoadException("Configuration: " + settings["type"] + " is not a " + ty.ToString() + "Backend"); } catch (System.IO.FileNotFoundException) { - Console.Error.WriteLine("Driver " + object_sensor + " could not load!"); - return null; + throw new DllNotFoundException("Driver " + object_sensor + " could not load!"); } catch (Exception e) { - Console.Error.WriteLine("Something bad Happend while Loading Connectior: "+e.Message); + throw new Exception("Something bad Happend while Loading Connectior: "+e.Message); } - return null; } protected void NotifyClientIncomming(BackendEvent value) => this.MessageIncomming?.Invoke(this, value); diff --git a/Utils-IoT/JsonSensor/AJsonSensor.cs b/Utils-IoT/JsonSensor/AJsonSensor.cs index 5579293..d713631 100644 --- a/Utils-IoT/JsonSensor/AJsonSensor.cs +++ b/Utils-IoT/JsonSensor/AJsonSensor.cs @@ -1,131 +1,134 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text.RegularExpressions; -using System.Threading; -using BlubbFish.Utils.IoT.Connector; -using BlubbFish.Utils.IoT.Events; - -namespace BlubbFish.Utils.IoT.JsonSensor { - public abstract class AJsonSensor : IDisposable { - protected String topic; - protected Int32 pollcount; - protected Dictionary settings; - protected ABackend backend; - private Thread pollingThread; - private Boolean pollEnabled = false; - - public AJsonSensor(Dictionary settings, String name, ABackend backend) { - this.GetBool = true; - this.GetFloat = 0.0f; - this.GetInt = 0; - this.topic = settings.Keys.Contains("topic") ? settings["topic"] : ""; - this.settings = settings; - this.Title = settings.Keys.Contains("title") ? settings["title"] : ""; - this.Name = name; - this.backend = backend; - this.backend.MessageIncomming += this.IncommingMqttMessage; - if (settings.Keys.Contains("polling")) { - this.pollEnabled = true; - this.Polling = Int32.Parse(settings["polling"]); - this.pollcount = this.Polling; - this.pollingThread = new Thread(this.SensorPolling); - this.pollingThread.Start(); - } - } - - private void SensorPolling() { - while(this.pollEnabled) { - Thread.Sleep(1000); - this.Poll(); - } - } - - private void IncommingMqttMessage(Object sender, BackendEvent e) { - if(Regex.Match(e.From.ToString(), this.topic).Success) { - if (this.UpdateValue(e)) { - this.Timestamp = DateTime.Now; - this.Update?.Invoke(this, e); - } - } - } - - public static AJsonSensor GetInstance(Dictionary backends, Dictionary settings, String name) { - String object_sensor = "BlubbFish.Utils.IoT.JsonSensor." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower(); - Type t; - try { - t = Type.GetType(object_sensor, true); - } catch(TypeLoadException) { - throw new ArgumentException("Sensor: " + object_sensor + " is not a Sensor"); - } - if(!settings.ContainsKey("backend") || !backends.ContainsKey(settings["backend"])) { - throw new ArgumentException("Backend not specified!"); - } - return (AJsonSensor)t.GetConstructor(new Type[] { typeof(Dictionary), typeof(String), typeof(ABackend) }).Invoke(new Object[] { settings, name, backends[settings["backend"]] }); - } - - protected virtual void Poll() { - if(this.pollcount++ >= this.Polling) { - this.pollcount = 1; - if (this.backend is ADataBackend) { - ((ADataBackend)this.backend).Send(this.topic + "/get", ""); - } - } - } - - public virtual void SetBool(Boolean v) { - if (this.backend is ADataBackend) { - ((ADataBackend)this.backend).Send(this.topic + "/set", v ? "on" : "off"); - } - } - - protected abstract Boolean UpdateValue(BackendEvent 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 String Title { get; protected set; } - public String Name { get; internal set; } - - public enum Types { - Bool, - Int, - Float - } - public delegate void UpdatedValue(Object sender, EventArgs e); - public event UpdatedValue Update; - - #region IDisposable Support - private Boolean disposedValue = false; - - - protected virtual void Dispose(Boolean disposing) { - if(!this.disposedValue) { - if(disposing) { - this.pollEnabled = false; - if (this.pollingThread != null && this.pollingThread.ThreadState == ThreadState.Running) { - this.pollingThread.Abort(); - while (this.pollingThread.ThreadState != ThreadState.Aborted) { } - } - this.backend.MessageIncomming -= this.IncommingMqttMessage; - } - this.settings = null; - this.pollingThread = null; - this.disposedValue = true; - } - } - - ~AJsonSensor() { - this.Dispose(false); - } - - public void Dispose() { - this.Dispose(true); - GC.SuppressFinalize(this); - } - #endregion - } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using System.Threading; + +using BlubbFish.Utils.IoT.Connector; +using BlubbFish.Utils.IoT.Events; + +namespace BlubbFish.Utils.IoT.JsonSensor { + public abstract class AJsonSensor : IDisposable { + protected String topic; + protected Int32 pollcount; + protected Dictionary settings; + protected ABackend backend; + private Thread pollingThread; + private Boolean pollEnabled = false; + + public AJsonSensor(Dictionary settings, String name, ABackend backend) { + this.GetBool = true; + this.GetFloat = 0.0f; + this.GetInt = 0; + this.topic = settings.Keys.Contains("topic") ? settings["topic"] : ""; + this.settings = settings; + this.Title = settings.Keys.Contains("title") ? settings["title"] : ""; + this.Name = name; + this.backend = backend; + this.backend.MessageIncomming += this.IncommingMqttMessage; + if (settings.Keys.Contains("polling")) { + this.pollEnabled = true; + this.Polling = Int32.Parse(settings["polling"]); + this.pollcount = this.Polling; + this.pollingThread = new Thread(this.SensorPolling); + this.pollingThread.Start(); + } + } + + private void SensorPolling() { + while(this.pollEnabled) { + Thread.Sleep(1000); + this.Poll(); + } + } + + private void IncommingMqttMessage(Object sender, BackendEvent e) { + if(Regex.Match(e.From.ToString(), this.topic).Success) { + if (this.UpdateValue(e)) { + this.Timestamp = DateTime.Now; + this.Update?.Invoke(this, e); + } + } + } + + public static AJsonSensor GetInstance(Dictionary backends, Dictionary settings, String name) { + if(!settings.ContainsKey("backend") || !backends.ContainsKey(settings["backend"])) { + throw new ArgumentException("Backend not specified!"); + } + String object_sensor = "BlubbFish.Utils.IoT.JsonSensor." + settings["type"].ToUpperLower(); + try { + Type t = Type.GetType(object_sensor, true); + return (AJsonSensor)t.GetConstructor(new Type[] { typeof(Dictionary), typeof(String), typeof(ABackend) }).Invoke(new Object[] { settings, name, backends[settings["backend"]] }); + } catch(TypeLoadException) { + throw new ArgumentException("Sensor: " + object_sensor + " is not a Sensor"); + } catch(Exception e) { + Helper.WriteError("Something went wrong! " + e.Message); + throw e; + } + } + + protected virtual void Poll() { + if(this.pollcount++ >= this.Polling) { + this.pollcount = 1; + if (this.backend is ADataBackend databackend) { + databackend.Send(this.topic + "/get", ""); + } + } + } + + public virtual void SetBool(Boolean v) { + if (this.backend is ADataBackend databackend) { + databackend.Send(this.topic + "/set", v ? "on" : "off"); + } + } + + protected abstract Boolean UpdateValue(BackendEvent 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 String Title { get; protected set; } + public String Name { get; internal set; } + + public enum Types { + Bool, + Int, + Float + } + public delegate void UpdatedValue(Object sender, EventArgs e); + public event UpdatedValue Update; + + #region IDisposable Support + private Boolean disposedValue = false; + + + protected virtual void Dispose(Boolean disposing) { + if(!this.disposedValue) { + if(disposing) { + this.pollEnabled = false; + if (this.pollingThread != null && this.pollingThread.ThreadState == ThreadState.Running) { + this.pollingThread.Abort(); + while (this.pollingThread.ThreadState != ThreadState.Aborted) { } + } + this.backend.MessageIncomming -= this.IncommingMqttMessage; + } + this.settings = null; + this.pollingThread = null; + this.disposedValue = true; + } + } + + ~AJsonSensor() { + this.Dispose(false); + } + + public void Dispose() { + this.Dispose(true); + GC.SuppressFinalize(this); + } + #endregion + } } \ No newline at end of file diff --git a/Utils-IoT/JsonSensor/Pir.cs b/Utils-IoT/JsonSensor/Pir.cs index fc3a4e1..64d9677 100644 --- a/Utils-IoT/JsonSensor/Pir.cs +++ b/Utils-IoT/JsonSensor/Pir.cs @@ -8,7 +8,7 @@ namespace BlubbFish.Utils.IoT.JsonSensor { public Pir(Dictionary settings, String name, ADataBackend backend) : base(settings, name, backend) => this.Datatypes = Types.Bool; protected override Boolean UpdateValue(BackendEvent e) { - this.GetBool = (e.Message.ToLower() == "on") ? true : false; + this.GetBool = e.Message.ToLower() == "on"; return true; } } diff --git a/Utils-IoT/JsonSensor/Switch.cs b/Utils-IoT/JsonSensor/Switch.cs index 99b28ee..5f537ba 100644 --- a/Utils-IoT/JsonSensor/Switch.cs +++ b/Utils-IoT/JsonSensor/Switch.cs @@ -8,7 +8,7 @@ namespace BlubbFish.Utils.IoT.JsonSensor { public Switch(Dictionary settings, String name, ADataBackend backend) : base(settings, name, backend) => this.Datatypes = Types.Bool; protected override Boolean UpdateValue(BackendEvent e) { - this.GetBool = (e.Message.ToLower() == "on") ? true : false; + this.GetBool = e.Message.ToLower() == "on"; return true; } } diff --git a/Utils-IoT/Utils-IoT.csproj b/Utils-IoT/Utils-IoT.csproj index e5abd96..9936dce 100644 --- a/Utils-IoT/Utils-IoT.csproj +++ b/Utils-IoT/Utils-IoT.csproj @@ -4,19 +4,22 @@ netcoreapp3.1 BlubbFish.Utils.IoT Utils-IoT - 1.0.0 de-DE - 1.0.0 Init - Copyright © BlubbFish 2017 - 24.11.2019 + + 1.3.0 Netcore default + 1.2.0 Refactoring + 1.1.0 Readme + 1.0.0.0 Init + + Copyright © BlubbFish 2017 - 15.01.2022 Provides classes for iot development LICENSE BlubbFish BlubbFish - 1.0.0 http://git.blubbfish.net/vs_utils/Utils-IoT http://git.blubbfish.net/vs_utils/Utils-IoT.git git - 1.0.0 + 1.3.0 IoT.Utils.BlubbFish