diff --git a/plugins/alexa/package-lock.json b/plugins/alexa/package-lock.json index b2be3225fd..9f2be83ba2 100644 --- a/plugins/alexa/package-lock.json +++ b/plugins/alexa/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/alexa", - "version": "0.2.9", + "version": "0.2.10", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@scrypted/alexa", - "version": "0.2.9", + "version": "0.2.10", "dependencies": { "axios": "^1.3.4", "uuid": "^9.0.0" diff --git a/plugins/alexa/package.json b/plugins/alexa/package.json index 302dd02349..9bc1aab55b 100644 --- a/plugins/alexa/package.json +++ b/plugins/alexa/package.json @@ -1,6 +1,6 @@ { "name": "@scrypted/alexa", - "version": "0.2.9", + "version": "0.2.10", "scripts": { "scrypted-setup-project": "scrypted-setup-project", "prescrypted-setup-project": "scrypted-package-json", diff --git a/plugins/alexa/src/main.ts b/plugins/alexa/src/main.ts index 3eb54552cb..642c59e8bf 100644 --- a/plugins/alexa/src/main.ts +++ b/plugins/alexa/src/main.ts @@ -79,7 +79,7 @@ class AlexaPlugin extends ScryptedDeviceBase implements HttpRequestHandler, Mixi if (status === DeviceMixinStatus.Setup) await this.syncEndpoints(); - if (status === DeviceMixinStatus.Setup || status === DeviceMixinStatus.AlreadySetup) { + if (status === DeviceMixinStatus.Setup || status === DeviceMixinStatus.AlreadySetup) { if (!this.devices.has(eventSource.id)) { this.devices.set(eventSource.id, eventSource); @@ -142,7 +142,7 @@ class AlexaPlugin extends ScryptedDeviceBase implements HttpRequestHandler, Mixi await this.syncEndpoints(); } - async deviceListen(eventSource: ScryptedDevice | undefined, eventDetails: EventDetails, eventData: any) : Promise { + async deviceListen(eventSource: ScryptedDevice | undefined, eventDetails: EventDetails, eventData: any): Promise { if (!eventSource) return; @@ -194,14 +194,14 @@ class AlexaPlugin extends ScryptedDeviceBase implements HttpRequestHandler, Mixi // nothing to report if (data.context === undefined && data.event.payload === undefined) - return; - + return; + data = await this.addAccessToken(data); await this.postEvent(data); } - private async addAccessToken(data: any) : Promise { + private async addAccessToken(data: any): Promise { const accessToken = await this.getAccessToken(); if (data.event === undefined) @@ -232,7 +232,7 @@ class AlexaPlugin extends ScryptedDeviceBase implements HttpRequestHandler, Mixi 'api.fe.amazonalexa.com' ]; - async getAlexaEndpoint() : Promise { + async getAlexaEndpoint(): Promise { if (this.storageSettings.values.apiEndpoint) return this.storageSettings.values.apiEndpoint; @@ -276,7 +276,7 @@ class AlexaPlugin extends ScryptedDeviceBase implements HttpRequestHandler, Mixi }); } - async getEndpoints() : Promise { + async getEndpoints(): Promise { const endpoints: DiscoveryEndpoint[] = []; for (const id of Object.keys(systemManager.getSystemState())) { @@ -284,7 +284,7 @@ class AlexaPlugin extends ScryptedDeviceBase implements HttpRequestHandler, Mixi if (!device.mixins?.includes(this.id)) continue; - + const endpoint = await this.getEndpointForDevice(device); if (endpoint) endpoints.push(endpoint); @@ -319,7 +319,7 @@ class AlexaPlugin extends ScryptedDeviceBase implements HttpRequestHandler, Mixi const endpoints = await this.getEndpoints(); if (!endpoints.length) - return []; + return []; const accessToken = await this.getAccessToken(); const data = { @@ -448,7 +448,7 @@ class AlexaPlugin extends ScryptedDeviceBase implements HttpRequestHandler, Mixi self.console.warn(error?.response?.data); self.log.a(error?.response?.data?.error_description); break; - + case 'expired_token': self.console.warn(error?.response?.data); self.log.a(error?.response?.data?.error_description); @@ -480,9 +480,14 @@ class AlexaPlugin extends ScryptedDeviceBase implements HttpRequestHandler, Mixi this.storageSettings.values.tokenInfo = grant; this.storageSettings.values.apiEndpoint = undefined; this.accessToken = undefined; - + const self = this; - let accessToken = await this.getAccessToken().catch(reason => { + let accessToken: any; + + try { + accessToken = await this.getAccessToken(); + } + catch (reason) { self.console.error(`Failed to handle the AcceptGrant directive because ${reason}`); this.storageSettings.values.tokenInfo = undefined; @@ -491,36 +496,23 @@ class AlexaPlugin extends ScryptedDeviceBase implements HttpRequestHandler, Mixi response.send(authErrorResponse("ACCEPT_GRANT_FAILED", `Failed to handle the AcceptGrant directive because ${reason}`, directive)); - return undefined; - }); - - if (accessToken !== undefined) { - this.log.clearAlerts(); - - try { - response.send({ - "event": { - "header": { - "namespace": "Alexa.Authorization", - "name": "AcceptGrant.Response", - "messageId": createMessageId(), - "payloadVersion": "3" - }, - "payload": {} - } - }); - } catch (error) { - this.console.error(`AcceptGrant.Response failed because ${error}`); - - this.storageSettings.values.tokenInfo = undefined; - this.storageSettings.values.apiEndpoint = undefined; - this.accessToken = undefined; - throw error; + return; + }; + this.log.clearAlerts(); + response.send({ + "event": { + "header": { + "namespace": "Alexa.Authorization", + "name": "AcceptGrant.Response", + "messageId": createMessageId(), + "payloadVersion": "3" + }, + "payload": {} } - } + }); } - async getEndpointForDevice(device: ScryptedDevice) : Promise { + async getEndpointForDevice(device: ScryptedDevice): Promise { if (!device) return; @@ -545,7 +537,7 @@ class AlexaPlugin extends ScryptedDeviceBase implements HttpRequestHandler, Mixi }; let supportedEndpointHealths: any[] = []; - + if (device.interfaces.includes(ScryptedInterface.Online)) { supportedEndpointHealths.push({ "name": "connectivity"