/// <summary> /// Mount data results /// </summary> /// <remarks> /// There could be timing issues between this method and timeouts for commands reading mount data /// </remarks> /// <param name="command"></param> /// <returns></returns> public static ISkyCommand GetCommandResult(ISkyCommand command) { if (!IsRunning || _cts.IsCancellationRequested || !_skyWatcher.IsConnected) { var e = new MountControlException(ErrorCode.ErrQueueFailed, "Queue not running"); command.Exception = e; command.Successful = false; return(command); } var sw = Stopwatch.StartNew(); while (sw.Elapsed.TotalMilliseconds < 22000) { if (_resultsDictionary == null) { break; } var success = _resultsDictionary.TryRemove(command.Id, out var result); if (success) { return(result); } Thread.Sleep(1); } var ex = new MountControlException(ErrorCode.ErrQueueFailed, $"Unable to Find Results {command.Id}, {command}, {sw.Elapsed.TotalMilliseconds}"); command.Exception = ex; command.Successful = false; return(command); }
/// <summary> /// Process command queue /// </summary> /// <param name="command"></param> private static void ProcessCommandQueue(ISkyCommand command) { try { if (!IsRunning || _cts.IsCancellationRequested || !_skyWatcher.IsConnected) { return; } command.Execute(_skyWatcher); if (command.Id <= 0) { return; } if (_resultsDictionary.TryAdd(command.Id, command) == false) { throw new MountControlException(ErrorCode.ErrQueueFailed, $"Unable to post results {command.Id}, {command}"); } } catch (Exception e) { var monitorItem = new MonitorEntry { Datetime = HiResDateTime.UtcNow, Device = MonitorDevice.Telescope, Category = MonitorCategory.Mount, Type = MonitorType.Information, Method = MethodBase.GetCurrentMethod().Name, Thread = Thread.CurrentThread.ManagedThreadId, Message = $"{command.Id},{e.Message}" }; MonitorLog.LogToMonitor(monitorItem); command.Exception = e; command.Successful = false; } }
/// <summary> /// Add a command to the blocking queue /// </summary> /// <param name="command"></param> public static void AddCommand(ISkyCommand command) { if (!IsRunning || _cts.IsCancellationRequested || !_skyWatcher.IsConnected) { return; } CleanResults(20, 120); if (_commandBlockingCollection.TryAdd(command) == false) { throw new MountControlException(ErrorCode.ErrQueueFailed, $"Unable to Add Command {command.Id}, {command}"); } }