2015-06-11 08:17:11 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// validates an Object containing Robot parameters
|
|
|
|
|
2015-06-18 00:03:12 +08:00
|
|
|
var Logger = require("./logger"),
|
|
|
|
_ = require("./utils/helpers");
|
2015-06-11 08:17:11 +08:00
|
|
|
|
|
|
|
function hasProp(object, prop) {
|
|
|
|
return object.hasOwnProperty(prop);
|
|
|
|
}
|
|
|
|
|
|
|
|
function die() {
|
|
|
|
var RobotDSLError = new Error("Unable to start robot due to a syntax error");
|
|
|
|
RobotDSLError.name = "RobotDSLError";
|
|
|
|
throw RobotDSLError;
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:03:12 +08:00
|
|
|
function warn(messages) {
|
|
|
|
messages = [].concat(messages);
|
2015-07-14 04:55:42 +08:00
|
|
|
messages.map(function(msg) { Logger.log(msg); });
|
2015-06-18 00:03:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function fatal(messages) {
|
2015-06-11 08:17:11 +08:00
|
|
|
messages = [].concat(messages);
|
2015-07-14 04:55:42 +08:00
|
|
|
messages.map(function(msg) { Logger.log(msg); });
|
2015-06-18 00:03:12 +08:00
|
|
|
die();
|
2015-06-11 08:17:11 +08:00
|
|
|
}
|
|
|
|
|
2015-06-18 00:03:12 +08:00
|
|
|
var checks = {};
|
2015-06-11 08:17:11 +08:00
|
|
|
|
2015-06-18 00:03:12 +08:00
|
|
|
checks.singleObjectSyntax = function(opts, key) {
|
2015-06-11 08:17:11 +08:00
|
|
|
var single = hasProp(opts, key),
|
|
|
|
plural = hasProp(opts, key + "s");
|
|
|
|
|
|
|
|
if (single && !plural) {
|
2015-06-18 00:03:12 +08:00
|
|
|
fatal([
|
2015-06-11 08:17:11 +08:00
|
|
|
"The single-object '" + key + "' syntax for robots is not valid.",
|
|
|
|
"Instead, use the multiple-value '" + key + "s' key syntax.",
|
|
|
|
"Details: http://cylonjs.com/documentation/guides/working-with-robots/"
|
|
|
|
]);
|
2015-06-18 00:03:12 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
checks.singleObjectSyntax = function(opts) {
|
|
|
|
["connection", "device"].map(function(key) {
|
|
|
|
var single = hasProp(opts, key),
|
|
|
|
plural = hasProp(opts, key + "s");
|
|
|
|
|
|
|
|
if (single && !plural) {
|
|
|
|
fatal([
|
|
|
|
"The single-object '" + key + "' syntax for robots is not valid.",
|
|
|
|
"Instead, use the multiple-value '" + key + "s' key syntax.",
|
|
|
|
"Details: http://cylonjs.com/documentation/guides/working-with-robots/"
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
checks.deviceWithoutDriver = function(opts) {
|
|
|
|
if (opts.devices) {
|
|
|
|
_.each(opts.devices, function(device, name) {
|
|
|
|
if (!device.driver || device.driver === "") {
|
|
|
|
fatal("No driver supplied for device " + name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
checks.devicesWithoutConnection = function(opts) {
|
|
|
|
var connections = opts.connections,
|
|
|
|
devices = opts.devices;
|
|
|
|
|
|
|
|
if (devices && connections && Object.keys(connections).length > 1) {
|
|
|
|
var first = Object.keys(connections)[0];
|
2015-06-11 08:17:11 +08:00
|
|
|
|
2015-06-18 00:03:12 +08:00
|
|
|
_.each(devices, function(device, name) {
|
|
|
|
if (!device.connection || device.connection === "") {
|
|
|
|
warn([
|
|
|
|
"No explicit connection provided for device " + name,
|
|
|
|
"Will default to using connection " + first
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
});
|
2015-06-11 08:17:11 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-27 01:31:23 +08:00
|
|
|
checks.noConnections = function(opts) {
|
|
|
|
var connections = Object.keys(opts.connections || {}).length,
|
|
|
|
devices = Object.keys(opts.devices || {}).length;
|
|
|
|
|
|
|
|
if (devices && !connections) {
|
|
|
|
fatal(["No connections provided for devices"]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-11 08:17:11 +08:00
|
|
|
module.exports.validate = function validate(opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
|
2015-06-18 00:03:12 +08:00
|
|
|
_.each(checks, function(check) {
|
|
|
|
check(opts);
|
2015-06-11 08:17:11 +08:00
|
|
|
});
|
|
|
|
};
|