/* © Siemens AG, 2017-2019 Author: Dr. Martin Bischoff (martin.bischoff@siemens.com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ using System; using System.Threading; using RosSharp.RosBridgeClient.Protocols; using UnityEngine; namespace RosSharp.RosBridgeClient { public class RosConnector : MonoBehaviour { public int SecondsTimeout = 10; public RosSocket RosSocket { get; private set; } public RosSocket.SerializerEnum Serializer; public Protocol protocol; public string RosBridgeServerUrl = "ws://192.168.0.1:9090"; public ManualResetEvent IsConnected { get; private set; } //modified public bool connected { get; set; } public virtual void Awake() { IsConnected = new ManualResetEvent(false); new Thread(ConnectAndWait).Start(); //modified connected = false; DontDestroyOnLoad(this.gameObject); } protected void ConnectAndWait() { RosSocket = ConnectToRos(protocol, RosBridgeServerUrl, OnConnected, OnClosed, Serializer); if (!IsConnected.WaitOne(SecondsTimeout * 1000)) Debug.LogWarning("Failed to connect to RosBridge at: " + RosBridgeServerUrl); } public static RosSocket ConnectToRos(Protocol protocolType, string serverUrl, EventHandler onConnected = null, EventHandler onClosed = null, RosSocket.SerializerEnum serializer = RosSocket.SerializerEnum.Microsoft) { IProtocol protocol = ProtocolInitializer.GetProtocol(protocolType, serverUrl); protocol.OnConnected += onConnected; protocol.OnClosed += onClosed; return new RosSocket(protocol, serializer); } private void OnApplicationQuit() { RosSocket.Close(); } private void OnConnected(object sender, EventArgs e) { IsConnected.Set(); Debug.Log("Connected to RosBridge: " + RosBridgeServerUrl); //modified connected = true; } private void OnClosed(object sender, EventArgs e) { IsConnected.Reset(); Debug.Log("Disconnected from RosBridge: " + RosBridgeServerUrl); //modified connected = false; } } }