Merge pull request #2900 from jlewin/design_tools

Extract DB sync to dedicated thread
This commit is contained in:
johnlewin 2018-01-16 14:08:02 -08:00 committed by GitHub
commit 223edd580c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 76 deletions

View file

@ -33,6 +33,7 @@ using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.ConfigurationPage.PrintLeveling;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.PrinterCommunication;
using MatterHackers.MatterControl.SlicerConfiguration;
@ -51,10 +52,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
this.printer = printer;
// add the finish setup button
finishSetupButton = theme.ButtonFactory.Generate("Setup...".Localize(), AggContext.StaticData.LoadIcon("icon_play_32x32.png", 14, 14, IconColor.Theme));
finishSetupButton.Name = "Finish Setup Button";
finishSetupButton.ToolTipText = "Run setup configuration for printer.".Localize();
finishSetupButton.Margin = theme.ButtonSpacing;
finishSetupButton = new TextButton("Setup...".Localize(), theme)
{
Name = "Finish Setup Button",
ToolTipText = "Run setup configuration for printer.".Localize(),
Margin = theme.ButtonSpacing,
BackgroundColor = theme.ButtonFactory.Options.NormalFillColor,
HoverColor = theme.ButtonFactory.Options.HoverFillColor,
};
finishSetupButton.Click += (s, e) =>
{
UiThread.RunOnIdle(async () =>

View file

@ -1879,6 +1879,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication
}
}
private CancellationTokenSource printingCancellation;
public async void StartPrint(string gcodeFilename, PrintTask printTaskToUse = null)
{
if (!PrinterIsConnected || PrinterIsPrinting)
@ -1886,6 +1888,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication
return;
}
printingCancellation = new CancellationTokenSource();
haveReportedError = false;
PrintWasCanceled = false;
@ -1897,9 +1901,53 @@ namespace MatterHackers.MatterControl.PrinterCommunication
await Task.Run(() =>
{
LoadGCodeToPrint(gcodeFilename);
// LoadGCodeToPrint
CreateStreamProcessors(gcodeFilename, this.RecoveryIsEnabled);
});
DoneLoadingGCodeToPrint();
// DoneLoadingGCodeToPrint
switch (communicationState)
{
case CommunicationStates.Connected:
// This can happen if the printer is reset during the slicing of the part.
break;
case CommunicationStates.PreparingToPrint:
{
var activePrintItem = printer.Bed.EditContext.printItem;
if (activePrintItem.PrintItem.Id == 0)
{
activePrintItem.PrintItem.Commit();
}
if (activePrintTask == null)
{
// TODO: Fix printerItemID int requirement
activePrintTask = new PrintTask
{
PrintStart = DateTime.Now,
PrinterId = this.printer.Settings.ID.GetHashCode(),
PrintName = activePrintItem.PrintItem.Name,
PrintItemId = activePrintItem.PrintItem.Id,
PrintingGCodeFileName = activePrintItem.GetGCodePathAndFileName(),
PrintComplete = false
};
activePrintTask.Commit();
Task.Run(() => this.SyncProgressToDB(printingCancellation.Token)).ConfigureAwait(false);
}
}
CommunicationState = CommunicationStates.Printing;
break;
default:
#if DEBUG
throw new Exception("We are not preparing to print so we should not be starting to print");
#endif
break;
}
}
public bool StartSdCardPrint(string m23FileName)
@ -1975,6 +2023,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication
{
lock (locker)
{
// Flag as canceled, wait briefly for listening threads to catch up
printingCancellation.Cancel();
Thread.Sleep(15);
// get rid of all the gcode we have left to print
ClearQueuedGCode();
@ -2143,53 +2194,47 @@ namespace MatterHackers.MatterControl.PrinterCommunication
ReadPosition();
}
private void LoadGCodeToPrint(string gcodeFilename)
private void SyncProgressToDB(CancellationToken cancellationToken)
{
CreateStreamProcessors(gcodeFilename, this.RecoveryIsEnabled);
}
//var timer = Stopwatch.StartNew();
private void DoneLoadingGCodeToPrint()
{
switch (communicationState)
while (!cancellationToken.IsCancellationRequested
&& this.CommunicationState != CommunicationStates.FinishedPrint
&& this.communicationState != CommunicationStates.Connected)
{
case CommunicationStates.Connected:
// This can happen if the printer is reset during the slicing of the part.
break;
double secondsSinceStartedPrint = timeSinceStartedPrint.Elapsed.TotalSeconds;
case CommunicationStates.PreparingToPrint:
if (timeSinceStartedPrint.Elapsed.TotalSeconds > 0
&& gCodeFileStream0 != null
&& (secondsSinceUpdateHistory > secondsSinceStartedPrint
|| secondsSinceUpdateHistory + 1 < secondsSinceStartedPrint
|| lineSinceUpdateHistory + 20 < gCodeFileStream0.LineIndex))
{
double currentDone = gCodeFileStream0.GCodeFile.PercentComplete(gCodeFileStream0.LineIndex);
// Only update the amount done if it is greater than what is recorded.
// We don't want to mess up the resume before we actually resume it.
if (activePrintTask != null
&& babyStepsStream7 != null
&& activePrintTask.PercentDone < currentDone)
{
var activePrintItem = printer.Bed.EditContext.printItem;
if (activePrintItem.PrintItem.Id == 0)
{
activePrintItem.PrintItem.Commit();
}
activePrintTask.PercentDone = currentDone;
activePrintTask.PrintingOffsetX = (float)babyStepsStream7.Offset.X;
activePrintTask.PrintingOffsetY = (float)babyStepsStream7.Offset.Y;
activePrintTask.PrintingOffsetZ = (float)babyStepsStream7.Offset.Z;
activePrintTask?.Commit();
if (activePrintTask == null)
{
// TODO: Fix printerItemID int requirement
activePrintTask = new PrintTask
{
PrintStart = DateTime.Now,
PrinterId = this.printer.Settings.ID.GetHashCode(),
PrintName = activePrintItem.PrintItem.Name,
PrintItemId = activePrintItem.PrintItem.Id,
PrintingGCodeFileName = activePrintItem.GetGCodePathAndFileName(),
PrintComplete = false
};
activePrintTask.Commit();
}
// Interval looks to be ~10ms
//Console.WriteLine("DB write: {0}ms", timer.ElapsedMilliseconds);
//timer.Restart();
}
secondsSinceUpdateHistory = secondsSinceStartedPrint;
lineSinceUpdateHistory = gCodeFileStream0.LineIndex;
}
CommunicationState = CommunicationStates.Printing;
break;
default:
#if DEBUG
throw new Exception("We are not preparing to print so we should not be starting to print");
#endif
break;
Thread.Sleep(5);
}
// Console.WriteLine("Syncing print to db stopped");
}
private void MovementWasSetToAbsoluteMode(object sender, EventArgs e)
@ -2356,37 +2401,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication
waitingForPosition.Restart();
}
double secondsSinceStartedPrint = timeSinceStartedPrint.Elapsed.TotalSeconds;
if (timeSinceStartedPrint.Elapsed.TotalSeconds > 0
&& gCodeFileStream0 != null
&& (secondsSinceUpdateHistory > secondsSinceStartedPrint
|| secondsSinceUpdateHistory + 1 < secondsSinceStartedPrint
|| lineSinceUpdateHistory + 20 < gCodeFileStream0.LineIndex))
{
double currentDone = gCodeFileStream0.GCodeFile.PercentComplete(gCodeFileStream0.LineIndex);
// Only update the amount done if it is greater than what is recorded.
// We don't want to mess up the resume before we actually resume it.
if (activePrintTask != null
&& babyStepsStream7 != null
&& activePrintTask.PercentDone < currentDone)
{
activePrintTask.PercentDone = currentDone;
activePrintTask.PrintingOffsetX = (float)babyStepsStream7.Offset.X;
activePrintTask.PrintingOffsetY = (float)babyStepsStream7.Offset.Y;
activePrintTask.PrintingOffsetZ = (float)babyStepsStream7.Offset.Z;
try
{
Task.Run(() => activePrintTask?.Commit());
}
catch
{
// Can't write for some reason, continue with the write.
}
}
secondsSinceUpdateHistory = secondsSinceStartedPrint;
lineSinceUpdateHistory = gCodeFileStream0.LineIndex;
}
currentSentLine = currentSentLine.Trim();
// Check if there is anything in front of the ;.
if (currentSentLine.Split(';')[0].Trim().Length > 0)

@ -1 +1 @@
Subproject commit 34251ebdeb15fe6df98daf569770d09314f1d80f
Subproject commit eb8c3919cef843eabb7dfedb2a65c10398e9ddf0