Remove ActivePrinter from serial ports, pass settings to port calls

- Add PrinterSettings to PortFactory Create/PortAvailable methods
- Add ApplicationController->LogInfo for status reporting
- Remove printer coupling in tcp/x3g for status reporting
- Issue MatterHackers/MCCentral#4549
Remove ActivePrinter from ApplicationController
This commit is contained in:
John Lewin 2018-11-30 12:35:56 -08:00
parent d850573340
commit bc4efaf18a
12 changed files with 116 additions and 71 deletions

View file

@ -1,12 +1,11 @@
using MatterHackers.MatterControl;
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.SerialPortCommunication.FrostedSerial;
using System;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using MatterHackers.MatterControl.PrinterCommunication;
using MatterHackers.MatterControl;
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.SerialPortCommunication.FrostedSerial;
namespace TcpipDriver
{
@ -42,18 +41,16 @@ namespace TcpipDriver
private int tempWriteTimeout;
private bool reconnecting = false;
PrinterConnection printerConnection;
private PrinterConfig printer;
private PrinterSettings settings;
public TcpipSerialPort(PrinterConfig printer, string name)
public TcpipSerialPort(PrinterSettings settings, string name)
{
this.printer = printer;
this.printerConnection = printer.Connection;
this.settings = settings;
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (int.TryParse(printer.Settings.GetValue("ip_port"), out port)
&& IPAddress.TryParse(printer.Settings.GetValue("ip_address"), out ipAddress))
if (int.TryParse(settings.GetValue("ip_port"), out port)
&& IPAddress.TryParse(settings.GetValue("ip_address"), out ipAddress))
{
ipEndPoint = new IPEndPoint(ipAddress, port);
readBuffer = new byte[1024];
@ -155,11 +152,12 @@ namespace TcpipDriver
public void Open()
{
try
{//ADD Attempt to connect Message to just the console
printerConnection.TerminalLog.WriteLine("Attempting to connect to: " + ipEndPoint.Address + " on port " + ipEndPoint.Port);
{
// Attempt to connect Message to just the console
this.LogInfo("Attempting to connect to: " + ipEndPoint.Address + " on port " + ipEndPoint.Port);
socket.Connect(ipEndPoint);
stream = new NetworkStream(socket);
printerConnection.TerminalLog.WriteLine("Connected to: " + ipEndPoint.Address + " on port " + ipEndPoint.Port);
this.LogInfo("Connected to: " + ipEndPoint.Address + " on port " + ipEndPoint.Port);
if (this.BaudRate != 0)
{
//Send Telnet handshake so that esp will enter the telnet mode allowing us to set baud and reset board
@ -170,8 +168,8 @@ namespace TcpipDriver
}
}
catch (Exception e)
{//ADD Error Message to just the console
printerConnection.TerminalLog.WriteLine("Exception:" + e.Message);
{
ApplicationController.Instance.LogError("Exception:" + e.Message);
}
//These were set before and are now set in the stream
@ -182,6 +180,11 @@ namespace TcpipDriver
}
}
private void LogInfo(string message)
{
ApplicationController.Instance.LogInfo(message);
}
public int Read(byte[] buffer, int offset, int count)
{
Array.Copy(readBuffer, offset, buffer, 0, count);
@ -216,7 +219,7 @@ namespace TcpipDriver
}
catch (Exception e)
{
printerConnection.TerminalLog.WriteLine("Exception:" + e.Message);
this.LogInfo("Exception:" + e.Message);
Reconnect();
stream.Write(buffer, offset, count);
}
@ -236,12 +239,13 @@ namespace TcpipDriver
private string ConvertBytesToString(byte[] inputBytes, int bytesRead)
{
StringBuilder builder = new StringBuilder();
var builder = new StringBuilder();
for (int index = 0; index < bytesRead; index++)
{
builder.Append(Convert.ToChar(inputBytes[index]));
}
return builder.ToString();
}
@ -259,19 +263,21 @@ namespace TcpipDriver
ipEndPoint = new IPEndPoint(ipAddress, port);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{//ADD Attempt to connect Message to just the console
printerConnection.TerminalLog.WriteLine("Attempting to connect to: " + ipEndPoint.Address + " on port " + ipEndPoint.Port);
{
// Attempt to connect Message to just the console
this.LogInfo("Attempting to connect to: " + ipEndPoint.Address + " on port " + ipEndPoint.Port);
socket.Connect(ipEndPoint);
stream = new NetworkStream(socket);
printerConnection.TerminalLog.WriteLine("Connected to: " + ipEndPoint.Address + " on port " + ipEndPoint.Port);
//Send telnet handshake
this.LogInfo("Connected to: " + ipEndPoint.Address + " on port " + ipEndPoint.Port);
// Send telnet handshake
byte[] bytes = new byte[] { IAC, WILL, ComPortOpt };
Write(bytes, 0, bytes.Length);
break;
}
catch (Exception e)
{//ADD Error Message to just the console
printerConnection.TerminalLog.WriteLine("Exception:" + e.Message);
{
ApplicationController.Instance.LogError("Exception:" + e.Message);
Thread.Sleep((int)(500 * Math.Pow(i,2)));
}
}

View file

@ -1,5 +1,4 @@
using System.Net;
using MatterHackers.MatterControl;
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.SerialPortCommunication.FrostedSerial;
@ -11,17 +10,15 @@ namespace TcpipDriver
protected override string GetDriverType() => "TCPIP";
public override IFrostedSerialPort Create(string serialPortName)
public override IFrostedSerialPort Create(string serialPortName, PrinterSettings settings)
{
return new TcpipSerialPort(ApplicationController.Instance.ActivePrinter, serialPortName);
return new TcpipSerialPort(settings, serialPortName);
}
public override bool SerialPortIsAvailable(string serialPortName)
public override bool SerialPortIsAvailable(string serialPortName, PrinterSettings settings)
{
var printer = ApplicationController.Instance.ActivePrinter;
return int.TryParse(printer.Settings.GetValue(SettingsKey.ip_port), out _)
&& IPAddress.TryParse(printer.Settings.GetValue(SettingsKey.ip_address), out _);
return int.TryParse(settings.GetValue(SettingsKey.ip_port), out _)
&& IPAddress.TryParse(settings.GetValue(SettingsKey.ip_address), out _);
}
}
}