From 689cd09aef6f7abb872429f4073437b9f50acd48 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Wed, 27 Jul 2022 09:16:36 +0300 Subject: [PATCH] feat: toggle serial port --- .../serial-signer/serial-signer.html | 2 +- .../components/serial-signer/serial-signer.js | 35 +++++++++++++++++++ .../extensions/watchonly/static/js/index.js | 30 ---------------- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.html b/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.html index 4690a26c..61c72bc3 100644 --- a/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.html +++ b/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.html @@ -5,7 +5,7 @@ color="primary" icon="usb" :text-color="selectedPort ? 'orange' : 'white'" - @click="openSerialPort" + @click="toggleSerialPortConection" > diff --git a/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.js b/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.js index cb1a981b..fc05cf69 100644 --- a/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.js +++ b/lnbits/extensions/watchonly/static/components/serial-signer/serial-signer.js @@ -34,6 +34,13 @@ async function serialSigner(path) { }, methods: { + toggleSerialPortConection: async function () { + if (!this.selectedPort) { + this.openSerialPort() + } else { + this.closeSerialPort() + } + }, openSerialPort: async function () { if (!this.checkSerialPortSupported()) return console.log('### openSerialPort', this.selectedPort) @@ -72,6 +79,34 @@ async function serialSigner(path) { }) } }, + closeSerialPort: async function () { + try { + if (this.writer) this.writer.close() + if (this.writableStreamClosed) await this.writableStreamClosed + if (this.reader) this.reader.cancel() + if (this.readableStreamClosed) + await this.readableStreamClosed.catch(() => { + /* Ignore the error */ + }) + if (this.selectedPort) await this.selectedPort.close() + this.selectedPort = null + this.$q.notify({ + type: 'positive', + message: 'Serial port disconnected!', + timeout: 5000 + }) + } catch (error) { + this.selectedPort = null + console.log('### error', error) + this.$q.notify({ + type: 'warning', + message: 'Cannot close serial port!', + caption: `${error}`, + timeout: 10000 + }) + } + }, + checkSerialPortSupported: function () { if (!navigator.serial) { this.$q.notify({ diff --git a/lnbits/extensions/watchonly/static/js/index.js b/lnbits/extensions/watchonly/static/js/index.js index d493e1c7..a3d7e19f 100644 --- a/lnbits/extensions/watchonly/static/js/index.js +++ b/lnbits/extensions/watchonly/static/js/index.js @@ -273,36 +273,6 @@ const watchOnly = async () => { }, //################### SERIAL PORT ################### - closeSerialPort: async function () { - try { - console.log('### closeSerialPort', this.serial.selectedPort) - if (this.serial.writer) this.serial.writer.close() - if (this.serial.writableStreamClosed) - await this.serial.writableStreamClosed - if (this.serial.reader) this.serial.reader.cancel() - if (this.serial.readableStreamClosed) - await this.serial.readableStreamClosed.catch(() => { - /* Ignore the error */ - }) - if (this.serial.selectedPort) await this.serial.selectedPort.close() - this.serial.selectedPort = null - this.$q.notify({ - type: 'positive', - message: 'Serial port disconnected!', - timeout: 5000 - }) - } catch (error) { - this.serial.selectedPort = null - console.log('### error', error) - this.$q.notify({ - type: 'warning', - message: 'Cannot close serial port!', - caption: `${error}`, - timeout: 10000 - }) - } - }, - sharePsbtWithAnimatedQRCode: async function () { console.log('### sharePsbtWithAnimatedQRCode') },