ffplayout/store/config.js

182 lines
5.7 KiB
JavaScript
Raw Normal View History

2021-03-23 13:56:31 +01:00
import _ from 'lodash'
2020-04-09 18:30:28 +02:00
export const state = () => ({
configID: 0,
configCount: 0,
2020-04-15 18:11:29 +02:00
configGui: null,
2020-12-30 22:08:54 +01:00
configGuiRaw: null,
2020-04-15 18:11:29 +02:00
netChoices: [],
2021-01-28 15:51:35 +01:00
startInSec: null,
playlistLength: 86400.0,
configPlayout: [],
2020-04-15 18:11:29 +02:00
currentUser: null,
2021-03-23 13:56:31 +01:00
configUser: null,
timezone: 'UTC'
2020-04-09 18:30:28 +02:00
})
export const mutations = {
UPDATE_CONFIG_ID (state, id) {
state.configID = id
},
UPDATE_CONFIG_COUNT (state, count) {
state.configCount = count
},
2020-04-15 18:11:29 +02:00
UPDATE_GUI_CONFIG (state, config) {
state.configGui = config
},
2020-12-30 22:08:54 +01:00
UPDATE_GUI_CONFIG_RAW (state, config) {
state.configGuiRaw = config
},
2020-04-15 18:11:29 +02:00
UPDATE_NET_CHOICES (state, list) {
state.netChoices = list
},
2021-01-28 15:51:35 +01:00
UPDATE_START_TIME (state, sec) {
state.startInSec = sec
},
UPDATE_PLAYLIST_LENGTH (state, sec) {
state.playlistLength = sec
},
2020-04-15 18:11:29 +02:00
UPDATE_PLAYLOUT_CONFIG (state, config) {
state.configPlayout = config
},
SET_CURRENT_USER (state, user) {
state.currentUser = user
},
UPDATE_USER_CONFIG (state, config) {
state.configUser = config
2021-03-23 13:56:31 +01:00
},
UPDATE_TIMEZONE (state, zone) {
state.timezone = zone
2020-04-09 18:30:28 +02:00
}
}
export const actions = {
2020-05-24 13:34:34 +02:00
async nuxtClientInit ({ commit, dispatch, rootState }) {
2020-05-24 17:51:28 +02:00
await dispatch('auth/inspectToken', null, { root: true })
2020-05-24 13:34:34 +02:00
if (rootState.auth.isLogin) {
2021-03-23 13:56:31 +01:00
await dispatch('getTimezone')
2020-05-24 13:34:34 +02:00
await dispatch('getGuiConfig')
await dispatch('getPlayoutConfig')
await dispatch('getUserConfig')
}
},
2021-03-23 13:56:31 +01:00
async getTimezone ({ commit, state }) {
const response = await this.$axios.get('api/player/stats/?stats=timezone')
if (response.data) {
commit('UPDATE_TIMEZONE', response.data.timezone)
} else {
commit('UPDATE_TIMEZONE', this.$dayjs.tz.guess())
}
},
2020-04-30 10:53:13 +02:00
async getGuiConfig ({ commit, state }) {
const options = await this.$axios.options('api/player/guisettings/')
const response = await this.$axios.get('api/player/guisettings/')
2020-04-15 18:11:29 +02:00
if (options.data) {
const choices = options.data.actions.POST.net_interface.choices.map(function (obj) {
obj.text = obj.display_name
delete obj.display_name
return obj
})
commit('UPDATE_NET_CHOICES', choices)
}
if (response.data && response.data[0]) {
for (const data of response.data) {
if (data.extra_extensions) {
data.extra_extensions = data.extra_extensions.split(',')
} else {
data.extra_extensions = []
}
}
commit('UPDATE_GUI_CONFIG', response.data)
2021-03-23 13:56:31 +01:00
commit('UPDATE_GUI_CONFIG_RAW', _.cloneDeep(response.data))
commit('UPDATE_CONFIG_COUNT', response.data.length)
} else {
commit('UPDATE_GUI_CONFIG', [{
id: 1,
channel: '',
player_url: '',
playout_config: '',
net_interface: '',
media_disk: '',
extra_extensions: []
}])
2020-04-15 18:11:29 +02:00
}
},
async setGuiConfig ({ commit, state, dispatch }, obj) {
2021-03-23 13:56:31 +01:00
const stringObj = _.cloneDeep(obj)
stringObj.extra_extensions = stringObj.extra_extensions.join(',')
let response
2020-12-30 22:08:54 +01:00
if (state.configGuiRaw.some(e => e.id === stringObj.id)) {
response = await this.$axios.put(`api/player/guisettings/${obj.id}/`, stringObj)
} else {
2020-12-30 22:08:54 +01:00
response = await this.$axios.post('api/player/guisettings/', stringObj)
const guiConfigs = []
for (const obj of state.configGui) {
if (obj.channel === stringObj.channel) {
response.data.extra_extensions = response.data.extra_extensions.split(',')
guiConfigs.push(response.data)
} else {
guiConfigs.push(obj)
}
}
commit('UPDATE_GUI_CONFIG', guiConfigs)
2021-03-23 13:56:31 +01:00
commit('UPDATE_GUI_CONFIG_RAW', _.cloneDeep(guiConfigs))
commit('UPDATE_CONFIG_COUNT', guiConfigs.length)
await dispatch('getPlayoutConfig')
}
2020-05-24 13:34:34 +02:00
return response
2020-04-15 18:11:29 +02:00
},
2020-04-30 10:53:13 +02:00
async getPlayoutConfig ({ commit, state }) {
2020-12-30 22:08:54 +01:00
const path = state.configGui[state.configID].playout_config
const response = await this.$axios.get(`api/player/config/?configPlayout&path=${path}`)
2020-04-15 18:11:29 +02:00
if (response.data) {
2021-01-28 15:51:35 +01:00
if (response.data.playlist.day_start) {
commit('UPDATE_START_TIME', this.$timeToSeconds(response.data.playlist.day_start))
}
if (response.data.playlist.length) {
commit('UPDATE_PLAYLIST_LENGTH', this.$timeToSeconds(response.data.playlist.length))
}
2020-04-15 18:11:29 +02:00
commit('UPDATE_PLAYLOUT_CONFIG', response.data)
}
},
2020-04-30 10:53:13 +02:00
async setPlayoutConfig ({ commit, state }, obj) {
2021-01-03 21:44:25 +01:00
const path = state.configGui[state.configID].playout_config
const update = await this.$axios.post(`api/player/config/?configPlayout&path=${path}`, { data: obj })
2020-04-16 18:29:44 +02:00
return update
2020-04-15 18:11:29 +02:00
},
2020-04-09 18:30:28 +02:00
2020-04-30 10:53:13 +02:00
async getUserConfig ({ commit, state }) {
2020-05-04 12:16:04 +02:00
const user = await this.$axios.get('api/player/user/current/')
const response = await this.$axios.get(`api/player/user/users/?username=${user.data.username}`)
2020-04-15 18:11:29 +02:00
if (user.data) {
commit('SET_CURRENT_USER', user.data.username)
}
2020-04-09 18:30:28 +02:00
if (response.data) {
2020-04-16 18:29:44 +02:00
commit('UPDATE_USER_CONFIG', response.data[0])
2020-04-09 18:30:28 +02:00
}
},
2020-04-30 10:53:13 +02:00
async setUserConfig ({ commit, state }, obj) {
2020-05-04 12:16:04 +02:00
const update = await this.$axios.put(`api/player/user/users/${obj.id}/`, obj)
2020-04-16 18:29:44 +02:00
return update
2020-04-09 18:30:28 +02:00
}
}