[NF] Nun mit erster Ausgabe

This commit is contained in:
BlubbFish 2017-09-15 22:28:11 +00:00
parent d460255d83
commit 07ab463ac1
4 changed files with 77 additions and 38 deletions

View File

@ -4,24 +4,43 @@ using LitJson;
namespace Mqtt_SWB_Dashboard {
class DevicePower {
public DevicePower(DateTime t, Double p, Double c) {
this.TimeStamp = t;
this.Power = p;
this.Comul = c;
public enum DevType {
State,
Consumption,
Production
}
public DevicePower(JsonData t, JsonData p, JsonData c) {
this.Update(t, p, c);
public DevicePower(JsonData data, DevType type) {
this.TimeStamp = new DateTime();
this.Power = 0;
this.Comul = 0;
this.Status = false;
this.Production = 0;
this.Type = type;
this.Update(data, type);
}
public DateTime TimeStamp { get; set; }
public Double Power { get; set; }
public Double Comul { get; set; }
public DateTime TimeStamp { get; private set; }
public Boolean Status { get; private set; }
public Double Production { get; private set; }
public DevType Type { get; private set; }
public Double Power { get; private set; }
public Double Comul { get; private set; }
internal void Update(JsonData t, JsonData p, JsonData c) {
this.TimeStamp = DateTime.Parse(t.ToString());
this.Power = Double.Parse(p.ToString(), new CultureInfo("en-US"));
this.Comul = Double.Parse(c.ToString(), new CultureInfo("en-US"));
internal void Update(JsonData data, DevType type) {
this.TimeStamp = DateTime.Parse(data["timestamp"].ToString());
if (type == DevType.State) {
this.Status = data["status"].ToString() == "active" ? true : false;
}
if(type == DevType.Consumption) {
this.Power = Double.Parse(data["power"].ToString(), new CultureInfo("en-US"));
this.Comul = Double.Parse(data["energyCumul"].ToString(), new CultureInfo("en-US"));
}
if(type == DevType.Production) {
this.Power = Double.Parse(data["power"].ToString(), new CultureInfo("en-US"));
this.Comul = Double.Parse(data["energyCumulative"].ToString(), new CultureInfo("en-US"));
this.Production = Double.Parse(data["productionCumulative"].ToString(), new CultureInfo("en-US"));
}
}
}
}

View File

@ -7,6 +7,9 @@
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="Haushalte" VerticalAlignment="Top" Width="60"/>
<TextBlock HorizontalAlignment="Left" Margin="75,10,0,0" TextWrapping="Wrap" Text="Geräte" VerticalAlignment="Top" Width="40"/>
<TextBlock x:Name="countHouses" HorizontalAlignment="Left" Margin="10,31,0,0" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="60" TextAlignment="Center"/>
<TextBlock x:Name="countDevices" HorizontalAlignment="Left" Margin="75,31,0,0" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="40" TextAlignment="Center"/>
</Grid>
</Window>

View File

@ -1,17 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Mqtt_SWB_Dashboard {
/// <summary>
@ -21,6 +9,14 @@ namespace Mqtt_SWB_Dashboard {
public MainWindow() {
InitializeComponent();
Stats s = new Stats(new Mosquitto());
s.UpdatedConsumption += this.S_UpdatedConsumption;
}
private void S_UpdatedConsumption(Stats sender, EventArgs e) {
this.Dispatcher.BeginInvoke((Action)(() => {
this.countHouses.Text = sender.GetNumberHouseholds().ToString();
this.countDevices.Text = sender.GetNumberDevices().ToString();
}));
}
}
}

View File

@ -8,8 +8,8 @@ namespace Mqtt_SWB_Dashboard {
internal class Stats {
private Dictionary<String, Dictionary<Int32, DevicePower>> household;
public delegate void UpdateMessage(Object sender, EventArgs e);
public event UpdateMessage UpdatedData;
public delegate void UpdateMessage(Stats sender, EventArgs e);
public event UpdateMessage UpdatedConsumption;
public Stats(Mosquitto mosquitto) {
this.household = new Dictionary<String, Dictionary<Int32, DevicePower>>();
@ -25,32 +25,53 @@ namespace Mqtt_SWB_Dashboard {
this.GatewayPing(e);
} else if (Regex.Match(e.Topic, "/flex4grid/v1/households/.*/connection").Success) {
this.HouseholdPing(e);
} else if (Regex.Match(e.Topic, "/flex4grid/v1/households/.*/consumption").Success) {
} else if (Regex.Match(e.Topic, "/flex4grid/v1/households/.*/consumption").Success ||
Regex.Match(e.Topic, "/flex4grid/v1/households/.*/device/consumption").Success ||
Regex.Match(e.Topic, "/flex4grid/v1/households/.*/device/state").Success) {
this.HouseholdConsumption(e);
} else if (Regex.Match(e.Topic, "/flex4grid/v1/households/.*/device/state").Success ||
Regex.Match(e.Topic, "/flex4grid/v1/households/.*/device/consumption").Success) {
this.DeviceStatus(e);
} else {
throw new NotImplementedException(e.Topic);
}//
}
internal Int32 GetNumberDevices() {
Int32 ret = 0;
foreach (KeyValuePair<String, Dictionary<Int32, DevicePower>> item in this.household) {
ret += item.Value.Count;
}
return ret;
}
internal Int32 GetNumberHouseholds() {
return this.household.Count;
}
private void HouseholdConsumption(MqttEventArgs e) {
String id = Regex.Match(e.Topic, "/flex4grid/v1/households/(.*)/consumption").Groups[1].Value;
Match m = Regex.Match(e.Topic, "/flex4grid/v1/households/([^/]*)/(consumption|device/(consumption|state))");
String id = m.Groups[1].Value;
DevicePower.DevType dtype = DevicePower.DevType.Consumption;
if (m.Groups[2].Value == "consumption") {
dtype = DevicePower.DevType.Production;
} else if(m.Groups[3].Value == "consumption") {
dtype = DevicePower.DevType.Consumption;
} else if(m.Groups[3].Value == "state") {
dtype = DevicePower.DevType.State;
}
JsonData stuff = JsonMapper.ToObject(e.Message);
Int32 did = Int32.Parse(stuff["id"].ToString());
if (this.household.Keys.Contains(id)) {
if (this.household[id].Keys.Contains(did)) {
this.household[id][did].Update(stuff["timestamp"], stuff["power"], stuff["energyCumul"]);
this.household[id][did].Update(stuff, dtype);
} else {
this.household[id].Add(did, new DevicePower(stuff["timestamp"], stuff["power"], stuff["energyCumul"]));
this.household[id].Add(did, new DevicePower(stuff, dtype));
}
} else {
Dictionary<int, DevicePower> d = new Dictionary<int, DevicePower>();
d.Add(did, new DevicePower(stuff["timestamp"], stuff["power"], stuff["energyCumul"]));
Dictionary<Int32, DevicePower> d = new Dictionary<Int32, DevicePower> {
{ did, new DevicePower(stuff, dtype) }
};
this.household.Add(id, d);
}
this.UpdatedData?.Invoke(this, null);
this.UpdatedConsumption?.Invoke(this, null);
}
private void DeviceStatus(MqttEventArgs e) {