Craig Oates
4 years ago
9 changed files with 216 additions and 185 deletions
@ -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; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,43 +1,78 @@ |
|||||||
using EyesAndEars.UWP.Models; |
using EyesAndEars.UWP.Models; |
||||||
using EyesAndEars.UWP.ViewModels; |
|
||||||
using System; |
using System; |
||||||
|
using System.Diagnostics; |
||||||
|
using System.Text.Json; |
||||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||||
|
using Windows.UI; |
||||||
|
using Windows.UI.Xaml.Media; |
||||||
|
|
||||||
namespace EyesAndEars.UWP.Services { |
namespace EyesAndEars.UWP.Services { |
||||||
public static class DataServices { |
public static class DataServices { |
||||||
|
|
||||||
public static async Task<FactoryDevice> UpdateFactoryDevice(int deviceId) { |
public static async Task<Device> UpdateDevice(string url, int deviceId) { |
||||||
// This should cause an error right now -- deliberate for error checking. |
try { |
||||||
var t = await WebServices.GetLightReading("ht", deviceId); |
var id = MapFactoryDeviceToGalleyDevice(deviceId); |
||||||
// Need to map the json result to the models and display in view. |
var readingAPI = $"{url}/api/readings/latest/{id}"; |
||||||
return new FactoryDevice(); |
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) { |
static SolidColorBrush UpdateStatusColour(string status) { |
||||||
// This should cause an error right now -- deliberate for error checking. |
return status.Equals("on", StringComparison.OrdinalIgnoreCase) ? |
||||||
var t = await WebServices.GetLightReading("ht", deviceId); |
new SolidColorBrush(Colors.SeaGreen) : |
||||||
// Need to map the json result to the models and display in view. |
new SolidColorBrush(Colors.DarkRed); |
||||||
return new GalleryDevice(); |
|
||||||
} |
} |
||||||
|
|
||||||
public static MainPageVM CreateFallBackViewModel() { |
static int MapFactoryDeviceToGalleyDevice(int deviceId) { |
||||||
// "999" and "-" are acting as sentinals in this context. |
int id = 0; |
||||||
var r = new LightReading(0, 999, DateTime.Now); |
if (deviceId > 3) { |
||||||
var s = new DeviceStatus(0, "-", DateTime.Now); |
switch (deviceId) { |
||||||
var fD1 = new FactoryDevice(1, r, s); |
case 4: |
||||||
var fD2 = new FactoryDevice(2, r, s); |
id = 1; |
||||||
var fD3 = new FactoryDevice(3, r, s); |
break; |
||||||
var gD1 = new GalleryDevice(4, s); |
case 5: |
||||||
var gD2 = new GalleryDevice(5, s); |
id = 2; |
||||||
var gD3 = new GalleryDevice(6, s); |
break; |
||||||
var vm = new MainPageVM(); |
case 6: |
||||||
vm.FactoryDevice1 = fD1; |
id = 3; |
||||||
vm.FactoryDevice2 = fD2; |
break; |
||||||
vm.FactoryDevice3 = fD3; |
default: |
||||||
vm.GalleryDevice1 = gD1; |
break; |
||||||
vm.GalleryDevice2 = gD2; |
} |
||||||
vm.GalleryDevice3 = gD3; |
} |
||||||
return vm; |
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; |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
Reference in new issue