Skip to content

Commit

Permalink
TSP-839 Fix issue with naming of instrument connection (#64)
Browse files Browse the repository at this point in the history
When we connect to an instrument, the information (io_type,
instr_address, model, serial_number, friendly_name etc.) is saved to
settings.json. Any item in Instruments pane (discovered or saved) is
initially created with label <model#serial_number>. For saved
instruments we need to update this to any connection_name if specified
by user and this information is fetched from settings.json. Since the
settings.json was not updated correctly due to not awaiting, the
Instruments pane showed wrong label as compared to terminal name. This
is fixed by awaiting when settings.json file is updated.

---------

Co-authored-by: GT, Shreya <[email protected]>
  • Loading branch information
Shreya-GT and GT, Shreya authored Oct 21, 2024
1 parent b7a5cc3 commit 7c273b0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
### Fixed

- Fix issue where connecting to an instrument can fail with a mysterious error message
- Connection name needs to be same in Instruments pane, terminal and quick pick for a given instrument connection (TSP-839)

## [0.18.2]

Expand Down
38 changes: 27 additions & 11 deletions src/communicationmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ export class CommunicationManager {
private async sendScript(_e: unknown) {
const uriObject = _e as vscode.Uri
const filePath = `.script "${uriObject.fsPath}"`
await this.handleSendTextToTerminal(filePath).then(() => {
void vscode.window.showInformationMessage(
"Sending script to terminal",
)
})
await this.handleSendTextToTerminal(filePath).then(
() => {
void vscode.window.showInformationMessage(
"Sending script to terminal",
)
},
(reason) => {
void vscode.window.showErrorMessage(reason as string)
},
)
}

/**
Expand Down Expand Up @@ -119,16 +124,27 @@ export class CommunicationManager {
this._connHelper.instrConnectionStringValidator,
}
const Ip = await vscode.window.showInputBox(options)
let isConnSuccessful = false
let err_msg = ""

if (Ip == undefined) {
return Promise.reject(new Error("IP is undefined"))
} else {
if (createTerminal(Ip, undefined, text))
return Promise.resolve(true)
else
return Promise.reject(
new Error("Unable to connect to instrument"),
)
await createTerminal(Ip, undefined, text).then(
() => {
isConnSuccessful = true
},
(reason) => {
err_msg = reason as string
isConnSuccessful = false
},
)
}

if (isConnSuccessful) {
return Promise.resolve(true)
} else {
return Promise.reject(new Error(err_msg))
}
}
} else if (kicTerminals.length === 1) {
Expand Down
32 changes: 22 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ let _connHelper: ConnectionHelper
* @param command_text command text that needs to send to terminal
* @returns None
*/
export function createTerminal(
export async function createTerminal(
connection_string: string,
model_serial?: string,
command_text?: string,
): boolean {
): Promise<boolean> {
const LOGLOC: SourceLocation = {
file: "extension.ts",
func: `createTerminal("${connection_string}", "${model_serial}", "${command_text}")`,
Expand Down Expand Up @@ -82,7 +82,7 @@ export function createTerminal(
void vscode.window.showErrorMessage(
"Unable to connect to instrument",
)
return false
return Promise.reject(new Error("Unable to connect to instrument"))
}
name = res[1] == undefined ? name : res[1]
const port_number = "5025"
Expand All @@ -92,7 +92,13 @@ export function createTerminal(
LOGLOC,
)
Log.trace("Saving connection", LOGLOC)
_instrExplorer.saveWhileConnect(ip, IoType.Lan, info, name, port_number)
await _instrExplorer.saveWhileConnect(
ip,
IoType.Lan,
info,
name,
port_number,
)
} else {
Log.debug("Connection type was determined to be VISA", LOGLOC)
//VISA
Expand All @@ -113,7 +119,7 @@ export function createTerminal(
void vscode.window.showErrorMessage(
"Unable to connect to instrument",
)
return false
return Promise.reject(new Error("Unable to connect to instrument"))
}
name = res[1] == undefined ? name : res[1]

Expand All @@ -123,9 +129,15 @@ export function createTerminal(
)

Log.trace("Saving connection", LOGLOC)
_instrExplorer.saveWhileConnect(ip, IoType.Visa, info, name, undefined)
await _instrExplorer.saveWhileConnect(
ip,
IoType.Visa,
info,
name,
undefined,
)
}
return true
return Promise.resolve(true)
}

// Called when the extension is activated.
Expand Down Expand Up @@ -478,7 +490,7 @@ async function pickConnection(connection_info?: string): Promise<void> {
(option) => option.label === selectedItem.label,
)
) {
createTerminal(selectedItem.label)
await createTerminal(selectedItem.label)
} else {
const Ip = selectedItem.label
if (Ip === undefined) {
Expand Down Expand Up @@ -518,7 +530,7 @@ async function connect(
}

if (_activeConnectionManager?.connectionRE.test(Ip)) {
createTerminal(Ip, model_serial)
await createTerminal(Ip, model_serial)
} else {
void vscode.window.showErrorMessage("Bad connection string")
}
Expand Down Expand Up @@ -550,7 +562,7 @@ function connectCmd(def: object) {

if (_activeConnectionManager?.connectionRE.test(connection_str)) {
Log.trace("Connection string is valid. Creating Terminal", LOGLOC)
createTerminal(connection_str, model_serial)
void createTerminal(connection_str, model_serial)
} else {
Log.error(
"Connection string is invalid. Unable to connect to instrument.",
Expand Down
45 changes: 25 additions & 20 deletions src/instruments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,8 @@ export class NewTDPModel {
// if any saved instrument is discovered and if the instrument address
// has changed, we need to update it
this.checkForSavedInstrIPChange()
.then(() => {})
.catch(() => {})

this.connection_list.forEach((instr) => {
const ret =
Expand Down Expand Up @@ -740,7 +742,10 @@ export class NewTDPModel {
}

//add from connect
public addFromConnectToSavedList(ioType: IoType, instr_details: InstrInfo) {
public async addFromConnectToSavedList(
ioType: IoType,
instr_details: InstrInfo,
) {
const LOGLOC: SourceLocation = {
file: "instruments.ts",
func: `NewTDPModel.addFromConnectToSavedList("${ioType.toString()}", "${String(instr_details)}")`,
Expand All @@ -752,7 +757,7 @@ export class NewTDPModel {
)
this.addToConnectionList(instr_details)

this.saveInstrInfoToPersist(instr_details)
await this.saveInstrInfoToPersist(instr_details)

const saved_list = this._savedNodeProvider?.getSavedInstrList()

Expand Down Expand Up @@ -781,7 +786,7 @@ export class NewTDPModel {
Log.warn("Instrument details not provided", LOGLOC)
}

public addSavedList(instr: unknown) {
public async addSavedList(instr: unknown) {
const nodeToBeSaved = instr as IOInstrNode
if (nodeToBeSaved != undefined) {
this._savedNodeProvider?.saveInstrToList(
Expand All @@ -792,7 +797,7 @@ export class NewTDPModel {
this.addToConnectionList(nodeToBeSaved.fetchInstrInfo())
const saved_list = this._savedNodeProvider?.getSavedInstrList()

this.saveInstrInfoToPersist(nodeToBeSaved.fetchInstrInfo())
await this.saveInstrInfoToPersist(nodeToBeSaved.fetchInstrInfo())

switch (nodeToBeSaved.FetchInstrIOType()) {
case IoType.Lan:
Expand Down Expand Up @@ -950,7 +955,7 @@ export class NewTDPModel {
* Used to update saved instrument details if it is discovered with a
* different instrument address
*/
private checkForSavedInstrIPChange() {
private async checkForSavedInstrIPChange() {
for (let i = 0; i < this.discovery_list.length; i++) {
for (let j = 0; j < this.connection_list.length; j++) {
if (
Expand All @@ -965,7 +970,7 @@ export class NewTDPModel {
this.discovery_list[i].instr_address

//also update persisted list in settings.json
this.saveInstrInfoToPersist(this.connection_list[j])
await this.saveInstrInfoToPersist(this.connection_list[j])
break
}
}
Expand Down Expand Up @@ -1019,7 +1024,7 @@ export class NewTDPModel {
*
* @param instr - saved instrument that needs to stored and recalled
*/
private saveInstrInfoToPersist(instr: InstrInfo) {
private async saveInstrInfoToPersist(instr: InstrInfo) {
try {
const instrList: Array<InstrInfo> =
vscode.workspace
Expand All @@ -1038,7 +1043,7 @@ export class NewTDPModel {
if (idx == -1) {
instrList.push(instr)

void config.update(
await config.update(
"savedInstruments",
instrList,
vscode.ConfigurationTarget.Global,
Expand All @@ -1056,7 +1061,7 @@ export class NewTDPModel {
doUpdate = true
}
if (doUpdate) {
void config.update(
await config.update(
"savedInstruments",
instrList,
vscode.ConfigurationTarget.Global,
Expand Down Expand Up @@ -1216,8 +1221,8 @@ export class InstrTDP implements newTDP {
*
* @param instr - instrument to be saved
*/
public saveInstrument(instr: unknown): void {
this.instrModel?.addSavedList(instr)
public async saveInstrument(instr: unknown): Promise<void> {
await this.instrModel?.addSavedList(instr)
this.reloadTreeData()
}

Expand All @@ -1228,16 +1233,16 @@ export class InstrTDP implements newTDP {
* @param ioType - ioType can be lan, usb etc.
* @param instr_details - additional info of instrument to be saved
*/
public saveInstrumentFromConnect(
public async saveInstrumentFromConnect(
ioType: IoType,
instr_details: InstrInfo,
): void {
): Promise<void> {
const LOGLOC: SourceLocation = {
file: "instruments.ts",
func: `InstrTDP.saveInstrumentFromConnect("${ioType.toString()}", "${String(instr_details)}")`,
}
Log.trace("Add from connect to saved list", LOGLOC)
this.instrModel?.addFromConnectToSavedList(ioType, instr_details)
await this.instrModel?.addFromConnectToSavedList(ioType, instr_details)
this.reloadTreeData()
}

Expand Down Expand Up @@ -1322,8 +1327,8 @@ export class InstrumentsExplorer {

const saveInstrument = vscode.commands.registerCommand(
"InstrumentsExplorer.save",
(e) => {
this.saveInstrument(e)
async (e) => {
await this.saveInstrument(e)
},
)

Expand Down Expand Up @@ -1430,7 +1435,7 @@ export class InstrumentsExplorer {
await this.genericUpgradeFW(_e, 0)
}

public saveWhileConnect(
public async saveWhileConnect(
ip: string,
ioType: IoType,
info: string,
Expand Down Expand Up @@ -1458,11 +1463,11 @@ export class InstrumentsExplorer {

Log.trace("Saving Instrument", LOGLOC)

this.treeDataProvider?.saveInstrumentFromConnect(ioType, __info)
await this.treeDataProvider?.saveInstrumentFromConnect(ioType, __info)
}

private saveInstrument(instr: unknown) {
this.treeDataProvider?.saveInstrument(instr)
private async saveInstrument(instr: unknown) {
await this.treeDataProvider?.saveInstrument(instr)
}

//from connect
Expand Down

0 comments on commit 7c273b0

Please sign in to comment.