Skip to content

Commit c3cf82a

Browse files
author
Ravi Nadahar
committed
Handle log-viewer heartbeat
Signed-off-by: Ravi Nadahar <[email protected]>
1 parent c452c3a commit c3cf82a

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

bundles/org.openhab.ui/web/src/js/openhab/ws.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ type CloseCallback = (event: CloseEvent) => void;
110110
* @param path the path to connect to, e.g. `/ws`
111111
* @param messageCallback the callback to handle incoming messages
112112
* @param readyCallback the callback to handle the connection being ready
113+
* @param closeCallback the callback to handle the connection being closed
113114
* @param errorCallback the callback to handle errors
114115
* @param heartbeatCallback the callback to handle heartbeats
115116
* @param heartbeatInterval the interval in seconds for sending heartbeats
@@ -118,6 +119,7 @@ function newWSConnection (
118119
path: string,
119120
messageCallback: MessageCallback,
120121
readyCallback: ReadyCallback | undefined,
122+
closeCallback: CloseCallback | undefined,
121123
errorCallback: ErrorCallback | undefined,
122124
heartbeatCallback: HeartbeatCallback | undefined,
123125
heartbeatInterval: number
@@ -154,6 +156,13 @@ function newWSConnection (
154156
if (readyCallback) readyCallback(event)
155157
}
156158

159+
// Handle WebSocket connection closed
160+
socket.onclose = (event: CloseEvent) => {
161+
socket.clearKeepalive()
162+
if (closeCallback) closeCallback(event)
163+
else console.debug('WebSocket connection closed', event)
164+
}
165+
157166
// Handle WebSocket message received
158167
socket.onmessage = (event: MessageEvent) => {
159168
let evt: WebSocketMessage
@@ -192,6 +201,7 @@ const WebSocketService = {
192201
* @param messageCallback message callback to handle incoming messages
193202
* @param heartbeatCallback heartbeat callback
194203
* @param readyCallback ready callback
204+
* @param closeCallback close callback
195205
* @param errorCallback error callback
196206
* @param heartbeatInterval heartbeat interval in seconds (defaults to 5)
197207
*/
@@ -200,10 +210,11 @@ const WebSocketService = {
200210
messageCallback: MessageCallback,
201211
heartbeatCallback: HeartbeatCallback,
202212
readyCallback?: ReadyCallback,
213+
closeCallback?: CloseCallback,
203214
errorCallback?: ErrorCallback,
204215
heartbeatInterval: number = 5
205216
): KeepaliveWebSocket {
206-
return newWSConnection(path, messageCallback, readyCallback, errorCallback, heartbeatCallback, heartbeatInterval)
217+
return newWSConnection(path, messageCallback, readyCallback, closeCallback, errorCallback, heartbeatCallback, heartbeatInterval)
207218
},
208219
/**
209220
* Connect to the event WebSocket, which provides direct access to the EventBus.
@@ -250,6 +261,7 @@ const WebSocketService = {
250261
extendedMessageCallback,
251262
heartbeatCallback,
252263
extendedReadyCallback,
264+
undefined,
253265
errorCallback
254266
)
255267

bundles/org.openhab.ui/web/src/pages/developer/log-viewer.vue

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@
237237
<input type="search" placeholder="Filter..." v-model="filterText" @keyup.enter="handleFilter"></input>
238238
</div> -->
239239
<div style="display: flex; flex-wrap: nowrap">
240-
<f7-badge class="log-period margin-left-half">
240+
<f7-badge class="log-period margin-left-half" :color="periodRangeColor" :tooltip="periodRangeTooltip">
241241
{{ logStart }}&nbsp;>&nbsp;{{ logEnd }}
242242
</f7-badge>
243243
<f7-badge class="margin-horizontal" :color="countersBadgeColor" tooltip="Log entries filtered/total">
@@ -322,6 +322,12 @@
322322
</template>
323323

324324
<style lang="stylus">
325+
.theme-filled
326+
.log-viewer
327+
.subnavbar
328+
.badge.color-red
329+
background-color #300
330+
color #ff4d4d
325331
.log-viewer
326332
327333
.subnavbar
@@ -578,6 +584,14 @@ export default {
578584
filteredTableData () {
579585
return this.tableData.filter((item) => item.visible)
580586
},
587+
periodRangeColor () {
588+
if (!this.stateConnected) return 'red'
589+
return this.stateProcessing ? 'green' : 'orange'
590+
},
591+
periodRangeTooltip () {
592+
if (!this.stateConnected) return 'Log period - Disconnected'
593+
return this.stateProcessing ? 'Log period - Receiving logs' : 'Log period - Paused'
594+
},
581595
countersBadgeColor () {
582596
if (this.tableData.length >= this.maxEntries) return 'red'
583597
if (this.filterCount < this.tableData.length) return 'orange'
@@ -645,6 +659,10 @@ export default {
645659
nextTick(() => this.scrollToBottom())
646660
}
647661
662+
const closeCallback = () => {
663+
this.stateConnected = false
664+
}
665+
648666
const messageCallback = (event) => {
649667
if (Array.isArray(event)) {
650668
event.forEach((ev) => {
@@ -656,10 +674,18 @@ export default {
656674
}
657675
658676
const heartbeatCallback = () => {
659-
this.socket.send('{}')
677+
try {
678+
this.socket.send('{}')
679+
} catch (e) {
680+
console.warn('WebSocket heartbeat failed:', e)
681+
}
682+
}
683+
684+
const errorCallback = (event) => {
685+
console.error('WebSocket error:', event)
660686
}
661687
662-
this.socket = this.$oh.ws.connect('/ws/logs', messageCallback, heartbeatCallback, readyCallback, null, 9)
688+
this.socket = this.$oh.ws.connect('/ws/logs', messageCallback, heartbeatCallback, readyCallback, closeCallback, errorCallback, 9)
663689
664690
// TEMP
665691
// for (let i = 0; i < 1980; i++) {

0 commit comments

Comments
 (0)