[NF] Aufgeräumt
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using BlubbFish.Utils.IoT.Connector;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Sensor {
|
||||
public abstract class ASensor : IDisposable {
|
||||
protected String topic;
|
||||
protected Int32 pollcount;
|
||||
private Dictionary<String, String> settings;
|
||||
protected ADataBackend backend;
|
||||
private Thread updateThread;
|
||||
private Boolean pollEnabled = false;
|
||||
|
||||
public ASensor(Dictionary<String, String> settings, String name, ADataBackend 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.updateThread = new Thread(this.SensorPolling);
|
||||
this.updateThread.Start();
|
||||
}
|
||||
}
|
||||
|
||||
private void SensorPolling() {
|
||||
while(this.pollEnabled) {
|
||||
Thread.Sleep(1000);
|
||||
this.Poll();
|
||||
}
|
||||
}
|
||||
|
||||
private void IncommingMqttMessage(Object sender, MqttEventArgs e) {
|
||||
if(Regex.Match(e.Topic, this.topic).Success) {
|
||||
if (this.UpdateValue(e)) {
|
||||
this.Timestamp = DateTime.Now;
|
||||
this.Update?.Invoke(this, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ASensor GetInstance(ADataBackend backend, Dictionary<String, String> dictionary, String name) {
|
||||
String object_sensor = "BlubbFish.Utils.IoT.Sensor." + Char.ToUpper(dictionary["type"][0]) + dictionary["type"].Substring(1).ToLower();
|
||||
Type t = null;
|
||||
try {
|
||||
t = Type.GetType(object_sensor, true);
|
||||
} catch(TypeLoadException) {
|
||||
throw new ArgumentException("sensor.ini: " + dictionary["type"] + " is not a Sensor");
|
||||
}
|
||||
return (ASensor)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>), typeof(String), typeof(ADataBackend) }).Invoke(new Object[] { dictionary, name, backend });
|
||||
}
|
||||
|
||||
protected virtual void Poll() {
|
||||
if(this.pollcount++ >= this.Polling) {
|
||||
this.pollcount = 1;
|
||||
this.backend.Send(this.topic + "/status","");
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SetBool(Boolean v) {
|
||||
this.backend.Send(this.topic + "/set", v ? "on" : "off");
|
||||
}
|
||||
|
||||
protected abstract Boolean UpdateValue(MqttEventArgs 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.updateThread != null && this.updateThread.ThreadState == ThreadState.Running) {
|
||||
this.updateThread.Abort();
|
||||
while (this.updateThread.ThreadState != ThreadState.Aborted) { }
|
||||
}
|
||||
this.backend.MessageIncomming -= this.IncommingMqttMessage;
|
||||
}
|
||||
this.settings = null;
|
||||
this.updateThread = null;
|
||||
this.disposedValue = true;
|
||||
}
|
||||
}
|
||||
~ASensor() {
|
||||
Dispose(false);
|
||||
}
|
||||
public void Dispose() {
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
using BlubbFish.Utils.IoT.Connector;
|
||||
using LitJson;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Sensor {
|
||||
class Flex4gridswitch : ASensor {
|
||||
private String id;
|
||||
|
||||
public Flex4gridswitch(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
|
||||
this.Datatypes = Types.Bool;
|
||||
this.id = settings["id"];
|
||||
}
|
||||
|
||||
protected override Boolean UpdateValue(MqttEventArgs e) {
|
||||
CultureInfo info = new CultureInfo("de-DE");
|
||||
info.NumberFormat.NumberDecimalSeparator = ".";
|
||||
CultureInfo.DefaultThreadCurrentCulture = info;
|
||||
CultureInfo.DefaultThreadCurrentUICulture = info;
|
||||
System.Threading.Thread.CurrentThread.CurrentCulture = info;
|
||||
System.Threading.Thread.CurrentThread.CurrentUICulture = info;
|
||||
try {
|
||||
JsonData data = JsonMapper.ToObject(e.Message);
|
||||
if (data.Keys.Contains("id") && data["id"].ToString() == this.id) {
|
||||
this.GetBool = data["status"].ToString() == "active" ? true : false;
|
||||
return true;
|
||||
}
|
||||
if(data.Keys.Contains(this.id)) {
|
||||
this.GetBool = data[this.id].ToString() == "active" ? true : false;
|
||||
return true;
|
||||
}
|
||||
} catch (Exception) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected override void Poll() {
|
||||
if (this.pollcount++ >= this.Polling) {
|
||||
this.pollcount = 1;
|
||||
String hid = Regex.Match(this.topic, "/flex4grid/v1/households/([^/]*)/device/state").Groups[1].Value;
|
||||
this.backend.Send("/flex4grid/v1/households/" + hid + "/devices/state/request", "{\"timestamp\": \"" + DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff'Z'") + "\"}");
|
||||
}
|
||||
}
|
||||
|
||||
public override void SetBool(Boolean v) {
|
||||
String hid = Regex.Match(this.topic, "/flex4grid/v1/households/([^/]*)/device/state").Groups[1].Value;
|
||||
this.backend.Send("/flex4grid/v1/households/" + hid + "/device/actuate", "{\"command\":\""+(v ? "ON" : "OFF") + "\",\"deviceId\":\""+ this.id + "\",\"timestamp\":\"" + DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff'Z'") + "\"}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using BlubbFish.Utils.IoT.Connector;
|
||||
using LitJson;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Sensor {
|
||||
class Flex4gridpower : ASensor {
|
||||
private String id;
|
||||
|
||||
public Flex4gridpower(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
|
||||
this.Datatypes = Types.Float;
|
||||
this.id = settings["id"];
|
||||
}
|
||||
|
||||
protected override Boolean UpdateValue(MqttEventArgs e) {
|
||||
CultureInfo info = new CultureInfo("de-DE");
|
||||
info.NumberFormat.NumberDecimalSeparator = ".";
|
||||
CultureInfo.DefaultThreadCurrentCulture = info;
|
||||
CultureInfo.DefaultThreadCurrentUICulture = info;
|
||||
System.Threading.Thread.CurrentThread.CurrentCulture = info;
|
||||
System.Threading.Thread.CurrentThread.CurrentUICulture = info;
|
||||
try {
|
||||
JsonData data = JsonMapper.ToObject(e.Message);
|
||||
if(data["id"].ToString() == this.id) {
|
||||
this.GetFloat = Single.Parse(data["power"].ToString());
|
||||
return true;
|
||||
}
|
||||
} catch (Exception) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using BlubbFish.Utils.IoT.Connector;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Sensor {
|
||||
class Luminanz : ASensor {
|
||||
public Luminanz(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
|
||||
this.GetBool = true;
|
||||
this.GetFloat = 0.0f;
|
||||
this.GetInt = 0;
|
||||
this.Datatypes = Types.Int;
|
||||
}
|
||||
|
||||
protected override Boolean UpdateValue(MqttEventArgs e) {
|
||||
this.GetInt = Int32.Parse(e.Message, new CultureInfo("en-US"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using BlubbFish.Utils.IoT.Connector;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Sensor {
|
||||
class Pir : ASensor {
|
||||
public Pir(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
|
||||
this.Datatypes = Types.Bool;
|
||||
}
|
||||
|
||||
protected override Boolean UpdateValue(MqttEventArgs e) {
|
||||
this.GetBool = (e.Message.ToLower() == "on") ? true : false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using BlubbFish.Utils.IoT.Connector;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Sensor {
|
||||
class Power : ASensor {
|
||||
public Power(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
|
||||
this.GetBool = true;
|
||||
this.GetFloat = 0.0f;
|
||||
this.GetInt = 0;
|
||||
this.Datatypes = Types.Float;
|
||||
}
|
||||
|
||||
protected override Boolean UpdateValue(MqttEventArgs e) {
|
||||
this.GetFloat = Single.Parse(e.Message, new CultureInfo("en-US"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using BlubbFish.Utils.IoT.Connector;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Sensor {
|
||||
class Switch : ASensor {
|
||||
public Switch(Dictionary<System.String, System.String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
|
||||
this.Datatypes = Types.Bool;
|
||||
}
|
||||
|
||||
protected override Boolean UpdateValue(MqttEventArgs e) {
|
||||
this.GetBool = (e.Message.ToLower() == "on") ? true : false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using BlubbFish.Utils.IoT.Connector;
|
||||
|
||||
namespace BlubbFish.Utils.IoT.Sensor {
|
||||
class Temperatur : ASensor {
|
||||
public Temperatur(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
|
||||
this.GetBool = true;
|
||||
this.GetFloat = 0.0f;
|
||||
this.GetInt = 0;
|
||||
this.Datatypes = Types.Float;
|
||||
}
|
||||
|
||||
protected override Boolean UpdateValue(MqttEventArgs e) {
|
||||
this.GetFloat = Single.Parse(e.Message, new CultureInfo("en-US"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user