Skip to content

Commit 6c8522e

Browse files
committed
clean webhooks
1 parent cff6029 commit 6c8522e

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

main.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,42 +176,65 @@ class Tedee extends utils.Adapter {
176176
res.send('OK');
177177
});
178178
//receive list of webhooks
179+
await this.cleanWebhooks();
180+
//register webhook
181+
this.log.debug('Registering webhook');
179182
await this.requestClient({
180-
methode: 'get',
183+
method: 'post',
181184
url: 'http://' + this.config.bridgeip + '/' + this.apiVersion + '/callback',
182185
headers: {
183186
accept: '*/*',
184187
api_token: this.hashedAPIKey(),
188+
content_type: 'application/json',
189+
},
190+
data: {
191+
url: 'http://' + host + ':' + port + '/webhook',
192+
headers: [],
185193
},
186194
})
187195
.then(async (res) => {
188196
this.log.debug(JSON.stringify(res.data));
197+
this.log.info('Webhook registered');
189198
})
190199
.catch((error) => {
200+
this.log.error("Couldn't register webhook");
191201
this.log.error(error);
192202
error.response && this.log.error(JSON.stringify(error.response.data));
193203
});
194-
//register webhook
195-
this.log.debug('Registering webhook');
204+
}
205+
206+
async cleanWebhooks() {
196207
await this.requestClient({
197-
method: 'post',
208+
methode: 'get',
198209
url: 'http://' + this.config.bridgeip + '/' + this.apiVersion + '/callback',
199210
headers: {
200211
accept: '*/*',
201212
api_token: this.hashedAPIKey(),
202-
content_type: 'application/json',
203-
},
204-
data: {
205-
url: 'http://' + host + ':' + port + '/webhook',
206-
headers: [],
207213
},
208214
})
209215
.then(async (res) => {
210216
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+
}
212236
})
213237
.catch((error) => {
214-
this.log.error("Couldn't register webhook");
215238
this.log.error(error);
216239
error.response && this.log.error(JSON.stringify(error.response.data));
217240
});
@@ -350,8 +373,9 @@ class Tedee extends utils.Adapter {
350373
* Is called when adapter shuts down - callback has to be called under any circumstances!
351374
* @param {() => void} callback
352375
*/
353-
onUnload(callback) {
376+
async onUnload(callback) {
354377
try {
378+
await this.cleanWebhooks();
355379
this.setState('info.connection', false, true);
356380
this.refreshTimeout && clearTimeout(this.refreshTimeout);
357381
this.reLoginTimeout && clearTimeout(this.reLoginTimeout);
@@ -385,7 +409,7 @@ class Tedee extends utils.Adapter {
385409

386410
await this.requestClient({
387411
method: 'POST',
388-
url: 'https://' + this.config.bridgeip + '/' + this.apiVersion + '/' + deviceId + '/' + command,
412+
url: 'http://' + this.config.bridgeip + '/' + this.apiVersion + '/' + deviceId + '/' + command,
389413
headers: {
390414
acceot: '*/*',
391415
api_token: this.hashedAPIKey(),

0 commit comments

Comments
 (0)