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

Feature/web app organization #216

Open
wants to merge 5 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
8 changes: 8 additions & 0 deletions BoneCentral/Brain/AI/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var Poseidon = require('../app.js');

var power = Poseidon.HardwareFactory.createPowerManager();

setTimeout(function() {
power.exit();
process.exit();
}, 10000);
32 changes: 14 additions & 18 deletions BoneCentral/Brain/CppInterface/Factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,37 @@
* Created by Nathan Copier on 4/28/2016.
*/

var HeadLights = require("./Headlights");
var PowerManager = require("./PowerManager");
var ThrustController = require("./ThrustController");
var CppLogSource = require("./CppLogSource");
var ImuSensor = require("./ImuSensor");
var Sockets = require('../Sockets');
var Ports = require('../Sockets/Ports.json');
var HeadLights = require("./Headlights");
var PowerManager = require("./PowerManager");
var ThrustController = require("./ThrustController");
var ImuSensor = require("./ImuSensor");
var CppLogSource = require("./CppLogSource");

module.exports = (function() {

var logSocket = Sockets.createSocket(Ports.LoggerPort);
var dispatcherSocket = Sockets.createSocket(Ports.DispatcherPort);

function Factory() {}
function Factory(dispatcherSocket, logSocket) {
this.dispatcherSocket = dispatcherSocket;
this.logSocket = logSocket;
}

Factory.prototype.createCppLogSource = function (loggerOutput) {
return new CppLogSource(logSocket.Output, loggerOutput)
return new CppLogSource(this.logSocket.Output, loggerOutput)
};

Factory.prototype.createHeadlights = function () {
return new HeadLights(dispatcherSocket.Input);
return new HeadLights(this.dispatcherSocket.Input);
};

Factory.prototype.createImuSensor = function () {
return new ImuSensor(dispatcherSocket.Output, dispatcherSocket.Input);
return new ImuSensor(this.dispatcherSocket.Output, this.dispatcherSocket.Input);
};

Factory.prototype.createPowerManager = function () {
return new PowerManager(dispatcherSocket.Input);
return new PowerManager(this.dispatcherSocket.Input);
};

Factory.prototype.createThrustController = function () {
return new ThrustController(dispatcherSocket.Input);
return new ThrustController(this.dispatcherSocket.Input);
};

return Factory;

})();
4 changes: 2 additions & 2 deletions BoneCentral/Brain/CppInterface/PeripheralsProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/

var Spawner = require("child_process");
var Path = require("path");
var Ports = require("../Sockets/Ports.json");
var Path = require("path");
var Ports = require("../Sockets/Ports.json");

