diff --git a/Mqtt-SWB-Dashboard/Helper.cs b/Mqtt-SWB-Dashboard/Helper.cs index 3fe7b99..616bb54 100644 --- a/Mqtt-SWB-Dashboard/Helper.cs +++ b/Mqtt-SWB-Dashboard/Helper.cs @@ -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")); + } } } } \ No newline at end of file diff --git a/Mqtt-SWB-Dashboard/MainWindow.xaml b/Mqtt-SWB-Dashboard/MainWindow.xaml index 6c72409..6cadfc4 100644 --- a/Mqtt-SWB-Dashboard/MainWindow.xaml +++ b/Mqtt-SWB-Dashboard/MainWindow.xaml @@ -7,6 +7,9 @@ mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> - + + + + diff --git a/Mqtt-SWB-Dashboard/MainWindow.xaml.cs b/Mqtt-SWB-Dashboard/MainWindow.xaml.cs index b791016..5aec133 100644 --- a/Mqtt-SWB-Dashboard/MainWindow.xaml.cs +++ b/Mqtt-SWB-Dashboard/MainWindow.xaml.cs @@ -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 { /// @@ -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(); + })); } } } diff --git a/Mqtt-SWB-Dashboard/Stats.cs b/Mqtt-SWB-Dashboard/Stats.cs index 6f4f55b..be04034 100644 --- a/Mqtt-SWB-Dashboard/Stats.cs +++ b/Mqtt-SWB-Dashboard/Stats.cs @@ -8,8 +8,8 @@ namespace Mqtt_SWB_Dashboard { internal class Stats { private Dictionary> 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>(); @@ -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> 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 d = new Dictionary(); - d.Add(did, new DevicePower(stuff["timestamp"], stuff["power"], stuff["energyCumul"])); + Dictionary d = new Dictionary { + { 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) {