From f6be6e9764e58720f6713309c359cd4291fc312a Mon Sep 17 00:00:00 2001 From: Kenneth Yang Date: Fri, 7 Feb 2025 15:47:03 -0800 Subject: [PATCH] Updated to new 2.0 API --- .../Scripts/EphysLink/CommunicationManager.cs | 24 ++- Assets/Scripts/EphysLink/EphysLinkModels.cs | 40 ++-- .../ManipulatorBehaviorController.cs | 14 +- .../UI/EphysLinkSettings/EphysLinkSettings.cs | 177 ++++++++---------- 4 files changed, 130 insertions(+), 125 deletions(-) diff --git a/Assets/Scripts/EphysLink/CommunicationManager.cs b/Assets/Scripts/EphysLink/CommunicationManager.cs index 9792a95c..fccef0a4 100644 --- a/Assets/Scripts/EphysLink/CommunicationManager.cs +++ b/Assets/Scripts/EphysLink/CommunicationManager.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.IO; using System.Linq; +using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers; using BestHTTP.SocketIO3; using UnityEngine; @@ -351,30 +352,33 @@ private async Awaitable GetVersion() } /// - /// Get the platform type. + /// Get the platform info. /// - /// Callback function to handle incoming platform type info. + /// Callback function to handle incoming platform info. /// Callback function to handle errors. - public void GetPlatformType(Action onSuccessCallback, Action onErrorCallback = null) + public void GetPlatformInfo(Action onSuccessCallback, Action onErrorCallback = null) { _connectionManager .Socket.ExpectAcknowledgement(data => { if (DataKnownAndNotEmpty(data)) - onSuccessCallback?.Invoke(data); + { + var parsedData = ParseJson(data); + onSuccessCallback?.Invoke(parsedData); + } else onErrorCallback?.Invoke(); }) - .Emit("get_platform_type"); + .Emit("get_platform_info"); } /// - /// Get the platform type. + /// Get the platform info. /// - /// Platform type. - public async Awaitable GetPlatformType() + /// Platform info. + public async Awaitable GetPlatformInfo() { - return await EmitAndGetStringResponse("get_platform_type", null); + return await EmitAndGetResponse("get_platform_info", null); } /// @@ -867,4 +871,4 @@ private static string ToJson(T data) #endregion } -} +} \ No newline at end of file diff --git a/Assets/Scripts/EphysLink/EphysLinkModels.cs b/Assets/Scripts/EphysLink/EphysLinkModels.cs index 039b30b5..238a4dc9 100644 --- a/Assets/Scripts/EphysLink/EphysLinkModels.cs +++ b/Assets/Scripts/EphysLink/EphysLinkModels.cs @@ -1,5 +1,5 @@ -using UnityEngine; using System; +using UnityEngine; [Serializable] public struct AngularResponse @@ -39,7 +39,16 @@ public struct EphysLinkOptions public int MpmPort; public string Serial; - public EphysLinkOptions(bool background, bool ignoreUpdates, string type, bool debug, bool useProxy, string proxyAddress, int mpmPort, string serial) + public EphysLinkOptions( + bool background, + bool ignoreUpdates, + string type, + bool debug, + bool useProxy, + string proxyAddress, + int mpmPort, + string serial + ) { Background = background; IgnoreUpdates = ignoreUpdates; @@ -52,24 +61,35 @@ public EphysLinkOptions(bool background, bool ignoreUpdates, string type, bool d } } - [Serializable] public struct GetManipulatorsResponse { public string[] Manipulators; - public int NumAxes; - public Vector4 Dimensions; public string Error; - public GetManipulatorsResponse(string[] manipulators, int numAxes, Vector4 dimensions, string error) + public GetManipulatorsResponse(string[] manipulators, string error) { Manipulators = manipulators; - NumAxes = numAxes; - Dimensions = dimensions; Error = error; } } +[Serializable] +public struct PlatformInfo +{ + public string Name; + public string CliName; + public int AxesCount; + public Vector4 Dimensions; + + public PlatformInfo(string name, string cliName, int axesCount, Vector4 dimensions) + { + Name = name; + CliName = cliName; + AxesCount = axesCount; + Dimensions = dimensions; + } +} [Serializable] public struct PositionalResponse @@ -125,7 +145,6 @@ public SetInsideBrainRequest(string manipulatorId, bool inside) } } - [Serializable] public struct SetPositionRequest { @@ -152,5 +171,4 @@ public ShankCountResponse(int shankCount, string error) ShankCount = shankCount; Error = error; } -} - +} \ No newline at end of file diff --git a/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController/ManipulatorBehaviorController.cs b/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController/ManipulatorBehaviorController.cs index 68590619..f3fedfd3 100644 --- a/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController/ManipulatorBehaviorController.cs +++ b/Assets/Scripts/Pinpoint/Probes/ManipulatorBehaviorController/ManipulatorBehaviorController.cs @@ -141,19 +141,13 @@ private void OnDisable() /// Whether this manipulator has been calibrated. public async void Initialize(string manipulatorID, bool calibrated) { - // Get manipulator information - var manipulatorResponse = await CommunicationManager.Instance.GetManipulators(); - if (CommunicationManager.HasError(manipulatorResponse.Error)) - return; - - // Shortcut exit if we have an invalid manipulator ID - if (!manipulatorResponse.Manipulators.Contains(manipulatorID)) - return; + // Get platform information + var platformInfoResponse = await CommunicationManager.Instance.GetPlatformInfo(); // Set manipulator ID, number of axes, and dimensions ManipulatorID = manipulatorID; - NumAxes = manipulatorResponse.NumAxes; - Dimensions = manipulatorResponse.Dimensions; + NumAxes = platformInfoResponse.AxesCount; + Dimensions = platformInfoResponse.Dimensions; // Update transform and space UpdateSpaceAndTransform(); diff --git a/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs b/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs index 7e8f6d88..1e1c8997 100644 --- a/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs +++ b/Assets/Scripts/Pinpoint/UI/EphysLinkSettings/EphysLinkSettings.cs @@ -20,7 +20,8 @@ public class EphysLinkSettings : MonoBehaviour { #region Constants - private const string EPHYS_LINK_NAME = "EphysLink-v2.0.0b7"; + private const string EPHYS_LINK_NAME = "EphysLink-v2.0.0rc0"; + private static string EphysLinkExePath => Path.Combine( Application.streamingAssetsPath, @@ -32,51 +33,36 @@ public class EphysLinkSettings : MonoBehaviour #region Components // Server connection - [SerializeField] - private TMP_Dropdown _manipulatorTypeDropdown; + [SerializeField] private TMP_Dropdown _manipulatorTypeDropdown; - [SerializeField] - private TMP_InputField _pathfinderPortInputField; + [SerializeField] private TMP_InputField _pathfinderPortInputField; - [SerializeField] - private Button _launchEphysLinkButton; + [SerializeField] private Button _launchEphysLinkButton; - [SerializeField] - private GameObject _existingServerGroup; + [SerializeField] private GameObject _existingServerGroup; - [SerializeField] - private TMP_InputField _ipAddressInputField; + [SerializeField] private TMP_InputField _ipAddressInputField; - [SerializeField] - private InputField _portInputField; + [SerializeField] private InputField _portInputField; - [SerializeField] - private GameObject _proxyServerGroup; + [SerializeField] private GameObject _proxyServerGroup; - [SerializeField] - private TMP_InputField _proxyAddressInputField; + [SerializeField] private TMP_InputField _proxyAddressInputField; - [SerializeField] - private TMP_InputField _pinpointIDInputField; + [SerializeField] private TMP_InputField _pinpointIDInputField; - [SerializeField] - private GameObject _connectButton; + [SerializeField] private GameObject _connectButton; - [SerializeField] - private Text _connectButtonText; + [SerializeField] private Text _connectButtonText; - [SerializeField] - private TMP_Text _connectionErrorText; + [SerializeField] private TMP_Text _connectionErrorText; // Manipulators - [SerializeField] - private GameObject _manipulatorList; + [SerializeField] private GameObject _manipulatorList; - [SerializeField] - private GameObject _manipulatorConnectionPanelPrefab; + [SerializeField] private GameObject _manipulatorConnectionPanelPrefab; - [SerializeField] - private Toggle _copilotToggle; + [SerializeField] private Toggle _copilotToggle; private UIManager _uiManager; @@ -85,7 +71,6 @@ public class EphysLinkSettings : MonoBehaviour #endregion #region Properties - private readonly Dictionary< string, @@ -149,74 +134,78 @@ private void UpdateManipulatorPanels() if (CommunicationManager.Instance.IsConnected) { - CommunicationManager.Instance.GetManipulators( - (response) => - { - // Keep track of handled manipulator panels - var handledManipulatorIds = new HashSet(); - - // Add any new manipulators in scene to list - foreach (var manipulatorID in response.Manipulators) + CommunicationManager.Instance.GetPlatformInfo(info => + { + CommunicationManager.Instance.GetManipulators( + (response) => { - // Create new manipulator connection settings panel if the manipulator is new - if ( - !_manipulatorIdToManipulatorConnectionSettingsPanel.ContainsKey( - manipulatorID + // Keep track of handled manipulator panels + var handledManipulatorIds = new HashSet(); + + // Add any new manipulators in scene to list + foreach (var manipulatorID in response.Manipulators) + { + // Create new manipulator connection settings panel if the manipulator is new + if ( + !_manipulatorIdToManipulatorConnectionSettingsPanel.ContainsKey( + manipulatorID + ) ) + { + // Instantiate panel + var manipulatorConnectionSettingsPanelGameObject = Instantiate( + _manipulatorConnectionPanelPrefab, + _manipulatorList.transform + ); + var manipulatorConnectionSettingsPanel = + manipulatorConnectionSettingsPanelGameObject + .GetComponent(); + + // Set manipulator id + manipulatorConnectionSettingsPanel.Initialize( + this, + manipulatorID, + info.AxesCount + ); + + // Add to dictionary + _manipulatorIdToManipulatorConnectionSettingsPanel.Add( + manipulatorID, + new ValueTuple( + manipulatorConnectionSettingsPanel, + manipulatorConnectionSettingsPanelGameObject + ) + ); + } + + // Mark ID as handled + handledManipulatorIds.Add(manipulatorID); + } + + // Remove any manipulators that are not connected anymore + foreach ( + var disconnectedManipulator in _manipulatorIdToManipulatorConnectionSettingsPanel + .Keys.Except(handledManipulatorIds) + .ToList() ) { - // Instantiate panel - var manipulatorConnectionSettingsPanelGameObject = Instantiate( - _manipulatorConnectionPanelPrefab, - _manipulatorList.transform + _manipulatorIdToManipulatorConnectionSettingsPanel.Remove( + disconnectedManipulator ); - var manipulatorConnectionSettingsPanel = - manipulatorConnectionSettingsPanelGameObject.GetComponent(); - - // Set manipulator id - manipulatorConnectionSettingsPanel.Initialize( - this, - manipulatorID, - response.NumAxes - ); - - // Add to dictionary - _manipulatorIdToManipulatorConnectionSettingsPanel.Add( - manipulatorID, - new ValueTuple( - manipulatorConnectionSettingsPanel, - manipulatorConnectionSettingsPanelGameObject - ) + Destroy( + _manipulatorIdToManipulatorConnectionSettingsPanel[ + disconnectedManipulator + ].gameObject ); } - // Mark ID as handled - handledManipulatorIds.Add(manipulatorID); + // Reorder panels to match order of availableIds + foreach (var manipulatorId in response.Manipulators) + _manipulatorIdToManipulatorConnectionSettingsPanel[manipulatorId] + .gameObject.transform.SetAsLastSibling(); } - - // Remove any manipulators that are not connected anymore - foreach ( - var disconnectedManipulator in _manipulatorIdToManipulatorConnectionSettingsPanel - .Keys.Except(handledManipulatorIds) - .ToList() - ) - { - _manipulatorIdToManipulatorConnectionSettingsPanel.Remove( - disconnectedManipulator - ); - Destroy( - _manipulatorIdToManipulatorConnectionSettingsPanel[ - disconnectedManipulator - ].gameObject - ); - } - - // Reorder panels to match order of availableIds - foreach (var manipulatorId in response.Manipulators) - _manipulatorIdToManipulatorConnectionSettingsPanel[manipulatorId] - .gameObject.transform.SetAsLastSibling(); - } - ); + ); + }); } else { @@ -396,7 +385,7 @@ public void InvokeShouldUpdateProbesListEvent() { ShouldUpdateProbesListEvent.Invoke(); } - + public void ToggleNewUI(bool isEnabled) { _newUI.SetActive(isEnabled); @@ -475,7 +464,7 @@ private void UpdateConnectionPanel() : "Connect"; _connectButton.SetActive( CommunicationManager.Instance.IsConnected - || _manipulatorTypeDropdown.value >= _manipulatorTypeDropdown.options.Count - 2 + || _manipulatorTypeDropdown.value >= _manipulatorTypeDropdown.options.Count - 2 ); _manipulatorTypeDropdown.interactable = !CommunicationManager.Instance.IsConnected; @@ -496,4 +485,4 @@ private void KillEphysLinkProcess() #endregion } -} +} \ No newline at end of file