Skip to content

Commit

Permalink
TSP-757 Implement default connection name if user doesn't enter one
Browse files Browse the repository at this point in the history
  • Loading branch information
GT, Shreya committed Jul 30, 2024
1 parent 8dc7a1b commit a6a7881
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
8 changes: 3 additions & 5 deletions src/communicationmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand All @@ -210,22 +210,20 @@ export class CommunicationManager {
// })

info = this._kicProcessMgr.createKicCell(
name,
term_name,
ip,
"lan",
maxerr,
filePath
)
} 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,
Expand Down
29 changes: 12 additions & 17 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -86,6 +72,7 @@ export async function createTerminal(
return
}
info = _activeConnectionManager?.createTerminal(
name,
undefined,
connection_string,
command_text
Expand All @@ -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,
Expand All @@ -113,6 +106,7 @@ export async function createTerminal(
msn.sn
)
info = _activeConnectionManager?.createTerminal(
name,
`${connection_string}:${msn.port}`,
undefined,
command_text
Expand All @@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/resourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<InstrInfo> =
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
*
Expand Down

0 comments on commit a6a7881

Please sign in to comment.