filo_caspar/api/content/routes.js

100 lines
2 KiB
JavaScript
Raw Normal View History

2016-04-10 08:37:05 +00:00
import _ from 'lodash'
2018-06-26 18:35:12 +00:00
import Content from './model'
2016-04-10 08:37:05 +00:00
2016-04-14 04:01:51 +00:00
export const active = { }
function getSocket(ctx, all) {
if (all === true) return ctx.io
return ctx.socket
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'content.display'
*
* Display a specific graphic content
*/
export async function display(ctx, data) {
2016-04-14 04:01:51 +00:00
let compiled = _.template(data.graphic.settings.html)
let html = compiled(data.data)
2016-04-10 08:37:05 +00:00
2018-06-26 18:35:12 +00:00
let old = await Content.getSingle(data.graphic.name)
if (old) {
await old.destroy()
}
2016-04-14 04:01:51 +00:00
let payload = {
graphic: data.graphic,
2018-06-26 18:35:12 +00:00
name: data.graphic.name,
html: html || '',
css: data.graphic.settings.css || '',
2016-04-14 04:01:51 +00:00
data: data.data,
2018-06-26 18:35:12 +00:00
is_deleted: false,
2016-04-14 04:01:51 +00:00
}
2018-06-26 18:35:12 +00:00
let content = await Content.create(payload)
ctx.io.emit('client.display', content.toJSON())
2016-04-14 04:01:51 +00:00
list(ctx, true)
2016-04-10 08:37:05 +00:00
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'content.hide'
*
* Hide a specific graphic content
*/
export async function hide(ctx, data) {
let content = await Content.getSingle(data.name)
if (!content) return
await content.destroy()
2016-04-14 04:01:51 +00:00
2016-04-10 08:37:05 +00:00
ctx.io.emit('client.hide', {
2016-04-14 04:01:51 +00:00
name: data.name,
2016-04-10 08:37:05 +00:00
})
2016-04-14 04:01:51 +00:00
list(ctx, true)
}
function generateDisplayText(item) {
2018-06-26 18:35:12 +00:00
// if (item.graphic.engine === 'countdown') {
// return `${item.data.text} - ${item.data.finished}`
// }
try {
return _.template(item.graphic.settings.main)(item.data)
} catch (e) {
return `Error creating display: ${e.message}`
2016-04-14 04:01:51 +00:00
}
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'content.list'
* Runs on start of every new connection
*
* Send a name list of all active graphics
*/
export async function list(ctx, all) {
let allContent = await Content.getAll()
let payload = await Promise.all(allContent.map(item => ({
name: item.get('name'),
display: generateDisplayText(item.toJSON()),
})))
2016-04-14 04:01:51 +00:00
getSocket(ctx, all).emit('content.list', payload)
}
2018-06-26 18:35:12 +00:00
/*
* Event: 'content.list'
* Runs on start of every new connection
*
* Send actual graphics of all active graphics
*/
export async function reset(ctx) {
let allContent = await Content.getAll()
ctx.socket.emit('client.reset', allContent.toJSON())
2016-04-10 08:37:05 +00:00
}