Exemplo n.º 1
0
        public EngineCommandResult ProcessAllCommands()
        {
            if (ActiveCommands.Count == 0)
            {
                return(EngineCommandResult.Success);
            }

            //if (!Proxy.CheckAllInitialized)
            //    return              ( EngineCommandResult.Failed );

            var res = EngineCommandResult.Success;

            foreach (var c in ActiveCommands)
            {
                Proxy.Core.Log.Info("[Proxy Command Processor] Starting command: " + c.Type);
                res = ProcessCommand(c);
                if (res == EngineCommandResult.CriticalFailed)
                {
                    break;
                }
            }

            ActiveCommands.Clear();

            return(res);
        }
Exemplo n.º 2
0
        private static void StreamWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                Task <string> receiveTask = Connection.ConnectionReader.ReadLineAsync();                //won't raise exception on disconnect, will set exception prop and IsCompleted prop

                while (!receiveTask.IsCompleted)
                {
                    if (GCodeProvider.HasLine && (CurrentGRBLBuffer + GCodeProvider.PeekLineLength() + 1 < Properties.Settings.Default.GrblBufferSize))
                    {
                        string line = GCodeProvider.GetLine();

                        CurrentGRBLBuffer += line.Length + 1;
                        ActiveCommands.Enqueue(line);

                        Connection.ConnectionWriter.WriteLine(line);
                        Console.WriteLine($"sent {line}");
                    }

                    System.Threading.Thread.Sleep(25);
                }

                if (receiveTask.Exception == null)
                {
                    string receivedLine = receiveTask.Result;
                    Console.WriteLine($"received {receivedLine}");

                    if (!string.IsNullOrWhiteSpace(receivedLine) && !HandleReceivedLine(receivedLine))
                    {
                        streamWorker.ReportProgress(0, receivedLine);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void ClientDisconnected(Client client)
        {
            lock (_activeCommandsLock)
                for (int i = ActiveCommands.Count - 1; i >= 0; i--)
                {
                    var activeCommand = ActiveCommands[i];

                    lock (activeCommand.ClientsLock)
                        if (activeCommand.Clients.Contains(client))
                        {
                            activeCommand.Clients.Remove(client);
                            ActiveCommandEventManager.RemoveClient(activeCommand, client);
                        }

                    if (activeCommand.Clients.Count == 0)
                    {
                        ActiveCommands.Remove(activeCommand);
                        if (activeCommand.DynamicCommand.Status == DynamicCommandStatus.Active)
                        {
                            //don't change the status when stopped
                            activeCommand.DynamicCommand.Status = DynamicCommandStatus.Done;
                        }
                        ActiveCommandEventManager.RemoveActiveCommand(activeCommand);
                    }
                }
        }
Exemplo n.º 4
0
        public void ReceivedResult(DynamicCommandEvent dynamicCommandEvent, Client client)
        {
            _cacheManager.AddCommandEvent(dynamicCommandEvent);

            if (dynamicCommandEvent.Status == ActivityType.Active || dynamicCommandEvent.Status == ActivityType.Stopped)
            {
                lock (_activeCommandsLock)
                {
                    var activeCommand =
                        ActiveCommands.FirstOrDefault(x => x.DynamicCommand.Id == dynamicCommandEvent.DynamicCommand);
                    if (activeCommand == null)
                    {
                        var dynamicCommand =
                            _databaseManager.GetDynamicCommandById(dynamicCommandEvent.DynamicCommand);
                        if (dynamicCommand == null)
                        {
                            //when there is no command on this server with the id, we stop it because it may be removed by an administrator
                            client.StopActiveCommand(dynamicCommandEvent.DynamicCommand);
                            return;
                        }

                        ActiveCommands.Add(activeCommand = new ActiveCommandInfo(dynamicCommand));
                    }

                    switch (dynamicCommandEvent.Status)
                    {
                    case ActivityType.Active:
                        lock (activeCommand.ClientsLock)
                            activeCommand.Clients.Add(client);
                        ActiveCommandEventManager.AddClient(activeCommand, client);
                        CheckClientExecuteActiveCommand(activeCommand, client);
                        break;

                    case ActivityType.Stopped:
                        lock (activeCommand.ClientsLock)
                            activeCommand.Clients.Remove(client);
                        ActiveCommandEventManager.RemoveClient(activeCommand, client);

                        if (activeCommand.Clients.Count == 0)
                        {
                            ActiveCommands.Remove(activeCommand);
                            if (activeCommand.DynamicCommand.Status == DynamicCommandStatus.Active)
                            {
                                //don't change the status when stopped
                                activeCommand.DynamicCommand.Status = DynamicCommandStatus.Done;
                            }
                            ActiveCommandEventManager.RemoveActiveCommand(activeCommand);
                        }
                        break;
                    }
                }
            }
        }
Exemplo n.º 5
0
        public void RemoveDynamicCommand(RegisteredDynamicCommand dynamicCommand)
        {
            Logger.Debug("Remove dynamic command with id {0}", dynamicCommand.Id);

            if (dynamicCommand.CommandType == CommandType.Active)
            {
                lock (_activeCommandsLock)
                {
                    var activeCommand = ActiveCommands.FirstOrDefault(x => x.DynamicCommand.Id == dynamicCommand.Id);
                    if (activeCommand != null)
                    {
                        StopActiveCommand(activeCommand);
                    }
                }
            }

            _dynamicCommandScheduler.RemoveDynamicCommand(dynamicCommand);
            lock (_dynamicCommandsLock)
                DynamicCommands.Remove(dynamicCommand);
            _databaseManager.RemoveDynamicCommand(dynamicCommand.Id);
        }
Exemplo n.º 6
0
        public List <RegisteredDynamicCommand> GetDynamicCommands()
        {
            Logger.Debug("Get all dynamic commands");

            var dynamicCommands = _databaseManager.GetDynamicCommands();

            lock (_activeCommandsLock)
            {
                foreach (var registeredDynamicCommand in dynamicCommands)
                {
                    var activeCommandInfo =
                        ActiveCommands.FirstOrDefault(x => x.DynamicCommand.Id == registeredDynamicCommand.Id);
                    if (activeCommandInfo != null)
                    {
                        registeredDynamicCommand.Status = DynamicCommandStatus.Active;
                        lock (activeCommandInfo.ClientsLock)
                            registeredDynamicCommand.ExecutingClientIds =
                                activeCommandInfo.Clients.Select(x => x.Id).ToArray();
                    }
                }
            }

            return(dynamicCommands);
        }
Exemplo n.º 7
0
        private void MachineConnection_LineReceived(string line)
        {
            if (line.StartsWith("<"))
            {
                Match statusMatch = StatusEx.Match(line);

                if (!statusMatch.Success)
                {
                    Console.WriteLine("Received Bad Status: '{0}'", line);
                    return;
                }

                Group status = statusMatch.Groups["State"];

                if (status.Success)
                {
                    Status = status.Value;
                }

                Group mx = statusMatch.Groups["MX"], my = statusMatch.Groups["MY"], mz = statusMatch.Groups["MZ"];

                if (mx.Success)
                {
                    MachinePosition = new Vector3(double.Parse(mx.Value, GCodeCommand.NumberFormat), double.Parse(my.Value, GCodeCommand.NumberFormat), double.Parse(mz.Value, GCodeCommand.NumberFormat));
                }

                Group wx = statusMatch.Groups["WX"], wy = statusMatch.Groups["WY"], wz = statusMatch.Groups["WZ"];

                if (wx.Success)
                {
                    WorkPosition = new Vector3(double.Parse(wx.Value, GCodeCommand.NumberFormat), double.Parse(wy.Value, GCodeCommand.NumberFormat), double.Parse(wz.Value, GCodeCommand.NumberFormat));
                }

                StatusUpdate();
            }
            else if (line.StartsWith("ok"))
            {
                if (ActiveCommands.Count > 0)
                {
                    CharBufferCount -= ActiveCommands.Dequeue().Length;
                }
                else
                {
                    Console.WriteLine("Received ok without active command");
                }
            }
            else if (line.StartsWith("[PRB:"))
            {
                Match probeMatch = ProbeEx.Match(line);
                Group mx         = probeMatch.Groups["MX"];

                if (!probeMatch.Success || !mx.Success)
                {
                    Console.WriteLine("Received Bad Probe: '{0}'", line);
                    return;
                }

                double height = double.Parse(mx.Value, GCodeCommand.NumberFormat);

                height += WorkPosition.Z - MachinePosition.Z;

                if (Probes.Count > 0)
                {
                    Probes.Dequeue().SetResult(height);
                }
            }
            else if (line.StartsWith("error"))
            {
                string command;

                if (ActiveCommands.Count > 0)
                {
                    command          = ActiveCommands.Dequeue();
                    CharBufferCount -= command.Length;
                }
                else
                {
                    command = "No active command";
                }

                HandleError(line, command);
            }
            else if (line.StartsWith("["))              //feedback message
            {
            }
            else if (line.StartsWith("ALARM"))
            {
            }
            else if (line.StartsWith("Grbl"))
            {
                Probes.Clear();
                ActiveCommands.Clear();
                CharBufferCount = 0;
                RequestStatus();
            }
        }
Exemplo n.º 8
0
 public void AddCommand(IEngineCommand c)
 {
     ActiveCommands.Enqueue(c);
 }
Exemplo n.º 9
0
 public ActiveCommandInfo GetActiveCommandById(int id)
 {
     lock (_activeCommandsLock)
         return(ActiveCommands.FirstOrDefault(x => x.DynamicCommand.Id == id));
 }
Exemplo n.º 10
0
        public void OnClientJoin(Client client)
        {
            Logger.Debug("Client CI-{0} joined, check if there are commands to execute", client.Id);

            lock (_dynamicCommandsLock)
            {
                foreach (
                    var command in
                    DynamicCommands.Where(
                        x =>
                        (x.TransmissionEvent.GetType() == typeof(OnJoinTransmissionEvent)) &&
                        IsClientInTargets(client, x.Target) &&
                        CheckConditions(client.GetOnlineClientInformation(), client.ComputerInformation.ClientConfig,
                                        x.Conditions)))
                {
                    if (ExecuteStaticCommand(new[] { client },
                                             DynamicCommandToPotentialCommand(command,
                                                                              _databaseManager.GetDynamicCommandParameter(command.Id)))
                        .Count == 1)
                    {
                        _databaseManager.AddDynamicCommandEvent(command.Id, client.Id, ActivityType.Sent, null);
                    }
                }

                foreach (
                    var command in
                    DynamicCommands.Where(
                        x =>
                        x.TransmissionEvent.GetType() == typeof(EveryClientOnceTransmissionEvent) &&
                        IsClientInTargets(client, x.Target) &&
                        CheckConditions(client.GetOnlineClientInformation(), client.ComputerInformation.ClientConfig,
                                        x.Conditions)))
                {
                    if (!_databaseManager.ClientCommandExecuted(client.Id, command.Id))
                    {
                        if (ExecuteStaticCommand(new[] { client },
                                                 DynamicCommandToPotentialCommand(command,
                                                                                  _databaseManager.GetDynamicCommandParameter(command.Id))).Count == 1)
                        {
                            _databaseManager.AddDynamicCommandEvent(command.Id, client.Id, ActivityType.Sent, null);
                        }
                    }
                }
            }

            if (client.ComputerInformation.ActiveCommands?.Count > 0)
            {
                lock (_activeCommandsLock)
                    foreach (var activeCommandId in client.ComputerInformation.ActiveCommands)
                    {
                        var activeCommand = ActiveCommands.FirstOrDefault(x => x.DynamicCommand.Id == activeCommandId);
                        if (activeCommand == null)
                        {
                            var dynamicCommand =
                                _databaseManager.GetDynamicCommandById(activeCommandId);
                            if (dynamicCommand == null)
                            {
                                //when there is no command on this server with the id, we stop it because it may be removed by an administrator
                                client.StopActiveCommand(activeCommandId);
                                return;
                            }

                            ActiveCommands.Add(activeCommand = new ActiveCommandInfo(dynamicCommand));
                            ActiveCommandEventManager.AddActiveCommand(activeCommand);
                        }

                        lock (activeCommand.ClientsLock)
                            activeCommand.Clients.Add(client);

                        CheckClientExecuteActiveCommand(activeCommand, client);
                        ActiveCommandEventManager.AddClient(activeCommand, client);
                    }
            }
        }
Exemplo n.º 11
0
        private static bool HandleReceivedLine(string line)
        {
            if (line.StartsWith("ok"))
            {
                if (ActiveCommands.Count > 0)
                {
                    CurrentGRBLBuffer -= ActiveCommands.Dequeue().Length + 1;
                }
                else
                {
                    Console.Error.WriteLine("Received ok without active command");
                }

                return(true);
            }
            else if (line.StartsWith("error"))
            {
                string activeCommand;

                if (ActiveCommands.Count > 0)
                {
                    activeCommand      = ActiveCommands.Dequeue();
                    CurrentGRBLBuffer -= activeCommand.Length + 1;
                }
                else
                {
                    Console.Error.WriteLine("Received ok without active command");
                    activeCommand = "no active command";
                }

                if (line.StartsWith(Properties.Resources.errorInvalidGCode))
                {
                    int errorNo;

                    if (int.TryParse(line.Substring(Properties.Resources.errorInvalidGCode.Length), out errorNo))
                    {
                        if (Util.GrblErrorProvider.Errors.ContainsKey(errorNo))
                        {
                            App.Message($"'{activeCommand}':\n'{line}'\n{Util.GrblErrorProvider.Errors[errorNo]}");
                            goto skipMessage;
                        }
                        else
                        {
                            Console.Error.WriteLine($"Unknown Error ID '{line}'\ntime to update error definitions?");
                        }
                    }
                    else
                    {
                        Console.Error.WriteLine($"Couldn't parse Error ID in '{line}'");
                    }
                }
                App.Message($"'{activeCommand}':\n'{line}'");

skipMessage:
                return(true);
            }
            else if (line.StartsWith("Alarm"))
            {
                App.Message(line);
                return(true);
            }

            return(false);
        }