filo_caspar/api/casparcg/client.mjs

110 lines
2.8 KiB
JavaScript
Raw Normal View History

import CasparConnection from 'casparcg-connection'
const CasparCG = CasparConnection.CasparCG
const AMCP = CasparConnection.AMCP
2018-06-26 18:35:12 +00:00
const timeoutDuration = 5000
2018-06-26 18:35:12 +00:00
let io
let logger
let connection
let casparIsPlaying
let casparIsConnected
let currentHost
export function initialise(log, db, socket) {
2018-06-26 18:35:12 +00:00
io = socket.socket
logger = log
db = db
2018-06-26 18:35:12 +00:00
connect(db)
2018-06-26 18:35:12 +00:00
}
export function connect(db) {
currentHost = db.get('settings').value().casparhost
2018-06-26 18:35:12 +00:00
casparIsPlaying = false
casparIsConnected = false
logger.info('CasparCG: Connectiong to', currentHost + ':' + 5250)
connection = new CasparCG({
host: currentHost,
port: 5250,
queueMode: 2,
autoReconnectInterval: timeoutDuration,
onError: err => {
logger.error(err, 'CasparCG: Error')
2018-06-26 18:35:12 +00:00
},
onConnectionStatus: data => {
if (casparIsPlaying) return
2018-06-26 18:35:12 +00:00
casparIsConnected = data.connected
if (!casparIsConnected) {
logger.warn(`CasparCG: connection down, retrying in ${timeoutDuration / 1000} seconds`)
2018-06-26 18:35:12 +00:00
io.emit('casparcg.status', currentStatus())
}
},
onConnected: async connected => {
if (casparIsPlaying) return
2018-06-26 18:35:12 +00:00
logger.info('CasparCG: connected', connected)
if (!casparIsPlaying) {
startPlaying(db).then()
} else {
logger.warn('CasparCG: Stopped from starting play again.')
}
2018-06-26 18:35:12 +00:00
},
})
}
export function currentStatus(e) {
return {
connected: casparIsConnected,
playing: casparIsPlaying,
error: e,
}
}
export async function startPlaying(db) {
let ip = db.get('settings').value().casparplayhost
2018-06-26 18:35:12 +00:00
// Check if we lost connection while attempting to start playing
if (!connection.connected) {
logger.error('CasparCG: Attempted to play but connection was lost')
}
let success = false
2018-06-26 18:35:12 +00:00
try {
// Send a play command
let command = `PLAY 1-100 [HTML] "http://${ip}/client.html" CUT 1 LINEAR RIGHT`
logger.info(`CasparCG Command: ${command}`)
await connection.do(new AMCP.CustomCommand(command))
success = true
} catch (e) {
// Weird error where it throws an error despite a successful play command on reconnect
if (e && e.responseProtocol && e.responseProtocol.code >= 200 && e.responseProtocol.code < 300) {
success = true
} else {
logger.error(e, 'CasparCG: Error starting play on client')
2018-06-26 18:35:12 +00:00
}
}
2018-06-26 18:35:12 +00:00
if (success) {
2018-06-26 18:35:12 +00:00
casparIsPlaying = true
// We are playing, notify all clients
io.emit('casparcg.status', currentStatus())
logger.info('CasparCG: client is up and playing')
/* console.log(connection)
for (var key in connection) {
console.log(key, '=', typeof(connection[key]))
} */
connection.autoConnect = false
// connection.close()
} else {
2018-06-26 18:35:12 +00:00
// Unknown error occured
casparIsPlaying = false
2018-06-26 18:35:12 +00:00
io.emit('casparcg.status', currentStatus(e))
}
}