优化usb外设插入时重复触发的问题
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
#define DEFAULT_POLL_INTERVAL 5 // 默认轮询间隔为5秒
|
#define DEFAULT_POLL_INTERVAL 5 // 默认轮询间隔为5秒
|
||||||
#define MAX_RETRY_ATTEMPTS 3 // 最大重试次数
|
#define MAX_RETRY_ATTEMPTS 3 // 最大重试次数
|
||||||
#define RETRY_DELAY 1 // 重试延迟(秒)
|
#define RETRY_DELAY 1 // 重试延迟(秒)
|
||||||
|
#define COOLDOWN_SECONDS 30 // 触摸屏检测冷却时间(秒)
|
||||||
|
|
||||||
FILE *log_file = NULL; // 日志文件指针
|
FILE *log_file = NULL; // 日志文件指针
|
||||||
// 函数提前声明
|
// 函数提前声明
|
||||||
@@ -213,6 +214,7 @@ void monitor_usb_devices(const char *control_file_path, int poll_interval) {
|
|||||||
printf("按Ctrl+C退出\n\n");
|
printf("按Ctrl+C退出\n\n");
|
||||||
|
|
||||||
// 主循环,监控设备事件
|
// 主循环,监控设备事件
|
||||||
|
time_t last_trigger_time = 0; // 上次触发控制文件的时间
|
||||||
while (keep_running) {
|
while (keep_running) {
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
@@ -292,9 +294,14 @@ void monitor_usb_devices(const char *control_file_path, int poll_interval) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 一轮检测完成后,如果需要更新控制文件状态
|
// 一轮检测完成后,如果需要更新控制文件状态(带冷却时间防重复触发)
|
||||||
if (use_control_file && touchscreen_detected) {
|
if (use_control_file && touchscreen_detected) {
|
||||||
|
time_t now = time(NULL);
|
||||||
|
if (difftime(now, last_trigger_time) < COOLDOWN_SECONDS) {
|
||||||
|
log_message("冷却中,跳过本次触发(距上次 %.0f 秒)\n", difftime(now, last_trigger_time));
|
||||||
|
} else {
|
||||||
if (write_file_content_with_retry(control_file_path, 1) == 0) {
|
if (write_file_content_with_retry(control_file_path, 1) == 0) {
|
||||||
|
last_trigger_time = time(NULL);
|
||||||
log_message("已更新控制文件状态为1\n");
|
log_message("已更新控制文件状态为1\n");
|
||||||
|
|
||||||
// 等待文件状态恢复为0
|
// 等待文件状态恢复为0
|
||||||
@@ -317,6 +324,7 @@ void monitor_usb_devices(const char *control_file_path, int poll_interval) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 清理资源
|
// 清理资源
|
||||||
udev_monitor_unref(mon);
|
udev_monitor_unref(mon);
|
||||||
|
|||||||
Reference in New Issue
Block a user