Skip to content

Commit

Permalink
Task/tsp 757 default connection name (#38)
Browse files Browse the repository at this point in the history
Co-authored-by: GT, Shreya <[email protected]>
  • Loading branch information
Shreya-GT and GT, Shreya authored Jul 31, 2024
1 parent 8dc7a1b commit e69c8d2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

### Added
- Add logging for terminal and discover
- Default friendly name if user doesn't provide one (TSP-757)

## [0.16.4]

Expand Down
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
45 changes: 45 additions & 0 deletions src/resourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,51 @@ export class FriendlyNameMgr {
return handled
}

/**
* method generates unique friendly name for instrument if user
* does not provide one
*
* @param io_type - Lan, Usb etc.
* @param model_serial - model and serial number of instrument
* @returns - unique friendly name for given instrument
*/
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 e69c8d2

Please sign in to comment.