Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hardware App Promises #223

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion BoneCentral/Brain/CppInterface/Factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
8 changes: 7 additions & 1 deletion BoneCentral/Brain/CppInterface/PowerManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module.exports = (function(){

function PowerManager(cmdOut) {
function PowerManager(cmdOut, events) {
this._cmdOut = cmdOut;
this._events = events;
}

PowerManager.prototype.turnOnEscs = function(){
Expand All @@ -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;
Expand Down
24 changes: 20 additions & 4 deletions BoneCentral/Brain/Sockets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down