Skip to content

Commit

Permalink
Trainingsstationen synchronisieren
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhollmann committed Mar 11, 2024
1 parent 60068be commit 90d44bb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 48 deletions.
9 changes: 0 additions & 9 deletions db/config/options.js

This file was deleted.

40 changes: 2 additions & 38 deletions src/controllers/training-station/TrainingStationAdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@ import { sequelize } from "../../core/Sequelize";
import { TrainingStation } from "../../models/TrainingStation";
import _TrainingStationAdminValidator from "./_TrainingStationAdmin.validator";
import { Config } from "../../core/Config";

type HomepageStation = {
id: number;
name: string;
ident: string;
frequency: number;
gcap_status?: string;
gcap_training_airport?: boolean;
s1_twr?: boolean;
};
import { updateTrainingStations } from "../../libraries/vatger/GithubLibrary";

async function getAll(request: Request, response: Response, next: NextFunction) {
try {
Expand Down Expand Up @@ -45,36 +36,9 @@ async function getByID(request: Request, response: Response, next: NextFunction)
}
}

type DataHubStations = {
logon: string;
frequency: string;
abbreviation: string;
description: string;
};

async function syncStations(request: Request, response: Response, next: NextFunction) {
try {
const res = await axios.get("https://raw.githubusercontent.com/VATGER-Nav/datahub/main/data.json");
const stations = res.data as DataHubStations[];

for (const station of stations) {
if (station.logon.length === 0 || station.frequency.length === 0) continue;

const dbStation = await TrainingStation.findOne({ where: { callsign: station.logon } });
if (dbStation == null) {
await TrainingStation.create({
callsign: station.logon,
frequency: Number(station.frequency),
});
continue;
}

await dbStation.update({
callsign: station.logon,
frequency: Number(station.frequency),
});
}

await updateTrainingStations();
const newStations = await TrainingStation.findAll();
response.send(newStations);
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const Config = {
},

VATEUD_CORE_CONFIG: {
API_KEY: process.env.VATEUD_CORE_API_KEY
API_KEY: process.env.VATEUD_CORE_API_KEY,
},

CONNECT_CONFIG: {
Expand Down Expand Up @@ -113,6 +113,7 @@ export const Config = {
SYSLOG_CLEANUP: "30 2 * * 1-6",
SEND_EMAIL: "*/5 * * * *",
CHECK_ENDORSEMENT: "* 0 12 * *",
SYNC_TRAINING_STATION: "0 */4 * * *",
},
};

Expand Down
5 changes: 5 additions & 0 deletions src/core/TaskScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dayjs from "dayjs";
import SendEmailTask from "./tasks/SendEmailTask";
import CheckEndorsementTask from "./tasks/CheckEndorsementTask";
import { Config } from "./Config";
import UpdateTrainingStationsTask from "./tasks/UpdateTrainingStationsTask";

export async function initScheduledJobs() {
Logger.log(LogLevels.LOG_INFO, `Scheduling Tasks (CRON)`);
Expand Down Expand Up @@ -36,5 +37,9 @@ export async function initScheduledJobs() {
await CheckEndorsementTask.handle();
}).start();

CronJob.schedule(Config.CRON_CONFIG.SYNC_TRAINING_STATION, async () => {
await UpdateTrainingStationsTask.handle();
}).start();

Logger.log(LogLevels.LOG_SUCCESS, "\0 \u2713 All Tasks scheduled.\n");
}
5 changes: 5 additions & 0 deletions src/core/tasks/UpdateTrainingStationsTask.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { updateTrainingStations } from "../../libraries/vatger/GithubLibrary";

async function handle() {
console.log("Handle...");
try {
await updateTrainingStations();
} catch (e) {}
}

export default {
Expand Down
42 changes: 42 additions & 0 deletions src/libraries/vatger/GithubLibrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import axios from "axios";
import { TrainingStation } from "../../models/TrainingStation";

type DataHubStations = {
logon: string;
frequency: string;
abbreviation: string;
description: string;
gcap_status?: string;
gcap_training_airport?: boolean;
s1_twr?: boolean;
};

export async function updateTrainingStations() {
const res = await axios.get("https://raw.githubusercontent.com/VATGER-Nav/datahub/main/data.json");

const stations = res.data as DataHubStations[];

for (const station of stations) {
if (station.logon.length === 0 || station.frequency.length === 0) continue;

const gcap_class_str = station.gcap_status ?? "0";
const gcap_class_nr = gcap_class_str === "0" || gcap_class_str === "1" ? parseInt(gcap_class_str, 10) : 2;

const stationData = {
callsign: station.logon,
frequency: Number(station.frequency),
gcap_class: gcap_class_nr,
gcap_class_group: gcap_class_str,
gcap_training_airport: station.gcap_training_airport ?? false,
s1_twr: station.s1_twr ?? false,
};

const dbStation = await TrainingStation.findOne({ where: { callsign: station.logon } });
if (dbStation == null) {
await TrainingStation.create(stationData);
continue;
}

await dbStation.update(stationData);
}
}

0 comments on commit 90d44bb

Please sign in to comment.