Skip to content

Commit

Permalink
Split getVendorSpecificPTPClass from initPTPUSBCamera
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Jun 12, 2024
1 parent a878b6d commit bb6da03
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
27 changes: 27 additions & 0 deletions src/TethrPTPUSB/getVendorSpecificPTPUSB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {DeviceInfo} from '../DeviceInfo'
import {TethrPanasonic} from './TethrPanasonic'
import {TethrPTPUSB} from './TethrPTPUSB'
import {TethrRicohTheta} from './TethrRicohTheta'
import {TethrSigma} from './TethrSigma'

/**
* Return the vendor-specific PTPUSB subclass for the given device info.
* @param info The device info to check
* @returns The vendor-specific PTPUSB class or undefined if no vendor-specific class is found
*/
export function getVendorSpecificPTPUSBClass(
info: DeviceInfo
): typeof TethrPTPUSB | undefined {
switch (info.vendorExtensionID) {
case 0x00000006: // Microsoft / Sigma / Ricoh
if (info.vendorExtensionDesc === 'SIGMA') {
return TethrSigma
} else if (info.model.match(/theta/i)) {
return TethrRicohTheta
}
break
case 0x0000001c: // Panasnoic
return TethrPanasonic
break
}
}
40 changes: 5 additions & 35 deletions src/TethrPTPUSB/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {DeviceInfo} from '../DeviceInfo'
import {PTPDevice} from '../PTPDevice'
import {TethrPanasonic} from './TethrPanasonic'
import {getVendorSpecificPTPUSBClass} from './getVendorSpecificPTPUSB'
import {TethrPTPUSB} from './TethrPTPUSB'
import {TethrRicohTheta} from './TethrRicohTheta'
import {TethrSigma} from './TethrSigma'

/**
* Try to initialize the given usb device as a PTP camera.
Expand All @@ -17,10 +15,9 @@ export async function initTethrUSBPTP(
let info: DeviceInfo

try {
if (device.opened) {
await device.close()
if (!device.opened) {
await device.open()
}
await device.open()
info = await TethrPTPUSB.getDeviceInfo(device)
} catch (err) {
if (
Expand All @@ -35,36 +32,9 @@ export async function initTethrUSBPTP(
return null
}

let tethr: TethrPTPUSB | null = null
const TethrVendor = getVendorSpecificPTPUSBClass(info) ?? TethrPTPUSB

switch (info.vendorExtensionID) {
case 0x00000006: // Microsoft / Sigma / Ricoh
if (info.vendorExtensionDesc === 'SIGMA') {
tethr = new TethrSigma(device)
} else if (info.model.match(/theta/i)) {
tethr = new TethrRicohTheta(device)
}
break
case 0x0000001c: // Panasnoic
tethr = new TethrPanasonic(device)
break
}

if (!tethr) {
tethr = new TethrPTPUSB(device)
}

// Though this is a little bit dirty, it is required to check whether
// the Open/CloseSession command works since some of PTP devices don't support
// any commands other than GetDeviceInfo.
try {
// await tethr.open()
// await tethr.close()
} catch {
return null
}

return tethr
return new TethrVendor(device)
}

export {TethrPTPUSB}

0 comments on commit bb6da03

Please sign in to comment.