Skip to content

Commit

Permalink
Fix faker
Browse files Browse the repository at this point in the history
  • Loading branch information
foucdeg committed Aug 15, 2019
1 parent 3925668 commit 2b580be
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
2 changes: 1 addition & 1 deletion server/src/MapServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import pick from 'lodash/pick';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import http from 'http';
import { PlaneList } from 'src/main';
import { PlaneList } from 'main';
import notams from './notams';

const headers = (req: express.Request, res: express.Response, next: () => void) => {
Expand Down
2 changes: 1 addition & 1 deletion server/src/UDPListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import dgram from 'dgram';
import geolib from 'geolib';
import last from 'lodash/last';
import { PlaneList, PlaneData } from 'src/main';
import { PlaneList, PlaneData } from 'main';

const HISTORY_DURATION = 5000;

Expand Down
2 changes: 1 addition & 1 deletion server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"target": "es6",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./"
"baseUrl": "./src"
},
"exclude": ["node_modules"]
}
25 changes: 20 additions & 5 deletions tools/faker.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const dgram = require('dgram');
const dgram = require("dgram");

const ip = process.env.TARGET_IP;
const port = process.env.TARGET_PORT || 49003;

let i = 0;
const lonOffset = 4 * Math.random();
const socket = dgram.createSocket('udp4');
const socket = dgram.createSocket("udp4");

function generateCoordinates(index) {
return {
longitude: lonOffset + 4 * Math.cos(index / 4000.0),
latitude: 45 + 2 * Math.sin(index / 4000.0),
altitude: 20000 * (1 + Math.sin(index / 400.0)),
altitude: 20000 * (1 + Math.sin(index / 400.0))
};
}

Expand All @@ -20,7 +20,7 @@ function sendDatagram() {
const { longitude, latitude, altitude } = generateCoordinates(i);

const startBuffer = Buffer.from([68, 65, 84, 65, 60, 20, 0, 0, 0]);
const endBuffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
const endBuffer = Buffer.from(Array(20).fill(0));

const latBuffer = Buffer.alloc(4);
latBuffer.writeFloatLE(latitude);
Expand All @@ -31,7 +31,22 @@ function sendDatagram() {
const altBuffer = Buffer.alloc(4);
altBuffer.writeFloatLE(altitude);

const finalBuffer = Buffer.concat([startBuffer, latBuffer, lonBuffer, altBuffer, endBuffer]);
const startTransponderBuffer = Buffer.from([104, 0, 0, 0, 0, 0, 0, 0]);
const endTransponderBuffer = Buffer.from(Array(24).fill(0));

const transponderBuffer = Buffer.alloc(4);
transponderBuffer.writeFloatLE(4242);

const finalBuffer = Buffer.concat([
startBuffer,
latBuffer,
lonBuffer,
altBuffer,
endBuffer,
startTransponderBuffer,
transponderBuffer,
endTransponderBuffer
]);
socket.send(finalBuffer, port, ip);
}

Expand Down
49 changes: 36 additions & 13 deletions tools/replay-faker.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const dgram = require('dgram');
const dgram = require("dgram");

const ip = process.env.TARGET_IP;
const port = process.env.TARGET_PORT || 49003;
const INTERVAL = process.env.INTERVAL || 100;
const SPEED = process.env.SPEED || 1;

const socket = dgram.createSocket('udp4');
const socket = dgram.createSocket("udp4");

const data = require('./traces/flight-playback.json');
const data = require("./traces/flight-playback.json");
const trace = data.result.response.data.flight.track;

let index = process.env.START_INDEX ? parseInt(process.env.START_INDEX) : 0;
Expand All @@ -16,7 +16,7 @@ const initialTrackTimestamp = trace[index].timestamp;

const emit = (latitude, longitude, altitude) => {
const startBuffer = Buffer.from([68, 65, 84, 65, 60, 20, 0, 0, 0]);
const endBuffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
const endBuffer = Buffer.from(Array(20).fill(0));

const latBuffer = Buffer.alloc(4);
latBuffer.writeFloatLE(latitude);
Expand All @@ -27,27 +27,50 @@ const emit = (latitude, longitude, altitude) => {
const altBuffer = Buffer.alloc(4);
altBuffer.writeFloatLE(altitude);

const finalBuffer = Buffer.concat([startBuffer, latBuffer, lonBuffer, altBuffer, endBuffer]);
socket.send(finalBuffer, port, ip);
}
const startTransponderBuffer = Buffer.from([104, 0, 0, 0, 0, 0, 0, 0]);
const endTransponderBuffer = Buffer.from(Array(24).fill(0));

const transponderBuffer = Buffer.alloc(4);
transponderBuffer.writeFloatLE(4242);

const finalBuffer = Buffer.concat([
startBuffer,
latBuffer,
lonBuffer,
altBuffer,
endBuffer,
startTransponderBuffer,
transponderBuffer,
endTransponderBuffer
]);
socket.send(finalBuffer, port, ip);
};

const sendNextPosition = () => {
const lastPosition = trace[index];
const targetTimestamp = initialTrackTimestamp * 1000 + SPEED * (Date.now() - startTimestamp);
const nextPositionIndex = trace.slice(index).findIndex(position => (position.timestamp * 1000) > targetTimestamp);
const nextPositionIndex = trace
.slice(index)
.findIndex(position => position.timestamp * 1000 > targetTimestamp);
if (nextPositionIndex === -1) {
process.exit(0);
}
const nextPosition = trace[index + nextPositionIndex];

const elapsedTimeProportion = (targetTimestamp - lastPosition.timestamp * 1000) / (nextPosition.timestamp * 1000 - lastPosition.timestamp * 1000);
const targetLat = lastPosition.latitude + elapsedTimeProportion * (nextPosition.latitude - lastPosition.latitude);
const targetLon = lastPosition.longitude + elapsedTimeProportion * (nextPosition.longitude - lastPosition.longitude);
const targetAlt = lastPosition.altitude.feet + elapsedTimeProportion * (nextPosition.altitude.feet - lastPosition.altitude.feet);
const elapsedTimeProportion =
(targetTimestamp - lastPosition.timestamp * 1000) /
(nextPosition.timestamp * 1000 - lastPosition.timestamp * 1000);
const targetLat =
lastPosition.latitude + elapsedTimeProportion * (nextPosition.latitude - lastPosition.latitude);
const targetLon =
lastPosition.longitude +
elapsedTimeProportion * (nextPosition.longitude - lastPosition.longitude);
const targetAlt =
lastPosition.altitude.feet +
elapsedTimeProportion * (nextPosition.altitude.feet - lastPosition.altitude.feet);

emit(targetLat, targetLon, targetAlt);
if ((nextPosition.timestamp * 1000 - targetTimestamp) < INTERVAL) {
if (nextPosition.timestamp * 1000 - targetTimestamp < INTERVAL) {
console.info(`Passed position ${index + nextPositionIndex}`);
index = index + nextPositionIndex;
}
Expand Down

0 comments on commit 2b580be

Please sign in to comment.