FIrst version of prodInfo DB persistence

This commit is contained in:
Samuele Locatelli
2020-06-16 19:28:27 +02:00
parent 125532639a
commit a6cdf864ae
8 changed files with 200 additions and 144 deletions
+1 -143
View File
@@ -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<DTOHeadModel> 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<DTOQueueModel> 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);
@@ -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<ProdInfoModel> 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;
}
/// <summary>
/// Process table and keep only maxKeep most recent ones
/// </summary>
/// <param name="maxKeep"></param>
/// <returns></returns>
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();
// }
//}
}
}
@@ -42,6 +42,10 @@ namespace Thermo.Active.Database
public DbSet<AlarmNoteModel> AlarmsNotes { get; set; }
public DbSet<AlarmFileModel> AlarmFiles { get; set; }
// thermo!
public DbSet<ProdInfoModel> ProdInfo { get; set; }
// Create migration string
public static string CONNECTION_STRING = "Server = " + "localhost" + "; Database=" + DATABASE_NAME + ";Uid=" + DATABASE_USER + ";Pwd=" + DATABASE_PWD + ";";
@@ -113,6 +113,7 @@
<ItemGroup>
<Compile Include="Controllers\AlarmsController.cs" />
<Compile Include="Controllers\FunctionsAccessController.cs" />
<Compile Include="Controllers\ProdInfoController.cs" />
<Compile Include="Controllers\MachinesController.cs" />
<Compile Include="Controllers\MaintenancesController.cs" />
<Compile Include="Controllers\QueueController.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; }
}
}
@@ -89,6 +89,7 @@
<Compile Include="DatabaseModels\NcOffsetModel.cs" />
<Compile Include="DatabaseModels\NcFamilyModel.cs" />
<Compile Include="DatabaseModels\PerformedMaintenanceModel.cs" />
<Compile Include="DatabaseModels\ProdInfoModel.cs" />
<Compile Include="DatabaseModels\QueueItemsModel.cs" />
<Compile Include="DatabaseModels\RoleModel.cs">
<Generator>DtsGenerator</Generator>
+21
View File
@@ -149,7 +149,14 @@ namespace Thermo.Active.NC
#region cached data
/// <summary>
/// Last recipe data from PLC
/// </summary>
protected static Dictionary<string, DTORecipeParam> lastRecipe = new Dictionary<string, DTORecipeParam>();
/// <summary>
/// Last prod info from PLC
/// </summary>
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)
+1 -1
View File
@@ -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")]