@@ -176,42 +176,65 @@ class Tedee extends utils.Adapter {
176
176
res . send ( 'OK' ) ;
177
177
} ) ;
178
178
//receive list of webhooks
179
+ await this . cleanWebhooks ( ) ;
180
+ //register webhook
181
+ this . log . debug ( 'Registering webhook' ) ;
179
182
await this . requestClient ( {
180
- methode : 'get ' ,
183
+ method : 'post ' ,
181
184
url : 'http://' + this . config . bridgeip + '/' + this . apiVersion + '/callback' ,
182
185
headers : {
183
186
accept : '*/*' ,
184
187
api_token : this . hashedAPIKey ( ) ,
188
+ content_type : 'application/json' ,
189
+ } ,
190
+ data : {
191
+ url : 'http://' + host + ':' + port + '/webhook' ,
192
+ headers : [ ] ,
185
193
} ,
186
194
} )
187
195
. then ( async ( res ) => {
188
196
this . log . debug ( JSON . stringify ( res . data ) ) ;
197
+ this . log . info ( 'Webhook registered' ) ;
189
198
} )
190
199
. catch ( ( error ) => {
200
+ this . log . error ( "Couldn't register webhook" ) ;
191
201
this . log . error ( error ) ;
192
202
error . response && this . log . error ( JSON . stringify ( error . response . data ) ) ;
193
203
} ) ;
194
- //register webhook
195
- this . log . debug ( 'Registering webhook' ) ;
204
+ }
205
+
206
+ async cleanWebhooks ( ) {
196
207
await this . requestClient ( {
197
- method : 'post ' ,
208
+ methode : 'get ' ,
198
209
url : 'http://' + this . config . bridgeip + '/' + this . apiVersion + '/callback' ,
199
210
headers : {
200
211
accept : '*/*' ,
201
212
api_token : this . hashedAPIKey ( ) ,
202
- content_type : 'application/json' ,
203
- } ,
204
- data : {
205
- url : 'http://' + host + ':' + port + '/webhook' ,
206
- headers : [ ] ,
207
213
} ,
208
214
} )
209
215
. then ( async ( res ) => {
210
216
this . log . debug ( JSON . stringify ( res . data ) ) ;
211
- this . log . info ( 'Webhook registered' ) ;
217
+ for ( const webhook of res . data ) {
218
+ this . log . info ( 'Deleting webhook ' + webhook . id + ' ' + webhook . url ) ;
219
+ await this . requestClient ( {
220
+ method : 'delete' ,
221
+ url : 'http://' + this . config . bridgeip + '/' + this . apiVersion + '/callback/' + webhook . id ,
222
+ headers : {
223
+ accept : '*/*' ,
224
+ api_token : this . hashedAPIKey ( ) ,
225
+ } ,
226
+ } )
227
+ . then ( ( res ) => {
228
+ this . log . debug ( JSON . stringify ( res . data ) ) ;
229
+ this . log . info ( 'Webhook deleted' ) ;
230
+ } )
231
+ . catch ( ( error ) => {
232
+ this . log . error ( error ) ;
233
+ error . response && this . log . error ( JSON . stringify ( error . response . data ) ) ;
234
+ } ) ;
235
+ }
212
236
} )
213
237
. catch ( ( error ) => {
214
- this . log . error ( "Couldn't register webhook" ) ;
215
238
this . log . error ( error ) ;
216
239
error . response && this . log . error ( JSON . stringify ( error . response . data ) ) ;
217
240
} ) ;
@@ -350,8 +373,9 @@ class Tedee extends utils.Adapter {
350
373
* Is called when adapter shuts down - callback has to be called under any circumstances!
351
374
* @param {() => void } callback
352
375
*/
353
- onUnload ( callback ) {
376
+ async onUnload ( callback ) {
354
377
try {
378
+ await this . cleanWebhooks ( ) ;
355
379
this . setState ( 'info.connection' , false , true ) ;
356
380
this . refreshTimeout && clearTimeout ( this . refreshTimeout ) ;
357
381
this . reLoginTimeout && clearTimeout ( this . reLoginTimeout ) ;
@@ -385,7 +409,7 @@ class Tedee extends utils.Adapter {
385
409
386
410
await this . requestClient ( {
387
411
method : 'POST' ,
388
- url : 'https ://' + this . config . bridgeip + '/' + this . apiVersion + '/' + deviceId + '/' + command ,
412
+ url : 'http ://' + this . config . bridgeip + '/' + this . apiVersion + '/' + deviceId + '/' + command ,
389
413
headers : {
390
414
acceot : '*/*' ,
391
415
api_token : this . hashedAPIKey ( ) ,
0 commit comments