예제 #1
0
        protected virtual void Start()
        {
            if (this.rosBridgeIP.Equals(string.Empty))
            {
                this.rosBridgeIP = ConfigManager.Instance.configInfo.rosbridgeIP;
            }
            if (this.rosBridgePort == 0)
            {
                this.rosBridgePort = ConfigManager.Instance.configInfo.rosbridgePort;
            }

            if (!RosConnectionManager.Instance.rosConnections.rosBridgeWebSocketConnectionMap.ContainsKey(topicName))
            {
                this.webSocketConnection = new SIGVerse.RosBridge.RosBridgeWebSocketConnection(rosBridgeIP, rosBridgePort);

                RosConnectionManager.Instance.rosConnections.rosBridgeWebSocketConnectionMap.Add(topicName, this.webSocketConnection);

                this.subscriber = this.webSocketConnection.Subscribe <Tmsg>(topicName, this.SubscribeMessageCallback);

                // Connect to ROSbridge server
                this.webSocketConnection.Connect <Tmsg>();
            }
            else
            {
                this.webSocketConnection = RosConnectionManager.Instance.rosConnections.rosBridgeWebSocketConnectionMap[topicName];

                this.subscriber = this.webSocketConnection.Subscribe <Tmsg>(topicName, this.SubscribeMessageCallback);
            }
        }
        /// <summary>
        /// Add a subscriber callback to this connection. There can be many subscribers.
        /// </summary>
        /// <typeparam name="Tmsg">Message type used in the callback</typeparam>
        /// <param name="sub">Subscriber</param>
        /// <param name="callback">Method to call when a message matching the given subscriber is received</param>
        public RosBridgeSubscriber <Tmsg> Subscribe <Tmsg>(string topic, RosMessageCallback <Tmsg> callback, uint queueSize = 0) where Tmsg : RosMessage, new()
        {
            MessageCallback MessageCallback = (RosMessage msg) =>
            {
                Tmsg message = msg as Tmsg;
                callback(message);
            };

            var getMessageType = typeof(Tmsg).GetMethod("GetMessageType");

            if (getMessageType == null)
            {
                Debug.LogError("Could not retrieve method GetMessageType() from " + typeof(Tmsg).ToString());
                return(null);
            }
            string messageType = (string)getMessageType.Invoke(null, null);

            if (messageType == null)
            {
                Debug.LogError("Could not retrieve valid message type from " + typeof(Tmsg).ToString());
                return(null);
            }

            RosBridgeSubscriber <Tmsg> subscriber = new RosBridgeSubscriber <Tmsg>(topic, messageType);

            this.subscribers.Add(subscriber, MessageCallback);
            this.msgQueue.Add(subscriber.Topic, new RenderQueue <MessageTask>(queueSize));

            if (this.IsConnected)
            {
                this.webSocket.Send(RosBridgeMsg.Subscribe(subscriber.Topic, subscriber.Type));
            }

            return(subscriber);
        }
        /// <summary>
        /// Remove a subscriber callback from this connection.
        /// </summary>
        /// <param name="subscriber"></param>
        public void Unsubscribe(RosBridgeSubscriber subscriber)
        {
            if (subscriber == null)
            {
                return;
            }

            this.subscribers.Remove(subscriber);
            this.msgQueue.Remove(subscriber.Topic);

            if (this.IsConnected)
            {
                this.webSocket.Send(RosBridgeMsg.UnSubscribe(subscriber.Topic));
            }
        }
 public MessageTask(RosBridgeSubscriber subscriber, RosMessage msg)
 {
     this.subscriber = subscriber;
     this.msg        = msg;
 }