From 4624ff0f88227bd31477e6809266d72a3c6b718d Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Wed, 22 Jun 2022 00:10:49 +0200 Subject: [PATCH 1/4] [Main UI] Add support for webaudio Fixes #743 Signed-off-by: Wouter Born --- .../web/build/webpack.config.js | 2 +- .../org.openhab.ui/web/src/components/app.vue | 46 +++++++++++++++++++ .../org.openhab.ui/web/src/js/openhab/api.js | 5 +- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.ui/web/build/webpack.config.js b/bundles/org.openhab.ui/web/build/webpack.config.js index 1450b309c5..2fe7f7f6fd 100644 --- a/bundles/org.openhab.ui/web/build/webpack.config.js +++ b/bundles/org.openhab.ui/web/build/webpack.config.js @@ -53,7 +53,7 @@ module.exports = { // poll: 1000, // }, proxy: [{ - context: ['/auth', '/rest', '/chart', '/proxy', '/icon', '/static', '/changePassword', '/createApiToken'], + context: ['/auth', '/rest', '/chart', '/proxy', '/icon', '/static', '/changePassword', '/createApiToken', '/audio'], target: apiBaseUrl }] }, diff --git a/bundles/org.openhab.ui/web/src/components/app.vue b/bundles/org.openhab.ui/web/src/components/app.vue index b107de8124..59c7188c9e 100644 --- a/bundles/org.openhab.ui/web/src/components/app.vue +++ b/bundles/org.openhab.ui/web/src/components/app.vue @@ -555,6 +555,50 @@ export default { ev.stopPropagation() ev.preventDefault() } + }, + startEventSource () { + this.eventSource = this.$oh.sse.connect('/rest/events?topics=openhab/webaudio/playurl', null, (event) => { + const topicParts = event.topic.split('/') + switch (topicParts[2]) { + case 'playurl': + this.playAudioUrl(JSON.parse(event.payload)) + break + } + }, () => { + // in case of error, try reloading to refresh + this.stopEventSource() + this.startEventSource() + }) + }, + stopEventSource () { + this.$oh.sse.close(this.eventSource) + this.eventSource = null + }, + playAudioUrl (audioUrl) { + let context + try { + window.AudioContext = window.AudioContext || window.webkitAudioContext + if (typeof (window.AudioContext) !== 'undefined') { + context = new AudioContext() + } + console.log('Playing audio URL: ' + audioUrl) + this.$oh.api.getPlain(audioUrl, '', '*/*', 'arraybuffer').then((data) => { + context.decodeAudioData(data, function (buffer) { + let source = context.createBufferSource() + source.buffer = buffer + source.connect(context.destination) + source.onended = function () { + context.close() + } + source.start(0) + }) + }) + } catch (e) { + console.warn('Error while playing audio URL: ' + e.toString()) + if (context) { + context.close() + } + } } }, created () { @@ -656,6 +700,8 @@ export default { if (window) { window.addEventListener('keydown', this.keyDown) } + + this.startEventSource() }) } } diff --git a/bundles/org.openhab.ui/web/src/js/openhab/api.js b/bundles/org.openhab.ui/web/src/js/openhab/api.js index c7a803c240..ac93fafc5c 100644 --- a/bundles/org.openhab.ui/web/src/js/openhab/api.js +++ b/bundles/org.openhab.ui/web/src/js/openhab/api.js @@ -30,13 +30,14 @@ export default { get (uri, data) { return wrapPromise(Framework7.request.promise.json(uri, data)) }, - getPlain (uri, data, contentType) { + getPlain (uri, data, contentType, responseType) { return wrapPromise(Framework7.request.promise({ method: 'GET', url: uri, data, processData: false, - contentType: contentType || 'text/plain' + contentType: contentType || 'text/plain', + xhrFields: typeof responseType !== 'undefined' ? { responseType: responseType } : null })) }, post (uri, data, dataType) { From ce2bb0501710389d14941a3faac9e5adbbc7a623 Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Wed, 22 Jun 2022 17:34:39 +0200 Subject: [PATCH 2/4] Add eventSource to data Signed-off-by: Wouter Born --- bundles/org.openhab.ui/web/src/components/app.vue | 1 + 1 file changed, 1 insertion(+) diff --git a/bundles/org.openhab.ui/web/src/components/app.vue b/bundles/org.openhab.ui/web/src/components/app.vue index 59c7188c9e..a450e0b103 100644 --- a/bundles/org.openhab.ui/web/src/components/app.vue +++ b/bundles/org.openhab.ui/web/src/components/app.vue @@ -283,6 +283,7 @@ export default { return { init: false, ready: false, + eventSource: null, // Framework7 Parameters f7params: { From 44668a8a43c9292a7665208c90e3fefd2e2a6957 Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Wed, 22 Jun 2022 19:13:59 +0200 Subject: [PATCH 3/4] Prevent a load on disconnect Signed-off-by: Wouter Born --- bundles/org.openhab.ui/web/src/components/app.vue | 4 ---- 1 file changed, 4 deletions(-) diff --git a/bundles/org.openhab.ui/web/src/components/app.vue b/bundles/org.openhab.ui/web/src/components/app.vue index a450e0b103..07fc7c291e 100644 --- a/bundles/org.openhab.ui/web/src/components/app.vue +++ b/bundles/org.openhab.ui/web/src/components/app.vue @@ -565,10 +565,6 @@ export default { this.playAudioUrl(JSON.parse(event.payload)) break } - }, () => { - // in case of error, try reloading to refresh - this.stopEventSource() - this.startEventSource() }) }, stopEventSource () { From c6219e68416cd651d3dc1b687a9b8abaad5bf229 Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Sat, 9 Jul 2022 12:50:17 +0200 Subject: [PATCH 4/4] Add Web Audio configuration option Signed-off-by: Wouter Born --- .../web/src/assets/i18n/theme-switcher/en.json | 3 ++- bundles/org.openhab.ui/web/src/components/app.vue | 4 +++- .../web/src/components/theme-switcher.vue | 11 +++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.ui/web/src/assets/i18n/theme-switcher/en.json b/bundles/org.openhab.ui/web/src/assets/i18n/theme-switcher/en.json index 2fa1def6f4..a13161dea4 100644 --- a/bundles/org.openhab.ui/web/src/assets/i18n/theme-switcher/en.json +++ b/bundles/org.openhab.ui/web/src/assets/i18n/theme-switcher/en.json @@ -11,5 +11,6 @@ "about.miscellaneous.home.background": "Standard home page background color", "about.miscellaneous.home.hideChatInput": "Hide chat input box on home page", "about.miscellaneous.home.disableCardExpansionAnimation": "Disable card expansion animations", - "about.miscellaneous.theme.disablePageTransition": "Disable page transition animations" + "about.miscellaneous.theme.disablePageTransition": "Disable page transition animations", + "about.miscellaneous.webaudio.enable": "Enable Web Audio sink support" } diff --git a/bundles/org.openhab.ui/web/src/components/app.vue b/bundles/org.openhab.ui/web/src/components/app.vue index 07fc7c291e..7ac25ce008 100644 --- a/bundles/org.openhab.ui/web/src/components/app.vue +++ b/bundles/org.openhab.ui/web/src/components/app.vue @@ -698,7 +698,9 @@ export default { window.addEventListener('keydown', this.keyDown) } - this.startEventSource() + if (localStorage.getItem('openhab.ui:webaudio.enable') === 'enabled') { + this.startEventSource() + } }) } } diff --git a/bundles/org.openhab.ui/web/src/components/theme-switcher.vue b/bundles/org.openhab.ui/web/src/components/theme-switcher.vue index a944e0ddec..1233edd493 100644 --- a/bundles/org.openhab.ui/web/src/components/theme-switcher.vue +++ b/bundles/org.openhab.ui/web/src/components/theme-switcher.vue @@ -70,6 +70,10 @@ + + + + @@ -119,6 +123,10 @@ export default { setPageTransitionAnimation (value) { localStorage.setItem('openhab.ui:theme.pagetransition', (value) ? 'disabled' : 'default') location.reload() + }, + setWebAudio (value) { + localStorage.setItem('openhab.ui:webaudio.enable', (value) ? 'enabled' : 'default') + location.reload() } }, computed: { @@ -145,6 +153,9 @@ export default { }, pageTransitionAnimation () { return localStorage.getItem('openhab.ui:theme.pagetransition') || 'default' + }, + webAudio () { + return localStorage.getItem('openhab.ui:webaudio.enable') || 'default' } } }