Skip to content

Commit

Permalink
Support disabling log
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Oct 5, 2023
1 parent 01e9b01 commit 5e3f641
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
45 changes: 33 additions & 12 deletions src/PTPDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ interface EventTypes {
disconnect: void
}

interface PTPDeviceOptions {
log?: boolean
}

export class PTPDevice extends EventEmitter<EventTypes> {
#transactionId = 0x00000000

Expand All @@ -70,8 +74,15 @@ export class PTPDevice extends EventEmitter<EventTypes> {

#commandQueue = new PromiseQueue(1, Infinity)

constructor(public usbDevice: USBDevice) {
#console = console as Pick<Console, 'groupCollapsed' | 'groupEnd' | 'info'>

constructor(
public usbDevice: USBDevice,
{log = true}: PTPDeviceOptions = {}
) {
super()

this.setLog(log)
}

open = async (): Promise<void> => {
Expand Down Expand Up @@ -128,6 +139,16 @@ export class PTPDevice extends EventEmitter<EventTypes> {
return this.#opened
}

setLog(log: boolean) {
this.#console = log
? console
: {
groupCollapsed: () => null,
groupEnd: () => null,
info: () => null,
}
}

onEventCode(eventCode: number, callback: PTPEventCallback) {
const eventName = toHexString(eventCode, 2)
this.on(`ptpevent:${eventName}`, callback)
Expand All @@ -142,12 +163,12 @@ export class PTPDevice extends EventEmitter<EventTypes> {
const queue = () =>
promiseTimeout(
new Promise<PTPResponse>((resolve, reject) => {
console.groupCollapsed(`Send Command [${option.label}]`)
this.#console.groupCollapsed(`Send Command [${option.label}]`)

this.sendCommandNow(option).then(resolve).catch(reject)
}),
{milliseconds: PTPDefaultTimeoutMs, message: 'Timeout'}
).finally(console.groupEnd)
).finally(this.#console.groupEnd)

return this.#commandQueue.add(queue)
}
Expand All @@ -156,12 +177,12 @@ export class PTPDevice extends EventEmitter<EventTypes> {
const queue = () =>
promiseTimeout(
new Promise<PTPResponse>((resolve, reject) => {
console.groupCollapsed(`Receive Data [${option.label}]`)
this.#console.groupCollapsed(`Receive Data [${option.label}]`)

this.sendDataNow(option).then(resolve).catch(reject)
}),
{milliseconds: PTPDefaultTimeoutMs, message: 'Timeout'}
).finally(console.groupEnd)
).finally(this.#console.groupEnd)

return this.#commandQueue.add(queue)
}
Expand All @@ -170,12 +191,12 @@ export class PTPDevice extends EventEmitter<EventTypes> {
const queue = () =>
promiseTimeout(
new Promise<PTPDataResponse>((resolve, reject) => {
console.groupCollapsed(`Receive Data [${option.label}]`)
this.#console.groupCollapsed(`Receive Data [${option.label}]`)

this.receiveDataNow(option).then(resolve).catch(reject)
}),
{milliseconds: PTPDefaultTimeoutMs, message: 'Timeout'}
).finally(console.groupEnd)
).finally(this.#console.groupEnd)

return this.#commandQueue.add(queue)
}
Expand Down Expand Up @@ -263,7 +284,7 @@ export class PTPDevice extends EventEmitter<EventTypes> {

if (res1.type === PTPType.Response) {
if (expectedResCodes.includes(res1.code)) {
console.groupEnd()
this.#console.groupEnd()
return {
resCode: res1.code,
parameters: [],
Expand Down Expand Up @@ -321,7 +342,7 @@ export class PTPDevice extends EventEmitter<EventTypes> {
this.#endpointNumberBulkOut,
dataView.toBuffer()
)
console.info(
this.#console.info(
'transferOutBulk',
'type=Command',
'opcode=' + toHexString(opcode, 2),
Expand Down Expand Up @@ -355,7 +376,7 @@ export class PTPDevice extends EventEmitter<EventTypes> {
this.#endpointNumberBulkOut,
dataView.toBuffer()
)
console.info(
this.#console.info(
'transferOutBulk',
'type=Data',
'opcode=' + toHexString(opcode, 2),
Expand Down Expand Up @@ -387,7 +408,7 @@ export class PTPDevice extends EventEmitter<EventTypes> {
const transactionId = data.getUint32(8, true)
const payload = data.buffer.slice(12)

console.info(
this.#console.info(
'transferInBulk',
'type=' + PTPType[type] ?? type,
'code=' + toHexString(code, 2),
Expand Down Expand Up @@ -434,7 +455,7 @@ export class PTPDevice extends EventEmitter<EventTypes> {

const eventName = toHexString(code, 2)

console.info(
this.#console.info(
'transferInInterrupt',
'type=' + PTPType[type],
'code=' + eventName,
Expand Down
5 changes: 5 additions & 0 deletions src/Tethr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ export abstract class Tethr

abstract get opened(): boolean

// eslint-disable-next-line @typescript-eslint/no-unused-vars
setLog(log: boolean) {
null
}

/**
* Export all writable configs to a plain object.
*/
Expand Down
12 changes: 8 additions & 4 deletions src/TethrPTPUSB/TethrPTPUSB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ export class TethrPTPUSB extends Tethr {
super()
}

get opened(): boolean {
return this._opened
}

async open(): Promise<void> {
if (!this.device.opened) {
await this.device.open()
Expand Down Expand Up @@ -107,6 +103,14 @@ export class TethrPTPUSB extends Tethr {
await this.device.close()
}

get opened(): boolean {
return this._opened
}

setLog(log: boolean) {
this.device.setLog(log)
}

// Configs

setAperture(value: Aperture) {
Expand Down
2 changes: 1 addition & 1 deletion src/TethrPTPUSB/TethrSigma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ enum OpCodeSigma {

GetCamCanSetInfo3 = 0x9026, // ver1.2
GetCamDataGroup5 = 0x9027, // ver1.2
SetCamDataGroup5 = 0x9028, // ver1.2
SetCamDataGroup5 = 0x9028, // ver1.2 x
GetCamDataGroup6 = 0x9029, // ver1.2
SetCamDataGroup6 = 0x902a, // ver1.2

Expand Down

0 comments on commit 5e3f641

Please sign in to comment.