Files
cms_thermo_active/Step.Database/Controllers/QueueController.cs
T
2018-09-18 17:07:18 +02:00

82 lines
2.6 KiB
C#

using Step.Model.DatabaseModels;
using Step.Model.DTOModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Step.Model.Constants;
namespace Step.Database.Controllers
{
public class QueueController : IDisposable
{
private DatabaseContext dbCtx;
public static Dictionary<int, List<DTOQueueModel>> PartProgramQueue = new Dictionary<int, List<DTOQueueModel>>();
public static Dictionary<int, int> QueueRunningIndexes = new Dictionary<int, int>();
public QueueController()
{
// Initialize database context
dbCtx = new DatabaseContext();
}
public void Dispose()
{
// Clear database context
dbCtx.Dispose();
}
public void UpdateQueue()
{
dbCtx.Queue.RemoveRange(dbCtx.Queue);
foreach(var item in PartProgramQueue)
{
// Create database model
var dbRows = item.Value.Select(x => new QueueItemsModel()
{
Id = x.Id,
AbsolutePath = x.AbsolutePath,
PartProgramName = x.PartProgramName,
Process = item.Key, // Process
Reps = x.Reps,
RemainingReps = x.RemainingReps,
Status = (int)x.Status
}).ToList();
// Add to db
dbCtx.Queue.AddRange(dbRows);
}
dbCtx.SaveChanges();
}
public void ReadAndPopulateQueue()
{
var dbQueue = dbCtx.Queue.ToList();
foreach(var entity in dbQueue)
{
// Check if process queue exists
if (!PartProgramQueue.ContainsKey(entity.Process))
PartProgramQueue.Add(entity.Process, new List<DTOQueueModel>());
// Add db row to queue
PartProgramQueue[entity.Process].Add(new DTOQueueModel()
{
Id = entity.Id,
AbsolutePath = entity.AbsolutePath,
PartProgramName = entity.PartProgramName,
Reps = entity.Reps,
RemainingReps = entity.RemainingReps,
Status = (QUEUE_ITEM_STATUS)entity.Status
});
if((QUEUE_ITEM_STATUS)entity.Status == QUEUE_ITEM_STATUS.RUNNING)
QueueRunningIndexes[entity.Process] = entity.Id - 1;
}
}
}
}