Implemented basic Plugin handling

This commit is contained in:
Pax1601
2023-09-15 17:05:26 +02:00
parent ad06117b78
commit 588228c050
75 changed files with 1920 additions and 1657 deletions

View File

@@ -1,82 +1,65 @@
var express = require('express');
var app = express();
var express = require('express');
var app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
/*
Flight:
"name"
"take-off time"
"priority"
"status"
//*/
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function Flight( name, boardId, unitId ) {
function Flight(name, boardId, unitId) {
this.assignedAltitude = 0;
this.assignedSpeed = 0;
this.id = uuidv4();
this.boardId = boardId;
this.name = name;
this.status = "unknown";
this.takeoffTime = -1;
this.unitId = parseInt( unitId );
this.assignedSpeed = 0;
this.id = uuidv4();
this.boardId = boardId;
this.name = name;
this.status = "unknown";
this.takeoffTime = -1;
this.unitId = parseInt(unitId);
}
Flight.prototype.getData = function() {
Flight.prototype.getData = function () {
return {
"assignedAltitude" : this.assignedAltitude,
"assignedSpeed" : this.assignedSpeed,
"id" : this.id,
"boardId" : this.boardId,
"name" : this.name,
"status" : this.status,
"takeoffTime" : this.takeoffTime,
"unitId" : this.unitId
"assignedAltitude": this.assignedAltitude,
"assignedSpeed": this.assignedSpeed,
"id": this.id,
"boardId": this.boardId,
"name": this.name,
"status": this.status,
"takeoffTime": this.takeoffTime,
"unitId": this.unitId
};
}
Flight.prototype.setAssignedAltitude = function (assignedAltitude) {
Flight.prototype.setAssignedAltitude = function( assignedAltitude ) {
if ( isNaN( assignedAltitude ) ) {
if (isNaN(assignedAltitude)) {
return "Altitude must be a number"
}
this.assignedAltitude = parseInt( assignedAltitude );
this.assignedAltitude = parseInt(assignedAltitude);
return true;
}
Flight.prototype.setAssignedSpeed = function (assignedSpeed) {
Flight.prototype.setAssignedSpeed = function( assignedSpeed ) {
if ( isNaN( assignedSpeed ) ) {
if (isNaN(assignedSpeed)) {
return "Speed must be a number"
}
this.assignedSpeed = parseInt( assignedSpeed );
this.assignedSpeed = parseInt(assignedSpeed);
return true;
}
Flight.prototype.setOrder = function( order ) {
Flight.prototype.setOrder = function (order) {
this.order = order;
@@ -84,10 +67,9 @@ Flight.prototype.setOrder = function( order ) {
}
Flight.prototype.setStatus = function (status) {
Flight.prototype.setStatus = function( status ) {
if ( [ "unknown", "checkedin", "readytotaxi", "clearedtotaxi", "halted", "terminated" ].indexOf( status ) < 0 ) {
if (["unknown", "checkedin", "readytotaxi", "clearedtotaxi", "halted", "terminated"].indexOf(status) < 0) {
return "Invalid status";
}
@@ -97,197 +79,186 @@ Flight.prototype.setStatus = function( status ) {
}
Flight.prototype.setTakeoffTime = function (takeoffTime) {
Flight.prototype.setTakeoffTime = function( takeoffTime ) {
if ( takeoffTime === "" || takeoffTime === -1 ) {
if (takeoffTime === "" || takeoffTime === -1) {
this.takeoffTime = -1;
}
if ( isNaN( takeoffTime ) ) {
if (isNaN(takeoffTime)) {
return "Invalid takeoff time"
}
this.takeoffTime = parseInt( takeoffTime );
this.takeoffTime = parseInt(takeoffTime);
return true;
}
function ATCDataHandler( data ) {
function ATCDataHandler(data) {
this.data = data;
}
ATCDataHandler.prototype.addFlight = function( flight ) {
ATCDataHandler.prototype.addFlight = function (flight) {
if ( flight instanceof Flight === false ) {
throw new Error( "Given flight is not an instance of Flight" );
if (flight instanceof Flight === false) {
throw new Error("Given flight is not an instance of Flight");
}
this.data.flights[ flight.id ] = flight;
this.data.flights[flight.id] = flight;
}
ATCDataHandler.prototype.deleteFlight = function( flightId ) {
delete this.data.flights[ flightId ];
ATCDataHandler.prototype.deleteFlight = function (flightId) {
delete this.data.flights[flightId];
}
ATCDataHandler.prototype.getFlight = function( flightId ) {
return this.data.flights[ flightId ] || false;
ATCDataHandler.prototype.getFlight = function (flightId) {
return this.data.flights[flightId] || false;
}
ATCDataHandler.prototype.getFlights = function() {
ATCDataHandler.prototype.getFlights = function () {
return this.data.flights;
}
const dataHandler = new ATCDataHandler( {
const dataHandler = new ATCDataHandler({
"flights": {}
} );
});
/**************************************************************************************************************/
// Endpoints
/**************************************************************************************************************/
app.get("/flight", (req, res) => {
let flights = Object.values(dataHandler.getFlights());
app.get( "/flight", ( req, res ) => {
if (flights && req.query.boardId) {
let flights = Object.values( dataHandler.getFlights() );
if ( flights && req.query.boardId ) {
flights = flights.reduce( ( acc, flight ) => {
if ( flight.boardId === req.query.boardId ) {
acc[ flight.id ] = flight;
flights = flights.reduce((acc, flight) => {
if (flight.boardId === req.query.boardId) {
acc[flight.id] = flight;
}
return acc;
}, {} );
}, {});
}
res.json( flights );
res.json(flights);
});
app.patch( "/flight/:flightId", ( req, res ) => {
app.patch("/flight/:flightId", (req, res) => {
const flightId = req.params.flightId;
const flight = dataHandler.getFlight( flightId );
const flight = dataHandler.getFlight(flightId);
if ( !flight ) {
res.status( 400 ).send( `Unrecognised flight ID (given: "${req.params.flightId}")` );
if (!flight) {
res.status(400).send(`Unrecognised flight ID (given: "${req.params.flightId}")`);
}
if ( req.body.hasOwnProperty( "assignedAltitude" ) ) {
if (req.body.hasOwnProperty("assignedAltitude")) {
const altitudeChangeSuccess = flight.setAssignedAltitude( req.body.assignedAltitude );
const altitudeChangeSuccess = flight.setAssignedAltitude(req.body.assignedAltitude);
if ( altitudeChangeSuccess !== true ) {
res.status( 400 ).send( altitudeChangeSuccess );
if (altitudeChangeSuccess !== true) {
res.status(400).send(altitudeChangeSuccess);
}
}
if ( req.body.hasOwnProperty( "assignedSpeed" ) ) {
if (req.body.hasOwnProperty("assignedSpeed")) {
const speedChangeSuccess = flight.setAssignedSpeed( req.body.assignedSpeed );
const speedChangeSuccess = flight.setAssignedSpeed(req.body.assignedSpeed);
if ( speedChangeSuccess !== true ) {
res.status( 400 ).send( speedChangeSuccess );
if (speedChangeSuccess !== true) {
res.status(400).send(speedChangeSuccess);
}
}
if ( req.body.status ) {
if (req.body.status) {
const statusChangeSuccess = flight.setStatus( req.body.status );
const statusChangeSuccess = flight.setStatus(req.body.status);
if ( statusChangeSuccess !== true ) {
res.status( 400 ).send( statusChangeSuccess );
if (statusChangeSuccess !== true) {
res.status(400).send(statusChangeSuccess);
}
}
if ( req.body.hasOwnProperty( "takeoffTime" ) ) {
if (req.body.hasOwnProperty("takeoffTime")) {
const takeoffChangeSuccess = flight.setTakeoffTime( req.body.takeoffTime );
const takeoffChangeSuccess = flight.setTakeoffTime(req.body.takeoffTime);
if ( takeoffChangeSuccess !== true ) {
res.status( 400 ).send( takeoffChangeSuccess );
if (takeoffChangeSuccess !== true) {
res.status(400).send(takeoffChangeSuccess);
}
}
res.json( flight.getData() );
res.json(flight.getData());
});
app.post( "/flight/order", ( req, res ) => {
app.post("/flight/order", (req, res) => {
if ( !req.body.boardId ) {
res.status( 400 ).send( "Invalid/missing boardId" );
if (!req.body.boardId) {
res.status(400).send("Invalid/missing boardId");
}
if ( !req.body.order || !Array.isArray( req.body.order ) ) {
res.status( 400 ).send( "Invalid/missing boardId" );
if (!req.body.order || !Array.isArray(req.body.order)) {
res.status(400).send("Invalid/missing boardId");
}
req.body.order.forEach( ( flightId, i ) => {
req.body.order.forEach((flightId, i) => {
dataHandler.getFlight( flightId ).setOrder( i );
dataHandler.getFlight(flightId).setOrder(i);
});
res.send( "" );
res.send("");
});
app.post( "/flight", ( req, res ) => {
app.post("/flight", (req, res) => {
if ( !req.body.boardId ) {
res.status( 400 ).send( "Invalid/missing boardId" );
if (!req.body.boardId) {
res.status(400).send("Invalid/missing boardId");
}
if ( !req.body.name ) {
res.status( 400 ).send( "Invalid/missing flight name" );
if (!req.body.name) {
res.status(400).send("Invalid/missing flight name");
}
if ( !req.body.unitId || isNaN( req.body.unitId ) ) {
res.status( 400 ).send( "Invalid/missing unitId" );
if (!req.body.unitId || isNaN(req.body.unitId)) {
res.status(400).send("Invalid/missing unitId");
}
const flight = new Flight( req.body.name, req.body.boardId, req.body.unitId );
const flight = new Flight(req.body.name, req.body.boardId, req.body.unitId);
dataHandler.addFlight( flight );
dataHandler.addFlight(flight);
res.status( 201 );
res.status(201);
res.json( flight.getData() );
res.json(flight.getData());
});
app.delete( "/flight/:flightId", ( req, res ) => {
app.delete("/flight/:flightId", (req, res) => {
const flight = dataHandler.getFlight( req.params.flightId );
const flight = dataHandler.getFlight(req.params.flightId);
if ( !flight ) {
res.status( 400 ).send( `Unrecognised flight ID (given: "${req.params.flightId}")` );
if (!flight) {
res.status(400).send(`Unrecognised flight ID (given: "${req.params.flightId}")`);
}
dataHandler.deleteFlight( req.params.flightId );
dataHandler.deleteFlight(req.params.flightId);
res.status( 204 ).send( "" );
res.status(204).send("");
});