From a6cdf864aeb11beb722968f85afef3d72cba7b7b Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 16 Jun 2020 19:28:27 +0200 Subject: [PATCH] FIrst version of prodInfo DB persistence --- Thermo.Active.Core/ThreadsFunctions.cs | 144 +----------------- .../Controllers/ProdInfoController.cs | 120 +++++++++++++++ Thermo.Active.Database/DatabaseContext.cs | 4 + .../Thermo.Active.Database.csproj | 1 + .../DatabaseModels/ProdInfoModel.cs | 51 +++++++ .../Thermo.Active.Model.csproj | 1 + Thermo.Active.NC/NcAdapter.cs | 21 +++ Thermo.Active/Properties/AssemblyInfo.cs | 2 +- 8 files changed, 200 insertions(+), 144 deletions(-) create mode 100644 Thermo.Active.Database/Controllers/ProdInfoController.cs create mode 100644 Thermo.Active.Model/DatabaseModels/ProdInfoModel.cs diff --git a/Thermo.Active.Core/ThreadsFunctions.cs b/Thermo.Active.Core/ThreadsFunctions.cs index 8a052366..ec9b6762 100644 --- a/Thermo.Active.Core/ThreadsFunctions.cs +++ b/Thermo.Active.Core/ThreadsFunctions.cs @@ -499,52 +499,6 @@ public static class ThreadsFunctions } } -#if false - public static void ReadHeadsData() - { - NcAdapter ncAdapter = new NcAdapter(); - Stopwatch sw = new Stopwatch(); - - try - { - // Try connection - CmsError libraryError = ncAdapter.Connect(); - if (libraryError.errorCode != 0) - ManageLibraryError(libraryError); - - while (true) - { - sw.Restart(); - - if (ncAdapter.numericalControl.NC_IsConnected()) - { - // Get Data from config and PLC - libraryError = ncAdapter.GetHeadsData(out List heads); - if (libraryError.errorCode != 0) - ManageLibraryError(libraryError); - else - // Send through signalR - MessageServices.Current.Publish(SEND_HEADS_DATA, null, heads); - } - else - RestoreConnection(); - - sw.Stop(); - - //Update thread timer - UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); - - // Wait - Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); - } - } - catch (ThreadAbortException) - { - ncAdapter.Dispose(); - } - } -#endif - public static void ReadAxesNamesData() { NcAdapter ncAdapter = new NcAdapter(); @@ -633,102 +587,6 @@ public static class ThreadsFunctions } } -#if false - public static void ReadActiveProgramData() - { - NcFileAdapter ncAdapter = new NcFileAdapter(); - Stopwatch sw = new Stopwatch(); - - try - { - // Try connection - CmsError libraryError = ncAdapter.Connect(); - if (libraryError.errorCode != 0) - ManageLibraryError(libraryError); - - while (true) - { - sw.Restart(); - - if (ncAdapter.numericalControl.NC_IsConnected()) - { - // Get Data from config and PLC - libraryError = ncAdapter.GetActiveProgramInfo(out DTOActiveProgramDataModel active); - - if (libraryError.IsError()) - ManageLibraryError(libraryError); - else - // Send through signalR - MessageServices.Current.Publish(SEND_ACTIVE_PROGRAM_DATA, null, active); - } - else - RestoreConnection(); - - sw.Stop(); - - //Update thread timer - UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); - - // Wait - Thread.Sleep(CalcSleepTime(400, (int)sw.ElapsedMilliseconds)); - } - } - catch (ThreadAbortException) - { - ncAdapter.Dispose(); - } - } -#endif - -#if false - public static void ReadPartProgramQueueData() - { - NcFileAdapter ncAdapter = new NcFileAdapter(); - Stopwatch sw = new Stopwatch(); - - try - { - // Try connection - CmsError libraryError = ncAdapter.Connect(); - if (libraryError.errorCode != 0) - ManageLibraryError(libraryError); - - while (true) - { - sw.Restart(); - - // Check if client is connected - if (ncAdapter.numericalControl.NC_IsConnected()) - { - // Read data - libraryError = ncAdapter.UpdateQueue(); - if (libraryError.IsError()) - ManageLibraryError(libraryError); - - libraryError = ncAdapter.GetSelectedProcessQueue(out List queue); - if (libraryError.IsError()) - ManageLibraryError(libraryError); - - MessageServices.Current.Publish(SEND_QUEUE_DATA, null, queue); - } - else - RestoreConnection(); - sw.Stop(); - - //Update thread timer - UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); - - // Wait - Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); - } - } - catch (ThreadAbortException) - { - ncAdapter.Dispose(); - } - } -#endif - public static void ReadScadaData() { NcAdapter ncAdapter = new NcAdapter(); @@ -835,7 +693,7 @@ public static class ThreadsFunctions // Check if client is connected if (ncAdapter.numericalControl.NC_IsConnected()) { - // Get new data from PLC + // Get new data from PLC (and log if changed...) libraryError = ncAdapter.ReadProdInfoData(out ThermoModels.ProdInfoModel prodInfoData); if (libraryError.IsError()) ManageLibraryError(libraryError); diff --git a/Thermo.Active.Database/Controllers/ProdInfoController.cs b/Thermo.Active.Database/Controllers/ProdInfoController.cs new file mode 100644 index 00000000..31b90cad --- /dev/null +++ b/Thermo.Active.Database/Controllers/ProdInfoController.cs @@ -0,0 +1,120 @@ +using Thermo.Active.Model.DatabaseModels; +using System; +using System.Linq; +using static Thermo.Active.Config.ServerConfig; +using System.Collections.Generic; +using CMS_CORE_Library.Models; + +namespace Thermo.Active.Database.Controllers +{ + public class ProdInfoController : IDisposable + { + private DatabaseContext dbCtx; + + public ProdInfoController() + { + // Initialize database context + dbCtx = new DatabaseContext(); + } + + public void Dispose() + { + // Clear database context + dbCtx.Dispose(); + } + + public ProdInfoModel FindByNumDone(int num) + { + return dbCtx + .ProdInfo + .Where(x => x.NumDone == num) + .SingleOrDefault(); + } + + public List GetPaginated(int numStart, int numRecord) + { + // cehck numEnd + int numEnd = numStart - numRecord; + if (numEnd < 0) + numEnd = 0; + // retrieve + return dbCtx + .ProdInfo + .Where(x => x.NumDone <= numStart && x.NumDone >= numEnd) + .ToList(); + } + + public ProdInfoModel Create(short NumTarget, short NumDone, int TimeWarm, int TimeVent, int TimeVacuum, int TimeCycleGross, int TimeCycleNet, double MaterialTempEndWarm, double MaterialTempEndVent, double MoldTemp, double VacuumReadVal, double MouldEnergyOUT, double MouldEnergyIN) + { + // Create database machine model + ProdInfoModel prodData = new ProdInfoModel() + { + NumTarget = NumTarget, + NumDone = NumDone, + TimeWarm = TimeWarm, + TimeVent = TimeVent, + TimeVacuum = TimeVacuum, + TimeCycleGross = TimeCycleGross, + TimeCycleNet = TimeCycleNet, + MaterialTempEndWarm = MaterialTempEndWarm, + MaterialTempEndVent = MaterialTempEndVent, + MoldTemp = MoldTemp, + VacuumReadVal = VacuumReadVal, + MouldEnergyOUT = MouldEnergyOUT, + MouldEnergyIN = MouldEnergyIN + }; + // Add to database + dbCtx.ProdInfo.Add(prodData); + // Commit changes + dbCtx.SaveChanges(); + + return prodData; + } + + /// + /// Process table and keep only maxKeep most recent ones + /// + /// + /// + public bool PurgeOldest(int maxKeep) + { + bool answ = false; + + // check if purge needed + int numRec = dbCtx.ProdInfo.Count(); + if (numRec > maxKeep) + { + ProdInfoModel firstToDelete = (ProdInfoModel)(from p in dbCtx.ProdInfo + orderby p.DtEvent descending + select p).Skip(maxKeep).Take(1); + + // call deletion + dbCtx + .ProdInfo + .RemoveRange( + dbCtx + .ProdInfo + .Where(x => x.DtEvent <= firstToDelete.DtEvent) + ); + + // save! + dbCtx.SaveChanges(); + + } + return answ; + } + + + //public void UpdateMachineName(int id, string name) + //{ + // // Find machine by id + // MachineModel machine = FindById(id); + // if (machine != null) + // { + // // Update machine name + // machine.Model = name; + // dbCtx.SaveChanges(); + // } + //} + } +} \ No newline at end of file diff --git a/Thermo.Active.Database/DatabaseContext.cs b/Thermo.Active.Database/DatabaseContext.cs index bfddd88d..db17d046 100644 --- a/Thermo.Active.Database/DatabaseContext.cs +++ b/Thermo.Active.Database/DatabaseContext.cs @@ -42,6 +42,10 @@ namespace Thermo.Active.Database public DbSet AlarmsNotes { get; set; } public DbSet AlarmFiles { get; set; } + // thermo! + public DbSet ProdInfo { get; set; } + + // Create migration string public static string CONNECTION_STRING = "Server = " + "localhost" + "; Database=" + DATABASE_NAME + ";Uid=" + DATABASE_USER + ";Pwd=" + DATABASE_PWD + ";"; diff --git a/Thermo.Active.Database/Thermo.Active.Database.csproj b/Thermo.Active.Database/Thermo.Active.Database.csproj index f580e258..b1ce8ba8 100644 --- a/Thermo.Active.Database/Thermo.Active.Database.csproj +++ b/Thermo.Active.Database/Thermo.Active.Database.csproj @@ -113,6 +113,7 @@ + diff --git a/Thermo.Active.Model/DatabaseModels/ProdInfoModel.cs b/Thermo.Active.Model/DatabaseModels/ProdInfoModel.cs new file mode 100644 index 00000000..9c95fef8 --- /dev/null +++ b/Thermo.Active.Model/DatabaseModels/ProdInfoModel.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Thermo.Active.Model.DatabaseModels +{ + [Table("ProdInfo")] + public class ProdInfoModel + { + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.None)] + [Column("DtEvent", Order = 0)] + public DateTime DtEvent { get; set; } + + [Column("NumTarget")] + public short NumTarget { get; set; } + [Column("NumDone")] + public short NumDone { get; set; } + + [Column("TimeWarm")] + public int TimeWarm { get; set; } + [Column("TimeVent")] + public int TimeVent { get; set; } + [Column("TimeVacuum")] + public int TimeVacuum { get; set; } + [Column("TimeCycleGross")] + public int TimeCycleGross { get; set; } + [Column("TimeCycleNet")] + public int TimeCycleNet { get; set; } + + [Column("MaterialTempEndWarm")] + public double MaterialTempEndWarm { get; set; } + [Column("MaterialTempEndVent")] + public double MaterialTempEndVent { get; set; } + [Column("MoldTemp")] + public double MoldTemp { get; set; } + + [Column("VacuumReadVal")] + public double VacuumReadVal { get; set; } + + [Column("MouldEnergyOUT")] + public double MouldEnergyOUT { get; set; } + [Column("MouldEnergyIN")] + public double MouldEnergyIN { get; set; } + + } +} diff --git a/Thermo.Active.Model/Thermo.Active.Model.csproj b/Thermo.Active.Model/Thermo.Active.Model.csproj index 83b17f6c..1583fc3f 100644 --- a/Thermo.Active.Model/Thermo.Active.Model.csproj +++ b/Thermo.Active.Model/Thermo.Active.Model.csproj @@ -89,6 +89,7 @@ + DtsGenerator diff --git a/Thermo.Active.NC/NcAdapter.cs b/Thermo.Active.NC/NcAdapter.cs index 9b2dc9cf..c427be01 100644 --- a/Thermo.Active.NC/NcAdapter.cs +++ b/Thermo.Active.NC/NcAdapter.cs @@ -149,7 +149,14 @@ namespace Thermo.Active.NC #region cached data + /// + /// Last recipe data from PLC + /// protected static Dictionary lastRecipe = new Dictionary(); + /// + /// Last prod info from PLC + /// + protected static ThermoModels.ProdInfoModel lastProdInfoData = new ThermoModels.ProdInfoModel(); #endregion @@ -1244,6 +1251,20 @@ namespace Thermo.Active.NC prodInfoData = new ThermoModels.ProdInfoModel(); CmsError cmsError = numericalControl.PLC_RProdInfo(ref prodInfoData); + // do comparison with old record and if changed --> persist on DB! + if(lastProdInfoData != null) + { + if(!prodInfoData.Equals(lastProdInfoData)) + { + // save on DB! + using (ProdInfoController prodInfoController = new ProdInfoController()) + prodInfoController.Create(prodInfoData.NumTarget, prodInfoData.NumDone, prodInfoData.TimeWarm, prodInfoData.TimeVent, prodInfoData.TimeVacuum, prodInfoData.TimeCycleGross, prodInfoData.TimeCycleNet, prodInfoData.MaterialTempEndWarm, prodInfoData.MaterialTempEndVent, prodInfoData.MoldTemp, prodInfoData.VacuumReadVal, prodInfoData.MouldEnergyOUT, prodInfoData.MouldEnergyIN); + + // update last info data + lastProdInfoData = prodInfoData; + } + } + return cmsError; } public CmsError ReadProdCycleData(out ThermoModels.ProdCycleModel prodCycleData) diff --git a/Thermo.Active/Properties/AssemblyInfo.cs b/Thermo.Active/Properties/AssemblyInfo.cs index 9accf279..45467190 100644 --- a/Thermo.Active/Properties/AssemblyInfo.cs +++ b/Thermo.Active/Properties/AssemblyInfo.cs @@ -31,4 +31,4 @@ using System.Runtime.InteropServices; // // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("0.8.1")] +[assembly: AssemblyVersion("0.8.2")]