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 RemoteJobStarted; public event EventHandler RemoteJobStopped; public event EventHandler 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(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; } } } } }