weight_dispatcher.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * weight_dispatcher.h — v43 并行称重调度器
  3. *
  4. * fix24 v43 核心改造:
  5. * tb_num 确定后立即启动独立重量线程,与照片上传并行执行。
  6. * 重量读取不再依赖照片上传成功,两路通过 tb_num 原子关联。
  7. *
  8. * 设计要点:
  9. * 1. dispatch() 在 save_file 后立即调用(main.cpp)
  10. * 2. 独立线程执行重量读取 + 平台上传 + DB 写入
  11. * 3. MQTT 去重 key 从 plate 改为 tb_num
  12. * 4. [sync] enabled=0 时完全不使用此模块,退回 v41 路径
  13. */
  14. #ifndef WEIGHT_DISPATCHER_H
  15. #define WEIGHT_DISPATCHER_H
  16. #include "common.h"
  17. #include <string>
  18. #include <atomic>
  19. // ==================== 重量任务结果 ====================
  20. struct WeightDispatchResult {
  21. std::string plate;
  22. std::string tb_num;
  23. int station_type = 0; // 1=进站 2=出站
  24. double weight_kg = 0.0;
  25. int read_status = 0; // 1=读取成功 2=读取失败/超时
  26. int upload_status = 0; // 0=未上传 1=上传成功 2=上传失败
  27. std::string response_msg;
  28. bool success() const { return read_status == 1 && upload_status == 1; }
  29. };
  30. // ==================== 公开接口 ====================
  31. /**
  32. * 启动重量调度器(主进程初始化时调用一次)
  33. */
  34. void weight_dispatcher_init();
  35. /**
  36. * 关闭重量调度器(等待所有进行中的任务完成)
  37. */
  38. void weight_dispatcher_shutdown();
  39. /**
  40. * tb_num 确定后立即调用,启动独立重量线程。
  41. * 非阻塞,立即返回。
  42. *
  43. * @param plate 车牌号
  44. * @param tb_num 联单编号(核心关联 key)
  45. * @param station_type 1=进站 2=出站
  46. */
  47. void weight_dispatch_async(const std::string& plate,
  48. const std::string& tb_num,
  49. int station_type);
  50. /**
  51. * 按 tb_num 查询重量结果(供 Reporter / upload_thread 等待)。
  52. * 阻塞直到结果可用或超时。
  53. *
  54. * @param tb_num 联单编号
  55. * @param timeout_sec 最大等待秒数(默认 30s)
  56. * @param[out] result 结果输出
  57. * @return true=结果已填充, false=超时未等到
  58. */
  59. bool weight_dispatch_wait_result(const std::string& tb_num,
  60. int timeout_sec,
  61. WeightDispatchResult& result);
  62. /**
  63. * 检查指定 tb_num 的重量任务是否已完成(非阻塞)
  64. */
  65. bool weight_dispatch_is_ready(const std::string& tb_num);
  66. /**
  67. * 获取当前进行中的重量任务数量(用于诊断)
  68. */
  69. int weight_dispatch_pending_count();
  70. /**
  71. * 清理指定 tb_num 的 map entry(防止照片上传失败时 entry 泄漏)。
  72. * 如果 entry 不存在或已被消费,则无操作。
  73. */
  74. void weight_dispatch_cleanup(const std::string& tb_num);
  75. // ==================== tb_num 去重(替代旧的 plate 去重) ====================
  76. /**
  77. * 检查 tb_num 是否在去重窗口内已处理过。
  78. * 如果未处理,记录并返回 false;如果已处理,返回 true。
  79. */
  80. bool weight_dispatch_is_tb_num_dup(const std::string& tb_num);
  81. #endif