diff --git a/src/communicationmanager.ts b/src/communicationmanager.ts index 783c4d5..1c30c39 100644 --- a/src/communicationmanager.ts +++ b/src/communicationmanager.ts @@ -174,6 +174,7 @@ export class CommunicationManager { //this is the main create //create a KIC terminal public createTerminal( + term_name: string, instrumentIp?: string, usb_unique_string?: string, filePath?: string @@ -184,7 +185,6 @@ export class CommunicationManager { if (instrumentIp != undefined) { const parts = instrumentIp.match(CONNECTION_RE) if (parts == null) return "" - const name = typeof parts[1] == "undefined" ? "KIC" : parts[1] const ip_addr = parts[2] const ip = ip_addr.split(":")[0] //take only IPv4 address, don't include socket. @@ -210,7 +210,7 @@ export class CommunicationManager { // }) info = this._kicProcessMgr.createKicCell( - name, + term_name, ip, "lan", maxerr, @@ -218,14 +218,12 @@ export class CommunicationManager { ) } else if (usb_unique_string != undefined) { let unique_string = usb_unique_string - let name = "KIC" const string_split = usb_unique_string.split("@") if (string_split.length > 1) { - name = string_split[0] unique_string = string_split[1] } info = this._kicProcessMgr.createKicCell( - name, + term_name, unique_string, "usb", undefined, diff --git a/src/extension.ts b/src/extension.ts index 30958ac..834e8c4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -54,28 +54,14 @@ export async function createTerminal( if (connection_string.split("@").length > 1) { name = connection_string.split("@")[0] ip = connection_string.split("@")[1] - } else { - const name_entered = await vscode.window.showInputBox({ - placeHolder: "Enter friendly name to proceed", - }) - if ( - //ToDo: need to add a common regex for all friendly name inputs - name_entered === undefined || - name_entered === null || - name_entered.length === 0 - ) { - void vscode.window.showErrorMessage( - "Cannot proceed with empty friendly name" - ) - return - } else { - name = name_entered - } } if (_connHelper.IPTest(ip) == false) { //USB //This only works if selected from Instrument discovery + if (name == "") { + name = FriendlyNameMgr.generateUniqueName(IoType.Usb, model_serial) + } if ( !FriendlyNameMgr.checkForDuplicateFriendlyName( IoType.Usb, @@ -86,6 +72,7 @@ export async function createTerminal( return } info = _activeConnectionManager?.createTerminal( + name, undefined, connection_string, command_text @@ -95,6 +82,12 @@ export async function createTerminal( msn = await _connHelper.getModelAndSerialNumber(ip) //const to let if (msn != undefined) { model_serial_no = msn.model + "#" + msn.sn //const to let + if (name == "") { + name = FriendlyNameMgr.generateUniqueName( + IoType.Lan, + model_serial_no + ) + } if ( !FriendlyNameMgr.checkForDuplicateFriendlyName( IoType.Lan, @@ -113,6 +106,7 @@ export async function createTerminal( msn.sn ) info = _activeConnectionManager?.createTerminal( + name, `${connection_string}:${msn.port}`, undefined, command_text @@ -121,6 +115,7 @@ export async function createTerminal( //TODO: Remove this else statement once lxi page is ready for versatest else { info = _activeConnectionManager?.createTerminal( + name, connection_string, undefined, command_text diff --git a/src/resourceManager.ts b/src/resourceManager.ts index bbaa4ad..27f3cef 100644 --- a/src/resourceManager.ts +++ b/src/resourceManager.ts @@ -118,6 +118,43 @@ export class FriendlyNameMgr { return handled } + public static generateUniqueName( + io_type: IoType, + model_serial: string | undefined + ): string { + let unique_name = "" + let found = false + const connections: Array = + vscode.workspace.getConfiguration("tsp").get("savedInstruments") ?? + [] + + if (connections.length > 0) { + connections.forEach((instr) => { + if ( + io_type === instr.io_type && + model_serial == instr.model + "#" + instr.serial_number + ) { + unique_name = instr.friendly_name + found = true + return + } + }) + } + + if (!found) { + const baseString = model_serial ?? "instrument" + let counter = 1 + let uniqueString = baseString + + while (connections.some((i) => i.friendly_name == uniqueString)) { + uniqueString = baseString + "_" + String(counter) + counter++ + } + unique_name = uniqueString + } + return unique_name + } + /** * method checks and adds/updates new friendly name *