private void onConnectButton_Click(object sender, EventArgs mouseEvent)
        {
            if (ActivePrinterProfile.Instance.ActivePrinter == null)
            {
#if __ANDROID__
                SetupWizardWindow.Show();
#else
                PrinterActionRow.OpenConnectionWindow(true);
#endif
            }
            else
            {
#if __ANDROID__
                if (!FrostedSerialPort.HasPermissionToDevice())
                {
                    // Opens the USB device permissions dialog which will call back into our UsbDevice broadcast receiver to connect
                    FrostedSerialPort.RequestPermissionToDevice();
                }
                else
#endif
                {
                    ConnectToActivePrinter();
                }
            }
        }
        private void onConnectButton_Click(object sender, EventArgs mouseEvent)
        {
            if (ActiveSliceSettings.Instance.PrinterSelected)
            {
#if __ANDROID__
                if (!FrostedSerialPort.HasPermissionToDevice())
                {
                    // Opens the USB device permissions dialog which will call back into our UsbDevice broadcast receiver to connect
                    FrostedSerialPort.RequestPermissionToDevice(RunTroubleShooting);
                }
                else
#endif
                {
                    PrinterConnectionAndCommunication.Instance.HaltConnectionThread();
                    PrinterConnectionAndCommunication.Instance.ConnectToActivePrinter(true);
                }
            }
        }
        public void UserRequestedConnectToActivePrinter()
        {
            if (printer.Settings.PrinterSelected)
            {
                listenForConnectFailed = true;
                connectStartMs         = UiThread.CurrentTimerMs;

#if __ANDROID__
                if (!printer.Settings.GetValue <bool>(SettingsKey.enable_network_printing) &&
                    !FrostedSerialPort.HasPermissionToDevice())
                {
                    // Opens the USB device permissions dialog which will call back into our UsbDevice broadcast receiver to connect
                    FrostedSerialPort.RequestPermissionToDevice(RunTroubleShooting);
                }
                else
#endif
                {
                    printer.Connection.HaltConnectionThread();
                    printer.Connection.Connect();
                }
            }
        }
        protected void AddChildElements()
        {
            addButton             = textImageButtonFactory.GenerateTooltipButton("Add".Localize(), StaticData.Instance.LoadIcon("icon_circle_plus.png", 32, 32).InvertLightness());
            addButton.ToolTipText = "Add a file to be printed".Localize();
            addButton.Margin      = new BorderDouble(6, 6, 6, 3);

            startButton             = textImageButtonFactory.GenerateTooltipButton("Print".Localize(), StaticData.Instance.LoadIcon("icon_play_32x32.png", 32, 32).InvertLightness());
            startButton.Name        = "Start Print Button";
            startButton.ToolTipText = "Begin printing the selected item.".Localize();
            startButton.Margin      = new BorderDouble(6, 6, 6, 3);
            startButton.Click      += onStartButton_Click;

            configureButton             = textImageButtonFactory.GenerateTooltipButton("Finish Setup...".Localize());
            configureButton.Name        = "Finish Setup Button";
            configureButton.ToolTipText = "Run setup configuration for printer.".Localize();
            configureButton.Margin      = new BorderDouble(6, 6, 6, 3);
            configureButton.Click      += onStartButton_Click;

            connectButton             = textImageButtonFactory.GenerateTooltipButton("Connect".Localize(), StaticData.Instance.LoadIcon("icon_power_32x32.png", 32, 32).InvertLightness());
            connectButton.ToolTipText = "Connect to the printer".Localize();
            connectButton.Margin      = new BorderDouble(6, 6, 6, 3);
            connectButton.Click      += (s, e) =>
            {
                if (ActiveSliceSettings.Instance.PrinterSelected)
                {
#if __ANDROID__
                    if (!FrostedSerialPort.HasPermissionToDevice())
                    {
                        // Opens the USB device permissions dialog which will call back into our UsbDevice broadcast receiver to connect
                        FrostedSerialPort.RequestPermissionToDevice(RunTroubleShooting);
                    }
                    else
#endif
                    {
                        PrinterConnectionAndCommunication.Instance.HaltConnectionThread();
                        PrinterConnectionAndCommunication.Instance.ConnectToActivePrinter(true);
                    }
                }
            };

            addPrinterButton             = textImageButtonFactory.GenerateTooltipButton("Add Printer".Localize());
            addPrinterButton.ToolTipText = "Select and add a new printer.".Localize();
            addPrinterButton.Margin      = new BorderDouble(6, 6, 6, 3);
            addPrinterButton.Click      += (s, e) =>
            {
                UiThread.RunOnIdle(() => WizardWindow.ShowPrinterSetup(true));
            };

            selectPrinterButton             = textImageButtonFactory.GenerateTooltipButton("Select Printer".Localize());
            selectPrinterButton.ToolTipText = "Select an existing printer.".Localize();
            selectPrinterButton.Margin      = new BorderDouble(6, 6, 6, 3);
            selectPrinterButton.Click      += (s, e) =>
            {
                WizardWindow.Show <SetupOptionsPage>("/SetupOptions", "Setup Wizard");
            };

            string resetConnectionButtontText   = "Reset".Localize();
            string resetConnectionButtonMessage = "Reboots the firmware on the controller".Localize();
            resetConnectionButton             = textImageButtonFactory.GenerateTooltipButton(resetConnectionButtontText, StaticData.Instance.LoadIcon("e_stop4.png", 32, 32).InvertLightness());
            resetConnectionButton.ToolTipText = resetConnectionButtonMessage;
            resetConnectionButton.ToolTipText = resetConnectionButtonMessage;
            resetConnectionButton.Margin      = new BorderDouble(6, 6, 6, 3);

            string skipButtonText    = "Skip".Localize();
            string skipButtonMessage = "Skip the current item and move to the next in queue".Localize();
            skipButton = makeButton(skipButtonText, skipButtonMessage);

            string removeButtonText    = "Remove".Localize();
            string removeButtonMessage = "Remove current item from queue".Localize();
            removeButton        = makeButton(removeButtonText, removeButtonMessage);
            removeButton.Click += onRemoveButton_Click;

            string pauseButtonText    = "Pause".Localize();
            string pauseButtonMessage = "Pause the current print".Localize();
            pauseButton        = makeButton(pauseButtonText, pauseButtonMessage);
            pauseButton.Click += (s, e) =>
            {
                PrinterConnectionAndCommunication.Instance.RequestPause();
                pauseButton.Enabled = false;
            };
            this.AddChild(pauseButton);
            allPrintButtons.Add(pauseButton);

            string cancelCancelButtonText     = "Cancel Connect".Localize();
            string cancelConnectButtonMessage = "Stop trying to connect to the printer.".Localize();
            cancelConnectButton = makeButton(cancelCancelButtonText, cancelConnectButtonMessage);

            string cancelButtonText    = "Cancel".Localize();
            string cancelButtonMessage = "Stop the current print".Localize();
            cancelButton      = makeButton(cancelButtonText, cancelButtonMessage);
            cancelButton.Name = "Cancel Print Button";

            string resumeButtonText    = "Resume".Localize();
            string resumeButtonMessage = "Resume the current print".Localize();
            resumeButton        = makeButton(resumeButtonText, resumeButtonMessage);
            resumeButton.Name   = "Resume Button";
            resumeButton.Click += (s, e) =>
            {
                if (PrinterConnectionAndCommunication.Instance.PrinterIsPaused)
                {
                    PrinterConnectionAndCommunication.Instance.Resume();
                }
                pauseButton.Enabled = true;
            };

            this.AddChild(resumeButton);
            allPrintButtons.Add(resumeButton);

            string reprintButtonText    = "Print Again".Localize();
            string reprintButtonMessage = "Print current item again".Localize();
            reprintButton      = makeButton(reprintButtonText, reprintButtonMessage);
            reprintButton.Name = "Print Again Button";

            string doneCurrentPartButtonText    = "Done".Localize();
            string doenCurrentPartButtonMessage = "Move to next print in queue".Localize();
            doneWithCurrentPartButton      = makeButton(doneCurrentPartButtonText, doenCurrentPartButtonMessage);
            doneWithCurrentPartButton.Name = "Done Button";

            this.Margin  = new BorderDouble(0, 0, 10, 0);
            this.HAnchor = HAnchor.FitToChildren;

            this.AddChild(connectButton);
            allPrintButtons.Add(connectButton);

            this.AddChild(addPrinterButton);
            allPrintButtons.Add(addPrinterButton);

            this.AddChild(selectPrinterButton);
            allPrintButtons.Add(selectPrinterButton);

            this.AddChild(addButton);
            allPrintButtons.Add(addButton);

            this.AddChild(startButton);
            allPrintButtons.Add(startButton);

            this.AddChild(configureButton);
            allPrintButtons.Add(configureButton);

            this.AddChild(doneWithCurrentPartButton);
            allPrintButtons.Add(doneWithCurrentPartButton);

            this.AddChild(skipButton);
            allPrintButtons.Add(skipButton);

            this.AddChild(cancelButton);
            allPrintButtons.Add(cancelButton);

            this.AddChild(cancelConnectButton);
            allPrintButtons.Add(cancelConnectButton);

            this.AddChild(reprintButton);
            allPrintButtons.Add(reprintButton);

            this.AddChild(removeButton);
            allPrintButtons.Add(removeButton);

            this.AddChild(resetConnectionButton);
            allPrintButtons.Add(resetConnectionButton);

            SetButtonStates();
        }
