Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        public override void UpdateConfiguration(string configAsJson, IEnumerable <string> fields)
        {
            var config = JsonConvert.DeserializeObject <MarkerConfiguration>(configAsJson);

            foreach (string field in fields)
            {
                switch (field)
                {
                case nameof(MarkerConfiguration.Visible):
                    listener.Visible = config.Visible;
                    break;

                case nameof(MarkerConfiguration.RenderAsOcclusionOnly):
                    listener.RenderAsOcclusionOnly = config.RenderAsOcclusionOnly;
                    break;

                case nameof(MarkerConfiguration.Tint):
                    listener.Tint = config.Tint.ToUnityColor();
                    break;

                case nameof(MarkerConfiguration.TriangleListFlipWinding):
                    listener.TriangleListFlipWinding = config.TriangleListFlipWinding;
                    break;

                default:
                    Logger.Error($"{this}: Unknown field '{field}'");
                    break;
                }
            }

            ResetPanel();
        }
Beispiel #3
0
        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, "");
        }
Beispiel #4
0
        void Initialize()
        {
            ThreadPool.SetMinThreads(25, 20);

            try
            {
                Logger.Debug("Hololens Manager: Initializing!");
                Settings.SettingsManager   = this;
                Settings.ScreenshotManager = new HololensScreenshotManager();

                if (!UnityEngine.Application.isEditor)
                {
                    floorHelper.gameObject.SetActive(false);
                }

                floorFrame.OkClicked += StartWorld;


                ModuleListPanel.Instance.MenuDialog = markerMenu;

                //StartLog();
                StartRosConnection();
                StartHandMenu();
                //StartPalms();
                StartOriginPlaceMode();
            }
            catch (Exception e)
            {
                Debug.Log(e);
                Logger.Error("Error initializing Hololens Manager", e);
            }
        }
Beispiel #5
0
        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!"));
            }
        }
Beispiel #6
0
        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;
        }
Beispiel #7
0
        static async ValueTask <(bool[] success, Pose[] poses)> TryGetFramePoseAsync(string[] ids)
        {
            bool[] success = null;
            Pose[] poses   = null;

            using (SemaphoreSlim signal = new SemaphoreSlim(0))
            {
                GameThread.Post(() =>
                {
                    try
                    {
                        List <bool> successList = new List <bool>();
                        List <Pose> posesList   = new List <Pose>();
                        foreach (string id in ids)
                        {
                            if (!TfListener.TryGetFrame(id, out var frame))
                            {
                                successList.Add(false);
                                posesList.Add(Pose.Identity);
                            }
                            else
                            {
                                successList.Add(true);
                                posesList.Add(frame.OriginWorldPose.Unity2RosPose());
                            }
                        }

                        success = successList.ToArray();
                        poses   = posesList.ToArray();
                    }
                    finally
                    {
                        signal.Release();
                    }
                });

                if (!await signal.WaitAsync(DefaultTimeoutInMs))
                {
                    Logger.Error("ControllerService: Unexpected timeout in TryGetFramePoseAsync");
                    return(new bool[ids.Length], new Pose[ids.Length]);
                }
            }

            return(success, poses);
        }
Beispiel #8
0
        async Task Run()
        {
            try
            {
                while (!connectionTs.IsCancellationRequested)
                {
                    DateTime now = GameThread.Now;
                    if (KeepReconnecting &&
                        ConnectionState != ConnectionState.Connected &&
                        (now - lastConnectionTry).TotalMilliseconds > ConnectionRetryTimeInMs)
                    {
                        SetConnectionState(ConnectionState.Connecting);

                        bool connectionResult;

                        try
                        {
                            lastConnectionTry = now;
                            connectionResult  = await Connect();
                        }
                        catch (Exception e)
                        {
                            Logger.Error("Unexpected error in RosConnection.Connect", e);
                            continue;
                        }

                        SetConnectionState(connectionResult ? ConnectionState.Connected : ConnectionState.Disconnected);
                    }

                    await signal.WaitAsync(TaskWaitTimeInMs);
                    await ExecuteTasks().AwaitNoThrow(this);
                }

                SetConnectionState(ConnectionState.Disconnected);
            }
            catch (Exception e)
            {
                // shouldn't happen
                Logger.Internal("Left connection thread!");
                Logger.Internal("Error:", e);
                Logger.Error("XXX Left connection thread: ", e);
            }

            connectionTs.Cancel();
        }
