[NF] Sortiere JSON Ausgabe und Formatiere sie schön

This commit is contained in:
BlubbFish 2017-09-18 22:27:35 +00:00
parent 66a517b935
commit a0efb63b99
4 changed files with 25 additions and 16 deletions

View File

@ -7,19 +7,19 @@ using LitJson;
namespace Mqtt_SWB_Dashboard.Helper { namespace Mqtt_SWB_Dashboard.Helper {
class Household { class Household {
public Dictionary<String, Device> Devices { get; private set; } public SortedDictionary<String, Device> Devices { get; private set; }
public DateTime Active { get; private set; } public DateTime Active { get; private set; }
public Boolean Connected { get; private set; } public Boolean Connected { get; private set; }
public Household(String did, Device device) { public Household(String did, Device device) {
this.Active = DateTime.Now; this.Active = DateTime.Now;
this.Devices = new Dictionary<String, Device> { this.Devices = new SortedDictionary<String, Device> {
{ did, device } { did, device }
}; };
} }
public Household(JsonData data) { public Household(JsonData data) {
this.Devices = new Dictionary<String, Device>(); this.Devices = new SortedDictionary<String, Device>();
if (data.Keys.Contains("Devices")) { if (data.Keys.Contains("Devices")) {
foreach (KeyValuePair<String, JsonData> item in data["Devices"]) { foreach (KeyValuePair<String, JsonData> item in data["Devices"]) {
this.Devices.Add(item.Key, new Device(item.Value)); this.Devices.Add(item.Key, new Device(item.Value));

View File

@ -20,13 +20,18 @@ namespace Mqtt_SWB_Dashboard {
System.Threading.Thread.CurrentThread.CurrentUICulture = info; System.Threading.Thread.CurrentThread.CurrentUICulture = info;
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)));
String broker = InIReader.GetInstance("settings.ini").GetValue("general", "broker"); try {
this.Dispatcher.BeginInvoke((Action)(() => { String broker = InIReader.GetInstance("settings.ini").GetValue("general", "broker");
this.connectedTo.Text = "Connected to: "+broker; this.Dispatcher.BeginInvoke((Action)(() => {
})); this.connectedTo.Text = "Connected to: " + broker;
this.s = new Stats(new Mosquitto(broker)); }));
this.s.UpdatedConsumption += this.S_UpdatedConsumption; this.s = new Stats(new Mosquitto(broker));
this.S_UpdatedConsumption(this.s, null); this.s.UpdatedConsumption += this.S_UpdatedConsumption;
this.S_UpdatedConsumption(this.s, null);
} catch(Exception e) {
MessageBox.Show(e.Message+"\n"+e.StackTrace, "Some Shit goes fucking wrong!");
Application.Current.Shutdown(1);
}
} }
private void S_UpdatedConsumption(Stats sender, EventArgs e) { private void S_UpdatedConsumption(Stats sender, EventArgs e) {

View File

@ -8,8 +8,8 @@ using Mqtt_SWB_Dashboard.Helper;
namespace Mqtt_SWB_Dashboard { namespace Mqtt_SWB_Dashboard {
internal class Stats : IDisposable { internal class Stats : IDisposable {
private Dictionary<String, Household> households = new Dictionary<String, Household>(); private SortedDictionary<String, Household> households = new SortedDictionary<String, Household>();
private Dictionary<String, Raspi> raspis = new Dictionary<String, Raspi>(); private SortedDictionary<String, Raspi> raspis = new SortedDictionary<String, Raspi>();
private Mosquitto mosquitto; private Mosquitto mosquitto;
public delegate void UpdateMessage(Stats sender, EventArgs e); public delegate void UpdateMessage(Stats sender, EventArgs e);
@ -290,10 +290,14 @@ namespace Mqtt_SWB_Dashboard {
if (!this.disposedValue) { if (!this.disposedValue) {
if (disposing) { if (disposing) {
this.mosquitto.MessageIncomming -= this.Mosquitto_MessageIncomming; this.mosquitto.MessageIncomming -= this.Mosquitto_MessageIncomming;
String households = JsonMapper.ToJson(this.households); JsonWriter writer = new JsonWriter {
File.WriteAllText("household.json", households); PrettyPrint = true
String raspis = JsonMapper.ToJson(this.raspis); };
File.WriteAllText("raspi.json", raspis); JsonMapper.ToJson(this.households, writer);
File.WriteAllText("household.json", writer.ToString());
writer.Reset();
JsonMapper.ToJson(this.raspis, writer);
File.WriteAllText("raspi.json", writer.ToString());
this.mosquitto.Dispose(); this.mosquitto.Dispose();
} }
this.mosquitto = null; this.mosquitto = null;