import type { Server } from 'node:http'; import type { pushResponse } from './types/devicepush.js'; import type { devices } from './types/deviceresponse.js'; import type { deviceStatus, deviceStatusRequest } from './types/devicestatus.js'; import { EventEmitter } from 'node:events'; /** * The `SwitchBotOpenAPI` class provides methods to interact with the SwitchBot OpenAPI. * It allows you to retrieve device information, control devices, and manage webhooks. * * @extends EventEmitter * * @example * ```typescript * const switchBotAPI = new SwitchBotOpenAPI('your-token', 'your-secret'); * * // Get devices * switchBotAPI.getDevices().then(response => { * console.log(response); * }).catch(error => { * console.error(error); * }); * * // Control a device * switchBotAPI.controlDevice('device-id', 'turnOn', 'default').then(response => { * console.log(response); * }).catch(error => { * console.error(error); * }); * * // Setup webhook * switchBotAPI.setupWebhook('http://your-webhook-url').then(() => { * console.log('Webhook setup successfully'); * }).catch(error => { * console.error(error); * }); * ``` * * @param {string} token - The API token used for authentication. * @param {string} secret - The secret key used for signing requests. */ export declare class SwitchBotOpenAPI extends EventEmitter { private token; private secret; private baseURL; webhookEventListener?: Server | null; /** * Creates an instance of the SwitchBot OpenAPI client. * * @param token - The API token used for authentication. * @param secret - The secret key used for signing requests. */ constructor(token: string, secret: string); /** * Emits a log event with the specified log level and message. * * @param level - The severity level of the log (e.g., 'info', 'warn', 'error'). * @param message - The log message to be emitted. */ private emitLog; /** * Retrieves the list of devices from the SwitchBot OpenAPI. * * @returns {Promise<{ response: body, statusCode: number }>} A promise that resolves to an object containing the API response. * @throws {Error} Throws an error if the request to get devices fails. */ getDevices(): Promise<{ response: devices; statusCode: number; }>; /** * Controls a device by sending a command to the SwitchBot API. * * @param deviceId - The unique identifier of the device to control. * @param command - The command to send to the device. * @param parameter - The parameter for the command. * @param commandType - The type of the command, defaults to 'command'. * @returns {Promise<{ response: pushResponse['body'], statusCode: pushResponse['statusCode'] }>} A promise that resolves to an object containing the API response. * @throws An error if the device control fails. */ controlDevice(deviceId: string, command: string, parameter: string, commandType?: string): Promise<{ response: pushResponse['body']; statusCode: pushResponse['statusCode']; }>; /** * Retrieves the status of a specific device. * * @param deviceId - The unique identifier of the device. * @returns {Promise<{ response: deviceStatus, statusCode: deviceStatusRequest['statusCode'] }>} A promise that resolves to the device status. * @throws An error if the request fails. */ getDeviceStatus(deviceId: string): Promise<{ response: deviceStatus; statusCode: deviceStatusRequest['statusCode']; }>; /** * Generates the headers required for authentication with the SwitchBot OpenAPI. * * @returns An object containing the following headers: * - `Authorization`: The token used for authorization. * - `sign`: The HMAC-SHA256 signature of the concatenated token, timestamp, and nonce. * - `nonce`: A unique identifier for the request, formatted as a UUID. * - `t`: The current timestamp in milliseconds since the Unix epoch. * - `Content-Type`: The content type of the request, set to `application/json`. */ private generateHeaders; /** * Sets up a webhook listener and configures the webhook on the server. * * This method performs the following steps: * 1. Creates a local server to listen for incoming webhook events. * 2. Sends a request to set up the webhook with the provided URL. * 3. Sends a request to update the webhook configuration. * 4. Sends a request to query the current webhook URL. * * @param url - The URL to which the webhook events will be sent. * @returns A promise that resolves when the webhook setup is complete. * * @throws Will log an error if any step in the webhook setup process fails. */ setupWebhook(url: string): Promise; /** * Deletes a webhook by sending a request to the specified URL. * * @param url - The URL of the webhook to be deleted. * @returns A promise that resolves when the webhook is successfully deleted. * * @throws Will log an error if the deletion fails. */ deleteWebhook(url: string): Promise; } //# sourceMappingURL=switchbot-openapi.d.ts.map