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 /// /// Recupero elenco allarmi (ultimi datop "skip") /// /// /// /// /// public List AlarmLogGetFilt(int MachineId, int skipRec, int numRec) { List dbResult = new List(); 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; } /// /// Inserimento di un record AlarmLog /// /// Record da inserire (senza ID...) /// public async Task 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; } /// /// Inserimento di un record AlarmLog /// /// Lista Record da inserire (senza ID...) /// public async Task AlarmLogInsertMany(List 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; } /// /// Recupero record DataLog data condizione filtro /// /// /// /// /// /// public List DataLogGetFilt(int MachineId, string FluxType, int skipRec, int numRec) { List dbResult = new List(); 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; } /// /// Inserimento di un SET di record DataLog (post aggregazione base VC) /// /// Lista Record da inserire (senza ID...) /// public async Task DataLogInsertMany(List 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; } /// /// Recupero record DataStAg (aggregati) data condizione filtro /// /// /// /// /// /// public List DataStAgGetFilt(int MachineId, string FluxType, int skipRec, int numRec) { List dbResult = new List(); 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; } /// /// Inserimento di un record DataStAg (aggregati) - creati da ANALYZER che effettua compattazione /// /// Record da inserire (senza ID...) /// public async Task 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 MachineGetDisplay() { List answ = new List(); // !!!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 } }