[NF] Ein bisschen Aufgeräumt

This commit is contained in:
BlubbFish 2017-09-26 19:39:02 +00:00
parent 59bbdbfc5a
commit 345d14483b
9 changed files with 58 additions and 128 deletions

View File

@ -48,7 +48,7 @@ namespace Mqtt_SWB_Dashboard.Helper {
internal Int32 GetActive() { internal Int32 GetActive() {
Int32 ret = 0; Int32 ret = 0;
foreach (KeyValuePair<String, Device> item in this.Devices) { foreach (KeyValuePair<String, Device> item in this.Devices) {
if (item.Value.TimeStamp > DateTime.Now.AddMinutes(-10)) { if (item.Value.TimeStamp > DateTime.Now.AddMinutes(-15)) {
ret++; ret++;
} }
} }
@ -58,7 +58,7 @@ namespace Mqtt_SWB_Dashboard.Helper {
internal Double GetPower() { internal Double GetPower() {
Double ret = 0; Double ret = 0;
foreach (KeyValuePair<String, Device> item in this.Devices) { foreach (KeyValuePair<String, Device> item in this.Devices) {
if (item.Value.TimeStamp > DateTime.Now.AddMinutes(-10) && item.Value.Type != Device.DevType.Production) { if (item.Value.TimeStamp > DateTime.Now.AddMinutes(-15) && item.Value.Type != Device.DevType.Production) {
ret += item.Value.Power; ret += item.Value.Power;
} }
} }
@ -78,7 +78,7 @@ namespace Mqtt_SWB_Dashboard.Helper {
internal Double GetColum() { internal Double GetColum() {
Double ret = 0; Double ret = 0;
foreach (KeyValuePair<String, Device> item in this.Devices) { foreach (KeyValuePair<String, Device> item in this.Devices) {
if (item.Value.TimeStamp > DateTime.Now.AddMinutes(-10) && item.Value.Type != Device.DevType.Production) { if (item.Value.TimeStamp > DateTime.Now.AddMinutes(-15) && item.Value.Type != Device.DevType.Production) {
ret += item.Value.Comul; ret += item.Value.Comul;
} }
} }

View File

@ -2,6 +2,7 @@
using System; using System;
using System.Globalization; using System.Globalization;
using System.Windows; using System.Windows;
using Dashboard.Connector;
namespace Mqtt_SWB_Dashboard { namespace Mqtt_SWB_Dashboard {
/// <summary> /// <summary>
@ -21,11 +22,11 @@ namespace Mqtt_SWB_Dashboard {
LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(System.Windows.Markup.XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag))); LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(System.Windows.Markup.XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
try { try {
String broker = InIReader.GetInstance("settings.ini").GetValue("general", "broker"); String broker = InIReader.GetInstance("settings.ini").GetValue("mqtt", "server");
this.Dispatcher.BeginInvoke((Action)(() => { this.Dispatcher.BeginInvoke((Action)(() => {
this.connectedTo.Text = "Connected to: " + broker; this.connectedTo.Text = "Connected to: " + broker;
})); }));
this.s = new Stats(new Mosquitto(broker)); this.s = new Stats(ADataBackend.GetInstance(InIReader.GetInstance("settings.ini").GetSection("mqtt")));
this.s.UpdatedConsumption += this.S_UpdatedConsumption; this.s.UpdatedConsumption += this.S_UpdatedConsumption;
this.S_UpdatedConsumption(this.s, null); this.S_UpdatedConsumption(this.s, null);
} catch(Exception e) { } catch(Exception e) {
@ -36,16 +37,17 @@ namespace Mqtt_SWB_Dashboard {
private void S_UpdatedConsumption(Stats sender, EventArgs e) { private void S_UpdatedConsumption(Stats sender, EventArgs e) {
this.Dispatcher.BeginInvoke((Action)(() => { this.Dispatcher.BeginInvoke((Action)(() => {
this.countHouses.Text = sender.GetNumberHouseholds();
this.countDevices.Text = sender.GetNumberDevices();
this.countRaspis.Text = sender.GetNumberRaspis(); this.countRaspis.Text = sender.GetNumberRaspis();
this.maxRaspi.Text = sender.GetMostRaspiUptime(); this.maxRaspi.Text = sender.GetMostRaspiUptime();
this.avgUptime.Text = sender.GetAvgRaspiUptime(); this.avgUptime.Text = sender.GetAvgRaspiUptime();
this.countColum.Text = sender.GetCurrentColum();
Tuple<Int32, Int32> devices = sender.GetNumberDevices();
this.countDevices.Text = devices.Item1 + " / " + devices.Item2;
Tuple<Int32, Int32, Int32> houses = sender.GetNumberHouseholds();
this.countHouses.Text = houses.Item1 + " / " + houses.Item2 + " / " + houses.Item3;
Tuple<Double, Double> power = sender.GetCurrentPower(); Tuple<Double, Double> power = sender.GetCurrentPower();
this.countPower.Text = power.Item1 + "W / " + power.Item2 + "W"; this.countPower.Text = power.Item1 + "W / " + power.Item2 + "W";
Tuple<Double, Double> colum = sender.GetCurrentColum(); ((Models.PowerChartModel)this.DataContext).AddPower(power, houses, devices);
this.countColum.Text = colum.Item1.ToString("F1") + "kW / " + colum.Item2.ToString("F1") + "kW";
((Models.PowerChartModel)this.DataContext).AddPower(power, colum);
})); }));
} }

View File

@ -12,7 +12,7 @@ using OxyPlot.Series;
namespace Mqtt_SWB_Dashboard.Models { namespace Mqtt_SWB_Dashboard.Models {
public class PowerChartModel : INotifyPropertyChanged { public class PowerChartModel : INotifyPropertyChanged {
private Double MaximumDrawnL = 5; private Double MaximumDrawnL = 5;
private Double MaximumDrawnR = 5; private Int32 MaximumDrawnR = 5;
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
@ -23,33 +23,28 @@ namespace Mqtt_SWB_Dashboard.Models {
this.Model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, AbsoluteMinimum = 0, Minimum = 0, AbsoluteMaximum = 1, IsZoomEnabled = false }); this.Model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, AbsoluteMinimum = 0, Minimum = 0, AbsoluteMaximum = 1, IsZoomEnabled = false });
this.Model.Axes.Add(new LinearAxis { Position = AxisPosition.Right, AbsoluteMinimum = 0, Minimum = 0, AbsoluteMaximum = 1, IsZoomEnabled = false, Key = "Right" }); this.Model.Axes.Add(new LinearAxis { Position = AxisPosition.Right, AbsoluteMinimum = 0, Minimum = 0, AbsoluteMaximum = 1, IsZoomEnabled = false, Key = "Right" });
this.Model.Series.Add(new LineSeries { Title = "Active [W]", Color = OxyColor.FromRgb(0, 150, 0) }); this.Model.Series.Add(new LineSeries { Title = "Power [W]", Color = OxyColor.FromRgb(0, 150, 0) });
this.Model.Series.Add(new LineSeries { Title = "Seen [W]", Color = OxyColor.FromRgb(0, 255, 0) }); this.Model.Series.Add(new LineSeries { Title = "Houses", Color = OxyColor.FromRgb(0, 0, 255), YAxisKey= "Right" });
this.Model.Series.Add(new LineSeries { Title = "Active [kW]", Color = OxyColor.FromRgb(0, 0, 150), YAxisKey= "Right" }); this.Model.Series.Add(new LineSeries { Title = "Devices", Color = OxyColor.FromRgb(255, 0, 0), YAxisKey = "Right" });
this.Model.Series.Add(new LineSeries { Title = "Seen [kW]", Color = OxyColor.FromRgb(0, 0, 255), YAxisKey = "Right" });
this.RaisePropertyChanged("Model"); this.RaisePropertyChanged("Model");
} }
internal void AddPower(Tuple<Double, Double> power, Tuple<Double, Double> colum) { internal void AddPower(Tuple<Double, Double> power, Tuple<Int32, Int32, Int32> houses, Tuple<Int32, Int32> devices) {
if((power.Item1 == 0 && power.Item2 == 0) || (colum.Item1 == 0 && colum.Item2 == 0)) { if((power.Item1 == 0 && power.Item2 == 0) || (houses.Item1 == 0 && houses.Item2 == 0 && houses.Item3 == 0) || (devices.Item1 == 0 && devices.Item2 == 0)) {
return; return;
} }
((LineSeries)this.Model.Series[0]).Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), power.Item1)); ((LineSeries)this.Model.Series[0]).Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), power.Item1));
((LineSeries)this.Model.Series[1]).Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), power.Item2)); ((LineSeries)this.Model.Series[1]).Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), houses.Item1));
((LineSeries)this.Model.Series[2]).Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), colum.Item1)); ((LineSeries)this.Model.Series[2]).Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), devices.Item1));
((LineSeries)this.Model.Series[3]).Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), colum.Item2));
if (this.MaximumDrawnL < power.Item1) { if (this.MaximumDrawnL < power.Item1) {
this.MaximumDrawnL = power.Item1; this.MaximumDrawnL = power.Item1;
} }
if (this.MaximumDrawnL < power.Item2) { if (this.MaximumDrawnR < houses.Item1) {
this.MaximumDrawnL = power.Item2; this.MaximumDrawnR = houses.Item1;
} }
if (this.MaximumDrawnR < colum.Item1) { if (this.MaximumDrawnR < devices.Item1) {
this.MaximumDrawnR = colum.Item1; this.MaximumDrawnR = devices.Item1;
}
if (this.MaximumDrawnR < colum.Item2) {
this.MaximumDrawnR = colum.Item2;
} }
//this.Model.Axes[1].Minimum = 0; //this.Model.Axes[1].Minimum = 0;
//this.Model.Axes[2].Minimum = 0; //this.Model.Axes[2].Minimum = 0;

View File

@ -1,88 +0,0 @@
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Mqtt_SWB_Dashboard {
class Mosquitto : IDisposable {
private Process p;
private String message;
public delegate void MqttMessage(Object sender, MqttEventArgs e);
public event MqttMessage MessageIncomming;
public Mosquitto(String broker) {
//mosquitto_sub --cafile ca.pem --cert cert.pem --key cert.key -h swb.broker.flex4grid.eu -p 8883 -t /# -v
this.message = "";
this.p = new Process();
this.p.StartInfo.FileName = "mosquitto_sub.exe";
this.p.StartInfo.Arguments = "--cafile ca.pem --cert cert.pem --key cert.key -h "+broker+" -p 8883 -t /# -v -d";
this.p.StartInfo.CreateNoWindow = true;
this.p.StartInfo.UseShellExecute = false;
this.p.StartInfo.RedirectStandardOutput = true;
this.p.StartInfo.RedirectStandardError = true;
this.p.OutputDataReceived += this.P_OutputDataReceived;
this.p.ErrorDataReceived += this.P_ErrorDataReceived;
this.p.Start();
this.p.BeginOutputReadLine();
}
private void P_ErrorDataReceived(Object sender, DataReceivedEventArgs e) {
if (e.Data != null) {
throw new NotImplementedException(e.Data);
}
}
private void P_OutputDataReceived(Object sender, DataReceivedEventArgs e) {
if (e.Data != null) {
if (e.Data.StartsWith("Client mosqsub")) {
if (this.message != "" && this.message.IndexOf(" received PUBLISH ") > 0) {
MatchCollection matches = (new Regex("^Client mosqsub\\|.*received PUBLISH \\(.*,.*,.*,.*, '(.*)'.*\\)\\)\n[^ ]* (.*)$", RegexOptions.IgnoreCase | RegexOptions.Singleline)).Matches(this.message);
String topic = matches[0].Groups[1].Value;
String message = matches[0].Groups[2].Value.Trim();
this.MessageIncomming?.Invoke(this, new MqttEventArgs(message, topic));
}
this.message = e.Data + "\n";
} else {
this.message += e.Data + "\n";
}
}
}
#region IDisposable Support
private Boolean disposedValue = false; // Dient zur Erkennung redundanter Aufrufe.
protected virtual void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
this.p.CancelOutputRead();
this.p.Kill();
this.p.Close();
}
this.p = null;
this.disposedValue = true;
}
}
~Mosquitto() {
Dispose(false);
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
class MqttEventArgs : EventArgs {
public MqttEventArgs() : base() { }
public MqttEventArgs(String message, String topic) {
this.Topic = topic;
this.Message = message;
this.Date = DateTime.Now;
}
public String Topic { get; private set; }
public String Message { get; private set; }
public DateTime Date { get; private set; }
}
}

