From adbe46f5f5510dd248e244372da34c6750497814 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Wed, 19 Feb 2020 15:44:31 +0100 Subject: [PATCH] Update prod x testing + salvataggio json locale --- NKC_SDK/NKC.cs | 131 +++++++++++++++++++++++-------- RedisExample/Form1.Designer.vb | 14 +++- RedisExample/Form1.vb | 11 ++- RedisExample/RedisExample.vbproj | 7 +- RedisExample/data/.placeholder | 1 + 5 files changed, 129 insertions(+), 35 deletions(-) create mode 100644 RedisExample/data/.placeholder diff --git a/NKC_SDK/NKC.cs b/NKC_SDK/NKC.cs index f33eb88..aa0e77e 100644 --- a/NKC_SDK/NKC.cs +++ b/NKC_SDK/NKC.cs @@ -1,5 +1,6 @@ using Newtonsoft.Json; using System; +using System.IO; using System.Net; using System.Net.NetworkInformation; @@ -27,7 +28,7 @@ namespace NKC_SDK } catch (Exception exc) { - Log.Instance.Error(exc, $"Eccezione durante callUrl per {URL}{Environment.NewLine}{exc}"); + Log.Instance.Error($"Eccezione durante callUrl per URL: {URL}{Environment.NewLine}{exc}"); answ = exc.Message; } // restituisco valore! @@ -48,8 +49,11 @@ namespace NKC_SDK { answ = client.UploadString(URL, payload); } - catch - { } + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante callUrl per URL: {URL} | payload: {payload}{Environment.NewLine}{exc}"); + answ = exc.Message; + } // restituisco valore! return answ; } @@ -74,7 +78,10 @@ namespace NKC_SDK answ = "ok"; } catch (Exception exc) - { } + { + Log.Instance.Error($"Eccezione durante putData per {URL}{Environment.NewLine}{exc}"); + answ = exc.Message; + } // restituisco valore! return answ; } @@ -88,14 +95,14 @@ namespace NKC_SDK { get { - return $"{_baseUrl}Alive"; + return $"{_baseUrl}api/Alive"; } } protected string urlAliveClock { get { - return $"{_baseUrl}Alive/Clock"; + return $"{_baseUrl}api/Alive/1"; } } protected string urlCurrBunk @@ -117,6 +124,10 @@ namespace NKC_SDK { return $"{_baseUrl}api/Bunk/{currBunkId}"; } + /// + /// file locale per persistenza BUNK + /// + protected string persistFileName = "data/persistFile.json"; #endregion @@ -156,17 +167,14 @@ namespace NKC_SDK /// /// Effettua test alive all'indirizzo del server /// - public string testAlive + public bool testAlive { get { - string answ = ""; - try - { - answ = callUrl(urlAlive); - } - catch - { } + bool answ = false; + string returnData = callUrl(urlAlive); + returnData = JsonConvert.DeserializeObject(returnData); + answ = returnData == "OK"; // rendo! return answ; } @@ -174,13 +182,22 @@ namespace NKC_SDK /// /// Effettua test ping all'indirizzo del server /// - public PingReply testClock + public DateTime testClock { get { - Ping myPing = new Ping(); - // timeout a 1 sec! - PingReply answ = myPing.Send(_baseIp, 1000); + DateTime oggi = DateTime.Today; + DateTime answ = oggi.AddYears(-oggi.Year + 1900).AddMonths(-oggi.Month).AddDays(-oggi.Day + 1); + // recupero! + string returnData = callUrl(urlAliveClock); + try + { + DateTime.TryParse(JsonConvert.DeserializeObject(returnData), out answ); + } + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante testClock, ricevuto {returnData}{Environment.NewLine}{exc}"); + } // rendo! return answ; } @@ -195,14 +212,17 @@ namespace NKC_SDK public ProdBunk getFirstBunk() { ProdBunk answ = null; + string rawdata = ""; // chiamo metodo x recupero WBunk... try { - string rawdata = callUrl(urlCurrBunk); + rawdata = callUrl(urlCurrBunk); answ = JsonConvert.DeserializeObject(rawdata); } - catch - { } + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante getFirstBunk, ricevuto {rawdata}{Environment.NewLine}{exc}"); + } return answ; } /// @@ -214,14 +234,17 @@ namespace NKC_SDK public ProdBunk getNextBunk(int currBunkId) { ProdBunk answ = null; + string rawdata = ""; // chiamo metodo x recupero WBunk... try { - string rawdata = callUrl(urlNextBunk(currBunkId)); + rawdata = callUrl(urlNextBunk(currBunkId)); answ = JsonConvert.DeserializeObject(rawdata); } - catch - { } + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante getNextBunk, ricevuto {rawdata}{Environment.NewLine}{exc}"); + } return answ; } /// @@ -233,32 +256,76 @@ namespace NKC_SDK public ProdBunk getBunk(int currBunkId) { ProdBunk answ = null; + string rawdata = ""; // chiamo metodo x recupero WBunk... try { - string rawdata = callUrl(urlGetBunk(currBunkId)); + rawdata = callUrl(urlGetBunk(currBunkId)); answ = JsonConvert.DeserializeObject(rawdata); } - catch - { } + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante getBunk, ricevuto {rawdata}{Environment.NewLine}{exc}"); + } return answ; } /// /// Effettua salvataggio del BUNK corrente su NKC /// /// - public void saveBunk(ProdBunk currBunk) + public bool saveBunk(ProdBunk currBunk) { + bool answ = false; + string rawdata = ""; try { // serializzo oggetto - string rawdata = JsonConvert.SerializeObject(currBunk); + rawdata = JsonConvert.SerializeObject(currBunk); // invio con metodo put! putData(urlPutBunk(currBunk.BunkId), rawdata); + answ = true; } - catch - { } - + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante saveBunk, ricevuto {rawdata}{Environment.NewLine}{exc}"); + } + return answ; } + /// + /// Oggetto che contiene il BUNK corrente LOCALMENTE + /// + public ProdBunk persistedBunk + { + get + { + ProdBunk answ = new ProdBunk(); + try + { + string rawdata = File.ReadAllText(persistFileName); + answ = JsonConvert.DeserializeObject(rawdata); + } + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante recupero locale del bunk{Environment.NewLine}{exc}"); + } + return answ; + } + set + { + try + { + // serializzo oggetto + string rawdata = JsonConvert.SerializeObject(value); + // salvo in locale + File.WriteAllText(persistFileName, rawdata); + } + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante salvataggio locale del BUNK{Environment.NewLine}{exc}"); + } + } + } + + } } diff --git a/RedisExample/Form1.Designer.vb b/RedisExample/Form1.Designer.vb index 1081722..6419670 100644 --- a/RedisExample/Form1.Designer.vb +++ b/RedisExample/Form1.Designer.vb @@ -47,6 +47,7 @@ Partial Class Form1 Me.lblPing = New System.Windows.Forms.Label() Me.btnAlive = New System.Windows.Forms.Button() Me.lblAlive = New System.Windows.Forms.Label() + Me.btnPersist = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'btnReset @@ -250,7 +251,7 @@ Partial Class Form1 Me.btnAlive.Name = "btnAlive" Me.btnAlive.Size = New System.Drawing.Size(75, 23) Me.btnAlive.TabIndex = 23 - Me.btnAlive.Text = "Test Ping" + Me.btnAlive.Text = "Test Alive" Me.btnAlive.UseVisualStyleBackColor = True ' 'lblAlive @@ -262,11 +263,21 @@ Partial Class Form1 Me.lblAlive.TabIndex = 24 Me.lblAlive.Text = "alive???" ' + 'btnPersist + ' + Me.btnPersist.Location = New System.Drawing.Point(423, 86) + Me.btnPersist.Name = "btnPersist" + Me.btnPersist.Size = New System.Drawing.Size(75, 23) + Me.btnPersist.TabIndex = 25 + Me.btnPersist.Text = "Save Local" + Me.btnPersist.UseVisualStyleBackColor = True + ' 'Form1 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(800, 450) + Me.Controls.Add(Me.btnPersist) Me.Controls.Add(Me.lblAlive) Me.Controls.Add(Me.btnAlive) Me.Controls.Add(Me.lblPing) @@ -324,4 +335,5 @@ Partial Class Form1 Friend WithEvents lblPing As Label Friend WithEvents btnAlive As Button Friend WithEvents lblAlive As Label + Friend WithEvents btnPersist As Button End Class diff --git a/RedisExample/Form1.vb b/RedisExample/Form1.vb index fba3c82..c37faa0 100644 --- a/RedisExample/Form1.vb +++ b/RedisExample/Form1.vb @@ -213,7 +213,16 @@ Public Class Form1 Private Sub btnAlive_Click(sender As Object, e As EventArgs) Handles btnAlive.Click Dim answ As String = "" - answ = currNKC.testAlive + If (currNKC.testAlive) Then + answ = currNKC.testClock + lblAlive.Text = "Server Alive con clock " + answ + + End If + + End Sub + + Private Sub btnPersist_Click(sender As Object, e As EventArgs) Handles btnPersist.Click + currNKC.persistedBunk = currentBunk End Sub End Class diff --git a/RedisExample/RedisExample.vbproj b/RedisExample/RedisExample.vbproj index 6effbf8..53d42a7 100644 --- a/RedisExample/RedisExample.vbproj +++ b/RedisExample/RedisExample.vbproj @@ -119,7 +119,12 @@ - + + Always + + + Always + MyApplicationCodeGenerator Application.Designer.vb diff --git a/RedisExample/data/.placeholder b/RedisExample/data/.placeholder new file mode 100644 index 0000000..5f28270 --- /dev/null +++ b/RedisExample/data/.placeholder @@ -0,0 +1 @@ + \ No newline at end of file