1
0
Fork 0
Browse Source

implement the device status update features.

pull/6/head
Craig Oates 4 years ago
parent
commit
b2324e77a7
  1. 1
      src/EyesAndEars.UWP/EyesAndEars.UWP/EyesAndEars.UWP.csproj
  2. 80
      src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml
  3. 75
      src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs
  4. 30
      src/EyesAndEars.UWP/EyesAndEars.UWP/Models/Device.cs
  5. 15
      src/EyesAndEars.UWP/EyesAndEars.UWP/Models/DeviceStatus.cs
  6. 13
      src/EyesAndEars.UWP/EyesAndEars.UWP/Models/LightReading.cs
  7. 93
      src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs
  8. 6
      src/EyesAndEars.UWP/EyesAndEars.UWP/Services/WebServices.cs
  9. 88
      src/EyesAndEars.UWP/EyesAndEars.UWP/ViewModels/MainPageVM.cs

1
src/EyesAndEars.UWP/EyesAndEars.UWP/EyesAndEars.UWP.csproj

@ -122,6 +122,7 @@
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="Models\Device.cs" />
<Compile Include="Models\DeviceStatus.cs" />
<Compile Include="Models\FactoryDevice.cs" />
<Compile Include="Models\GalleryDevice.cs" />

80
src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml

