From 6e9047b0ae9770c481e58b423c001d468c17bb6d Mon Sep 17 00:00:00 2001 From: lzq Date: Tue, 15 Jul 2025 18:03:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/application.cpp | 2 +- src/barrier/barrier.h | 5 +-- src/barrier/generic_barrier.cpp | 38 +++++++++-------- src/barrier/generic_barrier.h | 4 +- src/common/daily_and_size_sink.cpp | 24 +++++------ src/common/daily_and_size_sink.h | 6 +-- src/common/date_util.cpp | 19 +++++++++ src/common/date_util.h | 26 ++++++++++++ src/common/sys_util.cpp | 14 ------ src/common/thread_pool.h | 2 +- src/recognize_processor.cpp | 68 ++++++++++++++++++++++++++++++ src/recognize_processor.h | 15 +++++++ src/vidicon/barrier_vidicon.cpp | 11 +---- 13 files changed, 171 insertions(+), 63 deletions(-) create mode 100644 src/common/date_util.cpp create mode 100644 src/common/date_util.h create mode 100644 src/recognize_processor.cpp create mode 100644 src/recognize_processor.h diff --git a/src/application.cpp b/src/application.cpp index 2a1ece3..a941533 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -111,7 +111,7 @@ namespace zsy LOGGER_INFO("等待..."); std::this_thread::sleep_for(std::chrono::milliseconds(appProperties.delay * 1000)); } - threadPool = std::make_shared(4); + threadPool = std::make_shared(); eventManager = std::make_shared(threadPool); diff --git a/src/barrier/barrier.h b/src/barrier/barrier.h index ac04ab6..644de41 100644 --- a/src/barrier/barrier.h +++ b/src/barrier/barrier.h @@ -5,17 +5,16 @@ namespace zsy { - class Barrier { public: - virtual ~Barrier() = default; virtual void open() = 0; - virtual void resolveRecognizeResult(const std::string & license,const std::string & imageFile) = 0; + virtual bool isFront() = 0; + // virtual void resolveRecognizeResult(const std::string &license, const std::string &imageFile) = 0; }; } // zsy diff --git a/src/barrier/generic_barrier.cpp b/src/barrier/generic_barrier.cpp index 813237c..5b50b08 100644 --- a/src/barrier/generic_barrier.cpp +++ b/src/barrier/generic_barrier.cpp @@ -1,20 +1,14 @@ #include "generic_barrier.h" #include "application.h" +#include "recognize_processor.h" +#include "common/date_util.h" #include "common/loging.h" #include "common/snowflake.h" #include "common/sys_util.h" namespace zsy { - static long long getTime() - { - const auto now = std::chrono::system_clock::now(); - const auto now_ms = std::chrono::time_point_cast(now); - const auto epoch = now_ms.time_since_epoch(); - return std::chrono::duration_cast(epoch).count(); - } - GenericBarrier::GenericBarrier(const BarrierProperties &config) : config(config) { @@ -75,15 +69,18 @@ namespace zsy res.set_content(R"({})", "application/json"); return; } - Application::threadPool->submit([imageFile,license, barrier = Application::deviceHolder->getBarrier(serialno)] + auto barrier = Application::deviceHolder->getBarrier(serialno); + if (!barrier) { - if (!barrier) - { - LOGGER_ERROR("未找到对应的道闸"); - return; - } - barrier->resolveRecognizeResult(license, imageFile); + LOGGER_ERROR("未找到对应的道闸"); + return; + } + + std::thread t([front = barrier->isFront(), serialno,license,imageFile] + { + RecognizeProcessor::process(front, serialno, license, imageFile); }); + t.detach(); } catch (std::exception &e) { LOGGER_ERROR("车牌识别结果处理失败: {}", e.what()); @@ -101,7 +98,7 @@ namespace zsy void GenericBarrier::open() { LOGGER_INFO("收到开门请求:{} {}", config.name, config.sn); - const long timestamp = static_cast(getTime()); + const long timestamp = static_cast(DateUtil::timestamp() / 1000); BarrierBcd barrier; barrier.id = "open" + std::to_string(timestamp); barrier.sn = config.sn; @@ -135,6 +132,11 @@ namespace zsy } } + bool GenericBarrier::isFront() + { + return config.name.find("前置") != std::string::npos; + } + std::tuple GenericBarrier::parseRecognizeResult(const std::string &jsonStr) { try @@ -153,7 +155,7 @@ namespace zsy } } - void GenericBarrier::resolveRecognizeResult(const std::string &license, const std::string &imageFile) + /*void GenericBarrier::resolveRecognizeResult(const std::string &license, const std::string &imageFile) { try { @@ -224,5 +226,5 @@ namespace zsy { LOGGER_ERROR("未知异常,车牌识别结果处理失败"); } - } + }*/ } // zsy diff --git a/src/barrier/generic_barrier.h b/src/barrier/generic_barrier.h index 8802af8..c6fc634 100644 --- a/src/barrier/generic_barrier.h +++ b/src/barrier/generic_barrier.h @@ -148,7 +148,9 @@ namespace zsy void open() override; - void resolveRecognizeResult(const std::string &license, const std::string &imageFile) override; + bool isFront() override; + + // void resolveRecognizeResult(const std::string &license, const std::string &imageFile) override; static std::tuple parseRecognizeResult(const std::string &jsonStr); }; diff --git a/src/common/daily_and_size_sink.cpp b/src/common/daily_and_size_sink.cpp index 8d672fd..b57d04b 100644 --- a/src/common/daily_and_size_sink.cpp +++ b/src/common/daily_and_size_sink.cpp @@ -1,4 +1,8 @@ #include "daily_and_size_sink.h" +#include +#include + +#include "date_util.h" namespace zsy { @@ -12,7 +16,7 @@ namespace zsy baseFilename = base_path.stem().string(); baseExt = base_path.extension().string(); std::filesystem::create_directories(paths); - currentDate = today(); + currentDate = DateUtil::format(Ymd_); currentIndex = lastIndex(); currentSize = lastSize(); fileHelper.open(basePath + "\\" + baseFilename + baseExt, false); @@ -21,7 +25,7 @@ namespace zsy void DailyAndSizeSink::rotateFile() { - auto current_date = today(); + auto current_date = DateUtil::format(Ymd_); if (current_date != currentDate) { @@ -100,22 +104,18 @@ namespace zsy fileHelper.flush(); } - std::string DailyAndSizeSink::today() + /*std::string DailyAndSizeSink::today() { - // 获取当前时间点 auto now = std::chrono::system_clock::now(); time_t current_time = std::chrono::system_clock::to_time_t(now); - // 转换为本地时间 - tm local_tm; - localtime_s(&local_tm, ¤t_time); // 安全版本的 localtime - - // 格式化日期为 YYYY-MM-DD 格式 - char buffer[11]; // 足够存储 "YYYY-MM-DD\0" - strftime(buffer, sizeof(buffer), "%F", &local_tm); + tm local_tm{}; + localtime_s(&local_tm, ¤t_time); + char buffer[11]; + std::strftime(buffer, sizeof(buffer), "%F", &local_tm); return std::string(buffer); - } + }*/ uint32_t DailyAndSizeSink::lastIndex() { diff --git a/src/common/daily_and_size_sink.h b/src/common/daily_and_size_sink.h index efb3d85..a600136 100644 --- a/src/common/daily_and_size_sink.h +++ b/src/common/daily_and_size_sink.h @@ -6,10 +6,8 @@ #include #include #include -#include -#include #include -#include + namespace zsy { @@ -30,7 +28,7 @@ namespace zsy void cleanOldFiles(); - std::string today(); + // std::string today(); uint32_t lastIndex(); diff --git a/src/common/date_util.cpp b/src/common/date_util.cpp new file mode 100644 index 0000000..a7a8a39 --- /dev/null +++ b/src/common/date_util.cpp @@ -0,0 +1,19 @@ +#include "date_util.h" + +namespace zsy +{ + std::string DateUtil::format(const std::string &pattern, const TimePoint dt) + { + std::time_t t = std::chrono::system_clock::to_time_t(dt); + tm local_tm{}; + localtime_s(&local_tm, &t); + char buffer[32]; + std::strftime(buffer, sizeof(buffer), pattern.c_str(), &local_tm); + return std::string(buffer); + } + + long long DateUtil::timestamp() + { + return std::chrono::time_point_cast(std::chrono::system_clock::now()+ + std::chrono::hours(8)).time_since_epoch().count(); + } +} // zsy diff --git a/src/common/date_util.h b/src/common/date_util.h new file mode 100644 index 0000000..ea36852 --- /dev/null +++ b/src/common/date_util.h @@ -0,0 +1,26 @@ +#ifndef DATE_UTIL_H +#define DATE_UTIL_H +#include +#include + +#define Ymd_ "%F" +#define Ymd "%Y%m%d" +#define HMS_ "%T" +#define HMS "%H%M%S" +#define YmdHMS_ "%F %T" +#define YmdHMS "%Y%m%d%H%M%S" + +namespace zsy +{ + class DateUtil + { + public: + using TimePoint = std::chrono::time_point; + + static std::string format(const std::string &pattern = YmdHMS_, TimePoint dt = std::chrono::time_point_cast(std::chrono::system_clock::now())); + + static long long timestamp(); + }; +} // zsy + +#endif //DATE_UTIL_H diff --git a/src/common/sys_util.cpp b/src/common/sys_util.cpp index 3e92237..635847f 100644 --- a/src/common/sys_util.cpp +++ b/src/common/sys_util.cpp @@ -199,20 +199,6 @@ namespace zsy return result; } - - /*std::wstring SysUtil::str_a_w(const std::string &astr) - { - if (astr.empty()) return {}; - std::wstring_convert > converter; - return converter.from_bytes(astr); - } - std::string SysUtil::str_w_a(const std::wstring &wstr) -{ - if (wstr.empty()) return {}; - std::wstring_convert > converter; - return converter.to_bytes(wstr); -}*/ - std::shared_ptr SysUtil::ch_vlt(const char *ach) { if (ach == nullptr) return nullptr; diff --git a/src/common/thread_pool.h b/src/common/thread_pool.h index 40f0fb4..2644332 100644 --- a/src/common/thread_pool.h +++ b/src/common/thread_pool.h @@ -23,7 +23,7 @@ namespace zsy std::shared_mutex status_mtx; public: - explicit ThreadPool(size_t threadCount = std::thread::hardware_concurrency()); + explicit ThreadPool(size_t threadCount = std::thread::hardware_concurrency() * 2); template void submit(Fn &&task) noexcept diff --git a/src/recognize_processor.cpp b/src/recognize_processor.cpp new file mode 100644 index 0000000..0bacfa2 --- /dev/null +++ b/src/recognize_processor.cpp @@ -0,0 +1,68 @@ +#include "recognize_processor.h" +#include "application.h" +#include "common/date_util.h" +#include "common/loging.h" +#include "common/sys_util.h" +#include "common/snowflake.h" + +namespace zsy +{ + void RecognizeProcessor::process(bool front, const std::string &sn, const std::string &license, const std::string &imageFile) + { + try + { + if (front) + { + // 播放语音 1 + Application::threadPool->submit([sn] + { + Application::deviceHolder->getSoundColumn(sn)->play("欢迎光临"); + }); + } + // 上传车头照 + auto imageFileDecode = SysUtil::base64_decode(imageFile); + const std::shared_ptr imageFileStream = std::make_shared(imageFileDecode, std::ios_base::in | std::ios_base::binary); + auto photoId = Snowflake::genIdStr(); + std::string date = DateUtil::format(Ymd); + // 上报 1 + auto reportPassResult = Application::reportSvr->reportPass(license, sn, ""); + if (!(reportPassResult.code == 0 && (reportPassResult.data.type == 1 || reportPassResult.data.type == 2))) + { + LOGGER_INFO("不进行后续处理"); + return; + } + auto [carFrontUrlSucc,carFrontUrl] = Application::oss->upload(date + "/" + "front_" + photoId + ".jpg", *imageFileStream); + + // 上报结果 type 不为 0,播放语音 2 并读地磅,拍车斗并上传 + Application::threadPool->submit([sn] + { + Application::deviceHolder->getSoundColumn(sn)->play("正在称重,请稍后"); + }); + double weight = Application::deviceHolder->getPlatformScale(sn)->reading(); + auto photographPath = Application::deviceHolder->getVidicon(sn)->photograph(); + auto [carBodyUrlSucc,carBodyUrl] = Application::oss->upload(date + "/" + "body_" + photoId + ".jpg", photographPath); + if (!photographPath.empty()) + { + auto pathPtr = SysUtil::ch_vlt(photographPath.c_str()); + if (std::remove(pathPtr.get()) != 0) + { + auto [_,code,msg] = SysUtil::getError(); + LOGGER_ERROR("临时照片删除失败:{},错误码:{},错误信息:{}", photographPath, code, msg); + } else + { + LOGGER_ERROR("临时照片删除成功:{}", photographPath); + } + } + + // 上报 2 + Application::reportSvr->report(license, sn, weight, carFrontUrl, carBodyUrl, reportPassResult.data.orderNo); + } catch (std::exception &e) + { + LOGGER_ERROR("车牌识别结果处理失败: {}", e.what()); + } + catch (...) + { + LOGGER_ERROR("未知异常,车牌识别结果处理失败"); + } + } +} // zsy diff --git a/src/recognize_processor.h b/src/recognize_processor.h new file mode 100644 index 0000000..fbc371c --- /dev/null +++ b/src/recognize_processor.h @@ -0,0 +1,15 @@ +#ifndef RECOGNIZE_PROCESSOR_H +#define RECOGNIZE_PROCESSOR_H +#include + + +namespace zsy +{ + class RecognizeProcessor + { + public: + static void process(bool front, const std::string& sn, const std::string& license, const std::string& imageFile); + }; +} // zsy + +#endif //RECOGNIZE_PROCESSOR_H diff --git a/src/vidicon/barrier_vidicon.cpp b/src/vidicon/barrier_vidicon.cpp index e3554e0..f3cf361 100644 --- a/src/vidicon/barrier_vidicon.cpp +++ b/src/vidicon/barrier_vidicon.cpp @@ -1,5 +1,6 @@ #include "barrier_vidicon.h" #include "application.h" +#include "common/date_util.h" #include "nlohmann/json.hpp" #include "common/sys_util.h" #include "common/snowflake.h" @@ -75,20 +76,12 @@ namespace zsy }); } - static long long getTime() - { - const auto now = std::chrono::system_clock::now(); - const auto now_ms = std::chrono::time_point_cast(now); - const auto epoch = now_ms.time_since_epoch(); - return std::chrono::duration_cast(epoch).count(); - } - std::string BarrierVidicon::photograph() { try { LOGGER_INFO("摄像机正在拍照,设备名称:{}", config.name); - const long timestamp = static_cast(getTime()); + const long timestamp = static_cast(DateUtil::timestamp()/1000); nlohmann::json j{ {"id", "photograph" + std::to_string(timestamp)}, {"sn", config.ip},