125 lines
3.9 KiB
C#
125 lines
3.9 KiB
C#
using Termo.Active.Model.DatabaseModels;
|
|
using Termo.Active.Model.DTOModels;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static Termo.Active.Model.Constants;
|
|
|
|
namespace Termo.Active.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);
|
|
|
|
var dbRows = new List<QueueItemsModel>();
|
|
foreach (var item in PartProgramQueue)
|
|
{
|
|
// Create database model
|
|
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 UpdateReps(int processId, int id, int reps)
|
|
{
|
|
QueueItemsModel item = dbCtx.Queue.FirstOrDefault(x => x.Id == id && x.Process == processId);
|
|
|
|
if (item == null)
|
|
return;
|
|
|
|
// Update reps
|
|
item.Reps = reps;
|
|
item.RemainingReps = reps;
|
|
|
|
dbCtx.SaveChanges();
|
|
|
|
}
|
|
|
|
public void DeleteItem(int processId, int id)
|
|
{
|
|
QueueItemsModel item = dbCtx.Queue.FirstOrDefault(x => x.Id == id && x.Process == processId);
|
|
|
|
if(item == null)
|
|
return;
|
|
|
|
dbCtx.Queue.Remove(item);
|
|
|
|
dbCtx.SaveChanges();
|
|
}
|
|
|
|
public void ReadAndPopulateQueue()
|
|
{
|
|
var dbQueue = dbCtx.Queue.ToList();
|
|
bool foundData = false;
|
|
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.FINISHED && !foundData)
|
|
{
|
|
QueueRunningIndexes[entity.Process] = entity.Id - 1;
|
|
foundData = true;
|
|
}
|
|
|
|
if ((QUEUE_ITEM_STATUS)entity.Status == QUEUE_ITEM_STATUS.RUNNING)
|
|
QueueRunningIndexes[entity.Process] = entity.Id - 1;
|
|
}
|
|
}
|
|
|
|
public void UpdateQueueIdsAndSave(int processId)
|
|
{
|
|
// Fix new ids
|
|
for (int i = 0; i < PartProgramQueue[processId].Count(); i++)
|
|
PartProgramQueue[processId][i].Id = i + 1;
|
|
|
|
UpdateQueue();
|
|
}
|
|
}
|
|
}
|