1
0
Fork 0
Browse Source

implemete G.U.I. update cycle/timer.

pull/6/head
Craig Oates 4 years ago
parent
commit
9cec79abf9
  1. 40
      src/EyesAndEars.UWP/EyesAndEars.UWP/App.xaml.cs
  2. 3
      src/EyesAndEars.UWP/EyesAndEars.UWP/EyesAndEars.UWP.csproj
  3. 42
      src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml
  4. 93
      src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml.cs
  5. 2
      src/EyesAndEars.UWP/EyesAndEars.UWP/Models/DeviceStatus.cs
  6. 1
      src/EyesAndEars.UWP/EyesAndEars.UWP/Properties/AssemblyInfo.cs
  7. 7
      src/EyesAndEars.UWP/EyesAndEars.UWP/Services/DataServices.cs
  8. 6
      src/EyesAndEars.UWP/EyesAndEars.UWP/ViewModels/MainPageVM.cs

40
src/EyesAndEars.UWP/EyesAndEars.UWP/App.xaml.cs

@ -1,33 +1,20 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace EyesAndEars.UWP
{
namespace EyesAndEars.UWP {
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : Application
{
sealed partial class App : Application {
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
public App() {
this.InitializeComponent();
this.Suspending += OnSuspending;
}
@ -37,21 +24,18 @@ namespace EyesAndEars.UWP
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="e">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
protected override void OnLaunched(LaunchActivatedEventArgs e) {
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
if (rootFrame == null) {
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) {
//TODO: Load state from previously suspended application
}
@ -59,10 +43,8 @@ namespace EyesAndEars.UWP
Window.Current.Content = rootFrame;
}
if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
if (e.PrelaunchActivated == false) {
if (rootFrame.Content == null) {
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
@ -78,8 +60,7 @@ namespace EyesAndEars.UWP
/// </summary>
/// <param name="sender">The Frame which failed navigation</param>
/// <param name="e">Details about the navigation failure</param>
void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
void OnNavigationFailed(object sender, NavigationFailedEventArgs e) {
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
}
@ -90,8 +71,7 @@ namespace EyesAndEars.UWP
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>
private void OnSuspending(object sender, SuspendingEventArgs e)
{
private void OnSuspending(object sender, SuspendingEventArgs e) {
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
deferral.Complete();

3
src/EyesAndEars.UWP/EyesAndEars.UWP/EyesAndEars.UWP.csproj

@ -169,6 +169,9 @@
<PackageReference Include="System.Text.Json">
<Version>4.7.0</Version>
</PackageReference>
<PackageReference Include="System.Threading.Timer">
<Version>4.3.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup />
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">

42
src/EyesAndEars.UWP/EyesAndEars.UWP/MainPage.xaml

@ -47,9 +47,13 @@
<Grid x:Name="Factory1Grid" Grid.Row="0" Grid.Column="0"
Background="DarkRed" 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}"
<TextBlock x:Name="Factory1Time"
Text="{x:Bind _vm.FactoryDevice1.LatestStatus.DateOfStatusRecording,
Mode=OneWay, FallbackValue=444}"
Foreground="White" FontSize="24"/>
<TextBlock x:Name="Factory1Reading" Text="{x:Bind _vm.FactoryDevice1.LatestReading.Reading}" Foreground="White"
<TextBlock x:Name="Factory1Reading"
Text="{x:Bind _vm.FactoryDevice1.LatestReading.Reading ,
Mode=OneWay, FallbackValue=444}" Foreground="White"
VerticalAlignment="Bottom" FontSize="144"/>
<TextBlock Text="Factory1" Foreground="White" FontSize="36"
VerticalAlignment="Bottom"/>
@ -59,9 +63,11 @@
<Grid x:Name="Factory2Grid" Grid.Row="0" Grid.Column="1"
Background="DarkRed" Margin="12,0,12,12" Width="300" Height="300">
<StackPanel Margin="12" VerticalAlignment="Stretch">
<TextBlock x:Name="Factory2Time" Text="2020-23-32 23:23"
Foreground="White" FontSize="24"/>
<TextBlock x:Name="Factory2Reading" Text="00" Foreground="White"
<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"
VerticalAlignment="Bottom" FontSize="144"/>
<TextBlock Text="Factory2" Foreground="White" FontSize="36"
VerticalAlignment="Bottom"/>
@ -71,10 +77,14 @@
<Grid x:Name="Factory3Grid" Grid.Row="0" Grid.Column="2"
Background="DarkRed" Margin="0,0,0,12" Width="300" Height="300">
<StackPanel Margin="12" VerticalAlignment="Stretch">
<TextBlock x:Name="Factory3Time" Text="2020-23-32 23:23"
<TextBlock x:Name="Factory3Time"
Text="{x:Bind _vm.FactoryDevice3.LatestStatus.DateOfStatusRecording,
Mode=OneWay, FallbackValue=444}"
Foreground="White" FontSize="24"/>
<TextBlock x:Name="Factory3Reading" Text="00" Foreground="White"
VerticalAlignment="Bottom" FontSize="144"/>
<TextBlock x:Name="Factory3Reading" Foreground="White"
Text="{x:Bind _vm.FactoryDevice3.LatestReading.Reading,
Mode=OneWay, FallbackValue=444}" FontSize="144"
VerticalAlignment="Bottom"/>
<TextBlock Text="Factory3" Foreground="White" FontSize="36"
VerticalAlignment="Bottom"/>
</StackPanel>
@ -83,8 +93,9 @@
<Grid x:Name="Gallery1Grid" Grid.Row="1" Grid.Column="0"
Background="DarkRed" Margin="0,0,0,12" Width="300" Height="150">
<StackPanel Margin="12" VerticalAlignment="Bottom">
<TextBlock x:Name="Gallery1Time" Text="2020-23-32 23:23"
Foreground="White" FontSize="24"/>
<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"
VerticalAlignment="Bottom"/>
</StackPanel>
@ -93,8 +104,10 @@
<Grid x:Name="Gallery2Grid" Grid.Row="1" Grid.Column="1"
Background="DarkRed" Margin="0,0,0,12" Width="300" Height="150">
<StackPanel Margin="12" VerticalAlignment="Bottom">
<TextBlock x:Name="Gallery2Time" Text="2020-23-32 23:23"
Foreground="White" FontSize="24"/>
<TextBlock x:Name="Gallery2Time" Foreground="White" FontSize="24"
Text="{x:Bind _vm.GalleryDevice1.LatestStatus.DateOfStatusRecording,
Mode=OneWay, FallbackValue=444}"
/>
<TextBlock Text="Gallery2" Foreground="White" FontSize="36"
VerticalAlignment="Bottom"/>
</StackPanel>
@ -103,8 +116,9 @@
<Grid x:Name="Gallery3Grid" Grid.Row="1" Grid.Column="2"
Background="DarkRed" Margin="0,0,0,12" Width="300" Height="150">
<StackPanel Margin="12" VerticalAlignment="Bottom">
<TextBlock x:Name="Gallery3Time" Text="2020-23-32 23:23"
Foreground="White" FontSize="24"/>
<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"
VerticalAlignment="Bottom"/>
</StackPanel>

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

@ -1,32 +1,35 @@
using EyesAndEars.UWP.ViewModels;
using EyesAndEars.UWP.Services;
using EyesAndEars.UWP.Services;
using EyesAndEars.UWP.ViewModels;
using System;
using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using EyesAndEars.UWP.Models;
using System;
using System.Threading.Tasks;
using System.Threading;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace EyesAndEars.UWP
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private MainPageVM _vm = new MainPageVM();
public MainPage()
{
this.InitializeComponent();
IntialiseDataContextAsync();
// Need to set a refresh timer so it updates every 1 second or so...
namespace EyesAndEars.UWP {
public sealed partial class MainPage : Page {
MainPageVM _vm = new MainPageVM();
DispatcherTimer _dispatcherTimer = new DispatcherTimer();
public MainPage() {
InitializeComponent();
IntialiseDataContext();
IntialiseRefreshTime();
}
private void IntialiseDataContextAsync() {
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;
@ -36,15 +39,55 @@ namespace EyesAndEars.UWP
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;
//throw;
}
}
void IntialiseRefreshTime() {
// TimeSpan Intervals: Days, Hours, Minutes, Seconds, Milliseconds.
_dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
_dispatcherTimer.Tick += UpdateViewModel;
_dispatcherTimer.Start();
}
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();
}
}
private 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) {

2
src/EyesAndEars.UWP/EyesAndEars.UWP/Models/DeviceStatus.cs

@ -9,7 +9,7 @@ namespace EyesAndEars.UWP.Models {
public readonly DateTime DateOfStatusRecording;
public DeviceStatus() { }
public DeviceStatus(int id, string status, DateTime dateOfStatusRecording) {
Id = id;
Status = status;

1
src/EyesAndEars.UWP/EyesAndEars.UWP/Properties/AssemblyInfo.cs

@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following

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

@ -7,15 +7,16 @@ namespace EyesAndEars.UWP.Services {
public static class DataServices {
public static async Task<FactoryDevice> UpdateFactoryDevice(int deviceId) {
//var t = await WebServices.GetLightReading("ht", 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<GalleryDevice> UpdateGalleryDevice(int deviceId) {
//var t = await WebServices.GetLightReading("ht", 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();
}

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

@ -19,7 +19,7 @@ namespace EyesAndEars.UWP.ViewModels {
public MainPageVM(FactoryDevice f1, FactoryDevice f2, FactoryDevice f3,
GalleryDevice g1, GalleryDevice g2, GalleryDevice g3) {
FactoryDevice1 = f1;
FactoryDevice2 = f2;
FactoryDevice3 = f3;
@ -29,10 +29,10 @@ namespace EyesAndEars.UWP.ViewModels {
}
FactoryDevice _factoryDevice1;
public FactoryDevice FactoryDevice1 {
public FactoryDevice FactoryDevice1 {
get { return _factoryDevice1; }
set {
if(value != _factoryDevice1) {
if (value != _factoryDevice1) {
_factoryDevice1 = value;
NotifyPropertyChanged("FactoryDevice1");
}