mirror of
https://github.com/We-Dont-Byte/Mind_Reader.git
synced 2024-11-15 03:35:59 +00:00
Remove automatic connection
I did not heed the warning where 'only the path is guaranteed' when listing open serial ports and made the assumption that the manufacturer would be known (hint: it wasn't).
This commit is contained in:
parent
d7e44406ec
commit
2d2ca75177
3207
package-lock.json
generated
3207
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -301,11 +301,6 @@
|
|||||||
"description": "The number of words around the cursor to use when reading the cursor context",
|
"description": "The number of words around the cursor to use when reading the cursor context",
|
||||||
"default": 1
|
"default": 1
|
||||||
},
|
},
|
||||||
"mindreader.connection.connectAutomatically": {
|
|
||||||
"type": "boolean",
|
|
||||||
"description": "Specifies whether to try to automatically detect and communicate with a connected Hub.",
|
|
||||||
"default": "true"
|
|
||||||
},
|
|
||||||
"mindreader.connection.portPath": {
|
"mindreader.connection.portPath": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"markdownDescription": "Specifies the serial port path to use if `#mindreader.connectAutomatically#` is not set."
|
"markdownDescription": "Specifies the serial port path to use if `#mindreader.connectAutomatically#` is not set."
|
||||||
|
@ -392,13 +392,8 @@ async function connectHub(): Promise<void> {
|
|||||||
vscode.window.showWarningMessage('LEGO Hub is already connected, reconnecting...');
|
vscode.window.showWarningMessage('LEGO Hub is already connected, reconnecting...');
|
||||||
disconnectHub();
|
disconnectHub();
|
||||||
}
|
}
|
||||||
const config = vscode.workspace.getConfiguration();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (config.get('mindreader.connection.connectAutomatically')) {
|
|
||||||
hub = await HubManager.create();
|
|
||||||
vscode.window.showInformationMessage('LEGO Hub connected');
|
|
||||||
} else {
|
|
||||||
const ports = await HubManager.queryPorts();
|
const ports = await HubManager.queryPorts();
|
||||||
|
|
||||||
if (ports.length === 0) {
|
if (ports.length === 0) {
|
||||||
@ -406,6 +401,9 @@ async function connectHub(): Promise<void> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let portPath: string | undefined = vscode.workspace.getConfiguration('mindreader.connection').get('portPath');
|
||||||
|
|
||||||
|
if (!portPath) {
|
||||||
let slots: vscode.QuickPickItem[] = [];
|
let slots: vscode.QuickPickItem[] = [];
|
||||||
for (const port of ports) {
|
for (const port of ports) {
|
||||||
slots.push({ label: port.path });
|
slots.push({ label: port.path });
|
||||||
@ -417,11 +415,11 @@ async function connectHub(): Promise<void> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
hub = await HubManager.create({ port: picked.label });
|
portPath = picked.label;
|
||||||
vscode.window.showInformationMessage('LEGO Hub connected');
|
|
||||||
}
|
}
|
||||||
|
hub = await HubManager.create(portPath);
|
||||||
|
vscode.window.showInformationMessage('LEGO Hub connected');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// TODO: better handling
|
|
||||||
vscode.window.showErrorMessage('Could not connect to LEGO Hub');
|
vscode.window.showErrorMessage('Could not connect to LEGO Hub');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import * as vscode from 'vscode';
|
|
||||||
import * as SerialPort from 'serialport';
|
import * as SerialPort from 'serialport';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
|
|
||||||
@ -30,17 +29,6 @@ type RPCResponse = {
|
|||||||
'i': string | null;
|
'i': string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @type HubOptions Connection options
|
|
||||||
*
|
|
||||||
* @prop {boolean=} `magic` automatically try and find a suitable port to connect. Defaults to `true`
|
|
||||||
* @prop {string=} `port` port to use if `magic` is disabled. Defaults to `'/dev/ttyACM0'`.
|
|
||||||
*/
|
|
||||||
type HubOptions = {
|
|
||||||
magic?: boolean;
|
|
||||||
port?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages sending and receiving of JSON Remote Procedure Call (JSON-RPC) protocol messages
|
* Manages sending and receiving of JSON Remote Procedure Call (JSON-RPC) protocol messages
|
||||||
* to the Hub.
|
* to the Hub.
|
||||||
@ -48,14 +36,14 @@ type HubOptions = {
|
|||||||
export default class HubManager {
|
export default class HubManager {
|
||||||
private port: SerialPort;
|
private port: SerialPort;
|
||||||
private receiveBuffer: string = ''; // buffer for in-flight messages
|
private receiveBuffer: string = ''; // buffer for in-flight messages
|
||||||
private pendingRequests = new Map<string, [(result: any) => void, (error: string) => void]>();
|
private pendingRequests = new Map<string, [(result: any) => void, (error: string) => void]>(); // lists of requests that are still pending
|
||||||
|
|
||||||
// ======================== INSTANCE METHODS ========================
|
// ======================== INSTANCE METHODS ========================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private constructor, use static `create` + `init`
|
* Private constructor, use static `create` + `init`
|
||||||
*/
|
*/
|
||||||
private constructor(public options: HubOptions) { }
|
private constructor(public portPath: string) { }
|
||||||
|
|
||||||
public isOpen(): boolean {
|
public isOpen(): boolean {
|
||||||
return this.port.isOpen;
|
return this.port.isOpen;
|
||||||
@ -122,7 +110,7 @@ export default class HubManager {
|
|||||||
public async init(): Promise<void> {
|
public async init(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
this.port = new SerialPort(
|
this.port = new SerialPort(
|
||||||
this.options.port!,
|
this.portPath,
|
||||||
{
|
{
|
||||||
autoOpen: true,
|
autoOpen: true,
|
||||||
baudRate: 112500,
|
baudRate: 112500,
|
||||||
@ -359,46 +347,10 @@ export default class HubManager {
|
|||||||
|
|
||||||
// ======================== INSTANCE METHODS ========================
|
// ======================== INSTANCE METHODS ========================
|
||||||
|
|
||||||
public static async create(options?: HubOptions): Promise<HubManager> {
|
public static async create(portPath: string): Promise<HubManager> {
|
||||||
return new Promise(async (resolve) => {
|
return new Promise(async (resolve) => {
|
||||||
// merge passed options into default options
|
|
||||||
options = {
|
|
||||||
port: '/dev/ttyACM0',
|
|
||||||
magic: true,
|
|
||||||
...options
|
|
||||||
};
|
|
||||||
|
|
||||||
let mgr: HubManager;
|
|
||||||
|
|
||||||
// try to detect port automatically
|
|
||||||
if (options.magic) {
|
|
||||||
const availablePorts = await HubManager.queryPorts();
|
|
||||||
|
|
||||||
// get paths from port information
|
|
||||||
const portPaths = availablePorts.map(x => x.path);
|
|
||||||
|
|
||||||
if (portPaths.length > 0) {
|
|
||||||
// try to establish connections to found ports
|
|
||||||
for (const port of portPaths) {
|
|
||||||
try {
|
try {
|
||||||
mgr = new HubManager({ ...options, port });
|
let mgr = new HubManager(portPath);
|
||||||
await mgr.init();
|
|
||||||
|
|
||||||
return resolve(mgr);
|
|
||||||
} catch (err) {
|
|
||||||
// could not connect to port, try next port
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: better error, this will do for now
|
|
||||||
vscode.window.showErrorMessage('Mind_Reader: Magic is enabled but no ports were found. Is the Hub plugged in and turned on');
|
|
||||||
}
|
|
||||||
|
|
||||||
// magic disabled or failed, try normally
|
|
||||||
try {
|
|
||||||
mgr = new HubManager(options);
|
|
||||||
await mgr.init();
|
await mgr.init();
|
||||||
|
|
||||||
return resolve(mgr);
|
return resolve(mgr);
|
||||||
@ -413,15 +365,12 @@ export default class HubManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of serial devices
|
* Returns a list of serial devices
|
||||||
* advertising their manufacturer
|
|
||||||
* as `LEGO System A/S`
|
|
||||||
*/
|
*/
|
||||||
public static async queryPorts() {
|
public static async queryPorts() {
|
||||||
// get all ports
|
// get all ports
|
||||||
let ports = await SerialPort.list();
|
let ports = await SerialPort.list();
|
||||||
|
|
||||||
// filter by manufacturer
|
// filter by manufacturer
|
||||||
ports = ports.filter(x => x.manufacturer === 'LEGO System A/S');
|
|
||||||
return Promise.resolve(ports);
|
return Promise.resolve(ports);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user