예제 #1
0
        public void PurgeQueues()
        {
            while (_waitingCommandQueue.Count > 0)
            {
                _waitingCommandQueue.TryDequeue(out var dummy);
                CommandQueueLengthChanged?.Invoke(this, CommandQueueLength);
            }

            SystemCommands.Purge();
            ManualCommands.Purge();
            FileCommands.Purge();
            MacroCommands.Purge();
        }
예제 #2
0
        private void Send(Command cmd)
        {
            if (cmd == null)
            {
                return;
            }

            if (cmd.Type != RequestType.Realtime)
            {
                lock (_lockObject)
                {
                    _waitingCommandQueue.Enqueue(cmd);
                }
                CommandQueueLengthChanged?.Invoke(this, CommandQueueLength);

                _comService.Send(cmd.Data);
            }
            else
            {
                _comService.SendImmediate(cmd.Data);
            }
        }
예제 #3
0
        private void ComServiceLineReceived(object sender, string e)
        {
            lock (_lockObject)
            {
                _uiContext.Send(
                    x =>
                {
                    CommunicationLog.Insert(0, "grbl =>" + e);
                    if (CommunicationLog.Count > 500)
                    {
                        CommunicationLog.Remove(CommunicationLog.Last());
                    }
                }, null);

                var type = _responseTypeFinder.GetType(e);

                OnResponseReceived(new Response {
                    Data = e, Type = type
                });

                if ((type == ResponseType.Ok || type == ResponseType.Error || type == ResponseType.Alarm) && _waitingCommandQueue.Any() && _waitingCommandQueue.TryDequeue(out var cmd))
                {
                    CommandQueueLengthChanged?.Invoke(this, CommandQueueLength);
                    if (type == ResponseType.Ok)
                    {
                        cmd.ResultType = CommandResultType.Ok;
                    }
                    else if (type == ResponseType.Error)
                    {
                        cmd.ResultType         = CommandResultType.Error;
                        cmd.CommandResultCause = e.Split(':')[1];

                        if (cmd.Source == CommandSourceType.File)
                        {
                            FileCommands.PauseProcessing();
                        }
                        else if (cmd.Source == CommandSourceType.Macros)
                        {
                            MacroCommands.Purge();
                        }
                    }
                    else if (type == ResponseType.Alarm)
                    {
                        cmd.ResultType         = CommandResultType.Alarm;
                        cmd.CommandResultCause = e.Split(':')[1];

                        if (cmd.Source == CommandSourceType.File)
                        {
                            FileCommands.PauseProcessing();
                        }
                        else if (cmd.Source == CommandSourceType.Macros)
                        {
                            MacroCommands.Purge();
                        }
                    }

                    if (!string.IsNullOrEmpty(cmd.CommandOnResult))
                    {
                        SendAsync(cmd.CommandOnResult);
                    }

                    _uiContext.Send(
                        x =>
                    {
                        switch (cmd.Source)
                        {
                        case CommandSourceType.System:

                            SystemCommands.CommandList.Insert(0, cmd);
                            if (ManualCommands.CommandList.Count > 500)
                            {
                                ManualCommands.CommandList.Remove(ManualCommands.CommandList.Last());
                            }
                            break;

                        case CommandSourceType.Manual:

                            ManualCommands.CommandList.Insert(0, cmd);
                            if (ManualCommands.CommandList.Count > 500)
                            {
                                ManualCommands.CommandList.Remove(ManualCommands.CommandList.Last());
                            }
                            break;

                        case CommandSourceType.File:
                            FileCommands.CommandList.Add(cmd);
                            break;

                        case CommandSourceType.Macros:
                            MacroCommands.CommandList.Add(cmd);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        OnCommandFinished(cmd);
                        OnCommandListUpdated();
                    },
                        null);
                }
                else if (_waitingCommandQueue.Any() && _waitingCommandQueue.TryPeek(out var peekCmd))
                {
                    if (peekCmd.ExpectedResponses.Any(x => x == type))
                    {
                        peekCmd.Result += (string.IsNullOrEmpty(peekCmd.Result) ? "" : Environment.NewLine) + e;
                    }
                }
            }
        }