Example #5
0
        private void RefreshStatus()
        {
            CriteriaRow.ResetAll();

            // Clear the main container
            contentRow.CloseAllChildren();

            // Regen and refresh the troubleshooting criteria
            var printerNameLabel = new TextWidget(string.Format("{0}:", "Connection Troubleshooting".Localize()), 0, 0, labelFontSize)
            {
                TextColor = theme.TextColor,
                Margin    = new BorderDouble(bottom: 10)
            };

#if __ANDROID__
            IUsbSerialPort serialPort = FrostedSerialPort.LoadSerialDriver(null);

#if ANDROID7
            // Filter out the built-in 002 device and select the first item from the list
            // On the T7 Android device, there is a non-printer device always registered at usb/002/002 that must be ignored
            UsbDevice usbPrintDevice = usbManager.DeviceList.Values.Where(d => d.DeviceName != "/dev/bus/usb/002/002").FirstOrDefault();
#else
            UsbDevice usbPrintDevice = usbManager.DeviceList.Values.FirstOrDefault();
#endif

            UsbStatus usbStatus = new UsbStatus()
            {
                IsDriverLoadable   = (serialPort != null),
                HasUsbDevice       = true,
                HasUsbPermission   = false,
                AnyUsbDeviceExists = usbPrintDevice != null
            };

            if (!usbStatus.IsDriverLoadable)
            {
                usbStatus.HasUsbDevice = usbPrintDevice != null;

                if (usbStatus.HasUsbDevice)
                {
                    // TODO: Testing specifically for UsbClass.Comm seems fragile but no better alternative exists without more research
                    usbStatus.UsbDetails = new UsbDeviceDetails()
                    {
                        ProductID   = usbPrintDevice.ProductId,
                        VendorID    = usbPrintDevice.VendorId,
                        DriverClass = usbManager.DeviceList.Values.First().DeviceClass == Android.Hardware.Usb.UsbClass.Comm ? "cdcDriverType" : "ftdiDriverType"
                    };
                    usbStatus.Summary = string.Format("No USB device definition found. Click the 'Fix' button to add an override for your device ", usbStatus.UsbDetails.VendorID, usbStatus.UsbDetails.ProductID);
                }
            }

            usbStatus.HasUsbPermission = usbStatus.IsDriverLoadable && FrostedSerialPort.HasPermissionToDevice(serialPort);

            contentRow.AddChild(printerNameLabel);

            contentRow.AddChild(new CriteriaRow(
                                    "USB Connection",
                                    "Retry",
                                    "No USB device found. Check and reseat cables and try again",
                                    usbStatus.AnyUsbDeviceExists,
                                    () => UiThread.RunOnIdle(RefreshStatus),
                                    theme));

            contentRow.AddChild(new CriteriaRow(
                                    "USB Driver",
                                    "Fix",
                                    usbStatus.Summary,
                                    usbStatus.IsDriverLoadable,
                                    () =>
            {
                string overridePath         = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "usboverride.local");
                UsbDeviceDetails usbDetails = usbStatus.UsbDetails;
                File.AppendAllText(overridePath, string.Format("{0},{1},{2}\r\n", usbDetails.VendorID, usbDetails.ProductID, usbDetails.DriverClass));

                UiThread.RunOnIdle(() => RefreshStatus());
            },
                                    theme));

            contentRow.AddChild(new CriteriaRow(
                                    "USB Permission",
                                    "Request Permission",
                                    "Click the 'Request Permission' button to gain Android access rights",
                                    usbStatus.HasUsbPermission,
                                    () =>
            {
                if (checkForPermissionTimer == null)
                {
                    checkForPermissionTimer = new System.Threading.Timer((state) =>
                    {
                        if (FrostedSerialPort.HasPermissionToDevice(serialPort))
                        {
                            UiThread.RunOnIdle(this.RefreshStatus);
                            checkForPermissionTimer.Dispose();
                        }
                    }, null, 200, 200);
                }

                FrostedSerialPort.RequestPermissionToDevice(serialPort);
            },
                                    theme));
#endif
            connectToPrinterRow = new CriteriaRow(
                "Connect to Printer".Localize(),
                "Connect".Localize(),
                "Click the 'Connect' button to retry the original connection attempt".Localize(),
                false,
                () => printer.Connection.Connect(),
                theme);

            contentRow.AddChild(connectToPrinterRow);

            if (CriteriaRow.ActiveErrorItem != null)
            {
                var errorText = new FlowLayoutWidget()
                {
                    Padding = new BorderDouble(0, 15)
                };

                errorText.AddChild(
                    new TextWidget(CriteriaRow.ActiveErrorItem.ErrorText)
                {
                    TextColor = theme.PrimaryAccentColor
                });

                contentRow.AddChild(errorText);
            }
        }