@@ -70,6 +70,7 @@ export class SwitchBotOpenAPI extends EventEmitter {
7070 super ( )
7171 this . token = token
7272 this . secret = secret
73+ this . emitLog ( 'info' , `Token: ${ token } , Secret: ${ secret } ` )
7374 this . baseURL = urls . baseURL
7475
7576 if ( hostname ) {
@@ -93,9 +94,11 @@ export class SwitchBotOpenAPI extends EventEmitter {
9394 * @returns {Promise<{ response: body, statusCode: number }> } A promise that resolves to an object containing the API response.
9495 * @throws {Error } Throws an error if the request to get devices fails.
9596 */
96- async getDevices ( ) : Promise < { response : devices , statusCode : number } > {
97+ async getDevices ( token ?: string , secret ?: string ) : Promise < { response : devices , statusCode : number } > {
9798 try {
98- const { body, statusCode } = await request ( urls . devicesURL , { headers : this . generateHeaders ( ) } )
99+ const configToken = token || this . token
100+ const configSecret = secret || this . secret
101+ const { body, statusCode } = await request ( urls . devicesURL , { headers : this . generateHeaders ( configToken , configSecret ) } )
99102 const response = await body . json ( ) as devices
100103 this . emitLog ( 'debug' , `Got devices: ${ JSON . stringify ( response ) } ` )
101104 this . emitLog ( 'debug' , `statusCode: ${ statusCode } ` )
@@ -116,11 +119,13 @@ export class SwitchBotOpenAPI extends EventEmitter {
116119 * @returns {Promise<{ response: pushResponse['body'], statusCode: pushResponse['statusCode'] }> } A promise that resolves to an object containing the API response.
117120 * @throws An error if the device control fails.
118121 */
119- async controlDevice ( deviceId : string , command : string , parameter : string , commandType : string = 'command' ) : Promise < { response : pushResponse [ 'body' ] , statusCode : pushResponse [ 'statusCode' ] } > {
122+ async controlDevice ( deviceId : string , command : string , parameter : string , commandType : string = 'command' , token ?: string , secret ?: string ) : Promise < { response : pushResponse [ 'body' ] , statusCode : pushResponse [ 'statusCode' ] } > {
120123 try {
124+ const configToken = token || this . token
125+ const configSecret = secret || this . secret
121126 const { body, statusCode } = await request ( `${ this . baseURL } /devices/${ deviceId } /commands` , {
122127 method : 'POST' ,
123- headers : this . generateHeaders ( ) ,
128+ headers : this . generateHeaders ( configToken , configSecret ) ,
124129 body : JSON . stringify ( {
125130 command,
126131 parameter,
@@ -144,11 +149,13 @@ export class SwitchBotOpenAPI extends EventEmitter {
144149 * @returns {Promise<{ response: deviceStatus, statusCode: deviceStatusRequest['statusCode'] }> } A promise that resolves to the device status.
145150 * @throws An error if the request fails.
146151 */
147- async getDeviceStatus ( deviceId : string ) : Promise < { response : deviceStatus , statusCode : deviceStatusRequest [ 'statusCode' ] } > {
152+ async getDeviceStatus ( deviceId : string , token ?: string , secret ?: string ) : Promise < { response : deviceStatus , statusCode : deviceStatusRequest [ 'statusCode' ] } > {
148153 try {
154+ const configToken = token || this . token
155+ const configSecret = secret || this . secret
149156 const { body, statusCode } = await request ( `${ this . baseURL } /devices/${ deviceId } /status` , {
150157 method : 'GET' ,
151- headers : this . generateHeaders ( ) ,
158+ headers : this . generateHeaders ( configToken , configSecret ) ,
152159 } )
153160 const response = await body . json ( ) as deviceStatus
154161 this . emitLog ( 'debug' , `Got device status: ${ deviceId } ` )
@@ -170,18 +177,18 @@ export class SwitchBotOpenAPI extends EventEmitter {
170177 * - `t`: The current timestamp in milliseconds since the Unix epoch.
171178 * - `Content-Type`: The content type of the request, set to `application/json`.
172179 */
173- private generateHeaders = ( ) : { 'Authorization' : string , 'sign' : string , 'nonce' : `${string } -${string } -${string } -${string } -${string } `, 't' : string , 'Content-Type' : string } => {
180+ private generateHeaders = ( configToken : string , configSecret : string ) : { 'Authorization' : string , 'sign' : string , 'nonce' : `${string } -${string } -${string } -${string } -${string } `, 't' : string , 'Content-Type' : string } => {
174181 const t = `${ Date . now ( ) } `
175182 const nonce = randomUUID ( )
176- const data = this . token + t + nonce
183+ const data = configToken + t + nonce
177184 const signTerm = crypto
178- . createHmac ( 'sha256' , this . secret )
185+ . createHmac ( 'sha256' , configSecret )
179186 . update ( Buffer . from ( data , 'utf-8' ) )
180187 . digest ( )
181188 const sign = signTerm . toString ( 'base64' )
182189
183190 return {
184- 'Authorization' : this . token ,
191+ 'Authorization' : configToken ,
185192 'sign' : sign ,
186193 'nonce' : nonce ,
187194 't' : t ,
@@ -203,7 +210,7 @@ export class SwitchBotOpenAPI extends EventEmitter {
203210 *
204211 * @throws Will log an error if any step in the webhook setup process fails.
205212 */
206- async setupWebhook ( url : string ) : Promise < void > {
213+ async setupWebhook ( url : string , token ?: string , secret ?: string ) : Promise < void > {
207214 try {
208215 const xurl = new URL ( url )
209216 const port = Number ( xurl . port )
@@ -237,9 +244,11 @@ export class SwitchBotOpenAPI extends EventEmitter {
237244 }
238245
239246 try {
247+ const configToken = token || this . token
248+ const configSecret = secret || this . secret
240249 const { body, statusCode } = await request ( urls . setupWebhook , {
241250 method : 'POST' ,
242- headers : this . generateHeaders ( ) ,
251+ headers : this . generateHeaders ( configToken , configSecret ) ,
243252 body : JSON . stringify ( {
244253 action : 'setupWebhook' ,
245254 url,
@@ -256,9 +265,11 @@ export class SwitchBotOpenAPI extends EventEmitter {
256265 }
257266
258267 try {
268+ const configToken = token || this . token
269+ const configSecret = secret || this . secret
259270 const { body, statusCode } = await request ( urls . updateWebhook , {
260271 method : 'POST' ,
261- headers : this . generateHeaders ( ) ,
272+ headers : this . generateHeaders ( configToken , configSecret ) ,
262273 body : JSON . stringify ( {
263274 action : 'updateWebhook' ,
264275 config : {
@@ -277,9 +288,11 @@ export class SwitchBotOpenAPI extends EventEmitter {
277288 }
278289
279290 try {
291+ const configToken = token || this . token
292+ const configSecret = secret || this . secret
280293 const { body, statusCode } = await request ( urls . queryWebhook , {
281294 method : 'POST' ,
282- headers : this . generateHeaders ( ) ,
295+ headers : this . generateHeaders ( configToken , configSecret ) ,
283296 body : JSON . stringify ( {
284297 action : 'queryUrl' ,
285298 } ) ,
@@ -304,11 +317,13 @@ export class SwitchBotOpenAPI extends EventEmitter {
304317 *
305318 * @throws Will log an error if the deletion fails.
306319 */
307- async deleteWebhook ( url : string ) : Promise < void > {
320+ async deleteWebhook ( url : string , token ?: string , secret ?: string ) : Promise < void > {
308321 try {
322+ const configToken = token || this . token
323+ const configSecret = secret || this . secret
309324 const { body, statusCode } = await request ( urls . deleteWebhook , {
310325 method : 'POST' ,
311- headers : this . generateHeaders ( ) ,
326+ headers : this . generateHeaders ( configToken , configSecret ) ,
312327 body : JSON . stringify ( {
313328 action : 'deleteWebhook' ,
314329 url,
0 commit comments