mirror of
https://github.com/iNavFlight/inav-configurator.git
synced 2025-07-25 09:15:42 +03:00
Init
This commit is contained in:
parent
939f5af04b
commit
f24ccfc637
96 changed files with 16438 additions and 8058 deletions
|
@ -10,7 +10,7 @@ const ConnectionType = {
|
|||
class Connection {
|
||||
|
||||
constructor() {
|
||||
this._connectionId = false;
|
||||
this._connectionId = 0;
|
||||
this._openRequested = false;
|
||||
this._openCanceled = false;
|
||||
this._bitrate = 0;
|
||||
|
@ -145,7 +145,6 @@ class Connection {
|
|||
} else {
|
||||
this._openRequested = false;
|
||||
console.log('Failed to open');
|
||||
googleAnalytics.sendException('FailedToOpen', false);
|
||||
if (callback) {
|
||||
callback(false);
|
||||
}
|
||||
|
@ -163,13 +162,11 @@ class Connection {
|
|||
this.removeAllListeners();
|
||||
|
||||
this.disconnectImplementation(result => {
|
||||
this.checkChromeLastError();
|
||||
|
||||
if (result) {
|
||||
console.log('Connection with ID: ' + this._connectionId + ' closed, Sent: ' + this._bytesSent + ' bytes, Received: ' + this._bytesReceived + ' bytes');
|
||||
} else {
|
||||
console.log('Failed to close connection with ID: ' + this._connectionId + ' closed, Sent: ' + this._bytesSent + ' bytes, Received: ' + this._bytesReceived + ' bytes');
|
||||
googleAnalytics.sendException('Connection: FailedToClose', false);
|
||||
}
|
||||
|
||||
this._connectionId = false;
|
||||
|
@ -240,12 +237,6 @@ class Connection {
|
|||
}
|
||||
}
|
||||
|
||||
checkChromeLastError() {
|
||||
if (chrome.runtime.lastError) {
|
||||
console.error(chrome.runtime.lastError.message);
|
||||
}
|
||||
}
|
||||
|
||||
addOnReceiveCallback(callback) {
|
||||
throw new TypeError("Abstract method");
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ class ConnectionBle extends Connection {
|
|||
await this.openDevice()
|
||||
.then(() => {
|
||||
this.addOnReceiveErrorListener(error => {
|
||||
GUI.log(chrome.i18n.getMessage('connectionBleInterrupted'));
|
||||
GUI.log(localization.getMessage('connectionBleInterrupted'));
|
||||
this.abort();
|
||||
});
|
||||
|
||||
|
@ -71,7 +71,7 @@ class ConnectionBle extends Connection {
|
|||
});
|
||||
}
|
||||
}).catch(error => {
|
||||
GUI.log(chrome.i18n.getMessage('connectionBleError', [error]));
|
||||
GUI.log(localization.getMessage('connectionBleError', [error]));
|
||||
if (callback) {
|
||||
callback(false);
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ class ConnectionBle extends Connection {
|
|||
return device.gatt.connect()
|
||||
.then(server => {
|
||||
console.log("Connect to: " + device.name);
|
||||
GUI.log(chrome.i18n.getMessage('connectionConnected', [device.name]));
|
||||
GUI.log(localization.getMessage('connectionConnected', [device.name]));
|
||||
return server.getPrimaryServices();
|
||||
}).then(services => {
|
||||
let connectedService = services.find(service => {
|
||||
|
@ -131,7 +131,7 @@ class ConnectionBle extends Connection {
|
|||
throw new Error("Unsupported device (service UUID mismatch).");
|
||||
}
|
||||
|
||||
GUI.log(chrome.i18n.getMessage('connectionBleType', [this._deviceDescription.name]));
|
||||
GUI.log(localization.getMessage('connectionBleType', [this._deviceDescription.name]));
|
||||
return connectedService.getCharacteristics();
|
||||
}).then(characteristics => {
|
||||
characteristics.forEach(characteristic => {
|
||||
|
|
|
@ -1,139 +1,103 @@
|
|||
'use strict'
|
||||
|
||||
const { SerialPortStream } = require('@serialport/stream');
|
||||
const { autoDetect } = require('@serialport/bindings-cpp')
|
||||
const binding = autoDetect();
|
||||
|
||||
class ConnectionSerial extends Connection {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this._failed = 0;
|
||||
this._serialport = null;
|
||||
this._errorListeners = [];
|
||||
this._onReceiveListeners = [];
|
||||
this._onErrorListener = [];
|
||||
}
|
||||
|
||||
connectImplementation(path, options, callback) {
|
||||
chrome.serial.connect(path, options, (connectionInfo) => {
|
||||
this.checkChromeLastError();
|
||||
if (connectionInfo && !this._openCanceled) {
|
||||
this.addOnReceiveErrorListener(info => {
|
||||
console.error(info);
|
||||
googleAnalytics.sendException('Serial: ' + info.error, false);
|
||||
|
||||
switch (info.error) {
|
||||
case 'system_error': // we might be able to recover from this one
|
||||
if (!this._failed++) {
|
||||
chrome.serial.setPaused(this._connectionId, false, function () {
|
||||
SerialCom.getInfo((info) => {
|
||||
if (info) {
|
||||
if (!info.paused) {
|
||||
console.log('SERIAL: Connection recovered from last onReceiveError');
|
||||
googleAnalytics.sendException('Serial: onReceiveError - recovered', false);
|
||||
|
||||
this._failed = 0;
|
||||
} else {
|
||||
console.log('SERIAL: Connection did not recover from last onReceiveError, disconnecting');
|
||||
GUI.log(chrome.i18n.getMessage('serialPortUnrecoverable'));
|
||||
googleAnalytics.sendException('Serial: onReceiveError - unrecoverable', false);
|
||||
|
||||
this.abort();
|
||||
}
|
||||
} else {
|
||||
this.checkChromeLastError();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case 'break': // This occurs on F1 boards with old firmware during reboot
|
||||
case 'overrun':
|
||||
case 'frame_error': //Got disconnected
|
||||
// wait 50 ms and attempt recovery
|
||||
var error = info.error;
|
||||
setTimeout(() => {
|
||||
chrome.serial.setPaused(info.connectionId, false, function() {
|
||||
SerialCom.getInfo(function (info) {
|
||||
if (info) {
|
||||
if (info.paused) {
|
||||
// assume unrecoverable, disconnect
|
||||
console.log('SERIAL: Connection did not recover from ' + error + ' condition, disconnecting');
|
||||
GUI.log(chrome.i18n.getMessage('serialPortUnrecoverable'));;
|
||||
googleAnalytics.sendException('Serial: ' + error + ' - unrecoverable', false);
|
||||
|
||||
this.abort();
|
||||
} else {
|
||||
console.log('SERIAL: Connection recovered from ' + error + ' condition');
|
||||
googleAnalytics.sendException('Serial: ' + error + ' - recovered', false);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}, 50);
|
||||
break;
|
||||
|
||||
case 'timeout':
|
||||
// TODO
|
||||
break;
|
||||
|
||||
case 'device_lost':
|
||||
case 'disconnected':
|
||||
default:
|
||||
this.abort();
|
||||
}
|
||||
connectImplementation(path, options, callback) {
|
||||
this._serialport = new SerialPortStream({binding, path: path, baudRate: options.bitrate, autoOpen: true}, () => {
|
||||
|
||||
this._serialport.on('data', buffer => {
|
||||
this._onReceiveListeners.forEach(listener => {
|
||||
listener({
|
||||
connectionId: this._connectionId,
|
||||
data: buffer
|
||||
});
|
||||
});
|
||||
GUI.log(chrome.i18n.getMessage('connectionConnected', [path]));
|
||||
}
|
||||
})
|
||||
|
||||
this._serialport.on('error', error => {
|
||||
console.log("Serial error: " + error);
|
||||
this._onReceiveErrorListeners.forEach(listener => {
|
||||
listener(error);
|
||||
});
|
||||
});
|
||||
|
||||
if (callback) {
|
||||
callback(connectionInfo);
|
||||
callback({
|
||||
connectionId: ++this._connectionId,
|
||||
bitrate: options.bitrate
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
disconnectImplementation(callback) {
|
||||
chrome.serial.disconnect(this._connectionId, (result) => {
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
});
|
||||
if (this._serialport && this._serialport.isOpen) {
|
||||
this._serialport.close(error => {
|
||||
if (error) {
|
||||
console.log("Unable to close serial: " + error)
|
||||
}
|
||||
if (callback) {
|
||||
callback(error ? false : true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
sendImplementation(data, callback) {
|
||||
chrome.serial.send(this._connectionId, data, callback);
|
||||
}
|
||||
|
||||
addOnReceiveCallback(callback){
|
||||
chrome.serial.onReceive.addListener(callback);
|
||||
}
|
||||
|
||||
removeOnReceiveCallback(callback){
|
||||
chrome.serial.onReceive.removeListener(callback);
|
||||
}
|
||||
|
||||
addOnReceiveErrorCallback(callback) {
|
||||
chrome.serial.onReceiveError.addListener(callback);
|
||||
}
|
||||
|
||||
removeOnReceiveErrorCallback(callback) {
|
||||
chrome.serial.onReceiveError.removeListener(callback);
|
||||
}
|
||||
|
||||
static getDevices(callback) {
|
||||
chrome.serial.getDevices((devices_array) => {
|
||||
var devices = [];
|
||||
devices_array.forEach((device) => {
|
||||
devices.push(device.path);
|
||||
});
|
||||
callback(devices);
|
||||
this._serialport.write(Buffer.from(data), error => {
|
||||
var result = 0;
|
||||
if (error) {
|
||||
result = 1;
|
||||
console.log("Serial wrire error: " + error)
|
||||
}
|
||||
if (callback) {
|
||||
callback({
|
||||
bytesSent: data.byteLength,
|
||||
resultCode: result
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static getInfo(connectionId, callback) {
|
||||
chrome.serial.getInfo(connectionId, callback);
|
||||
addOnReceiveCallback(callback){
|
||||
this._onReceiveErrorListeners.push(callback);
|
||||
}
|
||||
|
||||
static getControlSignals(connectionId, callback) {
|
||||
chrome.serial.getControlSignals(connectionId, callback);
|
||||
removeOnReceiveCallback(callback){
|
||||
this._onReceiveListeners = this._onReceiveErrorListeners.filter(listener => listener !== callback);
|
||||
}
|
||||
|
||||
static setControlSignals(connectionId, signals, callback) {
|
||||
chrome.serial.setControlSignals(connectionId, signals, callback);
|
||||
addOnReceiveErrorCallback(callback) {
|
||||
this._onReceiveErrorListeners.push(callback);
|
||||
}
|
||||
|
||||
removeOnReceiveErrorCallback(callback) {
|
||||
this._onReceiveErrorListeners = this._onReceiveErrorListeners.filter(listener => listener !== callback);
|
||||
}
|
||||
|
||||
static async getDevices(callback) {
|
||||
SerialPort.list().then((ports, error) => {
|
||||
var devices = [];
|
||||
if (error) {
|
||||
GUI.log("Unable to list serial ports.");
|
||||
} else {
|
||||
ports.forEach(port => {
|
||||
devices.push(port.path);
|
||||
});
|
||||
}
|
||||
if (callback)
|
||||
callback(devices);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,7 +43,6 @@ class ConnectionTcp extends Connection {
|
|||
|
||||
this.addOnReceiveErrorListener(info => {
|
||||
console.error(info);
|
||||
googleAnalytics.sendException('TCP: ' + info.error, false);
|
||||
|
||||
let message;
|
||||
switch (info.resultCode) {
|
||||
|
@ -79,7 +78,7 @@ class ConnectionTcp extends Connection {
|
|||
this.abort();
|
||||
});
|
||||
|
||||
GUI.log(chrome.i18n.getMessage('connectionConnected', ["tcp://" + this._connectionIP + ":" + this._connectionPort]));
|
||||
GUI.log(localization.getMessage('connectionConnected', ["tcp://" + this._connectionIP + ":" + this._connectionPort]));
|
||||
|
||||
if (callback) {
|
||||
callback({
|
||||
|
|
|
@ -46,7 +46,7 @@ class ConnectionUdp extends Connection {
|
|||
|
||||
this._timeoutId = setTimeout(() => {
|
||||
if (!this._isCli) { // Disable timeout for CLI
|
||||
GUI.log(chrome.i18n.getMessage('connectionUdpTimeout'));
|
||||
GUI.log(localization.getMessage('connectionUdpTimeout'));
|
||||
this.abort();
|
||||
}
|
||||
}, 10000);
|
||||
|
@ -55,7 +55,6 @@ class ConnectionUdp extends Connection {
|
|||
// Actually useless, but according to chrome documentation also UDP triggers error events ¯\_(ツ)_/¯
|
||||
this.addOnReceiveErrorListener(info => {
|
||||
console.error(info);
|
||||
googleAnalytics.sendException('UDP: ' + info.error, false);
|
||||
|
||||
let message;
|
||||
switch (info.resultCode) {
|
||||
|
@ -91,7 +90,7 @@ class ConnectionUdp extends Connection {
|
|||
this.abort();
|
||||
});
|
||||
|
||||
GUI.log(chrome.i18n.getMessage('connectionConnected', ["udp://" + this._connectionIP + ":" + this._connectionPort]));
|
||||
GUI.log(localization.getMessage('connectionConnected', ["udp://" + this._connectionIP + ":" + this._connectionPort]));
|
||||
|
||||
if (callback) {
|
||||
callback({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue