1
0
Fork 0
Browse Source

add JSON types to rd.xml (release exceptions) and try-catch blocks.

pull/6/head
Craig Oates 4 years ago
parent
commit
a8147d1e75
  1. 2
      src/EyesAndEars.UWP/EyesAndEars.UWP/EyesAndEars.UWP.csproj
  2. 3
      src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml
  3. 35
      src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs
  4. 23
      src/EyesAndEars.UWP/EyesAndEars.UWP/Properties/Default.rd.xml
  5. 63
      src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs
  6. 11
      src/EyesAndEars.UWP/EyesAndEars.UWP/ViewModels/MainPageVM.cs

2
src/EyesAndEars.UWP/EyesAndEars.UWP/EyesAndEars.UWP.csproj

@ -108,6 +108,8 @@
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
<EnableGatekeeperAnalysis>true</EnableGatekeeperAnalysis>
<UseDotNetNativeToolchain>true</UseDotNetNativeToolchain>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>

3
src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml

@ -164,7 +164,8 @@
ToolTipService.ToolTip="This is a link to a webpage with information about Nicola and the project."/>
<TextBox x:Name="WebAddressBox" Grid.Column="1" FontSize="26"
Padding="20" Text="http://3.9.19.84" TextAlignment="Left"
Text="{x:Bind _vm.BaseURL , Mode=OneWay}"
Padding="20" TextAlignment="Left"
HorizontalAlignment="Stretch" TextWrapping="NoWrap"
Margin="0" VerticalAlignment="Center"
HorizontalContentAlignment="Left"

35
src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs

@ -1,6 +1,7 @@
using EyesAndEars.UWP.Services;
using EyesAndEars.UWP.ViewModels;
using System;
using System.Threading;
using Windows.ApplicationModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
@ -11,6 +12,7 @@ namespace EyesAndEars.UWP {
MainPageVM _vm = new MainPageVM();
DispatcherTimer _dispatcherTimer = new DispatcherTimer();
string _baseURL = "http://3.9.19.84";
public MainPage() {
InitializeComponent();
@ -20,14 +22,14 @@ namespace EyesAndEars.UWP {
}
void SetVersionNumber() {
Package package = Package.Current;
PackageId packageId = package.Id;
PackageVersion version = packageId.Version;
var package = Package.Current;
var packageId = package.Id;
var version = packageId.Version;
VersionNumber.Text = $"Ver: {version.Major}.{version.Minor}.{version.Build}.{version.Revision}";
}
void IntialiseDataContext() {
var url = WebAddressBox.Text;
async void IntialiseDataContext() {
_vm.BaseURL = await DataServices.GetBaseURLAsync();
_vm.Device1 = DataServices.CreateFallBackDevice(1);
_vm.Device2 = DataServices.CreateFallBackDevice(2);
_vm.Device3 = DataServices.CreateFallBackDevice(3);
@ -45,21 +47,22 @@ namespace EyesAndEars.UWP {
}
async void UpdateViewModel(object sender, object e) {
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);
var url = _vm.BaseURL;
if (!string.IsNullOrEmpty(url)) {
_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);
}
}
void RefreshButton_Click(object sender, RoutedEventArgs e) {
void RefreshButton_Click(object sender, RoutedEventArgs e) =>
IntialiseDataContext();
}
void SaveURLButton_Click(object sender, RoutedEventArgs e) {
// WORK ON THIS WENT YOU HAVE A LIVE SYSTEM WORKING WITH KNOWABLE DEFAULTS.
async void SaveURLButton_Click(object sender, RoutedEventArgs e) {
await DataServices.SaveBaseURLAsync(WebAddressBox.Text);
}
}
}

23
src/EyesAndEars.UWP/EyesAndEars.UWP/Properties/Default.rd.xml

@ -25,7 +25,26 @@
<!-- Add your application specific runtime directives here. -->
<!-- Note: The Inclusion of the JSON Serialization Converters.
===========================================================================
The Json.Serialization converts are needed so the app. can operate as "release".
Without them, the app. throw a System.Reflection.MissingMetadataException
when you try and run the app. using the "release" compiler setting.
I added the lines of code below based on the Issue raised and discussed on
GitHub. The link is https://github.com/dotnet/runtime/issues/978.
-->
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterString" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterUri" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterBoolean" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterInt32" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterInt16" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterByte" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterDecimal" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterGuid" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterEnum" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterDateTime" Dynamic="Required All" />
<Type Name="System.Text.Json.Serialization.Converters.JsonConverterDateTimeOffset" Dynamic="Required All" />
</Application>
</Directives>

63
src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs

@ -3,6 +3,7 @@ using System;
using System.Diagnostics;
using System.Text.Json;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Xaml.Media;
@ -22,15 +23,21 @@ namespace EyesAndEars.UWP.Services {
return new Device(deviceId, r, s, c);
}
catch (Exception e) {
Debug.WriteLine($"ERROR: {e.Message}");
Debug.WriteLine(e.Message);
return CreateFallBackDevice(deviceId);
}
}
static SolidColorBrush UpdateStatusColour(string status) {
return status.Equals("on", StringComparison.OrdinalIgnoreCase) ?
try {
return status.Equals("on", StringComparison.OrdinalIgnoreCase) ?
new SolidColorBrush(Colors.SeaGreen) :
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.
@ -78,13 +85,25 @@ namespace EyesAndEars.UWP.Services {
}
static LightReading MapToLightReading(string json) {
var reading = JsonSerializer.Deserialize<LightReading>(json);
return reading;
try {
var reading = JsonSerializer.Deserialize<LightReading>(json);
return reading;
}
catch (Exception e) {
Debug.WriteLine(e.Message);
return CreateDefaultLightReading();
}
}
static DeviceStatus MapToDeviceStatus(string json) {
var status = JsonSerializer.Deserialize<DeviceStatus>(json);
return status;
try {
var status = JsonSerializer.Deserialize<DeviceStatus>(json);
return status;
}
catch (Exception e) {
Debug.WriteLine(e.Message);
return CreateDefaultDeviceStatus();
}
}
public static Device CreateFallBackDevice(int deviceId) {
@ -95,5 +114,37 @@ namespace EyesAndEars.UWP.Services {
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<string> 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 "";
}
}
}
}

11
src/EyesAndEars.UWP/EyesAndEars.UWP/ViewModels/MainPageVM.cs

@ -28,6 +28,17 @@ namespace EyesAndEars.UWP.ViewModels {
Device3 = g3;
}
string _baseURL;
public string BaseURL {
get { return _baseURL; }
set {
if (value != _baseURL) {
_baseURL = value;
NotifyPropertyChanged("BaseURL");
}
}
}
Device _device1;
public Device Device1 {
get { return _device1; }