mirror of
https://github.com/iNavFlight/inav-configurator.git
synced 2025-07-14 11:59:51 +03:00
reworked serial layer (more verbose), added checks for chrome.runtime.lastError in connect and disconnect api calls
This commit is contained in:
parent
f241c0f70d
commit
de87fab530
3 changed files with 79 additions and 66 deletions
|
@ -25,7 +25,7 @@ function startApplication() {
|
||||||
valid_connection = createdWindow.contentWindow.CONFIGURATOR.connectionValid,
|
valid_connection = createdWindow.contentWindow.CONFIGURATOR.connectionValid,
|
||||||
mincommand = createdWindow.contentWindow.MISC.mincommand;
|
mincommand = createdWindow.contentWindow.MISC.mincommand;
|
||||||
|
|
||||||
if (connectionId > 0 && valid_connection) {
|
if (connectionId && valid_connection) {
|
||||||
// code below is handmade MSP message (without pretty JS wrapper), it behaves exactly like MSP.send_message
|
// code below is handmade MSP message (without pretty JS wrapper), it behaves exactly like MSP.send_message
|
||||||
// reset motors to default (mincommand)
|
// reset motors to default (mincommand)
|
||||||
var bufferOut = new ArrayBuffer(22),
|
var bufferOut = new ArrayBuffer(22),
|
||||||
|
@ -55,7 +55,7 @@ function startApplication() {
|
||||||
console.log('SERIAL: Connection closed - ' + result);
|
console.log('SERIAL: Connection closed - ' + result);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else if (connectionId > 0) {
|
} else if (connectionId) {
|
||||||
chrome.serial.disconnect(connectionId, function (result) {
|
chrome.serial.disconnect(connectionId, function (result) {
|
||||||
console.log('SERIAL: Connection closed - ' + result);
|
console.log('SERIAL: Connection closed - ' + result);
|
||||||
});
|
});
|
||||||
|
|
|
@ -13,11 +13,11 @@ var PortUsage = {
|
||||||
},
|
},
|
||||||
update: function() {
|
update: function() {
|
||||||
if (serial.bitrate) {
|
if (serial.bitrate) {
|
||||||
var port_usage_down = parseInt(((serial.bytes_received - this.previous_received) * 10 / serial.bitrate) * 100);
|
var port_usage_down = parseInt(((serial.bytesReceived - this.previous_received) * 10 / serial.bitrate) * 100);
|
||||||
var port_usage_up = parseInt(((serial.bytes_sent - this.previous_sent) * 10 / serial.bitrate) * 100);
|
var port_usage_up = parseInt(((serial.bytesSent - this.previous_sent) * 10 / serial.bitrate) * 100);
|
||||||
|
|
||||||
this.previous_received = serial.bytes_received;
|
this.previous_received = serial.bytesReceived;
|
||||||
this.previous_sent = serial.bytes_sent;
|
this.previous_sent = serial.bytesSent;
|
||||||
|
|
||||||
// update UI
|
// update UI
|
||||||
$('span.port_usage_down').text(chrome.i18n.getMessage('statusbar_usage_download', [port_usage_down]));
|
$('span.port_usage_down').text(chrome.i18n.getMessage('statusbar_usage_download', [port_usage_down]));
|
||||||
|
|
85
js/serial.js
85
js/serial.js
|
@ -1,29 +1,36 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var serial = {
|
var serial = {
|
||||||
connectionId: -1,
|
connectionId: false,
|
||||||
canceled: false,
|
openRequested: false,
|
||||||
|
openCanceled: false,
|
||||||
bitrate: 0,
|
bitrate: 0,
|
||||||
bytes_received: 0,
|
bytesReceived: 0,
|
||||||
bytes_sent: 0,
|
bytesSent: 0,
|
||||||
failed: 0,
|
failed: 0,
|
||||||
|
|
||||||
transmitting: false,
|
transmitting: false,
|
||||||
output_buffer: [],
|
outputBuffer: [],
|
||||||
|
|
||||||
connect: function (path, options, callback) {
|
connect: function (path, options, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
self.openRequested = true;
|
||||||
|
|
||||||
chrome.serial.connect(path, options, function (connectionInfo) {
|
chrome.serial.connect(path, options, function (connectionInfo) {
|
||||||
if (connectionInfo && !self.canceled) {
|
if (chrome.runtime.lastError) {
|
||||||
|
console.error(chrome.runtime.lastError.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connectionInfo && !self.openCanceled) {
|
||||||
self.connectionId = connectionInfo.connectionId;
|
self.connectionId = connectionInfo.connectionId;
|
||||||
self.bitrate = connectionInfo.bitrate;
|
self.bitrate = connectionInfo.bitrate;
|
||||||
self.bytes_received = 0;
|
self.bytesReceived = 0;
|
||||||
self.bytes_sent = 0;
|
self.bytesSent = 0;
|
||||||
self.failed = 0;
|
self.failed = 0;
|
||||||
|
self.openRequested = false;
|
||||||
|
|
||||||
self.onReceive.addListener(function log_bytes_received(info) {
|
self.onReceive.addListener(function log_bytesReceived(info) {
|
||||||
self.bytes_received += info.data.byteLength;
|
self.bytesReceived += info.data.byteLength;
|
||||||
});
|
});
|
||||||
|
|
||||||
self.onReceiveError.addListener(function watch_for_on_receive_errors(info) {
|
self.onReceiveError.addListener(function watch_for_on_receive_errors(info) {
|
||||||
|
@ -76,7 +83,7 @@ var serial = {
|
||||||
console.log('SERIAL: Connection opened with ID: ' + connectionInfo.connectionId + ', Baud: ' + connectionInfo.bitrate);
|
console.log('SERIAL: Connection opened with ID: ' + connectionInfo.connectionId + ', Baud: ' + connectionInfo.bitrate);
|
||||||
|
|
||||||
if (callback) callback(connectionInfo);
|
if (callback) callback(connectionInfo);
|
||||||
} else if (connectionInfo && self.canceled) {
|
} else if (connectionInfo && self.openCanceled) {
|
||||||
// connection opened, but this connect sequence was canceled
|
// connection opened, but this connect sequence was canceled
|
||||||
// we will disconnect without triggering any callbacks
|
// we will disconnect without triggering any callbacks
|
||||||
self.connectionId = connectionInfo.connectionId;
|
self.connectionId = connectionInfo.connectionId;
|
||||||
|
@ -84,17 +91,20 @@ var serial = {
|
||||||
|
|
||||||
// some bluetooth dongles/dongle drivers really doesn't like to be closed instantly, adding a small delay
|
// some bluetooth dongles/dongle drivers really doesn't like to be closed instantly, adding a small delay
|
||||||
setTimeout(function initialization() {
|
setTimeout(function initialization() {
|
||||||
self.canceled = false;
|
self.openRequested = false;
|
||||||
|
self.openCanceled = false;
|
||||||
self.disconnect(function resetUI() {
|
self.disconnect(function resetUI() {
|
||||||
if (callback) callback(false);
|
if (callback) callback(false);
|
||||||
});
|
});
|
||||||
}, 150);
|
}, 150);
|
||||||
} else if (self.canceled) {
|
} else if (self.openCanceled) {
|
||||||
// connection didn't open and sequence was canceled, so we will do nothing
|
// connection didn't open and sequence was canceled, so we will do nothing
|
||||||
console.log('SERIAL: Connection didn\'t open and request was canceled');
|
console.log('SERIAL: Connection didn\'t open and request was canceled');
|
||||||
self.canceled = false;
|
self.openRequested = false;
|
||||||
|
self.openCanceled = false;
|
||||||
if (callback) callback(false);
|
if (callback) callback(false);
|
||||||
} else {
|
} else {
|
||||||
|
self.openRequested = false;
|
||||||
console.log('SERIAL: Failed to open serial port');
|
console.log('SERIAL: Failed to open serial port');
|
||||||
googleAnalytics.sendException('Serial: FailedToOpen', false);
|
googleAnalytics.sendException('Serial: FailedToOpen', false);
|
||||||
if (callback) callback(false);
|
if (callback) callback(false);
|
||||||
|
@ -104,8 +114,8 @@ var serial = {
|
||||||
disconnect: function (callback) {
|
disconnect: function (callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
if (self.connectionId > -1) {
|
if (self.connectionId) {
|
||||||
self.empty_output_buffer();
|
self.empty_outputBuffer();
|
||||||
|
|
||||||
// remove listeners
|
// remove listeners
|
||||||
for (var i = (self.onReceive.listeners.length - 1); i >= 0; i--) {
|
for (var i = (self.onReceive.listeners.length - 1); i >= 0; i--) {
|
||||||
|
@ -117,14 +127,18 @@ var serial = {
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.serial.disconnect(this.connectionId, function (result) {
|
chrome.serial.disconnect(this.connectionId, function (result) {
|
||||||
|
if (chrome.runtime.lastError) {
|
||||||
|
console.error(chrome.runtime.lastError.message);
|
||||||
|
}
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
console.log('SERIAL: Connection with ID: ' + self.connectionId + ' closed, Sent: ' + self.bytes_sent + ' bytes, Received: ' + self.bytes_received + ' bytes');
|
console.log('SERIAL: Connection with ID: ' + self.connectionId + ' closed, Sent: ' + self.bytesSent + ' bytes, Received: ' + self.bytesReceived + ' bytes');
|
||||||
} else {
|
} else {
|
||||||
console.log('SERIAL: Failed to close connection with ID: ' + self.connectionId + ' closed, Sent: ' + self.bytes_sent + ' bytes, Received: ' + self.bytes_received + ' bytes');
|
console.log('SERIAL: Failed to close connection with ID: ' + self.connectionId + ' closed, Sent: ' + self.bytesSent + ' bytes, Received: ' + self.bytesReceived + ' bytes');
|
||||||
googleAnalytics.sendException('Serial: FailedToClose', false);
|
googleAnalytics.sendException('Serial: FailedToClose', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.connectionId = -1;
|
self.connectionId = false;
|
||||||
self.bitrate = 0;
|
self.bitrate = 0;
|
||||||
|
|
||||||
if (callback) callback(result);
|
if (callback) callback(result);
|
||||||
|
@ -132,7 +146,7 @@ var serial = {
|
||||||
} else {
|
} else {
|
||||||
// connection wasn't opened, so we won't try to close anything
|
// connection wasn't opened, so we won't try to close anything
|
||||||
// instead we will rise canceled flag which will prevent connect from continueing further after being canceled
|
// instead we will rise canceled flag which will prevent connect from continueing further after being canceled
|
||||||
self.canceled = true;
|
self.openCanceled = true;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getDevices: function (callback) {
|
getDevices: function (callback) {
|
||||||
|
@ -156,34 +170,31 @@ var serial = {
|
||||||
},
|
},
|
||||||
send: function (data, callback) {
|
send: function (data, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.output_buffer.push({'data': data, 'callback': callback});
|
this.outputBuffer.push({'data': data, 'callback': callback});
|
||||||
|
|
||||||
if (!this.transmitting) {
|
function send() {
|
||||||
this.transmitting = true;
|
|
||||||
|
|
||||||
var send = function () {
|
|
||||||
// store inside separate variables in case array gets destroyed
|
// store inside separate variables in case array gets destroyed
|
||||||
var data = self.output_buffer[0].data,
|
var data = self.outputBuffer[0].data,
|
||||||
callback = self.output_buffer[0].callback;
|
callback = self.outputBuffer[0].callback;
|
||||||
|
|
||||||
chrome.serial.send(self.connectionId, data, function (sendInfo) {
|
chrome.serial.send(self.connectionId, data, function (sendInfo) {
|
||||||
// track sent bytes for statistics
|
// track sent bytes for statistics
|
||||||
self.bytes_sent += sendInfo.bytesSent;
|
self.bytesSent += sendInfo.bytesSent;
|
||||||
|
|
||||||
// fire callback
|
// fire callback
|
||||||
if (callback) callback(sendInfo);
|
if (callback) callback(sendInfo);
|
||||||
|
|
||||||
// remove data for current transmission form the buffer
|
// remove data for current transmission form the buffer
|
||||||
self.output_buffer.shift();
|
self.outputBuffer.shift();
|
||||||
|
|
||||||
// if there is any data in the queue fire send immediately, otherwise stop trasmitting
|
// if there is any data in the queue fire send immediately, otherwise stop trasmitting
|
||||||
if (self.output_buffer.length) {
|
if (self.outputBuffer.length) {
|
||||||
// keep the buffer withing reasonable limits
|
// keep the buffer withing reasonable limits
|
||||||
if (self.output_buffer.length > 100) {
|
if (self.outputBuffer.length > 100) {
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
|
|
||||||
while (self.output_buffer.length > 100) {
|
while (self.outputBuffer.length > 100) {
|
||||||
self.output_buffer.pop();
|
self.outputBuffer.pop();
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,8 +206,10 @@ var serial = {
|
||||||
self.transmitting = false;
|
self.transmitting = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
|
if (!this.transmitting) {
|
||||||
|
this.transmitting = true;
|
||||||
send();
|
send();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -236,8 +249,8 @@ var serial = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
empty_output_buffer: function () {
|
empty_outputBuffer: function () {
|
||||||
this.output_buffer = [];
|
this.outputBuffer = [];
|
||||||
this.transmitting = false;
|
this.transmitting = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
Loading…
Add table
Add a link
Reference in a new issue