infoscreens/infodisplay/infodisplay.js

97 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-11-28 22:30:37 +01:00
const { msgflo } = window.infodisplay;
let timeout = null;
2017-11-28 22:21:35 +01:00
2017-11-28 22:30:37 +01:00
function getRotationUrl(urls, current) {
const newUrl = urls[Math.floor(Math.random() * urls.length)];
2017-11-28 22:21:35 +01:00
if (newUrl === current && urls.length > 1) {
// Flip the coin again
return getRotationUrl(urls, current);
}
return newUrl;
2017-11-28 22:30:37 +01:00
}
2017-11-28 22:21:35 +01:00
2017-11-28 22:30:37 +01:00
function DisplayParticipant(broker, role, defaultUrls, timer) {
let urls = defaultUrls;
let participant;
const def = {
2017-11-28 22:21:35 +01:00
component: 'msgflo-browser/infodisplay',
label: 'Browser-based information display',
icon: 'television',
inports: [
{
id: 'open',
2017-11-28 22:30:37 +01:00
type: 'string',
2017-11-28 22:21:35 +01:00
},
{
id: 'urls',
2017-11-28 22:30:37 +01:00
type: 'array',
},
2017-11-28 22:21:35 +01:00
],
outports: [
{
id: 'opened',
2017-11-28 22:30:37 +01:00
type: 'string',
2017-11-28 22:21:35 +01:00
},
{
id: 'urls',
type: 'array',
2017-11-28 22:30:37 +01:00
hidden: true,
},
],
2017-11-28 22:21:35 +01:00
};
2017-11-28 22:30:37 +01:00
const process = (inport, indata, callback) => {
const current = document.getElementById('current');
const next = document.getElementById('next');
2017-11-28 22:21:35 +01:00
if (inport === 'urls') {
// Update URL listing
urls = indata;
2017-11-28 22:30:37 +01:00
callback('urls', null, urls);
2017-11-28 22:21:35 +01:00
}
2017-11-28 22:30:37 +01:00
next.onerror = (err) => {
2017-11-28 22:21:35 +01:00
next.onload = null;
next.onerror = null;
participant.send('open', getRotationUrl(urls, indata));
2017-11-28 22:30:37 +01:00
callback('open', err);
};
next.onload = () => {
2017-11-28 22:21:35 +01:00
next.onload = null;
next.onerror = null;
// Cross-fade
next.id = 'current';
current.id = 'next';
if (timeout) {
clearTimeout(timeout);
}
2017-11-28 22:30:37 +01:00
timeout = setTimeout(() => {
2017-11-28 22:21:35 +01:00
participant.send('open', getRotationUrl(urls, indata));
}, timer);
2017-11-28 22:30:37 +01:00
callback('opened', null, next.getAttribute('src'));
2017-11-28 22:21:35 +01:00
};
next.setAttribute('src', indata);
// Rotate internal URLs list
};
2017-11-28 22:30:37 +01:00
const client = new msgflo.mqtt.Client(broker, {});
participant = new msgflo.participant.Participant(client, def, process, role);
2017-11-28 22:21:35 +01:00
return participant;
}
2017-11-28 22:30:37 +01:00
window.addEventListener('load', () => {
const params = msgflo.options({
2017-11-28 22:21:35 +01:00
broker: 'mqtt://c-beam.cbrp3.c-base.org:1882',
role: 'infodisplay',
urls: [
'http://c-beam.cbrp3.c-base.org/he1display',
2017-11-28 22:30:37 +01:00
'https://c-base.org',
2017-11-28 22:21:35 +01:00
],
timer: 120000,
});
2017-11-28 22:30:37 +01:00
const p = DisplayParticipant(params.broker, params.role, params.urls, params.timer);
p.start((err) => {
2017-11-28 22:21:35 +01:00
if (err) {
console.error(err);
return;
}
p.send('open', getRotationUrl(params.urls));
});
}, false);