actions.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import Vue from 'vue'
  2. import router from '../router'
  3. import { API_ROUTES, buildURI, buildWsURI, delay } from '../functions'
  4. export default {
  5. setGdprValidated({ state, commit }) {
  6. if (!state.gdprConsent) {
  7. commit('setGdprValidated')
  8. router.push('/hostConfig')
  9. }
  10. },
  11. setAppUniqueId({ state, commit }) {
  12. if (!state.uuid) commit('setAppUniqueId')
  13. },
  14. resetApp({ commit }, { gdprConsent = false, hostConfig = false, progression = false }) {
  15. commit('resetApp', { gdprConsent, hostConfig, progression })
  16. },
  17. async setHostConfig({ state, commit }, { ssl, host, port }) {
  18. // Timeout after 1s
  19. const controller = new AbortController()
  20. const signal = controller.signal
  21. setTimeout(() => controller.abort(), 1500)
  22. const URI = buildURI(ssl, host, port, API_ROUTES.ping())
  23. return fetch(URI, { signal })
  24. .then(async res => {
  25. if (res.status !== 200) throw new Error(`Received wrong HTTP status code : ${res.status} (Need 200).`)
  26. const content = await res.text()
  27. if (content !== 'pong') throw new Error('Received wrong web content (Need to receive "pong").')
  28. this._vm.$connect(buildWsURI(ssl, host, port))
  29. // $connect does not return a Promise, so we wait to know if it worked
  30. await delay(300)
  31. if (!state.socket.isConnected)
  32. throw new Error('Could not connect to remote WebSocket server.')
  33. // Configuration is valid
  34. commit('setHostConfig', { ssl, host, port })
  35. router.push('/experiments')
  36. })
  37. .catch(err => {
  38. // Host not reachable or invalid HTTP status code
  39. throw new Error(`Invalid configuration "${URI}". ${!err.message.includes('aborted') ? err.message : ''}`)
  40. })
  41. },
  42. async connectToWs({ state, getters }) {
  43. if (state.socket.isConnected) return /*eslint-disable-line */
  44. else if (getters.isHostConfigured) {
  45. this._vm.$connect(getters.getHostWsURI)
  46. // $connect does not return a Promise, so we wait to know if it worked
  47. await delay(300)
  48. if (!state.socket.isConnected)
  49. throw new Error('Could not connect to remote WebSocket server.')
  50. }
  51. else throw new Error('Could not connect to WebSocket server. Host is not configured.')
  52. },
  53. sendMessage(_, { msgId, msg = undefined }) {
  54. Vue.prototype.$socket.send(JSON.stringify({ msgId, msg }))
  55. },
  56. async loadScenesList({ getters: { isHostConfigured, getHostURI }, commit }) {
  57. if (!isHostConfigured) throw new Error('Host is not configured.')
  58. const URI = getHostURI
  59. const scenes = await fetch(`${URI}${API_ROUTES.listScenes()}`).then(res => res.json())
  60. commit('setListScenes', scenes.data)
  61. },
  62. setExperimentProgress({ commit }, { experimentName, sceneName, data }) {
  63. commit('setExperimentProgress', { experimentName, sceneName, data })
  64. },
  65. setExperimentDone({ commit }, { experimentName, sceneName, done = true }) {
  66. commit('setExperimentDone', { experimentName, sceneName, done })
  67. }
  68. }