From 2cfa495a24fab2b143292ab9c65e53834e406b46 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Mon, 7 Sep 2020 13:14:03 -0500 Subject: [PATCH] Add WorkspaceConnectionError Event --- QControlKit/QClient.cs | 10 ++--- QControlKit/QControlKit.csproj | 2 +- QControlKit/QServer.cs | 4 +- QControlKit/QWorkspace.cs | 38 +++++++++++++------ .../QControlKitXamDemo/CueListPage.xaml.cs | 31 ++++++++++++--- .../QControlKitXamDemo/MainPage.xaml.cs | 23 +++++++++-- 6 files changed, 80 insertions(+), 28 deletions(-) diff --git a/QControlKit/QClient.cs b/QControlKit/QClient.cs index a9a35a1..be2a7e6 100644 --- a/QControlKit/QClient.cs +++ b/QControlKit/QClient.cs @@ -93,7 +93,7 @@ namespace QControlKit } else { - Log.Debug($"[client] unhandled reply from cue: Type: {data.Type} value: {message.response}"); + Log.Warning($"[client] unhandled reply from cue: Type: {data.Type} value: {message.response}"); } } @@ -107,6 +107,7 @@ namespace QControlKit } else if (message.IsConnect) { + //Log.Debug($"[client] Connection message received: {message.response}"); if (message.response.ToString() == "ok") OnWorkspaceConnected(); else @@ -114,7 +115,7 @@ namespace QControlKit } else { - Log.Debug($"[client] unhandled reply message: {message.address}"); + Log.Warning($"[client] unhandled reply message: {message.address}"); } } else if(message.IsUpdate) { @@ -154,17 +155,16 @@ namespace QControlKit } else if (message.IsDisconnect) { - Log.Debug($"[client] disconnect message received: {message.address}"); OnWorkspaceDisconnected(); } else { - Log.Debug($"[client] unhandled update message: {message.address}"); + Log.Warning($"[client] unhandled update message: {message.address}"); } } else { - Log.Debug($"[client] unhandled message: {message.address}"); + Log.Warning($"[client] unhandled message: {message.address}"); } } diff --git a/QControlKit/QControlKit.csproj b/QControlKit/QControlKit.csproj index a9bc788..07418be 100644 --- a/QControlKit/QControlKit.csproj +++ b/QControlKit/QControlKit.csproj @@ -6,7 +6,7 @@ Joel Wetzell 1.0.0-devel true - 0.0.1 + 0.0.3 QControlKit diff --git a/QControlKit/QServer.cs b/QControlKit/QServer.cs index 5591e4b..f7c6a9e 100644 --- a/QControlKit/QServer.cs +++ b/QControlKit/QServer.cs @@ -1,6 +1,4 @@ -//General information about a QLab Workspace -using Serilog; -using System.Collections.Concurrent; +using Serilog; using System.Collections.Generic; using Zeroconf; diff --git a/QControlKit/QWorkspace.cs b/QControlKit/QWorkspace.cs index 6dfca0c..f3515e4 100644 --- a/QControlKit/QWorkspace.cs +++ b/QControlKit/QWorkspace.cs @@ -1,4 +1,4 @@ -//TODO: connect methods, cue property fetch methods, everything else +//TODO: workspace connection error handlers using Newtonsoft.Json; using Serilog; using System; @@ -32,6 +32,8 @@ namespace QControlKit public event QWorkspaceUpdatedHandler WorkspaceUpdated; public event QCueListChangedPlaybackPositionHandler CueListChangedPlaybackPosition; + public event QWorkspaceConnectionErrorHandler WorkspaceConnectionError; + private void Init() { name = ""; @@ -52,9 +54,6 @@ namespace QControlKit public QWorkspace(QWorkspaceInfo workspaceInfo, QServer server) { - - - if (workspaceInfo.version.Length > 0) version = workspaceInfo.version; @@ -143,19 +142,30 @@ namespace QControlKit #region Connection/reconnection - public void connectWithPasscode(string passcode) + public void connect(string passcode = null) { - //TODO + Log.Debug($"[workspace] connecting to {this.server.host} with passcode: {passcode}"); + + if(hasPasscode && passcode == null) + { + Log.Error($"[workspace] *** workspace <{name}> requires a passcode but none was supplied."); + OnWorkspaceConnectionError(this, new QWorkspaceConnectionErrorArgs { status = "badpass" }); + return; + } + if (!client.connect()) { - Log.Error($"[workspace] *** Error: couldn't connect to server client is not connected"); + Log.Error($"[workspace] *** couldn't connect to server client is not connected"); return; } //save password for reuse this.passcode = passcode; Log.Information("[workspace] connecting..."); - client.sendMessage($"{workspacePrefix}/connect",passcode); + if (passcode != null) + client.sendMessage($"{workspacePrefix}/connect", passcode); + else + client.sendMessage($"{workspacePrefix}/connect"); } private void finishConnection() @@ -173,7 +183,10 @@ namespace QControlKit { if (connected) return; - connectWithPasscode(passcode); + if(hasPasscode) + connect(passcode); + else + connect(); //todo } @@ -384,12 +397,15 @@ namespace QControlKit if (args.status.Equals("badpass")) { //clear passcode if there was one set in the connect() method - this.passcode = ""; + this.passcode = null; Log.Error($"[workspace] *** Password for workspace {name} was incorrect!"); - } + } else + { Log.Error($"[workspace] *** Unable to connect to workspace: {name} on server: {server.name}"); + } + WorkspaceConnectionError?.Invoke(this, new QWorkspaceConnectionErrorArgs { status = args.status }); } private void OnWorkspaceConnected(object source, QWorkspaceConnectedArgs args) diff --git a/QControlKitXamDemo/QControlKitXamDemo/CueListPage.xaml.cs b/QControlKitXamDemo/QControlKitXamDemo/CueListPage.xaml.cs index 6bd2563..bb18835 100644 --- a/QControlKitXamDemo/QControlKitXamDemo/CueListPage.xaml.cs +++ b/QControlKitXamDemo/QControlKitXamDemo/CueListPage.xaml.cs @@ -1,5 +1,4 @@ - -using System.Collections.Generic; +using System.Collections.Generic; using System.Threading.Tasks; using QControlKit; using QControlKitXamDemo.ViewModels; @@ -12,17 +11,39 @@ namespace QControlKitXamDemo { private QWorkspace connectedWorkspace; private Dictionary cueGridDict = new Dictionary(); - public CueListPage(QWorkspace workspace) + public CueListPage(QWorkspace workspace, string passcode = null) { InitializeComponent(); - connectedWorkspace = workspace; connectedWorkspace.WorkspaceUpdated += Workspace_WorkspaceUpdated; - connectedWorkspace.connectWithPasscode("1234"); + connectedWorkspace.WorkspaceConnectionError += ConnectedWorkspace_WorkspaceConnectionError; + connectedWorkspace.connect(passcode); connectedWorkspace.defaultSendUpdatesOSC = true; connectedWorkspace.CueListChangedPlaybackPosition += ConnectedWorkspace_CueListChangedPlaybackPosition; } + protected override void OnDisappearing() + { + base.OnDisappearing(); + connectedWorkspace.disconnect(); + connectedWorkspace.WorkspaceUpdated -= Workspace_WorkspaceUpdated; + connectedWorkspace.WorkspaceConnectionError -= ConnectedWorkspace_WorkspaceConnectionError; + connectedWorkspace.CueListChangedPlaybackPosition -= ConnectedWorkspace_CueListChangedPlaybackPosition; + } + + private async void ConnectedWorkspace_WorkspaceConnectionError(object source, QWorkspaceConnectionErrorArgs args) + { + System.Console.WriteLine("WorkspaceConnectionError Called"); + if (args.status.Equals("badpass")) + { + string passcode = await DisplayPromptAsync("Passcode was incorrect.", "Try Again", maxLength: 4, keyboard: Keyboard.Numeric); + if (passcode != null) + { + connectedWorkspace.connect(passcode); + } + } + } + void ConnectedWorkspace_CueListChangedPlaybackPosition(object source, QCueListChangedPlaybackPositionArgs args) { Device.BeginInvokeOnMainThread(() => diff --git a/QControlKitXamDemo/QControlKitXamDemo/MainPage.xaml.cs b/QControlKitXamDemo/QControlKitXamDemo/MainPage.xaml.cs index 1f48191..f67befc 100644 --- a/QControlKitXamDemo/QControlKitXamDemo/MainPage.xaml.cs +++ b/QControlKitXamDemo/QControlKitXamDemo/MainPage.xaml.cs @@ -25,9 +25,26 @@ namespace QControlKitXamDemo async void ServerListView_ItemSelected(object sender, SelectedItemChangedEventArgs e) { - QWorkspace selectedWorkspace = e.SelectedItem as QWorkspace; - Console.WriteLine($"[demo] workspace: {selectedWorkspace.nameWithoutPathExtension} has been selected"); - await Navigation.PushAsync(new CueListPage(selectedWorkspace)); + if(e.SelectedItem != null) + { + QWorkspace selectedWorkspace = e.SelectedItem as QWorkspace; + Console.WriteLine($"[demo] workspace: {selectedWorkspace.nameWithoutPathExtension} has been selected"); + if (selectedWorkspace.hasPasscode) + { + string passcode = await DisplayPromptAsync("Workspace has passcode", "Enter Passcode", maxLength: 4, keyboard: Keyboard.Numeric); + if (passcode != null) + { + + await Navigation.PushAsync(new CueListPage(selectedWorkspace, passcode)); + } + } + else + { + await Navigation.PushAsync(new CueListPage(selectedWorkspace)); + + } + serverListView.SelectedItem = null; + } } private void Browser_ServerUpdatedWorkspaces(object source, QServerUpdatedArgs args)