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
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.DI;
using Tango.FSE.Common.Connection;
using Tango.FSE.Common.Notifications;
using Tango.FSE.Common.RemoteJob;
using Tango.Integration.Operation;
using Tango.PPC.Shared.Jobs;
using Tango.Transport;
namespace Tango.FSE.UI.RemoteJob
{
[TangoCreateWhenRegistered]
public class DefaultRemoteJobProvider : IRemoteJobProvider
{
private JobHandler _handler;
public event EventHandler<RemoteJobStartedEventArgs> RemoteJobStarted;
public event EventHandler<RemoteJobStoppedEventArgs> RemoteJobStopped;
public bool IsRemoteJobRunning { get; private set; }
[TangoInject]
private IMachineProvider MachineProvider { get; set; }
[TangoInject]
private INotificationProvider NotificationProvider { get; set; }
public void OnApplicationStarted()
{
MachineProvider.MachineConnected += MachineProvider_MachineConnected;
}
private void MachineProvider_MachineConnected(object sender, MachineConnectedEventArgs e)
{
init();
}
private void init()
{
try
{
MachineProvider.MachineOperator.SendGenericContinuousRequest<RemoteJobUpdateRequest, RemoteJobUpdateResponse>(new RemoteJobUpdateRequest()
{
//Request
}, new TransportContinuousRequestConfig()
{
Timeout = TimeSpan.FromSeconds(30)
}).Subscribe((response) =>
{
//Response
InvalidateJobResponse(response);
}, (ex) =>
{
//Failed
NotificationProvider.PushErrorReportingSnackbar(ex, "Remote Job Module Error", "Remote job display failed to initialize.");
}, () =>
{
//Completed ? (never)
});
}
catch (Exception ex)
{
NotificationProvider.PushErrorReportingSnackbar(ex, "Remote Job Module Error", "Remote job display failed to initialize.");
}
}
private void InvalidateJobResponse(RemoteJobUpdateResponse response)
{
var stage = response.Progress.Stage;
if (stage == RemoteJobStage.None)
{
return;
}
if (stage == RemoteJobStage.Started && !IsRemoteJobRunning)
{
_handler = new JobHandler(() => { }, response.Job.ToObservable(), null, response.ProcessParameters.ToObservable(), JobHandlerModes.SettingUp);
IsRemoteJobRunning = true;
RemoteJobStarted?.Invoke(this, new RemoteJobStartedEventArgs()
{
JobHandler = _handler
});
return;
}
if (_handler != null)
{
if (response.Progress.JobStatus != null)
{
_handler.RaiseStatusReceived(response.Progress.JobStatus);
}
if (stage == RemoteJobStage.Failed && IsRemoteJobRunning)
{
IsRemoteJobRunning = false;
_handler.RaiseFailed(new Exception("Job failed"));
_handler = null;
}
else if (stage == RemoteJobStage.Aborted && IsRemoteJobRunning)
{
IsRemoteJobRunning = false;
_handler.RaiseCanceled();
_handler = null;
}
else if (stage == RemoteJobStage.Completed && IsRemoteJobRunning)
{
IsRemoteJobRunning = false;
RemoteJobStopped?.Invoke(this, new RemoteJobStoppedEventArgs());
_handler.RaiseCompleted();
_handler = null;
}
}
}
public void OnApplicationReady()
{
//Do Nothing.
}
}
}
|