aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Messages/MachineConnectionChangedMessage.cs
blob: af36f2edf614fca74590dcc8ceb42279829ced4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Integration.ExternalBridge;

namespace Tango.MachineStudio.Common.Messages
{
    public class MachineConnectionChangedMessage : IStudioMessage
    {
        public IExternalBridgeClient Machine { get; set; }
    }
}
hlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
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.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<RunningJobUpdateClient> _clients;
        private JobHandler _handler;
        private JobDTO _currentJobDTO;
        private ProcessParametersTableDTO _currentJobProcessParameters;

        public bool Enabled { get; set; } = true;

        private IMachineProvider MachineProvider { get; set; }

        public DefaultRemoteJobService(IPPCExternalBridgeService externalBridge, IMachineProvider machineProvider)
        {
            externalBridge.RegisterRequestHandler(this);

            MachineProvider = machineProvider;

            _clients = new List<RunningJobUpdateClient>();
            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);

            foreach (var client in _clients.ToList())
            {
                try
                {
                    RemoteJobProgress progress = new RemoteJobProgress();
                    progress.Stage = RemoteJobStage.Started;
                    progress.JobStatus = _handler.JobStatus;

                    await client.Receiver.SendGenericResponse(new RemoteJobUpdateResponse()
                    {
                        Job = _currentJobDTO,
                        ProcessParameters = _currentJobProcessParameters,
                        Progress = progress
                    }, client.Token);

                    client.JobSent = true;
                }
                catch { }
            }
        }

        private async void JobHandler_StatusChanged(object sender, RunningJobStatus e)
        {
            foreach (var client in _clients.ToList())
            {
                if (client.JobSent)
                {
                    try
                    {
                        await client.Receiver.SendGenericResponse(new RemoteJobUpdateResponse()
                        {
                            Progress = GetJobProgress(_handler)
                        }, client.Token);
                    }
                    catch { }
                }
                else
                {
                    try
                    {
                        RemoteJobProgress progress = new RemoteJobProgress();
                        progress.Stage = RemoteJobStage.Started;
                        progress.JobStatus = _handler.JobStatus;

                        await client.Receiver.SendGenericResponse(new RemoteJobUpdateResponse()
                        {
                            Job = _currentJobDTO,
                            ProcessParameters = _currentJobProcessParameters,
                            Progress = progress
                        }, 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 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);
        }
    }
}