From df5c2df43bbfabee7f2c6502b9a6813a5f602cc6 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Wed, 6 Mar 2019 11:37:00 +0100 Subject: [PATCH] reinclusione task run su libreria... --- MConnectSDK/MConnectClient.cs | 87 +++++++++++++++++++++++++++++++++++ MConnectSDK/Utils.cs | 4 ++ TestClient/MainForm.cs | 51 +++++++++++++++++--- 3 files changed, 136 insertions(+), 6 deletions(-) diff --git a/MConnectSDK/MConnectClient.cs b/MConnectSDK/MConnectClient.cs index 4766651..98f525f 100644 --- a/MConnectSDK/MConnectClient.cs +++ b/MConnectSDK/MConnectClient.cs @@ -2,7 +2,9 @@ using NLog; using System; using System.Collections.Generic; +using System.Text; using System.Threading; +using System.Threading.Tasks; namespace MConnectSDK { @@ -503,6 +505,7 @@ namespace MConnectSDK { // init output userAuthData answ = new userAuthData(); + StringBuilder sb = new StringBuilder(); // init altro reqStatus = preCheckStatus(); // verifico autorizzazione... se non c'è TENTO auth... @@ -528,6 +531,10 @@ namespace MConnectSDK // controllo SE sia stato autorizzato if (tokenRetVal == 200) { + sb.AppendLine("--------------------------------------------------"); + sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | OAuth process DONE", DateTime.Now)); + sb.AppendLine("--------------------------------------------------"); + answ.outMessage = sb.ToString(); // riporto in risposta i dati VUOTI... answ.deviceCode = ""; answ.userCode = ""; @@ -569,6 +576,10 @@ namespace MConnectSDK answ.deviceCode = tokResp.device_code; answ.userCode = tokResp.user_code; answ.qrCode = string.Format(qrAuthBaseUrl, tokResp.user_code); + sb.AppendLine("--------------------------------------------------"); + sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | Try OAuth process", DateTime.Now)); + sb.AppendLine("--------------------------------------------------"); + answ.outMessage = sb.ToString(); } } // ritorno! @@ -701,6 +712,82 @@ namespace MConnectSDK return answ; } + + /// + /// Task x AUTH + /// + /// + /// + /// + public async Task doOAuthCycleTask(CancellationToken token, IProgress progress) + { +#if false + if (progress == null) + { + throw new ArgumentNullException(nameof(progress)); + } +#endif + userAuthData authData = new userAuthData(); + StringBuilder sb = new StringBuilder(); + // fino a quanod NON è auth --> cicla... + while (!reqStatus.isHmiEnrolled) + { + await Task.Run(() => + { + authData = tryAuthorize(); + // recupero nuova reqStatus... + reqStatus = reqStatusUpd; + + // aggiorno che ho finito... + sb.AppendLine(">>>"); + sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | END", DateTime.Now)); + sb.AppendLine("--------------------------------------------------"); + authData.outMessage += sb.ToString(); + + // riporto progress... + if (progress != null) + { + progress.Report(authData); + } + + + // mi fermo... + Thread.Sleep(500); + }); + } + + if (reqStatus.isHmiEnrolled) + { + // indico che ho concluso + sb = new StringBuilder(); + sb.AppendLine("--------------------------------------------------"); + sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | Auth + enroll DONE", DateTime.Now)); + sb.AppendLine("--------------------------------------------------"); + authData.outMessage += sb.ToString(); + + // riporto progress... + if (progress != null) + { + progress.Report(authData); + } + } + else + { + // indico che ho concluso + sb = new StringBuilder(); + sb.AppendLine("--------------------------------------------------"); + sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | FORCE RESET", DateTime.Now)); + sb.AppendLine("--------------------------------------------------"); + authData.outMessage += sb.ToString(); + + // riporto progress... + if (progress != null) + { + progress.Report(authData); + } + } + } + #endregion } } diff --git a/MConnectSDK/Utils.cs b/MConnectSDK/Utils.cs index 5ac1134..262742d 100644 --- a/MConnectSDK/Utils.cs +++ b/MConnectSDK/Utils.cs @@ -145,6 +145,10 @@ /// Indica se il dispositivo sia stato arruolato /// public bool isEnrolled = false; + /// + /// Messaggio da mostrare + /// + public string outMessage = ""; } /// /// Enum delle sorgenti di comunicazione diff --git a/TestClient/MainForm.cs b/TestClient/MainForm.cs index ef88380..7022af0 100644 --- a/TestClient/MainForm.cs +++ b/TestClient/MainForm.cs @@ -11,12 +11,21 @@ namespace TestClient { public partial class MainForm : Form { + /// + /// Token x cancellazione thread + /// private CancellationTokenSource _cts; - - + /// + /// Context x sync thread + /// private readonly SynchronizationContext synchronizationContext; + /// + /// Timestamp precedente + /// private DateTime previousTime = DateTime.Now; - + /// + /// Istanze classe MaestroConnectClient + /// protected MConnectClient MCC; /// /// Risposta BASE della classe di comunicazione @@ -131,6 +140,8 @@ namespace TestClient /// private void updateAuthData(userAuthData authData) { + // aggiorno console... + lblConsole.Text = authData.outMessage; // verifico se sia AUTH (altrimenti NON mostro codice e QR...) if (reqStatus.isAuth) { @@ -450,12 +461,40 @@ namespace TestClient /// private async void btnTryAuth_Click(object sender, EventArgs e) { + // init variabili accessorie x chiamata doAuthSemap = true; - // init in modalità Task based... - await Task.Run(() => + _cts = new CancellationTokenSource(); + CancellationToken token = _cts.Token; + var progressHandler = new Progress(value => { - var initObj = doOAuthCycle(); + // mostro i dati SPECIFICI x AUTH... + updateAuthData(value); }); + var progress = progressHandler as IProgress; + + try + { + // init in modalità Task based... + await Task.Run(() => + { + if (false) + { + var initObj = doOAuthCycle(); + } + else + { + var initObj = MCC.doOAuthCycleTask(token, progress); + } + }); + } + catch (OperationCanceledException) + { + //label2.Text = "Cancelled."; + } + catch (Exception ex) + { + //label2.Text = ex.GetType().Name + ": " + ex.Message; + } // aggiorno log console StringBuilder sb = new StringBuilder();