Skip to content
This repository has been archived by the owner on Aug 9, 2020. It is now read-only.

Commit

Permalink
* Go directly from ConsoleListPage to StreamPage (solves issue #17)
Browse files Browse the repository at this point in the history
* Add PowerOn functionality (solves issue #16)
* Preparation for adding StreamPage as regular navigatable page (issue #19)
  • Loading branch information
tuxuser committed Apr 29, 2019
1 parent 3f1e3fa commit a239456
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 189 deletions.
1 change: 0 additions & 1 deletion xnano/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry)
containerRegistry.RegisterForNavigation<AuthenticationPage>();
containerRegistry.RegisterForNavigation<ConsoleListPage>();
containerRegistry.RegisterForNavigation<EnterIpAddressPopup>();
containerRegistry.RegisterForNavigation<ConnectionPage>();
containerRegistry.RegisterForNavigation<StreamPage>();

containerRegistry.RegisterPopupNavigationService();
Expand Down
154 changes: 0 additions & 154 deletions xnano/ViewModels/ConnectionPageViewModel.cs

This file was deleted.

158 changes: 152 additions & 6 deletions xnano/ViewModels/ConsoleListPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Prism.Services;

using xnano.Models;
using SmartGlass;
using SmartGlass.Nano;

namespace xnano.ViewModels
{
Expand All @@ -30,6 +32,9 @@ public string StatusMessage
public ICommand DeleteItemCommand { get; }
public ICommand ItemTappedCommand { get; }

public ICommand PowerOnCommand { get; }
public ICommand ConnectCommand { get; }

public ConsoleListPageViewModel(INavigationService navigationService,
IPageDialogService dialogService)
: base(navigationService)
Expand All @@ -54,9 +59,48 @@ public ConsoleListPageViewModel(INavigationService navigationService,
IsBusy = false;
});

ItemTappedCommand = new Command<SmartGlass.Device>(async (selectedItem) =>
PowerOnCommand = new Command<SmartGlass.Device>(async dev =>
{
var doPoweron = await _dialogService.DisplayAlertAsync(
"Console unavailable", "Do you want to poweron?", "Yes", "No");
if (!doPoweron)
return;
IsBusy = true;
StatusMessage = "Powering on console...";
await dev.PowerOnAsync();
StatusMessage = "Idle";
IsBusy = false;
});

ConnectCommand = new Command<SmartGlass.Device>(async dev =>
{
await NavigateToConnectionPage(selectedItem);
IsBusy = true;
StatusMessage = "Connecting to console...";
var success = await Task.Run<bool>(
async () => await ConnectToConsole(dev));
StatusMessage = "Idle";
IsBusy = false;
if (!success)
return;
Xamarin.Forms.Device.BeginInvokeOnMainThread(
async () => await NavigateToStreamPage(dev));
});

ItemTappedCommand = new Command<SmartGlass.Device>(dev =>
{
if (dev.State == DeviceState.Available)
{
ConnectCommand.Execute(dev);
}
else
{
PowerOnCommand.Execute(dev);
}
});

DeleteItemCommand = new Command<SmartGlass.Device>(DeleteConsoleEntry);
Expand Down Expand Up @@ -116,16 +160,118 @@ await _dialogService.DisplayAlertAsync(
}
}

async Task<bool> InitializeSmartGlass(string address)
{
string lastStatus = String.Empty;
try
{
lastStatus = "Connecting to console";
var client = await SmartGlassClient.ConnectAsync(address);

lastStatus = "Waiting for Broadcast channel";
await client.BroadcastChannel.WaitForEnabledAsync(
TimeSpan.FromSeconds(5));

if (!client.BroadcastChannel.Enabled)
{
throw new NotSupportedException("Broadcast channel is not enabled");
}

lastStatus = "Starting gamestream via Broadcast channel";
var config = Services.SmartGlassConnection.Instance.GamestreamConfiguration;
var session = await client.BroadcastChannel
.StartGamestreamAsync(config);

Services.SmartGlassConnection.Instance.Session = session;
Services.SmartGlassConnection.Instance.SgClient = client;
}
catch (TimeoutException)
{
Xamarin.Forms.Device.BeginInvokeOnMainThread(
async () => await ShowErrorDisplayAlert($"{lastStatus} timed out!"));
return false;
}
catch (NotSupportedException e)
{
Xamarin.Forms.Device.BeginInvokeOnMainThread(
async () => await ShowErrorDisplayAlert($"{e.Message}"));
return false;
}
catch (Exception)
{
Xamarin.Forms.Device.BeginInvokeOnMainThread(
async () => await ShowErrorDisplayAlert($"Unknown error: {lastStatus}"));
return false;
}

return true;
}

async Task<bool> InitializeGamestreaming(string address)
{
string lastStatus = String.Empty;
var client = new NanoClient(address,
Services.SmartGlassConnection.Instance.Session);

try
{
lastStatus = "Initializing Nano protocol";
await client.InitializeProtocolAsync();

var videoFmt = client.VideoFormats[0];
var audioFmt = client.AudioFormats[0];

lastStatus = "Initializing Nano stream";
await client.InitializeStreamAsync(audioFmt, videoFmt);

Services.SmartGlassConnection.Instance.NanoClient = client;
}
catch (TimeoutException)
{
Xamarin.Forms.Device.BeginInvokeOnMainThread(
async () => await ShowErrorDisplayAlert($"{lastStatus} timed out!"));
return false;
}
catch (Exception)
{
Xamarin.Forms.Device.BeginInvokeOnMainThread(
async () => await ShowErrorDisplayAlert($"Unknown error: {lastStatus}"));
return false;
}

return true;
}

async Task<bool> ConnectToConsole(SmartGlass.Device device)
{
var address = device.Address.ToString();
if(!await InitializeSmartGlass(address))
{
return false;
}

if (!await InitializeGamestreaming(address))
{
return false;
}

return true;
}

async Task ShowErrorDisplayAlert(string message)
{
await _dialogService.DisplayAlertAsync("Error", message, "OK");
}

async Task ShowAddConsolePopup()
{
await _navigationService.NavigateAsync(nameof(Views.EnterIpAddressPopup));
}

async Task NavigateToConnectionPage(SmartGlass.Device device)
async Task NavigateToStreamPage(SmartGlass.Device device)
{
var parameters = new NavigationParameters();
parameters.Add("console", device);
await _navigationService.NavigateAsync(nameof(Views.ConnectionPage), parameters);
Services.SmartGlassConnection.Instance.IPAddress = device.Address.ToString();
await _navigationService.NavigateAsync(nameof(Views.StreamPage));
}
}
}
12 changes: 0 additions & 12 deletions xnano/Views/ConnectionPage.xaml

This file was deleted.

13 changes: 0 additions & 13 deletions xnano/Views/ConnectionPage.xaml.cs

This file was deleted.

3 changes: 0 additions & 3 deletions xnano/xnano.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
<EmbeddedResource Update="Views\AuthenticationPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Views\ConnectionPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Views\ConsoleListPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down

0 comments on commit a239456

Please sign in to comment.