From e0c828131ddc1fee93819e29895262a0a44924b3 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Fri, 22 Feb 2019 17:48:14 +0100 Subject: [PATCH] Continuo prototipo classe e WebForm --- MConnectSDK/MConnectClient.cs | 33 +++- TestClient/MainForm.Designer.cs | 251 ++++++++++++++++++++++- TestClient/MainForm.cs | 341 +++++++++++++++++++++++++++++++- TestClient/MainForm.resx | 123 ++++++++++++ TestClient/TestClient.csproj | 3 + 5 files changed, 742 insertions(+), 9 deletions(-) create mode 100644 TestClient/MainForm.resx diff --git a/MConnectSDK/MConnectClient.cs b/MConnectSDK/MConnectClient.cs index 5c3d77b..2f0a877 100644 --- a/MConnectSDK/MConnectClient.cs +++ b/MConnectSDK/MConnectClient.cs @@ -53,6 +53,10 @@ _MConnectID = MConnectID; } + /// + /// Metodo di INIT della classe di comunicazione + /// + /// public requestStatus init() { // init output @@ -72,7 +76,34 @@ }; } else - { } + { + // !!!TBD: test status connessioni, tentativo call su cloud... + } + return answ; + } + + public requestStatus tryAuthorize() + { + // init output + requestStatus answ = new requestStatus(); + // verifico se sono in modalità testing o in produzione + if (_currParam.testMode) + { + // Fingo che tutto vada bene e di conseguenza setto risposta... + answ = new requestStatus + { + // non sono INIZIALMENTE enrolled... + isHmiEnrolled = false, + cloudConnection = true, + localConnection = true, + source = comSource.cloud, + callSuccess = true + }; + } + else + { + // !!!TBD: test status connessioni, tentativo call su cloud... + } return answ; } } diff --git a/TestClient/MainForm.Designer.cs b/TestClient/MainForm.Designer.cs index 9e03c24..bfac89a 100644 --- a/TestClient/MainForm.Designer.cs +++ b/TestClient/MainForm.Designer.cs @@ -28,13 +28,258 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); + this.button1 = new System.Windows.Forms.Button(); + this.button2 = new System.Windows.Forms.Button(); + this.button3 = new System.Windows.Forms.Button(); + this.button4 = new System.Windows.Forms.Button(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.statusStrip1 = new System.Windows.Forms.StatusStrip(); + this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); + this.toolStripProgressBar1 = new System.Windows.Forms.ToolStripProgressBar(); + this.lblClock = new System.Windows.Forms.ToolStripStatusLabel(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.chkTestMode = new System.Windows.Forms.CheckBox(); + this.txtMConnectID = new System.Windows.Forms.TextBox(); + this.label3 = new System.Windows.Forms.Label(); + this.label4 = new System.Windows.Forms.Label(); + this.txtConnParam = new System.Windows.Forms.TextBox(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.btnInit = new System.Windows.Forms.Button(); + this.btnResetParam = new System.Windows.Forms.Button(); + this.statusStrip1.SuspendLayout(); + this.groupBox1.SuspendLayout(); + this.groupBox2.SuspendLayout(); + this.SuspendLayout(); + // + // button1 + // + this.button1.Location = new System.Drawing.Point(455, 279); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(149, 29); + this.button1.TabIndex = 0; + this.button1.Text = "Background worker"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // button2 + // + this.button2.Location = new System.Drawing.Point(623, 279); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(152, 29); + this.button2.TabIndex = 1; + this.button2.Text = "Task Run"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.button2_Click); + // + // button3 + // + this.button3.Location = new System.Drawing.Point(455, 329); + this.button3.Name = "button3"; + this.button3.Size = new System.Drawing.Size(149, 30); + this.button3.TabIndex = 2; + this.button3.Text = "cancel BW"; + this.button3.UseVisualStyleBackColor = true; + this.button3.Click += new System.EventHandler(this.button3_Click); + // + // button4 + // + this.button4.Location = new System.Drawing.Point(623, 329); + this.button4.Name = "button4"; + this.button4.Size = new System.Drawing.Size(152, 30); + this.button4.TabIndex = 3; + this.button4.Text = "cancel TR"; + this.button4.UseVisualStyleBackColor = true; + this.button4.Click += new System.EventHandler(this.button4_Click); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(452, 392); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(20, 17); + this.label1.TabIndex = 4; + this.label1.Text = "..."; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(623, 391); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(20, 17); + this.label2.TabIndex = 5; + this.label2.Text = "..."; + // + // statusStrip1 + // + this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripStatusLabel1, + this.toolStripProgressBar1, + this.lblClock}); + this.statusStrip1.Location = new System.Drawing.Point(0, 421); + this.statusStrip1.Name = "statusStrip1"; + this.statusStrip1.Size = new System.Drawing.Size(921, 25); + this.statusStrip1.TabIndex = 7; + this.statusStrip1.Text = "statusStrip1"; + // + // toolStripStatusLabel1 + // + this.toolStripStatusLabel1.Name = "toolStripStatusLabel1"; + this.toolStripStatusLabel1.Size = new System.Drawing.Size(136, 20); + this.toolStripStatusLabel1.Text = "POC MConnectSDK"; + // + // toolStripProgressBar1 + // + this.toolStripProgressBar1.Name = "toolStripProgressBar1"; + this.toolStripProgressBar1.Size = new System.Drawing.Size(100, 19); + this.toolStripProgressBar1.Style = System.Windows.Forms.ProgressBarStyle.Marquee; + // + // lblClock + // + this.lblClock.Name = "lblClock"; + this.lblClock.Size = new System.Drawing.Size(27, 20); + this.lblClock.Text = "---"; + // + // groupBox1 + // + this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.groupBox1.AutoSize = true; + this.groupBox1.Controls.Add(this.txtConnParam); + this.groupBox1.Controls.Add(this.label4); + this.groupBox1.Controls.Add(this.chkTestMode); + this.groupBox1.Controls.Add(this.txtMConnectID); + this.groupBox1.Controls.Add(this.label3); + this.groupBox1.Location = new System.Drawing.Point(13, 12); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(896, 73); + this.groupBox1.TabIndex = 11; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Connection Parameters"; + // + // chkTestMode + // + this.chkTestMode.AutoSize = true; + this.chkTestMode.Location = new System.Drawing.Point(349, 29); + this.chkTestMode.Name = "chkTestMode"; + this.chkTestMode.Size = new System.Drawing.Size(97, 21); + this.chkTestMode.TabIndex = 13; + this.chkTestMode.Text = "Test Mode"; + this.chkTestMode.UseVisualStyleBackColor = true; + // + // txtMConnectID + // + this.txtMConnectID.Location = new System.Drawing.Point(103, 30); + this.txtMConnectID.Name = "txtMConnectID"; + this.txtMConnectID.Size = new System.Drawing.Size(221, 22); + this.txtMConnectID.TabIndex = 12; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(13, 33); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(84, 17); + this.label3.TabIndex = 11; + this.label3.Text = "MConnectID"; + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(452, 33); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(61, 17); + this.label4.TabIndex = 14; + this.label4.Text = "connPar"; + // + // txtConnParam + // + this.txtConnParam.Location = new System.Drawing.Point(520, 29); + this.txtConnParam.Name = "txtConnParam"; + this.txtConnParam.Size = new System.Drawing.Size(268, 22); + this.txtConnParam.TabIndex = 15; + // + // groupBox2 + // + this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.groupBox2.Controls.Add(this.btnInit); + this.groupBox2.Controls.Add(this.btnResetParam); + this.groupBox2.Location = new System.Drawing.Point(13, 91); + this.groupBox2.Name = "groupBox2"; + this.groupBox2.Size = new System.Drawing.Size(896, 100); + this.groupBox2.TabIndex = 14; + this.groupBox2.TabStop = false; + this.groupBox2.Text = "Metodi"; + // + // btnInit + // + this.btnInit.Location = new System.Drawing.Point(147, 32); + this.btnInit.Name = "btnInit"; + this.btnInit.Size = new System.Drawing.Size(118, 49); + this.btnInit.TabIndex = 15; + this.btnInit.Text = "Init"; + this.btnInit.UseVisualStyleBackColor = true; + this.btnInit.Click += new System.EventHandler(this.btnInit_Click); + // + // btnResetParam + // + this.btnResetParam.Location = new System.Drawing.Point(6, 32); + this.btnResetParam.Name = "btnResetParam"; + this.btnResetParam.Size = new System.Drawing.Size(118, 49); + this.btnResetParam.TabIndex = 14; + this.btnResetParam.Text = "Reset Param"; + this.btnResetParam.UseVisualStyleBackColor = true; + this.btnResetParam.Click += new System.EventHandler(this.btnResetParam_Click); + // + // MainForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; + this.ClientSize = new System.Drawing.Size(921, 446); + this.Controls.Add(this.groupBox2); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.statusStrip1); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.button4); + this.Controls.Add(this.button3); + this.Controls.Add(this.button2); + this.Controls.Add(this.button1); + this.Name = "MainForm"; + this.Text = "POC MConnectSDK"; + this.statusStrip1.ResumeLayout(false); + this.statusStrip1.PerformLayout(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + this.groupBox2.ResumeLayout(false); + this.ResumeLayout(false); + this.PerformLayout(); + } #endregion + + private System.Windows.Forms.Button button1; + private System.Windows.Forms.Button button2; + private System.Windows.Forms.Button button3; + private System.Windows.Forms.Button button4; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.StatusStrip statusStrip1; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1; + private System.Windows.Forms.ToolStripProgressBar toolStripProgressBar1; + private System.Windows.Forms.ToolStripStatusLabel lblClock; + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.CheckBox chkTestMode; + private System.Windows.Forms.TextBox txtMConnectID; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.TextBox txtConnParam; + private System.Windows.Forms.Label label4; + private System.Windows.Forms.GroupBox groupBox2; + private System.Windows.Forms.Button btnInit; + private System.Windows.Forms.Button btnResetParam; } } diff --git a/TestClient/MainForm.cs b/TestClient/MainForm.cs index 40cae26..f08c630 100644 --- a/TestClient/MainForm.cs +++ b/TestClient/MainForm.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -12,9 +8,344 @@ namespace TestClient { public partial class MainForm : Form { + private BackgroundWorker _bgw; + private CancellationTokenSource _cts; + + + private readonly SynchronizationContext synchronizationContext; + private DateTime previousTime = DateTime.Now; + public MainForm() { InitializeComponent(); + synchronizationContext = SynchronizationContext.Current; + var clockTask = myInitAsync(); + } + + private async Task myInitAsync() + { + await updateClockAsync(); + } + + private async Task updateClockAsync() + { +#if false + var progressHandler = new Progress(value => + { + lblClock.Text = value; + }); + var progress = progressHandler as IProgress; + await Task.Run(() => + { + while (true) + { + Thread.Sleep(50); + if (progress != null) + { + progress.Report(DateTime.Now.ToString("ddd dd.MM.yyyy HH:mm:ss.fff")); + } + } + }); +#endif + 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; + } + + private void button1_Click(object sender, EventArgs e) + { + procBackgroudWorker(); + + } + + + private async void button2_Click(object sender, EventArgs e) + { + await procTask(); + } + private void button3_Click(object sender, EventArgs e) + { + cancelBW(); + } + + + /// + /// Testing di BackgroundWorker + /// + private void procBackgroudWorker() + { +#if false + var bgw = new BackgroundWorker(); + bgw.DoWork += (_, __) => + { + Thread.Sleep(1000); + }; + bgw.RunWorkerCompleted += (_, __) => + { + MessageBox.Show("Hi from the UI thread!"); + }; + bgw.RunWorkerAsync(); +#endif + +#if false + var bgw = new BackgroundWorker(); + bgw.DoWork += (_, __) => + { + Thread.Sleep(1000); + throw new InvalidOperationException("Hi!"); + }; + bgw.RunWorkerCompleted += (_, args) => + { + if (args.Error != null) + { + MessageBox.Show(args.Error.Message); + } + }; + bgw.RunWorkerAsync(); +#endif + +#if false + var bgw = new BackgroundWorker(); + bgw.DoWork += (_, args) => + { + Thread.Sleep(1000); + args.Result = 13; + }; + bgw.RunWorkerCompleted += (_, args) => + { + var result = (int)args.Result; + MessageBox.Show("Result is " + result); + }; + bgw.RunWorkerAsync(); +#endif + +#if false + _bgw = new BackgroundWorker(); + var bgw = _bgw; + bgw.WorkerSupportsCancellation = true; + bgw.DoWork += (_, args) => + { + for (int i = 0; i != 100; ++i) + { + if (bgw.CancellationPending) + { + args.Cancel = true; + return; + } + Thread.Sleep(100); + } + }; + bgw.RunWorkerCompleted += (_, args) => + { + if (args.Cancelled) + { + MessageBox.Show("Cancelled."); + } + else + { + MessageBox.Show("Completed."); + } + }; + bgw.RunWorkerAsync(); +#endif + +#if false + var bgw = new BackgroundWorker(); + bgw.WorkerReportsProgress = true; + bgw.DoWork += (_, args) => + { + for (int i = 0; i != 100; ++i) + { + bgw.ReportProgress(i); + Thread.Sleep(100); + } + }; + bgw.ProgressChanged += (_, args) => + { + label1.Text = args.ProgressPercentage.ToString(); + }; + bgw.RunWorkerCompleted += (_, args) => + { + label1.Text = "Completed."; + }; + bgw.RunWorkerAsync(); +#endif + + var bgw = new BackgroundWorker(); + bgw.WorkerReportsProgress = true; + bgw.DoWork += (_, args) => + { + for (int i = 0; i != 100; ++i) + { + bgw.ReportProgress(0, "Stage " + i); + Thread.Sleep(20); + } + }; + bgw.ProgressChanged += (_, args) => + { + label1.Text = (string)args.UserState; + }; + bgw.RunWorkerCompleted += (_, args) => + { + label1.Text = "Completed."; + }; + bgw.RunWorkerAsync(); + + } + + + /// + /// Testing di TASK + /// + /// + private async Task procTask() + { +#if false + await Task.Run(() => + { + Thread.Sleep(1000); + }); + MessageBox.Show("Hi from the UI thread!"); +#endif + +#if false + try + { + await Task.Run(() => + { + Thread.Sleep(1000); + throw new InvalidOperationException("Hi!"); + }); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } +#endif + +#if false + var result = await Task.Run(() => + { + Thread.Sleep(1000); + return 13; + }); + MessageBox.Show("Result is " + result); +#endif + +#if false + _cts = new CancellationTokenSource(); + var token = _cts.Token; + try + { + await Task.Run(() => + { + for (int i = 0; i != 100; ++i) + { + token.ThrowIfCancellationRequested(); + Thread.Sleep(100); + } + }); + MessageBox.Show("Completed."); + } + catch (OperationCanceledException) + { + MessageBox.Show("Cancelled."); + } +#endif + +#if false + var progressHandler = new Progress(value => + { + label2.Text = value.ToString(); + }); + var progress = progressHandler as IProgress; + await Task.Run(() => + { + for (int i = 0; i != 100; ++i) + { + if (progress != null) + { + progress.Report(i); + } + + Thread.Sleep(100); + } + }); + label2.Text = "Completed."; +#endif + + var progressHandler = new Progress(value => + { + label2.Text = value; + }); + var progress = progressHandler as IProgress; + await Task.Run(() => + { + for (int i = 0; i != 100; ++i) + { + if (progress != null) + { + progress.Report("Stage " + i); + } + + Thread.Sleep(20); + } + }); + label2.Text = "Completed."; + + } + + private void cancelBW() + { + // esegue cancellazione TASK + if (_bgw != null) + { + _bgw.CancelAsync(); + } + } + + private void button4_Click(object sender, EventArgs e) + { + if (_cts != null) + { + _cts.Cancel(); + } + } + + private void btnInit_Click(object sender, EventArgs e) + { + + } + + private void btnResetParam_Click(object sender, EventArgs e) + { + txtMConnectID.Text = "MCID-0000-1234-5678"; + chkTestMode.Checked = true; + txtConnParam.Text = "127.0.0.1,abortConnect=false,ssl=false"; } } } diff --git a/TestClient/MainForm.resx b/TestClient/MainForm.resx new file mode 100644 index 0000000..174ebc7 --- /dev/null +++ b/TestClient/MainForm.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/TestClient/TestClient.csproj b/TestClient/TestClient.csproj index d8e7b5b..1df147b 100644 --- a/TestClient/TestClient.csproj +++ b/TestClient/TestClient.csproj @@ -57,6 +57,9 @@ + + MainForm.cs + ResXFileCodeGenerator Resources.Designer.cs