diff --git a/Mqtt-Dashboard/Connector/ADataBackend.cs b/Mqtt-Dashboard/Connector/ADataBackend.cs index 39d2dbd..6f74838 100644 --- a/Mqtt-Dashboard/Connector/ADataBackend.cs +++ b/Mqtt-Dashboard/Connector/ADataBackend.cs @@ -2,28 +2,26 @@ using System.Collections.Generic; namespace Dashboard.Connector { - abstract class ADataBackend { + public abstract class ADataBackend { public abstract event MqttMessage MessageIncomming; public abstract event MqttMessage MessageSending; public delegate void MqttMessage(Object sender, MqttEventArgs e); - internal static void SetInstance(Dictionary dictionary) { - String object_sensor = "Dashboard.Connector." + Char.ToUpper(dictionary["type"][0]) + dictionary["type"].Substring(1); + public static ADataBackend GetInstance(Dictionary dictionary) { + String object_sensor = "Dashboard.Connector." + 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("settings.ini: " + dictionary["type"] + " is not a Connector"); } - Instance = (ADataBackend)t.GetConstructor(new Type[] { typeof(Dictionary) }).Invoke(new Object[] { dictionary }); + return (ADataBackend)t.GetConstructor(new Type[] { typeof(Dictionary) }).Invoke(new Object[] { dictionary }); } public abstract void Send(String topic, String data); public abstract void Dispose(); - - public static ADataBackend Instance { get; private set; } } public class MqttEventArgs : EventArgs { public MqttEventArgs() : base() { } diff --git a/Mqtt-Dashboard/Dashboard.csproj b/Mqtt-Dashboard/Dashboard.csproj index faece59..72b5428 100644 --- a/Mqtt-Dashboard/Dashboard.csproj +++ b/Mqtt-Dashboard/Dashboard.csproj @@ -11,6 +11,21 @@ v4.5.2 512 true + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true AnyCPU @@ -118,6 +133,17 @@ Utils - + + + False + Microsoft .NET Framework 4.5.2 %28x86 und x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + + \ No newline at end of file diff --git a/Mqtt-Dashboard/Dashboard.csproj.user b/Mqtt-Dashboard/Dashboard.csproj.user index d79bedf..0d2e8c8 100644 --- a/Mqtt-Dashboard/Dashboard.csproj.user +++ b/Mqtt-Dashboard/Dashboard.csproj.user @@ -6,4 +6,14 @@ -debug + + + + + + + + de-DE + false + \ No newline at end of file diff --git a/Mqtt-Dashboard/Form1.cs b/Mqtt-Dashboard/Form1.cs index c9b4e73..7b95b97 100644 --- a/Mqtt-Dashboard/Form1.cs +++ b/Mqtt-Dashboard/Form1.cs @@ -9,10 +9,11 @@ using Dashboard.Tracings; namespace Dashboard { public partial class Dashboard : Form { private Dictionary sensors = new Dictionary(); + private ADataBackend mqtt; public Dashboard(Boolean debug) { InitializeComponent(); - ADataBackend.SetInstance(InIReader.GetInstance("settings.ini").GetSection("mqtt")); + this.mqtt = ADataBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("mqtt")); this.GenerateSensors(); this.GenerateForms(); @@ -21,8 +22,8 @@ namespace Dashboard { this.SizeChanged += this.Dashboard_SizeChanged; if (debug) { - ADataBackend.Instance.MessageIncomming += this.Instance_MessageIncomming; - ADataBackend.Instance.MessageSending += this.Instance_MessageSending; + this.mqtt.MessageIncomming += this.Instance_MessageIncomming; + this.mqtt.MessageSending += this.Instance_MessageSending; } } @@ -32,9 +33,9 @@ namespace Dashboard { private void GenerateSensors() { InIReader ini = InIReader.GetInstance("sensor.ini"); - List sensorini = ini.GetSections(); + List sensorini = ini.GetSections(false); foreach(String sensor in sensorini) { - this.sensors.Add(sensor.ToLower().Substring(1, sensor.Length - 2), ASensor.GetInstance(ini.GetValue(sensor, "type").ToLower(), ini.GetSection(sensor), sensor.Substring(1, sensor.Length - 2))); + this.sensors.Add(sensor.ToLower(), ASensor.GetInstance(this.mqtt, ini.GetSection(sensor), sensor)); } } @@ -54,8 +55,9 @@ namespace Dashboard { item.Value.Dispose(); } this.sensors.Clear(); - ADataBackend.Instance.MessageIncomming -= this.Instance_MessageIncomming; - ADataBackend.Instance.Dispose(); + this.mqtt.MessageIncomming -= this.Instance_MessageIncomming; + this.mqtt.MessageSending -= this.Instance_MessageSending; + this.mqtt.Dispose(); this.Dispose(true); } diff --git a/Mqtt-Dashboard/Program.cs b/Mqtt-Dashboard/Program.cs index 845faf4..31f4000 100644 --- a/Mqtt-Dashboard/Program.cs +++ b/Mqtt-Dashboard/Program.cs @@ -10,10 +10,9 @@ namespace Dashboard { /// [STAThread] static void Main(String[] args) { - Dictionary pargs = new Dictionary { + CmdArgs.Instance.SetArguments(new Dictionary { { "-debug", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Single) } - }; - CmdArgs.Instance.SetArguments(pargs, args); + }, args); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Boolean debug = false; diff --git a/Mqtt-Dashboard/Sensor/ASensor.cs b/Mqtt-Dashboard/Sensor/ASensor.cs index 486a94b..ee7f7bc 100644 --- a/Mqtt-Dashboard/Sensor/ASensor.cs +++ b/Mqtt-Dashboard/Sensor/ASensor.cs @@ -10,10 +10,11 @@ namespace Dashboard.Sensor { protected String topic; protected Int32 pollcount; private Dictionary settings; + protected ADataBackend backend; private Thread updateThread; private Boolean pollEnabled = false; - public ASensor(Dictionary settings, String name) { + public ASensor(Dictionary settings, String name, ADataBackend backend) { this.GetBool = true; this.GetFloat = 0.0f; this.GetInt = 0; @@ -21,7 +22,8 @@ namespace Dashboard.Sensor { this.settings = settings; this.Title = (settings.Keys.Contains("title")) ? settings["title"] : ""; this.Name = name; - ADataBackend.Instance.MessageIncomming += this.IncommingMqttMessage; + this.backend = backend; + this.backend.MessageIncomming += this.IncommingMqttMessage; if (settings.Keys.Contains("polling")) { this.pollEnabled = true; this.Polling = Int32.Parse(settings["polling"]); @@ -47,26 +49,26 @@ namespace Dashboard.Sensor { } } - internal static ASensor GetInstance(String v, Dictionary dictionary, String name) { - String object_sensor = "Dashboard.Sensor." + Char.ToUpper(v[0]) + v.Substring(1); + internal static ASensor GetInstance(ADataBackend backend, Dictionary dictionary, String name) { + String object_sensor = "Dashboard.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: " + v + " is not a Sensor"); + throw new ArgumentException("sensor.ini: " + dictionary["type"] + " is not a Sensor"); } - return (ASensor)t.GetConstructor(new Type[] { typeof(Dictionary), typeof(String) }).Invoke(new Object[] { dictionary, name }); + return (ASensor)t.GetConstructor(new Type[] { typeof(Dictionary), typeof(String), typeof(ADataBackend) }).Invoke(new Object[] { dictionary, name, backend }); } protected virtual void Poll() { if(this.pollcount++ >= this.Polling) { this.pollcount = 1; - ADataBackend.Instance.Send(this.topic + "/status",""); + this.backend.Send(this.topic + "/status",""); } } internal virtual void SetBool(Boolean v) { - ADataBackend.Instance.Send(this.topic + "/set", v ? "on" : "off"); + this.backend.Send(this.topic + "/set", v ? "on" : "off"); } protected abstract Boolean UpdateValue(MqttEventArgs e); @@ -100,7 +102,7 @@ namespace Dashboard.Sensor { this.updateThread.Abort(); while (this.updateThread.ThreadState != ThreadState.Aborted) { } } - ADataBackend.Instance.MessageIncomming -= this.IncommingMqttMessage; + this.backend.MessageIncomming -= this.IncommingMqttMessage; } this.settings = null; this.updateThread = null; diff --git a/Mqtt-Dashboard/Sensor/Flex4GridSwitch.cs b/Mqtt-Dashboard/Sensor/Flex4GridSwitch.cs index 3ea9133..a476a4f 100644 --- a/Mqtt-Dashboard/Sensor/Flex4GridSwitch.cs +++ b/Mqtt-Dashboard/Sensor/Flex4GridSwitch.cs @@ -9,7 +9,7 @@ namespace Dashboard.Sensor { class Flex4gridswitch : ASensor { private String id; - public Flex4gridswitch(Dictionary settings, String name) : base(settings, name) { + public Flex4gridswitch(Dictionary settings, String name, ADataBackend backend) : base(settings, name, backend) { this.Datatypes = Types.Bool; this.id = settings["id"]; } @@ -40,13 +40,13 @@ namespace Dashboard.Sensor { if (this.pollcount++ >= this.Polling) { this.pollcount = 1; String hid = Regex.Match(this.topic, "/flex4grid/v1/households/([^/]*)/device/state").Groups[1].Value; - ADataBackend.Instance.Send("/flex4grid/v1/households/" + hid + "/devices/state/request", "{\"timestamp\": \"" + DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff'Z'") + "\"}"); + this.backend.Send("/flex4grid/v1/households/" + hid + "/devices/state/request", "{\"timestamp\": \"" + DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff'Z'") + "\"}"); } } internal override void SetBool(Boolean v) { String hid = Regex.Match(this.topic, "/flex4grid/v1/households/([^/]*)/device/state").Groups[1].Value; - ADataBackend.Instance.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'") + "\"}"); + 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'") + "\"}"); } } } \ No newline at end of file diff --git a/Mqtt-Dashboard/Sensor/Flex4gridPower.cs b/Mqtt-Dashboard/Sensor/Flex4gridPower.cs index 6fffc25..6cdae43 100644 --- a/Mqtt-Dashboard/Sensor/Flex4gridPower.cs +++ b/Mqtt-Dashboard/Sensor/Flex4gridPower.cs @@ -8,7 +8,7 @@ namespace Dashboard.Sensor { class Flex4gridpower : ASensor { private String id; - public Flex4gridpower(Dictionary settings, String name) : base(settings, name) { + public Flex4gridpower(Dictionary settings, String name, ADataBackend backend) : base(settings, name, backend) { this.Datatypes = Types.Float; this.id = settings["id"]; } diff --git a/Mqtt-Dashboard/Sensor/Power.cs b/Mqtt-Dashboard/Sensor/Power.cs index 6776032..e176b4f 100644 --- a/Mqtt-Dashboard/Sensor/Power.cs +++ b/Mqtt-Dashboard/Sensor/Power.cs @@ -5,7 +5,7 @@ using Dashboard.Connector; namespace Dashboard.Sensor { class Power : ASensor { - public Power(Dictionary settings, String name) : base(settings, name) { + public Power(Dictionary settings, String name, ADataBackend backend) : base(settings, name, backend) { this.GetBool = true; this.GetFloat = 0.0f; this.GetInt = 0; diff --git a/Mqtt-Dashboard/Sensor/Switch.cs b/Mqtt-Dashboard/Sensor/Switch.cs index 0c0e002..86b8969 100644 --- a/Mqtt-Dashboard/Sensor/Switch.cs +++ b/Mqtt-Dashboard/Sensor/Switch.cs @@ -4,7 +4,7 @@ using Dashboard.Connector; namespace Dashboard.Sensor { class Switch : ASensor { - public Switch(Dictionary settings, String name) : base(settings, name) { + public Switch(Dictionary settings, String name, ADataBackend backend) : base(settings, name, backend) { this.Datatypes = Types.Bool; } diff --git a/Mqtt-Dashboard/bin/Release/Dashboard.exe b/Mqtt-Dashboard/bin/Release/Dashboard.exe index 62e1981..7db5a4e 100644 Binary files a/Mqtt-Dashboard/bin/Release/Dashboard.exe and b/Mqtt-Dashboard/bin/Release/Dashboard.exe differ diff --git a/Mqtt-Dashboard/bin/Release/Utils.dll b/Mqtt-Dashboard/bin/Release/Utils.dll index 9647ff5..28f5d3d 100644 Binary files a/Mqtt-Dashboard/bin/Release/Utils.dll and b/Mqtt-Dashboard/bin/Release/Utils.dll differ