reinclusione task run su libreria...

This commit is contained in:
Samuele E. Locatelli
2019-03-06 11:37:00 +01:00
parent 54cb54d7ee
commit df5c2df43b
3 changed files with 136 additions and 6 deletions
+87
View File
@@ -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;
}
/// <summary>
/// Task x AUTH
/// </summary>
/// <param name="token"></param>
/// <param name="progress"></param>
/// <returns></returns>
public async Task doOAuthCycleTask(CancellationToken token, IProgress<userAuthData> 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
}
}
+4
View File
@@ -145,6 +145,10 @@
/// Indica se il dispositivo sia stato arruolato
/// </summary>
public bool isEnrolled = false;
/// <summary>
/// Messaggio da mostrare
/// </summary>
public string outMessage = "";
}
/// <summary>
/// Enum delle sorgenti di comunicazione
+45 -6
View File
@@ -11,12 +11,21 @@ namespace TestClient
{
public partial class MainForm : Form
{
/// <summary>
/// Token x cancellazione thread
/// </summary>
private CancellationTokenSource _cts;
/// <summary>
/// Context x sync thread
/// </summary>
private readonly SynchronizationContext synchronizationContext;
/// <summary>
/// Timestamp precedente
/// </summary>
private DateTime previousTime = DateTime.Now;
/// <summary>
/// Istanze classe MaestroConnectClient
/// </summary>
protected MConnectClient MCC;
/// <summary>
/// Risposta BASE della classe di comunicazione
@@ -131,6 +140,8 @@ namespace TestClient
/// <param name="authData"></param>
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
/// <param name="e"></param>
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<userAuthData>(value =>
{
var initObj = doOAuthCycle();
// mostro i dati SPECIFICI x AUTH...
updateAuthData(value);
});
var progress = progressHandler as IProgress<userAuthData>;
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();