Skip to content

Commit

Permalink
Fixing focus issue, not working yet (#457)
Browse files Browse the repository at this point in the history
* Fixing focus issue, not working yet

Signed-off-by: AlexDygma <[email protected]>

* fix: solved connection issue by re-listing available ports

Signed-off-by: AlexDygma <[email protected]>

---------

Signed-off-by: AlexDygma <[email protected]>
  • Loading branch information
AlexDygma committed Jun 30, 2023
1 parent 9897e9b commit be6f03b
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 127 deletions.
4 changes: 2 additions & 2 deletions src/api/flash/defyFlasher/NRf52833-flasher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
}

Expand Down
4 changes: 2 additions & 2 deletions src/api/flash/index.js
Original file line number Diff line number Diff line change
@@ -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 };
4 changes: 2 additions & 2 deletions src/api/flash/raiseFlasher/arduino-flasher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
}

Expand Down
147 changes: 56 additions & 91 deletions src/api/focus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/api/hardware-dygma-defy-wired/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/api/hardware-dygma-defy-wireless/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down
18 changes: 7 additions & 11 deletions src/api/hardware-dygma-raise-ansi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
};
Expand Down
18 changes: 7 additions & 11 deletions src/api/hardware-dygma-raise-iso/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -289,7 +289,7 @@ class App extends React.Component {
};

onKeyboardDisconnect = async () => {
focus.close();
await focus.close();
this.setState({
connected: false,
device: null,
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/views/SelectKeyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 });
};

Expand Down

0 comments on commit be6f03b

Please sign in to comment.