pos_bluetooth_thermal_printer/MULTI_DEVICE_SUPPORT.md
2025-11-21 05:52:53 +07:00

7.5 KiB

Multi-Device Support Implementation

Overview

The POS Bluetooth Thermal Printer module implements comprehensive multi-device support, allowing multiple tablets or workstations to maintain independent printer configurations for the same POS configuration without interference.

Implementation Details

Device Identification

Each device is assigned a unique identifier (UUID v4) that persists across browser sessions:

getDeviceId() {
    // Check memory cache
    if (this._deviceId) {
        return this._deviceId;
    }

    // Try to load existing device ID from storage
    const storedDeviceId = localStorage.getItem(`${this.storagePrefix}_device_id`);
    
    if (storedDeviceId) {
        this._deviceId = storedDeviceId;
        return this._deviceId;
    }

    // Generate new device ID using UUID v4
    this._deviceId = this._generateUUID();
    
    // Store for future use
    localStorage.setItem(`${this.storagePrefix}_device_id`, this._deviceId);
    
    return this._deviceId;
}

Storage Key Structure

Printer configurations are stored using composite keys that include both the device ID and POS configuration ID:

Key Format: bluetooth_printer_{deviceId}_{posConfigId}

Example:

  • Device 1, POS Config 1: bluetooth_printer_6fd87a3b-9b95-4a65-9d67-c76afeabd693_1
  • Device 2, POS Config 1: bluetooth_printer_a1b2c3d4-e5f6-7890-abcd-ef1234567890_1

This ensures that:

  1. Each device has its own configuration namespace
  2. Multiple devices can configure different printers for the same POS
  3. Configurations never interfere with each other

Configuration Isolation

The storage manager ensures complete isolation between devices:

_getStorageKey(posConfigId) {
    const deviceId = this.getDeviceId();
    return `${this.storagePrefix}_${deviceId}_${posConfigId}`;
}

When Device 1 saves a configuration:

  • It uses its unique device ID in the storage key
  • The configuration is only accessible to Device 1

When Device 2 saves a configuration:

  • It uses its own unique device ID
  • The configuration is completely separate from Device 1's

Use Cases

Scenario 1: Two Tablets, Same POS, Different Printers

Setup:

  • Tablet A (Device ID: aaa-111)
  • Tablet B (Device ID: bbb-222)
  • Both use POS Configuration ID: 1

Configuration:

  • Tablet A connects to Printer 1 (MAC: 00:11:22:33:44:55)
  • Tablet B connects to Printer 2 (MAC: AA:BB:CC:DD:EE:FF)

Storage:

bluetooth_printer_aaa-111_1 → {printer: "Printer 1", mac: "00:11:22:33:44:55"}
bluetooth_printer_bbb-222_1 → {printer: "Printer 2", mac: "AA:BB:CC:DD:EE:FF"}

Result:

  • Each tablet prints to its own configured printer
  • No interference between devices
  • Each can have different printer settings (paper width, character set, etc.)

Scenario 2: One Device, Multiple POS Configurations

Setup:

  • Single Tablet (Device ID: aaa-111)
  • POS Configuration 1 (Kitchen)
  • POS Configuration 2 (Bar)

Configuration:

  • POS 1 connects to Kitchen Printer
  • POS 2 connects to Bar Printer

Storage:

bluetooth_printer_aaa-111_1 → {printer: "Kitchen Printer"}
bluetooth_printer_aaa-111_2 → {printer: "Bar Printer"}

Result:

  • Same device can manage multiple POS configurations
  • Each POS uses its designated printer
  • Switching between POS configs automatically uses the correct printer

Scenario 3: Configuration Updates Don't Affect Other Devices

Initial State:

  • Device A and Device B both configured with Printer X

Action:

  • Device A updates to Printer Y

Result:

  • Device A now uses Printer Y
  • Device B still uses Printer X
  • No synchronization or interference

Testing

The multi-device support is thoroughly tested with the following test cases:

  1. Unique Device IDs: Each device gets a unique identifier
  2. Storage Key Format: Keys include device ID
  3. Configuration Isolation: Different devices maintain independent configs
  4. Update Isolation: Changes on one device don't affect others
  5. Clear Isolation: Clearing config on one device doesn't affect others
  6. Multiple POS Support: Same device can handle multiple POS configs
  7. Device ID Persistence: Device ID persists across sessions
  8. getAllConfigurations: Returns only current device's configurations

Running Tests

cd customaddons/pos_bluetooth_thermal_printer
npm install
npm test

API Reference

BluetoothPrinterStorage

getDeviceId(): string

Returns the unique device identifier. Generates and stores a new UUID if none exists.

saveConfiguration(posConfigId: number, printerConfig: Object): void

Saves printer configuration for the current device and specified POS config.

Parameters:

  • posConfigId: POS configuration ID
  • printerConfig: Printer configuration object

Storage Key: bluetooth_printer_{deviceId}_{posConfigId}

loadConfiguration(posConfigId: number): Object|null

Loads printer configuration for the current device and specified POS config.

Returns: Configuration object or null if not found

clearConfiguration(posConfigId: number): void

Clears printer configuration for the current device and specified POS config.

getAllConfigurations(): Array<{posConfigId: number, config: Object}>

Returns all printer configurations for the current device only.

Browser Storage Considerations

Storage Limits

  • Each device's configurations are stored in browser localStorage
  • Typical limit: 5-10 MB per origin
  • Each configuration is typically < 1 KB

Storage Persistence

  • Device ID persists across browser sessions
  • Configurations persist until explicitly cleared or browser data is cleared
  • Clearing browser data requires reconfiguration

Privacy

  • All data stored locally in the browser
  • No server-side storage of printer configurations
  • MAC addresses and device IDs never leave the device

Troubleshooting

Device Gets New ID After Browser Data Clear

Cause: Device ID is stored in localStorage Solution: This is expected behavior. Reconfigure the printer.

Configuration Not Loading

Cause: Different device or cleared storage Solution: Check device ID in console, reconfigure if needed

Two Devices Showing Same Configuration

Cause: Impossible with current implementation Solution: If this occurs, check for browser profile sharing or cloning

Requirements Validation

This implementation satisfies Requirement 5.3:

"WHEN multiple devices access the same POS configuration THEN the system SHALL allow each device to maintain independent bluetooth printer settings"

Validation:

  • Each device has unique identifier
  • Storage keys include device ID
  • Configurations are completely isolated
  • Updates on one device don't affect others
  • Multiple devices can use same POS config with different printers
  • Comprehensive test coverage confirms isolation

Future Enhancements

Potential improvements for multi-device support:

  1. Device Naming: Allow users to name devices for easier identification
  2. Configuration Sync: Optional cloud sync for backup/restore
  3. Device Management UI: View all devices and their configurations
  4. Configuration Export/Import: Transfer configs between devices
  5. Shared Printer Pools: Allow multiple devices to share printer lists

Conclusion

The multi-device support implementation provides robust, isolated printer configurations for each device while maintaining simplicity and reliability. The device-specific storage key approach ensures that multiple tablets or workstations can operate independently without any risk of configuration conflicts.