using MConnectSDK; using System; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace TestClient { public partial class MainForm : Form { private CancellationTokenSource _cts; private readonly SynchronizationContext synchronizationContext; private DateTime previousTime = DateTime.Now; protected MConnectClient MCC; /// /// Risposta BASE della classe di comunicazione /// protected requestStatus reqStatus; protected bool doingAuth; public MainForm() { InitializeComponent(); myInit(); synchronizationContext = SynchronizationContext.Current; } protected void myInit() { var clockTask = updateClockAsync(); fixDisplay(); lblConsole.Text = ""; } private void fixDisplay() { // controllo se visualizzare parametri di testing... bool showTP = chkTestMode.Checked; //chkCloudOk.Visible = showTP; } private async Task updateClockAsync() { await Task.Run(() => { while (true) { Thread.Sleep(50); UpdateUI(); } }); } public void UpdateUI() { var timeNow = DateTime.Now; if ((DateTime.Now - previousTime).Milliseconds <= 250) { return; } synchronizationContext.Post(new SendOrPostCallback(o => { lblClock.Text = timeNow.ToString("ddd dd.MM.yyyy HH:mm:ss"); }), ""); previousTime = timeNow; } /// /// Reset parametri... /// /// /// private void btnResetParam_Click(object sender, EventArgs e) { txtMConnectID.Text = "MCID-0000-1234-5678"; txtClientID.Text = "HMI_TEST_CLI"; chkTestMode.Checked = true; txtConnParam.Text = "127.0.0.1,abortConnect=false,ssl=false"; fixDisplay(); } /// /// Simulazione INIT /// /// /// private async void btnInit_Click(object sender, EventArgs e) { confParam param = new confParam { redisConn = txtConnParam.Text, redisConnAdmin = txtConnParam.Text + ",allowAdmin=true", testMode = chkTestMode.Checked }; // salvo NON In auth... doingAuth = false; // init in modalità Task based... await Task.Run(() => { var initObj = doInitAsync(param); }); // aggiorno log console StringBuilder sb = new StringBuilder(); sb.AppendLine("--------------------------------------------------"); sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | START INIT", DateTime.Now)); sb.AppendLine(""); lblConsole.Text = sb.ToString(); } private async Task doInitAsync(confParam param) { await Task.Run(() => { MCC = new MConnectClient(txtMConnectID.Text.Trim(), txtClientID.Text.Trim(), param); reqStatus = MCC.init(); synchronizationContext.Post(new SendOrPostCallback(o => { refreshRequestStatus(); }), ""); }); } public void refreshRequestStatus() { // mostro risultato StringBuilder sb = new StringBuilder(); sb.AppendLine("-------------------------"); sb.AppendLine("- Request Status"); sb.AppendLine("-------------------------"); sb.AppendLine("isHmiEnrolled: " + reqStatus.isHmiEnrolled); sb.AppendLine("cloudConnection: " + reqStatus.cloudConnection); sb.AppendLine("localConnection: " + reqStatus.localConnection); sb.AppendLine("callSuccess: " + reqStatus.callSuccess); sb.AppendLine("-------------------------"); lblReqStatus.Text = sb.ToString(); sb.AppendLine(""); sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | END", DateTime.Now)); sb.AppendLine("--------------------------------------------------"); lblConsole.Text += sb.ToString(); // se NON enrolled --> abilita tryAuth... btnTryAuth.Enabled = !reqStatus.isHmiEnrolled; } /// /// Simulazione auth (SE non abilitato) /// /// /// private async void btnTryAuth_Click(object sender, EventArgs e) { // salvo NON In auth... doingAuth = true; // init in modalità Task based... await Task.Run(() => { var initObj = doOAuthCycle(); }); // aggiorno log console StringBuilder sb = new StringBuilder(); sb.AppendLine("--------------------------------------------------"); sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | START OAuth process", DateTime.Now)); sb.AppendLine(""); lblConsole.Text = sb.ToString(); } private async Task doOAuthCycle() { // fino a quanod NON è auth --> cicla... while (!reqStatus.isHmiEnrolled && doingAuth) { await Task.Run(() => { synchronizationContext.Post(new SendOrPostCallback(o => { StringBuilder sb = new StringBuilder(); sb.AppendLine("--------------------------------------------------"); sb.AppendLine(string.Format("{0:HH.mm.ss.fff} | Try OAuth process", DateTime.Now)); sb.AppendLine(""); lblConsole.Text = sb.ToString(); }), ""); reqStatus = MCC.init(); userAuthData authData = MCC.tryAuthorize(); synchronizationContext.Post(new SendOrPostCallback(o => { refreshRequestStatus(); // mostro i dati SPECIFICI x AUTH... updateAuthData(authData); }), ""); if (reqStatus.isHmiEnrolled) { doingAuth = false; } else { Thread.Sleep(1000); } }); } } /// /// Mostra info x USER AUTH /// /// private void updateAuthData(userAuthData authData) { StringBuilder sb = new StringBuilder(); //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); } private void chkCloudOk_Click(object sender, EventArgs e) { } private void chkLocalOk_Click(object sender, EventArgs e) { } private void chkEnrolled_Click(object sender, EventArgs e) { } private void chkTestMode_Click(object sender, EventArgs e) { fixDisplay(); } } }