Skip to content

Commit

Permalink
Configuration validation added
Browse files Browse the repository at this point in the history
  • Loading branch information
gvdhoven committed Nov 9, 2020
1 parent 1d899ee commit fc9dcc2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 44 deletions.
66 changes: 35 additions & 31 deletions lib/LocalApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class LocalApi {
* @param {double} closePosition Position in which the Slide is when closed.
*/
constructor(hostname, devicecode, openPosition=0.0, closedPosition=1.0) {
this.hostname = hostname;
this.devicecode = devicecode;
this.hostname = ((hostname !== '') ? hostname : '').trim();
this.devicecode = ((devicecode !== '') ? devicecode : '').trim();

openPosition = this.parseFloat(openPosition, 0.0);
if (openPosition < 0) {
Expand Down Expand Up @@ -60,37 +60,41 @@ class LocalApi {
*/
request(path, body='') {
return new Promise((resolve, reject) => {
const url = ((this.hostname.indexOf('://') === -1) ? 'http://' + this.hostname : this.hostname) + path;
const options = {
'method': 'POST',
'headers': {
'Content-Type': 'application/json'
},
'body': ((body !== '') ? JSON.stringify(body) : '')
};
const client = new DigestFetch('user', this.devicecode, { algorithm: 'MD5' });
client.fetch(url, options)
.then(res => {
if (res.ok) {
return res.json();
} else {
if (res.statusText === 'Unauthorized') {
reject({ 'code': 401, 'title': 'Invalid device code', 'message': 'The Slide at hostname "' + this.hostname +'" rejected device code "' + this.devicecode + '".' });
if ((this.hostname === '') || (this.devicecode === '')) {
reject({ 'code': 400, 'title': 'Incomplete configuration', 'message': 'Please enter both a hostname and a devicecode in order to control the Slide.' });
} else {
const url = ((this.hostname.indexOf('://') === -1) ? 'http://' + this.hostname : this.hostname) + path;
const options = {
'method': 'POST',
'headers': {
'Content-Type': 'application/json'
},
'body': ((body !== '') ? JSON.stringify(body) : '')
};
const client = new DigestFetch('user', this.devicecode, { algorithm: 'MD5' });
client.fetch(url, options)
.then(res => {
if (res.ok) {
return res.json();
} else {
reject({ 'code': 400, 'title': res.statusText, 'message': 'The Slide at hostname "' + this.hostname +'" with device code "' + this.devicecode + '" gave an unclear response.' });
if (res.statusText === 'Unauthorized') {
reject({ 'code': 401, 'title': 'Invalid device code', 'message': 'The Slide at hostname "' + this.hostname +'" rejected device code "' + this.devicecode + '".' });
} else {
reject({ 'code': 400, 'title': res.statusText, 'message': 'The Slide at hostname "' + this.hostname +'" with device code "' + this.devicecode + '" gave an unclear response.' });
}
}
}
})
.then(json => {
resolve(json);
})
.catch((e) => {
if (e.errno && e.errno === 'ECONNREFUSED') {
reject({ 'code': 404, 'title': 'Unable to connect', 'message': 'The Slide at hostname "' + this.hostname +'" with device code "' + this.devicecode + '" is unresponsive.' });
} else {
reject({ 'code': 500, 'title': 'Unkown error occured', 'message': e });
}
});
})
.then(json => {
resolve(json);
})
.catch((e) => {
if (e.errno && e.errno === 'ECONNREFUSED') {
reject({ 'code': 404, 'title': 'Unable to connect', 'message': 'The Slide at hostname "' + this.hostname +'" with device code "' + this.devicecode + '" is unresponsive.' });
} else {
reject({ 'code': 500, 'title': 'Unkown error occured', 'message': e });
}
});
}
});
}

Expand Down
51 changes: 38 additions & 13 deletions slide.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@
*
* @returns {Object} JSON object representing the currently entered configuration.
*/
function getSlideConfig() {
function validateAndGetSlideConfig() {
var slideConfig = {
'hostname': $('#node-config-input-hostname').val(),
'devicecode': $('#node-config-input-devicecode').val(),
'hostname': $.trim($('#node-config-input-hostname').val()),
'devicecode': $.trim($('#node-config-input-devicecode').val()),
'openPosition': $('node-config-input-openPosition').val(),
'closePosition': $('node-config-input-closePosition').val()
};

if ((slideConfig.hostname === '') || (slideConfig.devicecode === '')) {
RED.notify('Please enter the hostname and devicecode of your Slide in order to proceed.');
return false;
}

return slideConfig;
}

Expand Down Expand Up @@ -154,6 +160,11 @@
},
oneditprepare: function() {
$('#slide-calibrate').click(function() {
var slideConfig = validateAndGetSlideConfig();
if (slideConfig === false) {
return;
}

// Timeout function
var statusMessage;
var calibrationStopped = false;
Expand All @@ -168,7 +179,6 @@
var currPosition = -1;
var openPosition = -1;
var closePosition = -1;
var slideConfig = getSlideConfig();

// Get current curtain state (so the curtain moves back to this position)
statusMessage = RED.notify('Connecting to Slide (' + slideConfig.hostname + ') ...', { modal: true, fixed: true });
Expand Down Expand Up @@ -209,50 +219,68 @@
});

$('#slide-open').click(function() {
var slideConfig = validateAndGetSlideConfig();
if (slideConfig === false) {
return;
}

var statusMessage = RED.notify('Opening your Slide', { modal: true, fixed: true });
$.ajax({
'type': 'POST',
'url': '/slide/open',
'contentType': 'application/json',
'data': JSON.stringify(getSlideConfig())
'data': JSON.stringify(slideConfig)
}).done(function(data, textStatus, jqXHR) {
statusMessage.close();
if (jqXHR.status !== 200) {
RED.notify('Communication with your Slide resulted in an an error - more details in debug tab.');
RED.warn(data);
}
}).always(function() {
statusMessage.close();
});
});

$('#slide-stop').click(function() {
var slideConfig = validateAndGetSlideConfig();
if (slideConfig === false) {
return;
}

var statusMessage = RED.notify('Stopping your Slide', { modal: true, fixed: true });
$.ajax({
'type': 'POST',
'url': '/slide/stop',
'contentType': 'application/json',
'data': JSON.stringify(getSlideConfig())
'data': JSON.stringify(slideConfig)
}).done(function(data, textStatus, jqXHR) {
statusMessage.close();
if (jqXHR.status !== 200) {
RED.notify('Communication with your Slide resulted in an an error - more details in debug tab.');
RED.warn(data);
}
}).always(function() {
statusMessage.close();
});
});

$('#slide-close').click(function() {
var slideConfig = validateAndGetSlideConfig();
if (slideConfig === false) {
return;
}

var statusMessage = RED.notify('Closing your Slide', { modal: true, fixed: true });
$.ajax({
'type': 'POST',
'url': '/slide/close',
'contentType': 'application/json',
'data': JSON.stringify(getSlideConfig())
'data': JSON.stringify(slideConfig)
}).done(function(data, textStatus, jqXHR) {
statusMessage.close();
if (jqXHR.status !== 200) {
RED.notify('Communication with your Slide resulted in an an error - more details in debug tab.');
RED.warn(data);
}
}).always(function() {
statusMessage.close();
});
});
}
Expand Down Expand Up @@ -295,6 +323,3 @@
}
});
</script>



0 comments on commit fc9dcc2

Please sign in to comment.