Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup code #22

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 76 additions & 76 deletions src/utils/ewelink-connect.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,90 @@
const ewelink = require('ewelink-api');

module.exports = {
/**
* Perform a log in.
*
* @param {object} RED The NodeRED instance.
* @param {object} node The current node.
* @param {object} config The node configuration.
*/
login(RED, node, config) {
// Get credentials node
const credentialsNode = RED.nodes.getNode(config.auth);
/**
* Perform a log in.
*
* @param {object} RED The NodeRED instance.
* @param {object} node The current node.
* @param {object} config The node configuration.
*/
login(RED, node, config) {
// Get credentials node
const credentialsNode = RED.nodes.getNode(config.auth);

// Check for valid credential node
if (!credentialsNode) {
throw new Error('No credentials provided!');
}
// Check for valid credential node
if (!credentialsNode) {
throw new Error('No credentials provided!');
}

// Set the node status to 'connecting'
this.setNodeStatusToConnecting(node);

// Set the node status to 'connecting'
this.setNodeStatusToConnecting(node);
// Get global connection
let connection = node.context().global.eWeLinkConnection;

// Get global connection
let connection = node.context().global.eWeLinkConnection;

// If there is no global connection we create a new one and make it singleton
if (!connection) {
connection = new ewelink(credentialsNode.credentials);
node.context().global.eWeLinkConnection = connection;
// If there is no global connection we create a new one and make it singleton
if (!connection) {
connection = new ewelink(credentialsNode.credentials);
node.context().global.eWeLinkConnection = connection;
}

return new Promise((resolve, reject) => {
// Logging in
connection.login().then(response => {
// Check for errors in the response
if (response.error) {
this.setNodeStatusToDisconnected(node);
return reject(response);
}

return new Promise((resolve, reject) => {
// Logging in
connection.login().then(response => {
// Check for errors in the response
if (response.error) {
this.setNodeStatusToDisconnected(node);
return reject(response);
}

// If we are here everything is great
this.setNodeStatusToConnected(node);
resolve(connection);
// If we are here everything is great
this.setNodeStatusToConnected(node);
resolve(connection);

}).catch(error => {
this.setNodeStatusToDisconnected(node);
reject(error);
});
}).catch(error => {
this.setNodeStatusToDisconnected(node);
reject(error);
});
},
});
},

/**
* Set node status to 'connecting'.
*
* @param {object} node The node which status will be changed.
*/
setNodeStatusToConnecting(node) {
node.status({
fill: 'yellow',
shape: 'dot',
text: 'connecting'
});
},
/**
* Set node status to 'connecting'.
*
* @param {object} node The node which status will be changed.
*/
setNodeStatusToConnecting(node) {
node.status({
fill: 'yellow',
shape: 'dot',
text: 'connecting'
});
},

/**
* Set node status to 'connected'.
*
* @param {object} node The node which status will be changed.
*/
setNodeStatusToConnected(node) {
node.status({
fill: 'green',
shape: 'dot',
text: 'connected'
});
},
/**
* Set node status to 'connected'.
*
* @param {object} node The node which status will be changed.
*/
setNodeStatusToConnected(node) {
node.status({
fill: 'green',
shape: 'dot',
text: 'connected'
});
},

/**
* Set node status to 'disconnected'.
*
* @param {object} node The node which status will be changed.
*/
setNodeStatusToDisconnected(node) {
node.status({
fill: 'red',
shape: 'dot',
text: 'disconnected'
});
}
/**
* Set node status to 'disconnected'.
*
* @param {object} node The node which status will be changed.
*/
setNodeStatusToDisconnected(node) {
node.status({
fill: 'red',
shape: 'dot',
text: 'disconnected'
});
}
}