diff --git a/MConnectSDK/MConnectClient.cs b/MConnectSDK/MConnectClient.cs index 98f525f..347a8be 100644 --- a/MConnectSDK/MConnectClient.cs +++ b/MConnectSDK/MConnectClient.cs @@ -491,6 +491,7 @@ namespace MConnectSDK // svuoto TUTTI i dati x INIT... //ML.redFlushDB(_currParam.defaultDb); ML.redFlushKey("MCC*", _currParam.defaultDb); + enrollDate = DateTime.Now.AddYears(100); // iniziod a watchdog... setWatchdog(); // init output @@ -542,34 +543,32 @@ namespace MConnectSDK // registro che è OK x AUTH answ.hasAuth = true; reqStatus.isAuth = true; + answ.isEnrolled = false; + reqStatus.isHmiEnrolled = false; // controllo se sia stato arruolato, altrimenti lo arruolo... if (_currParam.testMode) { // fingo di chiamare associazione...per simularlo imposto una enroll date a + 5 secondi, se non ci fosse... altrimenti aspetto... if (enrollDate < DateTime.Now.AddSeconds(10)) { - // aspetto + answ.isEnrolled = true; + reqStatus.isHmiEnrolled = true; } else { enrollDate = DateTime.Now.AddSeconds(5); } - // se è scaduta enroll date fingo di aver fatto associazione... - if (enrollDate > DateTime.Now) - { - answ.isEnrolled = true; - reqStatus.isHmiEnrolled = true; - } } else { /*----------------------------------------- - * Processo: + * Processo VERA chiamata: * - chiamo metodo x associazione * - salvo dati orgCode *-----------------------------------------*/ } } + // non autorizzato... else { // riporto in risposta i dati... @@ -721,16 +720,11 @@ namespace MConnectSDK /// 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(); + bool processRequest = true; // fino a quanod NON è auth --> cicla... - while (!reqStatus.isHmiEnrolled) + while (!reqStatus.isHmiEnrolled && processRequest) { await Task.Run(() => { @@ -738,54 +732,51 @@ namespace MConnectSDK // recupero nuova reqStatus... reqStatus = reqStatusUpd; - // aggiorno che ho finito... - sb.AppendLine(">>>"); - sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | END", DateTime.Now)); + // aggiorno che ho finito... + sb = new StringBuilder(); + sb.AppendLine(string.Format("{0:HH.mm.ss.fff} >> keep trying", DateTime.Now)); sb.AppendLine("--------------------------------------------------"); authData.outMessage += sb.ToString(); + // verifico SE sia stato x autorizzato... + if (reqStatus.isAuth) + { + sb.AppendLine(""); + sb.AppendLine("--------------------------------------------------"); + sb.AppendLine("SUCCESS 200: AUTH RECEIVED!"); + sb.AppendLine("--------------------------------------------------"); + authData.outMessage += sb.ToString(); + } + + // verifico SE sia stato registrato... + if (reqStatus.isHmiEnrolled) + { + sb.AppendLine(""); + sb.AppendLine("--------------------------------------------------"); + sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | AUTH + ENROLL DONE", DateTime.Now)); + sb.AppendLine("--------------------------------------------------"); + authData.outMessage += sb.ToString(); + } + + // verifico se richiesta cancellazione... CHIUDO! + if (token.IsCancellationRequested) + { + // riporto richiesta cancellazione... ed esco + authData = new userAuthData(); + processRequest = false; + } + // 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/TestClient/MainForm.cs b/TestClient/MainForm.cs index 7022af0..a9ec8f9 100644 --- a/TestClient/MainForm.cs +++ b/TestClient/MainForm.cs @@ -32,10 +32,8 @@ namespace TestClient /// protected requestStatus reqStatus; /// - /// Semaforo x auth + /// Form principale /// - protected bool doAuthSemap = false; - public MainForm() { InitializeComponent(); @@ -49,6 +47,7 @@ namespace TestClient protected void myInit() { var clockTask = updateClockAsync(); + _cts = new CancellationTokenSource(); fixDisplay(); lblConsole.Text = ""; } @@ -143,11 +142,11 @@ namespace TestClient // aggiorno console... lblConsole.Text = authData.outMessage; // verifico se sia AUTH (altrimenti NON mostro codice e QR...) - if (reqStatus.isAuth) + if (authData.hasAuth || reqStatus.isAuth) { var currColor = Color.Orange; // se fosse ANCHE enrolled cambio colore... - if (reqStatus.isHmiEnrolled) + if (authData.isEnrolled || reqStatus.isHmiEnrolled) { currColor = Color.Green; } @@ -173,18 +172,21 @@ namespace TestClient } else { - StringBuilder sb = new StringBuilder(); - //sb.AppendLine("--------------------------------------------------"); - sb.AppendLine("device_code:"); - sb.AppendLine(authData.deviceCode.ToString()); - sb.AppendLine(""); - sb.AppendLine("user_code:"); - sb.AppendLine(authData.userCode.ToString()); - sb.AppendLine(""); - lblRetData.Text = sb.ToString(); - // mostro QR-code - Zen.Barcode.CodeQrBarcodeDraw qrcode = Zen.Barcode.BarcodeDrawFactory.CodeQr; - pictBox.Image = qrcode.Draw(authData.qrCode, 50); + // mostro info SE disponibili... + if (authData.userCode != "") + { + StringBuilder sb = new StringBuilder(); + //sb.AppendLine("--------------------------------------------------"); + sb.AppendLine("device_code:"); + sb.AppendLine(authData.deviceCode.ToString()); + sb.AppendLine(""); + sb.AppendLine("user_code:"); + sb.AppendLine(authData.userCode.ToString()); + sb.AppendLine(""); + lblRetData.Text = sb.ToString(); + Zen.Barcode.CodeQrBarcodeDraw qrcode = Zen.Barcode.BarcodeDrawFactory.CodeQr; + pictBox.Image = qrcode.Draw(authData.qrCode, 50); + } } } @@ -230,6 +232,7 @@ namespace TestClient }), ""); }); } +#if false /// /// Task x AUTH /// @@ -298,7 +301,8 @@ namespace TestClient resetAuthData(); } doAuthSemap = false; - } + } +#endif /// /// Recupero OrgCode /// @@ -452,7 +456,10 @@ namespace TestClient lblConsole.Text = sb.ToString(); // resetto aspetti grafici resetAuthData(); - doAuthSemap = false; + if (_cts != null) + { + _cts.Cancel(); + } } /// /// Simulazione auth (SE non abilitato) @@ -461,10 +468,15 @@ namespace TestClient /// private async void btnTryAuth_Click(object sender, EventArgs e) { - // init variabili accessorie x chiamata - doAuthSemap = true; - _cts = new CancellationTokenSource(); + // init variabili accessorie x chiamata + StringBuilder sb = new StringBuilder(); + // elimino eventuali cancel presenti... + if (_cts.IsCancellationRequested) + { + _cts = new CancellationTokenSource(); + } CancellationToken token = _cts.Token; + // gestione progress... var progressHandler = new Progress(value => { // mostro i dati SPECIFICI x AUTH... @@ -477,29 +489,26 @@ namespace TestClient // init in modalità Task based... await Task.Run(() => { - if (false) - { - var initObj = doOAuthCycle(); - } - else - { - var initObj = MCC.doOAuthCycleTask(token, progress); - } + var initObj = MCC.doOAuthCycleTask(token, progress); }); } catch (OperationCanceledException) { - //label2.Text = "Cancelled."; + sb = new StringBuilder(); + sb.AppendLine("--------------------------------------------------"); + sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | CANCELLED OAuth process", DateTime.Now)); + sb.AppendLine("--------------------------------------------------"); + lblConsole.Text = sb.ToString(); } catch (Exception ex) { //label2.Text = ex.GetType().Name + ": " + ex.Message; } - // aggiorno log console - StringBuilder sb = new StringBuilder(); + // aggiorno log console + sb.AppendLine("--------------------------------------------------"); + sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | STARTING OAuth process", DateTime.Now)); sb.AppendLine("--------------------------------------------------"); - sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | START OAuth process", DateTime.Now)); lblConsole.Text = sb.ToString(); } ///