mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-09 17:33:55 +00:00
setup api
This commit is contained in:
+43
-12
@@ -1,16 +1,34 @@
|
||||
// get config
|
||||
const config = require('./config.json');
|
||||
|
||||
// dependencies
|
||||
const express = require('express');
|
||||
const http = require('http');
|
||||
const socketIo = require('socket.io');
|
||||
const sampleData = require('./data/sampleData.js');
|
||||
|
||||
// TODO: implement timer
|
||||
// TODO: - Play / pause
|
||||
// Import Routes
|
||||
const eventsRouter = require('./routes/eventsRouter.js');
|
||||
const playbackRouter = require('./routes/playbackRouter.js');
|
||||
|
||||
// TODO: Move to config file
|
||||
// Setup default port
|
||||
const port = process.env.PORT || 4001;
|
||||
const index = require('./routes/index');
|
||||
|
||||
// Create express APP
|
||||
const app = express();
|
||||
app.use(index);
|
||||
|
||||
// Implement middleware
|
||||
// ---
|
||||
|
||||
// Implement route endpoints
|
||||
app.use('/events', eventsRouter);
|
||||
app.use('/playback', playbackRouter);
|
||||
|
||||
// Implement route for errors
|
||||
app.use((err, req, res, next) => {
|
||||
console.error(err.stack);
|
||||
res.status(500).send('Something broke!');
|
||||
});
|
||||
|
||||
// create HTTP server
|
||||
const server = http.createServer(app);
|
||||
@@ -23,28 +41,41 @@ const io = socketIo(server, {
|
||||
},
|
||||
});
|
||||
|
||||
// timer stuff, for now
|
||||
let timer = 5400;
|
||||
const Timer = require('./timer.js');
|
||||
// TODO: this should be replaced by some sort of calculation
|
||||
let durationForNow = 5400;
|
||||
let timer = new Timer(durationForNow);
|
||||
|
||||
// transmit
|
||||
// interval function
|
||||
let interval;
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
// send initial eventdata to any new clients
|
||||
console.log('New client connected');
|
||||
socket.emit('eventdata', sampleData);
|
||||
socket.emit('timer', timer.getObject());
|
||||
|
||||
// avoid multiple intervals
|
||||
if (interval) {
|
||||
clearInterval(interval);
|
||||
}
|
||||
interval = setInterval(() => getApiAndEmit(socket), 1000);
|
||||
|
||||
// set callback for timer events
|
||||
interval = setInterval(() => getApiAndEmit(socket), config.timer.refresh);
|
||||
|
||||
// handle client disconnect
|
||||
socket.on('disconnect', () => {
|
||||
console.log('Client disconnected');
|
||||
clearInterval(interval);
|
||||
});
|
||||
});
|
||||
|
||||
// send timer events
|
||||
const getApiAndEmit = (socket) => {
|
||||
socket.emit('timerSeconds', timer);
|
||||
if (timer > 0) timer = timer - 1;
|
||||
const t = timer.getObject();
|
||||
// console.log(t)
|
||||
// send current timer
|
||||
socket.emit('timer', t);
|
||||
};
|
||||
|
||||
// Start server
|
||||
server.listen(port, () => console.log(`Listening on port ${port}`));
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"timer": {
|
||||
"refresh": 1000
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// import json with initial data
|
||||
const events = require('../data/eventsData.json');
|
||||
|
||||
// Create controller for GET request to '/events'
|
||||
// Returns ACK message
|
||||
exports.eventsGet = async (req, res) => {
|
||||
res.send({ response: 'Events Controller API' });
|
||||
};
|
||||
|
||||
// Create controller for GET request to '/events/all'
|
||||
// Returns playback state object
|
||||
exports.eventsGetAll = async (req, res) => {
|
||||
res.json(events);
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
// import json with initial data
|
||||
const playbackState = require('../data/playbackData.json');
|
||||
|
||||
// Create controller for GET request to '/pb'
|
||||
// Returns ACK message
|
||||
exports.pbGet = async (req, res) => {
|
||||
// ?? Do i need to wrap this in an object?
|
||||
res.send({ response: 'Playback Controller API' });
|
||||
};
|
||||
|
||||
// Create controller for GET request to '/pb/all'
|
||||
// Returns playback state object
|
||||
exports.pbGetAll = async (req, res) => {
|
||||
res.json(playbackState);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
[
|
||||
{
|
||||
"id": "1",
|
||||
"order": 1,
|
||||
"title": "Is the internet a fad?",
|
||||
"subtitle": "It is",
|
||||
"presenter": "Carlos Valente",
|
||||
"timeStart": "",
|
||||
"timeEnd":"",
|
||||
"type": "event"
|
||||
},
|
||||
{
|
||||
"id": 0.4849093424577693,
|
||||
"order": 2,
|
||||
"timerDuration": 25,
|
||||
"type": "delay"
|
||||
},
|
||||
{
|
||||
"id": "2",
|
||||
"order": 3,
|
||||
"title": "Is reddit a dictatorship?",
|
||||
"subtitle": "It is",
|
||||
"presenter": "Carlos Valente",
|
||||
"timeStart": "",
|
||||
"timeEnd":"",
|
||||
"type": "event"
|
||||
},
|
||||
{
|
||||
"id": "3",
|
||||
"order": 4,
|
||||
"title": "Out of words",
|
||||
"subtitle": "",
|
||||
"presenter": "Carlos Valente",
|
||||
"timeStart": "",
|
||||
"timeEnd":"",
|
||||
"type": "event"
|
||||
},
|
||||
{
|
||||
"id": "4",
|
||||
"order": 5,
|
||||
"title": "Really",
|
||||
"subtitle": "",
|
||||
"presenter": "Carlos Valente",
|
||||
"timeStart": "",
|
||||
"timeEnd":"",
|
||||
"type": "event"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"current": null,
|
||||
"next": null,
|
||||
"currentTimer": null,
|
||||
"numEvents": 0,
|
||||
"state": "pause",
|
||||
"prevState": "pause"
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
const dummy = new Date();
|
||||
|
||||
const sampleData = [
|
||||
{
|
||||
id: '1',
|
||||
order: 1,
|
||||
title: 'Is the internet a fad?',
|
||||
subtitle: 'It is',
|
||||
presenter: 'Carlos Valente',
|
||||
timeStart: new Date('October 13, 2014 11:30:00'),
|
||||
timeEnd: new Date('October 13, 2014 11:50:00'),
|
||||
clockStarted: dummy,
|
||||
timerDuration: 60,
|
||||
type: 'event',
|
||||
},
|
||||
{
|
||||
id: 0.4849093424577693,
|
||||
order: 2,
|
||||
timerDuration: 25,
|
||||
type: 'delay',
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
order: 3,
|
||||
title: 'Is reddit a dictatorship?',
|
||||
subtitle: 'It is',
|
||||
presenter: 'Carlos Valente',
|
||||
timeStart: new Date('October 13, 2014 12:30:00'),
|
||||
timeEnd: new Date('October 13, 2014 12:50:00'),
|
||||
clockStarted: dummy,
|
||||
timerDuration: 60,
|
||||
type: 'event',
|
||||
},
|
||||
{
|
||||
id: '3',
|
||||
order: 4,
|
||||
title: 'Out of words',
|
||||
subtitle: '',
|
||||
presenter: 'Carlos Valente',
|
||||
timeStart: new Date('October 13, 2014 13:30:00'),
|
||||
timeEnd: new Date('October 13, 2014 13:50:00'),
|
||||
clockStarted: dummy,
|
||||
timerDuration: 60,
|
||||
type: 'event',
|
||||
},
|
||||
];
|
||||
|
||||
module.exports = sampleData;
|
||||
@@ -0,0 +1,19 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const router = express.Router();
|
||||
|
||||
// Cross origin stuff
|
||||
const corsOptions = {
|
||||
origin: 'http://localhost',
|
||||
}
|
||||
|
||||
// import playback controller
|
||||
const eventsController = require('../controllers/eventsController');
|
||||
|
||||
// create route between controller and '/events' endpoint
|
||||
router.get('/', cors(corsOptions), eventsController.eventsGet);
|
||||
|
||||
// create route between controller and '/events/all' endpoint
|
||||
router.get('/all', cors(corsOptions), eventsController.eventsGetAll);
|
||||
|
||||
module.exports = router;
|
||||
@@ -1,8 +0,0 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
res.send({ response: 'I am alive' }).status(200);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,19 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const router = express.Router();
|
||||
|
||||
// Cross origin stuff
|
||||
const corsOptions = {
|
||||
origin: 'http://localhost',
|
||||
};
|
||||
|
||||
// import event controller
|
||||
const playbackController = require('../controllers/playbackController');
|
||||
|
||||
// create route between controller and '/playback' endpoint
|
||||
router.get('/', cors(corsOptions), playbackController.pbGet);
|
||||
|
||||
// create route between controller and '/playback/all' endpoint
|
||||
router.get('/all', cors(corsOptions), playbackController.pbGetAll);
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,56 @@
|
||||
class Timer {
|
||||
constructor(durationInSeconds) {
|
||||
const now = new Date().getTime();
|
||||
this.duration = durationInSeconds;
|
||||
this.current = durationInSeconds;
|
||||
this.finish = new Date().getTime() + durationInSeconds * 1000;
|
||||
this.started = now;
|
||||
}
|
||||
|
||||
// update()
|
||||
update() {
|
||||
// doesnt include start time yet
|
||||
const now = new Date().getTime();
|
||||
this.current = (this.finish - now) * 0.001;
|
||||
}
|
||||
|
||||
// getObject
|
||||
getObject() {
|
||||
this.update();
|
||||
return {
|
||||
duration: this.duration,
|
||||
current: this.current,
|
||||
finish: this.finish,
|
||||
started: this.started,
|
||||
};
|
||||
}
|
||||
|
||||
// duration
|
||||
getDuration(durationInSeconds) {
|
||||
return this.duration;
|
||||
}
|
||||
setDuration() {
|
||||
try {
|
||||
this.duration = durationInSeconds;
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// current time in seconds
|
||||
getCurrentInSeconds() {
|
||||
// update timeStamp
|
||||
this.update();
|
||||
return this.current;
|
||||
}
|
||||
|
||||
// playback
|
||||
start() {}
|
||||
pause() {}
|
||||
// playback
|
||||
stop() {}
|
||||
}
|
||||
|
||||
module.exports = Timer;
|
||||
Reference in New Issue
Block a user