From 8505750958bf229697ed885c61c0eb2c406d4489 Mon Sep 17 00:00:00 2001 From: John Arild Berentsen Date: Mon, 10 Sep 2018 21:53:16 +0200 Subject: [PATCH] Load ozw-log in new window, and add tail-like button. (#1652) * Load log in new window, and add tail * avoid duplicate code --- src/panels/config/zwave/zwave-log.js | 64 +++++++++++++++++++--------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/src/panels/config/zwave/zwave-log.js b/src/panels/config/zwave/zwave-log.js index 0bde2c4fa0..712c9d7fab 100644 --- a/src/panels/config/zwave/zwave-log.js +++ b/src/panels/config/zwave/zwave-log.js @@ -27,23 +27,17 @@ class OzwLog extends PolymerElement { padding-bottom: 24px; } - .help-text { - padding: 5px 24px; - } OZW Log
- +
- Refresh -
-
-
[[ozwLogs]]
-
+ Load + Tail
`; @@ -58,25 +52,53 @@ class OzwLog extends PolymerElement { value: false, }, - ozwLogs: { - type: String, - value: 'Refresh to pull log' + _ozwLogs: String, + + _completeLog: { + type: Boolean, + value: true }, - numLogLines: { + _numLogLines: { type: Number, - value: 0 + value: 0, + observer: '_isCompleteLog' }, + + _intervalId: String, }; } - refreshLog() { - this.ozwLogs = 'Loading ozw log...'; - this.hass.callApi('GET', 'zwave/ozwlog?lines=' + this.numLogLines) - .then((info) => { - this.ozwLogs = info; - }); + async _tailLog() { + const ozwWindow = await this._openLogWindow(); + this.setProperties({ + _intervalId: setInterval(() => { this._refreshLog(ozwWindow); }, 1500) }); + } + + async _openLogWindow() { + const info = await this.hass.callApi('GET', 'zwave/ozwlog?lines=' + this._numLogLines); + this.setProperties({ _ozwLogs: info }); + const ozwWindow = window.open('', 'OpenZwave internal log', 'toolbar'); + ozwWindow.document.title = 'OpenZwave internal logfile'; + ozwWindow.document.body.innerText = this._ozwLogs; + return ozwWindow; + } + + async _refreshLog(ozwWindow) { + if (ozwWindow.closed === true) { + clearInterval(this._intervalId); + this.setProperties({ _intervalId: null }); + } else { + const info = await this.hass.callApi('GET', 'zwave/ozwlog?lines=' + this._numLogLines); + this.setProperties({ _ozwLogs: info }); + ozwWindow.document.body.innerText = this._ozwLogs; + } + } + + _isCompleteLog() { + if (this._numLogLines !== '0') { + this.setProperties({ _completeLog: false }); + } else { this.setProperties({ _completeLog: true }); } } } - customElements.define('ozw-log', OzwLog);