diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-07-09 15:27:55 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-07-09 15:27:55 +0300 |
| commit | 4147c287ad90a05eae551d4ccfdccc707bebd86f (patch) | |
| tree | 24acd52ab1240dc34b04db280b9c7439c760231a /Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs | |
| parent | ca96477441518c8300474a317e34cb5e7e1550fc (diff) | |
| download | Tango-4147c287ad90a05eae551d4ccfdccc707bebd86f.tar.gz Tango-4147c287ad90a05eae551d4ccfdccc707bebd86f.zip | |
Refactored job progress handling !!!
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs | 127 |
1 files changed, 122 insertions, 5 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs b/Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs index 2a9441ad6..0fcd070b3 100644 --- a/Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs +++ b/Software/Visual_Studio/Tango.Integration/Operation/JobHandler.cs @@ -3,23 +3,27 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tango.BL.Entities; +using Tango.Core; using Tango.PMR.Printing; namespace Tango.Integration.Operation { /// <summary> - /// Represents a <see cref="MachineOperator.Print(Observables.Job, Observables.ProcessParametersTable)"/> handler. + /// Represents a <see cref="MachineOperator"/> job handler. /// </summary> - public class JobHandler + /// <seealso cref="Tango.Core.ExtendedObject" /> + public class JobHandler : ExtendedObject { private Action _cancelAction; + private List<Segment> _effectiveSegments; #region Events /// <summary> /// Occurs when a job status has been received. /// </summary> - public event EventHandler<JobStatus> StatusReceived; + public event EventHandler<RunningJobStatus> StatusChanged; /// <summary> /// Occurs when the job has failed. @@ -36,6 +40,16 @@ namespace Tango.Integration.Operation /// </summary> public event EventHandler Canceled; + /// <summary> + /// Occurs when a segment has started. + /// </summary> + public event EventHandler<Segment> SegmentStarted; + + /// <summary> + /// Occurs when a segment has completed. + /// </summary> + public event EventHandler<Segment> SegmentCompleted; + #endregion #region Properties @@ -45,6 +59,27 @@ namespace Tango.Integration.Operation /// </summary> public bool IsCanceled { get; internal set; } + /// <summary> + /// Gets the process parameters. + /// </summary> + public ProcessParametersTable ProcessParameters { get; private set; } + + private RunningJobStatus _Status; + /// <summary> + /// Gets the current status. + /// </summary> + public RunningJobStatus Status + { + get { return _Status; } + private set { _Status = value; RaisePropertyChangedAuto(); } + } + + /// <summary> + /// Gets the job. + /// </summary> + public Job Job { get; private set; } + + #endregion #region Constructors @@ -61,9 +96,29 @@ namespace Tango.Integration.Operation /// Initializes a new instance of the <see cref="JobHandler"/> class. /// </summary> /// <param name="cancelAction">The cancel action.</param> - internal JobHandler(Action cancelAction) : this() + internal JobHandler(Action cancelAction, Job job, ProcessParametersTable processParameters) : this() { + ProcessParameters = processParameters; + Job = job; + + foreach (var s in Job.Segments) + { + s.Started = false; + s.Completed = false; + s.RemainingTime = TimeSpan.FromSeconds(0); + } + + _effectiveSegments = Job.GetEffectiveSegment(); + _cancelAction = () => { IsCanceled = true; cancelAction(); }; + + Status = new RunningJobStatus(); + + Status.TotalTime = job.GetEstimatedDuration(processParameters); + Status.TotalProgress = Job.Length; + Status.Progress = 0; + Status.RemainingTime = Status.TotalTime; + Status.RemainingProgress = Status.TotalProgress; } #endregion @@ -76,7 +131,7 @@ namespace Tango.Integration.Operation /// <param name="status">The status.</param> internal void RaiseStatusReceived(JobStatus status) { - StatusReceived?.Invoke(this, status); + InvalidateJobProgress(status); } /// <summary> @@ -106,6 +161,68 @@ namespace Tango.Integration.Operation #endregion + #region Private Methods + + private void InvalidateJobProgress(JobStatus s) + { + //Job Status + if (IsCanceled) + { + Status.IsCanceled = IsCanceled; + StatusChanged?.Invoke(this, Status); + return; + } + + Status.Progress = s.Progress; + Status.RemainingTime = Status.TotalTime - Job.TranslateProgressToTime(Status.Progress, ProcessParameters); + Status.RemainingProgress = Status.TotalProgress - Status.Progress; + Status.Message = s.Message; + StatusChanged?.Invoke(this, Status); + + //Segments Completion + for (int i = 0; i < _effectiveSegments.Count; i++) + { + Segment segment = _effectiveSegments[i]; + double previousSegmentsLengthWithThis = _effectiveSegments.Take(i + 1).Sum(x => x.Length); + TimeSpan segmentsDuration = Job.TranslateProgressToTime(previousSegmentsLengthWithThis, ProcessParameters); + TimeSpan segmentRemainingTime = segmentsDuration - Job.TranslateProgressToTime(Status.Progress, ProcessParameters); + + if (i == 0 && Status.Progress > 0) + { + if (!segment.Started) + { + segment.Started = true; + SegmentStarted?.Invoke(this, segment); + } + } + + if (Status.Progress >= previousSegmentsLengthWithThis) + { + if (!segment.Completed) + { + segment.Completed = true; + SegmentCompleted?.Invoke(this, segment); + } + + if (i < _effectiveSegments.Count - 1) + { + if (!_effectiveSegments[i + 1].Started) + { + _effectiveSegments[i + 1].Started = true; + SegmentStarted?.Invoke(this, _effectiveSegments[i + 1]); + } + } + } + + if (segment.Started && !segment.Completed) + { + segment.RemainingTime = segmentRemainingTime; + } + } + } + + #endregion + #region Public Methods /// <summary> |
