From 6318ba50a3cd5c35ac46404509b2444c4574cd0e Mon Sep 17 00:00:00 2001 From: Ayc0 <benjamin.koltes@gmail.com> Date: Tue, 27 Mar 2018 14:43:30 +0200 Subject: [PATCH] Set chrono in back --- front/src/App.js | 3 -- server/config.template.js | 4 +++ server/dummyResponse.json | 27 +++++++++++++++ server/index.js | 72 ++++++++++++++++----------------------- server/utils.js | 3 +- 5 files changed, 62 insertions(+), 47 deletions(-) create mode 100644 server/dummyResponse.json diff --git a/front/src/App.js b/front/src/App.js index 6d2009d..984b713 100644 --- a/front/src/App.js +++ b/front/src/App.js @@ -16,9 +16,6 @@ class App extends Component { onReceiveMessage = (data) => { this.setState({ data }); - setTimeout(() => { - socketIO.emit('panel_data'); - }, data.ttl); }; render() { diff --git a/server/config.template.js b/server/config.template.js index d5aa5ba..8955193 100644 --- a/server/config.template.js +++ b/server/config.template.js @@ -4,6 +4,10 @@ const path = require('path'); module.exports = { port: 5000, uuid: 'UUID A REMPLIR', + api: { + url: 'http://api.hermod.cs-campus.fr', + version: 'v1', + }, fontSize: '4vh', rowHeight: '100px', cert: fs.readFileSync(path.resolve(__dirname, '../key.pem')), diff --git a/server/dummyResponse.json b/server/dummyResponse.json new file mode 100644 index 0000000..93d53f7 --- /dev/null +++ b/server/dummyResponse.json @@ -0,0 +1,27 @@ +{ + "version": "0.0.4", + "ttl": 300000, + "rows": [ + { + "type": 1, + "image": "https://people.via.ecp.fr/~bebert/toddy.jpg", + "text": [ + { "text": "What I want to display", "style": { "color": "#000000" } }, + { "text": "42", "style": { "color": "#BB8800", "fontWeight": "bold" } } + ] + }, + { + "type": 0, + "text": [ + { "text": "Some text", "style": { "color": "#000000" } }, + { "text": "66", "style": { "color": "#BB8800", "fontWeight": "bold" } } + ] + }, + { + "type": 2, + "image": "https://people.via.ecp.fr/~bebert/chatenay.jpg", + "heightFactor": 3, + "text": [{ "text": "Nope" }] + } + ] +} diff --git a/server/index.js b/server/index.js index 0ecace4..a9a740d 100644 --- a/server/index.js +++ b/server/index.js @@ -1,60 +1,46 @@ const fetch = require('node-fetch'); const { - uuid, fontSize, rowHeight, port, + uuid, fontSize, rowHeight, port, api, } = require('./config'); +const dummyResponse = require('./dummyResponse.json'); + const io = require('socket.io')(port || 3000); -const { createSignedJWT } = require('./utils'); +const { createSignedJWT, interval } = require('./utils'); + +const useDummy = false; io.of('/').on('connection', (socket) => { socket.emit('config', { fontSize, rowHeight }); + const chrono = useDummy + ? interval(() => { + socket.emit('panel_data', dummyResponse); + return 15000; + }, 0) + : interval( + () => + fetch(`${api.url}/${api.version}/screen/${uuid}`, { + headers: { + Autorization: `Bearer ${createSignedJWT()}`, + }, + }) + .then(rawRes => rawRes.json()) + .then((res) => { + socket.emit('panel_data', res); + return res.ttl; + }) + .catch(console.log), + 0, + ); + // Respond to date message with the date socket.on('date', () => { socket.emit('date', { date: Date.now() }); }); - // Respond to panel_data message by fetching the info from the server - socket.on('panel_data', () => { - fetch(`http://api.hermod.cs-campus.fr/v1/screen/${uuid}`, { - headers: { - Autorization: `Bearer ${createSignedJWT()}`, - }, - }) - .then(rawRes => rawRes.json()) - .then(res => socket.emit('panel_data', res)) - .catch(console.log); - }); - - // TEMP Respond to panel_data message by fetching the info from the server - socket.on('panel_data0', () => { - socket.emit('panel_data', { - version: '0.0.4', - ttl: 300000, - rows: [ - { - type: 1, - image: 'https://people.via.ecp.fr/~bebert/toddy.jpg', - text: [ - { text: 'What I want to display', style: { color: '#000000' } }, - { text: '42', style: { color: '#BB8800', fontWeight: 'bold' } }, - ], - }, - { - type: 0, - text: [ - { text: 'Some text', style: { color: '#000000' } }, - { text: '66', style: { color: '#BB8800', fontWeight: 'bold' } }, - ], - }, - { - type: 2, - image: 'https://people.via.ecp.fr/~bebert/chatenay.jpg', - heightFactor: 3, - text: [{ text: 'Nope' }], - }, - ], - }); + socket.on('disconnect', () => { + chrono.clear(); }); }); diff --git a/server/utils.js b/server/utils.js index ab20147..7959483 100644 --- a/server/utils.js +++ b/server/utils.js @@ -28,6 +28,7 @@ const generateRandomString = (length) => { return text; }; -const createSignedJWT = () => jwt.sign({ state: generateRandomString(20) }, cert, { algorithm: 'RS256' }); +const createSignedJWT = () => + jwt.sign({ state: generateRandomString(20) }, cert, { algorithm: 'RS256' }); module.exports = { interval, createSignedJWT }; -- GitLab