From 78ba28666b686329661f4a8fded423af94bbfb84 Mon Sep 17 00:00:00 2001 From: Roy Date: Sun, 3 Sep 2023 17:35:54 +0300 Subject: Job Resume Manager. --- .../PPC/Tango.PPC.Common/PPCViewModel.cs | 4 + .../Resume/DefaultJobResumeManager.cs | 103 +++++++++++++++++++++ .../Tango.PPC.Common/Resume/IJobResumeManager.cs | 20 ++++ .../PPC/Tango.PPC.Common/Resume/JobResumeDB.cs | 83 +++++++++++++++++ .../Resume/JobResumeDroppedEventArgs.cs | 13 +++ .../PPC/Tango.PPC.Common/Resume/JobResumeModel.cs | 21 +++++ .../Resume/JobResumeUpdatedEventArgs.cs | 15 +++ .../PPC/Tango.PPC.Common/Tango.PPC.Common.csproj | 10 ++ .../PPC/Tango.PPC.Common/packages.config | 1 + 9 files changed, 270 insertions(+) create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/DefaultJobResumeManager.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/IJobResumeManager.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeDB.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeDroppedEventArgs.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeModel.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeUpdatedEventArgs.cs (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs index b54289813..7064a48e1 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs @@ -20,6 +20,7 @@ using Tango.PPC.Common.Printing; using Tango.PPC.Common.RemoteAssistance; using Tango.PPC.Common.RemoteDesktop; using Tango.PPC.Common.RemoteJobUpload; +using Tango.PPC.Common.Resume; using Tango.PPC.Common.Storage; using Tango.PPC.Common.Synchronization; using Tango.PPC.Common.ThreadLoading; @@ -145,6 +146,9 @@ namespace Tango.PPC.Common [TangoInject] public IBuildProvider BuildProvider { get; set; } + [TangoInject] + public IJobResumeManager JobResumeManager { get; set; } + private PPCSettings _settings; /// /// Gets the main PPC settings. diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/DefaultJobResumeManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/DefaultJobResumeManager.cs new file mode 100644 index 000000000..5f819d34d --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/DefaultJobResumeManager.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core; +using Tango.Core.DI; +using Tango.Integration.Operation; +using Tango.PPC.Common.Build; +using Tango.PPC.Common.Connection; + +namespace Tango.PPC.Common.Resume +{ + public class DefaultJobResumeManager : ExtendedObject, IJobResumeManager + { + private IMachineProvider _machineProvider; + private IBuildProvider _buildProvider; + + public event EventHandler JobResumeUpdated; + public event EventHandler JobResumeDropped; + + [TangoInject] + public DefaultJobResumeManager(IMachineProvider machineProvider,IBuildProvider buildProvider) + { + _machineProvider = machineProvider; + _buildProvider = buildProvider; + machineProvider.MachineOperator.PrintingEnded += MachineOperator_PrintingEnded; + } + + private void MachineOperator_PrintingEnded(object sender, PrintingEventArgs e) + { + if (_buildProvider.IsEureka) + { + UpdateJobResume(e); + } + } + + private void UpdateJobResume(PrintingEventArgs e) + { + try + { + if (!e.JobHandler.Status.IsCompleted) + { + if (e.JobHandler.JobStatus.Progress <= e.JobHandler.ProcessParameters.DryerBufferLengthMeters) return; + + var model = JobResumeDB.Default.Get(e.Job.Guid); + bool insert = false; + if (model == null) + { + model = new JobResumeModel(); + insert = true; + } + + model.JobGuid = e.Job.Guid; + model.FirstUnitStartPosition = e.JobHandler.Status.CurrentUnitProgress; + model.RemainingUnits = e.JobHandler.Status.RemainingUnits; + model.GlobalStartPosition = e.JobHandler.JobStatus.Progress; + + if (insert) + { + JobResumeDB.Default.Add(model); + } + else + { + JobResumeDB.Default.Update(model); + } + + JobResumeUpdated?.Invoke(this, new JobResumeUpdatedEventArgs() + { + JobGuid = e.Job.Guid, + ResumeModel = model + }); + } + else + { + JobResumeDB.Default.Delete(e.Job.Guid); + + JobResumeUpdated?.Invoke(this, new JobResumeUpdatedEventArgs() + { + JobGuid = e.Job.Guid, + ResumeModel = null + }); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error inserting/updating job resume info on db."); + } + } + + public JobResumeModel GetJobResumeModel(string jobGuid) + { + return JobResumeDB.Default.Get(jobGuid); + } + + public void DropResume(string jobGuid) + { + JobResumeDB.Default.Delete(jobGuid); + + JobResumeDropped?.Invoke(this, new JobResumeDroppedEventArgs() { JobGuid = jobGuid }); + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/IJobResumeManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/IJobResumeManager.cs new file mode 100644 index 000000000..002563fc4 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/IJobResumeManager.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Integration.Operation; + +namespace Tango.PPC.Common.Resume +{ + public interface IJobResumeManager + { + event EventHandler JobResumeUpdated; + + event EventHandler JobResumeDropped; + + void DropResume(String jobGuid); + + JobResumeModel GetJobResumeModel(String jobGuid); + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeDB.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeDB.cs new file mode 100644 index 000000000..904e37b2e --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeDB.cs @@ -0,0 +1,83 @@ +using LiteDB; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.Resume +{ + public class JobResumeDB : IDisposable + { + private LiteDatabase _db; + private ILiteCollection _collection; + + private static Lazy _default = new Lazy(() => new JobResumeDB()); + + public static JobResumeDB Default + { + get + { + return _default.Value; + } + } + + private JobResumeDB() + { + Init(); + } + + private void Init() + { + String dbFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Job Resume v2"); + Directory.CreateDirectory(dbFolder); + _db = new LiteDatabase($"Filename={Path.Combine(dbFolder, "job_resume.db")};connection=shared"); + _collection = _db.GetCollection("JobResume"); + } + + public void Update(JobResumeModel model) + { + _collection.Update(model); + } + + public void Add(JobResumeModel model) + { + _collection.Insert(model); + } + + public List GetAll() + { + return _collection.FindAll().ToList(); + } + + public JobResumeModel Get(String jobGuild) + { + return _collection.FindOne(x => x.JobGuid == jobGuild); + } + + public void Delete(JobResumeModel model) + { + _collection.Delete(model.JobGuid); + } + + public void Delete(String jobGuid) + { + _collection.Delete(jobGuid); + } + + ~JobResumeDB() + { + Dispose(); + } + + public void Dispose() + { + try + { + _db?.Dispose(); + } + catch { } + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeDroppedEventArgs.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeDroppedEventArgs.cs new file mode 100644 index 000000000..674087063 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeDroppedEventArgs.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.Resume +{ + public class JobResumeDroppedEventArgs : EventArgs + { + public String JobGuid { get; set; } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeModel.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeModel.cs new file mode 100644 index 000000000..40386566e --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeModel.cs @@ -0,0 +1,21 @@ +using LiteDB; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.Resume +{ + public class JobResumeModel + { + [BsonId] + public String JobGuid { get; set; } + + public int RemainingUnits { get; set; } + + public double GlobalStartPosition { get; set; } + + public double FirstUnitStartPosition { get; set; } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeUpdatedEventArgs.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeUpdatedEventArgs.cs new file mode 100644 index 000000000..037654370 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/JobResumeUpdatedEventArgs.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.Resume +{ + public class JobResumeUpdatedEventArgs : EventArgs + { + public JobResumeModel ResumeModel { get; set; } + + public String JobGuid { get; set; } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj index e598c6fe4..28cbfa6bc 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Tango.PPC.Common.csproj @@ -88,6 +88,9 @@ ..\..\packages\Ionic.Zip.1.9.1.8\lib\Ionic.Zip.dll + + ..\..\packages\LiteDB.5.0.4\lib\net45\LiteDB.dll + @@ -121,6 +124,7 @@ + @@ -221,6 +225,12 @@ + + + + + + diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/packages.config b/Software/Visual_Studio/PPC/Tango.PPC.Common/packages.config index adc33d349..cba40d441 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/packages.config +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/packages.config @@ -6,6 +6,7 @@ + -- cgit v1.3.1