From ca72de8f6669d89e33eb86652ec3aabbffa44643 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Sat, 1 Aug 2020 23:07:50 +0300 Subject: Added head cleaning jobs to job runs as "IS_HEAD_CLEANING" --- .../JobRuns/BasicJobRunsLogger.cs | 92 +++++++++++++++++++++- .../Tango.Integration/JobRuns/IJobRunsLogger.cs | 7 ++ .../Operation/HeadCleaningEndedEventArgs.cs | 24 ++++++ .../Operation/IMachineOperator.cs | 5 ++ .../Tango.Integration/Operation/MachineOperator.cs | 72 ++++++++++++++--- .../Tango.Integration/Tango.Integration.csproj | 3 +- 6 files changed, 191 insertions(+), 12 deletions(-) create mode 100644 Software/Visual_Studio/Tango.Integration/Operation/HeadCleaningEndedEventArgs.cs (limited to 'Software/Visual_Studio/Tango.Integration') diff --git a/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs b/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs index e7308dfc7..64ad60db5 100644 --- a/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs +++ b/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs @@ -19,6 +19,7 @@ namespace Tango.Integration.JobRuns public class BasicJobRunsLogger : ExtendedObject, IJobRunsLogger { private Job _job; + private Machine _defaultMachine; #region Properties @@ -74,6 +75,7 @@ namespace Tango.Integration.JobRuns MachineOperator.PrintingAborted += Machine_PrintingAborted; MachineOperator.PrintingFailed -= Machine_PrintingFailed; MachineOperator.PrintingFailed += Machine_PrintingFailed; + MachineOperator.HeadCleaningEnded += MachineOperator_HeadCleaningEnded; } private bool ShouldLog() @@ -168,13 +170,87 @@ namespace Tango.Integration.JobRuns } catch (Exception ex) { - LogManager.Log(ex, "Error logging the current job run to the database."); + LogManager.Log(ex, "Error logging the last job run to the database."); } }); } } } + private void InsertHeadCleaningJobRun(HeadCleaningEndedEventArgs e) + { + if (IsStarted && _defaultMachine != null) + { + Task.Factory.StartNew(() => + { + try + { + using (var db = ObservablesContext.CreateDefault()) + { + JobRun run = new JobRun(); + + run.IsHeadCleaning = true; + run.StartDate = e.StartDate; + run.UploadingStartDate = e.StartDate; + run.HeatingStartDate = e.StartDate; + run.ActualStartDate = e.StartDate; + run.EndDate = DateTime.UtcNow; + run.JobName = "HEAD CLEANING"; + run.Source = JobSource; + run.MachineGuid = _defaultMachine.Guid; + run.JobRunStatus = e.Status; + run.EndPosition = e.EndPosition; + run.JobLength = e.Length; + run.LiquidQuantities = e.LiquidQuantities; + + //Set individual liquid quantities + + //Cyan + var cyan = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Cyan); + run.CyanQuantity = cyan != null ? cyan.Quantity : 0; + + //Magenta + var magenta = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Magenta); + run.MagentaQuantity = magenta != null ? magenta.Quantity : 0; + + //Yellow + var yellow = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Yellow); + run.YellowQuantity = yellow != null ? yellow.Quantity : 0; + + //Black + var black = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Black); + run.BlackQuantity = black != null ? black.Quantity : 0; + + //TI + var ti = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.TransparentInk); + run.TransparentQuantity = ti != null ? ti.Quantity : 0; + + //Lubricant + var lubricant = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Lubricant); + run.LubricantQuantity = lubricant != null ? lubricant.Quantity : 0; + + //Cleaner + var cleaner = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Cleaner); + run.CleanerQuantity = cleaner != null ? cleaner.Quantity : 0; + + //if (exception != null) + //{ + // run.FailedMessage = exception.FlattenMessage(); + //} + + db.JobRuns.Add(run); + + db.SaveChanges(); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error logging the last head cleaning job run to the database."); + } + }); + } + } + #endregion #region Public Methods @@ -195,6 +271,15 @@ namespace Tango.Integration.JobRuns IsStarted = false; } + /// + /// Sets the head cleaning parameters. + /// + /// The machine. + public void SetDefaultMachine(Machine machine) + { + _defaultMachine = machine; + } + #endregion #region Event Handlers @@ -234,6 +319,11 @@ namespace Tango.Integration.JobRuns } } + private void MachineOperator_HeadCleaningEnded(object sender, HeadCleaningEndedEventArgs e) + { + InsertHeadCleaningJobRun(e); + } + #endregion } } diff --git a/Software/Visual_Studio/Tango.Integration/JobRuns/IJobRunsLogger.cs b/Software/Visual_Studio/Tango.Integration/JobRuns/IJobRunsLogger.cs index a5242c1a4..386298bb9 100644 --- a/Software/Visual_Studio/Tango.Integration/JobRuns/IJobRunsLogger.cs +++ b/Software/Visual_Studio/Tango.Integration/JobRuns/IJobRunsLogger.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tango.BL.Entities; using Tango.BL.Enumerations; using Tango.Integration.Operation; @@ -42,5 +43,11 @@ namespace Tango.Integration.JobRuns /// Stops the logger. /// void Stop(); + + /// + /// Sets the head cleaning parameters. + /// + /// The machine. + void SetDefaultMachine(Machine machine); } } diff --git a/Software/Visual_Studio/Tango.Integration/Operation/HeadCleaningEndedEventArgs.cs b/Software/Visual_Studio/Tango.Integration/Operation/HeadCleaningEndedEventArgs.cs new file mode 100644 index 000000000..0929c1254 --- /dev/null +++ b/Software/Visual_Studio/Tango.Integration/Operation/HeadCleaningEndedEventArgs.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL.Enumerations; +using Tango.BL.ValueObjects; + +namespace Tango.Integration.Operation +{ + public class HeadCleaningEndedEventArgs : EventArgs + { + public List LiquidQuantities { get; set; } + public DateTime StartDate { get; set; } + public double EndPosition { get; set; } + public double Length { get; set; } + public JobRunStatus Status { get; set; } + + public HeadCleaningEndedEventArgs() + { + LiquidQuantities = new List(); + } + } +} diff --git a/Software/Visual_Studio/Tango.Integration/Operation/IMachineOperator.cs b/Software/Visual_Studio/Tango.Integration/Operation/IMachineOperator.cs index d006848de..bee4a7523 100644 --- a/Software/Visual_Studio/Tango.Integration/Operation/IMachineOperator.cs +++ b/Software/Visual_Studio/Tango.Integration/Operation/IMachineOperator.cs @@ -240,6 +240,11 @@ namespace Tango.Integration.Operation /// event EventHandler ThreadLoadingFailed; + /// + /// Occurs when a head cleaning job has ended. + /// + event EventHandler HeadCleaningEnded; + /// /// Gets or sets a value indicating whether direct the embedded device to send diagnostics messages. /// diff --git a/Software/Visual_Studio/Tango.Integration/Operation/MachineOperator.cs b/Software/Visual_Studio/Tango.Integration/Operation/MachineOperator.cs index 0ae2086d3..57613aae2 100644 --- a/Software/Visual_Studio/Tango.Integration/Operation/MachineOperator.cs +++ b/Software/Visual_Studio/Tango.Integration/Operation/MachineOperator.cs @@ -74,9 +74,10 @@ namespace Tango.Integration.Operation private static RunningJobStatus _last_job_status; private bool _isPowerDownRequestInProgress; private bool _isHeadCleaningInProgress; - private List _currentJobLiquidQuantities; + private List _lastJobLiquidQuantities; private DateTime _diagnosticsTime; private MachineStatus _machineStatusBeforeJobStart; + private Configuration _machineConfiguration; private DateTime _jobStartDate; private DateTime? _jobUploadingStartDate; @@ -271,6 +272,11 @@ namespace Tango.Integration.Operation /// public event EventHandler PowerUpEnded; + /// + /// Occurs when a head cleaning job has ended. + /// + public event EventHandler HeadCleaningEnded; + #endregion #region Properties @@ -1115,7 +1121,7 @@ namespace Tango.Integration.Operation { PrintingCompleted?.Invoke(this, new PrintingEventArgs(handler, job) { - LiquidQuantities = _currentJobLiquidQuantities.ToList(), + LiquidQuantities = _lastJobLiquidQuantities.ToList(), StartDate = _jobStartDate, UploadingStartTime = _jobUploadingStartDate, HeatingStartTime = _jobHeatingStartDate, @@ -1135,7 +1141,7 @@ namespace Tango.Integration.Operation { PrintingFailed?.Invoke(this, new PrintingFailedEventArgs(handler, job, exception) { - LiquidQuantities = _currentJobLiquidQuantities.ToList(), + LiquidQuantities = _lastJobLiquidQuantities.ToList(), StartDate = _jobStartDate, UploadingStartTime = _jobUploadingStartDate, HeatingStartTime = _jobHeatingStartDate, @@ -1153,7 +1159,7 @@ namespace Tango.Integration.Operation { PrintingAborted?.Invoke(this, new PrintingEventArgs(handler, job) { - LiquidQuantities = _currentJobLiquidQuantities.ToList(), + LiquidQuantities = _lastJobLiquidQuantities.ToList(), StartDate = _jobStartDate, UploadingStartTime = _jobUploadingStartDate, HeatingStartTime = _jobHeatingStartDate, @@ -1171,7 +1177,7 @@ namespace Tango.Integration.Operation { PrintingEnded?.Invoke(this, new PrintingEventArgs(handler, job) { - LiquidQuantities = _currentJobLiquidQuantities.ToList(), + LiquidQuantities = _lastJobLiquidQuantities.ToList(), StartDate = _jobStartDate, UploadingStartTime = _jobUploadingStartDate, HeatingStartTime = _jobHeatingStartDate, @@ -1179,6 +1185,20 @@ namespace Tango.Integration.Operation }); } + protected virtual void OnHeadCleaningEnded(HeadCleaningHandler handler, JobRunStatus status) + { + SaveLastJobLiquidQuantities(null, null, null, null); + + HeadCleaningEnded?.Invoke(this, new HeadCleaningEndedEventArgs() + { + StartDate = _jobStartDate, + Length = handler.Status.Total, + EndPosition = handler.Status.Progress, + Status = status, + LiquidQuantities = _lastJobLiquidQuantities.ToList(), + }); + } + #endregion #region Override Methods @@ -1893,9 +1913,14 @@ namespace Tango.Integration.Operation /// The handler. private void SaveLastJobLiquidQuantities(Job job, Configuration configuration, ProcessParametersTable processParameters, JobHandler handler) { + if (configuration == null) + { + configuration = _machineConfiguration; + } + try { - _currentJobLiquidQuantities = new List(); + _lastJobLiquidQuantities = new List(); if (JobLiquidQuantityCalculationMode == JobLiquidQuantityCalculationMode.MachineStatus) { @@ -1906,7 +1931,7 @@ namespace Tango.Integration.Operation if (packLevelAfter != null && packLevelBefore != null) { - _currentJobLiquidQuantities.Add(new BL.ValueObjects.JobRunLiquidQuantity() + _lastJobLiquidQuantities.Add(new BL.ValueObjects.JobRunLiquidQuantity() { LiquidType = pack.LiquidType.Type, Quantity = packLevelBefore.DispenserLevel - packLevelAfter.DispenserLevel, @@ -1916,7 +1941,7 @@ namespace Tango.Integration.Operation } else { - _currentJobLiquidQuantities = CreateJobRunLiquidQuantities(job, configuration, processParameters, handler.Status.Progress, handler.Status.TotalProgress); + _lastJobLiquidQuantities = CreateJobRunLiquidQuantities(job, configuration, processParameters, handler.Status.Progress, handler.Status.TotalProgress); } } catch (Exception ex) @@ -2319,7 +2344,7 @@ namespace Tango.Integration.Operation LogManager.Log($"Executing job '{job.Name}'..."); - _currentJobLiquidQuantities = new List(); + _lastJobLiquidQuantities = new List(); _jobUploadingStartDate = null; _jobHeatingStartDate = null; _jobActualStartDate = null; @@ -2906,6 +2931,8 @@ namespace Tango.Integration.Operation /// public async Task UploadHardwareConfiguration(HardwareVersion hardwareVersion, Configuration configuration) { + _machineConfiguration = configuration; + try { hardwareVersion = configuration.GetHardwareConfiguration().Merge(hardwareVersion); @@ -3745,11 +3772,20 @@ namespace Tango.Integration.Operation } _isHeadCleaningInProgress = true; + bool _completed = false; - HeadCleaningHandler handler = new HeadCleaningHandler(() => + HeadCleaningHandler handler = null; + handler = new HeadCleaningHandler(() => { _isHeadCleaningInProgress = false; Thread.Sleep(1000); + + if (!_completed) + { + _completed = true; + OnHeadCleaningEnded(handler, JobRunStatus.Aborted); + } + var r = SendRequest(new AbortHeadCleaningRequest(), new TransportRequestConfig() { ShouldLog = true }).Result; }); @@ -3757,7 +3793,11 @@ namespace Tango.Integration.Operation { Thread.Sleep(100); + _lastJobLiquidQuantities = new List(); + _machineStatusBeforeJobStart = MachineStatus.Clone(); + bool firstResponse = true; + _jobStartDate = DateTime.UtcNow; SendContinuousRequest(new StartHeadCleaningRequest(), new TransportContinuousRequestConfig() { ContinuousTimeout = TimeSpan.FromSeconds(5), ShouldLog = true }).ObserveOn(new NewThreadScheduler()).Subscribe((response) => { @@ -3775,10 +3815,22 @@ namespace Tango.Integration.Operation LogManager.Log(ex, "Head cleaning error."); handler.RaiseFailed(ex); } + + if (!_completed) + { + _completed = true; + OnHeadCleaningEnded(handler, JobRunStatus.Failed); + } }, () => { _isHeadCleaningInProgress = false; handler.RaiseCompleted(); + + if (!_completed) + { + _completed = true; + OnHeadCleaningEnded(handler, JobRunStatus.Completed); + } }); }); diff --git a/Software/Visual_Studio/Tango.Integration/Tango.Integration.csproj b/Software/Visual_Studio/Tango.Integration/Tango.Integration.csproj index 91edb951e..7efd29d0a 100644 --- a/Software/Visual_Studio/Tango.Integration/Tango.Integration.csproj +++ b/Software/Visual_Studio/Tango.Integration/Tango.Integration.csproj @@ -117,6 +117,7 @@ + @@ -219,7 +220,7 @@ - + \ No newline at end of file -- cgit v1.3.1