static async ValueTask <(bool success, string message)> TrySetFixedFrame(string id) { (bool success, string message)result = default; using (SemaphoreSlim signal = new SemaphoreSlim(0)) { GameThread.Post(() => { try { TfListener.FixedFrameId = id; } finally { signal.Release(); } }); if (!await signal.WaitAsync(DefaultTimeoutInMs)) { Logger.Error("ControllerService: Unexpected timeout in TrySetFixedFrame"); return(result); } } return(true, ""); }
static async Task RemoveRobotAsync([NotNull] UpdateRobot srv) { string id = srv.Request.Id; if (id.Length == 0) { srv.Response.Success = false; srv.Response.Message = "EE Id field is empty"; return; } ModuleData moduleData; if ((moduleData = ModuleDatas.FirstOrDefault(data => data.Configuration.Id == id)) == null) { srv.Response.Success = true; srv.Response.Message = $"WW There is no node with name '{id}'"; return; } if (moduleData.ModuleType != ModuleType.Robot) { srv.Response.Success = false; srv.Response.Message = $"EE Another module of the same id already exists, but it has type {moduleData.ModuleType}"; return; } using (SemaphoreSlim signal = new SemaphoreSlim(0)) { GameThread.Post(() => { try { Logger.Info($"ControllerService: Removing robot"); ModuleListPanel.Instance.RemoveModule(moduleData); } catch (Exception e) { srv.Response.Success = false; srv.Response.Message = $"EE An exception was raised: {e.Message}"; } finally { signal.Release(); } }); if (!await signal.WaitAsync(DefaultTimeoutInMs)) { srv.Response.Success = false; srv.Response.Message = "EE Request timed out!"; return; } if (string.IsNullOrEmpty(srv.Response.Message)) { srv.Response.Success = true; } } }
static async ValueTask <string[]> GetModulesAsync() { using (SemaphoreSlim signal = new SemaphoreSlim(0)) { string[] result = Array.Empty <string>(); GameThread.Post(() => { try { IConfiguration[] configurations = ModuleDatas.Select(data => data.Configuration).ToArray(); result = configurations.Select(JsonConvert.SerializeObject).ToArray(); } catch (JsonException e) { Logger.Error("ControllerService: Unexpected JSON exception in GetModules", e); } catch (Exception e) { Logger.Error("ControllerService: Unexpected exception in GetModules", e); } finally { signal.Release(); } }); if (!await signal.WaitAsync(DefaultTimeoutInMs)) { Logger.Error("Timeout in GetModules"); } return(result); } }
static async Task AddRobotAsync([NotNull] UpdateRobot srv) { string id = srv.Request.Id; ModuleData moduleData; if (id.Length != 0) { moduleData = ModuleDatas.FirstOrDefault(data => data.Configuration.Id == id); if (moduleData != null && moduleData.ModuleType != ModuleType.Robot) { srv.Response.Success = false; srv.Response.Message = $"EE Another module of the same id already exists, but it has type {moduleData.ModuleType}"; return; } } else { moduleData = null; } using (var signal = new SemaphoreSlim(0)) { GameThread.Post(() => { try { //Logger.Info($"ControllerService: Creating robot"); var newModuleData = moduleData ?? ModuleListPanel.Instance.CreateModule( ModuleType.Robot, requestedId: id.Length != 0 ? id : null); srv.Response.Success = true; ((SimpleRobotModuleData)newModuleData).UpdateConfiguration( srv.Request.Configuration, srv.Request.ValidFields); } catch (Exception e) { srv.Response.Success = false; srv.Response.Message = $"EE An exception was raised: {e.Message}"; } finally { signal.Release(); } }); if (!await signal.WaitAsync(DefaultTimeoutInMs)) { srv.Response.Success = false; srv.Response.Message = "EE Request timed out!"; return; } if (string.IsNullOrEmpty(srv.Response.Message)) { srv.Response.Success = true; } } }
void UpdateParametersLink([NotNull] string link) { description.Clear(); if (!ConnectionManager.IsConnected) { panel.TextBottom.text = EmptyBottomText; return; } description.Append("<font=Bold>").Append(link).AppendLine("</font>"); if (!paramValue.IsEmpty) { description.AppendLine("<font=Bold>Value:</font>"); string value = JsonConvert .SerializeObject(paramValue, Formatting.Indented, JsonConverter); if (paramValue.ValueType != XmlRpcValue.Type.String) { description.Append(value); } else { description.Append(value .Replace("<", "<noparse><</noparse>") .Replace("\\n", "\n") .Replace("\\\"", "\"") ); } } else { tokenSource?.Cancel(); tokenSource = new CancellationTokenSource(5000); GetParamValue(link, tokenSource.Token); } panel.TextBottom.SetText(description); panel.TextBottom.UpdateVertexData(); async void GetParamValue(string param, CancellationToken token) { if (!ConnectionManager.IsConnected) { return; } try { (paramValue, _) = await ConnectionManager.Connection.GetParameterAsync(param, 5000, token); GameThread.Post(() => UpdateParametersLink(link)); } catch (OperationCanceledException) { } } }
void SetConnectionState(ConnectionState newState) { if (ConnectionState == newState) { return; } ConnectionState = newState; GameThread.Post(() => ConnectionStateChanged?.Invoke(newState)); }
static async ValueTask <(bool success, string message)> TryUpdateModuleAsync([NotNull] string id, string[] fields, string config) { (bool success, string message)result = default; if (string.IsNullOrWhiteSpace(id)) { result.message = "EE Empty configuration id!"; return(result); } if (string.IsNullOrWhiteSpace(config)) { result.message = "EE Empty configuration text!"; return(result); } using (var signal = new SemaphoreSlim(0)) { GameThread.Post(() => { try { var module = ModuleDatas.FirstOrDefault(data => data.Configuration.Id == id); if (module == null) { result.success = false; result.message = "EE There is no module with that id"; return; } module.UpdateConfiguration(config, fields); result.success = true; } catch (JsonException e) { result.success = false; result.message = $"EE Error parsing JSON config: {e.Message}"; Logger.Error("Error:", e); } catch (Exception e) { result.success = false; result.message = $"EE An exception was raised: {e.Message}"; Logger.Error("Error:", e); } finally { signal.Release(); } }); return(await signal.WaitAsync(DefaultTimeoutInMs) ? result : (false, "EE Request timed out!")); } }
internal static async Task StartCaptureAsync([NotNull] StartCapture srv) { if (Settings.ScreenCaptureManager == null) { srv.Response.Success = false; srv.Response.Message = "No screenshot manager has been set for this platform"; return; } string errorMessage = null; using (SemaphoreSlim signal = new SemaphoreSlim(0)) { GameThread.Post(async() => { try { await Settings.ScreenCaptureManager.StartAsync( srv.Request.ResolutionX, srv.Request.ResolutionY, srv.Request.WithHolograms); } catch (Exception e) { errorMessage = e.Message; } finally { signal.Release(); } }); if (!await signal.WaitAsync(DefaultTimeoutInMs)) { Logger.Error("ControllerService: Unexpected timeout in StartCaptureAsync"); srv.Response.Success = false; srv.Response.Message = "Request timed out"; return; } } if (errorMessage != null) { srv.Response.Success = false; srv.Response.Message = errorMessage; return; } srv.Response.Success = true; }
internal static async Task CaptureScreenshotAsync([NotNull] CaptureScreenshot srv) { if (Settings.ScreenCaptureManager == null) { srv.Response.Success = false; srv.Response.Message = "No screenshot manager has been set for this platform"; return; } string errorMessage = null; Screenshot ss = null; Pose? pose = null; using (SemaphoreSlim signal = new SemaphoreSlim(0)) { GameThread.Post(async() => { try { var assetHolder = UnityEngine.Resources .Load <GameObject>("App Asset Holder") .GetComponent <AppAssetHolder>(); AudioSource.PlayClipAtPoint(assetHolder.Screenshot, Settings.MainCamera.transform.position); ss = await Settings.ScreenCaptureManager.CaptureColorAsync(); pose = ss != null ? TfListener.RelativePoseToFixedFrame(ss.CameraPose).Unity2RosPose().ToCameraFrame() : (Pose?)null; } catch (Exception e) { errorMessage = e.Message; } finally { signal.Release(); } }); if (!await signal.WaitAsync(DefaultTimeoutInMs)) { Logger.Error("ControllerService: Unexpected timeout in CaptureScreenshotAsync"); srv.Response.Success = false; srv.Response.Message = "Request timed out"; return; } } if (errorMessage != null) { srv.Response.Success = false; srv.Response.Message = errorMessage; return; } if (ss == null) { srv.Response.Success = false; srv.Response.Message = "Captured failed for unknown reason"; return; } srv.Response.Success = true; srv.Response.Width = ss.Width; srv.Response.Height = ss.Height; srv.Response.Bpp = ss.Bpp; srv.Response.Header = (screenshotSeq++, ss.Timestamp, TfListener.FixedFrameId); srv.Response.Intrinsics = ss.Intrinsic.ToArray(); srv.Response.Pose = pose ?? Pose.Identity; srv.Response.Data = await CompressAsync(ss); }
static async ValueTask <(string id, bool success, string message)> TryAddModuleFromTopicAsync( [NotNull] string topic, [NotNull] string requestedId) { (string id, bool success, string message)result = default; if (string.IsNullOrWhiteSpace(topic)) { result.message = "EE Invalid topic name"; return(result); } var data = ModuleDatas.FirstOrDefault(module => module.Topic == topic); if (data != null) { result.message = requestedId == data.Configuration.Id ? "** Module already exists" : "WW A module with that topic but different id already exists"; result.id = data.Configuration.Id; result.success = true; return(result); } if (requestedId.Length != 0 && ModuleDatas.Any(module => module.Configuration.Id == requestedId)) { result.message = "EE There is already another module with that id"; return(result); } var topics = Connection.GetSystemPublishedTopicTypes(RequestType.CachedOnly); string type = topics.FirstOrDefault(topicInfo => topicInfo.Topic == topic)?.Type; if (type == null) { topics = await Connection.GetSystemPublishedTopicTypesAsync(DefaultTimeoutInMs); type = topics.FirstOrDefault(topicInfo => topicInfo.Topic == topic)?.Type; } if (type == null) { return("", false, $"EE Failed to find topic '{topic}'"); } if (!Resource.ResourceByRosMessageType.TryGetValue(type, out ModuleType resource)) { return("", false, $"EE Type '{type}' is unsupported"); } using (var signal = new SemaphoreSlim(0)) { GameThread.Post(() => { try { result.id = ModuleListPanel.Instance.CreateModule(resource, topic, type, requestedId: requestedId.Length != 0 ? requestedId : null).Configuration.Id; result.success = true; } catch (Exception e) { result.message = $"EE An exception was raised: {e.Message}"; Logger.Error("Exception raised in TryAddModuleFromTopicAsync", e); } finally { signal.Release(); } }); return(await signal.WaitAsync(DefaultTimeoutInMs) ? result : ("", false, "EE Request timed out!")); } }
static async ValueTask <(string id, bool success, string message)> TryAddModuleAsync( [NotNull] string moduleTypeStr, [NotNull] string requestedId) { (string id, bool success, string message)result = default; if (string.IsNullOrWhiteSpace(moduleTypeStr)) { result.message = "EE Invalid module type"; return(result); } ModuleType moduleType = ModuleTypeFromString(moduleTypeStr); if (moduleType == ModuleType.Invalid) { result.message = "EE Invalid module type"; return(result); } if (moduleType != ModuleType.Grid && moduleType != ModuleType.DepthCloud && moduleType != ModuleType.AugmentedReality && moduleType != ModuleType.Joystick && moduleType != ModuleType.Robot) { result.message = "EE Cannot create module of that type, use AddModuleFromTopic instead"; return(result); } ModuleData moduleData; if (requestedId.Length != 0 && (moduleData = ModuleDatas.FirstOrDefault(data => data.Configuration.Id == requestedId)) != null) { if (moduleData.ModuleType != moduleType) { result.message = $"EE Another module of the same id already exists, but it has type {moduleData.ModuleType}"; } else { result.success = true; result.id = requestedId; result.message = "** Module already exists"; } return(result); } using (SemaphoreSlim signal = new SemaphoreSlim(0)) { GameThread.Post(() => { try { Logger.Info($"Creating module of type {moduleType}"); var newModuleData = ModuleListPanel.Instance.CreateModule(moduleType, requestedId: requestedId.Length != 0 ? requestedId : null); result.id = newModuleData.Configuration.Id; result.success = true; Logger.Info("Done!"); } catch (Exception e) { result.message = $"EE An exception was raised: {e.Message}"; } finally { signal.Release(); } }); return(await signal.WaitAsync(DefaultTimeoutInMs) ? result : ("", false, "EE Request timed out!")); } }
protected void SetConnectionWarningState(bool value) { GameThread.Post(() => ConnectionWarningStateChanged?.Invoke(value)); }