Skip to content

Commit

Permalink
update ui deprecated part and remove usb-detection
Browse files Browse the repository at this point in the history
  • Loading branch information
koji committed Jan 10, 2024
1 parent 3dc8fb1 commit 2c9c929
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
13 changes: 10 additions & 3 deletions app-shell/src/system-info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
getActiveInterfaces,
} from './network-interfaces'

import type { WebUSBDevice } from 'usb'
import type { UsbDevice } from '@opentrons/app/src/redux/system-info/types'

Check failure on line 14 in app-shell/src/system-info/index.ts

View workflow job for this annotation

GitHub Actions / js checks

'UsbDevice' is defined but never used

Check failure on line 14 in app-shell/src/system-info/index.ts

View workflow job for this annotation

GitHub Actions / js checks

'UsbDevice' is defined but never used
import type { Action, Dispatch } from '../types'
import type { UsbDeviceMonitor, Device } from './usb-devices'
import type { UsbDeviceMonitor } from './usb-devices'
import type {
NetworkInterface,
NetworkInterfaceMonitor,
Expand All @@ -21,13 +22,19 @@ import type {
export { createNetworkInterfaceMonitor }
export type { NetworkInterface, NetworkInterfaceMonitor }

type Device = Partial<WebUSBDevice>

const RE_REALTEK = /realtek/i
const IFACE_POLL_INTERVAL_MS = 30000

const log = createLogger('system-info')

const addDriverVersion = (device: Device): Promise<UsbDevice> => {
if (isWindows() && RE_REALTEK.test(device.manufacturer)) {
const addDriverVersion = (device: WebUSBDevice): Promise<WebUSBDevice> => {
if (
isWindows() &&
device.manufacturerName != null &&
RE_REALTEK.test(device.manufacturerName)
) {
return getWindowsDriverVersion(device).then(windowsDriverVersion => ({

Check failure on line 38 in app-shell/src/system-info/index.ts

View workflow job for this annotation

GitHub Actions / js checks

Type 'Promise<WebUSBDevice | { windowsDriverVersion: string | null; usbVersionMajor: number; usbVersionMinor: number; usbVersionSubminor: number; deviceClass: number; deviceSubclass: number; deviceProtocol: number; vendorId: number; productId: number; deviceVersionMajor: number; deviceVersionMinor: number; deviceVersionSubminor: number; manufacturerName?: string | undefined; productName?: string | undefined; serialNumber?: string | undefined; configurations: USBConfiguration[]; }>' is not assignable to type 'Promise<WebUSBDevice>'.

Check failure on line 38 in app-shell/src/system-info/index.ts

View workflow job for this annotation

GitHub Actions / js checks

Type 'Promise<WebUSBDevice | { windowsDriverVersion: string | null; usbVersionMajor: number; usbVersionMinor: number; usbVersionSubminor: number; deviceClass: number; deviceSubclass: number; deviceProtocol: number; vendorId: number; productId: number; deviceVersionMajor: number; deviceVersionMinor: number; deviceVersionSubminor: number; manufacturerName?: string | undefined; productName?: string | undefined; serialNumber?: string | undefined; configurations: USBConfiguration[]; }>' is not assignable to type 'Promise<WebUSBDevice>'.
...device,
windowsDriverVersion,
Expand Down
20 changes: 9 additions & 11 deletions app-shell/src/system-info/usb-devices.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import assert from 'assert'
import execa from 'execa'
import usbDetection from 'usb-detection'
import { usb } from 'usb'
import { isWindows } from '../os'
import { createLogger } from '../log'

import type { Device } from 'usb-detection'
import type { Device } from 'usb'

export type { Device }

Expand All @@ -14,7 +14,7 @@ export type UsbDeviceMonitorOptions = Partial<{
}>

export interface UsbDeviceMonitor {
getAllDevices: () => Promise<Device[]>
getAllDevices: () => Device[]
stop: () => void
}

Expand All @@ -24,27 +24,25 @@ export function createUsbDeviceMonitor(
options: UsbDeviceMonitorOptions = {}
): UsbDeviceMonitor {
const { onDeviceAdd, onDeviceRemove } = options
usbDetection.startMonitoring()

if (typeof onDeviceAdd === 'function') {
usbDetection.on('add', onDeviceAdd)
usb.on('attach', onDeviceAdd)
}

if (typeof onDeviceRemove === 'function') {
usbDetection.on('remove', onDeviceRemove)
usb.off('detach', onDeviceRemove)
}

return {
getAllDevices: () => usbDetection.find(),
getAllDevices: () => usb.getDeviceList(),
stop: () => {
if (typeof onDeviceAdd === 'function') {
usbDetection.off('add', onDeviceAdd)
usb.off('attach', onDeviceAdd)
}
if (typeof onDeviceRemove === 'function') {
usbDetection.off('remove', onDeviceRemove)
usb.off('detach', onDeviceRemove)
}

usbDetection.stopMonitoring()
log.debug('usb detection monitoring stopped')
},
}
Expand All @@ -54,7 +52,7 @@ const decToHex = (number: number): string =>
number.toString(16).toUpperCase().padStart(4, '0')

export function getWindowsDriverVersion(
device: Device
device: USBDevice
): Promise<string | null> {
const { vendorId: vidDecimal, productId: pidDecimal, serialNumber } = device
const [vid, pid] = [decToHex(vidDecimal), decToHex(pidDecimal)]
Expand Down
13 changes: 8 additions & 5 deletions app-shell/src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ export function createUi(): BrowserWindow {
mainWindow.loadURL(url, { extraHeaders: 'pragma: no-cache\n' })

// open new windows (<a target="_blank" ...) in browser windows
mainWindow.webContents.on('new-window', (event, url) => {
log.debug('Opening external link', { url })
event.preventDefault()
// eslint-disable-next-line @typescript-eslint/no-floating-promises
shell.openExternal(url)
mainWindow.webContents.setWindowOpenHandler(({ url, disposition }) => {
if (disposition === 'new-window' && url === 'about:blank') {
// eslint-disable-next-line no-void
void shell.openExternal(url)
return { action: 'deny' }
} else {
return { action: 'allow' }
}
})

return mainWindow
Expand Down

0 comments on commit 2c9c929

Please sign in to comment.