Skip to content
Snippets Groups Projects
Commit 4031e242 authored by Hermod's avatar Hermod
Browse files

wq

Merge branch 'master' into feat/badge-reader
parents 3b3f21e3 2f0279aa
Branches
No related tags found
No related merge requests found
......@@ -16,9 +16,6 @@ class App extends Component {
onReceiveMessage = (data) => {
this.setState({ data });
setTimeout(() => {
socketIO.emit('panel_data');
}, data.ttl);
};
render() {
......
......@@ -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')),
......
{
"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" }]
}
]
}
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 });
// 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}`, {
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))
.catch(console.log);
});
.then((res) => {
socket.emit('panel_data', res);
return res.ttl;
})
.catch(console.log),
0,
);
// 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' }],
},
],
// Respond to date message with the date
socket.on('date', () => {
socket.emit('date', { date: Date.now() });
});
socket.on('disconnect', () => {
chrono.clear();
});
});
......@@ -4,10 +4,16 @@ const { cert } = require('./config');
const interval = (fn, initialTTL, output = { id: null, clear: () => undefined }) => {
const timeoutID = setTimeout(async () => {
let TTL = fn();
let TTL;
try {
TTL = fn();
if (TTL instanceof Promise) {
TTL = await TTL;
}
} catch (error) {
console.error(error);
TTL = initialTTL;
}
const nextTTL = parseInt(TTL, 10) || initialTTL;
interval(fn, nextTTL, output);
}, initialTTL);
......@@ -28,6 +34,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 };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment