Permalink
Browse files

Almost working implementation

  • Loading branch information...
1 parent 107ddef commit fc4dec932d3831905191795db62d177ea851b37c Mark Cahill committed Sep 7, 2011
Showing with 56 additions and 24 deletions.
  1. +15 −11 listeners/couchdb.js
  2. +11 −10 server.js
  3. +30 −3 telemetry.js
View
@@ -1,18 +1,22 @@
var Backbone = require('backbone');
+var _ = require('underscore');
-var Event = exports.event = Backbone.Model.extend({
+exports = module.exports = Backbone.Model.extend({
+ initialize: function(args, options) {
+ console.log("CouchDB loaded for", options.input.name);
+ _.bindAll(this, "post");
+ options.input.bind('event:new', this.post);
+ },
+
+ post: function(data) {
+ console.log("DATA:", data);
+ this.set(data);
+ this.save();
+ },
+
url: function() {
- return 'http://' + this.get('username') + ':' + this.get('password') +
+ return 'https://' + this.get('username') + ':' + this.get('password') +
'@' + this.get('host') + ':' + this.get('port') +
'/' + this.get('database');
}
-});
-
-exports.events = Backbone.Collection.extend({
- model: Event,
- url: function() {
- return 'http://' + this.get('username') + ':' + this.get('password') +
- '@' + this.get('host') + ':' + this.get('port') +
- '/' + this.get('database') + '/_all_docs';
- }
});
View
@@ -3,24 +3,25 @@ var express = require('express');
var Backbone = require('backbone');
var yaml = require('yaml');
var fs = require('fs');
-var headers = {
- accept:'application/json',
- 'content-type':'application/json'
-};
+global.$ = require('jquery');
// Load listeners
-var listeners = {};
var listener_files = fs.readdirSync(__dirname + "/listeners");
for (var i = 0; i < listener_files.length; i++) {
- var listener = listener_files[i].replace('.js', '');
- window[listener] = require(listener);
+ var listener = listener_files[i];
+ global[listener.replace('.js', '')] =
+ require(__dirname + "/listeners/" + listener);
}
-//Load configuration and inputs
-var telemetry = new require(__dirname + '/telemetry')(config);
+// Load configuration and inputs
+var config = yaml.eval(fs.readFileSync(__dirname + "/config.yaml", 'utf8')); //FIXME - this is just for testing. the real file should be in home dir
+var Telemetry_object = require(__dirname + '/telemetry');
+var telemetry = new Telemetry_object({}, config);
// Create the telemetry server and assign inputs
var app = express.createServer();
+app.use(express.bodyParser());
app.post('/input/:input', telemetry.new_event);
-// Start the telemetry server
+// Start the telemetry server
+app.listen(process.argv[2] || 7000);
View
@@ -1,4 +1,5 @@
var Backbone = require('backbone');
+var _ = require('underscore');
exports = module.exports = Backbone.Model.extend({
/**
@@ -10,8 +11,32 @@ exports = module.exports = Backbone.Model.extend({
* Read in configuration and set up inputs
* @param config
*/
- initialize: function(config) {
+ initialize: function(args, config) {
// Load inputs
+ for (var i in config) {
+ if (config.hasOwnProperty(i)) {
+ this.inputs[i] = _.extend({
+ name: i
+ }, Backbone.Events);
+ this.inputs[i].listeners = [];
+
+ // Initialize listeners for this input
+ for (var j = 0; j < config[i].length; j++) {
+ try {
+ var listener = new (global[config[i][j].type])(config[i][j],
+ {
+ input: this.inputs[i]
+ });
+ this.inputs[i].listeners.push(listener);
+ } catch (e) {
+ console.error("Could not load", config[i][j].type, e.message);
+ }
+ }
+ }
+ }
+
+ // Ensure that inputs are available in routes
+ _.bindAll(this, "new_event");
},
/**
@@ -22,13 +47,15 @@ exports = module.exports = Backbone.Model.extend({
*/
new_event: function(req, res, next) {
var input = req.params.input;
- if (! input in this.inputs) {
+ if (this.inputs[input] === undefined) {
// Bad input, send 404
res.end('', 404);
+ return;
}
// Send success
+ var data = JSON.parse(req.body.source.replace('castor ', ''));
+ this.inputs[input].trigger('event:new', data);
res.end('', 200);
- this.inputs[input].trigger('event:new', req.body);
}
});

0 comments on commit fc4dec9

Please sign in to comment.