diff --git a/AppData/ComLib.cs b/AppData/ComLib.cs index e585209..d7fdc15 100644 --- a/AppData/ComLib.cs +++ b/AppData/ComLib.cs @@ -1,4 +1,5 @@ -using MongoDB.Driver; +using System.Linq; +using MongoDB.Driver; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using NKC_SDK; @@ -62,6 +63,7 @@ namespace AppData public static string redOrders = "NKC:SERV:ORDERS"; public static string redOutPath = "NKC:SERV:BREQ"; public static string redProdAnsw = "NKC:PROD:BUNKS"; + public static string redProdMachStateData = "NKC:PROD:MACHSTATE"; public static string redProdReq = "NKC:SERV:BUNKS"; /// @@ -1784,6 +1786,73 @@ namespace AppData return answ; } + /// + /// Restituisce stato amcchina (da Redis) + /// + /// + public static List prodMachStateDataGet() + { + List answ = new List(); + + string redVal = memLayer.ML.getRSV(redProdMachStateData); + if (!string.IsNullOrEmpty(redVal)) + { + answ = JsonConvert.DeserializeObject>(redVal); + } + return answ; + } + + /// + /// Salva gli stati macchina ricevuti sul DB e poi salva ultimo su redis + /// Do x scontato tutti singola macchina + /// + /// + public static bool prodMachStateDataInsert(MachineStatData updateRecords) + { + List currData = new List(); + // FIXME TODO: salvare TUTTO sul DB !!! + + // leggo stato attuale... + string redVal = memLayer.ML.getRSV(redProdMachStateData); + if (!string.IsNullOrEmpty(redVal)) + { + currData = JsonConvert.DeserializeObject>(redVal); + + // certo info x macchina corrente + var machRecord = currData.Where(x => x.Machine == updateRecords.Machine).FirstOrDefault(); + // recupero ultimo stato + var lastRecord = updateRecords.Records.OrderByDescending(x => x.DtRecord).ToList(); + //var lastRecord = newData.Records.OrderByDescending(x => x.DtRecord).Take(1).ToList(); + + if (machRecord != null) + { + machRecord.Records = lastRecord; + } + // aggiungo + else + { + currData.Add(new MachineStatData() { Machine = updateRecords.Machine, Records = lastRecord }); + } + } + + // serializzo + string rawData = JsonConvert.SerializeObject(currData); + + // salvo! + bool answ = memLayer.ML.setRSV(redProdMachStateData, rawData); + return answ; + } + + /// + /// Reset stati macchina registrati + /// + /// + public static bool prodMachStateDataReset() + { + bool answ = memLayer.ML.setRSV(redProdMachStateData, "", 1); + return answ; + } + /// /// Leggo il Takt inviato /// diff --git a/NKC_SDK/NKC.cs b/NKC_SDK/NKC.cs index c34c778..87c5cdd 100644 --- a/NKC_SDK/NKC.cs +++ b/NKC_SDK/NKC.cs @@ -29,7 +29,7 @@ namespace NKC_SDK { _baseIp = baseIp; _baseUrl = baseUrl; - _codPost = codPost; + _machine = codPost; } #endregion Public Constructors @@ -52,7 +52,7 @@ namespace NKC_SDK /// /// COD macchina x cui si effettua chiamata /// - protected string _codPost { get; set; } = ""; + protected string _machine { get; set; } = ""; protected string urlAlive { @@ -82,7 +82,20 @@ namespace NKC_SDK { get { - return $"{_baseUrl}api/Sheet/{_codPost}"; + return $"{_baseUrl}api/Sheet/{_machine}"; + } + } + + /// + /// URL x salvataggio dati MachineStat + /// + /// + protected string urlPutMachStat + { + get + { + return $"{_baseUrl}api/MachineStat"; + //return $"{_baseUrl}api/MachineStat/{_machine}"; } } @@ -94,7 +107,7 @@ namespace NKC_SDK { get { - return $"{_baseUrl}api/Sheet/{_codPost}"; + return $"{_baseUrl}api/Sheet/{_machine}"; } } @@ -323,6 +336,31 @@ namespace NKC_SDK return answ; } + /// + /// Effettua salvataggio dell'elenco info stati/eventi su NKC + /// + /// + public bool saveMachStatRecords(MachineStatData updatedInfo) + { + bool answ = false; + string rawdata = ""; + try + { + // aggiungo macchina nel codice dei fogli... + updatedInfo.Machine = _machine; + // serializzo oggetto + rawdata = JsonConvert.SerializeObject(updatedInfo); + // invio con metodo put! + putData(urlPutMachStat, rawdata); + answ = true; + } + catch (Exception exc) + { + Log.Instance.Error($"Eccezione durante saveMachStatRecords, ricevuto {rawdata}{Environment.NewLine}{exc}"); + } + return answ; + } + /// /// Effettua salvataggio dell'elenco dei fogli (1..n) su NKC /// @@ -333,6 +371,8 @@ namespace NKC_SDK string rawdata = ""; try { + // aggiungo macchina nel codice dei fogli... + updatedSheetList.Machine = _machine; // serializzo oggetto rawdata = JsonConvert.SerializeObject(updatedSheetList); // invio con metodo put! diff --git a/NKC_SDK/Objects.cs b/NKC_SDK/Objects.cs index 5be77bf..833682f 100644 --- a/NKC_SDK/Objects.cs +++ b/NKC_SDK/Objects.cs @@ -233,6 +233,51 @@ namespace NKC_SDK #endregion Public Properties } + public class MachineStatData + { + #region Public Properties + + /// + /// Codice macchina + /// + public string Machine { get; set; } = "WRK000"; + + /// + /// record registrati dalla macchina + /// + public List Records { get; set; } = new List(); + + #endregion Public Properties + } + + public class MachineStatRecord + { + #region Public Properties + + /// + /// Codice evento/stato + /// 00 = KeepAlive --> imposto dataora/drift + /// + public string Code { get; set; } = ""; + + /// + /// DataOra dell'informazione (clock mittente) + /// + public DateTime DtRecord { get; set; } = DateTime.Now; + + /// + /// Valore / carico associato all'invio (opzionale) + /// + public string Payload { get; set; } = ""; + + /// + /// Codice postazione + /// + public string Station { get; set; } = ""; + + #endregion Public Properties + } + /// /// Definizione classe x materiali /// @@ -861,6 +906,11 @@ namespace NKC_SDK { #region Public Properties + /// + /// Codice impianto/macchina + /// + public string Machine { get; set; } = "WRK000"; + /// /// Numero di Sheets da lavorare ///