Skip to content

Commit

Permalink
Merge pull request #10 from gtorodelvalle/feature/simulation-configur…
Browse files Browse the repository at this point in the history
…ation-validation

Validation of the simulation configuration file
  • Loading branch information
gtorodelvalle authored Aug 10, 2016
2 parents 319caae + fad67e3 commit 03987d5
Show file tree
Hide file tree
Showing 10 changed files with 1,632 additions and 41 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ The generated entities and attributes can also be checked in this [CSV file](htt

The FIWARE Device Simulator library can be found in the [./lib](./lib) directory. It is composed of:

1. The main [`fiwareDeviceSimulator.js`](./lib/fiwareDeviceSimulator.js) file. It exposes the following functions:
1. The main [`./lib/fiwareDeviceSimulator.js`](./lib/fiwareDeviceSimulator.js) file. It exposes the following functions:
1. `start()`: it takes a simulation configuration JSON object and returns an instance of `EventEmitter` which informs of the following events to the client:
* `token-request`: Whenever a new authorization token is requested. No event object is passed as additional information for this event occurrence.
* `token-response`: Whenever a new authorization token is received. The passed event includes the following properties:
Expand All @@ -968,10 +968,12 @@ The FIWARE Device Simulator library can be found in the [./lib](./lib) directory
* `request`: The optional associated request (optional)
* `stop`: Whenever the simulation is stopped. No event object is passed as additional information for this event occurrence.
* `end`: Whenever the simulation ends. No event object is passed as additional information for this event occurrence.
2. `stop()`: it stops the currently running simulation, if any, and emits the `stop` event
2. The [`./lib/errors`](./lib/errors) directory including:
2. `stop()`: it stops the currently running simulation, if any, and emits the `stop` event.
2. The [`./lib/validators/fiwareDeviceSimulatorValidator.js`](./lib/validators/fiwareDeviceSimulatorValidator.js) file. It exposes the following functions:
* `validateConfiguration`: Validates a simulation configuration object asynchronously taking the simulation configuration object as input as well as a callback which will be called once the validation has been completed passing an error object with further information about the problem in case the simulation configuration object was not valid.
3. The [`./lib/errors`](./lib/errors) directory including:
1. The [`fdsErrors.js`](./lib/errors/fdsErrors.js) file. It includes the errors which may be sent when running a device simulation.
3. The [`./lib/interpolators`](./lib/interpolators) directory including:
4. The [`./lib/interpolators`](./lib/interpolators) directory including:
1. The [`dateIncrementInterpolator.js`](./lib/interpolators/dateIncrementInterpolator.js) file. It implements the date-increment-interpolator attribute value resolver.
2. The [`linearInterpolator.js`](./lib/interpolators/linearInterpolator.js) file. It implements the time-linear-interpolator attribute value resolver.
3. The [`randomLinearInterpolator.js`](./lib/interpolators/randomLinearInterpolator.js) file. It implements the time-random-linear-interpolator attribute value resolver.
Expand Down
6 changes: 5 additions & 1 deletion bin/fiwareDeviceSimulatorCLI
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ var commander = require('commander');
var logops = require('logops');
var deviceSimulator = require(ROOT_PATH + '/lib/fiwareDeviceSimulator');

process.on('SIGINT', function() {
deviceSimulator.stop();
process.exit(0);
});

/**
* Executes the requested commander
*/
Expand Down Expand Up @@ -79,7 +84,6 @@ function executeCommand() {

progressEmitter.on('stop', function() {
logops.info('stop event');
process.exit(0);
});

progressEmitter.on('end', function() {
Expand Down
2 changes: 1 addition & 1 deletion lib/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"undef": true,
"unused": true,
"trailing": true,
"maxparams": 4,
"maxparams": 6,
"maxdepth": 4,
"camelcase": true,
"maxlen": 120,
Expand Down
12 changes: 12 additions & 0 deletions lib/errors/fdsErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ function NGSIVersionNotSupported(message) {
}
NGSIVersionNotSupported.prototype = Object.create(Error.prototype);

/**
* Not valid simulation configuration error
* @param {String} message Human-readable description of the error
*/
function SimulationConfigurationNotValid(message) {
Error.call(this);
this.name = 'SimulationConfigurationNotValid';
this.message = message;
}
SimulationConfigurationNotValid.prototype = Object.create(Error.prototype);

/**
* Token not available error
* @param {String} message Human-readable description of the error
Expand All @@ -59,5 +70,6 @@ TokenNotAvailable.prototype = Object.create(Error.prototype);
module.exports = {
InvalidInterpolationSpec: InvalidInterpolationSpec,
NGSIVersionNotSupported: NGSIVersionNotSupported,
SimulationConfigurationNotValid: SimulationConfigurationNotValid,
TokenNotAvailable: TokenNotAvailable
};
66 changes: 40 additions & 26 deletions lib/fiwareDeviceSimulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var stepBeforeInterpolator = require(ROOT_PATH + '/lib/interpolators/stepBefore
var stepAfterInterpolator = require(ROOT_PATH + '/lib/interpolators/stepAfterInterpolator');
var dateIncrementInterpolator = require(ROOT_PATH + '/lib/interpolators/dateIncrementInterpolator');
var fdsErrors = require(ROOT_PATH + '/lib/errors/fdsErrors');
var fiwareDeviceSimulatorValidator = require(ROOT_PATH + '/lib/validators/fiwareDeviceSimulatorValidator');

/**
* The loaded configuration
Expand Down Expand Up @@ -126,6 +127,18 @@ function cloneDevice(device) {
return clonedDevice;
}

/**
* Cancels all the pending (token request and update) jobs
*/
function cancelAllJobs() {
for (var job in scheduler.scheduledJobs) {
if (scheduler.scheduledJobs.hasOwnProperty(job)) {
scheduler.scheduledJobs[job].cancel();
}
}
updateJobs = [];
}

/**
* Emits an "scheduled" event notifying that new update jobs have been scheduled
* @param {String} schedule The schedule
Expand Down Expand Up @@ -277,6 +290,7 @@ function emitStop() {
* Emits the "end" event
*/
function emitEnd() {
cancelAllJobs();
eventEmitter.emit('end');
}

Expand All @@ -294,16 +308,6 @@ function checkPendingInvocations() {
emitEnd();
}

/**
* Cancels all the pending update jobs
*/
function cancelAllUpdateJobs() {
updateJobs.forEach(function(updateJob) {
updateJob.cancel();
});
updateJobs = [];
}

/**
* Generates a new device from a device description and a counter
* @param {Object} device The device description
Expand All @@ -327,12 +331,8 @@ function generateDevice(device, counter) {
*/
function addActiveAttribute2Schedule(schedules, schedule, activeAttribute) {
/* jshint camelcase: false */
if (schedules[schedule]) {
schedules[schedule].push(activeAttribute);
} else {
schedules[schedule] = [];
schedules[schedule].push(activeAttribute);
}
schedules[schedule] = schedules[schedule] || [];
schedules[schedule].push(activeAttribute);
}

/**
Expand Down Expand Up @@ -402,11 +402,11 @@ function resolveValue(interpolator) {
return interpolate(interpolator, 'time-linear-interpolator', toDecimalHours(new Date()));
} else if (interpolator.indexOf('time-random-linear-interpolator(') === 0) {
return interpolate(interpolator, 'time-random-linear-interpolator', toDecimalHours(new Date()));
} else if (interpolator.indexOf('time-step-before-interpolator') === 0) {
} else if (interpolator.indexOf('time-step-before-interpolator(') === 0) {
return interpolate(interpolator, 'time-step-before-interpolator', toDecimalHours(new Date()));
} else if (interpolator.indexOf('time-step-after-interpolator') === 0) {
} else if (interpolator.indexOf('time-step-after-interpolator(') === 0) {
return interpolate(interpolator, 'time-step-after-interpolator', toDecimalHours(new Date()));
} else if (interpolator.indexOf('date-increment-interpolator') === 0) {
} else if (interpolator.indexOf('date-increment-interpolator(') === 0) {
return interpolate(interpolator, 'date-increment-interpolator');
} else {
return interpolator;
Expand Down Expand Up @@ -521,7 +521,7 @@ function update(entity, activeAttributes) {
*/
function scheduleJobs4Entity(entity) {
var schedules = {};
if (entity.active) {
if (entity.active && entity.active.length > 0) {
entity.active.forEach(function(activeAttribute) {
addActiveAttribute2Schedule(schedules, activeAttribute.schedule || entity.schedule, activeAttribute);
});
Expand All @@ -530,10 +530,10 @@ function scheduleJobs4Entity(entity) {
}
for (var schedule in schedules) {
if (schedules.hasOwnProperty(schedule)) {
var job = scheduler.scheduleJob(
var updateJob = scheduler.scheduleJob(
schedule === 'once' ? new Date(Date.now() + 500) : schedule,
update.bind(null, entity, schedules[schedule]));
updateJobs.push(job);
updateJobs.push(updateJob);
emitScheduled(schedule, entity, schedules[schedule]);
}
}
Expand Down Expand Up @@ -673,18 +673,32 @@ function onTokenResponse(err, response, body) {
*/
function start(config) {
/* jshint camelcase: false */
cancelAllUpdateJobs();
configuration = config;
setImmediate(requestToken.bind(null, onTokenResponse));
cancelAllJobs();
fiwareDeviceSimulatorValidator.validateConfiguration(config, function(err) {
if (err) {
setImmediate(function notifySimulationConfigurationError() {
emitError(err);
emitEnd();
});
} else {
configuration = config;
if (configuration.authentication) {
setImmediate(requestToken.bind(null, onTokenResponse));
} else {
setImmediate(scheduleJobs);
}
}
});
return eventEmitter;
}

/**
* Stops a currently running simulation, if any, and emits the "stop" event
*/
function stop() {
cancelAllUpdateJobs();
cancelAllJobs();
emitStop();
emitEnd();
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/interpolators/stepAfterInterpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function interpolatorArrayValidator(entry) {
* @return {Boolean} True if the interpolation array is valid, false otherwise
*/
function isValidInterpolationArray(interpolationArray) {
return interpolationArray.every(interpolatorArrayValidator);
return (interpolationArray.length > 0) && interpolationArray.every(interpolatorArrayValidator);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/interpolators/stepBeforeInterpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function interpolatorArrayValidator(entry) {
* @return {Boolean} True if the interpolation array is valid, false otherwise
*/
function isValidInterpolationArray(interpolationArray) {
return interpolationArray.every(interpolatorArrayValidator);
return (interpolationArray.length > 0) && interpolationArray.every(interpolatorArrayValidator);
}

/**
Expand Down
Loading

0 comments on commit 03987d5

Please sign in to comment.