Enhance logging functionality in API and UI components
- Implemented detailed logging for USB serial communication in readdevice.js. - Added log file management features in logfile.php, including deletion and selection of log files. - Created a new communication log API endpoint in com_log.php to store USB communication data. - Improved user interface for log file selection and added confirmation for log deletion.
This commit is contained in:
@@ -1,6 +1,52 @@
|
||||
var port, textEncoder, writableStreamClosed, writer, historyIndex = -1;
|
||||
const lineHistory = [];
|
||||
maintenanceRun = 0;
|
||||
handshakeComplete = false;
|
||||
|
||||
// Function to log communication to API
|
||||
async function logCommunication(data, direction) {
|
||||
// Log all communication including connection/disconnection events
|
||||
try {
|
||||
// Get service token for API authentication
|
||||
const serviceToken = document.getElementById("servicetoken")?.innerHTML || '';
|
||||
|
||||
// Get serial number if available
|
||||
let serialNumber = '';
|
||||
if (typeof serial !== 'undefined' && serial) {
|
||||
serialNumber = serial;
|
||||
}
|
||||
|
||||
const logData = {
|
||||
data: data,
|
||||
direction: direction, // 'sent', 'received', 'connected', 'disconnected', 'handshake'
|
||||
timestamp: new Date().toISOString(),
|
||||
serial_number: serialNumber,
|
||||
maintenance_run: maintenanceRun
|
||||
};
|
||||
|
||||
// Get base URL for API calls (assuming 'link' variable is defined globally)
|
||||
const url = link + '/v2/com_log/log';
|
||||
|
||||
const bearer = 'Bearer ' + serviceToken;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
withCredentials: true,
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Authorization': bearer,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(logData)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.warn('Failed to log communication:', response.status);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Error logging communication:', error);
|
||||
}
|
||||
}
|
||||
|
||||
function progressBar(percentage, message, color){
|
||||
var readbar = document.getElementById("readBar");
|
||||
@@ -14,6 +60,18 @@ async function connectSerial() {
|
||||
// Prompt user to select any serial port.
|
||||
const filters = [{ usbVendorId: 1027, usbProductId: 24597 }];
|
||||
port = await navigator.serial.requestPort({ filters });
|
||||
|
||||
// Log selected port details
|
||||
const portInfo = port.getInfo();
|
||||
const portDetails = {
|
||||
usbVendorId: portInfo.usbVendorId,
|
||||
usbProductId: portInfo.usbProductId,
|
||||
readable: !!port.readable,
|
||||
writable: !!port.writable,
|
||||
opened: port.readable !== null && port.writable !== null
|
||||
};
|
||||
await logCommunication(`Selected USB device - ${JSON.stringify(portDetails)}`, 'connected');
|
||||
|
||||
await port.open({ baudRate: 56700 });
|
||||
|
||||
listenToPort();
|
||||
@@ -22,7 +80,20 @@ async function connectSerial() {
|
||||
writableStreamClosed = textEncoder.readable.pipeTo(port.writable);
|
||||
|
||||
writer = textEncoder.writable.getWriter();
|
||||
} catch {
|
||||
|
||||
// Log successful connection with details
|
||||
const portInfoSuccess = port.getInfo();
|
||||
const portDetailsSuccess = {
|
||||
usbVendorId: portInfoSuccess.usbVendorId,
|
||||
usbProductId: portInfoSuccess.usbProductId,
|
||||
readable: !!port.readable,
|
||||
writable: !!port.writable,
|
||||
opened: port.readable !== null && port.writable !== null
|
||||
};
|
||||
await logCommunication(`Serial port opened successfully (baudRate: 56700) - ${JSON.stringify(portDetailsSuccess)}`, 'connected');
|
||||
} catch (error) {
|
||||
// Log connection failure details
|
||||
await logCommunication(`Serial connection failed: ${error.message || 'Unknown error'}`, 'disconnected');
|
||||
alert("Serial Connection Failed");
|
||||
}
|
||||
}
|
||||
@@ -36,6 +107,10 @@ async function sendSerialLine() {
|
||||
dataToSend = dataToSend + "\n"; //new line
|
||||
appendToTerminal("> " + dataToSend); //send echo
|
||||
await writer.write(dataToSend);
|
||||
|
||||
// Log sent data
|
||||
await logCommunication(dataToSend.trim(), 'sent');
|
||||
|
||||
document.getElementById("lineToSend").value = "";
|
||||
//await writer.releaseLock();
|
||||
}
|
||||
@@ -194,6 +269,10 @@ function writeToStream(command, payload){
|
||||
dataToSend = command + payload + limiter;
|
||||
console.log(dataToSend);
|
||||
writer.write(dataToSend);
|
||||
|
||||
// Log sent data
|
||||
logCommunication(dataToSend.trim(), 'sent');
|
||||
|
||||
maintenanceRun++;
|
||||
console.log(maintenanceRun);
|
||||
maintenanceTest();
|
||||
@@ -218,6 +297,8 @@ async function listenToPort() {
|
||||
if (x.indexOf("STATE=MAINTENANCE") > 0 && maintenanceRun == 1)
|
||||
{
|
||||
progressBar("50", "Initializing program" , "#ff6666")
|
||||
handshakeComplete = true; // Mark handshake as complete
|
||||
await logCommunication('Handshake complete - entering maintenance mode', 'handshake');
|
||||
setTimeout(maintenanceTest,3000);
|
||||
maintenanceRun = 2;
|
||||
}
|
||||
@@ -230,6 +311,18 @@ const plug_data = document.getElementById("plug_data");
|
||||
|
||||
async function appendToTerminal(newStuff) {
|
||||
serialResultsDiv.innerHTML += newStuff;
|
||||
|
||||
// Log received data - check if this is handshake data
|
||||
let logDirection = 'received';
|
||||
if (!handshakeComplete && maintenanceRun === 0) {
|
||||
// Check if this contains device identification info (handshake)
|
||||
if (newStuff.includes('PN=') || newStuff.includes('SN=') || newStuff.includes('HW=') ||
|
||||
newStuff.includes('FW=') || newStuff.includes('STATE=')) {
|
||||
logDirection = 'handshake';
|
||||
}
|
||||
}
|
||||
|
||||
await logCommunication(newStuff.trim(), logDirection);
|
||||
|
||||
const keyword = '$STATUS;STATE';
|
||||
const keyword2 = 'PWM';
|
||||
@@ -431,12 +524,33 @@ function getData() {
|
||||
|
||||
async function closePort(){
|
||||
|
||||
reader.cancel();
|
||||
await readableStreamClosed.catch(() => { /* Ignore the error */ });
|
||||
writer.close();
|
||||
await writableStreamClosed
|
||||
console.log(maintenanceRun);
|
||||
await port.close();
|
||||
try {
|
||||
// Log port closure start
|
||||
const portInfo = port.getInfo();
|
||||
const portDetails = {
|
||||
usbVendorId: portInfo.usbVendorId,
|
||||
usbProductId: portInfo.usbProductId,
|
||||
readable: !!port.readable,
|
||||
writable: !!port.writable,
|
||||
opened: port.readable !== null && port.writable !== null
|
||||
};
|
||||
await logCommunication(`Starting port closure - ${JSON.stringify(portDetails)}`, 'disconnected');
|
||||
|
||||
reader.cancel();
|
||||
await readableStreamClosed.catch(() => { /* Ignore the error */ });
|
||||
writer.close();
|
||||
await writableStreamClosed;
|
||||
console.log(maintenanceRun);
|
||||
await port.close();
|
||||
|
||||
// Log successful port closure
|
||||
await logCommunication(`Serial port closed successfully - VendorID: ${portInfo.usbVendorId}, ProductID: ${portInfo.usbProductId}`, 'disconnected');
|
||||
} catch (error) {
|
||||
// Log port closure failure
|
||||
await logCommunication(`Serial port closure failed: ${error.message || 'Unknown error'}`, 'disconnected');
|
||||
}
|
||||
|
||||
handshakeComplete = false; // Reset handshake for next connection
|
||||
|
||||
//Check for errors maintenanceRun = 999
|
||||
if (maintenanceRun != 999){
|
||||
@@ -463,12 +577,33 @@ async function closePort(){
|
||||
|
||||
async function closePortCarTest(){
|
||||
|
||||
reader.cancel();
|
||||
await readableStreamClosed.catch(() => { /* Ignore the error */ });
|
||||
writer.close();
|
||||
await writableStreamClosed
|
||||
console.log(maintenanceRun);
|
||||
await port.close();
|
||||
try {
|
||||
// Log port closure start
|
||||
const portInfo = port.getInfo();
|
||||
const portDetails = {
|
||||
usbVendorId: portInfo.usbVendorId,
|
||||
usbProductId: portInfo.usbProductId,
|
||||
readable: !!port.readable,
|
||||
writable: !!port.writable,
|
||||
opened: port.readable !== null && port.writable !== null
|
||||
};
|
||||
await logCommunication(`Starting port closure (car test) - ${JSON.stringify(portDetails)}`, 'disconnected');
|
||||
|
||||
reader.cancel();
|
||||
await readableStreamClosed.catch(() => { /* Ignore the error */ });
|
||||
writer.close();
|
||||
await writableStreamClosed;
|
||||
console.log(maintenanceRun);
|
||||
await port.close();
|
||||
|
||||
// Log successful port closure
|
||||
await logCommunication(`Serial port closed successfully (car test) - VendorID: ${portInfo.usbVendorId}, ProductID: ${portInfo.usbProductId}`, 'disconnected');
|
||||
} catch (error) {
|
||||
// Log port closure failure
|
||||
await logCommunication(`Serial port closure failed (car test): ${error.message || 'Unknown error'}`, 'disconnected');
|
||||
}
|
||||
|
||||
handshakeComplete = false; // Reset handshake for next connection
|
||||
|
||||
//Check for errors maintenanceRun = 999
|
||||
if (maintenanceRun != 999){
|
||||
|
||||
Reference in New Issue
Block a user