2015-11-28 19:59:14 -08:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2015, Lars Brubaker
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
The views and conclusions contained in the software and documentation are those
|
|
|
|
|
|
of the authors and should not be interpreted as representing official policies,
|
|
|
|
|
|
either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2017-02-02 07:54:52 -08:00
|
|
|
|
using System.Diagnostics;
|
2017-02-02 16:38:48 -08:00
|
|
|
|
using System.IO;
|
2016-11-04 16:00:54 -07:00
|
|
|
|
using System.Text.RegularExpressions;
|
2017-02-02 07:54:52 -08:00
|
|
|
|
using System.Threading;
|
2017-02-02 16:38:48 -08:00
|
|
|
|
using MatterHackers.Agg.Image;
|
|
|
|
|
|
using MatterHackers.Agg.PlatformAbstract;
|
2016-11-04 16:00:54 -07:00
|
|
|
|
using MatterHackers.Agg.UI;
|
2017-02-02 07:54:52 -08:00
|
|
|
|
using MatterHackers.MatterControl.PrinterControls;
|
|
|
|
|
|
using MatterHackers.MatterControl.SlicerConfiguration;
|
2015-11-28 19:59:14 -08:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.PrinterCommunication.Io
|
|
|
|
|
|
{
|
2016-11-04 16:00:54 -07:00
|
|
|
|
public class QueuedCommandsStream : GCodeStreamProxy
|
|
|
|
|
|
{
|
2017-02-03 17:54:49 -08:00
|
|
|
|
public const string MacroPrefix = "; host.";
|
2017-02-02 07:54:52 -08:00
|
|
|
|
private List<string> commandQueue = new List<string>();
|
|
|
|
|
|
private object locker = new object();
|
|
|
|
|
|
private bool waitingForUserInput = false;
|
|
|
|
|
|
private double maxTimeToWaitForOk = 0;
|
2017-02-02 16:38:48 -08:00
|
|
|
|
private int repeatCommandIndex = 0;
|
|
|
|
|
|
private List<string> commandsToRepeat = new List<string>();
|
2017-02-02 07:54:52 -08:00
|
|
|
|
private Stopwatch timeHaveBeenWaiting = new Stopwatch();
|
2015-11-28 19:59:14 -08:00
|
|
|
|
|
2016-11-04 16:00:54 -07:00
|
|
|
|
public QueuedCommandsStream(GCodeStream internalStream)
|
|
|
|
|
|
: base(internalStream)
|
2016-01-19 15:16:05 -08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Add(string line)
|
2016-11-04 16:00:54 -07:00
|
|
|
|
{
|
|
|
|
|
|
// lock queue
|
|
|
|
|
|
lock (locker)
|
2015-12-31 09:40:17 -08:00
|
|
|
|
{
|
2016-11-04 16:00:54 -07:00
|
|
|
|
commandQueue.Add(line);
|
2015-12-31 09:40:17 -08:00
|
|
|
|
}
|
2016-11-04 16:00:54 -07:00
|
|
|
|
}
|
2015-12-31 09:40:17 -08:00
|
|
|
|
|
2017-02-02 07:54:52 -08:00
|
|
|
|
public void Cancel()
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Continue()
|
|
|
|
|
|
{
|
|
|
|
|
|
waitingForUserInput = false;
|
|
|
|
|
|
timeHaveBeenWaiting.Reset();
|
|
|
|
|
|
maxTimeToWaitForOk = 0;
|
2017-02-02 16:38:48 -08:00
|
|
|
|
commandsToRepeat.Clear();
|
2017-02-02 07:54:52 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-04 16:00:54 -07:00
|
|
|
|
public override string ReadLine()
|
|
|
|
|
|
{
|
|
|
|
|
|
string lineToSend = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (waitingForUserInput)
|
|
|
|
|
|
{
|
|
|
|
|
|
lineToSend = "";
|
2017-02-02 07:54:52 -08:00
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
|
|
|
|
|
|
|
if(timeHaveBeenWaiting.IsRunning
|
2017-02-02 16:38:48 -08:00
|
|
|
|
&& timeHaveBeenWaiting.Elapsed.TotalSeconds > maxTimeToWaitForOk)
|
2017-02-02 07:54:52 -08:00
|
|
|
|
{
|
2017-02-02 16:38:48 -08:00
|
|
|
|
if(commandsToRepeat.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// We timed out without the user responding. Cancel the operation.
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// everything normal continue after time waited
|
|
|
|
|
|
Continue();
|
|
|
|
|
|
}
|
2017-02-02 07:54:52 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (maxTimeToWaitForOk > 0
|
|
|
|
|
|
&& timeHaveBeenWaiting.Elapsed.TotalSeconds < maxTimeToWaitForOk
|
2017-02-02 16:38:48 -08:00
|
|
|
|
&& commandsToRepeat.Count > 0)
|
2017-02-02 07:54:52 -08:00
|
|
|
|
{
|
2017-02-02 16:38:48 -08:00
|
|
|
|
lineToSend = commandsToRepeat[repeatCommandIndex % commandsToRepeat.Count];
|
|
|
|
|
|
repeatCommandIndex++;
|
2017-02-02 07:54:52 -08:00
|
|
|
|
}
|
2016-11-04 16:00:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
else
|
2015-12-31 09:40:17 -08:00
|
|
|
|
{
|
2016-11-04 16:00:54 -07:00
|
|
|
|
// lock queue
|
|
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (commandQueue.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
lineToSend = commandQueue[0];
|
2016-11-29 15:14:38 -08:00
|
|
|
|
lineToSend = GCodeProcessing.ReplaceMacroValues(lineToSend);
|
2016-11-04 16:00:54 -07:00
|
|
|
|
commandQueue.RemoveAt(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (lineToSend != null)
|
|
|
|
|
|
{
|
2017-02-03 17:54:49 -08:00
|
|
|
|
if (lineToSend.StartsWith(MacroPrefix) && lineToSend.TrimEnd().EndsWith(")"))
|
2016-11-04 16:00:54 -07:00
|
|
|
|
{
|
2017-02-03 17:54:49 -08:00
|
|
|
|
int parensAfterCommand = lineToSend.IndexOf('(', MacroPrefix.Length);
|
|
|
|
|
|
string command = "";
|
|
|
|
|
|
if (parensAfterCommand > 0)
|
2016-11-04 16:00:54 -07:00
|
|
|
|
{
|
2017-02-03 17:54:49 -08:00
|
|
|
|
command = lineToSend.Substring(MacroPrefix.Length, parensAfterCommand - MacroPrefix.Length);
|
2016-11-04 16:00:54 -07:00
|
|
|
|
}
|
2017-02-03 17:54:49 -08:00
|
|
|
|
|
|
|
|
|
|
RunningMacroPage.MacroCommandData macroData = new RunningMacroPage.MacroCommandData();
|
|
|
|
|
|
|
|
|
|
|
|
string value = "";
|
|
|
|
|
|
if (TryGetAfterString(lineToSend, "title", out value))
|
2016-11-04 16:00:54 -07:00
|
|
|
|
{
|
2017-02-03 17:54:49 -08:00
|
|
|
|
macroData.title = value;
|
2016-11-04 16:00:54 -07:00
|
|
|
|
}
|
2017-02-03 17:54:49 -08:00
|
|
|
|
if (TryGetAfterString(lineToSend, "expire", out value))
|
2016-11-04 16:00:54 -07:00
|
|
|
|
{
|
2017-02-03 17:54:49 -08:00
|
|
|
|
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))
|
|
|
|
|
|
{
|
2017-02-06 12:19:37 -08:00
|
|
|
|
foreach(string line in value.Split('|'))
|
2017-02-03 17:54:49 -08:00
|
|
|
|
{
|
|
|
|
|
|
commandsToRepeat.Add(line);
|
|
|
|
|
|
}
|
2016-11-04 16:00:54 -07:00
|
|
|
|
}
|
2017-02-02 16:38:48 -08:00
|
|
|
|
|
2016-11-04 16:00:54 -07:00
|
|
|
|
switch (command)
|
|
|
|
|
|
{
|
2017-02-03 17:54:49 -08:00
|
|
|
|
case "choose_material":
|
2017-02-02 07:54:52 -08:00
|
|
|
|
waitingForUserInput = true;
|
2017-02-03 17:54:49 -08:00
|
|
|
|
macroData.showMaterialSelector = true;
|
|
|
|
|
|
macroData.waitOk = true;
|
|
|
|
|
|
UiThread.RunOnIdle(() => RunningMacroPage.Show(macroData));
|
2017-02-02 07:54:52 -08:00
|
|
|
|
break;
|
|
|
|
|
|
|
2017-02-03 17:54:49 -08:00
|
|
|
|
case "close":
|
2017-02-02 07:54:52 -08:00
|
|
|
|
UiThread.RunOnIdle(() => WizardWindow.Close("Macro"));
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2017-02-03 17:54:49 -08:00
|
|
|
|
case "ding":
|
2017-02-03 14:41:18 -08:00
|
|
|
|
MatterControlApplication.Instance.PlaySound("timer-done.wav");
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2017-02-03 17:54:49 -08:00
|
|
|
|
case "show_message":
|
2017-02-06 12:19:37 -08:00
|
|
|
|
waitingForUserInput = macroData.waitOk | macroData.expireTime > 0;
|
2017-02-03 17:54:49 -08:00
|
|
|
|
UiThread.RunOnIdle(() => RunningMacroPage.Show(macroData));
|
2016-11-04 16:00:54 -07:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
// Don't know the command. Print to terminal log?
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
lineToSend = base.ReadLine();
|
|
|
|
|
|
}
|
2015-12-31 09:40:17 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return lineToSend;
|
2016-11-04 16:00:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-03 17:54:49 -08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-03 14:41:18 -08:00
|
|
|
|
public ImageBuffer LoadImageAsset(string uri)
|
2017-02-02 16:38:48 -08:00
|
|
|
|
{
|
2017-02-03 14:41:18 -08:00
|
|
|
|
string filePath = Path.Combine("Images", "Macros", uri);
|
|
|
|
|
|
bool imageOnDisk = false;
|
2017-02-03 17:54:49 -08:00
|
|
|
|
if (uri.IndexOfAny(Path.GetInvalidFileNameChars()) == -1)
|
2017-02-03 14:41:18 -08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
imageOnDisk = StaticData.Instance.FileExists(filePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
imageOnDisk = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (imageOnDisk)
|
2017-02-02 16:38:48 -08:00
|
|
|
|
{
|
|
|
|
|
|
return StaticData.Instance.LoadImage(filePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-02-03 14:41:18 -08:00
|
|
|
|
var imageBuffer = new ImageBuffer(320, 10);
|
2017-02-02 16:38:48 -08:00
|
|
|
|
|
2017-02-03 14:41:18 -08:00
|
|
|
|
ApplicationController.Instance.DownloadToImageAsync(imageBuffer, uri, true);
|
2017-02-02 16:38:48 -08:00
|
|
|
|
|
|
|
|
|
|
return imageBuffer;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-29 14:17:37 -08:00
|
|
|
|
public void Reset()
|
2016-11-04 16:00:54 -07:00
|
|
|
|
{
|
|
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
commandQueue.Clear();
|
|
|
|
|
|
}
|
2016-11-29 14:17:37 -08:00
|
|
|
|
|
|
|
|
|
|
waitingForUserInput = false;
|
2017-02-02 16:38:48 -08:00
|
|
|
|
timeHaveBeenWaiting.Reset();
|
|
|
|
|
|
maxTimeToWaitForOk = 0;
|
|
|
|
|
|
UiThread.RunOnIdle(() => WizardWindow.Close("Macro"));
|
2016-11-29 14:17:37 -08:00
|
|
|
|
}
|
2016-11-04 16:00:54 -07:00
|
|
|
|
}
|
2015-11-28 19:59:14 -08:00
|
|
|
|
}
|