| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /**
- * weight_dispatcher.h — v43 并行称重调度器
- *
- * fix24 v43 核心改造:
- * tb_num 确定后立即启动独立重量线程,与照片上传并行执行。
- * 重量读取不再依赖照片上传成功,两路通过 tb_num 原子关联。
- *
- * 设计要点:
- * 1. dispatch() 在 save_file 后立即调用(main.cpp)
- * 2. 独立线程执行重量读取 + 平台上传 + DB 写入
- * 3. MQTT 去重 key 从 plate 改为 tb_num
- * 4. [sync] enabled=0 时完全不使用此模块,退回 v41 路径
- */
- #ifndef WEIGHT_DISPATCHER_H
- #define WEIGHT_DISPATCHER_H
- #include "common.h"
- #include <string>
- #include <atomic>
- // ==================== 重量任务结果 ====================
- struct WeightDispatchResult {
- std::string plate;
- std::string tb_num;
- int station_type = 0; // 1=进站 2=出站
- double weight_kg = 0.0;
- int read_status = 0; // 1=读取成功 2=读取失败/超时
- int upload_status = 0; // 0=未上传 1=上传成功 2=上传失败
- std::string response_msg;
- bool success() const { return read_status == 1 && upload_status == 1; }
- };
- // ==================== 公开接口 ====================
- /**
- * 启动重量调度器(主进程初始化时调用一次)
- */
- void weight_dispatcher_init();
- /**
- * 关闭重量调度器(等待所有进行中的任务完成)
- */
- void weight_dispatcher_shutdown();
- /**
- * tb_num 确定后立即调用,启动独立重量线程。
- * 非阻塞,立即返回。
- *
- * @param plate 车牌号
- * @param tb_num 联单编号(核心关联 key)
- * @param station_type 1=进站 2=出站
- */
- void weight_dispatch_async(const std::string& plate,
- const std::string& tb_num,
- int station_type);
- /**
- * 按 tb_num 查询重量结果(供 Reporter / upload_thread 等待)。
- * 阻塞直到结果可用或超时。
- *
- * @param tb_num 联单编号
- * @param timeout_sec 最大等待秒数(默认 30s)
- * @param[out] result 结果输出
- * @return true=结果已填充, false=超时未等到
- */
- bool weight_dispatch_wait_result(const std::string& tb_num,
- int timeout_sec,
- WeightDispatchResult& result);
- /**
- * 检查指定 tb_num 的重量任务是否已完成(非阻塞)
- */
- bool weight_dispatch_is_ready(const std::string& tb_num);
- /**
- * 获取当前进行中的重量任务数量(用于诊断)
- */
- int weight_dispatch_pending_count();
- /**
- * 清理指定 tb_num 的 map entry(防止照片上传失败时 entry 泄漏)。
- * 如果 entry 不存在或已被消费,则无操作。
- */
- void weight_dispatch_cleanup(const std::string& tb_num);
- // ==================== tb_num 去重(替代旧的 plate 去重) ====================
- /**
- * 检查 tb_num 是否在去重窗口内已处理过。
- * 如果未处理,记录并返回 false;如果已处理,返回 true。
- */
- bool weight_dispatch_is_tb_num_dup(const std::string& tb_num);
- #endif
|