From d8656a78d4c7ade662be27b55a6590f8dee99d3c Mon Sep 17 00:00:00 2001 From: Anton Franzluebbers Date: Wed, 23 Aug 2023 18:40:51 -0400 Subject: [PATCH] fix some typos, change devicedata to userdata, change devicefieldlistener to use enum --- unity_package/Runtime/VELConnectManager.cs | 96 ++++++++++++---------- unity_package/package.json | 2 +- 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/unity_package/Runtime/VELConnectManager.cs b/unity_package/Runtime/VELConnectManager.cs index 2bdab21..aa3fdc6 100644 --- a/unity_package/Runtime/VELConnectManager.cs +++ b/unity_package/Runtime/VELConnectManager.cs @@ -34,7 +34,7 @@ namespace VELConnect public string id; public string username; public string created; - public string updted; + public string updated; } public class Device @@ -42,7 +42,6 @@ namespace VELConnect [CanBeNull] public readonly string id; [CanBeNull] public string created = null; [CanBeNull] public string updated = null; - [CanBeNull] public string device_id; [CanBeNull] public string os_info; [CanBeNull] public string friendly_name; [CanBeNull] public string modified_by; @@ -50,8 +49,10 @@ namespace VELConnect [CanBeNull] public string current_room; [CanBeNull] public string pairing_code; [CanBeNull] public string last_online; + [CanBeNull] public string owner; + [CanBeNull] public string[] past_owners; [CanBeNull] public DeviceExpand expand; - public DataBlock deviceData => expand?.data; + public DataBlock userData => expand?.data; public class DeviceExpand { @@ -65,7 +66,7 @@ namespace VELConnect public string TryGetData(string key) { string val = null; - return deviceData.data?.TryGetValue(key, out val) == true ? val : null; + return userData.data?.TryGetValue(key, out val) == true ? val : null; } } @@ -107,28 +108,33 @@ namespace VELConnect public enum DeviceField { - device_id, + id, os_info, friendly_name, modified_by, current_app, current_room, pairing_code, - last_online + last_online, + data, + owner, + past_owners } - public State lastState; - public State state; + private State lastState; + private State state; + + [CanBeNull] public static State CurrentState => instance == null ? null : instance.state; public static Action OnInitialState; - public static Action OnDeviceFieldChanged; - public static Action OnDeviceDataChanged; + public static Action OnDeviceFieldChanged; + public static Action OnUserDataChanged; public static Action OnRoomDataChanged; - private static readonly Dictionary> deviceFieldCallbacks = - new Dictionary>(); + private static readonly Dictionary> deviceFieldCallbacks = + new Dictionary>(); - private static readonly Dictionary> deviceDataCallbacks = + private static readonly Dictionary> userDataCallbacks = new Dictionary>(); private static readonly Dictionary> roomDataCallbacks = @@ -274,11 +280,13 @@ namespace VELConnect string oldValue = lastState != null ? fieldInfo.GetValue(lastState.device) as string : null; - if (newValue != oldValue) + + DeviceField fieldName; + if (Enum.TryParse(fieldInfo.Name, out fieldName) && newValue != oldValue) { try { - if (!isInitialState) OnDeviceFieldChanged?.Invoke(fieldInfo.Name, newValue); + if (!isInitialState) OnDeviceFieldChanged?.Invoke(fieldName, newValue); } catch (Exception e) { @@ -286,13 +294,13 @@ namespace VELConnect } // send specific listeners data - if (deviceFieldCallbacks.ContainsKey(fieldInfo.Name)) + if (deviceFieldCallbacks.ContainsKey(fieldName)) { // clear the list of old listeners - deviceFieldCallbacks[fieldInfo.Name].RemoveAll(e => e.keepAliveObject == null); + deviceFieldCallbacks[fieldName].RemoveAll(e => e.keepAliveObject == null); // send the callbacks - deviceFieldCallbacks[fieldInfo.Name].ForEach(e => + foreach (CallbackListener e in deviceFieldCallbacks[fieldName]) { if (!isInitialState || e.sendInitialState) { @@ -305,22 +313,22 @@ namespace VELConnect Debug.LogError(ex); } } - }); + } } } } - if (state.device.deviceData.data != null) + if (state.device.userData.data != null) { - foreach (KeyValuePair elem in state.device.deviceData.data) + foreach (KeyValuePair elem in state.device.userData.data) { string oldValue = null; - lastState?.device?.deviceData?.data?.TryGetValue(elem.Key, out oldValue); + lastState?.device?.userData?.data?.TryGetValue(elem.Key, out oldValue); if (elem.Value != oldValue) { try { - if (!isInitialState) OnDeviceDataChanged?.Invoke(elem.Key, elem.Value); + if (!isInitialState) OnUserDataChanged?.Invoke(elem.Key, elem.Value); } catch (Exception ex) { @@ -328,13 +336,13 @@ namespace VELConnect } // send specific listeners data - if (deviceDataCallbacks.ContainsKey(elem.Key)) + if (userDataCallbacks.ContainsKey(elem.Key)) { // clear the list of old listeners - deviceDataCallbacks[elem.Key].RemoveAll(e => e.keepAliveObject == null); + userDataCallbacks[elem.Key].RemoveAll(e => e.keepAliveObject == null); // send the callbacks - deviceDataCallbacks[elem.Key].ForEach(e => + foreach (CallbackListener e in userDataCallbacks[elem.Key]) { if (!isInitialState || e.sendInitialState) { @@ -347,7 +355,7 @@ namespace VELConnect Debug.LogError(ex); } } - }); + } } } } @@ -355,9 +363,10 @@ namespace VELConnect // on the initial state, also activate callbacks for null values if (isInitialState) { - foreach ((string deviceDataKey, List callbackList) in deviceDataCallbacks) + foreach ((string userDataKey, List callbackList) in + userDataCallbacks) { - if (!state.device.deviceData.data.ContainsKey(deviceDataKey)) + if (!state.device.userData.data.ContainsKey(userDataKey)) { // send the callbacks callbackList.ForEach(e => @@ -470,7 +479,8 @@ namespace VELConnect /// /// Adds a change listener callback to a particular field name within the Device main fields. /// - public static void AddDeviceFieldListener(string key, MonoBehaviour keepAliveObject, Action callback, + public static void AddDeviceFieldListener(DeviceField key, MonoBehaviour keepAliveObject, + Action callback, bool sendInitialState = false) { if (!deviceFieldCallbacks.ContainsKey(key)) @@ -489,7 +499,7 @@ namespace VELConnect { if (instance != null && instance.lastState?.device != null) { - if (instance.lastState.device.GetType().GetField(key) + if (instance.lastState.device.GetType().GetField(key.ToString()) ?.GetValue(instance.lastState.device) is string val) { try @@ -506,18 +516,18 @@ namespace VELConnect } /// - /// Adds a change listener callback to a particular field name within the Device data JSON. + /// Adds a change listener callback to a particular field name within the User data JSON. /// If the initial state doesn't contain this key, this sends back null /// - public static void AddDeviceDataListener(string key, MonoBehaviour keepAliveObject, Action callback, + public static void AddUserDataListener(string key, MonoBehaviour keepAliveObject, Action callback, bool sendInitialState = false) { - if (!deviceDataCallbacks.ContainsKey(key)) + if (!userDataCallbacks.ContainsKey(key)) { - deviceDataCallbacks[key] = new List(); + userDataCallbacks[key] = new List(); } - deviceDataCallbacks[key].Add(new CallbackListener() + userDataCallbacks[key].Add(new CallbackListener() { keepAliveObject = keepAliveObject, callback = callback, @@ -527,7 +537,7 @@ namespace VELConnect // if we have already received data, and we should send right away if (sendInitialState && instance.state != null) { - string val = GetDeviceData(key); + string val = GetUserData(key); try { callback(val); @@ -573,7 +583,7 @@ namespace VELConnect } } - public static string GetDeviceData(string key, string defaultValue = null) + public static string GetUserData(string key, string defaultValue = null) { return instance != null ? instance.lastState?.device?.TryGetData(key) : defaultValue; } @@ -629,23 +639,23 @@ namespace VELConnect /// /// Sets the 'data' object of the Device table /// - public static void SetDeviceData(Dictionary data) + public static void SetUserData(Dictionary data) { if (instance.state?.device != null) { foreach (string key in data.Keys.ToList()) { // if the value is unchanged from the current state, remove it so we don't double-update - if (instance.state.device.deviceData.data.TryGetValue(key, out string val) && val == data[key]) + if (instance.state.device.userData.data.TryGetValue(key, out string val) && val == data[key]) { data.Remove(key); } else { // update our local state, so we don't get change events on our own updates - if (instance.lastState?.device?.deviceData?.data != null) + if (instance.lastState?.device?.userData?.data != null) { - instance.lastState.device.deviceData.data[key] = data[key]; + instance.lastState.device.userData.data[key] = data[key]; } } } @@ -657,7 +667,7 @@ namespace VELConnect } // if we have no data, just set the whole thing - if (instance.lastState?.device?.deviceData != null) instance.lastState.device.deviceData.data ??= data; + if (instance.lastState?.device?.userData != null) instance.lastState.device.userData.data ??= data; } diff --git a/unity_package/package.json b/unity_package/package.json index bbf7a92..5b7208d 100644 --- a/unity_package/package.json +++ b/unity_package/package.json @@ -1,7 +1,7 @@ { "name": "edu.uga.engr.vel.vel-connect", "displayName": "VEL-Connect", - "version": "2.0.3", + "version": "2.1.0", "unity": "2019.1", "description": "Web-based configuration for VR applications", "keywords": [],