@ -45,83 +45,101 @@
</Grid.RowDefinitions>
<Grid x:Name="Factory1Grid" Grid.Row="0" Grid.Column="0"
Background="DarkRed" Margin="0,0,0,12" Width="300" Height="300">
Background="{x:Bind _vm.Device1.StatusColour, Mode=OneWay,
FallbackValue=Pink}" Margin="0,0,0,12" Width="300" Height="300">
<StackPanel Margin="12" VerticalAlignment="Stretch">
<TextBlock x:Name="Factory1Time"
Text="{x:Bind _vm.FactoryDevice1.LatestStatus.DateOfStatusRecording,
Mode=OneWay, FallbackValue=444}"
Text="{x:Bind _vm.Device1.LatestReading.time,
Mode=OneWay, FallbackValue=404}"
Foreground="White" FontSize="24"/>
<TextBlock x:Name="Factory1Reading"
Text="{x:Bind _vm.FactoryDevice1.LatestReading.Reading ,
Mode=OneWay, FallbackValue=444}" Foreground="White"
Text="{x:Bind _vm.Device1.LatestReading.reading ,
Mode=OneWay, FallbackValue=404}" Foreground="White"
VerticalAlignment="Bottom" FontSize="144"/>
<TextBlock Text="Factory1" Foreground="White" FontSize="36"
<TextBlock Text="Factory 1" Foreground="White" FontSize="36"
VerticalAlignment="Bottom"/>
</StackPanel>
</Grid>
<Grid x:Name="Factory2Grid" Grid.Row="0" Grid.Column="1"
Background="DarkRed" Margin="12,0,12,12" Width="300" Height="300">
<Grid x:Name="Factory2Grid" Grid.Row="0" Grid.Column="1"
Background="{x:Bind _vm.Device2.StatusColour, Mode=OneWay,
FallbackValue=Orange}" Margin="12,0,12,12" Width="300" Height="300">
<StackPanel Margin="12" VerticalAlignment="Stretch">
<TextBlock x:Name="Factory2Time" Foreground="White" FontSize="24"
Text="{x:Bind _vm.FactoryDevice2.LatestStatus.DateOfStatusRecording,
Mode=OneWay, FallbackValue=444}" />
<TextBlock x:Name="Factory2Reading" Text="{x:Bind _vm.FactoryDevice2.LatestReading.Reading,
Mode=OneWay, FallbackValue=444}" Foreground="White"
Text="{x:Bind _vm.Device2.LatestStatus.time,
Mode=OneWay, FallbackValue=404}" />
<TextBlock x:Name="Factory2Reading" Text="{x:Bind _vm.Device2.LatestReading.reading,
Mode=OneWay, FallbackValue=404}" Foreground="White"
VerticalAlignment="Bottom" FontSize="144"/>
<TextBlock Text="Factory2" Foreground="White" FontSize="36"
<TextBlock Text="Factory 2" Foreground="White" FontSize="36"
VerticalAlignment="Bottom"/>
</StackPanel>
</Grid>
<Grid x:Name="Factory3Grid" Grid.Row="0" Grid.Column="2"
Background="DarkRed" 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">
<StackPanel Margin="12" VerticalAlignment="Stretch">
<TextBlock x:Name="Factory3Time"
Text="{x:Bind _vm.FactoryDevice3.LatestStatus.DateOfStatusRecording,
Mode=OneWay, FallbackValue=444}"
Text="{x:Bind _vm.Device3.LatestStatus.time,
Mode=OneWay, FallbackValue=404}"
Foreground="White" FontSize="24"/>
<TextBlock x:Name="Factory3Reading" Foreground="White"
Text="{x:Bind _vm.FactoryDevice3.LatestReading.Reading,
Mode=OneWay, FallbackValue=444}" FontSize="144"
Text="{x:Bind _vm.Device3.LatestReading.reading,
Mode=OneWay, FallbackValue=404}" FontSize="144"
VerticalAlignment="Bottom"/>
<TextBlock Text="Factory3" Foreground="White" FontSize="36"
<TextBlock Text="Factory 3" Foreground="White" FontSize="36"
VerticalAlignment="Bottom"/>
</StackPanel>
</Grid>
<Grid x:Name="Gallery1Grid" Grid.Row="1" Grid.Column="0"
Background="DarkRed" Margin="0,0,0,12" Width="300" Height="150">
Background="{x:Bind _vm.Device4.StatusColour, Mode=OneWay,
FallbackValue=Pink}" Margin="0,0,0,12" Width="300" Height="150">
<StackPanel Margin="12" VerticalAlignment="Bottom">
<TextBlock x:Name="Gallery1Time" Foreground="White"
Text="{x:Bind _vm.GalleryDevice1.LatestStatus.DateOfStatusRecording,
Mode=OneWay, FallbackValue=444}" FontSize="24"/>
<TextBlock Text="Gallery1" Foreground="White" FontSize="36"
Text="{x:Bind _vm.Device4.LatestStatus.time,
Mode=OneWay, FallbackValue=404}" FontSize="24"/>
<TextBlock Text="Gallery 1" Foreground="White" FontSize="36"
VerticalAlignment="Bottom"/>
</StackPanel>
<TextBlock Text="{x:Bind _vm.Device1.LatestReading.reading ,
Mode=OneWay, FallbackValue=404}" Foreground="White"
HorizontalAlignment="Right" FontSize="16"
VerticalAlignment="Bottom" Padding="4"/>
</Grid>
<Grid x:Name="Gallery2Grid" Grid.Row="1" Grid.Column="1"
Background="DarkRed" 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">
<StackPanel Margin="12" VerticalAlignment="Bottom">
<TextBlock x:Name="Gallery2Time" Foreground="White" FontSize="24"
Text="{x:Bind _vm.GalleryDevice1.LatestStatus.DateOfStatusRecording,
Mode=OneWay, FallbackValue=444}"
Text="{x:Bind _vm.Device5.LatestStatus.time,
Mode=OneWay, FallbackValue=404}"
/>
<TextBlock Text="Gallery2" Foreground="White" FontSize="36"
<TextBlock Text="Gallery 2" Foreground="White" FontSize="36"
VerticalAlignment="Bottom"/>
</StackPanel>
<TextBlock Text="{x:Bind _vm.Device2.LatestReading.reading ,
Mode=OneWay, FallbackValue=404}" Foreground="White"
HorizontalAlignment="Right" FontSize="16"
VerticalAlignment="Bottom" Padding="4"/>
</Grid>
<Grid x:Name="Gallery3Grid" Grid.Row="1" Grid.Column="2"
Background="DarkRed" 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">
<StackPanel Margin="12" VerticalAlignment="Bottom">
<TextBlock x:Name="Gallery3Time" Foreground="White" FontSize="24"
Text="{x:Bind _vm.GalleryDevice1.LatestStatus.DateOfStatusRecording,
Mode=OneWay, FallbackValue=444}" />
<TextBlock Text="Gallery3" Foreground="White" FontSize="36"
Text="{x:Bind _vm.Device6.LatestStatus.time,
Mode=OneWay, FallbackValue=404}" />
<TextBlock Text="Gallery 3" Foreground="White" FontSize="36"
VerticalAlignment="Bottom"/>
</StackPanel>
<TextBlock Text="{x:Bind _vm.Device3.LatestReading.reading ,
Mode=OneWay, FallbackValue=404}" Foreground="White"
HorizontalAlignment="Right" FontSize="16"
VerticalAlignment="Bottom" Padding="4"/>
</Grid>
</Grid>

75
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.
}
}

30
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;
}
}
}

15
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; }
}
}

13
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;
}
}
}

93
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<FactoryDevice> 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<Device> 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<GalleryDevice> 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<LightReading>(json);
return reading;
}
static DeviceStatus MapToDeviceStatus(string json) {
var status = JsonSerializer.Deserialize<DeviceStatus>(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;
}
}
}

6
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<string> GetLightReading(string url, int deviceId) {
public static async Task<string> 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;
}
}

88
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");
}
}
}