Skip to content

Commit

Permalink
fix(): changed login strategy. Closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkow committed Dec 13, 2018
1 parent 32af8b8 commit 69a78c2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
15 changes: 12 additions & 3 deletions core/tahomalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var login = function login(username, password) {
return deferred.promise;
};

var getSetup = function getSetup() {
var getSetup = function getSetup(options) {
var deferred = Q.defer();

request({
Expand All @@ -35,6 +35,11 @@ var getSetup = function getSetup() {
}, function(err, res, body) {
if(res.statusCode === 200) {
deferred.resolve(body);
} else if(res.statusCode === 401) {
setTimeout(function() {
deferred.resolve(login(options.username, options.password)
.then(getSetup(options)));
}, 1000);
} else {
deferred.reject();
}
Expand All @@ -43,7 +48,7 @@ var getSetup = function getSetup() {
return deferred.promise;
};

var execute = function execute(row) {
var execute = function execute(row, options) {
var deferred = Q.defer();

request({
Expand All @@ -55,6 +60,11 @@ var execute = function execute(row) {
}, function(err, res, body) {
if(res.statusCode === 200) {
deferred.resolve(body);
} else if(res.statusCode === 401) {
setTimeout(function() {
deferred.resolve(login(options.username, options.password)
.then(execute(row, options)));
}, 1000);
} else {
deferred.reject();
}
Expand Down Expand Up @@ -110,7 +120,6 @@ var continueWhenFinished = function continueWhenFinished(deviceURL, expectedStat
return Q.Promise(function(resolve) {
setTimeout(function() {
getDeviceState(deviceURL).then(function(state) {
console.log(state.position, expectedState.position);
// - Checking on the position seems enough for now.
//var isOpen = state.open === "open";
if(/*isOpen === expectedState.open && */state.position === expectedState.position) {
Expand Down
58 changes: 26 additions & 32 deletions tahoma.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,30 @@ module.exports = function(RED) {

node.status({fill: 'yellow', shape: 'dot', text: statusProgressText});

tahomalink.login(configNode.username, configNode.password)
.then(function() {
tahomalink.execute(row)
.then(function(body) {
if(expectedState === null) {
node.status({fill: 'grey', shape: 'dot', text: 'Unknown'});
node.send(msg);
return;
}
tahomalink.execute(row, configNode)
.then(function(body) {
if(expectedState === null) {
node.status({fill: 'grey', shape: 'dot', text: 'Unknown'});
node.send(msg);
return;
}

tahomalink.continueWhenFinished(node.device, expectedState)
.then(function() {
node.status({
fill: 'green',
shape: 'dot',
text: statusDoneText
});
tahomalink.continueWhenFinished(node.device, expectedState)
.then(function() {
node.status({
fill: 'green',
shape: 'dot',
text: statusDoneText
});

if(!('payload' in msg)) {
msg.payload = {};
}
if(!('payload' in msg)) {
msg.payload = {};
}

// TODO: Find a better way to handle "my" position.
msg.payload.output = expectedState ? expectedState : {open: true};
// TODO: Find a better way to handle "my" position.
msg.payload.output = expectedState ? expectedState : {open: true};

node.send(msg);
});
node.send(msg);
});
});
});
Expand Down Expand Up @@ -144,16 +141,13 @@ module.exports = function(RED) {

RED.httpAdmin.get('/tahomasomfy/getSetup/:boxid', function(req, res, next){
var configNode = RED.nodes.getNode(req.params.boxid);
tahomalink.login(configNode.username, configNode.password)
.then(function() {
tahomalink.getSetup()
.then(function(body) {
if(typeof body === "string") {
body = JSON.parse(body);
}
tahomalink.getSetup(configNode)
.then(function(body) {
if(typeof body === "string") {
body = JSON.parse(body);
}

res.json(body);
});
res.json(body);
});
return;
});
Expand Down

0 comments on commit 69a78c2

Please sign in to comment.