ribbons.application
Version:
Prototype for Ribbons applications.
128 lines (103 loc) • 3.61 kB
JavaScript
var fileSystem = require("fs");
var http = require('http');
var url = require('url');
var path = require('path');
var util = require('util');
var Component = require('ribbons.component');
function Application(options) {
Component.call(this, options);
options = options || {};
this.name = options.name || 'Application';
this.directory = options.directory;
this.debug = options.debug;
this.apiPort = options.apiPort || 1701;
this.apiHost = options.apiHost || '127.0.0.1';
}
util.inherits(Application, Component);
// TODO: DRY up these loops
Application.prototype.init = function () {
requireComponents.call(this);
for (controller in this.controllers) {
this.controllers[controller].sensors = this.sensors;
this.controllers[controller].actuators = this.actuators;
this.controllers[controller].init();
}
for (sensor in this.sensors) {
this.sensors[sensor].platforms = this.platforms;
this.sensors[sensor].debug = this.debug;
this.sensors[sensor].init();
}
for (actuator in this.actuators) {
this.actuators[actuator].platforms = this.platforms;
this.actuators[actuator].debug = this.debug;
this.actuators[actuator].init();
}
}
Application.prototype.start = function() {
var self = this;
for (controller in this.controllers) {
this.controllers[controller].start();
}
for (sensor in this.sensors) {
this.sensors[sensor].start();
}
for (actuator in this.actuators) {
this.actuators[actuator].start();
}
// TODO consider making the API a module.
http.createServer(function (req, res) {
var pathParts = url.parse(req.url).pathname.split('/');
var resource = self[pathParts[1]];
if (pathParts.length === 3) {
resource = resource[pathParts[2]];
}
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(resource));
}).listen(this.apiPort, this.apiHost);
this.info('API running at http://' + this.apiHost + ':' + this.apiPort);
};
Application.prototype.stop = function() {
for (controller in this.controllers) {
this.controllers[controller].stop();
}
for (sensor in this.sensors) {
this.sensors[sensor].stop();
}
for (actuator in this.actuators) {
this.actuators[actuator].stop();
}
};
// user app directory names for each base type
Application.baseTypes = {
'platforms': undefined,
'actuators': undefined,
'sensors': undefined,
'controllers': undefined
};
function requireComponents () {
var app = this;
this.info('requireComponents','start');
// 'sensors', 'actuators', 'controllers'
for (baseTypeName in Application.baseTypes) {
// like: setting up app.actuators
app[baseTypeName] = {};
// like: file path to ./app/actuators/
var baseTypePath = this.directory + path.sep + baseTypeName;
if (fileSystem.existsSync(baseTypePath)) {
fileSystem.readdirSync(baseTypePath).forEach(function requireComponent(componentFileName) {
app.info('requireComponent',baseTypeName, baseTypePath);
// like: var leds = {led12: new Led(12), led13: new Led(13)}
var components = require(baseTypePath + path.sep + componentFileName);
// like: for (led in leds)
for (componentName in components) {
// like: app.actuators.led13 = leds[led13]
app[baseTypeName][componentName] = components[componentName];
components[componentName].name = componentName;
}
});
} else {
this.warn(baseTypeName, 'not found', this.directory + path.sep + baseTypeName);
}
}
}
module.exports = Application;