aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/UpdatePackages/Tango.PPC.Packages.JobRunsUpdater/JobRunsPatch.cs
blob: 5179a25531a1051cff876f4e6f939e0bbfac4cda (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Builders;
using Tango.Core;
using Tango.PPC.Common;
using Tango.PPC.Common.UpdatePackages;
using Tango.PPC.Shared.Updates;
using Tango.Settings;

namespace Tango.PPC.Packages.JobRunsUpdater
{
    [PPCPackage(PackageType.Pre, "JobRuns Statistics Patch", false)]
    public class JobRunsPatch : ExtendedObject, IPPCPackage
    {
        public Task Run(PackageContext context)
        {
            return Task.Factory.StartNew(() =>
            {
                try
                {
                    LogManager.Log("Applying JobRuns Statistics Patch...");

                    context.ReportProgress("Applying job runs statistics patch...");

                    Thread.Sleep(2000); //Just so we can see something happened.

                    using (ObservablesContext db = ObservablesContext.CreateDefault())
                    {
                        foreach (var jobRun in db.JobRuns.Where(x => x.JobLogicalLength == 0))
                        {
                            try
                            {
                                var job = new JobBuilder(db).Set(jobRun.JobGuid).WithSegments().WithBrushStops().Build();
                                jobRun.JobLogicalLength = job.Length;
                                jobRun.NumberOfUnits = job.NumberOfUnits;
                                jobRun.ApplicationVersion = Assembly.GetEntryAssembly().GetName().Version.ToString();
                                jobRun.FirmwareVersion = SettingsManager.Default.GetOrCreate<PPCSettings>().PreviousApplicationVersion;

                                var rml = new RmlBuilder(db).Set(job.RmlGuid).WithActiveParametersGroup().Build();
                                jobRun.CeVersion = rml.ColorConversionVersion.ToString();
                                jobRun.ProcessParametersTableGuid = rml.GetActiveProcessGroup().ProcessParametersTables[0].Guid;

                                var jobFile = jobRun.JobFile;

                                int segmentIndex = 0;

                                foreach (var segment in jobFile.Segments)
                                {
                                    segmentIndex++;

                                    var jobSegment = job.Segments.FirstOrDefault(x => x.SegmentIndex == segmentIndex);

                                    foreach (var stop in segment.BrushStops)
                                    {
                                        var jobStop = jobSegment.BrushStops.FirstOrDefault(x => x.StopIndex == stop.StopIndex);

                                        if (jobStop.BestMatchR != null)
                                        {
                                            stop.BestMatchR = jobStop.BestMatchR.Value;
                                            stop.BestMatchG = jobStop.BestMatchG.Value;
                                            stop.BestMatchB = jobStop.BestMatchB.Value;
                                        }
                                        else
                                        {
                                            stop.BestMatchR = jobStop.Red;
                                            stop.BestMatchG = jobStop.Green;
                                            stop.BestMatchB = jobStop.Blue;
                                        }
                                    }
                                }

                                jobRun.JobString = jobFile.ToString();
                                db.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                LogManager.Log(ex, $"Error updating JobRun '{jobRun.ID}'.");
                            }
                        }    
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error occurred while trying to apply the job runs statistics patch.");
                }
            });
        }
    }
}