diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/EyesAndEars.UWP.csproj b/src/EyesAndEars.UWP/EyesAndEars.UWP/EyesAndEars.UWP.csproj index 5ce7dc5..18666d5 100644 --- a/src/EyesAndEars.UWP/EyesAndEars.UWP/EyesAndEars.UWP.csproj +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/EyesAndEars.UWP.csproj @@ -122,6 +122,7 @@ MainPage.xaml + diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml b/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml index 3762aa2..6118441 100644 --- a/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml @@ -45,83 +45,101 @@ + Background="{x:Bind _vm.Device1.StatusColour, Mode=OneWay, + FallbackValue=Pink}" Margin="0,0,0,12" Width="300" Height="300"> - - + - + - + Background="{x:Bind _vm.Device3.StatusColour, Mode=OneWay, + FallbackValue=Pink}" Margin="0,0,0,12" Width="300" Height="300"> - + Background="{x:Bind _vm.Device4.StatusColour, Mode=OneWay, + FallbackValue=Pink}" Margin="0,0,0,12" Width="300" Height="150"> - + + + Background="{x:Bind _vm.Device5.StatusColour, Mode=OneWay, + FallbackValue=Pink}" Margin="0,0,0,12" Width="300" Height="150"> - + + Background="{x:Bind _vm.Device6.StatusColour, Mode=OneWay, + FallbackValue=Pink}" Margin="0,0,0,12" Width="300" Height="150"> - + + diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs b/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs index 0318898..c35b985 100644 --- a/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs @@ -18,42 +18,15 @@ namespace EyesAndEars.UWP { IntialiseRefreshTime(); } - private void IntialiseDataContext() { - try { - // The Update features are not yet working... - - /* - * Note: Why I Did Not Use "await" Here. - * ============================================================ - * "await" is not used here because the app. is useless until - * the data is retreived from DataServices (I.E. Web). It is, - * also, called from "MainPain" above and the compiler don't - * like you doing that. - */ - _vm.FactoryDevice1 = DataServices.UpdateFactoryDevice(1).Result; - _vm.FactoryDevice2 = DataServices.UpdateFactoryDevice(2).Result; - _vm.FactoryDevice3 = DataServices.UpdateFactoryDevice(3).Result; - _vm.GalleryDevice1 = DataServices.UpdateGalleryDevice(4).Result; - _vm.GalleryDevice2 = DataServices.UpdateGalleryDevice(5).Result; - _vm.GalleryDevice3 = DataServices.UpdateGalleryDevice(6).Result; - DataContext = _vm; - } - catch (Exception) { - /* - * Note: About this Exception - * ============================================================ - * This is catch-block is here to give the user a chance to - * update the base-URL or show me what the app. is doing before - * it is abruptly closed by Windows. It, also, gives the server - * a bit of time to fix any problems it is having before this - * app. thinks it is broken. Make no mistake, though. All this - * is doing it buying the user some time. The app. is not is a - * good state if execeptions are getting caught here -- - * regardless of the type or the reasons why. - */ - _vm = DataServices.CreateFallBackViewModel(); - DataContext = _vm; - } + void IntialiseDataContext() { + var url = WebAddressBox.Text; + _vm.Device1 = DataServices.CreateFallBackDevice(1); + _vm.Device2 = DataServices.CreateFallBackDevice(2); + _vm.Device3 = DataServices.CreateFallBackDevice(3); + _vm.Device4 = DataServices.CreateFallBackDevice(4); + _vm.Device5 = DataServices.CreateFallBackDevice(5); + _vm.Device6 = DataServices.CreateFallBackDevice(6); + DataContext = _vm; } void IntialiseRefreshTime() { @@ -64,33 +37,21 @@ namespace EyesAndEars.UWP { } async void UpdateViewModel(object sender, object e) { - try { - // I SHOULD PUT THE TRY-CATCH BLOCK IN DATASERVICES AND IF - // ERROR THROWN THERE, A ERROR/WARNING VM OBJECT SHOULD BE MADE - // UPDATED THERE. - // The Update features are not yet working... - _vm.FactoryDevice1 = await DataServices.UpdateFactoryDevice(1); - _vm.FactoryDevice2 = await DataServices.UpdateFactoryDevice(2); - _vm.FactoryDevice3 = await DataServices.UpdateFactoryDevice(3); - _vm.GalleryDevice1 = await DataServices.UpdateGalleryDevice(4); - _vm.GalleryDevice2 = await DataServices.UpdateGalleryDevice(5); - _vm.GalleryDevice3 = await DataServices.UpdateGalleryDevice(6); - Debug.WriteLine($"Updated GUI | VM: {_vm.FactoryDevice1.LatestReading.Reading}"); - } - catch (Exception) { - // Shouldn't be relying on this at this point in the cycle. - // If there is a problem it should be shown as such... - // and dealt with as one. - _vm = DataServices.CreateFallBackViewModel(); - } + var url = WebAddressBox.Text; + _vm.Device1 = await DataServices.UpdateDevice(url, 1); + _vm.Device2 = await DataServices.UpdateDevice(url, 2); + _vm.Device3 = await DataServices.UpdateDevice(url, 3); + _vm.Device4 = await DataServices.UpdateDevice(url, 4); + _vm.Device5 = await DataServices.UpdateDevice(url, 5); + _vm.Device6 = await DataServices.UpdateDevice(url, 6); } - private async void RefreshButton_Click(object sender, RoutedEventArgs e) { + async void RefreshButton_Click(object sender, RoutedEventArgs e) { // GET THE INITIALISATION WORKING FIRST AND THE DEFAULT CYCLE... } - private void SaveURLButton_Click(object sender, RoutedEventArgs e) { + void SaveURLButton_Click(object sender, RoutedEventArgs e) { // WORK ON THIS WENT YOU HAVE A LIVE SYSTEM WORKING WITH KNOWABLE DEFAULTS. } } diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/Models/Device.cs b/src/EyesAndEars.UWP/EyesAndEars.UWP/Models/Device.cs new file mode 100644 index 0000000..3622127 --- /dev/null +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/Models/Device.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Windows.UI; +using Windows.UI.Xaml.Media; + +namespace EyesAndEars.UWP.Models { + public class Device { + + public int Id { get; set; } + + public LightReading LatestReading { get; set; } + + public DeviceStatus LatestStatus { get; set; } + + public SolidColorBrush StatusColour { get; set; } + + public Device() { } + + public Device(int id, LightReading reading, DeviceStatus status, + SolidColorBrush colour) { + + Id = id; + LatestReading = reading; + LatestStatus = status; + StatusColour = colour; + } + } +} diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/Models/DeviceStatus.cs b/src/EyesAndEars.UWP/EyesAndEars.UWP/Models/DeviceStatus.cs index 7265d81..1bb30db 100644 --- a/src/EyesAndEars.UWP/EyesAndEars.UWP/Models/DeviceStatus.cs +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/Models/DeviceStatus.cs @@ -3,17 +3,8 @@ namespace EyesAndEars.UWP.Models { public class DeviceStatus { - - public readonly int Id; - public readonly string Status; - public readonly DateTime DateOfStatusRecording; - - public DeviceStatus() { } - - public DeviceStatus(int id, string status, DateTime dateOfStatusRecording) { - Id = id; - Status = status; - DateOfStatusRecording = dateOfStatusRecording; - } + public int id { get; set; } + public string status { get; set; } + public DateTime time { get; set; } } } diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/Models/LightReading.cs b/src/EyesAndEars.UWP/EyesAndEars.UWP/Models/LightReading.cs index aa77bc1..cf60b83 100644 --- a/src/EyesAndEars.UWP/EyesAndEars.UWP/Models/LightReading.cs +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/Models/LightReading.cs @@ -4,16 +4,9 @@ namespace EyesAndEars.UWP.Models { public class LightReading { - public readonly int Id; - public readonly int Reading; - public readonly DateTime MomentReadingWasTaken; + public int id { get; set; } + public int reading { get; set; } + public DateTime time { get; set; } - public LightReading() { } - - public LightReading(int id, int reading, DateTime momentOfRecording) { - Id = id; - Reading = reading; - MomentReadingWasTaken = momentOfRecording; - } } } diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs b/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs index 5a0a1c5..9da75a2 100644 --- a/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs @@ -1,43 +1,78 @@ using EyesAndEars.UWP.Models; -using EyesAndEars.UWP.ViewModels; using System; +using System.Diagnostics; +using System.Text.Json; using System.Threading.Tasks; +using Windows.UI; +using Windows.UI.Xaml.Media; namespace EyesAndEars.UWP.Services { public static class DataServices { - public static async Task UpdateFactoryDevice(int deviceId) { - // This should cause an error right now -- deliberate for error checking. - var t = await WebServices.GetLightReading("ht", deviceId); - // Need to map the json result to the models and display in view. - return new FactoryDevice(); + public static async Task UpdateDevice(string url, int deviceId) { + try { + var id = MapFactoryDeviceToGalleyDevice(deviceId); + var readingAPI = $"{url}/api/readings/latest/{id}"; + var statusAPI = $"{url}/api/status/latest/{deviceId}"; + var readingJSON = await WebServices.GetJSON(readingAPI); + var statusJSON = await WebServices.GetJSON(statusAPI); + var r = MapToLightReading(readingJSON); + var s = MapToDeviceStatus(statusJSON); + var c = UpdateStatusColour(s.status); + return new Device(deviceId, r, s, c); + } + catch (Exception e) { + Debug.WriteLine($"ERROR: {e.Message}"); + return CreateFallBackDevice(deviceId); + } } - public static async Task UpdateGalleryDevice(int deviceId) { - // This should cause an error right now -- deliberate for error checking. - var t = await WebServices.GetLightReading("ht", deviceId); - // Need to map the json result to the models and display in view. - return new GalleryDevice(); + static SolidColorBrush UpdateStatusColour(string status) { + return status.Equals("on", StringComparison.OrdinalIgnoreCase) ? + new SolidColorBrush(Colors.SeaGreen) : + new SolidColorBrush(Colors.DarkRed); } - public static MainPageVM CreateFallBackViewModel() { - // "999" and "-" are acting as sentinals in this context. - var r = new LightReading(0, 999, DateTime.Now); - var s = new DeviceStatus(0, "-", DateTime.Now); - var fD1 = new FactoryDevice(1, r, s); - var fD2 = new FactoryDevice(2, r, s); - var fD3 = new FactoryDevice(3, r, s); - var gD1 = new GalleryDevice(4, s); - var gD2 = new GalleryDevice(5, s); - var gD3 = new GalleryDevice(6, s); - var vm = new MainPageVM(); - vm.FactoryDevice1 = fD1; - vm.FactoryDevice2 = fD2; - vm.FactoryDevice3 = fD3; - vm.GalleryDevice1 = gD1; - vm.GalleryDevice2 = gD2; - vm.GalleryDevice3 = gD3; - return vm; + static int MapFactoryDeviceToGalleyDevice(int deviceId) { + int id = 0; + if (deviceId > 3) { + switch (deviceId) { + case 4: + id = 1; + break; + case 5: + id = 2; + break; + case 6: + id = 3; + break; + default: + break; + } + } + else { + id = deviceId; + } + return id; + } + + static LightReading MapToLightReading(string json) { + var reading = JsonSerializer.Deserialize(json); + return reading; + } + + static DeviceStatus MapToDeviceStatus(string json) { + var status = JsonSerializer.Deserialize(json); + return status; + } + + public static Device CreateFallBackDevice(int deviceId) { + // "999" and "err" are acting as sentinals in this context. + var r = new LightReading { id = 0, reading = 999, time = DateTime.Now }; + var s = new DeviceStatus { id = 0, status = "err", time = DateTime.Now }; + var c = new SolidColorBrush(Colors.DarkOrange); + var d = new Device(deviceId, r, s, c); + return d; } } } diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/WebServices.cs b/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/WebServices.cs index adc9cb7..7550f04 100644 --- a/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/WebServices.cs +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/Services/WebServices.cs @@ -7,11 +7,13 @@ namespace EyesAndEars.UWP.Services { private static readonly HttpClient _client = new HttpClient(); - public static async Task GetLightReading(string url, int deviceId) { + public static async Task GetJSON(string url) { _client.DefaultRequestHeaders.Accept.Clear(); + var response = await _client.GetAsync(url); _client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); - return await _client.GetStringAsync($"{url}/api/readings/latest/{deviceId}"); + var result = await _client.GetStringAsync(url); + return result; } } diff --git a/src/EyesAndEars.UWP/EyesAndEars.UWP/ViewModels/MainPageVM.cs b/src/EyesAndEars.UWP/EyesAndEars.UWP/ViewModels/MainPageVM.cs index b02695a..74f86fb 100644 --- a/src/EyesAndEars.UWP/EyesAndEars.UWP/ViewModels/MainPageVM.cs +++ b/src/EyesAndEars.UWP/EyesAndEars.UWP/ViewModels/MainPageVM.cs @@ -17,79 +17,79 @@ namespace EyesAndEars.UWP.ViewModels { public MainPageVM() { } - public MainPageVM(FactoryDevice f1, FactoryDevice f2, FactoryDevice f3, - GalleryDevice g1, GalleryDevice g2, GalleryDevice g3) { + public MainPageVM(Device f1, Device f2, Device f3, + Device g1, Device g2, Device g3) { - FactoryDevice1 = f1; - FactoryDevice2 = f2; - FactoryDevice3 = f3; - GalleryDevice1 = g1; - GalleryDevice2 = g2; - GalleryDevice3 = g3; + Device1 = f1; + Device2 = f2; + Device3 = f3; + Device1 = g1; + Device2 = g2; + Device3 = g3; } - FactoryDevice _factoryDevice1; - public FactoryDevice FactoryDevice1 { - get { return _factoryDevice1; } + Device _device1; + public Device Device1 { + get { return _device1; } set { - if (value != _factoryDevice1) { - _factoryDevice1 = value; - NotifyPropertyChanged("FactoryDevice1"); + if (value != _device1) { + _device1 = value; + NotifyPropertyChanged("Device1"); } } } - FactoryDevice _factoryDevice2; - public FactoryDevice FactoryDevice2 { - get { return _factoryDevice2; } + Device _device2; + public Device Device2 { + get { return _device2; } set { - if (value != _factoryDevice2) { - _factoryDevice2 = value; - NotifyPropertyChanged("FactoryDevice2"); + if (value != _device2) { + _device2 = value; + NotifyPropertyChanged("Device2"); } } } - FactoryDevice _factoryDevice3; - public FactoryDevice FactoryDevice3 { - get { return _factoryDevice3; } + Device _device3; + public Device Device3 { + get { return _device3; } set { - if (value != _factoryDevice3) { - _factoryDevice3 = value; - NotifyPropertyChanged("FactoryDevice3"); + if (value != _device3) { + _device3 = value; + NotifyPropertyChanged("Device3"); } } } - GalleryDevice _galleryDevice1; - public GalleryDevice GalleryDevice1 { - get { return _galleryDevice1; } + Device _device4; + public Device Device4 { + get { return _device4; } set { - if (value != _galleryDevice1) { - _galleryDevice1 = value; - NotifyPropertyChanged("GalleryDevice1"); + if (value != _device4) { + _device4 = value; + NotifyPropertyChanged("Device4"); } } } - GalleryDevice _galleryDevice2; - public GalleryDevice GalleryDevice2 { - get { return _galleryDevice2; } + Device _device5; + public Device Device5 { + get { return _device5; } set { - if (value != _galleryDevice2) { - _galleryDevice2 = value; - NotifyPropertyChanged("GalleryDevice2"); + if (value != _device5) { + _device5 = value; + NotifyPropertyChanged("Device5"); } } } - GalleryDevice _galleryDevice3; - public GalleryDevice GalleryDevice3 { - get { return _galleryDevice3; } + Device _device6; + public Device Device6 { + get { return _device6; } set { - if (value != _galleryDevice3) { - _galleryDevice3 = value; - NotifyPropertyChanged("GalleryDevice3"); + if (value != _device6) { + _device6 = value; + NotifyPropertyChanged("Device6"); } } }