338 lines
11 KiB
C#
338 lines
11 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using MP.MONO.Core.DTO;
|
|
using MP.MONO.Data.DbModels;
|
|
using NLog;
|
|
|
|
namespace MP.MONO.Data.Controllers
|
|
{
|
|
public class MpDbController : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public MpDbController()
|
|
{ }
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Recupero elenco allarmi (ultimi datop "skip")
|
|
/// </summary>
|
|
/// <param name="MachineId"></param>
|
|
/// <param name="skipRec"></param>
|
|
/// <param name="numRec"></param>
|
|
/// <returns></returns>
|
|
public List<AlarmLogModel> AlarmLogGetFilt(int MachineId, int skipRec, int numRec)
|
|
{
|
|
List<AlarmLogModel> dbResult = new List<AlarmLogModel>();
|
|
using (MapoMonoContext localDbCtx = new MapoMonoContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetAlarmLog
|
|
.Where(x => x.MachineId == MachineId)
|
|
.Include(m => m.MachineNav)
|
|
.OrderByDescending(x => x.AlarmLogId)
|
|
.Skip(skipRec)
|
|
.Take(numRec)
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante AlarmLogGetFilt{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserimento di un record AlarmLog
|
|
/// </summary>
|
|
/// <param name="newItem">Record da inserire (senza ID...)</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AlarmLogInsert(AlarmLogModel newItem)
|
|
{
|
|
bool fatto = false;
|
|
using (MapoMonoContext localDbCtx = new MapoMonoContext())
|
|
{
|
|
try
|
|
{
|
|
localDbCtx
|
|
.DbSetAlarmLog
|
|
.Add(newItem);
|
|
await localDbCtx.SaveChangesAsync();
|
|
fatto = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante AlarmLogInsert{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserimento di un record AlarmLog
|
|
/// </summary>
|
|
/// <param name="newItems">Lista Record da inserire (senza ID...)</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AlarmLogInsertMany(List<AlarmLogModel> newItems)
|
|
{
|
|
bool fatto = false;
|
|
using (MapoMonoContext localDbCtx = new MapoMonoContext())
|
|
{
|
|
try
|
|
{
|
|
await localDbCtx
|
|
.DbSetAlarmLog
|
|
.AddRangeAsync(newItems);
|
|
await localDbCtx.SaveChangesAsync();
|
|
fatto = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante AlarmLogInsert{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero record DataLog data condizione filtro
|
|
/// </summary>
|
|
/// <param name="MachineId"></param>
|
|
/// <param name="FluxType"></param>
|
|
/// <param name="skipRec"></param>
|
|
/// <param name="numRec"></param>
|
|
/// <returns></returns>
|
|
public List<DataLogModel> DataLogGetFilt(int MachineId, string FluxType, int skipRec, int numRec)
|
|
{
|
|
List<DataLogModel> dbResult = new List<DataLogModel>();
|
|
using (MapoMonoContext localDbCtx = new MapoMonoContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetDataLog
|
|
.Where(x => x.MachineId == MachineId && x.FluxType == FluxType)
|
|
.Include(m => m.MachineNav)
|
|
.OrderByDescending(x => x.DataLogId)
|
|
.Skip(skipRec)
|
|
.Take(numRec)
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante DataLogGetFilt{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserimento di un SET di record DataLog (post aggregazione base VC)
|
|
/// </summary>
|
|
/// <param name="newItems">Lista Record da inserire (senza ID...)</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DataLogInsertMany(List<DataLogModel> newItems)
|
|
{
|
|
bool fatto = false;
|
|
using (MapoMonoContext localDbCtx = new MapoMonoContext())
|
|
{
|
|
try
|
|
{
|
|
await localDbCtx
|
|
.DbSetDataLog
|
|
.AddRangeAsync(newItems);
|
|
await localDbCtx.SaveChangesAsync();
|
|
fatto = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante DataLogInsert{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
/// <summary>
|
|
/// Recupero record DataStAg (aggregati) data condizione filtro
|
|
/// </summary>
|
|
/// <param name="MachineId"></param>
|
|
/// <param name="FluxType"></param>
|
|
/// <param name="skipRec"></param>
|
|
/// <param name="numRec"></param>
|
|
/// <returns></returns>
|
|
public List<DataStAgModel> DataStAgGetFilt(int MachineId, string FluxType, int skipRec, int numRec)
|
|
{
|
|
List<DataStAgModel> dbResult = new List<DataStAgModel>();
|
|
using (MapoMonoContext localDbCtx = new MapoMonoContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = localDbCtx
|
|
.DbSetDataStAg
|
|
.Where(x => x.MachineId == MachineId && x.FluxType == FluxType)
|
|
.Include(m => m.MachineNav)
|
|
.OrderByDescending(x => x.DataStAgId)
|
|
.Skip(skipRec)
|
|
.Take(numRec)
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante DataLogGetFilt{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
/// <summary>
|
|
/// Inserimento di un record DataStAg (aggregati) - creati da ANALYZER che effettua compattazione
|
|
/// </summary>
|
|
/// <param name="newItem">Record da inserire (senza ID...)</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DataStAgInsert(DataStAgModel newItem)
|
|
{
|
|
bool fatto = false;
|
|
using (MapoMonoContext localDbCtx = new MapoMonoContext())
|
|
{
|
|
try
|
|
{
|
|
localDbCtx
|
|
.DbSetDataStAg
|
|
.Add(newItem);
|
|
await localDbCtx.SaveChangesAsync();
|
|
fatto = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante DataStAgInsert{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database context
|
|
//Log.Info("Dispose di GWMSController");
|
|
}
|
|
|
|
public List<DisplayDataDTO> MachineGetDisplay()
|
|
{
|
|
List<DisplayDataDTO> answ = new List<DisplayDataDTO>();
|
|
|
|
// !!!FIXME TODO... è fake...
|
|
|
|
Random rand = new Random();
|
|
double currVal = 0;
|
|
currVal = rand.Next(10, 120) * 100;
|
|
DisplayDataDTO displ01 = new DisplayDataDTO()
|
|
{
|
|
Order = 0,
|
|
Title = "SPEED",
|
|
ValueNum = currVal,
|
|
IsNumeric = true,
|
|
Value = currVal.ToString("N0"),
|
|
Type = "SPEED-5000-10000"
|
|
};
|
|
answ.Add(displ01);
|
|
|
|
currVal = rand.Next(10, 100) * 100;
|
|
DisplayDataDTO displ02 = new DisplayDataDTO()
|
|
{
|
|
Order = 0,
|
|
Title = "FEED",
|
|
ValueNum = currVal,
|
|
IsNumeric = true,
|
|
Value = currVal.ToString("N0"),
|
|
Type = "FEED-3000-5000"
|
|
};
|
|
answ.Add(displ02);
|
|
|
|
currVal = rand.NextDouble() * 1.2;
|
|
DisplayDataDTO displ03 = new DisplayDataDTO()
|
|
{
|
|
Order = 0,
|
|
Title = "SPINDLE LOAD",
|
|
ValueNum = currVal,
|
|
IsNumeric = true,
|
|
Value = currVal.ToString("P0"),
|
|
Type = "ORDER"
|
|
};
|
|
answ.Add(displ03);
|
|
|
|
currVal = rand.NextDouble() * 5000;
|
|
DisplayDataDTO displ04 = new DisplayDataDTO()
|
|
{
|
|
Order = 0,
|
|
Title = "X POS",
|
|
ValueNum = currVal,
|
|
IsNumeric = true,
|
|
Value = currVal.ToString("N1"),
|
|
Type = "POS"
|
|
};
|
|
answ.Add(displ04);
|
|
|
|
currVal = rand.NextDouble() * 10000;
|
|
DisplayDataDTO displ05 = new DisplayDataDTO()
|
|
{
|
|
Order = 0,
|
|
Title = "Y POS",
|
|
ValueNum = currVal,
|
|
IsNumeric = true,
|
|
Value = currVal.ToString("N1"),
|
|
Type = "POS"
|
|
};
|
|
answ.Add(displ05);
|
|
|
|
currVal = rand.NextDouble() * -3000;
|
|
DisplayDataDTO displ06 = new DisplayDataDTO()
|
|
{
|
|
Order = 0,
|
|
Title = "Z POS",
|
|
ValueNum = currVal,
|
|
IsNumeric = true,
|
|
Value = currVal.ToString("N1"),
|
|
Type = "POS"
|
|
};
|
|
answ.Add(displ06);
|
|
|
|
Task.Delay(200).Wait();
|
|
|
|
return answ;
|
|
}
|
|
|
|
public ProductionDTO MachineGetProd()
|
|
{
|
|
// !!!FIXME TODO... è fake...
|
|
|
|
Random rand = new Random();
|
|
int stdCycle = 5;
|
|
|
|
ProductionDTO currMachDto = new ProductionDTO()
|
|
{
|
|
Order = "ODL Test",
|
|
ItemCode = "ART.0000123",
|
|
ProgName = "P000012",
|
|
CurrQty = DateTime.Now.Minute + rand.Next(1, 40),
|
|
OrderQty = 100,
|
|
CycleTimeMin = rand.NextDouble() * stdCycle,
|
|
Message = "...simulated data..."
|
|
};
|
|
|
|
Task.Delay(400).Wait();
|
|
|
|
return currMachDto;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |