From 90b7fedbc5815116d1c3573f4b38371f022450fa Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Sat, 6 Feb 2021 21:36:56 +0000 Subject: [PATCH] implement logging system on dashboard. The code added here is more about populating the area created for logging each web request. The XAML code was done in the previous commit. As I stated there, these logs are more about the providing the user of this program visual feedback about the state of the program. There are times when the various devices (light meters/relays) don't update and it's hard to tell if that's Eyes and Ears, the internet connection or the devices on the other end. --- .../EyesAndEars.UWP/MainPage.xaml | 8 ++-- .../EyesAndEars.UWP/MainPage.xaml.cs | 8 ++-- .../EyesAndEars.UWP/Services/DataServices.cs | 38 ++++++++++++++++++- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml b/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml index 4473624..92ecf9d 100644 --- a/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml @@ -203,13 +203,13 @@ - + - - Executing Eyes and Ears..s. + VerticalScrollMode="Enabled" Margin="0,20"> + diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs b/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs index 94b528d..15e46b1 100644 --- a/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs @@ -50,11 +50,11 @@ namespace EyesAndEars.UWP { var url = _vm.BaseURL; if (!string.IsNullOrEmpty(url)) { // Devices 3 and 6 are not in use. - _vm.Device1 = await DataServices.UpdateDevice(url, 1); - _vm.Device2 = await DataServices.UpdateDevice(url, 2); + _vm.Device1 = await DataServices.UpdateDevice(url, 1, Logs); + _vm.Device2 = await DataServices.UpdateDevice(url, 2, Logs); // _vm.Device3 = await DataServices.UpdateDevice(url, 3); - _vm.Device4 = await DataServices.UpdateDevice(url, 4); - _vm.Device5 = await DataServices.UpdateDevice(url, 5); + _vm.Device4 = await DataServices.UpdateDevice(url, 4, Logs); + _vm.Device5 = await DataServices.UpdateDevice(url, 5, Logs); // _vm.Device6 = await DataServices.UpdateDevice(url, 6); _vm.CurrentTime = DateTime.UtcNow.ToShortTimeString(); } diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs b/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs index fa2916a..606b51e 100644 --- a/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs @@ -6,11 +6,15 @@ using System.Threading.Tasks; using Windows.Storage; using Windows.UI; using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Documents; +using Windows.UI.Text; namespace EyesAndEars.UWP.Services { public static class DataServices { - public static async Task UpdateDevice(string url, int deviceId) { + public static async Task UpdateDevice(string url, int deviceId, + RichTextBlock logs) { try { var id = MapFactoryDeviceToGalleyDevice(deviceId); // Has note. var readingAPI = $"{url}/api/readings/latest/{id}"; @@ -20,7 +24,9 @@ namespace EyesAndEars.UWP.Services { var r = MapToLightReading(readingJSON); var s = MapToDeviceStatus(statusJSON); var c = UpdateStatusColour(deviceId, s.status, r.reading); - return new Device(deviceId, r, s, c); + var dev = new Device(deviceId, r, s, c); + LogUpdate(logs, dev); + return dev; } catch (Exception e) { Debug.WriteLine(e.Message); @@ -28,6 +34,34 @@ namespace EyesAndEars.UWP.Services { } } + private static void LogUpdate(RichTextBlock logs, Device dev) { + // Devices 3 and 6 are not in use. + string device; + if (dev.Id == 1) device = "factory1"; + else if (dev.Id == 2) device = "factory2"; + else if (dev.Id == 4) device = "gallery1"; + else device = "gallery2"; + Paragraph paragraph = new Paragraph(); + var run1 = new Run { + FontWeight = FontWeights.Bold, + Text = $"{DateTime.Now} | " + }; + var run2 = new Run { + Foreground = dev.StatusColour, + FontWeight = FontWeights.Bold, + Text = device + }; + var run3 = new Run { + Foreground = dev.StatusColour, + Text = $" | {dev.LatestReading.id} | {dev.LatestReading.reading} | " + + $"{dev.LatestStatus.status} | {dev.LatestReading.time}" + }; + paragraph.Inlines.Add(run1); + paragraph.Inlines.Add(run2); + paragraph.Inlines.Add(run3); + logs.Blocks.Insert(0, paragraph); + } + private static SolidColorBrush UpdateStatusColour(int device, string status, int reading) { try { if (status.Equals("on", StringComparison.OrdinalIgnoreCase)) {