ffplayout/store/playlist.js

143 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-03-23 05:40:57 -04:00
import _ from 'lodash'
2020-04-12 15:15:10 -04:00
export const state = () => ({
2020-04-17 09:02:11 -04:00
playlist: null,
playlistToday: [],
2020-04-24 09:15:55 -04:00
progressValue: 0,
2020-04-21 11:34:37 -04:00
currentClip: 'No clips is playing',
currentClipIndex: null,
currentClipStart: null,
currentClipDuration: null,
currentClipIn: null,
currentClipOut: null,
2020-04-21 11:34:37 -04:00
timeStr: '00:00:00',
timeLeft: '00:00:00'
2020-04-12 15:15:10 -04:00
})
export const mutations = {
UPDATE_PLAYLIST (state, list) {
state.playlist = list
2020-04-17 09:02:11 -04:00
},
UPDATE_TODAYS_PLAYLIST (state, list) {
state.playlistToday = list
},
2020-04-17 09:02:11 -04:00
SET_PROGRESS_VALUE (state, value) {
2020-04-19 15:41:14 -04:00
state.progressValue = value
2020-04-17 09:02:11 -04:00
},
SET_CURRENT_CLIP (state, clip) {
state.currentClip = clip
},
SET_CURRENT_CLIP_INDEX (state, index) {
state.currentClipIndex = index
},
SET_CURRENT_CLIP_START (state, start) {
state.currentClipStart = start
},
SET_CURRENT_CLIP_DURATION (state, dur) {
state.currentClipDuration = dur
},
SET_CURRENT_CLIP_IN (state, _in) {
state.currentClipIn = _in
},
SET_CURRENT_CLIP_OUT (state, out) {
state.currentClipOut = out
},
2020-04-17 09:02:11 -04:00
SET_TIME (state, time) {
state.timeStr = time
},
SET_TIME_LEFT (state, time) {
state.timeLeft = time
2020-04-12 15:15:10 -04:00
}
}
export const actions = {
async getPlaylist ({ commit, dispatch, state, rootState }, { date }) {
2021-03-23 08:57:30 -04:00
const timeInSec = this.$timeToSeconds(this.$dayjs().tz(rootState.config.timezone).format('HH:mm:ss'))
const configPath = rootState.config.configGui[rootState.config.configID].playout_config
2021-03-23 08:57:30 -04:00
let dateToday = this.$dayjs().tz(this.timezone).format('YYYY-MM-DD')
if (rootState.config.startInSec > timeInSec) {
2021-03-23 08:57:30 -04:00
dateToday = this.$dayjs(dateToday).tz(rootState.config.timezone).subtract(1, 'day').format('YYYY-MM-DD')
}
2021-03-18 13:16:47 -04:00
const response = await this.$axios.get(`api/player/playlist/?date=${date}&config_path=${configPath}`)
console.log(rootState.config.configPlayout.playlist.day_start)
2020-04-12 15:15:10 -04:00
2020-04-13 15:34:37 -04:00
if (response.data && response.data.program) {
commit('UPDATE_PLAYLIST', this.$processPlaylist(rootState.config.startInSec, response.data.program))
2020-04-21 09:28:31 -04:00
if (date === dateToday) {
2021-03-23 05:40:57 -04:00
commit('UPDATE_TODAYS_PLAYLIST', _.cloneDeep(response.data.program))
dispatch('setCurrentClip')
} else {
commit('SET_CURRENT_CLIP_INDEX', null)
2020-04-21 09:28:31 -04:00
}
2020-04-24 09:15:55 -04:00
} else {
commit('UPDATE_PLAYLIST', [])
2020-04-17 09:02:11 -04:00
}
},
setCurrentClip ({ commit, dispatch, state, rootState }) {
let begin
2021-03-23 08:57:30 -04:00
let lastTime = this.$timeToSeconds(this.$dayjs().tz(rootState.config.timezone).format('HH:mm:ss'))
2021-02-02 03:15:44 -05:00
if (Number.isFinite(rootState.config.startInSec)) {
begin = rootState.config.startInSec
} else {
commit('SET_CURRENT_CLIP', 'day_start is not set, cannot calculate current clip')
return
}
if (lastTime < begin) {
lastTime += rootState.config.playlistLength
}
for (let i = 0; i < state.playlistToday.length; i++) {
const duration = state.playlistToday[i].out - state.playlistToday[i].in
// animate the progress bar
if (lastTime < begin + duration) {
const progValue = (lastTime - begin) * 100 / duration
commit('SET_PROGRESS_VALUE', progValue)
2020-04-30 13:03:44 -04:00
commit('SET_CURRENT_CLIP', state.playlistToday[i].source)
commit('SET_CURRENT_CLIP_INDEX', i)
commit('SET_CURRENT_CLIP_START', begin)
commit('SET_CURRENT_CLIP_DURATION', duration)
commit('SET_CURRENT_CLIP_IN', state.playlistToday[i].in)
commit('SET_CURRENT_CLIP_OUT', state.playlistToday[i].out)
break
2020-04-17 09:02:11 -04:00
}
begin += duration
2020-04-12 15:15:10 -04:00
}
},
animClock ({ commit, dispatch, state, rootState }) {
2021-03-23 08:57:30 -04:00
const time = this.$dayjs().tz(rootState.config.timezone).format('HH:mm:ss')
let timeSec = this.$timeToSeconds(time)
commit('SET_TIME', time)
if (timeSec < rootState.config.startInSec) {
timeSec += rootState.config.playlistLength
}
if (timeSec < state.currentClipStart) {
return
}
const playTime = timeSec - state.currentClipStart
const progValue = playTime * 100 / state.currentClipDuration
// set progress bar value
if (playTime <= state.currentClipDuration && progValue >= 0) {
commit('SET_PROGRESS_VALUE', progValue)
commit('SET_TIME_LEFT', this.$secToHMS(state.currentClipDuration - playTime))
} else {
commit('SET_PROGRESS_VALUE', 0)
dispatch('setCurrentClip')
}
2020-04-12 15:15:10 -04:00
}
}