var initialize = function () {
var args = [
Expand Down
2 changes: 1 addition & 1 deletion BoneCentral/Brain/CppInterface/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ module.exports = {
CppLogSource: require("./CppLogSource"),
ImuSensor: require("./ImuSensor"),
Factory: require("./Factory"),
Peripherals: require("./PeripheralsProcess")
Process: require("./PeripheralsProcess")
};
24 changes: 24 additions & 0 deletions BoneCentral/Brain/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var Config = require('./config.json');
var Ports = require('./Sockets/Ports.json');
var Sockets = require('./Sockets');
var Peripherals = require('./CppInterface/PeripheralsProcess.js');
var HardwareFactory = require('./CppInterface/Factory.js');
var Vision = require('./VisionInterface');

var server = Sockets.createSocket(Ports.DispatcherPort);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking encapsulation. These sockets should have been left inside the Factory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to disagree. App.js (while improperly named) is a form of "builder" design pattern. (The builder design pattern is one of the 23 discussed by the Gang of Four.)

A builder will take advantage of factories to create more complex objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I will admit that this builder module does need to be better organized.

var logServer = Sockets.createSocket(Ports.LoggerPort);
var hardwareFactory = new HardwareFactory(server, logServer);
var visionFactory = new Vision.Factory();

if(Config.appStartMode === "auto") {
Peripherals.initialize();
}
else {
console.log("Entering manual mode: run './Periphers --dispatcherPort="+
Ports.DispatcherPort+" --loggerPort="+Ports.LoggerPort+"'");
}

module.exports = {
HardwareFactory: hardwareFactory,
VisionFactory: visionFactory
};
3 changes: 3 additions & 0 deletions BoneCentral/Brain/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"appStartMode":"manual"
}
5 changes: 5 additions & 0 deletions BoneCentral/Brain/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
appStartMode:
manual - The js app is started seperately from the c++ app. (The user must
manualy start the c++ app after starting the js app.)
auto - The js app starts concurrently with the c++ app as if they were
one.
137 changes: 137 additions & 0 deletions BoneCentral/WebApplication/hardware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
var fileSystem = require('fs');
var path = require("path");
var Poseidon = require('../Brain/app.js');

var WebLogger = require('./WebLogger');
var FileLogger = require('./FileLogger');

var fileLogger = new FileLogger("./test.log");
var webLogger = new WebLogger(fileLogger);

var thrustController = Poseidon.HardwareFactory.createThrustController();
var powerManager = Poseidon.HardwareFactory.createPowerManager();
var imuSensor = Poseidon.HardwareFactory.createImuSensor();
var headLights = Poseidon.HardwareFactory.createHeadlights();

function DMSG(x) {
console.log(x);
}

module.exports = {
pullWebLog: function() {
return webLogger.pull();
},

move: function(value) {
DMSG("Moving: " + value);
thrustController.move(value);
},
dive: function(value) {
DMSG("Diving: " + value);
thrustController.dive(value);
},
secondaryDive: function(value) {
DMSG("Diving2: " + value);
thrustController.secondaryDive(value);
},
yaw: function(value) {
DMSG("Yawing: " + value);
thrustController.yaw(value);
},
pitch: function(value) {
DMSG("Pitching: " + value);
thrustController.pitch(value);
},
roll: function(value) {
DMSG("Rolling: " + value);
thrustController.roll(value);
},
goDirection: function(move, dive, dive2) {
DMSG("Go Direction: " + move + ", " + dive2 + ", " + dive);
thrustController.goDirection(move, dive2, dive);
},
rotate: function(yaw, pitch, roll) {
DMSG("Rotate: " + yaw + ", " + pitch + ", " + roll);
thrustController.rotate(yaw, pitch, roll);
},
killThrust: function() {
DMSG("Kill Thrusters");
thrustController.kill();
},

turnOnImu: function() {
powerManager.turnOnImu();
},
turnOffImu: function() {
powerManager.turnOffImu();
},
getAcceleration: function() {
imuSensor.getAcceleration().done(function(accel) {
webLogger.info("Acceleration: " + JSON.stringify(accel));
});
},
getAngularAcceleration: function() {
imuSensor.getAngularAcceleration().done(function(accel) {
webLogger.info("Angular Acceleration: " + JSON.stringify(accel));
});
},
getHeading: function() {
imuSensor.getHeading().done(function(heading) {
webLogger.info("Heading: " + JSON.stringify(heading));
});
},
getInternalTemperature: function() {
imuSensor.getInternalTemperature().done(function(temperature) {
webLogger.info("Internal Temperature: " + JSON.stringify(temperature));
});
},
getExternalTemperature: function() {
imuSensor.getExternalTemperature().done(function(temperature) {
webLogger.info("External Temperature: " + JSON.stringify(temperature));
});
},
getInternalPressure: function() {
imuSensor.getInternalPressure().done(function(pressure) {
webLogger.info("Internal Pressure: " + JSON.stringify(pressure));
});
},
getExternalPressure: function() {
imuSensor.getExternalPressure().done(function(pressure) {
webLogger.info("External Pressure: " + JSON.stringify(pressure));
});
},

exit: function() {
powerManager.exit();
process.exit();
},

turnOnEscs: function() {
DMSG("Turn On Escs");
powerManager.turnOnEscs();
},
turnOffEscs: function() {
DMSG("Turn Off Escs");
Copy link
Member

@nfcopier nfcopier Jun 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a logger. Use it. Also, this type of psuedo-macro looking naming does not feel appropriate for JavaScript.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was temporary (DMSG) - I forgot to delete it.
(Right now I'm not sure about the current integrity of the Logger object. I didn't change any of the code for it, but some portions of it were broken before I even started the WebBrainUpdate branch.)

powerManager.turnOffEscs();
},

toggleLights: function() {
DMSG("Toggle Lights");
headLights.toggleLights();
},

runScript: function(index) {
fileSystem.readdir("./TestScripts", function (err, fileNames) {
if(fileNames[index]) {
var filePath = path.resolve("TestScripts", fileNames[index]);
if(require.cache[filePath]) delete require.cache[filePath];
var script = require(filePath);
script.execute(thrustController, imuSensor, webLogger, headLights, powerManager);
return ('ran ' + fileNames[index]);
}
else {
return ('script not found');
}
})
}
};
Loading