node-switchbot
Version:
The node-switchbot is a Node.js module which allows you to control your Switchbot Devices through Bluetooth (BLE).
142 lines • 6.6 kB
TypeScript
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, hostname?: 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;
/**
* Generates the headers required for authentication with the SwitchBot OpenAPI.
*
* @param configToken - The token used for authorization.
* @param configSecret - The secret key used to sign the request.
* @returns An object containing the necessary headers:
* - `Authorization`: The authorization token.
* - `sign`: The HMAC-SHA256 signature of the token, timestamp, and nonce.
* - `nonce`: A unique identifier for the request.
* - `t`: The current timestamp in milliseconds.
* - `Content-Type`: The content type of the request, set to 'application/json'.
*/
private generateHeaders;
/**
* Retrieves the list of devices from the SwitchBot OpenAPI.
* @param token - (Optional) The token used for authentication. If not provided, the instance token will be used.
* @param secret - (Optional) The secret used for authentication. If not provided, the instance secret will be used.
* @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(token?: string, secret?: string): Promise<{
response: devices;
statusCode: number;
}>;
/**
* Controls a device by sending a command to the SwitchBot API.
*
* @param deviceId - The ID 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 (default is 'command').
* @param token - (Optional) The token used for authentication. If not provided, the instance token will be used.
* @param secret - (Optional) The secret used for authentication. If not provided, the instance secret will be used.
* @returns A promise that resolves to an object containing the response body and status code.
* @throws An error if the device control fails.
*/
controlDevice(deviceId: string, command: string, parameter: string, commandType?: string, token?: string, secret?: string): Promise<{
response: pushResponse['body'];
statusCode: pushResponse['statusCode'];
}>;
/**
* Retrieves the status of a specific device.
*
* @param deviceId - The unique identifier of the device.
* @param token - (Optional) The token used for authentication. If not provided, the instance token will be used.
* @param secret - (Optional) The secret used for authentication. If not provided, the instance secret will be used.
* @returns A promise that resolves to an object containing the device status and the status code of the request.
* @throws An error if the request fails.
*/
getDeviceStatus(deviceId: string, token?: string, secret?: string): Promise<{
response: deviceStatus;
statusCode: deviceStatusRequest['statusCode'];
}>;
/**
* 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.
* @param token - (Optional) The token used for authentication. If not provided, the instance token will be used.
* @param secret - (Optional) The secret used for authentication. If not provided, the instance secret will be used.
* @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, token?: string, secret?: string): Promise<void>;
/**
* Deletes a webhook by sending a request to the specified URL.
*
* @param url - The URL of the webhook to be deleted.
* @param token - (Optional) The token used for authentication. If not provided, the instance token will be used.
* @param secret - (Optional) The secret used for authentication. If not provided, the instance secret will be used.
* @returns A promise that resolves when the webhook is successfully deleted.
*
* @throws Will log an error if the deletion fails.
*/
deleteWebhook(url: string, token?: string, secret?: string): Promise<void>;
}
//# sourceMappingURL=switchbot-openapi.d.ts.map