Interactive Examples

Base Component Class

All components inherit from the base Component class, providing common functionality:

// Common component methods
await component.enable();
await component.disable();
await component.query();

// Component properties
const id = component.id;                     // Unique component identifier
const type = component.deviceType;           // Component device type (e.g., "BARCODE_READER")
const isReady = component.ready;             // Boolean ready status
const isEnabled = component.enabled;           // Boolean enabled status

// Event handling
component.on('readyStateChange', (isReady) => {
    console.log(`Component is now ${isReady ? 'ready' : 'not ready'}`);
});

๐Ÿ–จ๏ธ Printer Components

Printers are controlled via low-level ITPS commands. You will need to consult your device manufacturer's documentation for a list of available commands.

BoardingPassPrinter

const bpp = cuss2.boardingPassPrinter;

// This example queries the printer's environment status.
if (bpp) {
    await bpp.enable();
    const status = await bpp.getEnvironment();
    console.log('Printer Status:', status);

    // Example of sending a raw ITPS command (device-specific)
    // const response = await bpp.sendITPSCommand("YOUR_ITPS_COMMAND");
}

// Listen for generic state changes
bpp.on('stateChange', (status) => {
    console.log('BoardingPassPrinter state is now:', status.name);
});

BagTagPrinter

const btp = cuss2.bagTagPrinter;

// This example clears all stored PECTABs from the printer memory.
if (btp) {
    await btp.enable();
    const success = await btp.pectabs.clear();
    console.log('PECTABs cleared:', success);
}

Feeder (Sub-component)

const feeder = cuss2.boardingPassPrinter?.feeder;

// The Feeder is a basic component. You can check its ready status.
if (feeder?.ready) {
    console.log('Feeder is ready to accept media.');
}

feeder.on('stateChange', (status) => {
    console.log('Feeder state is now:', status.name);
});

Dispenser (Sub-component)

const dispenser = cuss2.boardingPassPrinter?.dispenser;

// Check if media is present in the output slot.
if (dispenser?.mediaPresent) {
    console.log('Document is waiting to be taken.');
}

// Listen for the mediaPresent event, which fires when media is detected.
dispenser.on('mediaPresent', (isPresent) => {
    console.log(isPresent ? 'Media is present.' : 'Media has been taken.');
});

๐Ÿ“Š Data Reader Components

Components that read various types of data from documents, cards, and biometric sources.

BarcodeReader

const barcodeReader = cuss2.barcodeReader;

// Enable and start reading
if (barcodeReader) {
    await barcodeReader.enable();

    // BarcodeReader emits a 'data' event with a string array payload.
    barcodeReader.on('data', (data) => {
        const barcodeData = data[0]; // Typically the first element
        console.log('Barcode scanned:', barcodeData);
    });
}

DocumentReader

const docReader = cuss2.documentReader;

// Enable the component to start reading
if (docReader) {
    await docReader.enable();

    // The DocumentReader emits a generic 'data' event with a string array.
    // The structure of the data within the array depends on the platform's implementation.
    docReader.on('data', (data) => {
        console.log('Document data received:', data);
        // You will need to parse the data based on your platform's format.
    });
}

โš–๏ธ Measurement Components

Components for weighing baggage.

Scale

const scale = cuss2.scale;

// Enable the component
if (scale) {
    await scale.enable();

    // The Scale component emits a generic 'data' event.
    // The structure of the data payload (e.g., a JSON string)
    // depends on the platform's specific implementation.
    scale.on('data', (data) => {
        console.log(`Weight data received:`, data[0]);
        try {
            const weightInfo = JSON.parse(data[0]);
            if (weightInfo.stable) {
                console.log(`Weight is stable at ${weightInfo.weight} ${weightInfo.unit}`);
            }
        } catch (e) {
            console.error("Could not parse scale data");
        }
    });
}

๐Ÿ”Œ System Components

Core system components for audio output.

Announcement

const announcement = cuss2.announcement;

// Play text-to-speech using the 'say' method
if (announcement) {
    await announcement.enable();
    await announcement.say("Please scan your boarding pass.", "en-US");
}

// Listen for events
announcement.on('stateChange', (status) => {
    console.log('Announcement state is now:', status.name);
});

Component Status Management

All components report their status and can be monitored for availability:

// Monitor all components for state changes
cuss2.on('componentStateChange', (component) => {
    console.log(`${component.deviceType} (ID: ${component.id}) is now ${component.ready ? 'ready' : 'not ready'}`);

    // Update UI based on status
    if (component.deviceType === 'BOARDING_PASS_PRINTER') {
        const printButton = document.getElementById('print-button');
        printButton.disabled = !component.ready;
    }
});

// Check specific component availability
if (cuss2.boardingPassPrinter?.ready) {
    showPrintOption();
}

// Wait for a component to be ready
async function waitForComponent(component) {
    if (component.ready) return Promise.resolve();

    return new Promise((resolve) => {
        component.once('readyStateChange', (isReady) => {
            if (isReady) {
                resolve();
            }
        });
    });
}

Events and Error Handling

Comprehensive event system for monitoring component state and handling errors:

// Component-specific events
component.on('stateChange', handler);
component.on('data', handler); // For DataReaderComponent instances
component.on('error', handler);

// Global Cuss2 events
cuss2.on('connected', () => {
    console.log('Platform connection established!');
});

cuss2.on('deactivated', (newState) => {
    console.log(`Session deactivated, moved to ${newState} state.`);
});

// Error handling patterns
try {
    await cuss2.requestActiveState();
} catch (error) {
    // Errors from the platform often have a code
    if (error.code === 1006) { // Invalid State
        console.error('Cannot request ACTIVE state from current state.');
    } else {
        console.error('An unexpected error occurred:', error.message);
    }
}