diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2025-08-14 03:43:16 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2025-08-14 03:43:16 +0300 |
| commit | d9322cdd83c46d1add0a3c8421e28ee878c29396 (patch) | |
| tree | db0dd91f24b9ae7af438f498a4f6200e04977599 /Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs | |
| parent | 61bc5d6672a0dd3c7fd0f8e1a95d5c037fb20cab (diff) | |
| download | Tango-d9322cdd83c46d1add0a3c8421e28ee878c29396.tar.gz Tango-d9322cdd83c46d1add0a3c8421e28ee878c29396.zip | |
Machine Operator Printing Operations Major Refactoring.
Revived Job Resume for X1/X4/PPC.
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs | 431 |
1 files changed, 211 insertions, 220 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs b/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs index 1596c4b22..293e0bcc6 100644 --- a/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs +++ b/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs @@ -123,212 +123,223 @@ namespace Tango.Integration.JobRuns private void InsertJobRun(PrintingEventArgs e, JobRunStatus status, Exception exception) { - if (ShouldLog()) + if (!e.IsResumingJob) { - if (e.Job.Guid == _job.Guid) + if (ShouldLog()) { - Task.Factory.StartNew(() => + if (e.Job.Guid == _job.Guid) { - try + Task.Factory.StartNew(() => { - using (var db = ObservablesContext.CreateDefault()) + try { - var colorSpaces = db.ColorSpaces.ToList(); - - JobRun run = new JobRun(); - - run.UserGuid = _job.UserGuid; - run.StartDate = e.StartDate; - run.UploadingStartDate = e.UploadingStartTime; - run.HeatingStartDate = e.HeatingStartTime; - run.ActualStartDate = e.ActualStartTime; - run.EndDate = DateTime.UtcNow; - run.JobName = _job.Name; - run.Source = JobSource; - run.Designation = _job.Designation; - run.JobGuid = _job.Guid; - run.RmlGuid = _job.RmlGuid; - run.MachineGuid = _job.MachineGuid; - run.JobRunStatus = status; - run.EndPosition = e.JobHandler.Status.Progress; - run.NumberOfUnits = _job.NumberOfUnits; - run.JobLength = e.JobHandler.Status.TotalProgress; - run.JobLogicalLength = e.Job.Length; - run.LiquidQuantities = e.LiquidQuantities; - run.IsGradient = _job.Segments.Any(x => x.BrushStops.Count > 1); - run.GradientResolutionCm = MachineOperator.GradientGenerationConfiguration.ResolutionCM; - run.ActualStartPosition = e.Job.ResumeStartPosition; - run.ActualEndPosition = e.JobHandler.Status.ProgressMinusSettingUp; - - if (_defaultMachine != null) + using (var db = ObservablesContext.CreateDefault()) { - run.MachineType = _defaultMachine.MachineType; - } - else if (_job.Machine != null) - { - run.MachineType = _job.Machine.MachineType; - } + var colorSpaces = db.ColorSpaces.ToList(); - var jobFile = e.Job.ToJobFileWhenLoaded(); + JobRun run = new JobRun(); - try - { - if (_job.Designation == JobDesignations.FineTuning) + run.UserGuid = _job.UserGuid; + run.StartDate = e.StartDate; + run.UploadingStartDate = e.UploadingStartTime; + run.HeatingStartDate = e.HeatingStartTime; + run.ActualStartDate = e.ActualStartTime; + run.EndDate = DateTime.UtcNow; + run.JobName = _job.Name; + run.Source = JobSource; + run.Designation = _job.Designation; + run.JobGuid = _job.Guid; + run.RmlGuid = _job.RmlGuid; + run.MachineGuid = _job.MachineGuid; + run.JobRunStatus = status; + run.EndPosition = e.JobHandler.Status.Progress; + run.NumberOfUnits = _job.NumberOfUnits; + run.JobLength = e.JobHandler.Status.TotalProgress; + run.JobLogicalLength = e.Job.Length; + run.LiquidQuantities = e.LiquidQuantities; + run.IsGradient = _job.Segments.Any(x => x.BrushStops.Count > 1); + run.GradientResolutionCm = MachineOperator.GradientGenerationConfiguration.ResolutionCM; + run.ActualStartPosition = e.Job.ResumeStartPosition; + run.ActualEndPosition = e.JobHandler.Status.ProgressMinusSettingUp; + + if (_defaultMachine != null) { - jobFile.Segments.First().BrushStops.First().ColorSpaceGuid = colorSpaces.First(x => x.Code == (int)ColorSpaces.LAB).Guid; + run.MachineType = _defaultMachine.MachineType; + } + else if (_job.Machine != null) + { + run.MachineType = _job.Machine.MachineType; } - } - catch (Exception ex) - { - LogManager.Log(ex, "Error setting brush stop color space to LAB on fine tuning job run (JobFileString)."); - } - - run.JobString = jobFile.ToString(); - - run.ApplicationVersion = Assembly.GetEntryAssembly().GetName().Version.ToString(); - run.FirmwareVersion = MachineOperator.DeviceInformation?.Version; - run.CeVersion = _job.Rml.ColorConversionVersion.ToString(); - run.ProcessParametersTableGuid = MachineOperator.CurrentProcessParameters?.Guid; - - //Set individual liquid quantities - - //Cyan - var cyan = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Cyan); - run.CyanQuantity = cyan != null ? cyan.Quantity : 0; - - //Magenta - var magenta = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Magenta); - run.MagentaQuantity = magenta != null ? magenta.Quantity : 0; - - //Yellow - var yellow = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Yellow); - run.YellowQuantity = yellow != null ? yellow.Quantity : 0; - //Black - var black = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Black); - run.BlackQuantity = black != null ? black.Quantity : 0; + var jobFile = e.Job.ToJobFileWhenLoaded(); - //TI - var ti = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.TransparentInk); - run.TransparentQuantity = ti != null ? ti.Quantity : 0; + try + { + if (_job.Designation == JobDesignations.FineTuning) + { + jobFile.Segments.First().BrushStops.First().ColorSpaceGuid = colorSpaces.First(x => x.Code == (int)ColorSpaces.LAB).Guid; + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error setting brush stop color space to LAB on fine tuning job run (JobFileString)."); + } - //Lubricant - var lubricant = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Lubricant); - run.LubricantQuantity = lubricant != null ? lubricant.Quantity : 0; + run.JobString = jobFile.ToString(); - //Cleaner - var cleaner = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Cleaner); - run.CleanerQuantity = cleaner != null ? cleaner.Quantity : 0; + run.ApplicationVersion = Assembly.GetEntryAssembly().GetName().Version.ToString(); + run.FirmwareVersion = MachineOperator.DeviceInformation?.Version; + run.CeVersion = _job.Rml.ColorConversionVersion.ToString(); + run.ProcessParametersTableGuid = MachineOperator.CurrentProcessParameters?.Guid; - //Light Cyan - var lightCyan = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.LightCyan); - run.LightCyanQuantity = lightCyan != null ? lightCyan.Quantity : 0; + //Set liquid quantities + SetJobRunLiquidQuantities(run, run.LiquidQuantities); - //Light Magenta - var lightMagenta = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.LightMagenta); - run.LightMagentaQuantity = lightMagenta != null ? lightMagenta.Quantity : 0; + if (exception != null) + { + run.FailedMessage = exception.FlattenMessage(); + } - //Light Yellow - var lightYellow = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.LightYellow); - run.LightYellowQuantity = lightYellow != null ? lightYellow.Quantity : 0; + if (_job.Designation == JobDesignations.FineTuning) + { + try + { + run.FineTuningString = JsonConvert.SerializeObject(_job.VectorFineTuningRunModel); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error serializing fine tuning model for job run."); + } + } - //Blue - var blue = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Blue); - run.BlueQuantity = blue != null ? blue.Quantity : 0; + db.JobRuns.Add(run); - //Light Blue - var lightBlue = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.LightBlue); - run.LightBlueQuantity = lightBlue != null ? lightBlue.Quantity : 0; + e.Job.LastRun = DateTime.UtcNow; + _job.LastRun = DateTime.UtcNow; - //Orange - var orange = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Orange); - run.OrangeQuantity = orange != null ? orange.Quantity : 0; + var job = db.Jobs.SingleOrDefault(x => x.Guid == _job.Guid); - //Light Orange - var lightOrange = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.LightOrange); - run.LightOrangeQuantity = lightOrange != null ? lightOrange.Quantity : 0; + if (job != null) + { + job.LastRun = DateTime.UtcNow; + } - //Rubine - var rubine = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Rubine); - run.RubineQuantity = rubine != null ? rubine.Quantity : 0; + LogManager.Log($"Inserting job run for '{run.JobName}'...\n{run.ToJsonString(nameof(JobRun.JobString), nameof(JobRun.LiquidQuantityString))}"); - //Light Rubine - var lightRubine = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.LightRubine); - run.LightRubineQuantity = lightRubine != null ? lightRubine.Quantity : 0; + db.SaveChanges(); - //Navy - var navy = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Navy); - run.NavyQuantity = navy != null ? navy.Quantity : 0; + JobRunInfo jobRunInfo = new JobRunInfo(); + jobRunInfo.JobRunID = run.ID; + jobRunInfo.JobTicket = _jobTicket; + jobRunInfo.Events = _currentJobEvents.Select(x => MachinesEventDTO.FromObservable(x)).ToList(); + jobRunInfo.StartMachineStatus = _startMachineStatus; + jobRunInfo.EndMachineStatus = MachineOperator.MachineStatus?.Clone(); - //Violet - var violet = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Violet); - run.VioletQuantity = violet != null ? violet.Quantity : 0; + if (CreateJobRunsFiles) + { + try + { + Directory.CreateDirectory(JobRunsFolder); - if (exception != null) - { - run.FailedMessage = exception.FlattenMessage(); - } + String json = jobRunInfo.ToJsonString(); + File.WriteAllText(Path.Combine(JobRunsFolder, $"{run.ID}.run"), json); - if (_job.Designation == JobDesignations.FineTuning) - { - try - { - run.FineTuningString = JsonConvert.SerializeObject(_job.VectorFineTuningRunModel); - } - catch (Exception ex) - { - LogManager.Log(ex, "Error serializing fine tuning model for job run."); + LogManager.Log($"JobRun extended info file '{run.ID}' created."); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error creating job run extended info file."); + } } + + JobRunAvailable?.Invoke(this, new JobRunAvailableEventArgs() { JobRunInfo = jobRunInfo, JobRun = run }); } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error logging the last job run to the database."); + } + }); + } + } + } + else + { + using (ObservablesContext db = ObservablesContext.CreateDefault()) + { + try + { + var run = db.JobRuns.OrderByDescending(x => x.LastUpdated).FirstOrDefault(x => x.JobGuid == _job.Guid); - db.JobRuns.Add(run); + if (run == null) + { + LogManager.Log($"Error updating job run by resumed job. Could not locate the existing job run by job guid '{_job.Guid}'."); + return; + } - e.Job.LastRun = DateTime.UtcNow; - _job.LastRun = DateTime.UtcNow; + run.LastUpdated = DateTime.UtcNow; + run.EndDate = DateTime.UtcNow; + run.JobRunStatus = status; + run.EndPosition = e.JobHandler.Status.Progress; + run.JobLength = e.JobHandler.Status.TotalProgress; + run.ActualEndPosition = e.JobHandler.Status.ProgressMinusSettingUp; - var job = db.Jobs.SingleOrDefault(x => x.Guid == _job.Guid); + if (exception != null) + { + run.FailedMessage = exception.FlattenMessage(); + } + else + { + run.FailedMessage = null; + } - if (job != null) - { - job.LastRun = DateTime.UtcNow; - } + var newQuantities = e.LiquidQuantities.ToList(); + var oldQuantities = run.LiquidQuantities.ToList(); - LogManager.Log($"Inserting job run for '{run.JobName}'...\n{run.ToJsonString(nameof(JobRun.JobString), nameof(JobRun.LiquidQuantityString))}"); + SetJobRunLiquidQuantities(run, newQuantities, update: true); - db.SaveChanges(); + //Append liquid quantities string. + foreach (var newQuantity in newQuantities) + { + var oldQuantity = oldQuantities.FirstOrDefault(x => x.LiquidType == newQuantity.LiquidType); + if (oldQuantity != null) + { + oldQuantity.Quantity += newQuantity.Quantity; + } + else + { + oldQuantities.Add(newQuantity); + } + } - JobRunInfo jobRunInfo = new JobRunInfo(); - jobRunInfo.JobRunID = run.ID; - jobRunInfo.JobTicket = _jobTicket; - jobRunInfo.Events = _currentJobEvents.Select(x => MachinesEventDTO.FromObservable(x)).ToList(); - jobRunInfo.StartMachineStatus = _startMachineStatus; - jobRunInfo.EndMachineStatus = MachineOperator.MachineStatus?.Clone(); + run.LiquidQuantities = oldQuantities; - if (CreateJobRunsFiles) - { - try - { - Directory.CreateDirectory(JobRunsFolder); + db.SaveChanges(); - String json = jobRunInfo.ToJsonString(); - File.WriteAllText(Path.Combine(JobRunsFolder, $"{run.ID}.run"), json); + //TODO: Needs to resubmit this to Azure! Then in Azure Distinct by job run ID, by job guid, by latest! - LogManager.Log($"JobRun extended info file '{run.ID}' created."); - } - catch (Exception ex) - { - LogManager.Log(ex, "Error creating job run extended info file."); - } - } + try + { + String jobInfoPath = Path.Combine(JobRunsFolder, $"{run.ID}.run"); - JobRunAvailable?.Invoke(this, new JobRunAvailableEventArgs() { JobRunInfo = jobRunInfo, JobRun = run }); + if (File.Exists(jobInfoPath)) + { + String json = File.ReadAllText(jobInfoPath); + JobRunInfo info = JsonConvert.DeserializeObject<JobRunInfo>(json); + info.Events.AddRange(_currentJobEvents.Select(x => MachinesEventDTO.FromObservable(x)).ToList()); + info.EndMachineStatus = MachineOperator.MachineStatus?.Clone(); + File.WriteAllText(jobInfoPath, info.ToJsonString()); } } catch (Exception ex) { - LogManager.Log(ex, "Error logging the last job run to the database."); + LogManager.Log(ex, "Error while trying to update jobrun info file after job resume."); } - }); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error while trying to update jobrun by resumed job."); + } } } } @@ -359,47 +370,7 @@ namespace Tango.Integration.JobRuns run.JobLength = e.Length; run.LiquidQuantities = e.LiquidQuantities; - //Set individual liquid quantities - - //Cyan - var cyan = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Cyan); - run.CyanQuantity = cyan != null ? cyan.Quantity : 0; - - //Magenta - var magenta = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Magenta); - run.MagentaQuantity = magenta != null ? magenta.Quantity : 0; - - //Yellow - var yellow = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Yellow); - run.YellowQuantity = yellow != null ? yellow.Quantity : 0; - - //Black - var black = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Black); - run.BlackQuantity = black != null ? black.Quantity : 0; - - //TI - var ti = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.TransparentInk); - run.TransparentQuantity = ti != null ? ti.Quantity : 0; - - //Lubricant - var lubricant = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Lubricant); - run.LubricantQuantity = lubricant != null ? lubricant.Quantity : 0; - - //Cleaner - var cleaner = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.Cleaner); - run.CleanerQuantity = cleaner != null ? cleaner.Quantity : 0; - - //Light Cyan - var lightCyan = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.LightCyan); - run.LightCyanQuantity = lightCyan != null ? lightCyan.Quantity : 0; - - //Light Magenta - var lightMagenta = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.LightMagenta); - run.LightMagentaQuantity = lightMagenta != null ? lightMagenta.Quantity : 0; - - //Light Yellow - var lightYellow = run.LiquidQuantities.SingleOrDefault(x => x.LiquidType == LiquidTypes.LightYellow); - run.LightYellowQuantity = lightYellow != null ? lightYellow.Quantity : 0; + SetJobRunLiquidQuantities(run, run.LiquidQuantities); //if (exception != null) //{ @@ -421,6 +392,43 @@ namespace Tango.Integration.JobRuns } } + private void SetJobRunLiquidQuantities(JobRun run, List<BL.ValueObjects.JobRunLiquidQuantity> quantities, bool update = false) + { + if (run == null || quantities == null) + return; + + // Map each LiquidType to the corresponding JobRun property updater + var setters = new Dictionary<LiquidTypes, Action<long>> + { + { LiquidTypes.Cyan, q => run.CyanQuantity = update ? run.CyanQuantity + q : q }, + { LiquidTypes.Magenta, q => run.MagentaQuantity = update ? run.MagentaQuantity + q : q }, + { LiquidTypes.Yellow, q => run.YellowQuantity = update ? run.YellowQuantity + q : q }, + { LiquidTypes.Black, q => run.BlackQuantity = update ? run.BlackQuantity + q : q }, + { LiquidTypes.TransparentInk, q => run.TransparentQuantity = update ? run.TransparentQuantity + q : q }, + { LiquidTypes.Lubricant, q => run.LubricantQuantity = update ? run.LubricantQuantity + q : q }, + { LiquidTypes.Cleaner, q => run.CleanerQuantity = update ? run.CleanerQuantity + q : q }, + { LiquidTypes.LightCyan, q => run.LightCyanQuantity = update ? run.LightCyanQuantity + q : q }, + { LiquidTypes.LightMagenta, q => run.LightMagentaQuantity = update ? run.LightMagentaQuantity + q : q }, + { LiquidTypes.LightYellow, q => run.LightYellowQuantity = update ? run.LightYellowQuantity + q : q }, + { LiquidTypes.Blue, q => run.BlueQuantity = update ? run.BlueQuantity + q : q }, + { LiquidTypes.LightBlue, q => run.LightBlueQuantity = update ? run.LightBlueQuantity + q : q }, + { LiquidTypes.Orange, q => run.OrangeQuantity = update ? run.OrangeQuantity + q : q }, + { LiquidTypes.LightOrange, q => run.LightOrangeQuantity = update ? run.LightOrangeQuantity + q : q }, + { LiquidTypes.Rubine, q => run.RubineQuantity = update ? run.RubineQuantity + q : q }, + { LiquidTypes.LightRubine, q => run.LightRubineQuantity = update ? run.LightRubineQuantity + q : q }, + { LiquidTypes.Navy, q => run.NavyQuantity = update ? run.NavyQuantity + q : q }, + { LiquidTypes.Violet, q => run.VioletQuantity = update ? run.VioletQuantity + q : q } + }; + + foreach (var liquidType in setters.Keys) + { + var quantityObj = quantities.SingleOrDefault(x => x.LiquidType == liquidType); + var quantity = quantityObj?.Quantity ?? 0; + setters[liquidType](quantity); + } + } + + #endregion #region Public Methods @@ -469,29 +477,12 @@ namespace Tango.Integration.JobRuns InsertJobRun(e, JobRunStatus.Completed, null); } - private async void Machine_PrintingStarted(object sender, PrintingEventArgs e) + private void Machine_PrintingStarted(object sender, PrintingEventArgs e) { _job = e.Job; _currentJobEvents = new List<MachinesEvent>(); _startMachineStatus = MachineOperator.MachineStatus?.Clone(); _jobTicket = e.JobHandler.JobTicket; - - //if (_job.Designation == JobDesignations.FineTuning) return; - - if (e.IsResumed) - { - try - { - using (ObservablesContext db = ObservablesContext.CreateDefault()) - { - _job = await new JobBuilder(db).Set(e.Job.Guid).WithConfiguration().WithSegments().WithBrushStops().BuildAsync(); - } - } - catch (Exception ex) - { - LogManager.Log(ex, "Error loading resumed job from database."); - } - } } private void MachineOperator_HeadCleaningEnded(object sender, HeadCleaningEndedEventArgs e) |
