From cd41b22addf61d539be9aa67c8d6283fc9b5a3d5 Mon Sep 17 00:00:00 2001 From: AlexDygma Date: Fri, 30 Jun 2023 17:28:13 +0200 Subject: [PATCH 1/2] Fixing focus issue, not working yet Signed-off-by: AlexDygma --- src/api/flash/defyFlasher/NRf52833-flasher.js | 4 +- src/api/flash/index.js | 4 +- src/api/flash/raiseFlasher/arduino-flasher.js | 4 +- src/api/focus/index.js | 172 +++++++++--------- src/api/hardware-dygma-defy-wired/index.js | 4 +- src/api/hardware-dygma-defy-wireless/index.js | 4 +- src/api/hardware-dygma-raise-ansi/index.js | 18 +- src/api/hardware-dygma-raise-iso/index.js | 18 +- src/renderer/App.js | 6 +- src/renderer/views/SelectKeyboard.js | 1 + 10 files changed, 116 insertions(+), 119 deletions(-) diff --git a/src/api/flash/defyFlasher/NRf52833-flasher.js b/src/api/flash/defyFlasher/NRf52833-flasher.js index f57f4c074..4df629309 100644 --- a/src/api/flash/defyFlasher/NRf52833-flasher.js +++ b/src/api/flash/defyFlasher/NRf52833-flasher.js @@ -102,8 +102,8 @@ async function read_cb(callback) { * Closes the connection to the bootloader. * @param {function} cb - An optional callback to run once all the functions have completed. */ -function disconnect_cb(cb) { - focus.close(); +async function disconnect_cb(cb) { + await focus.close(); cb(null, ""); } diff --git a/src/api/flash/index.js b/src/api/flash/index.js index 3cc0d1bfb..75b8be2af 100644 --- a/src/api/flash/index.js +++ b/src/api/flash/index.js @@ -1,4 +1,4 @@ import { FlashRaise } from "./raiseFlasher/flash"; -import { FlashDefyWired, FlashDefyWireless } from "./defyFlasher/flash"; +import { FlashDefyWireless } from "./defyFlasher/flash"; -export { FlashRaise, FlashDefyWired, FlashDefyWireless }; +export { FlashRaise, FlashDefyWireless }; diff --git a/src/api/flash/raiseFlasher/arduino-flasher.js b/src/api/flash/raiseFlasher/arduino-flasher.js index 50e419c33..ddf073aa9 100644 --- a/src/api/flash/raiseFlasher/arduino-flasher.js +++ b/src/api/flash/raiseFlasher/arduino-flasher.js @@ -101,8 +101,8 @@ async function read_cb(callback) { * Closes the connection to the bootloader. * @param {function} cb - An optional callback to run once all the functions have completed. */ -function disconnect_cb(cb) { - focus.close(); +async function disconnect_cb(cb) { + await focus.close(); cb(null, ""); } diff --git a/src/api/focus/index.js b/src/api/focus/index.js index cd3ccc8ef..aefadcb9e 100644 --- a/src/api/focus/index.js +++ b/src/api/focus/index.js @@ -25,13 +25,12 @@ global.focus_instance = null; class Focus { constructor() { - this.delay = ms => new Promise(res => setTimeout(res, ms)); if (!global.focus_instance) { global.focus_instance = this; this.commands = { help: this._help }; - this.timeout = 5000; + this.timeout = 30000; this.debug = false; this.closed = true; this.file = false; @@ -39,6 +38,8 @@ class Focus { return global.focus_instance; } + delay = ms => new Promise(res => setTimeout(res, ms)); + debugLog(...args) { if (!this.debug) return; console.log(...args); @@ -83,65 +84,69 @@ class Focus { return true; } - console.warn("Warning! device being opened", device.isOpen, device, info); - let count = 0; - while (device._eventsCount < 5 && count < 8) { - console.log("waiting for device"); - await this.delay(250); - count++; + if (this._port !== undefined && this._port.isOpen === false) { + delete this._port; } - if (count == 8) return; - if (this._port) this._port.close(); - if (typeof device == "string") { - if (!info) throw new Error("Device descriptor argument is mandatory"); - this._port = new SerialPort( - { - path: device, - baudRate: 115200 - }, - err => { - if (err !== null) { - console.error(err); - } - } - ); - } else if (typeof device == "object") { - if (device.hasOwnProperty("binding")) { - if (!info) throw new Error("Device descriptor argument is mandatory"); - const path = device.path; - await device.close(); - this._port = new SerialPort( - { - path: path, - baudRate: 115200 - }, - err => { - if (err !== null) { - console.error(err); - } + + console.log("Warning! device being opened"); + console.log("port status opened?", this._port ? this._port.isOpen : "unknown"); + console.log("received device", device); + console.log("receivec info: ", info); + + try { + let path = undefined; + if (typeof device == "string") path = device; + if (path !== undefined) { + this._port = new SerialPort({ path: path, baudRate: 115200, autoOpen: false }); + this._port.open(function (err) { + console.log("opening port"); + if (err) { + return console.log("Error opening port: ", err.message); } - ); + }); } else { - let devices = await this.find(device); - if (devices && devices.length >= 1) { - this._port = new SerialPort( - { - path: devices[0].path, - baudRate: 115200 - }, - err => { - if (err !== null) { - console.error(err); - } - } - ); - } - info = device; + throw Error("device not a string!"); } - } else { - throw new Error("Invalid argument"); + } catch (error) { + console.error("found this error!", error); + throw new Error("Unable to connect"); } + // if (typeof device == "string") { + // console.log("type is string"); + // if (!info) throw new Error("Device descriptor argument is mandatory"); + // try { + // this._port = new SerialPort({ path: device, baudRate: 115200 }); + // } catch (error) { + // console.error("Error opening serial port", { error }); + // delete this._port; + // throw new Error("Unable to connect"); + // } + // } else if (typeof device == "object") { + // console.log("type is object"); + // if (device.hasOwnProperty("binding")) { + // if (!info) throw new Error("Device descriptor argument is mandatory"); + // console.log("has binding: ", device); + // this._port.open({ + // path: device.path, + // baudRate: 115200 + // }); + // } else { + // console.log("doesn't have binding: ", device); + // let devices = await this.find(info.device); + // console.log("devices found", devices); + // if (devices && devices.length >= 1) { + // this._port = new SerialPort({ + // path: devices.path, + // baudRate: 115200 + // }); + // } + // info = device; + // } + // } else { + // throw new Error("Invalid argument"); + // } + this.device = info; this.parser = this._port.pipe(new DelimiterParser({ delimiter: "\r\n" })); this.result = ""; @@ -182,38 +187,38 @@ class Focus { } } - // Setup close port alert - this._port.on("close", function (error) { - if (error !== null && error.disconnected === true) { - console.error("Error: device disconnected without control"); - this.closed = true; - } else { - console.warn("Warning: device disconnected by user interaction"); - this.closed = true; - } - }); // Setup error port alert - this._port.on("error", function (err) { + this._port.on("error", async function (err) { console.error("Error on SerialPort: " + err); + await this._port.close(); + delete this._port; }); this.closed = false; return this._port; } async close() { - // if (this._port) { - // console.log("closing port data >>"); - // console.log(inspect(this._port)); - // console.log(this._port._eventsCount); - // console.log( - // "Port State: ", - // this._port ? this._port.isOpen : "unable to open" - // ); - // } + if (this.file !== false) { + this.result = ""; + this.callbacks = []; + this.device = null; + this.supportedCommands = []; + this.file = false; + this.fileData = null; + this.closed = true; + return true; + } + let result; try { - console.log("CLOSING!!", this._port); - if (this._port && this._port.isOpen) { - await this._port.close(); + console.log("Closing device port!!", this._port); + if (this._port) { + while (this._port.isOpen === true) { + console.log("trying to close!!", this._port.isOpen); + result = await this._port.close(); + console.log("after close!!", this._port.isOpen); + this._port.destroy(); + } + delete this._port; } } catch (error) { console.log(error); @@ -221,12 +226,11 @@ class Focus { this.result = ""; this.callbacks = []; - this._port = null; this.device = null; this.supportedCommands = []; - this.closed = true; this.file = false; this.fileData = null; + return result; } async isDeviceAccessible(port) { @@ -240,12 +244,12 @@ class Focus { return true; } - async isDeviceSupported(port) { - if (!port.device.isDeviceSupported) { + async isDeviceSupported(device) { + if (!device.device.isDeviceSupported) { return true; } - const supported = await port.device.isDeviceSupported(port); - this.debugLog("focus.isDeviceSupported: port=", port, "supported=", supported); + const supported = await device.device.isDeviceSupported(device); + this.debugLog("focus.isDeviceSupported: port=", device, "supported=", supported); return supported; } diff --git a/src/api/hardware-dygma-defy-wired/index.js b/src/api/hardware-dygma-defy-wired/index.js index b348d2e1a..3b3099440 100644 --- a/src/api/hardware-dygma-defy-wired/index.js +++ b/src/api/hardware-dygma-defy-wired/index.js @@ -66,12 +66,12 @@ const Defy_wired = { }, isDeviceSupported: async port => { - let focus = new Focus(); + const focus = new Focus(); focus._port && focus._port.path === port.path ? await focus.open(focus._port, port.device, null) : await focus.open(port.path, port.device, null); port.serialNumber = await focus.command("hardware.chip_id"); - focus.close(); + let result = await focus.close(); return true; } }; diff --git a/src/api/hardware-dygma-defy-wireless/index.js b/src/api/hardware-dygma-defy-wireless/index.js index 9adc78ad8..7b690faff 100644 --- a/src/api/hardware-dygma-defy-wireless/index.js +++ b/src/api/hardware-dygma-defy-wireless/index.js @@ -67,12 +67,12 @@ const Defy_wireless = { }, isDeviceSupported: async port => { - let focus = new Focus(); + const focus = new Focus(); focus._port && focus._port.path === port.path ? await focus.open(focus._port, port.device, null) : await focus.open(port.path, port.device, null); port.serialNumber = await focus.command("hardware.chip_id"); - focus.close(); + let result = await focus.close(); return true; } }; diff --git a/src/api/hardware-dygma-raise-ansi/index.js b/src/api/hardware-dygma-raise-ansi/index.js index bfcf7bc47..785930da2 100644 --- a/src/api/hardware-dygma-raise-ansi/index.js +++ b/src/api/hardware-dygma-raise-ansi/index.js @@ -66,17 +66,13 @@ const Raise_ANSI = { }, isDeviceSupported: async port => { - let focus = new Focus(); - let layout = localStorage.getItem(port.serialNumber); - if (!layout) { - focus._port && focus._port.path === port.path - ? await focus.open(focus._port, port.device, null) - : await focus.open(port.path, port.device, null); - port.serialNumber = await focus.command("hardware.chip_id"); - layout = await focus.command("hardware.layout"); - focus.close(); - localStorage.setItem(port.serialNumber, layout); - } + const focus = new Focus(); + focus._port && focus._port.path === port.path + ? await focus.open(focus._port, port, null) + : await focus.open(port.path, port.device, null); + port.serialNumber = await focus.command("hardware.chip_id"); + let layout = await focus.command("hardware.layout"); + await focus.close(); return layout.trim() === "ANSI"; } }; diff --git a/src/api/hardware-dygma-raise-iso/index.js b/src/api/hardware-dygma-raise-iso/index.js index 310c50ed3..7a095f45e 100644 --- a/src/api/hardware-dygma-raise-iso/index.js +++ b/src/api/hardware-dygma-raise-iso/index.js @@ -66,17 +66,13 @@ const Raise_ISO = { }, isDeviceSupported: async port => { - let focus = new Focus(); - let layout = localStorage.getItem(port.serialNumber); - if (!layout) { - focus._port && focus._port.path === port.path - ? await focus.open(focus._port, port.device, null) - : await focus.open(port.path, port.device, null); - port.serialNumber = await focus.command("hardware.chip_id"); - layout = await focus.command("hardware.layout"); - focus.close(); - localStorage.setItem(port.serialNumber, layout); - } + const focus = new Focus(); + focus._port && focus._port.path === port.path + ? await focus.open(focus._port, port.device, null) + : await focus.open(port.path, port.device, null); + port.serialNumber = await focus.command("hardware.chip_id"); + let layout = await focus.command("hardware.layout"); + await focus.close(); return layout.trim() === "ISO"; } }; diff --git a/src/renderer/App.js b/src/renderer/App.js index 65b1b841a..f03856fb8 100644 --- a/src/renderer/App.js +++ b/src/renderer/App.js @@ -53,8 +53,8 @@ const { ipcRenderer } = require("electron"); const path = require("path"); let focus = new Focus(); -focus.debug = false; -focus.timeout = 5000; +focus.debug = true; +focus.timeout = 30000; class App extends React.Component { constructor(props) { @@ -289,7 +289,7 @@ class App extends React.Component { }; onKeyboardDisconnect = async () => { - focus.close(); + await focus.close(); this.setState({ connected: false, device: null, diff --git a/src/renderer/views/SelectKeyboard.js b/src/renderer/views/SelectKeyboard.js index a8bce2629..7146a98f2 100644 --- a/src/renderer/views/SelectKeyboard.js +++ b/src/renderer/views/SelectKeyboard.js @@ -255,6 +255,7 @@ class SelectKeyboard extends Component { let supported_devices = []; for (const device of devices) { device.accessible = await focus.isDeviceAccessible(device); + console.log("before checking device supported", device, focus); if (device.accessible && (await focus.isDeviceSupported(device))) { supported_devices.push(device); } else if (!device.accessible) { From 3079ae745706e010884a854b56e6c7a5b874ecf8 Mon Sep 17 00:00:00 2001 From: AlexDygma Date: Fri, 30 Jun 2023 18:31:20 +0200 Subject: [PATCH 2/2] fix: solved connection issue by re-listing available ports Signed-off-by: AlexDygma --- src/api/focus/index.js | 95 ++++++++-------------------- src/renderer/App.js | 2 +- src/renderer/views/SelectKeyboard.js | 4 +- 3 files changed, 31 insertions(+), 70 deletions(-) diff --git a/src/api/focus/index.js b/src/api/focus/index.js index aefadcb9e..72838bb48 100644 --- a/src/api/focus/index.js +++ b/src/api/focus/index.js @@ -30,7 +30,7 @@ class Focus { this.commands = { help: this._help }; - this.timeout = 30000; + this.timeout = 5000; this.debug = false; this.closed = true; this.file = false; @@ -85,68 +85,29 @@ class Focus { } if (this._port !== undefined && this._port.isOpen === false) { - delete this._port; + this.close(); } - console.log("Warning! device being opened"); - console.log("port status opened?", this._port ? this._port.isOpen : "unknown"); - console.log("received device", device); - console.log("receivec info: ", info); + // console.log("Warning! device being opened"); + // console.log("port status opened?", this._port ? this._port.isOpen : "unknown"); + // console.log("received device", device); + // console.log("received info: ", info); try { let path = undefined; if (typeof device == "string") path = device; + if (typeof device == "object") path = device.settings.path; if (path !== undefined) { - this._port = new SerialPort({ path: path, baudRate: 115200, autoOpen: false }); - this._port.open(function (err) { - console.log("opening port"); - if (err) { - return console.log("Error opening port: ", err.message); - } - }); + await SerialPort.list(); + this._port = new SerialPort({ path: path, baudRate: 115200, autoOpen: true }); } else { - throw Error("device not a string!"); + throw Error("device not a string or object!"); } } catch (error) { console.error("found this error!", error); throw new Error("Unable to connect"); } - // if (typeof device == "string") { - // console.log("type is string"); - // if (!info) throw new Error("Device descriptor argument is mandatory"); - // try { - // this._port = new SerialPort({ path: device, baudRate: 115200 }); - // } catch (error) { - // console.error("Error opening serial port", { error }); - // delete this._port; - // throw new Error("Unable to connect"); - // } - // } else if (typeof device == "object") { - // console.log("type is object"); - // if (device.hasOwnProperty("binding")) { - // if (!info) throw new Error("Device descriptor argument is mandatory"); - // console.log("has binding: ", device); - // this._port.open({ - // path: device.path, - // baudRate: 115200 - // }); - // } else { - // console.log("doesn't have binding: ", device); - // let devices = await this.find(info.device); - // console.log("devices found", devices); - // if (devices && devices.length >= 1) { - // this._port = new SerialPort({ - // path: devices.path, - // baudRate: 115200 - // }); - // } - // info = device; - // } - // } else { - // throw new Error("Invalid argument"); - // } - this.device = info; this.parser = this._port.pipe(new DelimiterParser({ delimiter: "\r\n" })); this.result = ""; @@ -191,45 +152,45 @@ class Focus { this._port.on("error", async function (err) { console.error("Error on SerialPort: " + err); await this._port.close(); - delete this._port; }); this.closed = false; return this._port; } + clearContext() { + this.result = ""; + this.callbacks = []; + this.device = null; + this.supportedCommands = []; + this.file = false; + this.fileData = null; + } + async close() { if (this.file !== false) { - this.result = ""; - this.callbacks = []; - this.device = null; - this.supportedCommands = []; - this.file = false; - this.fileData = null; + this.clearContext(); this.closed = true; return true; } let result; try { - console.log("Closing device port!!", this._port); if (this._port) { while (this._port.isOpen === true) { - console.log("trying to close!!", this._port.isOpen); + // console.log("Closing device port!!", this._port); result = await this._port.close(); - console.log("after close!!", this._port.isOpen); - this._port.destroy(); + // console.log("is it still open? ", this._port.isOpen, result); + await this._port.removeAllListeners(); + await this._port.destroy(); + // console.log("is it destroyed?", this._port.destroyed); } delete this._port; + this.closed = true; } } catch (error) { - console.log(error); + console.error("error when closing", error); } - this.result = ""; - this.callbacks = []; - this.device = null; - this.supportedCommands = []; - this.file = false; - this.fileData = null; + this.clearContext(); return result; } diff --git a/src/renderer/App.js b/src/renderer/App.js index f03856fb8..c71fe8cbe 100644 --- a/src/renderer/App.js +++ b/src/renderer/App.js @@ -54,7 +54,7 @@ const path = require("path"); let focus = new Focus(); focus.debug = true; -focus.timeout = 30000; +focus.timeout = 5000; class App extends React.Component { constructor(props) { diff --git a/src/renderer/views/SelectKeyboard.js b/src/renderer/views/SelectKeyboard.js index c05b4b2db..da4546dc5 100644 --- a/src/renderer/views/SelectKeyboard.js +++ b/src/renderer/views/SelectKeyboard.js @@ -335,12 +335,12 @@ class SelectKeyboard extends Component { } selectVirtualKeyboard = event => { - console.log(event); + // console.log(event); this.setState({ selectedVirtualKeyboard: event }); }; selectPort = event => { - console.log(event); + // console.log(event); this.setState({ selectedPortIndex: event }); };