Skip to content

Commit

Permalink
Prevent from creating too many xhr
Browse files Browse the repository at this point in the history
Relates to #7
  • Loading branch information
dashdashzako committed May 23, 2016
1 parent a1ad53b commit b6512df
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
29 changes: 12 additions & 17 deletions vendor/app/octopus.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,19 @@ define([ 'ergojr', 'pypot', 'gui' ], function(ERGOJR, PYPOT, gui) {
});

octopus.update = function() {
if (gui.guiData.remoteStatus) {
PYPOT.startPoll();
octopus.ergo.S1.rotation.z = PYPOT.motors.m1 * PI / 180;
octopus.ergo.S2.rotation.z = PYPOT.motors.m2 * PI / 180;
octopus.ergo.S3.rotation.z = PYPOT.motors.m3 * PI / 180;
octopus.ergo.S4.rotation.z = -gui.guiData.m4 * PI / 180 + PI / 2;
octopus.ergo.S5.rotation.z = PYPOT.motors.m5 * PI / 180;
octopus.ergo.S6.rotation.z = PYPOT.motors.m6 * PI / 180;
} else {
PYPOT.stopPoll();
if (octopus.ergo !== undefined) {
octopus.ergo.S1.rotation.z = gui.guiData.m1 * PI / 180;
octopus.ergo.S2.rotation.z = gui.guiData.m2 * PI / 180;
octopus.ergo.S3.rotation.z = gui.guiData.m3 * PI / 180;
octopus.ergo.S4.rotation.z = -gui.guiData.m4 * PI / 180 + PI / 2;
octopus.ergo.S5.rotation.z = gui.guiData.m5 * PI / 180;
octopus.ergo.S6.rotation.z = gui.guiData.m6 * PI / 180;
if (octopus.ergo !== undefined) {
if (gui.guiData.remoteStatus) {
PYPOT.startPoll();
} else {
PYPOT.stopPoll();
}

octopus.ergo.S1.rotation.z = gui.guiData.m1 * PI / 180;
octopus.ergo.S2.rotation.z = gui.guiData.m2 * PI / 180;
octopus.ergo.S3.rotation.z = gui.guiData.m3 * PI / 180;
octopus.ergo.S4.rotation.z = -gui.guiData.m4 * PI / 180 + PI / 2;
octopus.ergo.S5.rotation.z = gui.guiData.m5 * PI / 180;
octopus.ergo.S6.rotation.z = gui.guiData.m6 * PI / 180;
}
}

Expand Down
23 changes: 14 additions & 9 deletions vendor/ergo/pypot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ define([ 'gui' ], function(gui) {
PORT: '8080',
FREQ: 20,
motors: {},
pollRequest: undefined,
pollRequest: null,
motorIds: defaultMotors,
poller: undefined,
motors: (function(_motors) {
Expand All @@ -31,34 +31,39 @@ define([ 'gui' ], function(gui) {
if (PYPOT.poller) {
clearInterval(PYPOT.poller);
PYPOT.poller = undefined;
PYPOT.pollRequest = null;
}
}

PYPOT.pollPos = function() {
var req, pollUrl;

if (PYPOT.pollRequest !== null) {
return;
}

pollUrl = 'http://' + PYPOT.HOST + ':' + PYPOT.PORT + '/motors/register/present_position';
PYPOT.pollRequest = new XMLHttpRequest();
req = PYPOT.pollRequest;

req.onreadystatechange = function() {
PYPOT.pollRequest.onreadystatechange = function() {
var res;

if (req.readyState === 4) {
if (req.status === 200) {
res = JSON.parse(req.responseText);
if (PYPOT.pollRequest.readyState === 4) {
if (PYPOT.pollRequest.status === 200) {
res = JSON.parse(PYPOT.pollRequest.responseText);
for (var motorId in res) {
PYPOT.motors[motorId] = res[motorId].present_position;
gui.guiData[motorId] = res[motorId].present_position;
}
PYPOT.pollRequest = null;
} else {
console.log('Something wrong while reading REST API (code: ' + req.status + ')');
console.log('Something wrong while reading REST API (code: ' + PYPOT.pollRequest.status + ')');
}
}
}

req.open('GET', pollUrl);
req.send();
PYPOT.pollRequest.open('GET', pollUrl);
PYPOT.pollRequest.send();
}

window.PYPOT = PYPOT;
Expand Down

0 comments on commit b6512df

Please sign in to comment.