Skip to content
Snippets Groups Projects
Commit 740e1801 authored by Sylvain Renault's avatar Sylvain Renault
Browse files

Draft implementation of the REST api and websockets logic.

parent 63e2d5d8
No related branches found
No related tags found
2 merge requests!2Move tracking prefab to package, add export assetbundle capabilities for...,!1Integration of NSwag interface in the package.
......@@ -9,28 +9,35 @@ 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()
{
if (asset1 != null) asset1.GetComponent<Renderer>().material.color = Color.red;
rest.CheckServer();
rest.PrintCapabilities();
//websocketServer = rest.GetWebSocketEndpoint();
websocketServer = "ws://localhost:61788/ws";
Capability[] caps;
if (rest.GetCapabilities(token, out caps) == WorldAnalysisInterface.CapabilityResult.OK)
{
rest.PrintCapabilities(caps);
}
WebSocketConnect();
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
......@@ -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()
{
if (websocketConnected) WebSocketClose();
}
//
// WebSocket methods
//
public void WebSocketClose()
{
webSocket.Send("unregister");
webSocket.Close();
}
public void WebSocketConnect()
public void SubscribeToPose()
{
webSocket = new WebSocketSharp.WebSocket(websocketServer.ToString());
Guid uuid = new Guid(); // request id from anchor or trackable
Guid subId;
int validity = 1;
webSocket.OnOpen += (sender, e) =>
{
websocketConnected = true;
Debug.Log("[WS] Connected");
webSocket.Send("client:UnitySceneManagement");
};
webSocket.OnClose += (sender, e) =>
InformationSubscriptionResult response = rest.SubscribeToPose(token, uuid, Mode_WorldAnalysis.TRACKABLES_TO_DEVICE, PoseCallback, ref validity, out subId);
if (response == InformationSubscriptionResult.OK)
{
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();
// State: yellow
}
}
bool ok = false;
public void WebSocketHandleMessage(string data)
public void PoseCallback(WorldAnalysisInterface.PoseEstimationResult result, ETSI.ARF.OpenAPI.WorldAnalysis.Pose pose)
{
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)
switch (result)
{
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.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;
}
}
}
......@@ -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
......
Subproject commit cafd4e6c5d163bd637415b09531f072fb38f1644
Subproject commit 02020b5023a9b58f0fc78f88f6cd68287d236b00
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment