"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
const Led_1 = __importDefault(require("./Led/Led"));
const SerialPort_1 = __importDefault(require("./SerialPort"));
const BarcodeScanner_1 = __importDefault(require("./BarcodeScanner/BarcodeScanner"));
/**
 * The `sos.hardware` API groups together methods for working with hardware. It allows opening serial ports, using bar scanner or controlling LEDs.
 *
 * :::warning
 * - Before using this API, ensure that the display [supports](/sdk/sos/display#supports) serial via `sos.display.supports("SERIAL")`.
 * - Samsung Kiosk serial connection only works over serial ports, not over USB ports.
 * :::
 *
 * <details>
 *     <summary>Device Hardware Capabilities</summary>
 * 		| Capability | Description |
 * 		|:------------|:-------------|
 * 		| `SERIAL` | If the device supports sending and receiving data via serial ports |
 *
 * 		If you want to check if the device supports this capability, use [`sos.display.supports()`](https://developers.signageos.io/sdk/sos/display#supports).
 * </details>
 *
 * ### List of supported serial ports
 * Bellow is example list of serial ports that can be used with `sos.hardware.openSerialPort()` method.
 *
 * | Device type | Default value | Other available values |
 * |:----|:-----|:------|
 * | RaspberryPi / Linux | `/dev/ttyS0` | `/dev/ttyS1`, `/dev/ttyUSB0`, `/dev/ttyUSB1` |
 * | Windows | `COM3` | `COM1`, `COM2`, `COM4` etc. Usually it's always `COM3` |
 * | Samsung Kiosk | `PORT1` | `PORT2`, `PORT3`, `PORT4` |
 * | Android | `/dev/ttyusb0` | `/dev/ttyusb1`, `/dev/ttyusb2` |
 * | BrightSign | `0` | `0` (Serial Jack as `/dev/ttyS0`), `1` (GPIO port as `/dev/ttyS1`), `USB:A/0` (USB-to-serial as `/dev/ttyUSB0`) |
 */
class Hardware {
    messagePrefix;
    postMessage;
    static MESSAGE_PREFIX = 'hardware';
    led;
    barcodeScanner;
    eventEmitter;
    /** @internal */
    constructor(messagePrefix, postMessage) {
        this.messagePrefix = messagePrefix;
        this.postMessage = postMessage;
        this.eventEmitter = new events_1.EventEmitter();
        this.led = new Led_1.default(this.getMessagePrefix(), this.postMessage);
        this.barcodeScanner = new BarcodeScanner_1.default(this.getMessagePrefix(), this.postMessage);
    }
    /**
     * The `openSerialPort()` method opens a serial port for reading/writing. After the port is opened the method returns {@link ISerialPort} interface.
     *
     * @param options.device Specifies the address of the external device, check the table above for ports.
     * @param options.baudRate Specifies the data transmission speed in bits per second.
     * @param options.parity Specifies the form of error checking, whether (or what) extra bits are added to a byte.
     * @param options.databits Specifies the number of bits in a byte.
     * @param options.stopbits Specifies the number of bits used to signal the end of a communication packet.
     * @param options.rtscts  Enables or disables RTS/CTS handshaking over the serial port. Currently supported by selected models of BrightSign.
     * @returns Returns a promise that resolves to an instance of {@link ISerialPort} interface, which can be used to read/write data from/to the serial port.
     * @throws {Error} If the serial port cannot be opened.
     * @throws {Error} If the device address is not supported by the platform.
     * @throws {Error} If the device fails to process the data.
     * @throws {Error} If any other error occurs while opening the serial port.
     * @since 4.4.0
     *
     * @example
     * // Open serial port on BrightSign with default settings
     * const serialPort = await sos.hardware.openSerialPort({
     * 	device: '0',
     * 	baudRate: 9600,
     * });
     * serialPort.write('68656c6c6f'); // Write "hello" in hexadecimal
     */
    async openSerialPort(options) {
        const { refid } = await this.postMessage({
            type: this.getMessage('serial_port.open'),
            options,
        });
        return new SerialPort_1.default(refid, this.eventEmitter, this.getMessagePrefix(), this.postMessage);
    }
    /** @internal */
    handleMessageData(data) {
        switch (data.type) {
            case this.getMessage('serial_port.data'):
                this.eventEmitter.emit('serial_port.data.' + data.refid, data.data);
                break;
            default:
                this.barcodeScanner.handleMessageData(data);
        }
    }
    getMessage(name) {
        return this.getMessagePrefix() + '.' + name;
    }
    getMessagePrefix() {
        return this.messagePrefix + '.' + Hardware.MESSAGE_PREFIX;
    }
}
exports.default = Hardware;
//# sourceMappingURL=Hardware.js.map