2017-08-23 23:59:45 -07:00
|
|
|
|
/*
|
2019-04-05 13:18:32 -07:00
|
|
|
|
Copyright (c) 2019, Lars Brubaker, John Lewin
|
2017-08-23 23:59:45 -07:00
|
|
|
|
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;
|
2018-11-01 14:07:21 -07:00
|
|
|
|
using System.IO;
|
2017-08-23 23:59:45 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using MatterHackers.Agg;
|
2018-11-01 14:07:21 -07:00
|
|
|
|
using MatterHackers.Agg.Platform;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
using MatterHackers.Agg.UI;
|
2014-02-04 13:30:42 -08:00
|
|
|
|
using MatterHackers.Localizations;
|
2014-06-11 14:52:58 -07:00
|
|
|
|
using MatterHackers.MatterControl.PrinterCommunication;
|
2014-07-28 13:48:28 -07:00
|
|
|
|
using MatterHackers.SerialPortCommunication.FrostedSerial;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
|
|
|
|
|
|
{
|
2017-11-08 15:56:37 -08:00
|
|
|
|
public class SetupStepComPortTwo : DialogPage
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
|
|
|
|
|
private string[] startingPortNames;
|
2017-10-18 09:24:29 -07:00
|
|
|
|
|
2018-04-14 20:51:01 -07:00
|
|
|
|
private GuiWidget nextButton;
|
|
|
|
|
|
private GuiWidget connectButton;
|
2021-11-30 11:54:36 -08:00
|
|
|
|
private TextWidget printerConnectionMessage;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-09-17 21:08:16 -07:00
|
|
|
|
private PrinterConfig printer;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-09-17 21:08:16 -07:00
|
|
|
|
public SetupStepComPortTwo(PrinterConfig printer)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2017-09-17 21:08:16 -07:00
|
|
|
|
this.printer = printer;
|
|
|
|
|
|
|
2015-03-11 10:30:46 -07:00
|
|
|
|
startingPortNames = FrostedSerialPort.GetPortNames();
|
2018-11-03 10:12:27 -07:00
|
|
|
|
contentRow.AddChild(createPrinterConnectionMessageContainer());
|
2017-08-23 23:54:31 -07:00
|
|
|
|
|
2017-10-18 09:24:29 -07:00
|
|
|
|
//Construct buttons
|
2018-04-14 20:51:01 -07:00
|
|
|
|
nextButton = theme.CreateDialogButton("Done".Localize());
|
2019-06-28 22:22:58 -07:00
|
|
|
|
nextButton.Click += (s, e) => Parent.Close();
|
2017-10-18 09:24:29 -07:00
|
|
|
|
nextButton.Visible = false;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
2021-11-30 11:54:36 -08:00
|
|
|
|
var connectButtonHasBeenClicked = false;
|
|
|
|
|
|
void CheckOnPorts()
|
|
|
|
|
|
{
|
|
|
|
|
|
string candidatePort = FrostedSerialPort.GetPortNames().Except(startingPortNames).FirstOrDefault();
|
|
|
|
|
|
if (candidatePort != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// we found a new added port click the connect button for the user
|
|
|
|
|
|
connectButton.InvokeClick();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (!connectButtonHasBeenClicked && this.ActuallyVisibleOnScreen())
|
|
|
|
|
|
{
|
|
|
|
|
|
// keep checking as long as this is open
|
|
|
|
|
|
UiThread.RunOnIdle(CheckOnPorts, .2);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UiThread.RunOnIdle(CheckOnPorts, .2);
|
|
|
|
|
|
|
2018-04-14 20:51:01 -07:00
|
|
|
|
connectButton = theme.CreateDialogButton("Connect".Localize());
|
2017-10-18 09:24:29 -07:00
|
|
|
|
connectButton.Click += (s, e) =>
|
|
|
|
|
|
{
|
2021-11-30 11:54:36 -08:00
|
|
|
|
connectButtonHasBeenClicked = true;
|
2017-10-18 09:24:29 -07:00
|
|
|
|
// Select the first port that's in GetPortNames() but not in startingPortNames
|
|
|
|
|
|
string candidatePort = FrostedSerialPort.GetPortNames().Except(startingPortNames).FirstOrDefault();
|
|
|
|
|
|
if (candidatePort == null)
|
2017-08-23 23:54:31 -07:00
|
|
|
|
{
|
2021-11-30 11:54:36 -08:00
|
|
|
|
printerConnectionMessage.TextColor = Color.Red;
|
|
|
|
|
|
printerConnectionMessage.Text = "Oops! Printer could not be detected ".Localize();
|
2017-10-18 09:24:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-11-30 11:54:36 -08:00
|
|
|
|
printerConnectionMessage.TextColor = theme.TextColor;
|
|
|
|
|
|
printerConnectionMessage.Text = "Attempting to connect".Localize() + "...";
|
2017-10-18 09:24:29 -07:00
|
|
|
|
|
2018-10-05 09:24:57 -07:00
|
|
|
|
printer.Settings.Helpers.SetComPort(candidatePort);
|
2017-10-18 09:24:29 -07:00
|
|
|
|
printer.Connection.Connect();
|
|
|
|
|
|
connectButton.Visible = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2018-11-01 14:07:21 -07:00
|
|
|
|
var backButton = theme.CreateDialogButton("<< Back".Localize());
|
|
|
|
|
|
backButton.Click += (s, e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
DialogWindow.ChangeToPage(new SetupStepComPortOne(printer));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-10-18 09:24:29 -07:00
|
|
|
|
this.AddPageAction(nextButton);
|
2018-11-01 14:07:21 -07:00
|
|
|
|
this.AddPageAction(backButton);
|
2017-10-18 09:24:29 -07:00
|
|
|
|
this.AddPageAction(connectButton);
|
2018-11-16 08:44:56 -08:00
|
|
|
|
|
|
|
|
|
|
// Register listeners
|
|
|
|
|
|
printer.Connection.CommunicationStateChanged += Connection_CommunicationStateChanged;
|
2017-10-18 09:24:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnCancel(out bool abortCancel)
|
|
|
|
|
|
{
|
|
|
|
|
|
printer.Connection.HaltConnectionThread();
|
|
|
|
|
|
abortCancel = false;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-23 16:44:11 -07:00
|
|
|
|
public override void OnClosed(EventArgs e)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2018-11-16 08:44:56 -08:00
|
|
|
|
// Unregister listeners
|
|
|
|
|
|
printer.Connection.CommunicationStateChanged -= Connection_CommunicationStateChanged;
|
|
|
|
|
|
|
2015-04-08 15:20:10 -07:00
|
|
|
|
base.OnClosed(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public FlowLayoutWidget createPrinterConnectionMessageContainer()
|
|
|
|
|
|
{
|
2019-04-05 13:18:32 -07:00
|
|
|
|
var container = new FlowLayoutWidget(FlowDirection.TopToBottom)
|
|
|
|
|
|
{
|
|
|
|
|
|
VAnchor = VAnchor.Stretch,
|
|
|
|
|
|
Margin = new BorderDouble(5)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var elementMargin = new BorderDouble(top: 5);
|
|
|
|
|
|
|
2021-11-30 11:54:36 -08:00
|
|
|
|
var printerMessageOne = new TextWidget("MatterControl will now attempt to auto-detect your printer.".Localize(), 0, 0, 10)
|
2019-04-05 13:18:32 -07:00
|
|
|
|
{
|
|
|
|
|
|
Margin = elementMargin,
|
|
|
|
|
|
TextColor = theme.TextColor,
|
|
|
|
|
|
HAnchor = HAnchor.Stretch
|
|
|
|
|
|
};
|
|
|
|
|
|
container.AddChild(printerMessageOne);
|
|
|
|
|
|
|
2021-11-30 11:54:36 -08:00
|
|
|
|
var printerMessageFour = new TextWidget(string.Format("1.) {0}.", "Plug in printer USB cable and turn printer on".Localize()), 0, 0, 12)
|
2019-04-05 13:18:32 -07:00
|
|
|
|
{
|
|
|
|
|
|
TextColor = theme.TextColor,
|
|
|
|
|
|
HAnchor = HAnchor.Stretch,
|
|
|
|
|
|
Margin = elementMargin
|
|
|
|
|
|
};
|
|
|
|
|
|
container.AddChild(printerMessageFour);
|
|
|
|
|
|
|
2021-11-30 11:54:36 -08:00
|
|
|
|
printerConnectionMessage = new TextWidget("", 0, 0, 10)
|
2017-09-17 21:08:16 -07:00
|
|
|
|
{
|
|
|
|
|
|
AutoExpandBoundsToText = true,
|
2017-10-31 11:43:25 -07:00
|
|
|
|
TextColor = Color.Red,
|
2017-09-17 21:08:16 -07:00
|
|
|
|
HAnchor = HAnchor.Stretch,
|
|
|
|
|
|
Margin = elementMargin
|
|
|
|
|
|
};
|
2021-11-30 11:54:36 -08:00
|
|
|
|
container.AddChild(printerConnectionMessage);
|
2018-11-01 14:07:21 -07:00
|
|
|
|
|
2020-11-25 07:39:36 -08:00
|
|
|
|
var removeImage = StaticData.Instance.LoadImage(Path.Combine("Images", "insert usb.png")).SetPreMultiply();
|
2018-11-01 14:07:21 -07:00
|
|
|
|
container.AddChild(new ImageWidget(removeImage)
|
|
|
|
|
|
{
|
|
|
|
|
|
HAnchor = HAnchor.Center,
|
|
|
|
|
|
Margin = new BorderDouble(0, 10),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2019-04-05 13:18:32 -07:00
|
|
|
|
container.AddChild(new GuiWidget
|
|
|
|
|
|
{
|
|
|
|
|
|
VAnchor = VAnchor.Stretch
|
|
|
|
|
|
});
|
2015-04-08 15:20:10 -07:00
|
|
|
|
|
2017-08-07 15:47:27 -07:00
|
|
|
|
container.HAnchor = HAnchor.Stretch;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
return container;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-16 08:44:56 -08:00
|
|
|
|
private void Connection_CommunicationStateChanged(object sender, EventArgs e)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2018-02-01 14:51:44 -08:00
|
|
|
|
if (printer.Connection.IsConnected)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2021-11-30 11:54:36 -08:00
|
|
|
|
printerConnectionMessage.TextColor = theme.TextColor;
|
|
|
|
|
|
printerConnectionMessage.Text = "Connection succeeded".Localize() + "!";
|
|
|
|
|
|
printerConnectionMessage.TextColor = Color.Red;
|
2016-04-18 11:31:31 -07:00
|
|
|
|
nextButton.Visible = true;
|
|
|
|
|
|
connectButton.Visible = false;
|
2021-11-30 11:54:36 -08:00
|
|
|
|
UiThread.RunOnIdle(() => this?.Parent?.Close(), 1);
|
|
|
|
|
|
ApplicationController.Instance.ShowNotification("Connection succeeded");
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
2017-09-17 21:08:16 -07:00
|
|
|
|
else if (printer.Connection.CommunicationState != CommunicationStates.AttemptingToConnect)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2021-11-30 11:54:36 -08:00
|
|
|
|
printerConnectionMessage.TextColor = Color.Red;
|
|
|
|
|
|
printerConnectionMessage.Text = "Uh-oh! Could not connect to printer.".Localize();
|
2016-04-18 11:31:31 -07:00
|
|
|
|
connectButton.Visible = true;
|
|
|
|
|
|
nextButton.Visible = false;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|