[NF] Ein paar verbesserungen

This commit is contained in:
BlubbFish 2017-09-26 17:55:25 +00:00
parent 7f0de5503f
commit ed92555d1e
12 changed files with 69 additions and 32 deletions

View File

@ -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<String, String> dictionary) {
String object_sensor = "Dashboard.Connector." + Char.ToUpper(dictionary["type"][0]) + dictionary["type"].Substring(1);
public static ADataBackend GetInstance(Dictionary<String, String> 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<String, String>) }).Invoke(new Object[] { dictionary });
return (ADataBackend)t.GetConstructor(new Type[] { typeof(Dictionary<String, String>) }).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() { }

View File

@ -11,6 +11,21 @@
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -118,6 +133,17 @@
<Name>Utils</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.5.2">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.5.2 %28x86 und x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -6,4 +6,14 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartArguments>-debug</StartArguments>
</PropertyGroup>
<PropertyGroup>
<PublishUrlHistory />
<InstallUrlHistory />
<SupportUrlHistory />
<UpdateUrlHistory />
<BootstrapperUrlHistory />
<ErrorReportUrlHistory />
<FallbackCulture>de-DE</FallbackCulture>
<VerifyUploadedFiles>false</VerifyUploadedFiles>
</PropertyGroup>
</Project>

View File

@ -9,10 +9,11 @@ using Dashboard.Tracings;
namespace Dashboard {
public partial class Dashboard : Form {
private Dictionary<String, ASensor> sensors = new Dictionary<String, ASensor>();
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<String> sensorini = ini.GetSections();
List<String> 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);
}

View File

@ -10,10 +10,9 @@ namespace Dashboard {
/// </summary>
[STAThread]
static void Main(String[] args) {
Dictionary<String, CmdArgs.VaildArguments> pargs = new Dictionary<String, CmdArgs.VaildArguments> {
CmdArgs.Instance.SetArguments(new Dictionary<String, CmdArgs.VaildArguments> {
{ "-debug", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Single) }
};
CmdArgs.Instance.SetArguments(pargs, args);
}, args);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Boolean debug = false;

View File

@ -10,10 +10,11 @@ namespace Dashboard.Sensor {
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) {
public ASensor(Dictionary<String, String> 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<String, String> dictionary, String name) {
String object_sensor = "Dashboard.Sensor." + Char.ToUpper(v[0]) + v.Substring(1);
internal static ASensor GetInstance(ADataBackend backend, Dictionary<String, String> 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<String, String>), typeof(String) }).Invoke(new Object[] { dictionary, name });
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;
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;

View File

@ -9,7 +9,7 @@ namespace Dashboard.Sensor {
class Flex4gridswitch : ASensor {
private String id;
public Flex4gridswitch(Dictionary<String, String> settings, String name) : base(settings, name) {
public Flex4gridswitch(Dictionary<String, String> 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'") + "\"}");
}
}
}

View File

@ -8,7 +8,7 @@ namespace Dashboard.Sensor {
class Flex4gridpower : ASensor {
private String id;
public Flex4gridpower(Dictionary<String, String> settings, String name) : base(settings, name) {
public Flex4gridpower(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
this.Datatypes = Types.Float;
this.id = settings["id"];
}

View File

@ -5,7 +5,7 @@ using Dashboard.Connector;
namespace Dashboard.Sensor {
class Power : ASensor {
public Power(Dictionary<String, String> settings, String name) : base(settings, name) {
public Power(Dictionary<String, String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
this.GetBool = true;
this.GetFloat = 0.0f;
this.GetInt = 0;

View File

@ -4,7 +4,7 @@ using Dashboard.Connector;
namespace Dashboard.Sensor {
class Switch : ASensor {
public Switch(Dictionary<System.String, System.String> settings, String name) : base(settings, name) {
public Switch(Dictionary<System.String, System.String> settings, String name, ADataBackend backend) : base(settings, name, backend) {
this.Datatypes = Types.Bool;
}

Binary file not shown.