Skip to content

Commit

Permalink
Refactor promises in PositionService
Browse files Browse the repository at this point in the history
  • Loading branch information
lukicdarkoo committed Dec 13, 2016
1 parent a98f3fa commit fde761a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
2 changes: 0 additions & 2 deletions src/services/path/pathfinding/lib/Makefile~

This file was deleted.

43 changes: 19 additions & 24 deletions src/services/position/PositionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,29 @@ class PositionService {
points.splice(0, 1);
positionService._basicSet(point, direction, tolerance, speed).then(goToNext);
return;
}
resolve();
} else {
resolve();
}
}

goToNext();
});
}

_promiseToReachDestionation() {
return new Promise((resolve, reject) => {
this.motionDriver.on('stateChanged', (state) => {
if (state === MotionDriverConstants.STATE_IDLE) {
resolve();
}
else if (state === MotionDriverConstants.STATE_ERROR ||
state === MotionDriverConstants.STATE_STUCK) {
reject(state);
}
});
});
}

_basicSet(point, direction, tolerance, speed) {
// Set speed
if (speed !== undefined && this.currentSpeed !== speed) {
Expand All @@ -127,33 +142,13 @@ class PositionService {
Mep.Log.debug(TAG, 'Robot move command sent.', point);

// Check when robot reached the position
return new Promise((resolve, reject) => {
this.motionDriver.on('stateChanged', (state) => {
if (state === MotionDriverConstants.STATE_IDLE) {
resolve();
}
else if (state === MotionDriverConstants.STATE_ERROR ||
state === MotionDriverConstants.STATE_STUCK) {
reject(state);
}
});
});
return this._promiseToReachDestionation();
}

arc(point, angle, direction) {
this.motionDriver.moveArc(point.getX(), point.getY(), angle, direction);

return new Promise((resolve, reject) => {
this.motionDriver.on('stateChanged', (state) => {
if (state === MotionDriverConstants.STATE_IDLE) {
resolve();
}
else if (state === MotionDriverConstants.STATE_ERROR ||
state === MotionDriverConstants.STATE_STUCK) {
reject(state);
}
});
});
return this._promiseToReachDestionation();
}

rotate(tunedAngle, options) {
Expand Down
3 changes: 0 additions & 3 deletions src/utils/DriverChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ class DriverChecker {
if (typeof driver.getPosition !== 'function') {
throw TypeError(driverClassName + ' requires method getPosition()');
}
if (driver.getPosition().constructor.name !== 'Point') {
throw TypeError('Method '+ driverClassName +'.getPosition() must return Point');
}
}

static _checkTerrain(driver) {
Expand Down

0 comments on commit fde761a

Please sign in to comment.