-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
1,265 additions
and
1,848 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
server/services/ewelink/lib/config/ewelink.createClient.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @description Create eWeLink client. | ||
* @example | ||
* this.createClient(); | ||
*/ | ||
async function createClient() { | ||
const { applicationId, applicationSecret, applicationRegion } = this.configuration; | ||
|
||
this.ewelinkClient = new this.eweLinkApi.WebAPI({ | ||
appId: applicationId, | ||
appSecret: applicationSecret, | ||
region: applicationRegion, | ||
}); | ||
} | ||
|
||
module.exports = { | ||
createClient, | ||
}; |
53 changes: 53 additions & 0 deletions
53
server/services/ewelink/lib/config/ewelink.loadConfiguration.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const { ServiceNotConfiguredError } = require('../../../../utils/coreErrors'); | ||
const logger = require('../../../../utils/logger'); | ||
const { CONFIGURATION_KEYS } = require('../utils/constants'); | ||
|
||
/** | ||
* @description Load eWeLink configuration. | ||
* @example | ||
* await this.loadConfiguration(); | ||
*/ | ||
async function loadConfiguration() { | ||
logger.info('eWeLink: loading stored configuration...'); | ||
this.updateStatus({ configured: false }); | ||
|
||
try { | ||
const applicationId = await this.gladys.variable.getValue(CONFIGURATION_KEYS.APPLICATION_ID, this.serviceId); | ||
const applicationSecret = await this.gladys.variable.getValue( | ||
CONFIGURATION_KEYS.APPLICATION_SECRET, | ||
this.serviceId, | ||
); | ||
const applicationRegion = await this.gladys.variable.getValue( | ||
CONFIGURATION_KEYS.APPLICATION_REGION, | ||
this.serviceId, | ||
); | ||
|
||
if (!applicationId || !applicationSecret || !applicationRegion) { | ||
throw new ServiceNotConfiguredError('eWeLink configuration is not setup'); | ||
} | ||
|
||
this.configuration = { applicationId, applicationSecret, applicationRegion }; | ||
|
||
this.createClient(); | ||
|
||
// Load tokens from databate | ||
const tokens = await this.gladys.variable.getValue(CONFIGURATION_KEYS.USER_TOKENS, this.serviceId); | ||
if (tokens) { | ||
const tokenObject = JSON.parse(tokens); | ||
this.ewelinkClient.at = tokenObject.accessToken; | ||
this.ewelinkClient.rt = tokenObject.refreshToken; | ||
} else { | ||
throw new ServiceNotConfiguredError('eWeLink user is not connected'); | ||
} | ||
|
||
this.updateStatus({ configured: true, connected: !!tokens }); | ||
logger.info('eWeLink: stored configuration well loaded...'); | ||
} catch (e) { | ||
this.updateStatus({ configured: false, connected: false }); | ||
throw e; | ||
} | ||
} | ||
|
||
module.exports = { | ||
loadConfiguration, | ||
}; |
42 changes: 42 additions & 0 deletions
42
server/services/ewelink/lib/config/ewelink.saveConfiguration.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const { BadParameters } = require('../../../../utils/coreErrors'); | ||
const logger = require('../../../../utils/logger'); | ||
const { CONFIGURATION_KEYS } = require('../utils/constants'); | ||
|
||
/** | ||
* @description Save eWeLink application configuration. | ||
* @param {object} configuration - EWeLink application configuration. | ||
* @param {string} [configuration.applicationId] - Application ID. | ||
* @param {string} [configuration.applicationSecret] - Application secret. | ||
* @param {string} [configuration.applicationRegion] - Application region. | ||
* @example | ||
* await this.saveConfiguration(configuration); | ||
*/ | ||
async function saveConfiguration({ applicationId = '', applicationSecret = '', applicationRegion = '' }) { | ||
logger.info('eWeLink: saving new configuration...'); | ||
this.updateStatus({ configured: false }); | ||
|
||
if (applicationId === '' || applicationSecret === '' || applicationRegion === '') { | ||
throw new BadParameters('eWeLink: all application ID/Secret/Region are required.'); | ||
} | ||
|
||
try { | ||
await this.gladys.variable.setValue(CONFIGURATION_KEYS.APPLICATION_ID, applicationId, this.serviceId); | ||
await this.gladys.variable.setValue(CONFIGURATION_KEYS.APPLICATION_SECRET, applicationSecret, this.serviceId); | ||
await this.gladys.variable.setValue(CONFIGURATION_KEYS.APPLICATION_REGION, applicationRegion, this.serviceId); | ||
await this.gladys.variable.destroy(CONFIGURATION_KEYS.USER_TOKENS, this.serviceId); | ||
|
||
this.configuration = { applicationId, applicationSecret, applicationRegion }; | ||
|
||
this.createClient(); | ||
|
||
this.updateStatus({ configured: true, connected: false }); | ||
logger.info('eWeLink: new configuration well saved...'); | ||
} catch (e) { | ||
this.updateStatus({ configured: false }); | ||
throw e; | ||
} | ||
} | ||
|
||
module.exports = { | ||
saveConfiguration, | ||
}; |
20 changes: 20 additions & 0 deletions
20
server/services/ewelink/lib/config/ewelink.updateStatus.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { EVENTS, WEBSOCKET_MESSAGE_TYPES } = require('../../../../utils/constants'); | ||
|
||
/** | ||
* @description Update the service status and emit WebSocket event. | ||
* @param {object} newStatus - New service status. | ||
* @example | ||
* this.updateStatus({ configured: true }); | ||
*/ | ||
function updateStatus(newStatus) { | ||
this.status = { ...this.status, ...newStatus }; | ||
|
||
this.gladys.event.emit(EVENTS.WEBSOCKET.SEND_ALL, { | ||
type: WEBSOCKET_MESSAGE_TYPES.EWELINK.STATUS, | ||
payload: this.status, | ||
}); | ||
} | ||
|
||
module.exports = { | ||
updateStatus, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.