Skip to content

Commit

Permalink
Improve MSP
Browse files Browse the repository at this point in the history
  • Loading branch information
haslinghuis committed Apr 10, 2023
1 parent ef40e88 commit a4d876d
Showing 1 changed file with 29 additions and 34 deletions.
63 changes: 29 additions & 34 deletions src/js/msp.js
Expand Up @@ -47,6 +47,7 @@ const MSP = {
message_checksum: 0,
messageIsJumboFrame: false,
crcError: false,
sequence: 0,

callbacks: [],
packet_error: 0,
Expand Down Expand Up @@ -320,64 +321,58 @@ const MSP = {
return bufferOut;
},
send_message: function (code, data, callback_sent, callback_msp, doCallbackOnError) {
if (CONFIGURATOR.virtualMode) {
if (code === undefined || !serial.connectionId || CONFIGURATOR.virtualMode) {
if (callback_msp) {
callback_msp();
}
return false;
}

if (code === undefined || !serial.connectionId) {
return false;
for (const instance of MSP.callbacks) {
if (instance.code === code) {
// request already exists in queue, don't add it again
if (callback_msp) {
callback_msp();
}
return false;
}
}

const bufferOut = code <= 254 ? this.encode_message_v1(code, data) : this.encode_message_v2(code, data);

const obj = {
'code': code,
'requestBuffer': bufferOut,
'callback': callback_msp ? callback_msp : false,
'timer': false,
'callbackOnError': doCallbackOnError,
'start': performance.now(),
'sequence': MSP.sequence++,
};

let requestExists = false;

for (const instance of MSP.callbacks) {
if (instance.code === code) {
// request already exist, we will just attach
requestExists = true;
break;
}
}

if (!requestExists) {
obj.timer = setInterval(function () {
console.warn(`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${MSP.timeout} QUEUE: ${MSP.callbacks.length}`);
serial.send(bufferOut, function (_sendInfo) {
obj.stop = performance.now();
const executionTime = Math.round(obj.stop - obj.start);
MSP.timeout = Math.max(MSP.MIN_TIMEOUT, Math.min(executionTime, MSP.MAX_TIMEOUT));
});
obj.timer = setInterval(function () {
console.warn(`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${MSP.timeout} QUEUE: ${MSP.callbacks.length} SEQUENCE: ${obj.sequence}`);
serial.send(bufferOut, function (_sendInfo) {
obj.stop = performance.now();
const executionTime = Math.round(obj.stop - obj.start);
MSP.timeout = Math.max(MSP.MIN_TIMEOUT, Math.min(executionTime, MSP.MAX_TIMEOUT));
});

}, MSP.timeout);
}
}, MSP.timeout);

MSP.callbacks.push(obj);

// always send messages with data payload (even when there is a message already in the queue)
if (data || !requestExists) {
if (MSP.timeout > MSP.MIN_TIMEOUT) {
MSP.timeout--;
serial.send(bufferOut, function (sendInfo) {
if (sendInfo.bytesSent === bufferOut.length) {
if (callback_sent) {
callback_sent();
}
}
});

serial.send(bufferOut, function (sendInfo) {
if (sendInfo.bytesSent === bufferOut.byteLength) {
if (callback_sent) {
callback_sent();
}
}
});
// Decrement timeout if it is above the minimum
if (MSP.timeout > MSP.MIN_TIMEOUT) {
MSP.timeout--;
}

return true;
Expand Down

0 comments on commit a4d876d

Please sign in to comment.