Files
egtbeamwall/EgtBEAMWALL.DataLayer/Controllers/LogMachineController.cs
T
2021-06-21 17:50:11 +02:00

230 lines
7.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EgtBEAMWALL.DataLayer.DatabaseModels;
namespace EgtBEAMWALL.DataLayer.Controllers
{
public class LogMachineController : IDisposable
{
#region Private Fields
private DatabaseContext dbCtx;
#endregion Private Fields
#region Public Constructors
public LogMachineController()
{
// Initialize database context
dbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING);
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Conversion of base class to DB model class
/// </summary>
/// <param name="coreMacLog"></param>
/// <returns></returns>
public static LogMachineModel ConvertFromCore(Core.MachLog coreMacLog)
{
LogMachineModel answ = new LogMachineModel();
if (coreMacLog != null)
{
answ = new LogMachineModel()
{
AlarmCode = coreMacLog.AlarmCode,
AlarmDatetime = coreMacLog.AlarmDateTime,
AlarmMessage = coreMacLog.AlarmMessage,
AlarmOperation = coreMacLog.AlarmOperation,
AlarmType = coreMacLog.AlarmType,
CommandExecutedCorrectly = coreMacLog.CommandExecutedCorrectly,
CommandState = coreMacLog.CommandState,
CommandType = coreMacLog.CommandType,
Description = coreMacLog.Description,
NewOpState = coreMacLog.newOpState,
ResultType = coreMacLog.ResultType,
VarAddress = coreMacLog.VarAddress,
VarValue = coreMacLog.VarValue
};
}
return answ;
}
/// <summary>
/// Conversion from DB to Core class
/// </summary>
/// <param name="dbLog"></param>
/// <returns></returns>
public static Core.MachLog ConvertToCore(LogMachineModel dbLog)
{
var newRecord = Core.MachLog.CreateMacLog(dbLog.AlarmCode, dbLog.AlarmDatetime, dbLog.AlarmMessage, dbLog.AlarmOperation, dbLog.AlarmType, dbLog.CommandExecutedCorrectly, dbLog.CommandState, dbLog.CommandType, dbLog.Description, dbLog.NewOpState, dbLog.ResultType, dbLog.VarAddress, dbLog.VarValue);
return newRecord;
}
/// <summary>
/// Create machine LOG record
/// </summary>
/// <param name="newLogMac"></param>
/// <returns></returns>
public bool Create(LogMachineModel newLogMac)
{
bool fatto = false;
try
{
try
{
using (var currDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
// Add to database
currDbCtx.LogMachineList.Add(newLogMac);
// Commit changes
currDbCtx.SaveChanges();
ResetController();
}
}
catch (Exception exc)
{
Console.WriteLine($"EXCEPTION on Create LogMachine: {exc}");
}
}
catch
{ }
return fatto;
}
/// <summary>
/// Create machine LOG record (da modello dati CORE)
/// </summary>
/// <param name="newLogMac"></param>
/// <returns></returns>
public bool Create(Core.MachLog newMachLog)
{
var dbLogModel = ConvertFromCore(newMachLog);
return Create(dbLogModel);
}
/// <summary>
/// Delete by key
/// </summary>
/// <param name="LogDbId"></param>
/// <returns></returns>
public bool DeleteByKey(int LogDbId)
{
bool done = false;
var items2del = dbCtx
.LogMachineList
.Where(x => x.LogDbId == LogDbId);
try
{
// Add to database
dbCtx.LogMachineList.RemoveRange(items2del);
// Commit changes
dbCtx.SaveChanges();
ResetController();
done = true;
}
catch (Exception exc)
{
Console.WriteLine($"EXCEPTION on DeleteByKey: {exc}");
}
return done;
}
public void Dispose()
{
// Clear database context
dbCtx.Dispose();
}
/// <summary>
/// Get record by LogDbId
/// </summary>
/// <param name="LogDbId"></param>
/// <returns></returns>
public LogMachineModel FindByDbId(int LogDbId)
{
return dbCtx
.LogMachineList
.Where(x => x.LogDbId == LogDbId)
.SingleOrDefault();
}
/// <summary>
/// Get paginated data from DB (ASC ordered)
/// </summary>
/// <param name="dtStart"></param>
/// <param name="dtEnd"></param>
/// <returns></returns>
public List<LogMachineModel> GetAsc(DateTime dtStart, DateTime dtEnd)
{
// retrieve
return dbCtx
.LogMachineList
.Where(x => x.AlarmDatetime >= dtStart && x.AlarmDatetime <= dtEnd)
.OrderBy(x => x.AlarmDatetime)
.ToList();
}
/// <summary>
/// Get paginated data from DB (DESC ordered)
/// </summary>
/// <param name="dtStart"></param>
/// <param name="dtEnd"></param>
/// <returns></returns>
public List<LogMachineModel> GetDesc(DateTime dtStart, DateTime dtEnd)
{
// retrieve
return dbCtx
.LogMachineList
.Where(x => x.AlarmDatetime >= dtStart && x.AlarmDatetime <= dtEnd)
.OrderByDescending(x => x.AlarmDatetime)
.ToList();
}
/// <summary>
/// Reinizializzaizone del controller
/// </summary>
public void ResetController()
{
// Re-Initialize database context
dbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING);
}
/// <summary>
/// Update single LogMachineModel
/// </summary>
/// <param name="updItem"></param>
/// <returns></returns>
public bool Update(LogMachineModel updItem)
{
bool done = false;
var item2update = dbCtx
.LogMachineList
.Where(x => x.LogDbId == updItem.LogDbId)
.SingleOrDefault();
try
{
// update, vers 1...
dbCtx.Entry(item2update).CurrentValues.SetValues(updItem);
// Commit changes
dbCtx.SaveChanges();
ResetController();
done = true;
}
catch (Exception exc)
{
Console.WriteLine($"EXCEPTION on Update: {exc}");
}
return done;
}
#endregion Public Methods
}
}