diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-26 14:04:34 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-26 14:04:34 +0200 |
| commit | 3e71ab50870db524f7e17aa9d2042da52b15ad63 (patch) | |
| tree | e1bee015a2ce129316bbc30a0ff6466f239b33fd /Software/Visual_Studio/Tango.Integration/JobRuns | |
| parent | a756bdd61609c9bd093e81d1e2a9edf6bd912882 (diff) | |
| download | Tango-3e71ab50870db524f7e17aa9d2042da52b15ad63.tar.gz Tango-3e71ab50870db524f7e17aa9d2042da52b15ad63.zip | |
Implemented integration IJobRunsLogger.
Implemented DEBUG/RELEASE JsonController.
Implemented Local/Remote machine studio setting.
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration/JobRuns')
| -rw-r--r-- | Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs | 221 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Integration/JobRuns/IJobRunsLogger.cs | 41 |
2 files changed, 262 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs b/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs new file mode 100644 index 000000000..07844af17 --- /dev/null +++ b/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs @@ -0,0 +1,221 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.Entities; +using Tango.BL.Enumerations; +using Tango.Integration.Operation; + +namespace Tango.Integration.JobRuns +{ + /// <summary> + /// Represents a basic database job runs logger. + /// </summary> + /// <seealso cref="Tango.Integration.JobRuns.IJobRunsLogger" /> + public class BasicJobRunsLogger : IJobRunsLogger + { + private DateTime _start_date; + private Job _job; + + #region Properties + + /// <summary> + /// Gets the machine operator. + /// </summary> + public IMachineOperator MachineOperator { get; private set; } + + /// <summary> + /// Gets a value indicating whether this instance is started. + /// </summary> + public bool IsStarted { get; private set; } + + /// <summary> + /// Gets or sets the job designations of which the logger should log (supports multiple flags). + /// </summary> + public JobDesignations JobDesignation { get; set; } + + #endregion + + #region Constructors + + /// <summary> + /// Initializes a new instance of the <see cref="BasicJobRunsLogger"/> class. + /// </summary> + /// <param name="machineOperator">The machine operator.</param> + public BasicJobRunsLogger(IMachineOperator machineOperator) + { + JobDesignation = JobDesignations.Default; + MachineOperator = machineOperator; + Init(); + } + + #endregion + + #region Private Methods + + /// <summary> + /// Initializes this instance. + /// </summary> + private void Init() + { + MachineOperator.PrintingStarted -= Machine_PrintingStarted; + MachineOperator.PrintingStarted += Machine_PrintingStarted; + MachineOperator.PrintingCompleted -= Machine_PrintingCompleted; + MachineOperator.PrintingCompleted += Machine_PrintingCompleted; + MachineOperator.PrintingAborted -= Machine_PrintingAborted; + MachineOperator.PrintingAborted += Machine_PrintingAborted; + MachineOperator.PrintingFailed -= Machine_PrintingFailed; + MachineOperator.PrintingFailed += Machine_PrintingFailed; + } + + private bool ShouldLog() + { + return IsStarted && _job != null && JobDesignation.HasFlag(_job.Designation); + } + + #endregion + + #region Public Methods + + /// <summary> + /// Starts the logger. + /// </summary> + public void Start() + { + IsStarted = true; + } + + /// <summary> + /// Stops the logger. + /// </summary> + public void Stop() + { + IsStarted = false; + } + + #endregion + + #region Event Handlers + + private void Machine_PrintingFailed(object sender, PrintingFailedEventArgs e) + { + if (ShouldLog()) + { + if (e.Job.Guid == _job.Guid) + { + Task.Factory.StartNew(() => + { + using (var db = ObservablesContext.CreateDefault()) + { + db.JobRuns.Add(new JobRun() + { + StartDate = _start_date, + EndDate = DateTime.UtcNow, + JobGuid = _job.Guid, + JobRunStatus = JobRunStatus.Failed, + EndPosition = e.JobHandler.Status.Progress, + FailedMessage = e.Exception.Message, + }); + + e.Job.LastRun = DateTime.UtcNow; + _job.LastRun = DateTime.UtcNow; + + var job = db.Jobs.SingleOrDefault(x => x.Guid == _job.Guid); + + if (job != null) + { + job.LastRun = DateTime.UtcNow; + } + + db.SaveChanges(); + } + }); + } + } + } + + private void Machine_PrintingAborted(object sender, PrintingEventArgs e) + { + if (ShouldLog()) + { + if (e.Job.Guid == _job.Guid) + { + Task.Factory.StartNew(() => + { + using (var db = ObservablesContext.CreateDefault()) + { + db.JobRuns.Add(new JobRun() + { + StartDate = _start_date, + EndDate = DateTime.UtcNow, + JobGuid = _job.Guid, + EndPosition = e.JobHandler.Status.Progress, + JobRunStatus = JobRunStatus.Aborted, + }); + + e.Job.LastRun = DateTime.UtcNow; + _job.LastRun = DateTime.UtcNow; + + var job = db.Jobs.SingleOrDefault(x => x.Guid == _job.Guid); + + if (job != null) + { + job.LastRun = DateTime.UtcNow; + } + + db.SaveChanges(); + } + }); + } + } + } + + private void Machine_PrintingCompleted(object sender, PrintingEventArgs e) + { + if (ShouldLog()) + { + if (e.Job.Guid == _job.Guid) + { + Task.Factory.StartNew(() => + { + using (var db = ObservablesContext.CreateDefault()) + { + db.JobRuns.Add(new JobRun() + { + StartDate = _start_date, + EndDate = DateTime.UtcNow, + JobGuid = _job.Guid, + EndPosition = e.JobHandler.Status.Progress, + JobRunStatus = JobRunStatus.Completed, + }); + + e.Job.LastRun = DateTime.UtcNow; + _job.LastRun = DateTime.UtcNow; + + var job = db.Jobs.SingleOrDefault(x => x.Guid == _job.Guid); + + if (job != null) + { + job.LastRun = DateTime.UtcNow; + } + + db.SaveChanges(); + } + }); + } + } + } + + private void Machine_PrintingStarted(object sender, PrintingEventArgs e) + { + _job = e.Job; + _start_date = DateTime.UtcNow; + } + + + + #endregion + } +} diff --git a/Software/Visual_Studio/Tango.Integration/JobRuns/IJobRunsLogger.cs b/Software/Visual_Studio/Tango.Integration/JobRuns/IJobRunsLogger.cs new file mode 100644 index 000000000..8c4174311 --- /dev/null +++ b/Software/Visual_Studio/Tango.Integration/JobRuns/IJobRunsLogger.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL.Enumerations; +using Tango.Integration.Operation; + +namespace Tango.Integration.JobRuns +{ + /// <summary> + /// Represents a machine operator job runs logger + /// </summary> + public interface IJobRunsLogger + { + /// <summary> + /// Gets the machine operator. + /// </summary> + IMachineOperator MachineOperator { get; } + + /// <summary> + /// Gets or sets the job designations of which the logger should log (supports multiple flags). + /// </summary> + JobDesignations JobDesignation { get; set; } + + /// <summary> + /// Gets a value indicating whether this instance is started. + /// </summary> + bool IsStarted { get; } + + /// <summary> + /// Starts the logger. + /// </summary> + void Start(); + + /// <summary> + /// Stops the logger. + /// </summary> + void Stop(); + } +} |
