node-switchbot 2.5.0-beta.25 → 2.5.0-beta.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,55 +1,121 @@
|
|
|
1
|
+
import type { Server } from 'node:http';
|
|
2
|
+
import type { ApiResponse } from './types/types.js';
|
|
1
3
|
import { EventEmitter } from 'node:events';
|
|
2
|
-
import { createServer } from 'node:http';
|
|
3
4
|
/**
|
|
4
|
-
* SwitchBotOpenAPI class to interact with SwitchBot
|
|
5
|
+
* The `SwitchBotOpenAPI` class provides methods to interact with the SwitchBot OpenAPI.
|
|
6
|
+
* It allows you to retrieve device information, control devices, and manage webhooks.
|
|
7
|
+
*
|
|
8
|
+
* @extends EventEmitter
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const switchBotAPI = new SwitchBotOpenAPI('your-token', 'your-secret');
|
|
13
|
+
*
|
|
14
|
+
* // Get devices
|
|
15
|
+
* switchBotAPI.getDevices().then(response => {
|
|
16
|
+
* console.log(response);
|
|
17
|
+
* }).catch(error => {
|
|
18
|
+
* console.error(error);
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* // Control a device
|
|
22
|
+
* switchBotAPI.controlDevice('device-id', 'turnOn', 'default').then(response => {
|
|
23
|
+
* console.log(response);
|
|
24
|
+
* }).catch(error => {
|
|
25
|
+
* console.error(error);
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // Setup webhook
|
|
29
|
+
* switchBotAPI.setupWebhook('http://your-webhook-url').then(() => {
|
|
30
|
+
* console.log('Webhook setup successfully');
|
|
31
|
+
* }).catch(error => {
|
|
32
|
+
* console.error(error);
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @param {string} token - The API token used for authentication.
|
|
37
|
+
* @param {string} secret - The secret key used for signing requests.
|
|
5
38
|
*/
|
|
6
39
|
export declare class SwitchBotOpenAPI extends EventEmitter {
|
|
7
40
|
private token;
|
|
8
41
|
private secret;
|
|
9
42
|
private baseURL;
|
|
10
|
-
webhookEventListener?:
|
|
43
|
+
webhookEventListener?: Server | null;
|
|
44
|
+
/**
|
|
45
|
+
* Creates an instance of the SwitchBot OpenAPI client.
|
|
46
|
+
*
|
|
47
|
+
* @param token - The API token used for authentication.
|
|
48
|
+
* @param secret - The secret key used for signing requests.
|
|
49
|
+
*/
|
|
11
50
|
constructor(token: string, secret: string);
|
|
12
51
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* @param
|
|
52
|
+
* Emits a log event with the specified log level and message.
|
|
53
|
+
*
|
|
54
|
+
* @param level - The severity level of the log (e.g., 'info', 'warn', 'error').
|
|
55
|
+
* @param message - The log message to be emitted.
|
|
16
56
|
*/
|
|
17
57
|
private emitLog;
|
|
18
58
|
/**
|
|
19
|
-
*
|
|
59
|
+
* Retrieves the list of devices from the SwitchBot OpenAPI.
|
|
60
|
+
*
|
|
61
|
+
* @returns {Promise<{ response: ApiResponse }>} A promise that resolves to an object containing the API response.
|
|
62
|
+
* @throws {Error} Throws an error if the request to get devices fails.
|
|
63
|
+
*/
|
|
64
|
+
getDevices(): Promise<{
|
|
65
|
+
response: ApiResponse;
|
|
66
|
+
}>;
|
|
67
|
+
/**
|
|
68
|
+
* Generates the headers required for authentication with the SwitchBot OpenAPI.
|
|
69
|
+
*
|
|
70
|
+
* @returns An object containing the following headers:
|
|
71
|
+
* - `Authorization`: The token used for authorization.
|
|
72
|
+
* - `sign`: The HMAC-SHA256 signature of the concatenated token, timestamp, and nonce.
|
|
73
|
+
* - `nonce`: A unique identifier for the request, formatted as a UUID.
|
|
74
|
+
* - `t`: The current timestamp in milliseconds since the Unix epoch.
|
|
75
|
+
* - `Content-Type`: The content type of the request, set to `application/json`.
|
|
20
76
|
*/
|
|
21
|
-
getDevices(): Promise<any>;
|
|
22
77
|
private generateHeaders;
|
|
23
78
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @param
|
|
27
|
-
* @param
|
|
28
|
-
* @param
|
|
79
|
+
* Controls a device by sending a command to the SwitchBot API.
|
|
80
|
+
*
|
|
81
|
+
* @param deviceId - The unique identifier of the device to control.
|
|
82
|
+
* @param command - The command to send to the device.
|
|
83
|
+
* @param parameter - The parameter for the command.
|
|
84
|
+
* @param commandType - The type of the command, defaults to 'command'.
|
|
85
|
+
* @returns A promise that resolves to the API response.
|
|
86
|
+
* @throws An error if the device control fails.
|
|
29
87
|
*/
|
|
30
88
|
controlDevice(deviceId: string, command: string, parameter: string, commandType?: string): Promise<any>;
|
|
31
89
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
90
|
+
* Retrieves the status of a specific device.
|
|
91
|
+
*
|
|
92
|
+
* @param deviceId - The unique identifier of the device.
|
|
93
|
+
* @returns A promise that resolves to the device status.
|
|
94
|
+
* @throws An error if the request fails.
|
|
34
95
|
*/
|
|
35
96
|
getDeviceStatus(deviceId: string): Promise<any>;
|
|
36
97
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
98
|
+
* Sets up a webhook listener and configures the webhook on the server.
|
|
99
|
+
*
|
|
100
|
+
* This method performs the following steps:
|
|
101
|
+
* 1. Creates a local server to listen for incoming webhook events.
|
|
102
|
+
* 2. Sends a request to set up the webhook with the provided URL.
|
|
103
|
+
* 3. Sends a request to update the webhook configuration.
|
|
104
|
+
* 4. Sends a request to query the current webhook URL.
|
|
105
|
+
*
|
|
106
|
+
* @param url - The URL to which the webhook events will be sent.
|
|
107
|
+
* @returns A promise that resolves when the webhook setup is complete.
|
|
108
|
+
*
|
|
109
|
+
* @throws Will log an error if any step in the webhook setup process fails.
|
|
39
110
|
*/
|
|
40
111
|
setupWebhook(url: string): Promise<void>;
|
|
41
112
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
*
|
|
48
|
-
*/
|
|
49
|
-
queryWebhook(): Promise<void>;
|
|
50
|
-
/**
|
|
51
|
-
* Delete webhook configuration.
|
|
52
|
-
* @param url The webhook URL.
|
|
113
|
+
* Deletes a webhook by sending a request to the specified URL.
|
|
114
|
+
*
|
|
115
|
+
* @param url - The URL of the webhook to be deleted.
|
|
116
|
+
* @returns A promise that resolves when the webhook is successfully deleted.
|
|
117
|
+
*
|
|
118
|
+
* @throws Will log an error if the deletion fails.
|
|
53
119
|
*/
|
|
54
120
|
deleteWebhook(url: string): Promise<void>;
|
|
55
121
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"switchbot-openapi.d.ts","sourceRoot":"","sources":["../src/switchbot-openapi.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"switchbot-openapi.d.ts","sourceRoot":"","sources":["../src/switchbot-openapi.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAmB,MAAM,EAAkB,MAAM,WAAW,CAAA;AAExE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAInD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAO1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,qBAAa,gBAAiB,SAAQ,YAAY;IAChD,OAAO,CAAC,KAAK,CAAQ;IACrB,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,OAAO,CAAQ;IAEvB,oBAAoB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAO;IAE3C;;;;;OAKG;gBACS,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAOzC;;;;;OAKG;YACW,OAAO;IAIrB;;;;;OAKG;IACG,UAAU,IAAI,OAAO,CAAC;QAAE,QAAQ,EAAE,WAAW,CAAA;KAAE,CAAC;IAatD;;;;;;;;;OASG;IACH,OAAO,CAAC,eAAe,CAiBtB;IAED;;;;;;;;;OASG;IACG,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,GAAE,MAAkB,GAAG,OAAO,CAAC,GAAG,CAAC;IAqBxH;;;;;;OAMG;IACG,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAgBrD;;;;;;;;;;;;;OAaG;IACG,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA6F9C;;;;;;;OAOG;IACG,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAqBhD"}
|
|
@@ -5,13 +5,51 @@ import { createServer } from 'node:http';
|
|
|
5
5
|
import { request } from 'undici';
|
|
6
6
|
import { deleteWebhook, Devices, queryWebhook, setupWebhook, updateWebhook } from './settings.js';
|
|
7
7
|
/**
|
|
8
|
-
* SwitchBotOpenAPI class to interact with SwitchBot
|
|
8
|
+
* The `SwitchBotOpenAPI` class provides methods to interact with the SwitchBot OpenAPI.
|
|
9
|
+
* It allows you to retrieve device information, control devices, and manage webhooks.
|
|
10
|
+
*
|
|
11
|
+
* @extends EventEmitter
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const switchBotAPI = new SwitchBotOpenAPI('your-token', 'your-secret');
|
|
16
|
+
*
|
|
17
|
+
* // Get devices
|
|
18
|
+
* switchBotAPI.getDevices().then(response => {
|
|
19
|
+
* console.log(response);
|
|
20
|
+
* }).catch(error => {
|
|
21
|
+
* console.error(error);
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Control a device
|
|
25
|
+
* switchBotAPI.controlDevice('device-id', 'turnOn', 'default').then(response => {
|
|
26
|
+
* console.log(response);
|
|
27
|
+
* }).catch(error => {
|
|
28
|
+
* console.error(error);
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* // Setup webhook
|
|
32
|
+
* switchBotAPI.setupWebhook('http://your-webhook-url').then(() => {
|
|
33
|
+
* console.log('Webhook setup successfully');
|
|
34
|
+
* }).catch(error => {
|
|
35
|
+
* console.error(error);
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @param {string} token - The API token used for authentication.
|
|
40
|
+
* @param {string} secret - The secret key used for signing requests.
|
|
9
41
|
*/
|
|
10
42
|
export class SwitchBotOpenAPI extends EventEmitter {
|
|
11
43
|
token;
|
|
12
44
|
secret;
|
|
13
45
|
baseURL;
|
|
14
|
-
webhookEventListener;
|
|
46
|
+
webhookEventListener = null;
|
|
47
|
+
/**
|
|
48
|
+
* Creates an instance of the SwitchBot OpenAPI client.
|
|
49
|
+
*
|
|
50
|
+
* @param token - The API token used for authentication.
|
|
51
|
+
* @param secret - The secret key used for signing requests.
|
|
52
|
+
*/
|
|
15
53
|
constructor(token, secret) {
|
|
16
54
|
super();
|
|
17
55
|
this.token = token;
|
|
@@ -19,15 +57,19 @@ export class SwitchBotOpenAPI extends EventEmitter {
|
|
|
19
57
|
this.baseURL = 'https://api.switch-bot.com/v1.0';
|
|
20
58
|
}
|
|
21
59
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* @param
|
|
60
|
+
* Emits a log event with the specified log level and message.
|
|
61
|
+
*
|
|
62
|
+
* @param level - The severity level of the log (e.g., 'info', 'warn', 'error').
|
|
63
|
+
* @param message - The log message to be emitted.
|
|
25
64
|
*/
|
|
26
|
-
emitLog(level, message) {
|
|
65
|
+
async emitLog(level, message) {
|
|
27
66
|
this.emit('log', { level, message });
|
|
28
67
|
}
|
|
29
68
|
/**
|
|
30
|
-
*
|
|
69
|
+
* Retrieves the list of devices from the SwitchBot OpenAPI.
|
|
70
|
+
*
|
|
71
|
+
* @returns {Promise<{ response: ApiResponse }>} A promise that resolves to an object containing the API response.
|
|
72
|
+
* @throws {Error} Throws an error if the request to get devices fails.
|
|
31
73
|
*/
|
|
32
74
|
async getDevices() {
|
|
33
75
|
try {
|
|
@@ -42,6 +84,16 @@ export class SwitchBotOpenAPI extends EventEmitter {
|
|
|
42
84
|
throw new Error(`Failed to get devices: ${error.message}`);
|
|
43
85
|
}
|
|
44
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Generates the headers required for authentication with the SwitchBot OpenAPI.
|
|
89
|
+
*
|
|
90
|
+
* @returns An object containing the following headers:
|
|
91
|
+
* - `Authorization`: The token used for authorization.
|
|
92
|
+
* - `sign`: The HMAC-SHA256 signature of the concatenated token, timestamp, and nonce.
|
|
93
|
+
* - `nonce`: A unique identifier for the request, formatted as a UUID.
|
|
94
|
+
* - `t`: The current timestamp in milliseconds since the Unix epoch.
|
|
95
|
+
* - `Content-Type`: The content type of the request, set to `application/json`.
|
|
96
|
+
*/
|
|
45
97
|
generateHeaders = () => {
|
|
46
98
|
const t = `${Date.now()}`;
|
|
47
99
|
const nonce = randomUUID();
|
|
@@ -60,11 +112,14 @@ export class SwitchBotOpenAPI extends EventEmitter {
|
|
|
60
112
|
};
|
|
61
113
|
};
|
|
62
114
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* @param
|
|
66
|
-
* @param
|
|
67
|
-
* @param
|
|
115
|
+
* Controls a device by sending a command to the SwitchBot API.
|
|
116
|
+
*
|
|
117
|
+
* @param deviceId - The unique identifier of the device to control.
|
|
118
|
+
* @param command - The command to send to the device.
|
|
119
|
+
* @param parameter - The parameter for the command.
|
|
120
|
+
* @param commandType - The type of the command, defaults to 'command'.
|
|
121
|
+
* @returns A promise that resolves to the API response.
|
|
122
|
+
* @throws An error if the device control fails.
|
|
68
123
|
*/
|
|
69
124
|
async controlDevice(deviceId, command, parameter, commandType = 'command') {
|
|
70
125
|
try {
|
|
@@ -88,8 +143,11 @@ export class SwitchBotOpenAPI extends EventEmitter {
|
|
|
88
143
|
}
|
|
89
144
|
}
|
|
90
145
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
146
|
+
* Retrieves the status of a specific device.
|
|
147
|
+
*
|
|
148
|
+
* @param deviceId - The unique identifier of the device.
|
|
149
|
+
* @returns A promise that resolves to the device status.
|
|
150
|
+
* @throws An error if the request fails.
|
|
93
151
|
*/
|
|
94
152
|
async getDeviceStatus(deviceId) {
|
|
95
153
|
try {
|
|
@@ -108,41 +166,56 @@ export class SwitchBotOpenAPI extends EventEmitter {
|
|
|
108
166
|
}
|
|
109
167
|
}
|
|
110
168
|
/**
|
|
111
|
-
*
|
|
112
|
-
*
|
|
169
|
+
* Sets up a webhook listener and configures the webhook on the server.
|
|
170
|
+
*
|
|
171
|
+
* This method performs the following steps:
|
|
172
|
+
* 1. Creates a local server to listen for incoming webhook events.
|
|
173
|
+
* 2. Sends a request to set up the webhook with the provided URL.
|
|
174
|
+
* 3. Sends a request to update the webhook configuration.
|
|
175
|
+
* 4. Sends a request to query the current webhook URL.
|
|
176
|
+
*
|
|
177
|
+
* @param url - The URL to which the webhook events will be sent.
|
|
178
|
+
* @returns A promise that resolves when the webhook setup is complete.
|
|
179
|
+
*
|
|
180
|
+
* @throws Will log an error if any step in the webhook setup process fails.
|
|
113
181
|
*/
|
|
114
182
|
async setupWebhook(url) {
|
|
115
183
|
try {
|
|
116
184
|
const xurl = new URL(url);
|
|
117
|
-
const port = Number(xurl.port)
|
|
185
|
+
const port = Number(xurl.port);
|
|
118
186
|
const path = xurl.pathname;
|
|
119
|
-
this.webhookEventListener = createServer((request, response) => {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
187
|
+
this.webhookEventListener = createServer(async (request, response) => {
|
|
188
|
+
try {
|
|
189
|
+
if (request.url === path && request.method === 'POST') {
|
|
190
|
+
request.on('data', async (data) => {
|
|
191
|
+
try {
|
|
192
|
+
const body = JSON.parse(data);
|
|
193
|
+
await this.emitLog('debug', `Received Webhook: ${JSON.stringify(body)}`);
|
|
194
|
+
this.emit('webhookEvent', body);
|
|
195
|
+
}
|
|
196
|
+
catch (e) {
|
|
197
|
+
await this.emitLog('error', `Failed to handle webhook event data. Error:${e}`);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
response.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
201
|
+
response.end('OK');
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
await this.emitLog('error', `Invalid request received. URL:${request.url}, Method:${request.method}`);
|
|
205
|
+
response.writeHead(403, { 'Content-Type': 'text/plain' });
|
|
206
|
+
response.end(`NG`);
|
|
207
|
+
}
|
|
140
208
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
response.end('NG');
|
|
209
|
+
catch (e) {
|
|
210
|
+
await this.emitLog('error', `Failed to handle webhook event. Error:${e}`);
|
|
144
211
|
}
|
|
145
|
-
}).listen(port);
|
|
212
|
+
}).listen(port || 80);
|
|
213
|
+
}
|
|
214
|
+
catch (e) {
|
|
215
|
+
await this.emitLog('error', `Failed to create webhook listener. Error:${e.message}`);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
try {
|
|
146
219
|
const { body, statusCode } = await request(setupWebhook, {
|
|
147
220
|
method: 'POST',
|
|
148
221
|
headers: this.generateHeaders(),
|
|
@@ -153,20 +226,14 @@ export class SwitchBotOpenAPI extends EventEmitter {
|
|
|
153
226
|
}),
|
|
154
227
|
});
|
|
155
228
|
const response = await body.json();
|
|
156
|
-
this.emitLog('debug', `setupWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
|
|
229
|
+
await this.emitLog('debug', `setupWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
|
|
157
230
|
if (statusCode !== 200 || response?.statusCode !== 100) {
|
|
158
|
-
this.emitLog('error', `Failed to configure webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
231
|
+
await this.emitLog('error', `Failed to configure webhook. Existing webhook well be overridden. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
159
232
|
}
|
|
160
233
|
}
|
|
161
234
|
catch (e) {
|
|
162
|
-
this.emitLog('error', `Failed to configure webhook. Error: ${e.message}`);
|
|
235
|
+
await this.emitLog('error', `Failed to configure webhook. Error: ${e.message}`);
|
|
163
236
|
}
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Update webhook configuration.
|
|
167
|
-
* @param url The webhook URL.
|
|
168
|
-
*/
|
|
169
|
-
async updateWebhook(url) {
|
|
170
237
|
try {
|
|
171
238
|
const { body, statusCode } = await request(updateWebhook, {
|
|
172
239
|
method: 'POST',
|
|
@@ -180,19 +247,14 @@ export class SwitchBotOpenAPI extends EventEmitter {
|
|
|
180
247
|
}),
|
|
181
248
|
});
|
|
182
249
|
const response = await body.json();
|
|
183
|
-
this.emitLog('debug', `updateWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
|
|
250
|
+
await this.emitLog('debug', `updateWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
|
|
184
251
|
if (statusCode !== 200 || response?.statusCode !== 100) {
|
|
185
|
-
this.emitLog('error', `Failed to update webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
252
|
+
await this.emitLog('error', `Failed to update webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
186
253
|
}
|
|
187
254
|
}
|
|
188
255
|
catch (e) {
|
|
189
|
-
this.emitLog('error', `Failed to update webhook. Error
|
|
256
|
+
await this.emitLog('error', `Failed to update webhook. Error:${e.message}`);
|
|
190
257
|
}
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Query webhook configuration.
|
|
194
|
-
*/
|
|
195
|
-
async queryWebhook() {
|
|
196
258
|
try {
|
|
197
259
|
const { body, statusCode } = await request(queryWebhook, {
|
|
198
260
|
method: 'POST',
|
|
@@ -202,22 +264,25 @@ export class SwitchBotOpenAPI extends EventEmitter {
|
|
|
202
264
|
}),
|
|
203
265
|
});
|
|
204
266
|
const response = await body.json();
|
|
205
|
-
this.emitLog('debug', `queryWebhook: body:${JSON.stringify(response)}`);
|
|
206
|
-
this.emitLog('debug', `queryWebhook: statusCode:${statusCode}`);
|
|
267
|
+
await this.emitLog('debug', `queryWebhook: body:${JSON.stringify(response)}, statusCode:${statusCode}`);
|
|
207
268
|
if (statusCode !== 200 || response?.statusCode !== 100) {
|
|
208
|
-
this.emitLog('error', `Failed to query webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
269
|
+
await this.emitLog('error', `Failed to query webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
209
270
|
}
|
|
210
271
|
else {
|
|
211
|
-
this.emitLog('info', `Listening webhook on ${response?.body?.urls
|
|
272
|
+
await this.emitLog('info', `Listening webhook on ${response?.body?.urls[0]}`);
|
|
212
273
|
}
|
|
213
274
|
}
|
|
214
275
|
catch (e) {
|
|
215
|
-
this.emitLog('error', `Failed to query webhook. Error
|
|
276
|
+
await this.emitLog('error', `Failed to query webhook. Error:${e}`);
|
|
216
277
|
}
|
|
217
278
|
}
|
|
218
279
|
/**
|
|
219
|
-
*
|
|
220
|
-
*
|
|
280
|
+
* Deletes a webhook by sending a request to the specified URL.
|
|
281
|
+
*
|
|
282
|
+
* @param url - The URL of the webhook to be deleted.
|
|
283
|
+
* @returns A promise that resolves when the webhook is successfully deleted.
|
|
284
|
+
*
|
|
285
|
+
* @throws Will log an error if the deletion fails.
|
|
221
286
|
*/
|
|
222
287
|
async deleteWebhook(url) {
|
|
223
288
|
try {
|
|
@@ -230,17 +295,16 @@ export class SwitchBotOpenAPI extends EventEmitter {
|
|
|
230
295
|
}),
|
|
231
296
|
});
|
|
232
297
|
const response = await body.json();
|
|
233
|
-
this.emitLog('debug', `deleteWebhook: url:${url}, body:${JSON.stringify(response)}`);
|
|
234
|
-
this.emitLog('debug', `statusCode: ${statusCode}`);
|
|
298
|
+
await this.emitLog('debug', `deleteWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
|
|
235
299
|
if (statusCode !== 200 || response?.statusCode !== 100) {
|
|
236
|
-
this.emitLog('error', `Failed to delete webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
300
|
+
await this.emitLog('error', `Failed to delete webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
|
|
237
301
|
}
|
|
238
302
|
else {
|
|
239
|
-
this.emitLog('info', 'Unregistered webhook to close listening.');
|
|
303
|
+
await this.emitLog('info', 'Unregistered webhook to close listening.');
|
|
240
304
|
}
|
|
241
305
|
}
|
|
242
306
|
catch (e) {
|
|
243
|
-
this.emitLog('error', `Failed to delete webhook. Error
|
|
307
|
+
await this.emitLog('error', `Failed to delete webhook. Error:${e.message}`);
|
|
244
308
|
}
|
|
245
309
|
}
|
|
246
310
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"switchbot-openapi.js","sourceRoot":"","sources":["../src/switchbot-openapi.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,MAAM,EAAE,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AAExC,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AAEhC,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAEjG
|
|
1
|
+
{"version":3,"file":"switchbot-openapi.js","sourceRoot":"","sources":["../src/switchbot-openapi.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OA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Ae;oBACvB,GAAG;iBACJ,CAAC;aACH,CAAC,CAAA;YACF,MAAM,QAAQ,GAAQ,MAAM,IAAI,CAAC,IAAI,EAAiB,CAAA;YACtD,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,sBAAsB,GAAG,UAAU,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,gBAAgB,UAAU,EAAE,CAAC,CAAA;YACpH,IAAI,UAAU,KAAK,GAAG,IAAI,QAAQ,EAAE,UAAU,KAAK,GAAG,EAAE,CAAC;gBACvD,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,kCAAkC,UAAU,QAAQ,QAAQ,EAAE,UAAU,YAAY,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAA;YACtI,CAAC;iBAAM,CAAC;gBACN,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,0CAA0C,CAAC,CAAA;YACxE,CAAC;QACH,CAAC;QAAC,OAAO,CAAM,EAAE,CAAC;YAChB,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,mCAAmC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAA;QAC7E,CAAC;IACH,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-switchbot",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.5.0-beta.
|
|
4
|
+
"version": "2.5.0-beta.26",
|
|
5
5
|
"description": "The node-switchbot is a Node.js module which allows you to control your Switchbot Devices through Bluetooth (BLE).",
|
|
6
6
|
"author": "OpenWonderLabs (https://github.com/OpenWonderLabs)",
|
|
7
7
|
"license": "MIT",
|