public void Activate(bool activate, ViewType chgMode)
        {
            if (activate)
            {
                GCodeSender.RewindFile();
                GCodeSender.CallHandler(GCode.File.IsLoaded ? StreamingState.Idle : (sdStream ? StreamingState.Start : StreamingState.NoFile), false);
                sdStream = false;

                model.ResponseLogFilterOk = AppConfig.Settings.Base.FilterOkResponse;

                if (initOK != true)
                {
                    focusedControl = this;

                    var message = model.Message;
                    model.Message = string.Format("Waiting for controller ({0})...", AppConfig.Settings.Base.PortParams);

                    string response = GrblInfo.Startup(model);

                    if (response.StartsWith("<"))
                    {
                        if (model.GrblState.State != GrblStates.Unknown)
                        {
                            switch (model.GrblState.State)
                            {
                            case GrblStates.Alarm:

                                model.Poller.SetState(AppConfig.Settings.Base.PollInterval);

                                switch (model.GrblState.Substate)
                                {
                                case 1:         // Hard limits
                                    if (!GrblInfo.IsLoaded)
                                    {
                                        if (model.LimitTriggered)
                                        {
                                            MessageBox.Show(string.Format("Controller is not able to communicate due to alarm {0}, attempting a soft reset.", model.GrblState.Substate.ToString()), "ioSender");
                                            if (AttemptReset())
                                            {
                                                model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                            }
                                            else
                                            {
                                                MessageBox.Show("Controller soft reset failed, exiting.", "ioSender");
                                                MainWindow.ui.Close();
                                            }
                                        }
                                        else if (AttemptReset())
                                        {
                                            model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                        }
                                    }
                                    else
                                    {
                                        response = string.Empty;
                                    }
                                    break;

                                case 2:         // Soft limits
                                    if (!GrblInfo.IsLoaded)
                                    {
                                        MessageBox.Show(string.Format("Controller is not able to communicate due to alarm {0}, attempting a soft reset.", model.GrblState.Substate.ToString()), "ioSender");
                                        if (AttemptReset())
                                        {
                                            model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                        }
                                        else
                                        {
                                            MessageBox.Show("Controller soft reset failed, exiting.", "ioSender");
                                            MainWindow.ui.Close();
                                        }
                                    }
                                    else
                                    {
                                        response = string.Empty;
                                    }
                                    break;

                                case 10:         // EStop
                                    if (GrblInfo.IsGrblHAL && model.Signals.Value.HasFlag(Signals.EStop))
                                    {
                                        MessageBox.Show("E-Stop active! - clear before continuing.", "ioSender", MessageBoxButton.OK, MessageBoxImage.Warning);
                                        while (!AttemptReset() && model.GrblState.State == GrblStates.Alarm)
                                        {
                                            if (MessageBox.Show("E-Stop still active, exit?", "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                            {
                                                MainWindow.ui.Close();
                                            }
                                        }
                                        ;
                                    }
                                    else
                                    {
                                        AttemptReset();
                                    }
                                    if (!GrblInfo.IsLoaded)
                                    {
                                        model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                    }
                                    break;

                                case 11:         // Homing required
                                    if (GrblInfo.IsLoaded)
                                    {
                                        response = string.Empty;
                                    }
                                    else
                                    {
                                        message = "Homing cycle required, <Home> to continue";
                                    }
                                    break;
                                }
                                break;

                            case GrblStates.Tool:
                                Comms.com.WriteByte(GrblConstants.CMD_STOP);
                                break;

                            case GrblStates.Door:
                                if (!GrblInfo.IsLoaded)
                                {
                                    if (MessageBox.Show("Door is open, close door and continue?", "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                                    {
                                        MainWindow.ui.Close();
                                    }
                                    else
                                    {
                                        bool exit = false;
                                        do
                                        {
                                            Comms.com.PurgeQueue();

                                            bool?res = null;
                                            CancellationToken cancellationToken = new CancellationToken();

                                            new Thread(() =>
                                            {
                                                res = WaitFor.SingleEvent <string>(
                                                    cancellationToken,
                                                    s => TrapReset(s),
                                                    a => model.OnGrblReset += a,
                                                    a => model.OnGrblReset -= a,
                                                    200, () => Comms.com.WriteByte(GrblConstants.CMD_STATUS_REPORT));
                                            }).Start();

                                            while (res == null)
                                            {
                                                EventUtils.DoEvents();
                                            }

                                            if (!(exit = !model.Signals.Value.HasFlag(Signals.SafetyDoor)))
                                            {
                                                if (MessageBox.Show("Door is still open, exit?", "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                                {
                                                    exit = true;
                                                    MainWindow.ui.Close();
                                                }
                                            }
                                        } while (!exit);
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("Door state cannot be cleared with <Reset>", "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                                    response = string.Empty;
                                }
                                break;

                            case GrblStates.Hold:
                            case GrblStates.Sleep:
                                if (MessageBox.Show(string.Format("Controller is in {0} state and cannot respond, try a soft reset?", model.GrblState.State.ToString()),
                                                    "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                                {
                                    MainWindow.ui.Close();
                                }
                                else if (!AttemptReset())
                                {
                                    MessageBox.Show("Controller soft reset failed, exiting.", "ioSender");
                                    MainWindow.ui.Close();
                                }
                                break;
                            }
                        }
                    }

                    if (response != string.Empty)
                    {
                        InitSystem();
                    }

                    model.Message = message;
                }

                if (initOK == null)
                {
                    initOK = false;
                }

                #if ADD_CAMERA
                if (MainWindow.UIViewModel.Camera != null)
                {
                    MainWindow.UIViewModel.Camera.MoveOffset += Camera_MoveOffset;
                    MainWindow.UIViewModel.Camera.Opened     += Camera_Opened;
                }
                #endif
                //if (viewer == null)
                //    viewer = new Viewer();

                if (GCode.File.IsLoaded)
                {
                    MainWindow.ui.WindowTitle = ((GrblViewModel)DataContext).FileName;
                }
            }
            else if (ViewType != ViewType.Shutdown)
            {
                DRO.IsFocusable = false;
                #if ADD_CAMERA
                if (MainWindow.UIViewModel.Camera != null)
                {
                    MainWindow.UIViewModel.Camera.MoveOffset -= Camera_MoveOffset;
                }
                #endif
                focusedControl = AppConfig.Settings.Base.KeepMdiFocus && Keyboard.FocusedElement is TextBox && (string)(Keyboard.FocusedElement as TextBox).Tag == "MDI"
                                  ? Keyboard.FocusedElement
                                  : this;
            }

            if (GCodeSender.Activate(activate))
            {
                Task.Delay(500).ContinueWith(t => DRO.EnableFocus());
                Application.Current.Dispatcher.BeginInvoke(new System.Action(() =>
                {
                    focusedControl.Focus();
                }), DispatcherPriority.Render);
            }
        }
Example #2
0
        public RestartResult Restart()
        {
            Message       = model.Message;
            model.Message = string.Format(LibStrings.FindResource("MsgWaiting"), AppConfig.Settings.Base.PortParams);

            string response = GrblInfo.Startup(model);

            if (response.StartsWith("<"))
            {
                if (model.GrblState.State != GrblStates.Unknown)
                {
                    switch (model.GrblState.State)
                    {
                    case GrblStates.Alarm:

                        model.Poller.SetState(AppConfig.Settings.Base.PollInterval);

                        switch (model.GrblState.Substate)
                        {
                        case 1:         // Hard limits
                            if (!GrblInfo.IsLoaded)
                            {
                                if (model.LimitTriggered)
                                {
                                    MessageBox.Show(string.Format(LibStrings.FindResource("MsgNoCommAlarm"), model.GrblState.Substate.ToString()), "ioSender");
                                    if (AttemptReset())
                                    {
                                        model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                    }
                                    else
                                    {
                                        MessageBox.Show(LibStrings.FindResource("MsgResetFailed"), "ioSender");
                                        return(RestartResult.Close);
                                    }
                                }
                                else if (AttemptReset())
                                {
                                    model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                }
                            }
                            else
                            {
                                response = string.Empty;
                            }
                            break;

                        case 2:         // Soft limits
                            if (!GrblInfo.IsLoaded)
                            {
                                MessageBox.Show(string.Format(LibStrings.FindResource("MsgNoCommAlarm"), model.GrblState.Substate.ToString()), "ioSender");
                                if (AttemptReset())
                                {
                                    model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                }
                                else
                                {
                                    MessageBox.Show(LibStrings.FindResource("MsgResetFailed"), "ioSender");
                                    return(RestartResult.Close);
                                }
                            }
                            else
                            {
                                response = string.Empty;
                            }
                            break;

                        case 10:         // EStop
                            if (GrblInfo.IsGrblHAL && model.Signals.Value.HasFlag(Signals.EStop))
                            {
                                MessageBox.Show(LibStrings.FindResource("MsgEStop"), "ioSender", MessageBoxButton.OK, MessageBoxImage.Warning);
                                while (!AttemptReset() && model.GrblState.State == GrblStates.Alarm)
                                {
                                    if (MessageBox.Show(LibStrings.FindResource("MsgEStopExit"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                    {
                                        return(RestartResult.Close);
                                    }
                                }
                                ;
                            }
                            else
                            {
                                AttemptReset();
                            }
                            if (!GrblInfo.IsLoaded)
                            {
                                model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                            }
                            break;

                        case 11:         // Homing required
                            if (GrblInfo.IsLoaded)
                            {
                                response = string.Empty;
                            }
                            else
                            {
                                Message = LibStrings.FindResource("MsgHome");
                            }
                            break;
                        }
                        break;

                    case GrblStates.Tool:
                        Comms.com.WriteByte(GrblConstants.CMD_STOP);
                        break;

                    case GrblStates.Door:
                        if (!GrblInfo.IsLoaded)
                        {
                            if (MessageBox.Show(LibStrings.FindResource("MsgDoorOpen"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                            {
                                return(RestartResult.Close);
                            }
                            else
                            {
                                bool exit = false;
                                do
                                {
                                    Comms.com.PurgeQueue();

                                    bool?res = null;
                                    CancellationToken cancellationToken = new CancellationToken();

                                    new Thread(() =>
                                    {
                                        res = WaitFor.SingleEvent <string>(
                                            cancellationToken,
                                            s => TrapReset(s),
                                            a => model.OnGrblReset += a,
                                            a => model.OnGrblReset -= a,
                                            200, () => Comms.com.WriteByte(GrblConstants.CMD_STATUS_REPORT));
                                    }).Start();

                                    while (res == null)
                                    {
                                        EventUtils.DoEvents();
                                    }

                                    if (!(exit = !model.Signals.Value.HasFlag(Signals.SafetyDoor)))
                                    {
                                        if (MessageBox.Show(LibStrings.FindResource("MsgDoorExit"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                        {
                                            exit = true;
                                            return(RestartResult.Close);
                                        }
                                    }
                                } while (!exit);
                            }
                        }
                        else
                        {
                            MessageBox.Show(LibStrings.FindResource("MsgDoorPersist"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                            response = string.Empty;
                        }
                        break;

                    case GrblStates.Hold:
                    case GrblStates.Sleep:
                        if (MessageBox.Show(string.Format(LibStrings.FindResource("MsgNoComm"), model.GrblState.State.ToString()),
                                            "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                        {
                            return(RestartResult.Close);
                        }
                        else if (!AttemptReset())
                        {
                            MessageBox.Show(LibStrings.FindResource("MsgResetExit"), "ioSender");
                            return(RestartResult.Close);
                        }
                        break;

                    case GrblStates.Idle:
                        if (response.Contains("|SD:Pending"))
                        {
                            AttemptReset();
                        }
                        break;
                    }
                }
            }
            else
            {
                MessageBox.Show(response == string.Empty
                                    ? "No respone received from controller, exiting."
                                    : string.Format("Unexpected response received from controller: \"{0}\", exiting.", response),
                                "ioSender", MessageBoxButton.OK, MessageBoxImage.Stop);
                return(RestartResult.Exit);
            }

            return(response == string.Empty ? RestartResult.NoResponse : RestartResult.Ok);
        }
        public void Activate(bool activate, ViewType chgMode)
        {
            if (activate)
            {
                GCodeSender.RewindFile();
                GCodeSender.CallHandler(GCode.File.IsLoaded ? StreamingState.Idle : (sdStream ? StreamingState.Start : StreamingState.NoFile), false);
                sdStream = false;

                if (initOK != true)
                {
                    focusedControl = this;
                    model.Message  = string.Format("Waiting for controller ({0})...", AppConfig.Settings.Base.PortParams);

                    string response = GrblInfo.Startup();

                    if (response.StartsWith("<Alarm"))
                    {
                        GrblViewModel data = DataContext as GrblViewModel;
                        data.ParseStatus(response);

                        // Alarm 1, 2 and 10 are critical events
                        if (!(data.GrblState.Substate == 1 || data.GrblState.Substate == 2 || data.GrblState.Substate == 10))
                        {
                            InitSystem();
                        }
                    }
                    else if (response.StartsWith("<Tool"))
                    {
                        Comms.com.WriteByte(GrblConstants.CMD_STOP);
                        GrblViewModel data = DataContext as GrblViewModel;
                        data.ParseStatus(Comms.com.Reply);
                    }
                    else if (response != string.Empty)
                    {
                        InitSystem();
                    }
                }

                if (initOK == null)
                {
                    initOK = false;
                }

                #if ADD_CAMERA
                if (MainWindow.UIViewModel.Camera != null)
                {
                    MainWindow.UIViewModel.Camera.MoveOffset += Camera_MoveOffset;
                    MainWindow.UIViewModel.Camera.Opened     += Camera_Opened;
                }
                #endif
                //if (viewer == null)
                //    viewer = new Viewer();

                if (GCode.File.IsLoaded)
                {
                    MainWindow.ui.WindowTitle = ((GrblViewModel)DataContext).FileName;
                }
            }
            else if (ViewType != ViewType.Shutdown)
            {
                DRO.IsFocusable = false;
                #if ADD_CAMERA
                if (MainWindow.UIViewModel.Camera != null)
                {
                    MainWindow.UIViewModel.Camera.MoveOffset -= Camera_MoveOffset;
                }
                #endif
                focusedControl = AppConfig.Settings.Base.KeepMdiFocus && Keyboard.FocusedElement is TextBox && (string)(Keyboard.FocusedElement as TextBox).Tag == "MDI"
                                  ? Keyboard.FocusedElement
                                  : this;
            }

            if (GCodeSender.Activate(activate))
            {
                Task.Delay(500).ContinueWith(t => DRO.EnableFocus());
                Application.Current.Dispatcher.BeginInvoke(new System.Action(() =>
                {
                    focusedControl.Focus();
                }), DispatcherPriority.Render);
            }
        }