Skip to main content
AdvancedTimeouts and logging

Two Camera options control how Darkgrade Slate talks to the camera under every method: transactionPolicy sets its patience, and logger shows you the conversation.

import { Camera } from '@darkgrade/slate'

const camera = new Camera({
    // Wait 10 s for each phase. The default is 30 s.
    transactionPolicy: { timeoutMilliseconds: 10_000 },
    // Draw each exchange in the terminal.
    logger: { renderInTerminal: true },
})

Both take a partial object: what you leave out keeps its default.

Transaction policy

A transaction is one PTP exchange: a command, optional data, and the camera's response. Every Camera method is made of them, and the policy applies to each one. What is PTP? has the detail.

timeoutMillisecondsnumberdefault: 30000

How long one send or receive phase may take.

minimumThroughputBytesPerSecondnumberdefault: 2097152

Large transfers get extra time at this rate (2 MiB/s) on top of the timeout, so a big download is not cut off while it is still moving.

deviceBusyRetriesnumberdefault: 6

How many times a standard PTP operation answered DeviceBusy is tried again. 0 turns retries off; the most is 32.

deviceBusyBackoffMillisecondsnumberdefault: 50

The wait before the first retry. Each further retry doubles it: 50, 100, 200, 400, 800 ms.

deviceBusyBudgetMillisecondsnumberdefault: 2000

Retries stop once this much time has passed since the first attempt. The last wait is shortened to fit.

automaticUSBResetbooleandefault: true

After a timeout or a malformed reply on USB, Slate resets the connection and reopens the session before the next command. See After a timeout.

recoverTransport(failure) => Promise<void>

Your own recovery, run in place of the automatic USB reset. See Your own recovery.

The defaults are exported as DEFAULT_TRANSACTION_POLICY, with the type TransactionPolicy:

import { Camera, DEFAULT_TRANSACTION_POLICY } from '@darkgrade/slate'

const { timeoutMilliseconds } = DEFAULT_TRANSACTION_POLICY

const camera = new Camera({
    transactionPolicy: {
        timeoutMilliseconds: timeoutMilliseconds * 2,
        deviceBusyRetries: 0, // report a busy camera at once
    },
})

An invalid value (a timeout that is not a positive integer, more than 32 retries) throws when the Camera is constructed.

Busy retries only repeat a command the camera refused to start. A command that was sent and not confirmed is never resent: it becomes CameraOutcomeUnknownError. See Errors and recovery.

After a timeout

A timeout can leave a half-finished exchange on the cable, and the next command would read its leftovers.

  • USB, automaticUSBReset: true (the default): before the next command, Slate sends the USB still-image class Device Reset, waits for the abandoned transfer to settle and reopens the session.
  • If that reset fails, or automaticUSBReset is false: every later command fails with PTPRecoveryRequiredError until you disconnect() and connect(), or call recover().
  • PTP/IP: there is no class reset. IPTransport also has a timeout of its own (15 s by default), and closes the connection when it runs out. Either way, connect again. See Wi-Fi with PTP/IP.

Your own recovery

Set recoverTransport when a reset must respect something Slate can't see, such as a recording in progress on a rig, or a lease another process holds. It replaces the automatic reset.

import { Camera } from '@darkgrade/slate'

const camera = new Camera({
    transactionPolicy: {
        recoverTransport: async failure => {
            console.warn(`Recovering after ${failure.operation}`)
            // Settle the connection here.
        },
    },
})
  • The callback receives the failed transaction's operation, transactionId and cause.
  • It must finish any outstanding transport I/O before it resolves, and it must not call methods of the same Camera.
  • While it is pending, and if it throws, further commands fail with PTPRecoveryRequiredError.
  • Slate does not reopen the session after your callback. If your recovery ended the session, call disconnect() and connect().

Logging

The logger is off by default: it draws nothing and leaves console alone. Turn on what you need:

renderInTerminalbooleandefault: false

Draws every transaction in the terminal as it happens: the operation, its parameters, the data and the response. Node.js only.

captureConsolebooleandefault: false

Routes console.log, console.info, console.warn and console.error into the logger's display, so your own output does not tear it. Node.js only.

expandedbooleandefault: true

Shows each entry's detail in the terminal. false shows one line per entry.

maxTransactionsnumberdefault: 1000

How many transactions are kept in memory. Older ones are dropped, with any data they carried.

import { Camera } from '@darkgrade/slate'

const camera = new Camera({
    logger: {
        renderInTerminal: true,
        captureConsole: true,
        expanded: false,
    },
})
await camera.connect()
await camera.getCapabilities() // each exchange appears as it happens
await camera.disconnect()

The defaults are exported as defaultLoggerConfig, with the type LoggerConfig.

For a record of what a failed call sent, rather than a live display, see the callId that telemetry attaches to each Camera call and the commands behind it.

Other options

keepAlive, usb, transport, vendorId and firstContact are the remaining Camera options: see the constructor.

⌘I