Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
using Newtonsoft.Json;
using WebSocketSharp;
using ETSI.ARF.WorldStorage;
using ETSI.ARF.WorldStorage.REST;
using ETSI.ARF.WorldAnalysis;
using ETSI.ARF.WorldAnalysis.REST;
using ETSI.ARF.OpenAPI.WorldAnalysis;
using Pose = ETSI.ARF.OpenAPI.WorldAnalysis.Pose;
using static WorldAnalysisInterface;
public class ServerMaintenanceClient : MonoBehaviour
{
static public string token = "dev";
static public string session = "HHI";
public GameObject floatingLogo;
/// <summary>
/// UUID of the node or trackable
/// </summary>
public string ARFMainNodeUUID;
[Space(8)]
public WorldStorageServer worldStorageServer;
public WorldAnalysisREST waRESTServer;
[Space(8)]
public TMP_Text console;
public GameObject asset1;
string consoleText = "<B>ETSI ISG-ARF Client Console (STF 669)</B>\n";
bool isConsoleText = false;
int validity = int.MaxValue;
// Track this trackable/anchor
Guid uuidTarget = Guid.Empty;
// Subscription id
Guid uuidSub = Guid.Empty;
// Thread safe
bool updateTransformMatrix = false;
bool updateTransformQuat = false;
Matrix4x4 matrix;
ETSI.ARF.OpenAPI.WorldAnalysis.VectorQuaternionPoseValue quat;
// Start is called before the first frame update
void Start()
{
Instance = waRESTServer;
if (asset1 != null) asset1.GetComponent<Renderer>().material.color = Color.red;
uuidTarget = Guid.Parse(ARFMainNodeUUID);
ETSI.ARF.WorldStorage.REST.AdminRequest.CheckServer(worldStorageServer);
ETSI.ARF.WorldAnalysis.REST.AdminRequest.CheckServer(waRESTServer.waServer);
consoleText += "World Storage server: " + worldStorageServer.URI + "... connected!\n";
consoleText += "World Analysis server: " + waRESTServer.waServer.URI + "... connected!\n";
isConsoleText = true;
Capability[] caps;
if (waRESTServer.GetCapabilities(token, out caps) == CapabilityResult.OK)
{
waRESTServer.PrintCapabilities(caps);
consoleText += "<B>Capabilities of World Analysis modules:</B>\n";
if (caps.Length == 0)
{
consoleText += "No capabilities!\n";
}
else foreach (var item in caps)
{
consoleText += " Type: " + item.TrackableType.ToString() + " Acc: " + item.Accuracy.ToString() + " FPS: " + item.Framerate + "\n";
}
consoleText += "\n";
isConsoleText = true;
}
consoleText += "Subscription of ARF objects (Trackables and World Anchrors)... OK\n";
isConsoleText = true;
/*
bool isSupported = false;
TypeWorldStorage wsType;
Capability[] capsOfUUID;
if (waRESTServer.GetCapability(token, uuidTarget, out isSupported, out wsType, out capsOfUUID) == CapabilityResult.OK)
{
waRESTServer.PrintCapabilities(capsOfUUID);
consoleText += "\nCapabilities for UUID=" + uuidTarget.ToString() + ":\n";
if (caps.Length == 0)
{
consoleText += "No capabilities!\n";
}
else foreach (var item in capsOfUUID)
{
consoleText += " " + item.TrackableType.ToString() + " fps=" + item.Framerate + "\n";
}
}
else consoleText += "No capabilities found!\n";
isConsoleText = true;
*/
}
// 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 (isConsoleText)
{
isConsoleText = false;
console.text = consoleText;
}
if (updateAsset1)
{
updateAsset1 = false;
//asset1.GetComponent<Renderer>().material.color = asset1Color;
}
// Thread safe
if (updateTransformMatrix)
{
updateTransformMatrix = false;
if (floatingLogo != null) floatingLogo.transform.SetPositionAndRotation(matrix.GetPosition(), matrix.rotation);
}
else if (updateTransformQuat)
{
updateTransformQuat = false;
if (floatingLogo != null) floatingLogo.transform.position = WorldAnalysisUnityHelper.ConvertETSIVector3ToUnity(quat.Position);
if (floatingLogo != null) floatingLogo.transform.rotation = WorldAnalysisUnityHelper.ConvertETSIARFQuaternionToUnity(quat.Rotation);
}
}
public void QuitApplication()
{
#if UNITY_EDITOR
// Application.Quit() does not work in the editor so
// UnityEditor.EditorApplication.isPlaying need to be set to false to end the game
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
private void OnDestroy()
{
SubscribeToPose(false);
}
public void SubscribeToPose(bool yes = true)
{
if (yes)
{
InformationSubscriptionResult response = waRESTServer.SubscribeToPose(token, uuidTarget, Mode_WorldAnalysis.TRACKABLES_TO_DEVICE, PoseCallback, ref validity, out uuidSub);
if (response == InformationSubscriptionResult.OK)
{
SetColor(Color.yellow);
}
else SetColor(Color.red);
}
else
{
if (uuidSub != Guid.Empty) waRESTServer.UnsubscribeFromPose(uuidSub);
uuidSub = Guid.Empty;
SetColor(Color.red);
}
}
public void OnButtonPoseStart()
{
SubscribeToPose(true);
}
public void OnButtonPoseStop()
{
SubscribeToPose(false);
}
public void OnButtonSendUserPose()
{
UnityEngine.Vector3 newPos = Camera.main.transform.position;
UnityEngine.Quaternion newRot = Camera.main.transform.rotation;
//Pose value
VectorQuaternionPoseValue newPoseValue = new VectorQuaternionPoseValue();
newPoseValue.Type = PoseValueType.VECTOR_QUATERNION;
newPoseValue.Unit = UnitSystem.M;
newPoseValue.Position = new ETSI.ARF.OpenAPI.WorldAnalysis.Vector3(newPos.x, newPos.y, newPos.z);
newPoseValue.Rotation = new ETSI.ARF.OpenAPI.WorldAnalysis.Quaternion(newRot.x, newRot.y, newRot.z, newRot.w);
Pose user = new Pose();
user.Uuid = Guid.Parse(worldStorageServer.currentUser.UUID);
user.Value = newPoseValue;
string json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
Debug.Log("[WS] Sending user's pose: " + json);
waRESTServer.WebSocketClient_Send("CurrentUserPose=" + json);
}
void SetColor(Color c)
{
asset1Color = c;
updateAsset1 = true;
}
public void MessageCallback(string messageFromServer)
{
//
// Is something from the WebSocket to be observed?
//
if (messageFromServer.StartsWith("Time="))
{
Debug.Log("[CLIENT] Server time is: " + messageFromServer.Split('=')[1]);
// test the communication
consoleText += "Server time is: " + messageFromServer.Split('=')[1] + "\n";
isConsoleText = true;
}
}
public void PoseCallback(PoseEstimationResult result, Pose pose)
{
switch (result)
{
case PoseEstimationResult.OK:
//Debug.Log($"Updating UUID { pose.Uuid } from subscription.");
SetColor(Color.green);
if (pose.Value.Type == PoseValueType.MATRIX)
{
// Workaround (Thread safe)
//MatrixPoseValue m = (MatrixPoseValue)pose.Value;
string v = JsonConvert.SerializeObject(pose.Value);
MatrixPoseValue value = JsonConvert.DeserializeObject<MatrixPoseValue>(v);
if (value.Transform.Count == 16)
{
matrix = WorldAnalysisUnityHelper.ConvertETSIARFTransform3DToUnity(value.Transform);
updateTransformMatrix = true;
}
else
{
// Ignore a wrong pose
}
}
else if (pose.Value.Type == ETSI.ARF.OpenAPI.WorldAnalysis.PoseValueType.VECTOR_QUATERNION)
{
// Workaround (Thread safe)
//VectorQuaternionPoseValue value = (ETSI.ARF.OpenAPI.WorldAnalysis.VectorQuaternionPoseValue)pose.Value;
string v = JsonConvert.SerializeObject(pose.Value);
quat = JsonConvert.DeserializeObject<VectorQuaternionPoseValue>(v);
updateTransformQuat = true;
}
else
{
Debug.LogWarning("Pose value type not supported yet :" + pose.Value.Type); // Todo : manage other types
}
break;
case PoseEstimationResult.NONE:
SetColor(Color.yellow);
break;
default:
SetColor(Color.red);
break;
}
}
}