From 740e1801c1aa990eab9ff8c5a68b57e82ee4d533 Mon Sep 17 00:00:00 2001 From: Sylvain Renault <sylvain.renault@hhi.fraunhofer.de> Date: Thu, 22 Aug 2024 18:41:46 +0200 Subject: [PATCH] Draft implementation of the REST api and websockets logic. --- Assets/HHI/Scripts/ClientTest.cs | 109 +++++++++++++++ ...cketsClient.cs.meta => ClientTest.cs.meta} | 0 .../{WAClientTest.unity => ClientTest.unity} | 2 +- ...tTest.unity.meta => ClientTest.unity.meta} | 0 Assets/HHI/Scripts/TestAPIWebSocketsClient.cs | 132 ------------------ Packages/unity-world-analysis-package | 2 +- 6 files changed, 111 insertions(+), 134 deletions(-) create mode 100644 Assets/HHI/Scripts/ClientTest.cs rename Assets/HHI/Scripts/{TestAPIWebSocketsClient.cs.meta => ClientTest.cs.meta} (100%) rename Assets/HHI/Scripts/{WAClientTest.unity => ClientTest.unity} (99%) rename Assets/HHI/Scripts/{WAClientTest.unity.meta => ClientTest.unity.meta} (100%) delete mode 100644 Assets/HHI/Scripts/TestAPIWebSocketsClient.cs diff --git a/Assets/HHI/Scripts/ClientTest.cs b/Assets/HHI/Scripts/ClientTest.cs new file mode 100644 index 0000000..d429c9f --- /dev/null +++ b/Assets/HHI/Scripts/ClientTest.cs @@ -0,0 +1,109 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +using WebSocketSharp; + +using ETSI.ARF.WorldAnalysis; +using ETSI.ARF.WorldAnalysis.REST; +using ETSI.ARF.OpenAPI.WorldAnalysis; +using Pose = ETSI.ARF.OpenAPI.WorldAnalysis.Pose; +using UnityEngine.InputSystem; +using System; +using static WorldAnalysisInterface; + +public class ClientTest : MonoBehaviour +{ + static public string token = "ARF_Permission"; + static public string session = "HHI"; + + public WorldAnalysisREST rest; + public GameObject asset1; + + + // Start is called before the first frame update + void Start() + { + if (asset1 != null) asset1.GetComponent<Renderer>().material.color = Color.red; + + rest.CheckServer(); + Capability[] caps; + if (rest.GetCapabilities(token, out caps) == WorldAnalysisInterface.CapabilityResult.OK) + { + rest.PrintCapabilities(caps); + } + + int validity = 0; + Guid subs; + Guid anchor = Guid.Parse("fa8bbe40-8052-11ec-a8a3-0242ac120002"); + + rest.SubscribeToPose(token, anchor, Mode_WorldAnalysis.TRACKABLES_TO_DEVICE, PoseCallback, ref validity, out subs); + } + + // Update is called once per frame + bool updateAsset1 = false; + Color asset1Color = Color.grey; + void Update() + { + if (Keyboard.current.spaceKey.wasPressedThisFrame) + { + //webSocket.Send("PoseStart:10"); + } + + if (updateAsset1) + { + updateAsset1 = false; + asset1.GetComponent<Renderer>().material.color = asset1Color; + } + } + + void SetColor(Color c) + { + updateAsset1 = true; + asset1Color = c; + } + + public void SubscribeToPose() + { + Guid uuid = new Guid(); // request id from anchor or trackable + Guid subId; + int validity = 1; + + InformationSubscriptionResult response = rest.SubscribeToPose(token, uuid, Mode_WorldAnalysis.TRACKABLES_TO_DEVICE, PoseCallback, ref validity, out subId); + if (response == InformationSubscriptionResult.OK) + { + // State: yellow + } + } + + public void PoseCallback(WorldAnalysisInterface.PoseEstimationResult result, ETSI.ARF.OpenAPI.WorldAnalysis.Pose pose) + { + switch (result) + { + case PoseEstimationResult.OK: + // State: green + //asset1.transform.rotation = WorldAnalysisUnityHelper.ConvertETSIARFQuaternionToUnity(pose.Value.ro...) + break; + + case PoseEstimationResult.NONE: + // State: yellow + break; + + case PoseEstimationResult.NOT_ALLOWED: + // State: red + break; + + case PoseEstimationResult.FAILURE: + // State: red + break; + + case PoseEstimationResult.NOT_SUPPORTED: + // State: red + break; + + case PoseEstimationResult.UNKNOWN_ID: + // State: red + break; + } + } +} diff --git a/Assets/HHI/Scripts/TestAPIWebSocketsClient.cs.meta b/Assets/HHI/Scripts/ClientTest.cs.meta similarity index 100% rename from Assets/HHI/Scripts/TestAPIWebSocketsClient.cs.meta rename to Assets/HHI/Scripts/ClientTest.cs.meta diff --git a/Assets/HHI/Scripts/WAClientTest.unity b/Assets/HHI/Scripts/ClientTest.unity similarity index 99% rename from Assets/HHI/Scripts/WAClientTest.unity rename to Assets/HHI/Scripts/ClientTest.unity index f1aac87..bdf6439 100644 --- a/Assets/HHI/Scripts/WAClientTest.unity +++ b/Assets/HHI/Scripts/ClientTest.unity @@ -334,7 +334,7 @@ GameObject: - component: {fileID: 960712812} - component: {fileID: 960712814} m_Layer: 0 - m_Name: WAClient + m_Name: WA ClientTest m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 diff --git a/Assets/HHI/Scripts/WAClientTest.unity.meta b/Assets/HHI/Scripts/ClientTest.unity.meta similarity index 100% rename from Assets/HHI/Scripts/WAClientTest.unity.meta rename to Assets/HHI/Scripts/ClientTest.unity.meta diff --git a/Assets/HHI/Scripts/TestAPIWebSocketsClient.cs b/Assets/HHI/Scripts/TestAPIWebSocketsClient.cs deleted file mode 100644 index 0396f02..0000000 --- a/Assets/HHI/Scripts/TestAPIWebSocketsClient.cs +++ /dev/null @@ -1,132 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -using WebSocketSharp; - -using ETSI.ARF.WorldAnalysis; -using ETSI.ARF.WorldAnalysis.REST; -using ETSI.ARF.OpenAPI.WorldAnalysis; -using Pose = ETSI.ARF.OpenAPI.WorldAnalysis.Pose; -using UnityEngine.InputSystem; - -public class TestWebSocketsClient : MonoBehaviour -{ - public WorldAnalysisREST rest; - public GameObject asset1; - - private WebSocketSharp.WebSocket webSocket; - string websocketServer; - bool websocketConnected = false; - - // Start is called before the first frame update - void Start() - { - if (asset1 != null) asset1.GetComponent<Renderer>().material.color = Color.red; - - rest.CheckServer(); - rest.PrintCapabilities(); - - //websocketServer = rest.GetWebSocketEndpoint(); - websocketServer = "ws://localhost:61788/ws"; - - WebSocketConnect(); - } - - // Update is called once per frame - bool updateAsset1 = false; - Color asset1Color = Color.grey; - void Update() - { - if (Keyboard.current.spaceKey.wasPressedThisFrame) - { - webSocket.Send("StartSendingPose:10"); - } - - if (updateAsset1) - { - updateAsset1 = false; - asset1.GetComponent<Renderer>().material.color = asset1Color; - } - } - - void SetColor(Color c) - { - updateAsset1 = true; - asset1Color = c; - } - - private void OnDestroy() - { - if (websocketConnected) WebSocketClose(); - } - - // - // WebSocket methods - // - public void WebSocketClose() - { - webSocket.Send("unregister"); - webSocket.Close(); - } - - public void WebSocketConnect() - { - webSocket = new WebSocketSharp.WebSocket(websocketServer.ToString()); - - webSocket.OnOpen += (sender, e) => - { - websocketConnected = true; - Debug.Log("[WS] Connected"); - webSocket.Send("client:UnitySceneManagement"); - }; - webSocket.OnClose += (sender, e) => - { - websocketConnected = false; - SetColor(Color.red); - Debug.Log("[WS] Disconnected"); - }; - webSocket.OnError += (sender, e) => Debug.Log("[WS] Error!"); - webSocket.OnMessage += (sender, e) => WebSocketHandleMessage(e.Data); - webSocket.Connect(); - } - - bool ok = false; - public void WebSocketHandleMessage(string data) - { - Debug.Log("[WS] Receiving: " + data); - - if (data.Contains("You are now registered")) - { - ok = true; - SetColor(Color.yellow); - webSocket.Send("StartSendingPose:10"); - } - else if (data == "Stop") - { - SetColor(Color.yellow); - //ok = false; - } - else if (ok) - { - if (data.Contains("estimationState")) - { - // Handle pose - Pose p = JsonUtility.FromJson<Pose>(data); - Debug.Log("[WS][Pose] State: " + p.EstimationState.ToString()); - if (p.EstimationState == PoseEstimationState.OK) - { - if (asset1 != null) - { - SetColor(Color.green); - //asset1.transform.rotation = WorldAnalysisUnityHelper.ConvertETSIARFQuaternionToUnity(p.Value.ro) - } - } - else - { - SetColor(Color.yellow); - } - } - } - } -} diff --git a/Packages/unity-world-analysis-package b/Packages/unity-world-analysis-package index cafd4e6..02020b5 160000 --- a/Packages/unity-world-analysis-package +++ b/Packages/unity-world-analysis-package @@ -1 +1 @@ -Subproject commit cafd4e6c5d163bd637415b09531f072fb38f1644 +Subproject commit 02020b5023a9b58f0fc78f88f6cd68287d236b00 -- GitLab