diff --git a/front/src/App.js b/front/src/App.js index 6d2009d5e4c460404bc85683f2da6d8e57444ac9..984b71302caaffe6e726ada51eda7094954c5176 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 d5aa5babdb02761483dd3844a59cd304d743e976..8955193fbe722783eb4e259361cf5c3cb7631d8b 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 0000000000000000000000000000000000000000..93d53f730b8eaa8c96053c039e35a217c8377254 --- /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 0ecace49154358f532ac79ca47df179f76ebf272..a9a740d2bb915c42911b6e26bbf0ccc144bb43cf 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 ab20147645fa2d1eb6c9701d310e0996f42a8368..7959483793ed981317bbe300dd8d6711bd4b1896 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 };