From 333553214f989425eaef259869fdb66d08057eaa Mon Sep 17 00:00:00 2001 From: Milap Naik Date: Thu, 2 Nov 2023 00:38:33 +0100 Subject: [PATCH] Make requested changes --- src/BrightScriptCommands.ts | 60 ++++++++++--- src/util.ts | 15 ++++ .../OnlineDevicesViewProvider.ts | 85 +------------------ 3 files changed, 67 insertions(+), 93 deletions(-) diff --git a/src/BrightScriptCommands.ts b/src/BrightScriptCommands.ts index 1a212e0c..a8cacae2 100644 --- a/src/BrightScriptCommands.ts +++ b/src/BrightScriptCommands.ts @@ -10,6 +10,7 @@ import { util as rokuDebugUtil } from 'roku-debug/dist/util'; import type { RemoteControlManager, RemoteControlModeInitiator } from './managers/RemoteControlManager'; import type { WhatsNewManager } from './managers/WhatsNewManager'; import type { ActiveDeviceManager } from './ActiveDeviceManager'; +import * as xml2js from 'xml2js'; export class BrightScriptCommands { @@ -263,20 +264,21 @@ export class BrightScriptCommands { } }); - this.registerCommand('openQuickInputBox', async (url: string, placeholder: string, appNames: vscode.QuickPickItem[]) => { - const stuffUserTyped = await util.showQuickPickInputBox({ - placeholder: placeholder, - items: appNames, - returnDetail: true - }); + this.registerCommand('openRegistry', async (host: string) => { + if (!host) { + host = await this.getRemoteHost(); + } + const apps = await util.httpGet(`http://${host}:8060/query/apps`); + let parsedApps = this.parseXmlResponse(apps.body); + const selectedApp = await vscode.window.showQuickPick(parsedApps, { placeHolder: 'Which app would you like to see the registry for?' }); - if (stuffUserTyped) { - const appId = stuffUserTyped.replace('App ID: ', ''); - const newUrl = url.replace('{appId}', appId); + if (selectedApp) { + const appId = (selectedApp as any).appId; + let url = `http://${host}:8060/query/registry/${appId}`; try { - await vscode.env.openExternal(vscode.Uri.parse(newUrl)); + await vscode.env.openExternal(vscode.Uri.parse(url)); } catch (error) { - await vscode.window.showErrorMessage(`Tried to open url but failed: ${newUrl}`); + await vscode.window.showErrorMessage(`Tried to open url but failed: ${url}`); } } }); @@ -439,4 +441,40 @@ export class BrightScriptCommands { private async sendAsciiToDevice(character: string) { await this.sendRemoteCommand(character, undefined, true); } + + private parseXmlResponse(responseData) { + let appNames: vscode.QuickPickItem[] = []; + + // Extract the XML content + const xmlStartIndex = responseData.indexOf(' { + if (err) { + console.error('Error parsing XML:', err); + return; + } + + appNames = result.apps.app + // Map the XML data to QuickPickItem objects + .map((appData: any) => { + return { + label: appData._, + detail: `ID: ${appData.$.id}`, + description: `${appData.$.version}`, + appId: `${appData.$.id}` + } as vscode.QuickPickItem; + }) + // Have the app with id 'dev' be at the top + .sort((a, b) => { + if (a.appId === 'dev') { + return -1; + } + if (b.appId === 'dev') { + return 1; + } + return a.label.localeCompare(b.label); + }); + }); + return appNames; + } } diff --git a/src/util.ts b/src/util.ts index a74f8aca..17e64f09 100644 --- a/src/util.ts +++ b/src/util.ts @@ -5,6 +5,10 @@ import * as url from 'url'; import { debounce } from 'debounce'; import * as vscode from 'vscode'; import { Cache } from 'brighterscript/dist/Cache'; +import * as r from 'postman-request'; +import type { Response } from 'request'; +import type * as requestType from 'request'; +const request = r as typeof requestType; class Util { public async readDir(dirPath: string) { @@ -385,6 +389,17 @@ class Util { public escapeRegex(text: string) { return text?.toString().replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); } + + /** + * Do an http GET request + */ + public httpGet(url: string) { + return new Promise((resolve, reject) => { + request.get(url, (err, response) => { + return err ? reject(err) : resolve(response); + }); + }); + } } const util = new Util(); diff --git a/src/viewProviders/OnlineDevicesViewProvider.ts b/src/viewProviders/OnlineDevicesViewProvider.ts index ebfe2ab3..7baf1a37 100644 --- a/src/viewProviders/OnlineDevicesViewProvider.ts +++ b/src/viewProviders/OnlineDevicesViewProvider.ts @@ -6,7 +6,6 @@ import { firstBy } from 'thenby'; import { util } from '../util'; import { ViewProviderId } from './ViewProviderId'; import { Socket } from 'net'; -import * as xml2js from 'xml2js'; /** * A sequence used to generate unique IDs for tree items that don't care about having a key @@ -51,80 +50,7 @@ export class OnlineDevicesViewProvider implements vscode.TreeDataProvider; - private async getAppData(deviceIp: string): Promise { - return new Promise((resolve, reject) => { - const appNames: vscode.QuickPickItem[] = []; - const client: Socket = new Socket(); - const port = 8060; - - const httpRequest = `GET /query/apps HTTP/1.1\r\n` + - `Host: ${deviceIp}:${port}\r\n` + - `Connection: close\r\n\r\n`; - client.connect({ host: deviceIp, port: port }, () => { - client.write(httpRequest); - }); - - let responseData = ''; - client.on('data', (data) => { - responseData += data.toString(); - }); - - client.on('end', () => { - try { - const parsedData = this.parseXmlResponse(responseData); - resolve(parsedData); - } catch (error) { - console.error('Error:', error); - reject(error); - } finally { - client.end(); - } - }); - - client.on('error', (error) => { - console.error(`Error: ${error.message}`); - client.destroy(); - reject(error); - }); - }); - } - - private parseXmlResponse(responseData) { - let appNames: vscode.QuickPickItem[] = []; - - // Extract the XML content - const xmlStartIndex = responseData.indexOf(' { - if (err) { - console.error('Error parsing XML:', err); - return; - } - - appNames = result.apps.app - // Map the XML data to QuickPickItem objects - .map((appData: any) => { - return { - label: appData._, - detail: `App ID: ${appData.$.id}`, - description: `${appData.$.version}` - } as vscode.QuickPickItem; - }) - // Have the app with id 'dev' be at the top - .sort((a, b) => { - if (a.detail === 'App ID: dev') { - return -1; - } - if (b.detail === 'App ID: dev') { - return 1; - } - return a.label.localeCompare(b.label); - }); - }); - return appNames; - } - - async getChildren(element?: DeviceTreeItem | DeviceInfoTreeItem): Promise { + getChildren(element?: DeviceTreeItem | DeviceInfoTreeItem): vscode.ProviderResult { if (!element) { if (this.devices) { let items: DeviceTreeItem[] = []; @@ -216,9 +142,6 @@ export class OnlineDevicesViewProvider implements vscode.TreeDataProvider=11')) { // TODO: add ECP system hooks here in the future (like registry call, etc...) - let appNames: vscode.QuickPickItem[] = []; - appNames = await this.getAppData(device.ip); - result.unshift( this.createDeviceInfoTreeItem({ label: '🔗 View Registry', @@ -227,11 +150,9 @@ export class OnlineDevicesViewProvider implements vscode.TreeDataProvider