aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Resume/DefaultJobResumeManager.cs
blob: 18c899fd332c9705e1688fc4a8062405019d3fd8 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.DI;
using Tango.Integration.Operation;
using Tango.PPC.Common.Build;
using Tango.PPC.Common.Connection;

namespace Tango.PPC.Common.Resume
{
    public class DefaultJobResumeManager : ExtendedObject, IJobResumeManager
    {
        private IMachineProvider _machineProvider;
        private IBuildProvider _buildProvider;

        public event EventHandler<JobResumeUpdatedEventArgs> JobResumeUpdated;
        public event EventHandler<JobResumeDroppedEventArgs> JobResumeDropped;

        [TangoInject]
        public DefaultJobResumeManager(IMachineProvider machineProvider,IBuildProvider buildProvider)
        {
            _machineProvider = machineProvider;
            _buildProvider = buildProvider;
            machineProvider.MachineOperator.PrintingEnded += MachineOperator_PrintingEnded;
        }

        private void MachineOperator_PrintingEnded(object sender, PrintingEventArgs e)
        {
            if (_buildProvider.MachineType.IsXMachine())
            {
                UpdateJobResume(e);
            }
        }

        private void UpdateJobResume(PrintingEventArgs e)
        {
            try
            {
                if (!e.JobHandler.Status.IsCompleted)
                {
                    if (e.JobHandler.JobStatus.Progress <= e.JobHandler.ProcessParameters.DryerBufferLengthMeters) return;

                    var model = JobResumeDB.Default.Get(e.Job.Guid);
                    bool insert = false;
                    if (model == null)
                    {
                        model = new JobResumeModel();
                        insert = true;
                    }

                    model.JobGuid = e.Job.Guid;
                    model.FirstUnitStartPosition = e.JobHandler.Status.CurrentUnitProgress;
                    model.RemainingUnits = e.JobHandler.Status.RemainingUnits;
                    model.GlobalStartPosition = e.JobHandler.JobStatus.Progress;
                    model.ResumeProgress = e.JobHandler.Status.ProgressMinusSettingUp;

                    if (insert)
                    {
                        JobResumeDB.Default.Add(model);
                    }
                    else
                    {
                        JobResumeDB.Default.Update(model);
                    }

                    JobResumeUpdated?.Invoke(this, new JobResumeUpdatedEventArgs()
                    {
                        JobGuid = e.Job.Guid,
                        ResumeModel = model
                    });
                }
                else
                {
                    JobResumeDB.Default.Delete(e.Job.Guid);

                    JobResumeUpdated?.Invoke(this, new JobResumeUpdatedEventArgs()
                    {
                        JobGuid = e.Job.Guid,
                        ResumeModel = null
                    });
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error inserting/updating job resume info on db.");
            }
        }

        public JobResumeModel GetJobResumeModel(string jobGuid)
        {
            return JobResumeDB.Default.Get(jobGuid);
        }

        public void DropResume(string jobGuid)
        {
            JobResumeDB.Default.Delete(jobGuid);

            JobResumeDropped?.Invoke(this, new JobResumeDroppedEventArgs() { JobGuid = jobGuid });
        }
    }
}