Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
Dawn-Field Control Integration via Socket.IO (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmasd authored Mar 3, 2018
1 parent 2c37aff commit 35ac1b1
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 328 deletions.
8 changes: 4 additions & 4 deletions dawn/main/MenuTemplate/DebugMenu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fork } from 'child_process';
import RendererBridge from '../RendererBridge';
import LCMObject from '../networking/FieldControlLCM';
import FCObject from '../networking/FieldControl';

let fakeRuntime = null;

Expand Down Expand Up @@ -31,10 +31,10 @@ const DebugMenu = {
},
},
{
label: 'Restart LCM',
label: 'Restart FC',
click() {
LCMObject.LCMInternal.quit();
LCMObject.setup();
FCObject.FCInternal.quit();
FCObject.setup();
},
},
{
Expand Down
18 changes: 9 additions & 9 deletions dawn/main/main-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import RendererBridge from './RendererBridge';
import { killFakeRuntime } from './MenuTemplate/DebugMenu';
import Template from './MenuTemplate/Template';
import Ansible from './networking/Ansible';
import LCMObject from './networking/FieldControlLCM';
import FCObject from './networking/FieldControl';


app.on('window-all-closed', () => {
Expand All @@ -25,17 +25,17 @@ app.on('will-quit', () => {
}
});

function initializeLCM(event) { // eslint-disable-line no-unused-vars
function initializeFC(event) { // eslint-disable-line no-unused-vars
try {
LCMObject.setup();
FCObject.setup();
} catch (err) {
console.log(err);
}
}

function teardownLCM(event) { // eslint-disable-line no-unused-vars
if (LCMObject.LCMInternal !== null) {
LCMObject.LCMInternal.quit();
function teardownFC(event) { // eslint-disable-line no-unused-vars
if (FCObject.FCInternal !== null) {
FCObject.FCInternal.quit();
}
}

Expand All @@ -59,9 +59,9 @@ export default function showAPI() {

app.on('ready', () => {
Ansible.setup();
ipcMain.on('LCM_CONFIG_CHANGE', LCMObject.changeLCMInfo);
ipcMain.on('LCM_INITIALIZE', initializeLCM);
ipcMain.on('LCM_TEARDOWN', teardownLCM);
ipcMain.on('FC_CONFIG_CHANGE', FCObject.changeFCInfo);
ipcMain.on('FC_INITIALIZE', initializeFC);
ipcMain.on('FC_TEARDOWN', teardownFC);

const mainWindow = new BrowserWindow();

Expand Down
44 changes: 18 additions & 26 deletions dawn/main/networking/Ansible.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import _ from 'lodash';
import RendererBridge from '../RendererBridge';
import {
updateConsole,
clearConsole,
} from '../../renderer/actions/ConsoleActions';
import {
ansibleDisconnect,
Expand All @@ -18,7 +17,7 @@ import {
} from '../../renderer/actions/InfoActions';
import { updatePeripherals } from '../../renderer/actions/PeripheralActions';
import { robotState, Logger, defaults } from '../../renderer/utils/utils';
import LCMObject from './FieldControlLCM';
import FCObject from './FieldControl';

const DawnData = (new protobuf.Root()).loadSync(`${__dirname}/ansible.proto`, { keepCase: true }).lookupType('DawnData');
const { StudentCodeStatus } = DawnData;
Expand Down Expand Up @@ -59,7 +58,7 @@ function buildProto(data) {
return DawnData.create({
student_code_status: status,
gamepads,
team_color: (LCMObject.stationNumber < 2) ? DawnData.TeamColor.BLUE : DawnData.TeamColor.GOLD,
team_color: (FCObject.stationNumber < 2) ? DawnData.TeamColor.BLUE : DawnData.TeamColor.GOLD,
});
}

Expand Down Expand Up @@ -179,8 +178,9 @@ class TCPSocket {
constructor(socket, logger) {
this.tryUpload = this.tryUpload.bind(this);
this.requestTimestamp = this.requestTimestamp.bind(this);
this.logger = logger;
this.sendFieldControl = this.sendFieldControl.bind(this);

this.logger = logger;
this.socket = socket;

this.logger.log('Runtime connected');
Expand Down Expand Up @@ -227,6 +227,20 @@ class TCPSocket {
});
}

sendFieldControl(data) {
const rawMsg = {
header: Notification.Type.GAMECODE_TRANSMISSION,
gamecode_solutions: data.solutions,
gamecodes: data.codes,
rfids: data.rfids,
};
const message = Notification.encode(Notification.create(rawMsg)).finish();

this.socket.write(message, () => {
this.logger.log(`FC Message Sent: ${rawMsg}`);
});
}

tryUpload() {
const message = Notification.encode(Notification.create({
header: Notification.Type.STUDENT_SENT,
Expand Down Expand Up @@ -274,28 +288,6 @@ class TCPServer {
}
}

const onUpdateCodeStatus = (status) => {
RendererBridge.reduxDispatch(updateCodeStatus(status));
};

const onClearConsole = () => {
RendererBridge.reduxDispatch(clearConsole());
};

/* Redux short-circuiting for when field control wants to start/stop robot
*/
const startRobot = () => { // eslint-disable-line no-unused-vars
// TODO: Probably move this to Editor using ipcRenderer/ipcMain.
// this.state.mode doesn't exist here.
RendererBridge.reduxDispatch(onUpdateCodeStatus(this.state.mode));
RendererBridge.reduxDispatch(onClearConsole());
};

const stopRobot = () => { // eslint-disable-line no-unused-vars
// TODO: Probably move this to Editor using ipcRenderer/ipcMain. GUI can't change here.
RendererBridge.reduxDispatch(onUpdateCodeStatus(robotState.IDLE));
};

const Ansible = {
conns: [],
logger: new Logger('ansible', 'Ansible Debug'),
Expand Down
71 changes: 71 additions & 0 deletions dawn/main/networking/FieldControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import io from 'socket.io-client';
import { updateRobot } from '../../renderer/actions/FieldActions';
import RendererBridge from '../RendererBridge';
import { Logger } from '../../renderer/utils/utils';
import Ansible from './Ansible';

class FCInternals {
constructor(stationNumber, bridgeAddress, logger) {
this.socket = null;
this.queuedPublish = null;
this.stationNumber = stationNumber;
this.bridgeAddress = bridgeAddress;
this.logger = logger;
this.logger.log(`Field Control: SN-${this.stationNumber} BA-${this.bridgeAddress}`);
this.init = this.init.bind(this);
this.quit = this.quit.bind(this);
}

init() {
this.socket = io(`http://${this.bridgeAddress}:7000`);
this.socket.on('connect', () => {
this.socket.on('robot_state', (data) => {
RendererBridge.reduxDispatch(updateRobot(JSON.parse(data)));
});
this.socket.on('codes', (data) => {
if (Ansible.conns[2].socket !== null) {
Ansible.conns[2].socket.sendFieldControl(JSON.parse(data));
} else {
this.logger.log('Trying to send FC Info to Disconnected Robot');
}
});
});
}

quit() {
try {
this.socket.close();
} catch (err) {
this.logger.log(err);
}
this.socket = null;
}
}

const FCObject = {
FCInternal: null,
stationNumber: 4,
bridgeAddress: 'localhost',
logger: new Logger('fieldControl', 'Field Control Debug'),
setup() {
if (this.FCInternal !== null) {
this.FCInternal.quit();
}
this.FCInternal = new FCInternals(this.stationNumber, this.bridgeAddress, FCObject.logger);
this.FCInternal.init();
},
changeFCInfo(event, arg) {
if (arg.stationNumber !== null) {
FCObject.stationNumber = arg.stationNumber;
FCObject.logger.log(`stationNumber set to ${FCObject.stationNumber}`);
}

if (arg.bridgeAddress !== null) {
FCObject.bridgeAddress = arg.bridgeAddress;
FCObject.logger.log(`bridgeAddress set to ${FCObject.bridgeAddress}`);
}
},
};


export default FCObject;
122 changes: 0 additions & 122 deletions dawn/main/networking/FieldControlLCM.js

This file was deleted.

2 changes: 1 addition & 1 deletion dawn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"dependencies": {
"brace": "0.11.0",
"bufferutil": "3.0.3",
"circular-json": "0.5.1",
"create-react-class": "15.6.2",
"electron": "1.7.11",
"electron-json-storage": "4.0.2",
Expand All @@ -81,6 +80,7 @@
"redux-saga": "0.16.0",
"seedrandom": "2.4.3",
"smalltalk": "2.5.1",
"socket.io-client": "^2.0.4",
"ssh2": "0.5.5",
"superagent": "3.8.2",
"utf-8-validate": "4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion dawn/renderer/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class AppComponent extends React.Component {
return;
}
this.props.onFCUpdate(data);
ipcRenderer.send('LCM_CONFIG_CHANGE', data);
ipcRenderer.send('FC_CONFIG_CHANGE', data);
});
}

Expand Down
2 changes: 1 addition & 1 deletion dawn/renderer/components/ConfigBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ConfigBoxComponent extends React.Component {
storage.set('fieldControl', newConfig, (err) => {
if (err) logging.log(err);
});
ipcRenderer.send('LCM_CONFIG_CHANGE', newConfig);
ipcRenderer.send('FC_CONFIG_CHANGE', newConfig);

this.props.hide();
}
Expand Down
Loading

0 comments on commit 35ac1b1

Please sign in to comment.