Refactored the macro command language.
This commit is contained in:
parent
97dffa1d53
commit
53b7576b17
2 changed files with 75 additions and 65 deletions
|
|
@ -35,7 +35,6 @@ using System.Threading;
|
|||
using MatterHackers.Agg.Image;
|
||||
using MatterHackers.Agg.PlatformAbstract;
|
||||
using MatterHackers.Agg.UI;
|
||||
using MatterHackers.GCodeVisualizer;
|
||||
using MatterHackers.MatterControl.PrinterControls;
|
||||
using MatterHackers.MatterControl.SlicerConfiguration;
|
||||
|
||||
|
|
@ -43,7 +42,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
|
|||
{
|
||||
public class QueuedCommandsStream : GCodeStreamProxy
|
||||
{
|
||||
public const string MacroPrefix = "; Command:";
|
||||
public const string MacroPrefix = "; host.";
|
||||
private List<string> commandQueue = new List<string>();
|
||||
private object locker = new object();
|
||||
private bool waitingForUserInput = false;
|
||||
|
|
@ -126,75 +125,67 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
|
|||
|
||||
if (lineToSend != null)
|
||||
{
|
||||
if (lineToSend.StartsWith(MacroPrefix))
|
||||
if (lineToSend.StartsWith(MacroPrefix) && lineToSend.TrimEnd().EndsWith(")"))
|
||||
{
|
||||
int spaceAfterCommand = lineToSend.IndexOf(' ', MacroPrefix.Length);
|
||||
string command;
|
||||
if (spaceAfterCommand > 0)
|
||||
int parensAfterCommand = lineToSend.IndexOf('(', MacroPrefix.Length);
|
||||
string command = "";
|
||||
if (parensAfterCommand > 0)
|
||||
{
|
||||
command = lineToSend.Substring(MacroPrefix.Length, spaceAfterCommand - MacroPrefix.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
command = lineToSend.Substring(MacroPrefix.Length);
|
||||
command = lineToSend.Substring(MacroPrefix.Length, parensAfterCommand - MacroPrefix.Length);
|
||||
}
|
||||
|
||||
List<string> messages = new List<string>();
|
||||
foreach (Match match in Regex.Matches(lineToSend, "\"([^\"]*)\""))
|
||||
{
|
||||
string matchString = match.ToString();
|
||||
messages.Add(matchString.Substring(1, matchString.Length - 2));
|
||||
}
|
||||
RunningMacroPage.MacroCommandData macroData = new RunningMacroPage.MacroCommandData();
|
||||
|
||||
var macroMatch = Regex.Match(lineToSend, "MacroImage:([^\\s]+)\\s*");
|
||||
var macroImage = macroMatch.Success ? LoadImageAsset(macroMatch.Groups[1].Value) : null;
|
||||
string value = "";
|
||||
if (TryGetAfterString(lineToSend, "title", out value))
|
||||
{
|
||||
macroData.title = value;
|
||||
}
|
||||
if (TryGetAfterString(lineToSend, "expire", out value))
|
||||
{
|
||||
double.TryParse(value, out macroData.expireTime);
|
||||
maxTimeToWaitForOk = macroData.expireTime;
|
||||
}
|
||||
if (TryGetAfterString(lineToSend, "count_down", out value))
|
||||
{
|
||||
double.TryParse(value, out macroData.countDown);
|
||||
}
|
||||
if (TryGetAfterString(lineToSend, "image", out value))
|
||||
{
|
||||
macroData.image = LoadImageAsset(value);
|
||||
}
|
||||
if (TryGetAfterString(lineToSend, "wait_ok", out value))
|
||||
{
|
||||
macroData.waitOk = value == "true";
|
||||
}
|
||||
if (TryGetAfterString(lineToSend, "repeat_gcode", out value))
|
||||
{
|
||||
foreach(string line in value.Split('\n'))
|
||||
{
|
||||
commandsToRepeat.Add(line);
|
||||
}
|
||||
}
|
||||
|
||||
switch (command)
|
||||
{
|
||||
case "ChooseMaterial":
|
||||
case "choose_material":
|
||||
waitingForUserInput = true;
|
||||
UiThread.RunOnIdle(() => RunningMacroPage.Show(messages.Count > 0 ? messages[0] : "", true, true));
|
||||
macroData.showMaterialSelector = true;
|
||||
macroData.waitOk = true;
|
||||
UiThread.RunOnIdle(() => RunningMacroPage.Show(macroData));
|
||||
break;
|
||||
|
||||
case "Close":
|
||||
case "close":
|
||||
UiThread.RunOnIdle(() => WizardWindow.Close("Macro"));
|
||||
break;
|
||||
|
||||
case "Ding":
|
||||
case "ding":
|
||||
MatterControlApplication.Instance.PlaySound("timer-done.wav");
|
||||
break;
|
||||
|
||||
case "Message":
|
||||
if (messages.Count > 0)
|
||||
{
|
||||
double seconds = 0;
|
||||
GCodeFile.GetFirstNumberAfter("ExpectedSeconds:", lineToSend, ref seconds);
|
||||
double temperature = 0;
|
||||
GCodeFile.GetFirstNumberAfter("ExpectedTemperature:", lineToSend, ref temperature);
|
||||
UiThread.RunOnIdle(() => RunningMacroPage.Show(messages[0], expectedSeconds: seconds, expectedTemperature: temperature, image: macroImage));
|
||||
}
|
||||
break;
|
||||
|
||||
case "RepeatUntil": // Repeat a command until the user clicks or or the max time elapses.
|
||||
if(messages.Count > 1)
|
||||
{
|
||||
double seconds = 10;
|
||||
GCodeFile.GetFirstNumberAfter("ExpectedSeconds:", lineToSend, ref seconds);
|
||||
timeHaveBeenWaiting.Restart();
|
||||
maxTimeToWaitForOk = seconds;
|
||||
waitingForUserInput = true;
|
||||
for (int i = 1; i < messages.Count; i++)
|
||||
{
|
||||
commandsToRepeat.Add(messages[i]);
|
||||
}
|
||||
|
||||
UiThread.RunOnIdle(() => RunningMacroPage.Show(messages[0], true, expectedSeconds: seconds, image: macroImage));
|
||||
}
|
||||
break;
|
||||
|
||||
case "WaitOK":
|
||||
waitingForUserInput = true;
|
||||
UiThread.RunOnIdle(() => RunningMacroPage.Show(messages.Count > 0 ? messages[0] : "", true, image: macroImage));
|
||||
case "show_message":
|
||||
waitingForUserInput = macroData.waitOk;
|
||||
UiThread.RunOnIdle(() => RunningMacroPage.Show(macroData));
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
@ -212,11 +203,19 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
|
|||
return lineToSend;
|
||||
}
|
||||
|
||||
private bool TryGetAfterString(string macroLine, string variableName, out string value)
|
||||
{
|
||||
var macroMatch = Regex.Match(macroLine, variableName + ":\"([^\"]+)");
|
||||
value = macroMatch.Success ? macroMatch.Groups[1].Value : null;
|
||||
|
||||
return macroMatch.Success;
|
||||
}
|
||||
|
||||
public ImageBuffer LoadImageAsset(string uri)
|
||||
{
|
||||
string filePath = Path.Combine("Images", "Macros", uri);
|
||||
bool imageOnDisk = false;
|
||||
if (uri.IndexOfAny(Path.GetInvalidFileNameChars()) == 0)
|
||||
if (uri.IndexOfAny(Path.GetInvalidFileNameChars()) == -1)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue