From 5dd117cc3a4b21f1f63fc1be336a29cbf5e96ec0 Mon Sep 17 00:00:00 2001 From: Sylvain Renault Date: Thu, 13 Jun 2024 19:39:21 +0200 Subject: [PATCH] Scripts reviewed and updated for new WS Editor. Extended some functionalities/inheritances. --- Editor/Scripts/WorldStorageInfoEditor.cs | 2 +- Runtime/Scriptables/WorldStorageServer.cs | 2 +- Runtime/Scriptables/WorldStorageUser.cs | 2 +- Runtime/Scripts/OpenAPI/BaseClient.cs | 38 +++++++++++++++++ Runtime/Scripts/OpenAPI/BaseClient.cs.meta | 11 +++++ Runtime/Scripts/OpenAPI/DataModels.cs | 23 ++++++++++ Runtime/Scripts/OpenAPI/ResponseObject.cs | 32 +++++++------- .../OpenAPI/UnityWebRequestHttpClient.cs | 20 +++++++++ Runtime/Scripts/OpenAPI/WorldStorageClient.cs | 42 ++++--------------- Runtime/Scripts/WorldStorageInfo.cs | 2 +- Runtime/Scripts/WorldStorageUnityHelper.cs | 25 +++++++++++ .../Scripts/WorldStorageUnityHelper.cs.meta | 11 +++++ Runtime/Scripts/csc.rsp.meta | 2 +- 13 files changed, 159 insertions(+), 53 deletions(-) create mode 100644 Runtime/Scripts/OpenAPI/BaseClient.cs create mode 100644 Runtime/Scripts/OpenAPI/BaseClient.cs.meta create mode 100644 Runtime/Scripts/WorldStorageUnityHelper.cs create mode 100644 Runtime/Scripts/WorldStorageUnityHelper.cs.meta diff --git a/Editor/Scripts/WorldStorageInfoEditor.cs b/Editor/Scripts/WorldStorageInfoEditor.cs index 7d7e422..89cbd53 100644 --- a/Editor/Scripts/WorldStorageInfoEditor.cs +++ b/Editor/Scripts/WorldStorageInfoEditor.cs @@ -1,7 +1,7 @@ // // ARF - Augmented Reality Framework (ETSI ISG ARF) // -// Copyright 2022 ETSI +// Copyright 2024 ETSI // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/Runtime/Scriptables/WorldStorageServer.cs b/Runtime/Scriptables/WorldStorageServer.cs index 8b09622..01a2f1d 100644 --- a/Runtime/Scriptables/WorldStorageServer.cs +++ b/Runtime/Scriptables/WorldStorageServer.cs @@ -1,7 +1,7 @@ // // ARF - Augmented Reality Framework (ETSI ISG ARF) // -// Copyright 2022 ETSI +// Copyright 2024 ETSI // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/Runtime/Scriptables/WorldStorageUser.cs b/Runtime/Scriptables/WorldStorageUser.cs index d00b20a..cce6247 100644 --- a/Runtime/Scriptables/WorldStorageUser.cs +++ b/Runtime/Scriptables/WorldStorageUser.cs @@ -1,7 +1,7 @@ // // ARF - Augmented Reality Framework (ETSI ISG ARF) // -// Copyright 2022 ETSI +// Copyright 2024 ETSI // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/Runtime/Scripts/OpenAPI/BaseClient.cs b/Runtime/Scripts/OpenAPI/BaseClient.cs new file mode 100644 index 0000000..83fa8c3 --- /dev/null +++ b/Runtime/Scripts/OpenAPI/BaseClient.cs @@ -0,0 +1,38 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace ETSI.ARF.OpenAPI +{ + /// + /// Simple class to debug the requests + /// + public class BaseClient + { + static public bool EnableClientLog = false; + public string lastJsonText; + public long lastPayload; + + protected void _prepareRequest(ETSI.ARF.OpenAPI.IHttpClient client, System.Net.Http.HttpRequestMessage request, string url) + { + if (EnableClientLog) + { + Debug.Log("[REST][URL] Send request: " + client.BaseAddress + url); + Debug.Log("[REST][URL] Send request: " + request); + } + } + + protected void _processResponse(ETSI.ARF.OpenAPI.IHttpClient client, System.Net.Http.HttpResponseMessage response) + { + lastJsonText = response.Content.ReadAsStringAsync().Result.ToString(); + lastPayload = response.Content.Headers.ContentLength.Value; + + var status_ = (int)response.StatusCode; + + if (EnableClientLog) + { + Debug.Log("[REST][Data] Status: " + status_ + " Response: " + client.BaseAddress + " Len: " + lastPayload + " JSON: " + lastJsonText); + } + } + } +} \ No newline at end of file diff --git a/Runtime/Scripts/OpenAPI/BaseClient.cs.meta b/Runtime/Scripts/OpenAPI/BaseClient.cs.meta new file mode 100644 index 0000000..d0fa65b --- /dev/null +++ b/Runtime/Scripts/OpenAPI/BaseClient.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2e0af995ecbb4654a9191f8157a1cf0e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts/OpenAPI/DataModels.cs b/Runtime/Scripts/OpenAPI/DataModels.cs index d017018..17f60fe 100644 --- a/Runtime/Scripts/OpenAPI/DataModels.cs +++ b/Runtime/Scripts/OpenAPI/DataModels.cs @@ -8,6 +8,23 @@ namespace ETSI.ARF.OpenAPI.WorldStorage public interface IModel { public System.Guid UUID { get; set; } + + public string ToJson(); + } + + // Class to monitor the server + public class Server : IModel + { + public System.Guid UUID { get; set; } + public string Name { get; set; } + + public Server(string name) + { + UUID = Guid.Empty; + Name = name; + } + + public string ToJson() { return JsonUtility.ToJson(this); } } // @@ -20,6 +37,8 @@ namespace ETSI.ARF.OpenAPI.WorldStorage UUID = Guid.NewGuid(); Name = name; } + + public string ToJson() { return JsonUtility.ToJson(this); } } public partial class WorldAnchor : IModel @@ -29,6 +48,8 @@ namespace ETSI.ARF.OpenAPI.WorldStorage UUID = Guid.NewGuid(); Name = name; } + + public string ToJson() { return JsonUtility.ToJson(this); } } public partial class WorldLink : IModel @@ -37,5 +58,7 @@ namespace ETSI.ARF.OpenAPI.WorldStorage { UUID = Guid.NewGuid(); } + + public string ToJson() { return JsonUtility.ToJson(this); } } } \ No newline at end of file diff --git a/Runtime/Scripts/OpenAPI/ResponseObject.cs b/Runtime/Scripts/OpenAPI/ResponseObject.cs index 4e6ffb7..2be59dc 100644 --- a/Runtime/Scripts/OpenAPI/ResponseObject.cs +++ b/Runtime/Scripts/OpenAPI/ResponseObject.cs @@ -15,13 +15,26 @@ using UnityEngine; namespace ETSI.ARF.OpenAPI { - public class ResponseObject + public class CancelToken + { + protected CancellationTokenSource tokenSource; + protected CancellationToken ct; + + public CancellationToken cancellationToken { get => ct; } + + public void Cancel() + { + tokenSource.Cancel(); + } + } + + public class ResponseObject : CancelToken { // Management stuffs static int ID = 0; public int transactionId = 0; public string message = ""; // custom message, type of data... - + // Time monitoring public TimeSpan DeltaTime { get => responseTime - requestTime; } public DateTime requestTime; @@ -30,34 +43,23 @@ namespace ETSI.ARF.OpenAPI // Incoming data public T result; public int payload; // size of data - + //public string result = ""; // text result //public object data = null; // custom result // Callback public Action> callback; - // Task cancelllation - public CancellationToken cancellationToken { get => ct; } - private CancellationTokenSource tokenSource; - private CancellationToken ct; - public ResponseObject(string msg, Action> func = null) { requestTime = DateTime.Now; - transactionId = ++ID; message = msg; - callback = func; + transactionId = ++ID; tokenSource = new CancellationTokenSource(); ct = tokenSource.Token; } - - public void Cancel() - { - tokenSource.Cancel(); - } } } \ No newline at end of file diff --git a/Runtime/Scripts/OpenAPI/UnityWebRequestHttpClient.cs b/Runtime/Scripts/OpenAPI/UnityWebRequestHttpClient.cs index 94466ce..c70ef4c 100644 --- a/Runtime/Scripts/OpenAPI/UnityWebRequestHttpClient.cs +++ b/Runtime/Scripts/OpenAPI/UnityWebRequestHttpClient.cs @@ -1,3 +1,23 @@ +// +// ARF - Augmented Reality Framework (ETSI ISG ARF) +// +// Copyright 2024 ETSI +// +// 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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. +// +// Last change: June 2024 +// + // Depends on UniTask to support cancellation token and GetAwaiter: https://github.com/Cysharp/UniTask // Otherwise, the code can be adapted using https://gist.github.com/krzys-h/9062552e33dd7bd7fe4a6c12db109a1a diff --git a/Runtime/Scripts/OpenAPI/WorldStorageClient.cs b/Runtime/Scripts/OpenAPI/WorldStorageClient.cs index fd66570..55a043e 100644 --- a/Runtime/Scripts/OpenAPI/WorldStorageClient.cs +++ b/Runtime/Scripts/OpenAPI/WorldStorageClient.cs @@ -1,23 +1,3 @@ -// -// ARF - Augmented Reality Framework (ETSI ISG ARF) -// -// Copyright 2024 ETSI -// -// 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 -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// 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. -// -// Last change: March 2024 -// - using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -25,28 +5,24 @@ using UnityEngine.Networking; namespace ETSI.ARF.OpenAPI.WorldStorage { - // SylR - public partial class WorldStorageClient + /// + /// Catch the pre/pos request methods from the autogenerated classes + /// + public partial class WorldStorageClient : BaseClient { - public string lastJsonText; - public long lastPayload; - - partial void PrepareRequest(IHttpClient client, System.Net.Http.HttpRequestMessage request, string url) + partial void PrepareRequest(ETSI.ARF.OpenAPI.IHttpClient client, System.Net.Http.HttpRequestMessage request, string url) { - // If needed to make some special things !!! + _prepareRequest(client, request, url); } - partial void PrepareRequest(IHttpClient client, System.Net.Http.HttpRequestMessage request, System.Text.StringBuilder urlBuilder) + partial void PrepareRequest(ETSI.ARF.OpenAPI.IHttpClient client, System.Net.Http.HttpRequestMessage request, System.Text.StringBuilder urlBuilder) { // do something... } - partial void ProcessResponse(IHttpClient client, System.Net.Http.HttpResponseMessage response) + partial void ProcessResponse(ETSI.ARF.OpenAPI.IHttpClient client, System.Net.Http.HttpResponseMessage response) { - lastJsonText = response.Content.ReadAsStringAsync().Result.ToString(); - lastPayload = response.Content.Headers.ContentLength.Value; - - // If needed to make some special things !!! + _processResponse(client, response); } } } \ No newline at end of file diff --git a/Runtime/Scripts/WorldStorageInfo.cs b/Runtime/Scripts/WorldStorageInfo.cs index 83421b5..ad81582 100644 --- a/Runtime/Scripts/WorldStorageInfo.cs +++ b/Runtime/Scripts/WorldStorageInfo.cs @@ -1,7 +1,7 @@ // // ARF - Augmented Reality Framework (ETSI ISG ARF) // -// Copyright 2022 ETSI +// Copyright 2024 ETSI // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/Runtime/Scripts/WorldStorageUnityHelper.cs b/Runtime/Scripts/WorldStorageUnityHelper.cs new file mode 100644 index 0000000..0a5478e --- /dev/null +++ b/Runtime/Scripts/WorldStorageUnityHelper.cs @@ -0,0 +1,25 @@ +using ETSI.ARF.OpenAPI.WorldStorage; +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class WorldStorageUnityHelper +{ + /// + /// Convert a float array of length 16 to a Matrix + /// + /// the values to convert + /// Converted Unity Matrix + public static ETSI.ARF.OpenAPI.WorldStorage.Transform3D ConvertUnityToETSIARFTransform3D(Matrix4x4 value) + { + Transform3D result = new Transform3D + { + value.m00, value.m01, value.m02, value.m03, + value.m10, value.m11, value.m12, value.m13, + value.m20, value.m21, value.m22, value.m23, + value.m30, value.m31, value.m32, value.m33, + }; + return result; + } +} diff --git a/Runtime/Scripts/WorldStorageUnityHelper.cs.meta b/Runtime/Scripts/WorldStorageUnityHelper.cs.meta new file mode 100644 index 0000000..8ef5dba --- /dev/null +++ b/Runtime/Scripts/WorldStorageUnityHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dc92958843a03644e8524fd2312c3f42 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts/csc.rsp.meta b/Runtime/Scripts/csc.rsp.meta index 8c85794..773242b 100644 --- a/Runtime/Scripts/csc.rsp.meta +++ b/Runtime/Scripts/csc.rsp.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: a94d6effd437d7842907e9a5cfd2732f +guid: 7cf3a1fe1b3964e41afa78147db793b0 DefaultImporter: externalObjects: {} userData: -- GitLab