[1.3.0] Netcore default

This commit is contained in:
BlubbFish 2022-01-15 21:52:41 +01:00
parent a2e5ca6c7e
commit 2e6f2f464b
6 changed files with 179 additions and 143 deletions

View File

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

View File

@ -27,15 +27,12 @@ namespace BlubbFish.Utils.IoT.Connector {
Type t = Type.GetType(object_sensor, true); Type t = Type.GetType(object_sensor, true);
return (ABackend)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>) }).Invoke(new Object[] { settings }); return (ABackend)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>) }).Invoke(new Object[] { settings });
} catch (TypeLoadException) { } catch (TypeLoadException) {
Console.Error.WriteLine("Configuration: " + settings["type"] + " is not a " + ty.ToString() + "Backend"); throw new TypeLoadException("Configuration: " + settings["type"] + " is not a " + ty.ToString() + "Backend");
return null;
} catch (System.IO.FileNotFoundException) { } catch (System.IO.FileNotFoundException) {
Console.Error.WriteLine("Driver " + object_sensor + " could not load!"); throw new DllNotFoundException("Driver " + object_sensor + " could not load!");
return null;
} catch (Exception e) { } 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); protected void NotifyClientIncomming(BackendEvent value) => this.MessageIncomming?.Invoke(this, value);

View File

@ -1,131 +1,134 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events; using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
namespace BlubbFish.Utils.IoT.JsonSensor {
public abstract class AJsonSensor : IDisposable { namespace BlubbFish.Utils.IoT.JsonSensor {
protected String topic; public abstract class AJsonSensor : IDisposable {
protected Int32 pollcount; protected String topic;
protected Dictionary<String, String> settings; protected Int32 pollcount;
protected ABackend backend; protected Dictionary<String, String> settings;
private Thread pollingThread; protected ABackend backend;
private Boolean pollEnabled = false; private Thread pollingThread;
private Boolean pollEnabled = false;
public AJsonSensor(Dictionary<String, String> settings, String name, ABackend backend) {
this.GetBool = true; public AJsonSensor(Dictionary<String, String> settings, String name, ABackend backend) {
this.GetFloat = 0.0f; this.GetBool = true;
this.GetInt = 0; this.GetFloat = 0.0f;
this.topic = settings.Keys.Contains("topic") ? settings["topic"] : ""; this.GetInt = 0;
this.settings = settings; this.topic = settings.Keys.Contains("topic") ? settings["topic"] : "";
this.Title = settings.Keys.Contains("title") ? settings["title"] : ""; this.settings = settings;
this.Name = name; this.Title = settings.Keys.Contains("title") ? settings["title"] : "";
this.backend = backend; this.Name = name;
this.backend.MessageIncomming += this.IncommingMqttMessage; this.backend = backend;
if (settings.Keys.Contains("polling")) { this.backend.MessageIncomming += this.IncommingMqttMessage;
this.pollEnabled = true; if (settings.Keys.Contains("polling")) {
this.Polling = Int32.Parse(settings["polling"]); this.pollEnabled = true;
this.pollcount = this.Polling; this.Polling = Int32.Parse(settings["polling"]);
this.pollingThread = new Thread(this.SensorPolling); this.pollcount = this.Polling;
this.pollingThread.Start(); this.pollingThread = new Thread(this.SensorPolling);
} this.pollingThread.Start();
} }
}
private void SensorPolling() {
while(this.pollEnabled) { private void SensorPolling() {
Thread.Sleep(1000); while(this.pollEnabled) {
this.Poll(); Thread.Sleep(1000);
} this.Poll();
} }
}
private void IncommingMqttMessage(Object sender, BackendEvent e) {
if(Regex.Match(e.From.ToString(), this.topic).Success) { private void IncommingMqttMessage(Object sender, BackendEvent e) {
if (this.UpdateValue(e)) { if(Regex.Match(e.From.ToString(), this.topic).Success) {
this.Timestamp = DateTime.Now; if (this.UpdateValue(e)) {
this.Update?.Invoke(this, e); this.Timestamp = DateTime.Now;
} this.Update?.Invoke(this, e);
} }
} }
}
public static AJsonSensor GetInstance(Dictionary<String, ABackend> backends, Dictionary<String, String> settings, String name) {
String object_sensor = "BlubbFish.Utils.IoT.JsonSensor." + Char.ToUpper(settings["type"][0]) + settings["type"].Substring(1).ToLower(); public static AJsonSensor GetInstance(Dictionary<String, ABackend> backends, Dictionary<String, String> settings, String name) {
Type t; if(!settings.ContainsKey("backend") || !backends.ContainsKey(settings["backend"])) {
try { throw new ArgumentException("Backend not specified!");
t = Type.GetType(object_sensor, true); }
} catch(TypeLoadException) { String object_sensor = "BlubbFish.Utils.IoT.JsonSensor." + settings["type"].ToUpperLower();
throw new ArgumentException("Sensor: " + object_sensor + " is not a Sensor"); try {
} Type t = Type.GetType(object_sensor, true);
if(!settings.ContainsKey("backend") || !backends.ContainsKey(settings["backend"])) { return (AJsonSensor)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>), typeof(String), typeof(ABackend) }).Invoke(new Object[] { settings, name, backends[settings["backend"]] });
throw new ArgumentException("Backend not specified!"); } catch(TypeLoadException) {
} throw new ArgumentException("Sensor: " + object_sensor + " is not a Sensor");
return (AJsonSensor)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>), typeof(String), typeof(ABackend) }).Invoke(new Object[] { settings, name, backends[settings["backend"]] }); } 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) { protected virtual void Poll() {
((ADataBackend)this.backend).Send(this.topic + "/get", ""); 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) { }
((ADataBackend)this.backend).Send(this.topic + "/set", v ? "on" : "off");
} 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; } protected abstract Boolean UpdateValue(BackendEvent e);
public Int32 GetInt { get; protected set; }
public Types Datatypes { get; protected set; } public Single GetFloat { get; protected set; }
public DateTime Timestamp { get; protected set; } public Boolean GetBool { get; protected set; }
public Int32 Polling { get; private set; } public Int32 GetInt { get; protected set; }
public String Title { get; protected set; } public Types Datatypes { get; protected set; }
public String Name { get; internal set; } public DateTime Timestamp { get; protected set; }
public Int32 Polling { get; private set; }
public enum Types { public String Title { get; protected set; }
Bool, public String Name { get; internal set; }
Int,
Float public enum Types {
} Bool,
public delegate void UpdatedValue(Object sender, EventArgs e); Int,
public event UpdatedValue Update; Float
}
#region IDisposable Support public delegate void UpdatedValue(Object sender, EventArgs e);
private Boolean disposedValue = false; public event UpdatedValue Update;
#region IDisposable Support
protected virtual void Dispose(Boolean disposing) { private Boolean disposedValue = false;
if(!this.disposedValue) {
if(disposing) {
this.pollEnabled = false; protected virtual void Dispose(Boolean disposing) {
if (this.pollingThread != null && this.pollingThread.ThreadState == ThreadState.Running) { if(!this.disposedValue) {
this.pollingThread.Abort(); if(disposing) {
while (this.pollingThread.ThreadState != ThreadState.Aborted) { } this.pollEnabled = false;
} if (this.pollingThread != null && this.pollingThread.ThreadState == ThreadState.Running) {
this.backend.MessageIncomming -= this.IncommingMqttMessage; this.pollingThread.Abort();
} while (this.pollingThread.ThreadState != ThreadState.Aborted) { }
this.settings = null; }
this.pollingThread = null; this.backend.MessageIncomming -= this.IncommingMqttMessage;
this.disposedValue = true; }
} this.settings = null;
} this.pollingThread = null;
this.disposedValue = true;
~AJsonSensor() { }
this.Dispose(false); }
}
~AJsonSensor() {
public void Dispose() { this.Dispose(false);
this.Dispose(true); }
GC.SuppressFinalize(this);
} public void Dispose() {
#endregion this.Dispose(true);
} GC.SuppressFinalize(this);
}
#endregion
}
} }

View File

@ -8,7 +8,7 @@ namespace BlubbFish.Utils.IoT.JsonSensor {
public Pir(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) => this.Datatypes = Types.Bool; public Pir(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) => this.Datatypes = Types.Bool;
protected override Boolean UpdateValue(BackendEvent e) { protected override Boolean UpdateValue(BackendEvent e) {
this.GetBool = (e.Message.ToLower() == "on") ? true : false; this.GetBool = e.Message.ToLower() == "on";
return true; return true;
} }
} }

View File

@ -8,7 +8,7 @@ namespace BlubbFish.Utils.IoT.JsonSensor {
public Switch(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) => this.Datatypes = Types.Bool; public Switch(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) => this.Datatypes = Types.Bool;
protected override Boolean UpdateValue(BackendEvent e) { protected override Boolean UpdateValue(BackendEvent e) {
this.GetBool = (e.Message.ToLower() == "on") ? true : false; this.GetBool = e.Message.ToLower() == "on";
return true; return true;
} }
} }

View File

@ -4,19 +4,22 @@
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>BlubbFish.Utils.IoT</RootNamespace> <RootNamespace>BlubbFish.Utils.IoT</RootNamespace>
<AssemblyName>Utils-IoT</AssemblyName> <AssemblyName>Utils-IoT</AssemblyName>
<AssemblyVersion>1.0.0</AssemblyVersion>
<NeutralLanguage>de-DE</NeutralLanguage> <NeutralLanguage>de-DE</NeutralLanguage>
<PackageReleaseNotes>1.0.0 Init</PackageReleaseNotes> <PackageReleaseNotes>
<Copyright>Copyright © BlubbFish 2017 - 24.11.2019</Copyright> 1.3.0 Netcore default
1.2.0 Refactoring
1.1.0 Readme
1.0.0.0 Init
</PackageReleaseNotes>
<Copyright>Copyright © BlubbFish 2017 - 15.01.2022</Copyright>
<Description>Provides classes for iot development</Description> <Description>Provides classes for iot development</Description>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<Company>BlubbFish</Company> <Company>BlubbFish</Company>
<Authors>BlubbFish</Authors> <Authors>BlubbFish</Authors>
<FileVersion>1.0.0</FileVersion>
<PackageProjectUrl>http://git.blubbfish.net/vs_utils/Utils-IoT</PackageProjectUrl> <PackageProjectUrl>http://git.blubbfish.net/vs_utils/Utils-IoT</PackageProjectUrl>
<RepositoryUrl>http://git.blubbfish.net/vs_utils/Utils-IoT.git</RepositoryUrl> <RepositoryUrl>http://git.blubbfish.net/vs_utils/Utils-IoT.git</RepositoryUrl>
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
<Version>1.0.0</Version> <Version>1.3.0</Version>
<PackageId>IoT.Utils.BlubbFish</PackageId> <PackageId>IoT.Utils.BlubbFish</PackageId>
</PropertyGroup> </PropertyGroup>