aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteJob/DefaultRemoteJobService.cs
blob: 00b4f5ad1c127e8fceec952783db55cadd2882dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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<RunningJobUpdateClient> _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<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);
            _rmlName = e.Job.Rml.FinalName;

            RemoteJobInputOutput inputOutput = new RemoteJobInputOutput();

            if (_clients.Count > 0 && _buildProvider.IsEureka)
            {
                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.IsEureka)
            {
                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.IsEureka)
            {
                _inputOutputProvider = TangoIOC.Default.GetInstance<IRemoteJobInputOutputProvider>();
            }

            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:
                            inputOutput.Input.Add(new Tuple<string, String>("C", i.CurrentBrushStop.Cyan.ToString("0.00")));
                            inputOutput.Input.Add(new Tuple<string, String>("M", i.CurrentBrushStop.Magenta.ToString("0.00")));
                            inputOutput.Input.Add(new Tuple<string, String>("Y", i.CurrentBrushStop.Yellow.ToString("0.00")));
                            inputOutput.Input.Add(new Tuple<string, String>("K", i.CurrentBrushStop.Black.ToString("0.00")));
                            break;
                        case BL.Enumerations.ColorSpaces.RGB:
                            inputOutput.Input.Add(new Tuple<string, String>("R", i.CurrentBrushStop.Red.ToString()));
                            inputOutput.Input.Add(new Tuple<string, String>("G", i.CurrentBrushStop.Green.ToString()));
                            inputOutput.Input.Add(new Tuple<string, String>("B", i.CurrentBrushStop.Blue.ToString()));
                            break;
                        case BL.Enumerations.ColorSpaces.LAB:
                            inputOutput.Input.Add(new Tuple<string, String>("L", i.CurrentBrushStop.L.ToString("0.00")));
                            inputOutput.Input.Add(new Tuple<string, String>("A", i.CurrentBrushStop.A.ToString("0.00")));
                            inputOutput.Input.Add(new Tuple<string, String>("B", i.CurrentBrushStop.B.ToString("0.00")));
                            break;
                        case BL.Enumerations.ColorSpaces.Catalog:
                            inputOutput.Input.Add(new Tuple<string, String>("Catalog", i.CurrentBrushStop.ColorCatalog.Name));
                            inputOutput.Input.Add(new Tuple<string, String>("Color", i.CurrentBrushStop.ColorCatalogsItem.Name));
                            break;
                        default:
                            break;
                    }

                    try
                    {
                        inputOutput.Output.Add(new Tuple<string, string>("C", i.CyanOutput.ToString()));
                        inputOutput.Output.Add(new Tuple<string, string>("LC", i.LightCyanOutput.ToString()));
                        inputOutput.Output.Add(new Tuple<string, string>("M", i.MagentaOutput.ToString()));
                        inputOutput.Output.Add(new Tuple<string, string>("LM", i.LightMagentaOutput.ToString()));
                        inputOutput.Output.Add(new Tuple<string, string>("Y", i.YellowOutput.ToString()));
                        inputOutput.Output.Add(new Tuple<string, string>("LY", i.LightYellowOutput.ToString()));
                        inputOutput.Output.Add(new Tuple<string, string>("K", i.BlackOutput.ToString()));
                    }
                    catch
                    {
                        inputOutput.Output.Add(new Tuple<string, string>("C", "N/A"));
                        inputOutput.Output.Add(new Tuple<string, string>("LC", "N/A"));
                        inputOutput.Output.Add(new Tuple<string, string>("M", "N/A"));
                        inputOutput.Output.Add(new Tuple<string, string>("LM", "N/A"));
                        inputOutput.Output.Add(new Tuple<string, string>("Y", "N/A"));
                        inputOutput.Output.Add(new Tuple<string, string>("LY", "N/A"));
                        inputOutput.Output.Add(new Tuple<string, string>("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);
        }
    }
}