Skip to content

Commit

Permalink
Add timeout when adding things.
Browse files Browse the repository at this point in the history
Fixes #37
  • Loading branch information
Mike Stegeman authored and hobinjk committed Feb 6, 2018
1 parent 0443190 commit 241ffd6
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 17 deletions.
1 change: 0 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ module.exports = {
// port-free urls
behindForwarding: true,
addonManager: {
pairingTimeout: 3 * 60, // seconds
path: './addons',
api: 1,
listUrl: 'https://raw.githubusercontent.com/mozilla-iot/addon-list/master/list.json',
Expand Down
3 changes: 1 addition & 2 deletions src/addon-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class AddonManager extends EventEmitter {
* The user then presses the "button" on the device to be added.
* @returns A promise that resolves to the newly added device.
*/
addNewThing() {
addNewThing(pairingTimeout) {
var deferredAdd = new Deferred();

if (this.deferredAdd) {
Expand All @@ -94,7 +94,6 @@ class AddonManager extends EventEmitter {
deferredAdd.reject('Remove already in progress');
} else {
this.deferredAdd = deferredAdd;
var pairingTimeout = config.get('addonManager.pairingTimeout');
this.adapters.forEach(adapter => {
console.log('About to call startPairing on', adapter.name);
adapter.startPairing(pairingTimeout);
Expand Down
2 changes: 1 addition & 1 deletion src/models/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Actions extends EventEmitter {

switch(action.name) {
case 'pair':
AddonManager.addNewThing().then(function() {
AddonManager.addNewThing(action.parameters.timeout).then(function() {
action.updateStatus('completed');
}).catch(function(error) {
action.error = error;
Expand Down
10 changes: 8 additions & 2 deletions src/test/integration/actions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ describe('actions/', function() {

it('should list and retrieve the new action', async () => {
let descr = {
name: 'pair'
name: 'pair',
parameters: {
timeout: 60,
},
};

const pair = await chai.request(server)
Expand Down Expand Up @@ -98,7 +101,10 @@ describe('actions/', function() {

it('should remove an action', async () => {
let descr = {
name: 'pair'
name: 'pair',
parameters: {
timeout: 60,
},
};

await chai.request(server)
Expand Down
25 changes: 17 additions & 8 deletions src/test/integration/things-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe('things/', function() {
.post(Constants.ACTIONS_PATH)
.set('Accept', 'application/json')
.set(...headerAuth(jwt))
.send({name: 'pair'});
.send({name: 'pair', parameters: {timeout: 60}});

await mockAdapter().addDevice('test-4', makeDescr('test-4'));
await mockAdapter().addDevice('test-5', makeDescr('test-5'));
Expand All @@ -311,7 +311,7 @@ describe('things/', function() {
.post(Constants.ACTIONS_PATH)
.set(...headerAuth(jwt))
.set('Accept', 'application/json')
.send({name: 'pair'});
.send({name: 'pair', parameters: {timeout: 60}});
expect(res.status).toEqual(201);

res = await chai.request(server)
Expand Down Expand Up @@ -374,7 +374,7 @@ describe('things/', function() {
.post(Constants.ACTIONS_PATH)
.set(...headerAuth(jwt))
.set('Accept', 'application/json')
.send({name: 'pair'});
.send({name: 'pair', parameters: {timeout: 60}});
expect(pair.status).toEqual(201);

let res = await chai.request(server)
Expand Down Expand Up @@ -424,7 +424,7 @@ describe('things/', function() {
.post(Constants.ACTIONS_PATH)
.set('Accept', 'application/json')
.set(...headerAuth(jwt))
.send({name: 'pair'});
.send({name: 'pair', parameters: {timeout: 60}});
expect(pair.status).toEqual(201);
await mockAdapter().removeDevice(thingId);

Expand Down Expand Up @@ -689,7 +689,7 @@ describe('things/', function() {
.post(Constants.ACTIONS_PATH)
.set('Accept', 'application/json')
.set(...headerAuth(jwt))
.send({name: 'pair'});
.send({name: 'pair', parameters: {timeout: 60}});

let res = await chai.request(server)
.get(Constants.ACTIONS_PATH)
Expand Down Expand Up @@ -794,7 +794,10 @@ describe('things/', function() {
const thingBase = Constants.THINGS_PATH + '/nonexistent-thing';

const actionDescr = {
name: 'pair'
name: 'pair',
parameters: {
timeout: 60,
},
};

let err = await pFinal(chai.request(server)
Expand All @@ -817,7 +820,10 @@ describe('things/', function() {
expect(res.status).toEqual(200);

const actionDescr = {
name: 'pair'
name: 'pair',
parameters: {
timeout: 60,
},
};

let err = await pFinal(chai.request(server)
Expand Down Expand Up @@ -871,7 +877,10 @@ describe('things/', function() {
webSocketSend(ws, {
messageType: Constants.REQUEST_ACTION,
data: {
name: 'pair'
name: 'pair',
parameters: {
timeout: 60,
},
}
}),
webSocketRead(ws, 2)
Expand Down
3 changes: 3 additions & 0 deletions src/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ <h2 class="delete-prompt">Drop devices here to disconnect</h2>
<p id="add-thing-status">
<img class="spinner" alt="Scanning" src="/images/loading.gif" />Scanning for new devices...
</p>
<p id="add-adapters-hint" class="hidden">
No new things found. Try <a id="add-adapters-hint-anchor" href="/settings/addons">adding some add-ons</a>.
</p>
<div id="new-things"></div>
<button id="add-thing-cancel-button" class="text-button">Done</button>
</div>
Expand Down
12 changes: 12 additions & 0 deletions static/css/add-thing.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
max-height: 100%;
}

#add-adapters-hint.hidden,
#add-thing-status.hidden {
display: none;
}

#add-adapters-hint-anchor:link,
#add-adapters-hint-anchor:visited,
#add-adapters-hint-anchor:hover,
#add-adapters-hint-anchor:active {
color: #fff;
}

.new-thing-save-button {
height: 4rem;
background-color: #7f93a1;
Expand Down
38 changes: 35 additions & 3 deletions static/js/add-thing.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,25 @@ var AddThingScreen = {
this.backButton = document.getElementById('add-thing-back-button');
this.cancelButton = document.getElementById('add-thing-cancel-button');
this.newThingsElement = document.getElementById('new-things');
this.scanStatus = document.getElementById('add-thing-status');
this.addonsHint = document.getElementById('add-adapters-hint');
this.addonsHintAnchor =
document.getElementById('add-adapters-hint-anchor');
this.pairingTimeout = null;
this.visibleThings = new Set();
// Add event listeners
this.backButton.addEventListener('click', this.hide.bind(this));
this.cancelButton.addEventListener('click', this.hide.bind(this));
this.addonsHintAnchor.addEventListener('click', this.hide.bind(this));
},

/**
* Create a new "pair" action on the gateway.
*/
requestPairing: function() {
this.scanStatus.classList.remove('hidden');
this.addonsHint.classList.add('hidden');

let proto = 'ws:';
if (window.location.protocol === 'https:') {
proto = 'wss:';
Expand All @@ -48,8 +57,14 @@ var AddThingScreen = {
this.showNewThing(JSON.parse(event.data));
}).bind(this);

// Timeout, in seconds.
const timeout = 60;

var action = {
'name': 'pair'
'name': 'pair',
'parameters': {
'timeout': timeout,
},
};
fetch('/actions', {
method: 'POST',
Expand All @@ -60,10 +75,22 @@ var AddThingScreen = {
'Content-Type': 'application/json'
}
})
.then(function(response) {
.then((response) => {
return response.json();
}).then(function(json) {
}).then((json) => {
AddThingScreen.actionUrl = json.href;

this.pairingTimeout = setTimeout(() => {
this.scanStatus.classList.add('hidden');

if (this.visibleThings.size === 0) {
this.addonsHint.classList.remove('hidden');
}

this.pairingTimeout = null;
this.requestCancelPairing();
}, timeout * 1000);

console.log('Pairing request created with URL ' + json.href);
})
.catch(function(error) {
Expand All @@ -75,6 +102,11 @@ var AddThingScreen = {
* Cancel a pairing request.
*/
requestCancelPairing: function() {
if (this.pairingTimeout !== null) {
clearTimeout(this.pairingTimeout);
this.pairingTimeout = null;
}

// Close websocket.
if (typeof this.socket !== 'undefined') {
this.socket.close();
Expand Down

0 comments on commit 241ffd6

Please sign in to comment.