using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.BL.DTO; using Tango.Core; using Tango.Core.DI; using Tango.Integration.ExternalBridge; using Tango.Integration.Operation; using Tango.PPC.Common.Build; using Tango.PPC.Common.Connection; using Tango.PPC.Common.ExternalBridge; using Tango.PPC.Shared.Jobs; namespace Tango.PPC.Common.RemoteJob { [TangoCreateWhenRegistered] public class DefaultRemoteJobService : IRemoteJobService, IExternalBridgeRequestHandler { private class RunningJobUpdateClient { public String Token { get; set; } public ExternalBridgeReceiver Receiver { get; set; } public bool JobSent { get; set; } } private List _clients; private JobHandler _handler; private JobDTO _currentJobDTO; private String _rmlName; private ProcessParametersTableDTO _currentJobProcessParameters; private IRemoteJobInputOutputProvider _inputOutputProvider; private IBuildProvider _buildProvider; public bool Enabled { get; set; } = true; private IMachineProvider MachineProvider { get; set; } public DefaultRemoteJobService(IPPCExternalBridgeService externalBridge, IMachineProvider machineProvider, IBuildProvider buildProvider) { externalBridge.RegisterRequestHandler(this); MachineProvider = machineProvider; _buildProvider = buildProvider; _clients = new List(); MachineProvider.MachineOperator.PrintingStarted += MachineOperator_PrintingStarted; } private async void MachineOperator_PrintingStarted(object sender, Integration.Operation.PrintingEventArgs e) { _handler = e.JobHandler; e.JobHandler.StatusChanged += JobHandler_StatusChanged; e.JobHandler.Stopped += JobHandler_Stopped; _currentJobDTO = JobDTO.FromObservable(e.Job); _currentJobProcessParameters = ProcessParametersTableDTO.FromObservable(_handler.ProcessParameters); _rmlName = e.Job.Rml.FinalName; RemoteJobInputOutput inputOutput = new RemoteJobInputOutput(); if (_clients.Count > 0 && _buildProvider.MachineType.IsXMachine()) { inputOutput = GetCurrentJobInputOutput(); } foreach (var client in _clients.ToList()) { try { RemoteJobProgress progress = new RemoteJobProgress(); progress.Stage = RemoteJobStage.Started; progress.JobStatus = _handler.JobStatus; progress.InputOutput = inputOutput; await client.Receiver.SendGenericResponse(new RemoteJobUpdateResponse() { Job = _currentJobDTO, ProcessParameters = _currentJobProcessParameters, Progress = progress, RmlName = _rmlName }, client.Token); client.JobSent = true; } catch { } } } private async void JobHandler_StatusChanged(object sender, RunningJobStatus e) { var currentProgress = GetJobProgress(_handler); RemoteJobInputOutput inputOutput = new RemoteJobInputOutput(); if (_clients.Count > 0 && _buildProvider.MachineType.IsXMachine()) { inputOutput = GetCurrentJobInputOutput(); } currentProgress.InputOutput = inputOutput; foreach (var client in _clients.ToList()) { if (client.JobSent) { try { await client.Receiver.SendGenericResponse(new RemoteJobUpdateResponse() { Progress = currentProgress, RmlName = _rmlName, }, client.Token); } catch { } } else { try { RemoteJobProgress progress = new RemoteJobProgress(); progress.Stage = RemoteJobStage.Started; progress.JobStatus = _handler.JobStatus; progress.InputOutput = inputOutput; await client.Receiver.SendGenericResponse(new RemoteJobUpdateResponse() { Job = _currentJobDTO, ProcessParameters = _currentJobProcessParameters, Progress = progress, RmlName = _rmlName }, client.Token); client.JobSent = true; } catch { } } } } private async void JobHandler_Stopped(object sender, EventArgs e) { foreach (var client in _clients.ToList().Where(x => x.JobSent)) { try { await client.Receiver.SendGenericResponse(new RemoteJobUpdateResponse() { Progress = GetJobProgress(_handler), }, client.Token); client.JobSent = false; } catch { } } } private RemoteJobInputOutput GetCurrentJobInputOutput() { var inputOutput = new RemoteJobInputOutput(); if (_inputOutputProvider == null && _buildProvider.MachineType.IsXMachine()) { _inputOutputProvider = TangoIOC.Default.GetInstance(); } if (_inputOutputProvider != null) { var i = _inputOutputProvider; if (i.CurrentBrushStop != null) { var colorSpace = i.CurrentBrushStop.BrushColorSpace; inputOutput.ColorSpace = colorSpace; switch (colorSpace) { case BL.Enumerations.ColorSpaces.Volume: if (i.CurrentBrushStop.LiquidVolumes != null) { foreach (var input in i.CurrentBrushStop.LiquidVolumesOrderedPigmentedForStandardUser.ToList()) { inputOutput.Input.Add(new Tuple(input.IdsPack.LiquidType.ShortName, input.Volume.ToString())); } } break; case BL.Enumerations.ColorSpaces.RGB: inputOutput.Input.Add(new Tuple("R", i.CurrentBrushStop.Red.ToString())); inputOutput.Input.Add(new Tuple("G", i.CurrentBrushStop.Green.ToString())); inputOutput.Input.Add(new Tuple("B", i.CurrentBrushStop.Blue.ToString())); break; case BL.Enumerations.ColorSpaces.LAB: inputOutput.Input.Add(new Tuple("L", i.CurrentBrushStop.L.ToString("0.00"))); inputOutput.Input.Add(new Tuple("A", i.CurrentBrushStop.A.ToString("0.00"))); inputOutput.Input.Add(new Tuple("B", i.CurrentBrushStop.B.ToString("0.00"))); break; case BL.Enumerations.ColorSpaces.Catalog: inputOutput.Input.Add(new Tuple("Catalog", i.CurrentBrushStop.ColorCatalog.Name)); inputOutput.Input.Add(new Tuple("Color", i.CurrentBrushStop.ColorCatalogsItem.Name)); break; default: break; } try { foreach (var output in i.LiquidOutputs) { inputOutput.Output.Add(new Tuple(output.LiquidType.ShortName, output.Volume.ToString())); } } catch { inputOutput.Output.Add(new Tuple("C", "N/A")); inputOutput.Output.Add(new Tuple("LC", "N/A")); inputOutput.Output.Add(new Tuple("M", "N/A")); inputOutput.Output.Add(new Tuple("LM", "N/A")); inputOutput.Output.Add(new Tuple("Y", "N/A")); inputOutput.Output.Add(new Tuple("LY", "N/A")); inputOutput.Output.Add(new Tuple("K", "N/A")); } } } return inputOutput; } private RemoteJobProgress GetJobProgress(JobHandler handler) { RemoteJobStage stage = RemoteJobStage.Running; if (_handler.Status.IsCanceled) { stage = RemoteJobStage.Aborted; } else if (_handler.Status.IsCompleted) { stage = RemoteJobStage.Completed; } else if (_handler.Status.IsFailed) { stage = RemoteJobStage.Failed; } return new RemoteJobProgress() { Stage = stage, JobStatus = handler.JobStatus, }; } [ExternalBridgeRequestHandlerMethod(typeof(RemoteJobUpdateRequest), RequestHandlerLoggingMode.LogRequestName)] public async Task OnRunningJobUpdateRequest(RemoteJobUpdateRequest request, String token, ExternalBridgeReceiver receiver) { this.ThrowIfDisabled(); if (!_clients.ToList().Exists(x => x.Receiver == receiver)) { _clients.Add(new RunningJobUpdateClient() { Receiver = receiver, Token = token }); } await receiver.SendGenericResponse(new RemoteJobUpdateResponse(), token); } public void OnReceiverDisconnected(ExternalBridgeReceiver receiver) { _clients.RemoveAll(x => x.Receiver == receiver); } } }