Exemplo n.º 1
0
        void UpdateAliasesLink(int index)
        {
            string hostname = panel.HostNames[index].Value;
            string address  = panel.Addresses[index].Value;

            if (!string.IsNullOrWhiteSpace(hostname) && !string.IsNullOrWhiteSpace(address))
            {
                var newHostAlias = new HostAlias(hostname, address);
                if (HostAliases[index] == newHostAlias)
                {
                    return;
                }

                HostAliases[index] = newHostAlias;
                Logger.Info($"{this}: Adding pair {hostname} -> {address} to resolver list.");
                ConnectionUtils.GlobalResolver[hostname] = address;
                ModuleListPanel.UpdateSimpleConfigurationSettings();
            }
            else if (HostAliases[index] != default)
            {
                ConnectionUtils.GlobalResolver.Remove(HostAliases[index].Hostname);
                Logger.Info($"{this}: Removing {HostAliases[index].Hostname} from resolver list.");
                HostAliases[index] = default;
                ModuleListPanel.UpdateSimpleConfigurationSettings();
            }
        }
Exemplo n.º 2
0
        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;
                }
            }
        }
Exemplo n.º 3
0
        public InternalResourceManager()
        {
            string robotsFile = UnityEngine.Resources.Load <TextAsset>("Package/iviz/resources")?.text;

            if (string.IsNullOrEmpty(robotsFile))
            {
                Logger.Warn($"{this}: Empty resource file!");
                robotDescriptions = new Dictionary <string, string>().AsReadOnly();
                return;
            }

            Dictionary <string, string> robots = JsonConvert.DeserializeObject <Dictionary <string, string> >(robotsFile);
            Dictionary <string, string> tmpRobotDescriptions = new Dictionary <string, string>();

            foreach (var pair in robots)
            {
                string robotDescription =
                    UnityEngine.Resources.Load <TextAsset>("Package/iviz/robots/" + pair.Value)?.text;
                if (string.IsNullOrEmpty(robotDescription))
                {
                    Logger.Info($"{this}: Empty or null description file {pair.Value}!");
                    continue;
                }

                tmpRobotDescriptions[pair.Key] = robotDescription;
            }

            robotDescriptions = tmpRobotDescriptions.AsReadOnly();
        }
Exemplo n.º 4
0
        public Sender([NotNull] string topic)
        {
            if (string.IsNullOrWhiteSpace(topic))
            {
                throw new ArgumentException("Invalid topic!", nameof(topic));
            }

            Topic = topic;
            Type  = BuiltIns.GetMessageType(typeof(T));

            Logger.Info($"Advertising <b>{topic}</b> <i>[{Type}]</i>.");
            GameThread.EverySecond += UpdateStats;
            Connection.Advertise(this);
        }
Exemplo n.º 5
0
        void WritePanelToConfiguration()
        {
            var markers = new List <ARExecutableMarker>();

            for (int index = 0; index < panel.Types.Count; index++)
            {
                var type = (ARMarkerType)panel.Types[index].Index;
                if (type == ARMarkerType.Unset)
                {
                    continue;
                }

                if (!float.TryParse(panel.Sizes[index].Value, out float sizeInMm) || sizeInMm <= 0)
                {
                    Logger.Info($"{this}: Ignoring size for entry {index}, cannot parse '{panel.Sizes[index].Value}' " +
                                "into a positive number.");
                    continue;
                }

                string code = panel.Codes[index].Value.Trim();
                if (string.IsNullOrEmpty(code))
                {
                    Logger.Info($"{this}: Ignoring empty code for entry {index}.");
                    continue;
                }

                ARExecutableMarker marker = new ARExecutableMarker
                {
                    Type     = type,
                    Action   = (ARMarkerAction)panel.Actions[index].Index,
                    Code     = code,
                    SizeInMm = sizeInMm,
                };
                markers.Add(marker);
            }

            Configuration = new ARMarkersConfiguration
            {
                MaxMarkerDistanceInM = 0.5f,
                Markers = markers.ToArray(),
            };

            if (ARController.Instance != null)
            {
                ARController.Instance.MarkerExecutor.Configuration = Configuration;
            }

            ModuleListPanel.UpdateSimpleConfigurationSettings();
        }