View File

@ -63,6 +63,8 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="Connector\ADataBackend.cs" />
<Compile Include="Connector\Mosquitto.cs" />
<Compile Include="Helper\Device.cs" /> <Compile Include="Helper\Device.cs" />
<Compile Include="Helper\Household.cs" /> <Compile Include="Helper\Household.cs" />
<Compile Include="Helper\Raspi.cs" /> <Compile Include="Helper\Raspi.cs" />
@ -82,7 +84,6 @@
</Compile> </Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Mosquitto.cs" />
<Compile Include="Properties\AssemblyInfo.cs"> <Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
@ -105,6 +106,7 @@
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None> </None>
<None Include="settings.ini.example" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Dashboard.Connector;
using LitJson; using LitJson;
using Mqtt_SWB_Dashboard.Helper; using Mqtt_SWB_Dashboard.Helper;
@ -10,12 +11,12 @@ namespace Mqtt_SWB_Dashboard {
internal class Stats : IDisposable { internal class Stats : IDisposable {
private SortedDictionary<String, Household> households = new SortedDictionary<String, Household>(); private SortedDictionary<String, Household> households = new SortedDictionary<String, Household>();
private SortedDictionary<String, Raspi> raspis = new SortedDictionary<String, Raspi>(); private SortedDictionary<String, Raspi> raspis = new SortedDictionary<String, Raspi>();
private Mosquitto mosquitto; private ADataBackend mosquitto;
public delegate void UpdateMessage(Stats sender, EventArgs e); public delegate void UpdateMessage(Stats sender, EventArgs e);
public event UpdateMessage UpdatedConsumption; public event UpdateMessage UpdatedConsumption;
public Stats(Mosquitto mosquitto) { public Stats(ADataBackend mosquitto) {
LoadSavedData(); LoadSavedData();
this.mosquitto = mosquitto; this.mosquitto = mosquitto;
this.mosquitto.MessageIncomming += this.Mosquitto_MessageIncomming; this.mosquitto.MessageIncomming += this.Mosquitto_MessageIncomming;
@ -46,6 +47,8 @@ namespace Mqtt_SWB_Dashboard {
this.LvcMessages(e); this.LvcMessages(e);
} else if(Regex.Match(e.Topic, "/flex4grid/v1/load_balancing/events/.*").Success) { } else if(Regex.Match(e.Topic, "/flex4grid/v1/load_balancing/events/.*").Success) {
this.PowerEvents(e); this.PowerEvents(e);
} else if (Regex.Match(e.Topic, "/flex4grid/v1/households/[^/]*/device/error").Success) {
this.ErrorDeviceMessage(e);
} else if (Regex.Match(e.Topic, "/flex4grid/VersionControl/LocalGateway/hosts/.*/status").Success || } else if (Regex.Match(e.Topic, "/flex4grid/VersionControl/LocalGateway/hosts/.*/status").Success ||
e.Topic == "/flex4grid/VersionControl/LocalGateway/status") { e.Topic == "/flex4grid/VersionControl/LocalGateway/status") {
this.RaspiStatus(e); this.RaspiStatus(e);
@ -69,6 +72,14 @@ namespace Mqtt_SWB_Dashboard {
#region Mqtt-Message Parser #region Mqtt-Message Parser
/// <summary>
/// Fehlermeldung eines Plugs
/// </summary>
/// <param name="e"></param>
private void ErrorDeviceMessage(MqttEventArgs e) {
// /flex4grid/v1/households/[^/]*/device/error {"errorCode": "200", "timestamp": "2017-09-26T19:21:58.561839Z", "errorMessage": "Unable_to_start_ZWave_network", "id": "1"}
}
/// <summary> /// <summary>
/// Raspberry Powerevents /// Raspberry Powerevents
/// </summary> /// </summary>
@ -183,7 +194,7 @@ namespace Mqtt_SWB_Dashboard {
#region Statistics Output #region Statistics Output
internal String GetNumberDevices() { internal Tuple<Int32, Int32> GetNumberDevices() {
Int32 active = 0; Int32 active = 0;
Int32 all = 0; Int32 all = 0;
try { try {
@ -192,18 +203,18 @@ namespace Mqtt_SWB_Dashboard {
all += item.Value.Devices.Count; all += item.Value.Devices.Count;
} }
} catch(Exception) { } catch(Exception) {
return "0 / 0"; return new Tuple<Int32, Int32>(0, 0);
} }
return active.ToString() + " / " + all.ToString(); return new Tuple<Int32, Int32>(active, all);
} }
internal String GetNumberHouseholds() { internal Tuple<Int32, Int32, Int32> GetNumberHouseholds() {
Int32 active = 0; Int32 active = 0;
Int32 ping = 0; Int32 ping = 0;
Int32 all = this.households.Count; Int32 all = this.households.Count;
try { try {
foreach (KeyValuePair<String, Household> item in this.households) { foreach (KeyValuePair<String, Household> item in this.households) {
if (item.Value.Active > DateTime.Now.AddMinutes(-10)) { if (item.Value.Active > DateTime.Now.AddMinutes(-15)) {
ping++; ping++;
} }
if (item.Value.Connected) { if (item.Value.Connected) {
@ -211,9 +222,9 @@ namespace Mqtt_SWB_Dashboard {
} }
} }
} catch (Exception) { } catch (Exception) {
return "0 / 0 / 0"; return new Tuple<Int32, Int32, Int32>(0, 0, 0);
} }
return ping + " / " + active + " / " + all; return new Tuple<Int32, Int32, Int32>(ping, active, all);
} }
internal Tuple<Double, Double> GetCurrentPower() { internal Tuple<Double, Double> GetCurrentPower() {
@ -230,7 +241,7 @@ namespace Mqtt_SWB_Dashboard {
return new Tuple<Double, Double>(active, all); return new Tuple<Double, Double>(active, all);
} }
internal Tuple<Double, Double> GetCurrentColum() { internal String GetCurrentColum() {
Double active = 0; Double active = 0;
Double all = 0; Double all = 0;
try { try {
@ -239,9 +250,9 @@ namespace Mqtt_SWB_Dashboard {
all += item.Value.GetAllColum(); all += item.Value.GetAllColum();
} }
} catch(Exception) { } catch(Exception) {
return new Tuple<Double, Double>(0d, 0d); return "0kWh / 0kWh";
} }
return new Tuple<Double, Double>(active, all); return active.ToString("F1") + "kWh / " + all.ToString("F1") + "kWh";
} }
internal String GetNumberRaspis() { internal String GetNumberRaspis() {

View File

@ -0,0 +1,8 @@
[mqtt]
type=mosquitto
; server=localhost
server=swb.broker.flex4grid.eu
port=8883
cafile=ca.pem
cert=cert.pem
key=cert.key