Skip to content

Commit

Permalink
Implement mechanism to validate if driver is implemented correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lukicdarkoo committed Dec 5, 2016
1 parent 6ca3d99 commit a98f3fa
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 75 deletions.
14 changes: 11 additions & 3 deletions src/drivers/DriverManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @namespace drivers */
const DriverChecker = Mep.require('utils/DriverChecker');

const TAG = 'DriverManager';

Expand Down Expand Up @@ -62,7 +63,7 @@ class DriverManager {
let load = moduleConfig.load;
let classPath = moduleConfig.class;

// Do not initialize if `init field == false`
// Do not initialize if `load field == false`
if (load != false) {
let ModuleClass = Mep.require(classPath);

Expand All @@ -72,14 +73,21 @@ class DriverManager {
}

if (typeof ModuleClass === 'function') {
let driverInstance;
let loadedWithoutErrors = true;
try {
let driverInstance = new ModuleClass(driverIdentifier, moduleConfig);
driverInstance = new ModuleClass(driverIdentifier, moduleConfig);
this.drivers[driverIdentifier] = driverInstance;
Mep.Log.debug(TAG, 'Driver `' + driverIdentifier + '` loaded');

} catch (error) {
loadedWithoutErrors = false;
this.putDriverOutOfOrder(driverIdentifier, error);
}

// Test if all methods are OK
if (loadedWithoutErrors === true) {
DriverChecker.check(driverInstance);
}
}

else {
Expand Down
8 changes: 6 additions & 2 deletions src/drivers/laser/LaserDriver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @namespace drivers.laser */

const TerrainDriver = Mep.require('types/TerrainDriver');
const EventEmitter = require('events');
const Point = Mep.require('types/Point');
const Polygon = Mep.require('types/Polygon');

Expand All @@ -13,7 +13,7 @@ const TAG = 'LaserDriver';
* @author Darko Lukic <lukicdarkoo@gmail.com>
* @fires LaserDriver#terrain
*/
class LaserDriver extends TerrainDriver {
class LaserDriver extends EventEmitter {
/**
* Make instance of LaserDriver.
*
Expand Down Expand Up @@ -119,6 +119,10 @@ class LaserDriver extends TerrainDriver {
static dependencies() {
return ['ModbusDriver'];
}

getGroups() {
return ['terrain'];
}
}

module.exports = LaserDriver;
8 changes: 6 additions & 2 deletions src/drivers/modbus/ModbusDriverSimulator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const BaseDriver = Mep.require('types/BaseDriver');
const EventEmitter = require('events');

const TAG = 'ModbusDriverSimulator';

class ModbusDriverSimulator extends BaseDriver {
class ModbusDriverSimulator extends EventEmitter {
constructor(name, config) {
super();

Expand All @@ -12,6 +12,10 @@ class ModbusDriverSimulator extends BaseDriver {
registerCoilReading(slaveAddress, functionAddress) {

}

getGroups() {
return [];
}
}

module.exports = ModbusDriverSimulator;
2 changes: 0 additions & 2 deletions src/drivers/motion/MotionDriver.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const MotionDriverBinder = require('bindings')('motion').MotionDriverBinder;
const Point = Mep.require('types/Point');
const PositionDriver = Mep.require('types/PositionDriver');
const Constants = require('./Constants');
const Util = require('util');
const EventEmitter = require('events');
//const mixin = require('es6-mixin').mixin;

Util.inherits(MotionDriverBinder, EventEmitter);

Expand Down
8 changes: 6 additions & 2 deletions src/drivers/motion/MotionDriverSimulator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const WebSocketClient = Mep.require('utils/WebSocketClient');
const Point = Mep.require('types/Point');
const PositionDriver = Mep.require('types/PositionDriver');
const EventEmitter = require('events');

const TAG = 'MotionDriverSimulator';

Expand Down Expand Up @@ -32,7 +32,7 @@ const TAG = 'MotionDriverSimulator';
* }
* </pre>
*/
class MotionDriverSimulator extends PositionDriver {
class MotionDriverSimulator extends EventEmitter {
constructor(name, config) {
super();

Expand Down Expand Up @@ -117,6 +117,10 @@ class MotionDriverSimulator extends PositionDriver {
params: params
}));
}

getGroups() {
return ['position'];
}
}

module.exports = MotionDriverSimulator;
15 changes: 0 additions & 15 deletions src/types/BaseDriver.js

This file was deleted.

15 changes: 0 additions & 15 deletions src/types/PositionDriver.js

This file was deleted.

11 changes: 0 additions & 11 deletions src/types/TerrainDriver.js

This file was deleted.

59 changes: 59 additions & 0 deletions src/types/TunedAngle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/** @namespace types */

const TAG = 'TunedAngle';

/**
* Tunable angle. Angle is chosen depends on table name in configuration.
*
* @author Darko Lukic <lukicdarkoo@gmail.com>
* @memberof types
* @example
* new TunePoint(
* 150, 129,
* [151, 129, 'table_1'],
* [148, 128, 'table_2']
* );
*/
class TunedAngle {
/**
* Add multiple Points, add Points for each table. It must has
* at least one Point which will be used as default. Other Points
* must have tag!
*
* @param defaultX {integer} - Default point X coordinate
*/
constructor(defaultAngle) {
// If there are table dependent points
for (let i = 1; i < arguments.length; i++) {

// Check if the argument is valid
if (typeof arguments[i][0] === 'undefined' ||
typeof arguments[i][1] === 'undefined') {

Mep.Log.warn(TAG, 'Invalid arguments');
continue;
}

// Check if table name matches
if (Mep.Config.get('table') == arguments[i][1]) {
this.point = arguments[i][0];
}
}

// Otherwise use default point
if (typeof this.point === 'undefined') {
this.angle = defaultAngle;
}
}

/**
* Get angle depending on the chosen table in configuration.
*
* @returns {number} - Point
*/
getAngle() {
return this.angle;
}
}

module.exports = TunedAngle;
47 changes: 47 additions & 0 deletions src/utils/DriverChecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* A goal of the class is to check if drivers are correctly implemented.
*
* @author Darko Lukic <lukicdarkoo@gmail.com>
* @memberOf utils
*/

class DriverChecker {
static check(driver) {
let groups;
if (typeof driver.getGroups === 'function') {
groups = driver.getGroups();
} else {
throw TypeError(driver.constructor.name + ' doesn\'t have member getGroups()');
}

for (let group of groups) {
switch (group) {
case 'position':
DriverChecker._checkPosition(driver);
break;

case 'terrain':
DriverChecker._checkTerrain(driver);
break;
}
}
}

static _checkPosition(driver) {
let driverClassName = driver.constructor.name;

// Check getPosition()
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) {

}
}

module.exports = DriverChecker;
25 changes: 2 additions & 23 deletions strategies/default/InitTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,9 @@ class InitTask extends Task {
// Wait WebSocketClient to connect to WebSocketServer
await Delay(200);

await position.set(new TunedPoint(1100, 0), { speed: 130 });
await position.set(new TunedPoint(-1300, 0));


if (false) {
await position.set(new TunedPoint(-1100, 0), { speed: 40 });
await position.arc(new Point(-1300, 100), 30, 1);
await position.arc(new Point(-1300, 100), 60, 1);
}

if (false) {
await position.set(new TunedPoint(-1200, 0), { speed: 40 });
await position.set(new TunedPoint(-735, -850), { pathfinding: true });
await position.set(new TunedPoint(765, -850), { pathfinding: true });
await position.set(new TunedPoint(-1200, 0), { pathfinding: true });
}

//await position.set(new TunedPoint(-1300, 0));

// Let's move around
//await position.set(new TunedPoint(-735, -850), { pathfinding: true });
//await position.set(new TunedPoint(765, -850), { pathfinding: true });
//await position.set(new TunedPoint(-1200, 400), { pathfinding: true });
//await position.set(new TunedPoint(-170, 820), { pathfinding: true });
await position.set(new TunedPoint(1100, 0), {speed: 130});
await position.set(new TunedPoint(-1300, 0));
}

}
Expand Down

0 comments on commit a98f3fa

Please sign in to comment.