Beispiel #9
0
        public override void UpdateConfiguration(string configAsJson, IEnumerable <string> fields)
        {
            var config = JsonConvert.DeserializeObject <GridMapConfiguration>(configAsJson);

            foreach (string field in fields)
            {
                switch (field)
                {
                case nameof(GridMapConfiguration.Visible):
                    listener.Visible = config.Visible;
                    break;

                case nameof(GridMapConfiguration.Colormap):
                    listener.Colormap = config.Colormap;
                    break;

                case nameof(GridMapConfiguration.ForceMinMax):
                    listener.ForceMinMax = config.ForceMinMax;
                    break;

                case nameof(GridMapConfiguration.MinIntensity):
                    listener.MinIntensity = config.MinIntensity;
                    break;

                case nameof(GridMapConfiguration.MaxIntensity):
                    listener.MaxIntensity = config.MaxIntensity;
                    break;

                case nameof(GridMapConfiguration.FlipMinMax):
                    listener.FlipMinMax = config.FlipMinMax;
                    break;

                default:
                    Logger.Error($"{this}: Unknown field '{field}'");
                    break;
                }
            }

            ResetPanel();
        }
Beispiel #10
0
        public Task ClearModelCacheAsync(CancellationToken token = default)
        {
            runningTs.Cancel();
            runningTs = new CancellationTokenSource();

            var allFiles = resourceFiles.Models.Values
                           .Concat(resourceFiles.Scenes.Values)
                           .Concat(resourceFiles.Textures.Values)
                           .Concat(resourceFiles.RobotDescriptions.Values);

            Logger.Debug($"{this}: Removing all files in {Settings.SavedRobotsPath}");
            Logger.Debug($"{this}: Removing all files in {Settings.ResourcesPath}");

            foreach (string path in allFiles)
            {
                try
                {
                    File.Delete(path);
                }
                catch (Exception e)
                {
                    Logger.Error($"ExternalResourceManager: Failed to delete file '{path}' :", e);
                }
            }

            resourceFiles.Models.Clear();
            resourceFiles.Scenes.Clear();
            resourceFiles.Textures.Clear();
            resourceFiles.RobotDescriptions.Clear();

            loadedModels.Clear();
            loadedScenes.Clear();
            loadedTextures.Clear();

            return(WriteResourceFileAsync(token));
        }
Beispiel #11
0
        public ExternalResourceManager(bool createNode = true)
        {
            if (createNode)
            {
                Node = new GameObject("External Resources");
                Node.SetActive(false);
            }

            try
            {
                Directory.CreateDirectory(Settings.ResourcesPath);
                Directory.CreateDirectory(Settings.SavedRobotsPath);
            }
            catch (Exception e)
            {
                Logger.Error($"{this}: Error creating directories", e);
            }

            if (!File.Exists(Settings.ResourcesFilePath))
            {
                Logger.Debug("ExternalResourceManager: Failed to find file " + Settings.ResourcesFilePath);
                return;
            }

            Logger.Debug("ExternalResourceManager: Using resource file " + Settings.ResourcesFilePath);

            try
            {
                string text = File.ReadAllText(Settings.ResourcesFilePath);
                resourceFiles = JsonConvert.DeserializeObject <ResourceFiles>(text);
            }
            catch (Exception e)
            {
                Logger.Error($"{this}: Error reading config file", e);
            }
        }
Beispiel #12
0
        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);
        }
Beispiel #13
0
        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!"));
            }
        }