diff --git a/BoneCentral/Brain/CppInterface/Factory.js b/BoneCentral/Brain/CppInterface/Factory.js index 2727448..f07ccf2 100644 --- a/BoneCentral/Brain/CppInterface/Factory.js +++ b/BoneCentral/Brain/CppInterface/Factory.js @@ -30,7 +30,7 @@ module.exports = (function() { }; Factory.prototype.createPowerManager = function () { - return new PowerManager(dispatcherSocket.Input); + return new PowerManager(this.dispatcherSocket.Input, this.dispatcherSocket.Events); }; Factory.prototype.createThrustController = function () { diff --git a/BoneCentral/Brain/CppInterface/PowerManager.js b/BoneCentral/Brain/CppInterface/PowerManager.js index d10e7e6..fd9d71b 100644 --- a/BoneCentral/Brain/CppInterface/PowerManager.js +++ b/BoneCentral/Brain/CppInterface/PowerManager.js @@ -1,7 +1,8 @@ module.exports = (function(){ - function PowerManager(cmdOut) { + function PowerManager(cmdOut, events) { this._cmdOut = cmdOut; + this._events = events; } PowerManager.prototype.turnOnEscs = function(){ @@ -19,9 +20,14 @@ module.exports = (function(){ PowerManager.prototype.turnOffImu = function () { this._cmdOut.write("turnOffImuSensor\n"); }; + + PowerManager.prototype.connect = function() { + return this._events.OnConnect; + } PowerManager.prototype.exit = function () { this._cmdOut.write("exit\n"); + return this._events.OnExit; }; return PowerManager; diff --git a/BoneCentral/Brain/Sockets/index.js b/BoneCentral/Brain/Sockets/index.js index 4882c3a..a2da922 100644 --- a/BoneCentral/Brain/Sockets/index.js +++ b/BoneCentral/Brain/Sockets/index.js @@ -3,15 +3,31 @@ */ var Streams = require('stream'); -var Net = require('net'); +var Net = require('net'); +var $ = require('../Utilities').Promises module.exports.createSocket = function (port) { - var input = new Streams.PassThrough(); - var output = new Streams.PassThrough(); + var input = new Streams.PassThrough(); + var output = new Streams.PassThrough(); + var onExit = $.Deferred(); + var onConnect = $.Deferred(); + Net.createServer(function (socket) { _handleClient(socket, input, output); + socket.on("close", function() { + onExit.resolve(); + }); + onConnect.resolve(); }).listen(port); - return {Input: input, Output: output}; + + return { + Input: input, + Output: output, + Events: { + OnConnect: onConnect.promise(), + OnExit: onExit.promise() + } + }; }; var _handleClient = function (socket, input, output) {