129 lines
3.5 KiB
C#
129 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using GWMS.Data.DTO;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
|
|
namespace GWMS.Data.Controllers
|
|
{
|
|
public class GWMSController : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration;
|
|
private static GWMSContext dbCtx;
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public GWMSController(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
dbCtx = new GWMSContext(configuration);
|
|
Log.Info("Avviata classe GWMSController");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database context
|
|
dbCtx.Dispose();
|
|
}
|
|
|
|
public List<DatabaseModels.ConfigModel> GetConfig()
|
|
{
|
|
var dbResult = dbCtx
|
|
.DbSetConfig
|
|
.ToList();
|
|
|
|
return dbResult;
|
|
}
|
|
|
|
public List<DatabaseModels.ItemModel> GetItems()
|
|
{
|
|
var dbResult = dbCtx
|
|
.DbSetItems
|
|
.ToList();
|
|
|
|
return dbResult;
|
|
}
|
|
|
|
public DatabaseModels.PlantDetailModel GetPlant(int PlantId)
|
|
{
|
|
var dbResult = dbCtx
|
|
.DbSetPlant
|
|
.Where(x => x.PlantId == PlantId)
|
|
.FirstOrDefault();
|
|
|
|
return dbResult;
|
|
}
|
|
|
|
public List<DatabaseModels.PlantDetailModel> GetPlants()
|
|
{
|
|
var dbResult = dbCtx
|
|
.DbSetPlant
|
|
.ToList();
|
|
|
|
return dbResult;
|
|
}
|
|
|
|
public List<PlantDTO> GetPlantsDTO()
|
|
{
|
|
var plantList = dbCtx
|
|
.DbSetPlant
|
|
.ToList();
|
|
|
|
var dbResult = plantList
|
|
.Select(x => PlantDTO(x.PlantId))
|
|
.ToList();
|
|
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero info PLANT modalità DTO
|
|
/// </summary>
|
|
/// <param name="PlantId"></param>
|
|
/// <returns></returns>
|
|
public PlantDTO PlantDTO(int PlantId)
|
|
{
|
|
var currPlant = GetPlant(PlantId);
|
|
|
|
// FIXME TODO !!!! è casuale
|
|
Random rnd = new Random();
|
|
var simLevel = rnd.Next(1, (int)currPlant.LevelMax);
|
|
List<TSData> SimLevelTS = new List<TSData>();
|
|
DateTime adesso = DateTime.Now;
|
|
int lastLevel = simLevel;
|
|
for (int i = 100; i > 0; i--)
|
|
{
|
|
lastLevel = lastLevel - rnd.Next(0, 100);
|
|
// se < 500 --> rialzo
|
|
lastLevel = lastLevel < 1000 ? 9000 : lastLevel;
|
|
SimLevelTS.Add(new TSData() { DtEvent = adesso.AddMinutes(-i * 3), ValDouble = lastLevel });
|
|
//SimLevelTS.Add(new TSData() { DtEvent = adesso.AddMinutes(-i), ValDouble = simLevel + i * 20 + rnd.Next(0, 5) });
|
|
}
|
|
|
|
PlantDTO answ = new PlantDTO()
|
|
{
|
|
PlantId = PlantId,
|
|
PlantCode = currPlant.PlantCode,
|
|
PlantDesc = currPlant.PlantDesc,
|
|
LevelAct = simLevel,
|
|
LevelMax = currPlant.LevelMax,
|
|
LevelTS = SimLevelTS
|
|
};
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |