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

Output CSV and implement packet logger #226

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions src/Core/RoveProtocol/Rovecomm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
/* eslint-disable import/no-cycle */
import { parseHeader, createHeader, TCPParseWrapper, VersionNumber } from './Rovecomm3';

export let RovecommManifest: any = {};

Check warning on line 15 in src/Core/RoveProtocol/Rovecomm.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, lint)

Unexpected any. Specify a different type
export let dataSizes: number[] = [];
export let DataTypes: any = {};

Check warning on line 17 in src/Core/RoveProtocol/Rovecomm.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, lint)

Unexpected any. Specify a different type
export let headerLength = 0;
export let SystemPackets: any = {};

Check warning on line 19 in src/Core/RoveProtocol/Rovecomm.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, lint)

Unexpected any. Specify a different type
export let NetworkDevices: any = {};

Check warning on line 20 in src/Core/RoveProtocol/Rovecomm.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, lint)

Unexpected any. Specify a different type
let ethernetUDPPort = 11000;
let ethernetTCPPort = 12000;
const filepath = path.join(__dirname, '../assets/RovecommManifest.json');
Expand Down Expand Up @@ -46,7 +46,7 @@

interface TCPSocket {
RCSocket: netSocket;
RCDeque: Deque<any>;

Check warning on line 49 in src/Core/RoveProtocol/Rovecomm.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, lint)

Unexpected any. Specify a different type
}

function decodePacket(dataType: number, dataCount: number, data: Buffer): number[] | string {
Expand Down Expand Up @@ -105,7 +105,7 @@
return retArray;
}

export async function parse(packet: Buffer, rinfo?: any) {

Check warning on line 108 in src/Core/RoveProtocol/Rovecomm.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, lint)

Unexpected any. Specify a different type
/*
* Parse takes in a packet buffer and will call decodePacket and emit
* the rovecomm event with the proper dataId and that typed data
Expand Down Expand Up @@ -336,7 +336,7 @@
* Listens on the class UDP socket, always calling parse if it recieves anything,
* and properly binding the socket to a port
*/
this.UDPSocket.on('message', async (msg: Buffer, rinfo: any) => {

Check warning on line 339 in src/Core/RoveProtocol/Rovecomm.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, lint)

Unexpected any. Specify a different type
parse(msg, rinfo);
});
this.UDPSocket.bind(11000);
Expand Down Expand Up @@ -387,7 +387,7 @@
}

// While most "any" variable types have been removed, data really can be almost any type
sendCommand(dataIdStr: string, dataIn: any, reliability = false): void {

Check warning on line 390 in src/Core/RoveProtocol/Rovecomm.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, lint)

Unexpected any. Specify a different type
/*
* Takes a dataIdString, data, and optional reliability (to determine)
* UDP or TCP, properly types the data according to the type in the manifest
Expand Down Expand Up @@ -514,6 +514,15 @@
} else {
this.sendTCP(packet, destinationIp, port);
}
// eslint-disable-next-line @typescript-eslint/no-use-before-define
rovecomm.emit('BasestationCommand', {
name: dataIdStr,
dataId,
time: new Date().toLocaleTimeString(),
dataType,
dataCount,
data,
});
}

async resubscribe() {
Expand Down
2 changes: 2 additions & 0 deletions src/RON/RON.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PingGraph from './components/PingGraph';
import PingTool from './components/PingTool';
import PacketLogger from './components/PacketLogger';
import PingMap from './components/PingMap';
import SentPacketLogger from './components/SentPacketLogger';

const row: CSS.Properties = {
display: 'flex',
Expand Down Expand Up @@ -44,6 +45,7 @@ class RoverOverviewOfNetwork extends Component<IProps, IState> {
<div style={column}>
<PingMap devices={this.state.devices} />
<PacketLogger />
<SentPacketLogger />
</div>
<div style={column}>
<CustomPackets />
Expand Down
31 changes: 30 additions & 1 deletion src/RON/components/PacketLogger.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from 'react';
import CSS from 'csstype';
import ReactTable from 'react-table-v6';
import fs from 'fs';
// import "../../node_modules/react-table-v6/react-table.css"
import { rovecomm, RovecommManifest } from '../../Core/RoveProtocol/Rovecomm';
wiidler marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -42,6 +43,12 @@ const selector: CSS.Properties = {
width: '200px',
};

const buttons: CSS.Properties = {
fontFamily: 'arial',
lineHeight: '20px',
fontSize: '16px',
};

wiidler marked this conversation as resolved.
Show resolved Hide resolved
interface IProps {
style?: CSS.Properties;
}
Expand Down Expand Up @@ -104,10 +111,29 @@ class PacketLogger extends Component<IProps, IState> {
this.setState((prevState) => ({ data: [newData].concat(prevState.data) }));
}

exportPacket(): void {
fs.writeFile(
'packetLog.csv',
Copy link
Member

Choose a reason for hiding this comment

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

Should specify that it's the received packet log, and include the timestamp so it doesn't overwrite if we save multiple logs before we analyze them.

this.state.data
.map((temp: any) => {
return `${temp.name}, ${temp.dataId}, ${temp.time}, ${temp.dataType}, ${temp.dataCount}, ${temp.data.join(
' '
)}`;
})
.join('\n'),
(err) => {
if (err) console.log(err);
else {
console.log('File written successfully\n');
}
Comment on lines +124 to +128
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to log that it was a success. We can assume it was successful if it didn't give an error. We should log this error to the app's logger, not the console log. rovecomm.emit('all', 'Error saving log');

}
);
}

render(): JSX.Element {
return (
<div style={{ ...this.props.style }}>
<div style={label}>Packet Logger</div>
<div style={label}>Received Packet Logger</div>
<div style={container}>
<div style={selectbox}>
<div style={h1Style}>Board:</div>
Expand All @@ -131,6 +157,9 @@ class PacketLogger extends Component<IProps, IState> {
showPageSizeOptions={false}
style={{ textAlign: 'center', margin: 'auto' }}
/>
<button type="button" style={buttons} onClick={() => this.exportPacket()}>
Copy link
Member

Choose a reason for hiding this comment

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

You should pass the exportPacket function as a parameter here instead of creating an anonymous function just to call it.

Save Data
</button>
</div>
</div>
);
Expand Down
145 changes: 145 additions & 0 deletions src/RON/components/SentPacketLogger.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import React, { Component } from 'react';
import CSS from 'csstype';
import ReactTable from 'react-table-v6';
import fs from 'fs';
// import "../../node_modules/react-table-v6/react-table.css"
import { rovecomm, RovecommManifest } from '../../Core/RoveProtocol/Rovecomm';
Comment on lines +5 to +6
Copy link
Member

Choose a reason for hiding this comment

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

Manifest is unused, so can be removed. Commented line can be removed


const h1Style: CSS.Properties = {
fontFamily: 'arial',
fontSize: '18px',
margin: '5px 0px',
};
Comment on lines +8 to +12
Copy link
Member

Choose a reason for hiding this comment

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

Unused css


const container: CSS.Properties = {
display: 'flex',
flexDirection: 'column',
fontFamily: 'arial',
borderTopWidth: '28px',
borderColor: '#990000',
borderBottomWidth: '2px',
borderStyle: 'solid',
padding: '5px',
alignItems: 'center',
overflow: 'auto',
};
const label: CSS.Properties = {
marginTop: '-10px',
position: 'relative',
top: '24px',
left: '3px',
fontFamily: 'arial',
fontSize: '16px',
zIndex: 1,
color: 'white',
};
const selectbox: CSS.Properties = {
display: 'flex',
flexDirection: 'row',
width: '75%',
margin: '2.5px',
justifyContent: 'space-around',
};
const selector: CSS.Properties = {
width: '200px',
};
Comment on lines +36 to +45
Copy link
Member

Choose a reason for hiding this comment

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

Unused CSS


const buttons: CSS.Properties = {
fontFamily: 'arial',
lineHeight: '20px',
fontSize: '16px',
};

interface IProps {
style?: CSS.Properties;
}

interface IState {
data: any;
columns: any;
}

class SentPacketLogger extends Component<IProps, IState> {
static defaultProps = {
style: {},
};

constructor(props: any) {
super(props);
this.state = {
data: [],
columns: [
{ Header: 'Name', accessor: 'name', width: '100' },
{ Header: 'Data Id', accessor: 'dataId', width: '75' },
{ Header: 'Time', accessor: 'time', width: '100' },
{ Header: 'Type', accessor: 'dataType', width: '50' },
{ Header: 'Count', accessor: 'dataCount', width: '50' },
{
Header: 'Data',
accessor: 'data',
width: 'fill',
Cell: (data: any) => (
<div
style={{
overflow: 'none',
textOverflow: 'ellipsis',
}}
>
{data.value.join(', ')}
</div>
),
},
],
};
this.addData = this.addData.bind(this);
rovecomm.on('BasestationCommand', (data: any) => this.addData(data));
}

addData(newData: any): void {
this.setState((prevState) => ({ data: [newData].concat(prevState.data) }));
}
Copy link
Member

Choose a reason for hiding this comment

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

Should add a clear button or only save up to like 100 packets for this since we don't want it to eat up the entire system's memory over the entire time of a task saving every packet that was sent.


exportPacket(): void {
fs.writeFile(
'SentPacket.csv',
this.state.data
.map((temp: any) => {
return `${temp.name}, ${temp.dataId}, ${temp.time}, ${temp.dataType}, ${temp.dataCount}, ${temp.data.join(
' '
)}`;
})
.join('\n'),
(err) => {
if (err) console.log(err);
else {
console.log('File written successfully\n');
}
}
);
}

render(): JSX.Element {
return (
<div style={{ ...this.props.style }}>
<div style={label}>Sent Packet Logger</div>
<div style={container}>
<ReactTable
className="-striped"
data={this.state.data}
columns={this.state.columns}
filterable
defaultPageSize={10}
resizable={false}
showPageSizeOptions={false}
style={{ textAlign: 'center', margin: 'auto' }} // <button type="button" style={buttons} onClick={() => this.receivePackets()}></button>
/>
<button type="button" style={buttons} onClick={() => this.exportPacket()}>
Save Data
</button>
</div>
</div>
);
}
}

export default SentPacketLogger;
Loading