Exemplo n.º 6
0
        void PrintListenerStatus()
        {
            StringBuilder str     = new StringBuilder();
            var           modules = ModuleDatas.OfType <ListenerModuleData>();

            foreach (var data in modules)
            {
                ListenerController controller = (ListenerController)data.Controller;
                var listener = controller.Listener;
                str.Append(listener.Topic).Append(listener.Subscribed ? " (On): " : " (Off): ")
                .Append(listener.NumPublishers).Append(" publishers").AppendLine();
            }

            Logger.Info(str.ToString());
        }
Exemplo n.º 7
0
        internal static async Task LaunchDialogAsync([NotNull] LaunchDialog srv)
        {
            if (string.IsNullOrEmpty(srv.Request.Dialog.Id))
            {
                srv.Response.Success = false;
                srv.Response.Message = "Dialog Id is null or empty";
                return;
            }

            if (Math.Abs(srv.Request.Dialog.Scale) < 1e-8)
            {
                srv.Response.Success = false;
                srv.Response.Message = "Cannot launch dialog with scale 0";
                return;
            }

            Feedback feedback        = new Feedback();
            bool     overrideExpired = false;

            using (var signal = new SemaphoreSlim(0))
            {
                GameThread.Post(() =>
                {
                    try
                    {
                        Logger.Info($"ControllerService: Creating dialog");

                        /*
                         * var dialogTimeSpan = srv.Request.Dialog.Lifetime.ToTimeSpan();
                         * if (srv.Request.Dialog.Lifetime == default || dialogTimeSpan.TotalSeconds > 10)
                         * {
                         *  srv.Request.Dialog.Lifetime = TimeSpan.FromSeconds(10);
                         * }
                         */

                        var dialog = GuiDialogListener.DefaultHandler.AddDialog(srv.Request.Dialog);
                        if (dialog == null)
                        {
                            TryRelease(signal);
                            return;
                        }

                        dialog.ButtonClicked    += TriggerButton;
                        dialog.MenuEntryClicked += TriggerMenu;
                        dialog.Expired          += Expired;

                        void TriggerButton(ARDialog mDialog, int buttonId)
                        {
                            mDialog.ButtonClicked    -= TriggerButton;
                            mDialog.MenuEntryClicked -= TriggerMenu;
                            mDialog.Expired          -= Expired;

                            feedback.VizId        = ConnectionManager.MyId ?? "";
                            feedback.Id           = mDialog.Id ?? "";
                            feedback.FeedbackType = FeedbackType.ButtonClick;
                            feedback.EntryId      = buttonId;
                            overrideExpired       = true;

                            TryRelease(signal);
                        }

                        void TriggerMenu(ARDialog mDialog, int buttonId)
                        {
                            mDialog.ButtonClicked    -= TriggerButton;
                            mDialog.MenuEntryClicked -= TriggerMenu;
                            mDialog.Expired          -= Expired;

                            feedback.VizId        = ConnectionManager.MyId ?? "";
                            feedback.Id           = mDialog.Id ?? "";
                            feedback.FeedbackType = FeedbackType.MenuEntryClick;
                            feedback.EntryId      = buttonId;
                            overrideExpired       = true;

                            TryRelease(signal);
                        }

                        void Expired(ARDialog mDialog)
                        {
                            if (overrideExpired)
                            {
                                return;
                            }

                            mDialog.ButtonClicked    -= TriggerButton;
                            mDialog.MenuEntryClicked -= TriggerMenu;
                            mDialog.Expired          -= Expired;

                            feedback.VizId        = ConnectionManager.MyId ?? "";
                            feedback.Id           = mDialog.Id ?? "";
                            feedback.FeedbackType = FeedbackType.Expired;
                            feedback.EntryId      = 0;

                            TryRelease(signal);
                        }
                    }
                    catch (Exception e)
                    {
                        srv.Response.Success = false;
                        srv.Response.Message = $"EE An exception was raised: {e.Message}";
                        signal.Release();
                    }
                });

                /*
                 * if (!await signal.WaitAsync(10000))
                 * {
                 *  srv.Response.Success = false;
                 *  srv.Response.Message = "EE Request timed out!";
                 *  return;
                 * }
                 */
                await signal.WaitAsync();

                if (string.IsNullOrEmpty(srv.Response.Message))
                {
                    srv.Response.Success  = true;
                    srv.Response.Feedback = feedback;
                }
            }
        }
Exemplo n.º 8
0
        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!"));
            }
        }