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..72838bb48 100644 --- a/src/api/focus/index.js +++ b/src/api/focus/index.js @@ -25,7 +25,6 @@ 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 = { @@ -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,63 +84,28 @@ 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) { + this.close(); } - 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("received info: ", info); + + try { + let path = undefined; + if (typeof device == "string") path = device; + if (typeof device == "object") path = device.settings.path; + if (path !== undefined) { + await SerialPort.list(); + this._port = new SerialPort({ path: path, baudRate: 115200, autoOpen: true }); } 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 or object!"); } - } else { - throw new Error("Invalid argument"); + } catch (error) { + console.error("found this error!", error); + throw new Error("Unable to connect"); } this.device = info; @@ -182,53 +148,52 @@ 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(); }); 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" - // ); - // } - try { - console.log("CLOSING!!", this._port); - if (this._port && this._port.isOpen) { - await this._port.close(); - } - } catch (error) { - console.log(error); - } - + clearContext() { this.result = ""; this.callbacks = []; - this._port = null; this.device = null; this.supportedCommands = []; - this.closed = true; this.file = false; this.fileData = null; } + async close() { + if (this.file !== false) { + this.clearContext(); + this.closed = true; + return true; + } + let result; + try { + if (this._port) { + while (this._port.isOpen === true) { + // console.log("Closing device port!!", this._port); + result = await this._port.close(); + // 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.error("error when closing", error); + } + + this.clearContext(); + return result; + } + async isDeviceAccessible(port) { if (process.platform !== "linux") return true; @@ -240,12 +205,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..c71fe8cbe 100644 --- a/src/renderer/App.js +++ b/src/renderer/App.js @@ -53,7 +53,7 @@ const { ipcRenderer } = require("electron"); const path = require("path"); let focus = new Focus(); -focus.debug = false; +focus.debug = true; focus.timeout = 5000; class App extends React.Component { @@ -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 39c5dd4cb..da4546dc5 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) { @@ -334,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 }); };