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.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2019-03-01 19:38:59 -08:00
|
|
|
|
using System;
|
2015-11-28 19:59:14 -08:00
|
|
|
|
using System.Collections.Generic;
|
2017-02-02 16:38:48 -08:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using MatterHackers.Agg.Image;
|
2017-08-20 02:34:39 -07:00
|
|
|
|
using MatterHackers.Agg.Platform;
|
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-02 07:54:52 -08:00
|
|
|
|
private List<string> commandQueue = new List<string>();
|
|
|
|
|
|
private object locker = new object();
|
2015-11-28 19:59:14 -08:00
|
|
|
|
|
2018-11-12 15:02:47 -08:00
|
|
|
|
public QueuedCommandsStream(PrinterConfig printer, GCodeStream internalStream)
|
|
|
|
|
|
: base(printer, internalStream)
|
2016-01-19 15:16:05 -08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-02 16:14:37 -07:00
|
|
|
|
public int Count => commandQueue.Count;
|
|
|
|
|
|
|
2019-02-27 18:20:27 -08:00
|
|
|
|
public override string DebugInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2019-03-01 19:38:59 -08:00
|
|
|
|
return "";
|
2019-02-27 18:20:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-26 15:53:02 -07:00
|
|
|
|
public string Peek()
|
2019-04-01 15:51:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
// lock queue
|
|
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (commandQueue.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return commandQueue[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-01 08:46:05 -07:00
|
|
|
|
public string LastAdd()
|
|
|
|
|
|
{
|
|
|
|
|
|
// lock queue
|
|
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (commandQueue.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return commandQueue[commandQueue.Count - 1];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-01 19:38:59 -08:00
|
|
|
|
public void Add(string lineIn, bool forceTopOfQueue = false)
|
2016-11-04 16:00:54 -07:00
|
|
|
|
{
|
|
|
|
|
|
// lock queue
|
|
|
|
|
|
lock (locker)
|
2015-12-31 09:40:17 -08:00
|
|
|
|
{
|
2019-03-01 19:38:59 -08:00
|
|
|
|
if (lineIn.Contains("\\n"))
|
|
|
|
|
|
{
|
|
|
|
|
|
lineIn = lineIn.Replace("\\n", "\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-26 10:01:02 -07:00
|
|
|
|
// Check line for line breaks, split and process separate if necessary
|
2019-03-01 19:38:59 -08:00
|
|
|
|
if (lineIn.Contains("\n"))
|
|
|
|
|
|
{
|
|
|
|
|
|
string[] linesToWrite = lineIn.Split(new string[] { "\n" }, StringSplitOptions.None);
|
|
|
|
|
|
for (int i = 0; i < linesToWrite.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
string line = linesToWrite[i].Trim();
|
|
|
|
|
|
if (line.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Add(line);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-04-26 10:01:02 -07:00
|
|
|
|
|
2019-03-01 19:38:59 -08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-11 10:41:23 -07:00
|
|
|
|
if (forceTopOfQueue)
|
|
|
|
|
|
{
|
2019-03-01 19:38:59 -08:00
|
|
|
|
commandQueue.Insert(0, lineIn);
|
2017-04-11 10:41:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-03-01 19:38:59 -08:00
|
|
|
|
commandQueue.Add(lineIn);
|
2019-02-08 13:50:48 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-02-07 14:28:22 -08:00
|
|
|
|
|
2017-02-02 07:54:52 -08:00
|
|
|
|
public void Cancel()
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-04 16:00:54 -07:00
|
|
|
|
public override string ReadLine()
|
|
|
|
|
|
{
|
|
|
|
|
|
string lineToSend = null;
|
|
|
|
|
|
|
2019-02-08 13:50:48 -08:00
|
|
|
|
// lock queue
|
|
|
|
|
|
lock (locker)
|
2015-12-31 09:40:17 -08:00
|
|
|
|
{
|
2019-02-08 13:50:48 -08:00
|
|
|
|
if (commandQueue.Count > 0)
|
2016-11-04 16:00:54 -07:00
|
|
|
|
{
|
2019-02-08 13:50:48 -08:00
|
|
|
|
lineToSend = commandQueue[0];
|
2019-06-26 12:43:44 -07:00
|
|
|
|
lineToSend = printer.Settings.ReplaceMacroValues(lineToSend);
|
2019-02-08 13:50:48 -08:00
|
|
|
|
commandQueue.RemoveAt(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (lineToSend == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
lineToSend = base.ReadLine();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-08 17:53:32 -08:00
|
|
|
|
return lineToSend;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-06 14:17:49 -08:00
|
|
|
|
public void Reset()
|
2017-02-03 17:54:49 -08:00
|
|
|
|
{
|
2017-02-06 14:17:49 -08:00
|
|
|
|
lock (locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
commandQueue.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-11-04 16:00:54 -07:00
|
|
|
|
}
|
2015-11-28 19:59:14 -08:00
|
|
|
|
}
|