Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved some stuff around and started the the set primary device command #445

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ActiveDeviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class ActiveDeviceManager extends EventEmitter {

public firstRequestForDevices: boolean;
public lastUsedDevice: string;
private enabled: boolean;
public enabled: boolean;
private showInfoMessages: boolean;
private deviceCache: NodeCache;
private exponentialBackoff: any;
Expand All @@ -54,7 +54,7 @@ export class ActiveDeviceManager extends EventEmitter {
// Returns an object will all the active devices by device id
public getActiveDevices() {
this.firstRequestForDevices = false;
return this.deviceCache.mget(this.deviceCache.keys());
return this.deviceCache.mget<Record<string, RokuDeviceDetails>>(this.deviceCache.keys());
}

// Returns the device cache statistics.
Expand Down
8 changes: 8 additions & 0 deletions src/BrightScriptCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ export class BrightScriptCommands {
this.registerCommand('showReleaseNotes', () => {
this.whatsNewManager.showReleaseNotes();
});

this.registerCommand('setPrimaryDevice', async (host?: string) => {
if (host) {

} else {
host = await util.showDeviceQuickPicker(this.activeDeviceManager);
}
});
}

public async openFile(filename: string, range: vscode.Range = null, preview = false): Promise<boolean> {
Expand Down
79 changes: 7 additions & 72 deletions src/DebugConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,62 +364,9 @@ export class BrightScriptDebugConfigurationProvider implements DebugConfiguratio
let showInputBox = false;

if (config.host.trim() === '${promptForHost}' || (config?.deepLinkUrl?.includes('${promptForHost}'))) {
if (this.activeDeviceManager.enabled) {
if (this.activeDeviceManager.firstRequestForDevices && !this.activeDeviceManager.getCacheStats().keys) {
let deviceWaitTime = 5000;
if (this.showDeviceInfoMessages) {
await vscode.window.showInformationMessage(`Device Info: Allowing time for device discovery (${deviceWaitTime} ms)`);
}

await util.delay(deviceWaitTime);
}

let activeDevices = this.activeDeviceManager.getActiveDevices();

if (activeDevices && Object.keys(activeDevices).length) {
let items = [];

// Create the Quick Picker option items
for (const key of Object.keys(activeDevices)) {
let device = activeDevices[key];
let itemText = `${device.ip} | ${device.deviceInfo['default-device-name']} - ${device.deviceInfo['model-number']}`;

if (this.activeDeviceManager.lastUsedDevice && device.deviceInfo['default-device-name'] === this.activeDeviceManager.lastUsedDevice) {
items.unshift(itemText);
} else {
items.push(itemText);
}
}

// Give the user the option to type their own IP incase the device they want has not yet been detected on the network
let manualIpOption = 'Other';
items.push(manualIpOption);

let host = await vscode.window.showQuickPick(items, { placeHolder: `Please Select a Roku or use the "${manualIpOption}" option to enter a IP` });

if (host === manualIpOption) {
showInputBox = true;
} else if (host) {
let defaultDeviceName = host.substring(host.toLowerCase().indexOf(' | ') + 3, host.toLowerCase().lastIndexOf(' - '));
let deviceIP = host.substring(0, host.toLowerCase().indexOf(' | '));
if (defaultDeviceName) {
this.activeDeviceManager.lastUsedDevice = defaultDeviceName;
}
config.host = deviceIP;
} else {
// User canceled. Give them one more change to enter an ip
showInputBox = true;
}
} else {
showInputBox = true;
}
} else {
showInputBox = true;
}
}

if (showInputBox) {
config.host = await this.openInputBox('The IP address of your Roku device');
config.host = await util.showDeviceQuickPicker(this.activeDeviceManager, true);
} else if (showInputBox) {
config.host = await util.openInputBox('The IP address of your Roku device');
}
// #endregion

Expand All @@ -440,7 +387,7 @@ export class BrightScriptDebugConfigurationProvider implements DebugConfiguratio
private async processPasswordParameter(config: BrightScriptLaunchConfiguration) {
//prompt for password if not hardcoded
if (config.password.trim() === '${promptForPassword}') {
config.password = await this.openInputBox('The developer account password for your Roku device.');
config.password = await util.openInputBox('The developer account password for your Roku device.');
if (!config.password) {
throw new Error('Debug session terminated: password is required.');
}
Expand All @@ -458,30 +405,18 @@ export class BrightScriptDebugConfigurationProvider implements DebugConfiguratio
config.deepLinkUrl = config.deepLinkUrl.replace('${host}', config.host);
config.deepLinkUrl = config.deepLinkUrl.replace('${promptForHost}', config.host);
if (config.deepLinkUrl.includes('${promptForQueryParams}')) {
let queryParams = await this.openInputBox('Querystring params for deep link');
let queryParams = await util.openInputBox('Querystring params for deep link');
config.deepLinkUrl = config.deepLinkUrl.replace('${promptForQueryParams}', queryParams);
}
if (config.deepLinkUrl === '${promptForDeepLinkUrl}') {
config.deepLinkUrl = await this.openInputBox('Full deep link url');
config.deepLinkUrl = await util.openInputBox('Full deep link url');
}
}
return config;
}

/**
* Helper to open a vscode input box ui
* @param placeHolder placeHolder text
* @param value default value
*/
private async openInputBox(placeHolder: string, value = '') {
return vscode.window.showInputBox({
placeHolder: placeHolder,
value: value
});
}

/**
* Get the bsconfig file, if available
* Get the brsConfig file, if available
*/
public getBsConfig(workspaceFolder: vscode.Uri) {
//try to load bsconfig settings
Expand Down
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class Extension {
private telemetryManager: TelemetryManager;
private remoteControlManager: RemoteControlManager;
private brightScriptCommands: BrightScriptCommands;
private activeDeviceManager: ActiveDeviceManager;

public odc?: rta.OnDeviceComponent;

Expand All @@ -63,6 +64,7 @@ export class Extension {
this.globalStateManager = new GlobalStateManager(context);
this.whatsNewManager = new WhatsNewManager(this.globalStateManager, currentExtensionVersion);
this.chanperfStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
this.activeDeviceManager = new ActiveDeviceManager();

//initialize the analytics manager
context.subscriptions.push(
Expand All @@ -86,7 +88,6 @@ export class Extension {
//update the tracked version of the extension
this.globalStateManager.lastRunExtensionVersion = currentExtensionVersion;


const declarationProvider = new DeclarationProvider();
context.subscriptions.push(declarationProvider);

Expand Down Expand Up @@ -138,7 +139,7 @@ export class Extension {
);

//register the debug configuration provider
let configProvider = new BrightScriptDebugConfigurationProvider(context, activeDeviceManager, this.telemetryManager);
let configProvider = new BrightScriptDebugConfigurationProvider(context, this.activeDeviceManager, this.telemetryManager);
context.subscriptions.push(
vscode.debug.registerDebugConfigurationProvider('brightscript', configProvider)
);
Expand Down
69 changes: 69 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as net from 'net';
import * as url from 'url';
import { debounce } from 'debounce';
import * as vscode from 'vscode';
import type { ActiveDeviceManager } from './ActiveDeviceManager';
import { Cache } from 'brighterscript/dist/Cache';

class Util {
Expand Down Expand Up @@ -239,6 +240,74 @@ class Util {
return channel;
}

public async showDeviceQuickPicker(activeDeviceManager: ActiveDeviceManager, updateLastUsedDevice = false) {
let host: string;

const config: any = vscode.workspace.getConfiguration('brightscript') || {};
const showDeviceInfoMessages = (config.deviceDiscovery || {}).showInfoMessages;
if (activeDeviceManager.enabled) {
if (activeDeviceManager.firstRequestForDevices && !activeDeviceManager.getCacheStats().keys) {
let deviceWaitTime = 5000;
if (showDeviceInfoMessages) {
await vscode.window.showInformationMessage(`Device Info: Allowing time for device discovery (${deviceWaitTime} ms)`);
}

await util.delay(deviceWaitTime);
}

let activeDevices = activeDeviceManager.getActiveDevices();

if (activeDevices && Object.keys(activeDevices).length) {
let items = [];

// Create the Quick Picker option items
for (const key of Object.keys(activeDevices)) {
let device = activeDevices[key];
let itemText = `${device.ip} | ${device.deviceInfo['default-device-name']} - ${device.deviceInfo['model-number']}`;

if (activeDeviceManager.lastUsedDevice && device.deviceInfo['default-device-name'] === activeDeviceManager.lastUsedDevice) {
items.unshift(itemText);
} else {
items.push(itemText);
}
}

// Give the user the option to type their own IP incase the device they want has not yet been detected on the network
let manualIpOption = 'Other';
items.push(manualIpOption);

let host = await vscode.window.showQuickPick(items, { placeHolder: `Please Select a Roku or use the "${manualIpOption}" option to enter a IP` });

if (host !== manualIpOption) {
let defaultDeviceName = host.substring(host.toLowerCase().indexOf(' | ') + 3, host.toLowerCase().lastIndexOf(' - '));
let deviceIP = host.substring(0, host.toLowerCase().indexOf(' | '));
if (defaultDeviceName && updateLastUsedDevice) {
activeDeviceManager.lastUsedDevice = defaultDeviceName;
}
host = deviceIP;
}
}
}

if (!host) {
host = await this.openInputBox('The IP address of your Roku device');
}

return host;
}

/**
* Helper to open a vscode input box ui
* @param placeHolder placeHolder text
* @param value default value
*/
public async openInputBox(placeHolder: string, value = '') {
return vscode.window.showInputBox({
placeHolder: placeHolder,
value: value
});
}

/**
* Shows ether a QuickPick or InputBox to the user and allows them to enter
* items not in the QuickPick list of items
Expand Down