修复问题

master
lzq 2025-07-15 18:03:31 +08:00
parent 62ba7d9a11
commit 6e9047b0ae
13 changed files with 171 additions and 63 deletions

View File

@ -111,7 +111,7 @@ namespace zsy
LOGGER_INFO("等待...");
std::this_thread::sleep_for(std::chrono::milliseconds(appProperties.delay * 1000));
}
threadPool = std::make_shared<ThreadPool>(4);
threadPool = std::make_shared<ThreadPool>();
eventManager = std::make_shared<EventManager>(threadPool);

View File

@ -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

View File

@ -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<std::chrono::milliseconds>(now);
const auto epoch = now_ms.time_since_epoch();
return std::chrono::duration_cast<std::chrono::milliseconds>(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<long>(getTime());
const long timestamp = static_cast<long>(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<bool, RecognizeResult> 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

View File

@ -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<bool, RecognizeResult> parseRecognizeResult(const std::string &jsonStr);
};

View File

@ -1,4 +1,8 @@
#include "daily_and_size_sink.h"
#include <ctime>
#include <regex>
#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, &current_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, &current_time);
char buffer[11];
std::strftime(buffer, sizeof(buffer), "%F", &local_tm);
return std::string(buffer);
}
}*/
uint32_t DailyAndSizeSink::lastIndex()
{

View File

@ -6,10 +6,8 @@
#include <mutex>
#include <string>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <filesystem>
#include <regex>
namespace zsy
{
@ -30,7 +28,7 @@ namespace zsy
void cleanOldFiles();
std::string today();
// std::string today();
uint32_t lastIndex();

View File

@ -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::milliseconds>(std::chrono::system_clock::now()+ + std::chrono::hours(8)).time_since_epoch().count();
}
} // zsy

View File

@ -0,0 +1,26 @@
#ifndef DATE_UTIL_H
#define DATE_UTIL_H
#include <chrono>
#include <string>
#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<std::chrono::system_clock, std::chrono::milliseconds>;
static std::string format(const std::string &pattern = YmdHMS_, TimePoint dt = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()));
static long long timestamp();
};
} // zsy
#endif //DATE_UTIL_H

View File

@ -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<std::codecvt_utf8<wchar_t> > converter;
return converter.from_bytes(astr);
}
std::string SysUtil::str_w_a(const std::wstring &wstr)
{
if (wstr.empty()) return {};
std::wstring_convert<std::codecvt_utf8<wchar_t> > converter;
return converter.to_bytes(wstr);
}*/
std::shared_ptr<char[]> SysUtil::ch_vlt(const char *ach)
{
if (ach == nullptr) return nullptr;

View File

@ -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<typename Fn>
void submit(Fn &&task) noexcept

View File

@ -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<std::istream> imageFileStream = std::make_shared<std::istringstream>(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

View File

@ -0,0 +1,15 @@
#ifndef RECOGNIZE_PROCESSOR_H
#define RECOGNIZE_PROCESSOR_H
#include <string>
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

View File

@ -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<std::chrono::milliseconds>(now);
const auto epoch = now_ms.time_since_epoch();
return std::chrono::duration_cast<std::chrono::milliseconds>(epoch).count();
}
std::string BarrierVidicon::photograph()
{
try
{
LOGGER_INFO("摄像机正在拍照,设备名称:{}", config.name);
const long timestamp = static_cast<long>(getTime());
const long timestamp = static_cast<long>(DateUtil::timestamp()/1000);
nlohmann::json j{
{"id", "photograph" + std::to_string(timestamp)},
{"sn", config.ip},