import { Buffer as Buffer$1 } from "buffer"; import { ExtractType } from "typed-struct"; import { TypedEmitter } from "tiny-typed-emitter"; import { Duplex } from "stream"; //#region src/Packet.d.ts /** * Destination device type */ declare enum DeviceType { /** * Devices connected to the COM/USB port */ SendingCard = 0, ReceivingCard = 1, FunctionCard = 2 } /** * I/O operation type */ declare enum IO { Read = 0, Write = 1 } /** * Operation result code */ declare enum ErrorType { Succeeded = 0, Timeout = 1, RequestCRCError = 2, ResponseCRCError = 3, UnknownCommand = 4, Invalid = 255 } /** * Request header */ declare const REQUEST = 43605; /** * Response header */ declare const RESPONSE = 21930; /** * For a request, this is the source (sender) address; for a response, the destination address */ declare const COMPUTER = 254; /** * Constructor for creating and processing binary packages used when communicating with novastar devices * @see {@link Packet} * @see {@link https://sarakusha.github.io/typed-struct/interfaces/StructConstructor.html} */ declare const Packet: import("typed-struct",{ with: { "resolution-mode": "import" } }).StructConstructor<{ head: 43605 | 21930; ack: ErrorType; serno: number; source: number; destination: number; deviceType: number; port: number; rcvIndex: number; io: IO; address: number; length: number; readonly data: Buffer; crc: number; }, "Packet"> & import("typed-struct",{ with: { "resolution-mode": "import" } }).CRC; crc: number; }, "Packet">>; /** * @typedef Packet * @property head - [REQUEST](#REQUEST) - for requests and [RESPONSE](#RESPONSE) for responses * @property ack - always `0` for request and result code for response * @property serno - sequential number of the request, and the corresponding response (set automatically) * @property source - source address, always [COMPUTER](#COMPUTER) for request * @property destination - destination address, usually `0` for request * @property deviceType - target device type * @property port - port number * @property rcvIndex - receiving card index * @property io - I/O operation type * @property address - register unit address * @property length - length of data requested when reading or length of data sent when writing * @property data - data sent on write or empty buffer on read * @property crc - cyclic redundancy check */ interface Packet extends ExtractType {} /** * [[Packet]] type guard * @param packet */ declare function isPacket(packet: unknown): packet is Packet; //#endregion //#region src/Request.d.ts /** * Custom request prepared for sending to Novastar devices */ declare class Request extends Packet { private static counter; /** * If this is true, then do not wait for a response from the device */ readonly broadcast?: Broadcast; /** * Overriding the timeout for this request */ timeout?: number; /** * Overriding the maximum data length for this request */ maxLength?: number; /** * Preserves the size of the original request if it exceeds 65535. */ readonly originalLength?: number; /** * For debugging and describing exceptions */ readonly tag?: string; /** * @internal * Copy constructor * @param other * @param broadcast * @param tag */ constructor(other: Packet, broadcast?: Broadcast, tag?: string); /** * Create a read request * @param readLength - requested data length * @param tag - description */ constructor(readLength: number, tag?: string); /** * Create a write request. If `broadcast` is true, then do not wait for a response from the device * @param writeData - data to send * @param broadcast * @param tag description */ constructor(writeData: Buffer | ReadonlyArray, broadcast?: Broadcast, tag?: string); constructor(writeData: Buffer | ReadonlyArray, tag: string); /** * Split the original request into chunks with the specified maximum data length * @param req - original request * @param maxLength - maximum data length * @returns - chunks */ static makeChunks(req: Request, maxLength?: number): Request[]; private static next; } //#endregion //#region src/Connection.d.ts type ResolveResponse = (res: Packet) => void; type WaitingRequest = [req: Request, resolve: ResolveResponse]; interface ConnectionEvents { /** * The open event happens when the connection is opened and ready for writing */ open(): void; /** * The close event is emitted when the connection is closed. */ close(): void; } type Result = SkipErrors extends false ? Packet : Packet | null; type Response = Promise : void>; type ConnectionParams = { /** * automatically opens this connection, see {@link Connection.open} * @default true */ open?: boolean; /** * Default timeout for this connection * @default 1000 */ timeout?: number; /** * Maximum default data length in single {@link Request} for this connection * @default 256 */ maxLength?: number; /** * Whether to process requests serially * @default false */ serial?: boolean; }; /** * Wrapper for I/O stream using {@link Request} for out and {@link Packet} to in */ declare class Connection extends TypedEmitter { readonly stream: S; /** * Default timeout for this connection */ timeout: number; /** * Maximum default data length in single {@link Request} for this connection */ maxLength: number; protected ready: Promise; protected queue: WaitingRequest[]; private encoder; private decoder; private connected; protected serial: boolean; /** * Constructor * @param stream - wrapped I/O stream */ constructor(stream: S, { open, timeout, maxLength, serial }?: ConnectionParams); /** * Returns true if the connection is ready for writing. */ get isConnected(): boolean; /** * Pipes streams (encoder->stream->decoder) together. * Emits {@link ConnectionEvents.open} */ open(): void; /** * Detaches streams. Emits {@link ConnectionEvents.close} */ close(): void; /** * Decodes the request and sends the raw data to the stream after the previous operation * completes, If the request `length` exceeds the `maxLength`, the request will be split into * multiple requests and responses will be composed. * @param req - request * @returns If the request is not broadcast, then a successful response to the request is * returned. If the response status is not successful or a timeout occurs, an exception is thrown. * @see TimeoutError * @see ResponseError */ send(req: Request): Response; /** * Sends a request, `length` of which does not exceed `maxLength`. * @param req * @returns In case of no response by timeout, returns null. Don't forget to check the `ack` * status of a response packet. */ trySend(req: Request): Promise; protected sendImpl(req: Request, skipError?: SkipErrors): Response; protected wait(req: Request, skipErrors?: SkipErrors): Promise>; protected listener: (res: Packet) => void; protected getMaxLength(req: Request): number; } //#endregion //#region src/Session.d.ts /** * Base implementation of the extensible `Session` class */ interface Session { /** * Current connection to interact with the device */ readonly connection: Connection; /** * Is current [connection]{@link Session.connection} active */ readonly isConnected: boolean; /** * Change the current timeout keeping the previous value * @param timeout */ pushTimeout(timeout: number): void; /** * Restore previous timeout value */ popTimeout(): number; /** * Close current connection */ close(): boolean; } /** * API extensible via plugins from @novastar/gen. * @remarks Original API contains more than a thousand methods, and you won't need all of * them, so you just include the methods you need, and they will be embedded into your `Session` * instance */ interface API { readonly version: '2.0'; } interface SessionStatic { new (connection: Connection): Session & API; } /** * Session */ declare const Session: SessionStatic; //#endregion //#region src/ResponseError.d.ts /** * Response error */ declare class ResponseError extends Error { readonly res: Readonly; readonly tag?: string | undefined; /** * Constructor * @param res - response * @param tag - description */ constructor(res: Readonly, tag?: string | undefined); } //#endregion //#region src/TimeoutError.d.ts declare class TimeoutError extends Error { readonly req: Packet; readonly tag?: string | undefined; constructor(req: Packet, tag?: string | undefined); } //#endregion //#region src/ConnectionClosedError.d.ts declare class ConnectionClosedError extends Error { constructor(); } //#endregion //#region src/helper.d.ts type ArrayLike = unknown[] | Buffer | string; /** * Buffer formatting * @param buffer */ declare function printBuffer(buffer: Buffer): string; /** * Create a promise which resolves after the specified milliseconds. * @param ms */ declare const delay: (ms: number) => Promise; /** * Performs an asynchronous operation sequentially on all elements of an array * @param array * @param action */ declare function series(array: ReadonlyArray, action: (item: T, index: number, arr: ReadonlyArray, results: ReadonlyArray) => Promise): Promise; /** * 'Not Empty' type guard * @param value */ declare function notEmpty(value: TValue | null | undefined | void): value is TValue; //#endregion //#region src/index.d.ts /** * Converts a `data` property of type `Buffer` of length 1, 2, or 4 bytes to an unsigned integer. * @param data - Buffer 1, 2 or 4 bytes long to convert */ declare const decodeUIntLE: ({ data }: Packet) => number; /** * Stores a non-negative number in a buffer of the specified length * @param value - Non-negative integer * @param size - Buffer size (1, 2, 4 bytes) */ declare const encodeUIntLE: (value: number, size: number) => Buffer$1; //#endregion export { API, ArrayLike, COMPUTER, Connection, ConnectionClosedError, type ConnectionEvents, DeviceType, ErrorType, IO, Packet, REQUEST, RESPONSE, Request, ResponseError, Session, SessionStatic, TimeoutError, decodeUIntLE, delay, encodeUIntLE, isPacket, notEmpty, printBuffer, series }; //# sourceMappingURL=index.d.mts.map