From 1e24679bf65e42e5df96113bd1eef371036f0940 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Wed, 24 Oct 2018 16:18:39 +0300 Subject: Implemented job resume!! --- .../Tango.Integration/Operation/MachineOperator.cs | 141 +++++++++++++++++++++ 1 file changed, 141 insertions(+) (limited to 'Software/Visual_Studio/Tango.Integration/Operation/MachineOperator.cs') diff --git a/Software/Visual_Studio/Tango.Integration/Operation/MachineOperator.cs b/Software/Visual_Studio/Tango.Integration/Operation/MachineOperator.cs index 8098f755d..d098d9bb9 100644 --- a/Software/Visual_Studio/Tango.Integration/Operation/MachineOperator.cs +++ b/Software/Visual_Studio/Tango.Integration/Operation/MachineOperator.cs @@ -70,6 +70,7 @@ namespace Tango.Integration.Operation DeviceInformation = new DeviceInformation(); MachineEventsStateProvider = new DefaultMachineEventsStateProvider(); EnableEventsNotification = true; + EnableJobResume = true; LogEmbeddedDebuggingToFile = true; } @@ -146,6 +147,11 @@ namespace Tango.Integration.Operation /// public event EventHandler PrintingAborted; + /// + /// Occurs when the machine operator has detected that a job is in progress after connecting to the machine. + /// + public event EventHandler ResumingJob; + #endregion #region Properties @@ -279,6 +285,22 @@ namespace Tango.Integration.Operation } } + private bool _enableJobResume; + /// + /// Gets or sets a value indicating whether to check whether a job is in progress after connection was successful. + /// + public bool EnableJobResume + { + get + { + return _enableJobResume; + } + set + { + _enableJobResume = value; RaisePropertyChangedAuto(); + } + } + private bool _logEmbeddedDebuggingToFile; /// /// Gets or sets a value indicating whether to automatically save incoming log data from the embedded device. @@ -673,6 +695,11 @@ namespace Tango.Integration.Operation OnEnableDiagnosticsChanged(EnableDiagnostics); OnEnableEmbeddedDebuggingChanged(EnableEmbeddedDebugging); OnEnableEventsNotification(EnableEventsNotification); + + if (EnableJobResume) + { + ResumeJob(); + } } catch (Exception ex) { @@ -796,6 +823,7 @@ namespace Tango.Integration.Operation } JobTicket ticket = new JobTicket(); + ticket.Guid = originalJob.Guid; ticket.EnableInterSegment = job.EnableInterSegment; ticket.InterSegmentLength = job.InterSegmentLength; ticket.Length = job.Length; @@ -1345,6 +1373,119 @@ namespace Tango.Integration.Operation #region Private Methods + private async void ResumeJob() + { + LogManager.Log("Checking is a job is in progress..."); + var res = await SendRequest(new CurrentJobRequest()); + if (res.Message.IsJobInProgress) + { + JobTicket jobTicket = res.Message.JobTicket; + + ProcessParametersTable processParameters = new ProcessParametersTable(); + jobTicket.ProcessParameters.MapPrimitivesTo(processParameters); + + ResumingJobEventArgs args = new ResumingJobEventArgs((job) => + { + if (Status != MachineStatuses.ReadyToDye) + { + throw new InvalidOperationException("Could not print while status = " + Status); + } + + RunningJob = null; + RunningJobStatus = null; + + var originalJob = job; + + CurrentProcessParameters = processParameters; + + if (job.NumberOfUnits < 1) + { + job.NumberOfUnits = 1; + } + + job = job.Clone(); + + var segments = job.Segments.ToList(); + + for (int i = 0; i < job.NumberOfUnits - 1; i++) + { + foreach (var s in segments) + { + job.Segments.Add(s); + } + } + + var request = new ResumeCurrentJobRequest(); + + JobHandler handler = null; + + handler = new JobHandler(async () => + { + try + { + var result = await SendRequest(new AbortJobRequest()); + PrintingAborted?.Invoke(this, new PrintingEventArgs(handler, originalJob)); + handler.RaiseCanceled(); + } + catch (Exception ex) + { + LogManager.Log(ex, "Failed to cancel job."); + } + }, originalJob, processParameters, JobHandlingMode); + + handler.StatusChanged += (x, s) => + { + RunningJobStatus = s; + }; + + LogRequestSent(request); + + bool responseLogged = false; + + SendContinuousRequest(request, null, TimeSpan.FromSeconds(2)).Subscribe((response) => + { + handler.RaiseStatusReceived(response.Message.Status); + + if (!responseLogged) + { + responseLogged = true; + Status = MachineStatuses.Printing; + RunningJob = originalJob; + PrintingStarted?.Invoke(this, new PrintingEventArgs(handler, originalJob)); + LogResponseReceived(response.Message); + } + }, (ex) => + { + if (!(ex is ContinuousResponseAbortedException)) + { + Status = MachineStatuses.ReadyToDye; + + if (!handler.IsCanceled) + { + PrintingFailed?.Invoke(this, new PrintingFailedEventArgs(handler, originalJob, ex)); + handler.RaiseFailed(ex); + LogRequestFailed(request, ex); + } + } + else + { + Status = MachineStatuses.ReadyToDye; + } + }, () => + { + Status = MachineStatuses.ReadyToDye; + PrintingCompleted?.Invoke(this, new PrintingEventArgs(handler, originalJob)); + handler.RaiseCompleted(); + }); + + return handler; + }); + + args.JobGuid = jobTicket.Guid; + ResumingJob?.Invoke(this, args); + } + } + /// /// Logs the request sent. /// -- cgit v1.3.1