Files
assetmgt/api/v2/post/com_log.php
“VeLiTi” bd27bab30f 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.
2025-11-14 14:04:46 +01:00

88 lines
2.6 KiB
PHP

<?php
//defined($security_key) or exit;
//------------------------------------------
// Communication Log API endpoint
// Stores all USB serial communication data in date-organized files
//------------------------------------------
$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
//------------------------------------------
// Application related calls
//------------------------------------------
$action = $request[2];
$post_content = json_decode($input,true);
//SET PARAMETERS FOR QUERY
$date = date('Y-m-d H:i:s');
$current_date = date('Y-m-d');
// Connect to DB (if needed for future enhancements)
$pdo = dbConnect($dbname);
if ($action == 'log'){
// Validate input data
if (!empty($post_content['data']) && isset($post_content['direction'])) {
$communication_data = $post_content['data'];
$direction = $post_content['direction']; // 'sent' or 'received'
$timestamp = $post_content['timestamp'] ?? $date;
$serial_number = $post_content['serial_number'] ?? '';
$maintenance_run = $post_content['maintenance_run'] ?? 0;
// Create log directory if it doesn't exist
$log_dir = './log/';
if (!is_dir($log_dir)) {
mkdir($log_dir, 0755, true);
}
// Create filename based on current date (YYYY-MM-DD)
$log_file = $log_dir .'serial'. $current_date . '.log';
// Format log entry
$log_entry = sprintf(
"[%s] [%s] [SN:%s] [MAINT:%d] %s: %s\n",
$timestamp,
$direction,
$serial_number,
$maintenance_run,
strtoupper($direction),
$communication_data
);
// Append to log file
if (file_put_contents($log_file, $log_entry, FILE_APPEND | LOCK_EX) !== false) {
// Success response
echo json_encode(array(
'status' => 'success',
'message' => 'Communication logged successfully',
'file' => $log_file
));
} else {
// Error response
http_response_code(500);
echo json_encode(array(
'status' => 'error',
'message' => 'Failed to write to log file'
));
}
} else {
// Invalid payload
http_response_code(400);
echo json_encode(array(
'status' => 'error',
'message' => 'Invalid payload: missing data or direction'
));
}
} else {
// Invalid action
http_response_code(400);
echo json_encode(array(
'status' => 'error',
'message' => 'Invalid action'
));
}
?>