A UWP dashboard which provides live updates for the 'Personal Flash in Real-Time (Andy)' and 'Personal Flash in Real-Time (Tony)' artworks -- part of the 'Return to Ritherdon' project by Nicola Ellis. Documentation can be found at
https://git.abbether.net/return-to-ritherdon/rtr-docs/src/branch/master/eyes-and-ears
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.7 KiB
78 lines
2.7 KiB
using EyesAndEars.UWP.Models; |
|
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<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); |
|
} |
|
} |
|
|
|
static SolidColorBrush UpdateStatusColour(string status) { |
|
return status.Equals("on", StringComparison.OrdinalIgnoreCase) ? |
|
new SolidColorBrush(Colors.SeaGreen) : |
|
new SolidColorBrush(Colors.DarkRed); |
|
} |
|
|
|
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; |
|
} |
|
} |
|
}
|
|
|