using EyesAndEars.UWP.Models; using System; using System.Diagnostics; using System.Text.Json; using System.Threading.Tasks; using Windows.Storage; using Windows.UI; using Windows.UI.Xaml.Media; namespace EyesAndEars.UWP.Services { public static class DataServices { public static async Task UpdateDevice(string url, int deviceId) { try { var id = MapFactoryDeviceToGalleyDevice(deviceId); // Has note. 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, r.reading); return new Device(deviceId, r, s, c); } catch (Exception e) { Debug.WriteLine(e.Message); return CreateFallBackDevice(deviceId); } } static SolidColorBrush UpdateStatusColour(string status, int reading) { try { if (status.Equals("on", StringComparison.OrdinalIgnoreCase)) { /* Note: Reading values breakdown. * ======================================= * 1. When testing the light meters, the base line for normal * light is 48 or below. Anything above this is when one of * the guys in the factory was welding. Becuase of this, * I have set 0 to 48 as the 'default' status on the * dashboard. * * 2. During testing we noticed the light meters would * return 'negative light' values. There is a reason for * this but that is out of the scope of this project. This * project just needs to process the data. The negative * light values are when the lights in the factory are off. * The light meter is still running but there is no light * to measure. So, everything is fine but it looks broken. * The 'LightSkyBlue' colour is used to help relay this * bit of information. */ if (reading > 0 && reading < 48) // On but no weld detected. return new SolidColorBrush(Colors.LightSeaGreen); if (reading > 48) // On and weld detected. return new SolidColorBrush(Colors.DarkSeaGreen); else { // On but factory lights are off. return new SolidColorBrush(Colors.LightSkyBlue); } } else return new SolidColorBrush(Colors.DarkRed); } catch (Exception e) { Debug.WriteLine(e.Message); return new SolidColorBrush(Colors.DarkOrange); } } /* Note: The Purpose of Mapping a Factory to a Gallery Device. * ================================================================ * Every device in the project is paired up with another. * Factory1 => Gallery1 | Device1 => Device4 * Factory2 => Gallery2 | Device2 => Device5 * Factory3 => Gallery3 | Device3 => Device6 (Note in use at this time) * At the moment, when a device completes a status update, each one * is ran throught the "UpdateDevice" function (above). This means * the latest light reading for a light meter (factory) must be * retrieved when a galley-based device's status is updated. * From a strict point-of-view, the gallery device does not need it * so I should/could seperate out this functionality into seperate * functions. For now though, a light reading is still needed to be * retrieved because of the way the functions are called. * This function just maps/pairs the (factory) light reading to the * appropriate gallery device. By doing this, the gallery device * has extra information on its paired up light meter. One way I * have used that information is to show a small piece of text in * the gallery status box to quickly check the accuracy of the * synchronisation between the factory and gallery devices. */ 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) { try { var reading = JsonSerializer.Deserialize(json); return reading; } catch (Exception e) { Debug.WriteLine(e.Message); return CreateDefaultLightReading(); } } static DeviceStatus MapToDeviceStatus(string json) { try { var status = JsonSerializer.Deserialize(json); return status; } catch (Exception e) { Debug.WriteLine(e.Message); return CreateDefaultDeviceStatus(); } } 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; } static LightReading CreateDefaultLightReading() => new LightReading { id = 0, reading = 999, time = DateTime.Now }; static DeviceStatus CreateDefaultDeviceStatus() => new DeviceStatus { id = 0, status = "err", time = DateTime.Now }; public static async Task SaveBaseURLAsync(string contents) { try { var storageFolder = ApplicationData.Current.LocalFolder; var baseURLFile = await storageFolder.CreateFileAsync("baseURL.txt", CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(baseURLFile, contents); } catch (Exception e) { Debug.WriteLine(e.Message); } } public static async Task GetBaseURLAsync() { try { var storageFolder = ApplicationData.Current.LocalFolder; var baseURLFile = await storageFolder.GetFileAsync("baseURL.txt"); var text = await FileIO.ReadTextAsync(baseURLFile); return text; } catch (Exception e) { Debug.WriteLine(e.Message); return ""; } } } }