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
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.DI;
using Tango.FSE.Common.Connection;
using Tango.FSE.Common.FSEApplication;
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 : ExtendedObject, IRemoteJobProvider
{
private JobHandler _handler;
public event EventHandler<RemoteJobStartedEventArgs> RemoteJobStarted;
public event EventHandler<RemoteJobStoppedEventArgs> RemoteJobStopped;
public event EventHandler<RemoteJobUpdatedEventArgs> RemoteJobUpdated;
private bool _isRemoteJobRunning;
public bool IsRemoteJobRunning
{
get { return _isRemoteJobRunning; }
private set { _isRemoteJobRunning = value; RaisePropertyChangedAuto(); }
}
[TangoInject]
private IMachineProvider MachineProvider { get; set; }
[TangoInject]
private INotificationProvider NotificationProvider { get; set; }
public void OnApplicationStarted(IFSEApplicationManager applicationManager)
{
MachineProvider.MachineConnected += MachineProvider_MachineConnected;
}
private void MachineProvider_MachineConnected(object sender, MachineConnectedEventArgs e)
{
if (MachineProvider.IsPPCAvailable)
{
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) =>
{
if (!(ex is TransporterDisconnectedException))
{
//Failed
LogManager.Log(ex, "Error initializing remote job tracking.");
NotificationProvider.PushErrorReportingSnackbar(ex, "Remote Job Module Error", "Remote job tracking failed to initialize.");
}
}, () =>
{
//Completed ? (never)
});
}
catch (Exception ex)
{
NotificationProvider.PushErrorReportingSnackbar(ex, "Remote Job Module Error", "Remote job tracking 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
});
RemoteJobUpdated?.Invoke(this, new RemoteJobUpdatedEventArgs() { RemoteJobUpdateResponse = response });
return;
}
if (_handler != null)
{
if (response.Progress.JobStatus != null)
{
_handler.RaiseStatusReceived(response.Progress.JobStatus);
RemoteJobUpdated?.Invoke(this, new RemoteJobUpdatedEventArgs() { RemoteJobUpdateResponse = response });
}
if (stage == RemoteJobStage.Failed && IsRemoteJobRunning)
{
IsRemoteJobRunning = false;
_handler.RaiseFailed(new Exception("Job failed."));
RemoteJobStopped?.Invoke(this, new RemoteJobStoppedEventArgs() { Stage = stage });
_handler = null;
}
else if (stage == RemoteJobStage.Aborted && IsRemoteJobRunning)
{
IsRemoteJobRunning = false;
_handler.RaiseCanceled();
RemoteJobStopped?.Invoke(this, new RemoteJobStoppedEventArgs() { Stage = stage });
_handler = null;
}
else if (stage == RemoteJobStage.Completed && IsRemoteJobRunning)
{
IsRemoteJobRunning = false;
_handler.RaiseCompleted();
RemoteJobStopped?.Invoke(this, new RemoteJobStoppedEventArgs() { Stage = stage });
_handler = null;
}
}
}
}
}
|