Commit 740e1801 authored by Sylvain Renault's avatar Sylvain Renault
Browse files

Draft implementation of the REST api and websockets logic.

parent 63e2d5d8
Loading
Loading
Loading
Loading
+109 −0
Original line number Diff line number Diff line
@@ -9,15 +9,17 @@ 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 TestWebSocketsClient : MonoBehaviour
public class ClientTest : MonoBehaviour
{
    static public string token = "ARF_Permission";
    static public string session = "HHI";

    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()
@@ -25,12 +27,17 @@ public class TestWebSocketsClient : MonoBehaviour
        if (asset1 != null) asset1.GetComponent<Renderer>().material.color = Color.red;

        rest.CheckServer();
        rest.PrintCapabilities();
        Capability[] caps;
        if (rest.GetCapabilities(token, out caps) == WorldAnalysisInterface.CapabilityResult.OK)
        {
            rest.PrintCapabilities(caps);
        }

        //websocketServer = rest.GetWebSocketEndpoint();
        websocketServer = "ws://localhost:61788/ws";
        int validity = 0;
        Guid subs;
        Guid anchor = Guid.Parse("fa8bbe40-8052-11ec-a8a3-0242ac120002");
        
        WebSocketConnect();
        rest.SubscribeToPose(token, anchor, Mode_WorldAnalysis.TRACKABLES_TO_DEVICE, PoseCallback, ref validity, out subs);
    }

    // Update is called once per frame
@@ -40,7 +47,7 @@ public class TestWebSocketsClient : MonoBehaviour
    {
        if (Keyboard.current.spaceKey.wasPressedThisFrame)
        {
            webSocket.Send("StartSendingPose:10");
            //webSocket.Send("PoseStart:10");
        }

        if (updateAsset1)
@@ -56,77 +63,47 @@ public class TestWebSocketsClient : MonoBehaviour
        asset1Color = c;
    }

    private void OnDestroy()
    public void SubscribeToPose()
    {
        if (websocketConnected) WebSocketClose();
    }
        Guid uuid = new Guid(); // request id from anchor or trackable
        Guid subId;
        int validity = 1;

    //
    // WebSocket methods
    //
    public void WebSocketClose()
        InformationSubscriptionResult response = rest.SubscribeToPose(token, uuid, Mode_WorldAnalysis.TRACKABLES_TO_DEVICE, PoseCallback, ref validity, out subId);
        if (response == InformationSubscriptionResult.OK)
        {
        webSocket.Send("unregister");
        webSocket.Close();
            // State: yellow
        }
    }

    public void WebSocketConnect()
    {
        webSocket = new WebSocketSharp.WebSocket(websocketServer.ToString());

        webSocket.OnOpen += (sender, e) =>
    public void PoseCallback(WorldAnalysisInterface.PoseEstimationResult result, ETSI.ARF.OpenAPI.WorldAnalysis.Pose pose)
    {
            websocketConnected = true;
            Debug.Log("[WS] Connected");
            webSocket.Send("client:UnitySceneManagement");
        };
        webSocket.OnClose += (sender, e) =>
        switch (result)
        {
            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();
    }
            case PoseEstimationResult.OK:
                // State: green
                //asset1.transform.rotation = WorldAnalysisUnityHelper.ConvertETSIARFQuaternionToUnity(pose.Value.ro...)
                break;
            
    bool ok = false;
    public void WebSocketHandleMessage(string data)
    {
        Debug.Log("[WS] Receiving: " + data);
            case PoseEstimationResult.NONE:
                // State: yellow
                break;
            
        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);
                }
            }
            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;
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -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
Compare cafd4e6c to 02020b50
Original line number Diff line number Diff line
Subproject commit cafd4e6c5d163bd637415b09531f072fb38f1644
Subproject commit 02020b5023a9b58f0fc78f88f6cd68287d236b00
Loading