Merge branch 'master' into feat/info

This commit is contained in:
Carlos Valente
2021-06-13 14:34:20 +02:00
committed by GitHub
71 changed files with 6100 additions and 754 deletions
+7 -1
View File
@@ -1,7 +1,13 @@
import { Server } from 'node-osc';
let oscServer = null;
export const shutdownOSCServer = () => {
if (oscServer != null) oscServer.close();
};
export const initiateOSC = (config) => {
const oscServer = new Server(config.port, '0.0.0.0', () => {
oscServer = new Server(config.port, '0.0.0.0', () => {
console.log(`OSC Server is listening on port ${config.port}`);
});
+40 -13
View File
@@ -1,6 +1,9 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// get database
import { db, data } from '../app.js';
import fs from 'fs';
import {
event as eventDef,
delay as delayDef,
@@ -9,6 +12,9 @@ import {
import { dbModel } from '../data/dataModel.js';
import { networkInterfaces } from 'os';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function getEventTitle() {
return data.event.title;
}
@@ -24,6 +30,7 @@ async function deleteFile(file) {
// parses version 1 of the data system
async function parsev1(jsonData) {
let numEntries = 0;
if ('events' in jsonData) {
let events = [];
let ids = [];
@@ -46,15 +53,19 @@ async function parsev1(jsonData) {
isPublic: e.isPublic,
id: e.id,
});
numEntries++;
} else if (e.type === 'delay') {
events.push({ ...delayDef, duration: e.duration });
numEntries++;
} else if (e.type === 'block') {
events.push({ ...blockDef });
numEntries++;
}
}
// write to db
db.data.events = events;
db.write();
console.log(`Uploaded file with ${numEntries} entries`);
}
if ('event' in jsonData) {
@@ -83,7 +94,9 @@ async function parsev1(jsonData) {
// Returns -
export const dbDownload = async (req, res) => {
const fileTitle = getEventTitle() || 'ontime events';
res.download('db.json', `${fileTitle}.json`, (err) => {
const dbFile = path.resolve(__dirname, '../', 'data/db.json');
res.download(dbFile, `${fileTitle}.json`, (err) => {
if (err) {
res.status(500).send({
message: 'Could not download the file. ' + err,
@@ -92,15 +105,7 @@ export const dbDownload = async (req, res) => {
});
};
// Create controller for POST request to '/ontime/db'
// Returns -
export const dbUpload = async (req, res) => {
if (!req.file) {
res.status(400).send({ message: 'File not found' });
return;
}
const file = req.file.path;
const upload = async (file, req, res) => {
if (!fs.existsSync(file)) {
res.status(500).send({ message: 'Upload failed' });
return;
@@ -108,8 +113,8 @@ export const dbUpload = async (req, res) => {
try {
// get file
let rawdata = fs.readFileSync(file);
let uploadedJson = JSON.parse(rawdata);
const rawdata = fs.readFileSync(file);
const uploadedJson = JSON.parse(rawdata);
// delete file
deleteFile(file);
@@ -158,4 +163,26 @@ export const getInfo = async (req, res) => {
res.status(200).send({
networkInterfaces: ni,
});
// Create controller for POST request to '/ontime/db'
// Returns -
export const dbUpload = async (req, res) => {
if (!req.file) {
res.status(400).send({ message: 'File not found' });
return;
}
const file = req.file.path;
upload(file, req, res);
};
// Create controller for POST request to '/ontime/dbpath'
// Returns -
export const dbPathToUpload = async (req, res) => {
if (!req.body.path) {
res.status(400).send({ message: 'Path to file not found' });
return;
}
upload(req.body.path, req, res);
};