diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a0f80e..1e06598 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ set(SOURCE_DIRS src/barrier src/sound_column src/http + src/controller src/mqtt src/oss src/serial_port diff --git a/config/config.json b/config/config.json index a10cde6..efb890d 100644 --- a/config/config.json +++ b/config/config.json @@ -1,12 +1,15 @@ { + "name": "测试", + "delay": 0, + "daemon": false, "httpSvr": { "port": 11000 }, "oss": { - "endpoint": "oss-cn-shanghai.aliyuncs.com", - "bucketName": "tq-cdn", - "ak": "LTAI5t9ppUx8fkEnPwnNwmnx", - "sk": "uxrwL01P1Nw6M3YRFWoMIluY4swKwC" + "endpoint": "localhost", + "bucketName": "a", + "ak": "a", + "sk": "a" }, "mqtts": [ { @@ -45,15 +48,15 @@ } ], "reportSvr": { - "server": "https://biz-api.jstqhj.cn:443", + "server": "http://127.0.0.1:11000", "passUrl": "/api/site/report-car-pass?debug=1", "reportUrl": "/api/site/report-v2?debug=1" }, "barriers": [ { "name": "进前置", - "sn": "b11ceeb5-af42d80f", - "ip": "192.168.120.240", + "sn": "1", + "ip": "127.0.0.1", "io": 0, "platformScale": "1", "soundColumn": "1", @@ -61,30 +64,30 @@ }, { "name": "进", - "sn": "25ec7559-74125e77", - "ip": "192.168.120.235", + "sn": "2", + "ip": "127.0.0.1", "io": 0, "platformScale": "1", - "soundColumn": "2", + "soundColumn": "1", "vidicon": "1" }, { "name": "出前置", - "sn": "351b2406-6fae5ab5", - "ip": "192.168.120.243", - "io": 0, - "platformScale": "1", - "soundColumn": "2", - "vidicon": "2" - }, - { - "name": "出", - "sn": "34fc9ce5-8b091060", - "ip": "192.168.120.238", + "sn": "3", + "ip": "127.0.0.1", "io": 0, "platformScale": "1", "soundColumn": "1", - "vidicon": "2" + "vidicon": "1" + }, + { + "name": "出", + "sn": "4", + "ip": "127.0.0.1", + "io": 0, + "platformScale": "1", + "soundColumn": "1", + "vidicon": "1" } ], "platformScales": [ @@ -93,7 +96,7 @@ "name": "地磅", "sample": 20, "port": "COM2", - "baudRate": 1200, + "baudRate": 9600, "byteSize": 8, "parity": 0, "stopBits": 0 @@ -103,32 +106,18 @@ { "sn": "1", "name": "音柱1", - "server": "http://192.168.120.246:80", - "path": "/v1/speech" - }, - { - "sn": "2", - "name": "音柱2", - "server": "http://192.168.120.237:80", + "server": "http://127.0.0.1:11000", "path": "/v1/speech" } ], "vidicons": [ { "sn": "1", - "name": "摄像头1", - "ip": "192.168.120.245", + "name": "摄像头", + "ip": "", "port": 8000, - "username": "admin", - "passwd": "gk147258" - }, - { - "sn": "2", - "name": "摄像头2", - "ip": "192.168.120.244", - "port": 8000, - "username": "admin", - "passwd": "gk147258" + "username": "", + "passwd": "" } ] } \ No newline at end of file diff --git a/src/application.cpp b/src/application.cpp index 8889bf0..2a1ece3 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -16,6 +16,8 @@ namespace zsy std::shared_ptr Application::deviceHolder = nullptr; + std::shared_ptr Application::test = nullptr; + void Application::loadConfig() { LOGGER_INFO("正在加载配置文件:{}", configFile); @@ -109,7 +111,7 @@ namespace zsy LOGGER_INFO("等待..."); std::this_thread::sleep_for(std::chrono::milliseconds(appProperties.delay * 1000)); } - threadPool = std::make_shared(); + threadPool = std::make_shared(4); eventManager = std::make_shared(threadPool); @@ -122,6 +124,8 @@ namespace zsy reportSvr = std::make_shared(config); deviceHolder = std::make_shared(config); + + test = std::make_shared(); } void Application::run() diff --git a/src/application.h b/src/application.h index 0340b3c..b8c004a 100644 --- a/src/application.h +++ b/src/application.h @@ -5,19 +5,21 @@ #include "report_svr/report_svr.h" #include "oss/oss.h" #include "device_holder.h" - +#include "controller/test.h" namespace zsy { struct AppProperties { bool daemon{false}; uint32_t delay{0}; + std::string name; }; inline void from_json(const nlohmann::json &j, AppProperties &o) { PARSE_JSON(daemon, o.daemon); PARSE_JSON(delay, o.delay); + PARSE_JSON(name, o.name); } class Application @@ -50,6 +52,8 @@ namespace zsy static std::shared_ptr deviceHolder; + static std::shared_ptr test; + Application(const std::string &configFile); static void run(); diff --git a/src/barrier/generic_barrier.cpp b/src/barrier/generic_barrier.cpp index b754fb3..813237c 100644 --- a/src/barrier/generic_barrier.cpp +++ b/src/barrier/generic_barrier.cpp @@ -46,94 +46,8 @@ namespace zsy LOGGER_ERROR("未找到 MQTT 远程客户端"); } - Application::httpSvr->getEndpoint("/weight", [](const httplib::Request &req, httplib::Response &res) - { - nlohmann::json j; - try - { - auto sn = req.get_param_value("sn"); - double weight = Application::deviceHolder->getPlatformScale(sn)->reading(); - j["结果"] = weight; - } catch (std::exception &e) - { - LOGGER_ERROR("错误", e.what()); - j["结果"] = "错误"; - } - catch (...) - { - LOGGER_ERROR("错误"); - j["结果"] = "错误"; - } - res.set_content(j.dump(), "application/json"); - }); - - Application::httpSvr->getEndpoint("/opendoor", [](const httplib::Request &req, httplib::Response &res) - { - nlohmann::json j; - try - { - auto sn = req.get_param_value("sn"); - Application::deviceHolder->getBarrier(sn)->open(); - j["结果"] = "成功"; - } catch (std::exception &e) - { - LOGGER_ERROR("错误", e.what()); - j["结果"] = "错误"; - } - catch (...) - { - LOGGER_ERROR("错误"); - j["结果"] = "错误"; - } - res.set_content(j.dump(), "application/json"); - }); - - Application::httpSvr->getEndpoint("/playvoice", [](const httplib::Request &req, httplib::Response &res) - { - nlohmann::json j; - try - { - auto sn = req.get_param_value("sn"); - Application::deviceHolder->getSoundColumn(sn)->play("欢迎光临"); - j["结果"] = "成功"; - } catch (std::exception &e) - { - LOGGER_ERROR("错误", e.what()); - j["结果"] = "错误"; - } - catch (...) - { - LOGGER_ERROR("错误"); - j["结果"] = "错误"; - } - res.set_content(j.dump(), "application/json"); - }); - - Application::httpSvr->getEndpoint("/photo", [](const httplib::Request &req, httplib::Response &res) - { - nlohmann::json j; - try - { - auto sn = req.get_param_value("sn"); - auto photograph = Application::deviceHolder->getVidicon(sn)->photograph(); - j["结果"] = photograph; - } catch (std::exception &e) - { - LOGGER_ERROR("错误", e.what()); - j["结果"] = "错误"; - } - catch (...) - { - LOGGER_ERROR("错误"); - j["结果"] = "错误"; - } - res.set_content(j.dump(), "application/json"); - }); - Application::httpSvr->postEndpoint("/plateRecognize", [](const httplib::Request &req, httplib::Response &res) { - // static std::unordered_map cache; - // static std::mutex cacheMutex; try { auto body = req.body; @@ -161,36 +75,6 @@ namespace zsy res.set_content(R"({})", "application/json"); return; } - /*auto ms = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); - auto key = serialno + license; - long long time = ms + 60000 * 5; - { - std::lock_guard lock(cacheMutex); - std::erase_if(cache, [time](const auto &pair) - { - return pair.second >= time; - }); - if (cache.contains(key)) - { - if (cache[key] < time) - { - res.set_content(R"({})", "application/json"); - LOGGER_INFO("重复的车牌识别结果:{} {}", serialno, license); - return; - } - } - cache[key] = ms; - }*/ - - /*std::jthread t1([imageFile,license, barrier = Application::deviceHolder->getBarrier(serialno)] - { - if (!barrier) - { - LOGGER_ERROR("未找到对应的道闸"); - return; - } - barrier->resolveRecognizeResult(license, imageFile); - });*/ Application::threadPool->submit([imageFile,license, barrier = Application::deviceHolder->getBarrier(serialno)] { if (!barrier) diff --git a/src/common/daily_and_size_sink.cpp b/src/common/daily_and_size_sink.cpp index 5a2ea78..8d672fd 100644 --- a/src/common/daily_and_size_sink.cpp +++ b/src/common/daily_and_size_sink.cpp @@ -102,9 +102,19 @@ namespace zsy std::string DailyAndSizeSink::today() { + // 获取当前时间点 auto now = std::chrono::system_clock::now(); - auto local_time = std::chrono::zoned_time{std::chrono::current_zone(), now}; - return std::format("{:%F}", local_time); + 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); + + 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 41df9f5..efb3d85 100644 --- a/src/common/daily_and_size_sink.h +++ b/src/common/daily_and_size_sink.h @@ -15,7 +15,6 @@ namespace zsy { class DailyAndSizeSink final : public spdlog::sinks::base_sink { - bool init; std::string basePath; std::string baseFilename; std::string baseExt; diff --git a/src/common/event_manager.cpp b/src/common/event_manager.cpp index f90f451..0884474 100644 --- a/src/common/event_manager.cpp +++ b/src/common/event_manager.cpp @@ -38,7 +38,7 @@ namespace zsy for (auto fn_ptr: *list) { - try + /*try { (*fn_ptr.get())(Event(name, data)); } catch (const std::exception &e) @@ -47,8 +47,8 @@ namespace zsy }catch (...) { LOGGER_ERROR("事件处理异常:{}", name); - } - /*threadPool->submit([fn = fn_ptr.get(),name,data] + }*/ + threadPool->submit([fn = fn_ptr.get(),name,data] { try { @@ -60,7 +60,7 @@ namespace zsy { LOGGER_ERROR("事件处理异常:{}", name); } - });*/ + }); } } } // zsy diff --git a/src/common/thread_pool.cpp b/src/common/thread_pool.cpp index 8e79830..c8579a5 100644 --- a/src/common/thread_pool.cpp +++ b/src/common/thread_pool.cpp @@ -4,18 +4,21 @@ namespace zsy { - ThreadPool::ThreadPool(size_t threadCount):status(0) + ThreadPool::ThreadPool(size_t threadCount): status(0) { if (threadCount < 1) threadCount = 1; workers.reserve(threadCount); - + { + std::unique_lock lock(status_mtx); + status = 1; + } for (size_t i = 0; i < threadCount; ++i) { workers.emplace_back([this](const std::stop_token &st) - { while (!st.stop_requested()) { + std::function task; { std::unique_lock lock(queueMutex); taskCondition.wait(lock, st, [this] @@ -28,26 +31,23 @@ namespace zsy LOGGER_INFO("已停止线程"); return; } - std::function &task = tasks.front(); - - try - { - if (task) task(); - } catch (const std::exception &e) - { - LOGGER_ERROR("任务执行失败: {}", e.what()); - } - catch (...) - { - LOGGER_ERROR("未知异常,任务执行失败"); - } - + task = std::move(tasks.front()); tasks.pop(); } + try + { + if (task) task(); + } catch (const std::exception &e) + { + LOGGER_ERROR("任务执行失败: {}", e.what()); + } + catch (...) + { + LOGGER_ERROR("未知异常,任务执行失败"); + } } }); } - status = 1; } ThreadPool::~ThreadPool() diff --git a/src/controller/test.cpp b/src/controller/test.cpp new file mode 100644 index 0000000..d7eb7d1 --- /dev/null +++ b/src/controller/test.cpp @@ -0,0 +1,133 @@ +#include "test.h" +#include "application.h" + +namespace zsy +{ + Test::Test() + { + Application::httpSvr->postEndpoint("/api/site/report-car-pass", [this](const httplib::Request &req, httplib::Response &res) + { + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::string data = R"({ + "code": 0, + "msg": "", + "data": { + "car_number": "", + "type": 2, + "order_no": "", + "err_msg": "", + "scene": "", + "no_weight": false + } + })"; + res.set_content(data, "application/json"); + }); + Application::httpSvr->postEndpoint("/api/site/report-v2", [this](const httplib::Request &req, httplib::Response &res) + { + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::string data = R"({ + "code": 0, + "msg": "", + "data": { + "car_number": "", + "order_no": "", + "lane_code": "" + } + })"; + res.set_content(data, "application/json"); + }); + Application::httpSvr->postEndpoint("/v1/speech", [this](const httplib::Request &req, httplib::Response &res) + { + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::string data = R"({ + "code": 200, + "message": "播放成功" + })"; + res.set_content(data, "application/json"); + }); + + Application::httpSvr->getEndpoint("/weight", [](const httplib::Request &req, httplib::Response &res) + { + nlohmann::json j; + try + { + auto sn = req.get_param_value("sn"); + double weight = Application::deviceHolder->getPlatformScale(sn)->reading(); + j["结果"] = weight; + } catch (std::exception &e) + { + LOGGER_ERROR("错误", e.what()); + j["结果"] = "错误"; + } + catch (...) + { + LOGGER_ERROR("错误"); + j["结果"] = "错误"; + } + res.set_content(j.dump(), "application/json"); + }); + + Application::httpSvr->getEndpoint("/opendoor", [](const httplib::Request &req, httplib::Response &res) + { + nlohmann::json j; + try + { + auto sn = req.get_param_value("sn"); + Application::deviceHolder->getBarrier(sn)->open(); + j["结果"] = "成功"; + } catch (std::exception &e) + { + LOGGER_ERROR("错误", e.what()); + j["结果"] = "错误"; + } + catch (...) + { + LOGGER_ERROR("错误"); + j["结果"] = "错误"; + } + res.set_content(j.dump(), "application/json"); + }); + + Application::httpSvr->getEndpoint("/play", [](const httplib::Request &req, httplib::Response &res) + { + nlohmann::json j; + try + { + auto sn = req.get_param_value("sn"); + Application::deviceHolder->getSoundColumn(sn)->play("欢迎光临"); + j["结果"] = "成功"; + } catch (std::exception &e) + { + LOGGER_ERROR("错误", e.what()); + j["结果"] = "错误"; + } + catch (...) + { + LOGGER_ERROR("错误"); + j["结果"] = "错误"; + } + res.set_content(j.dump(), "application/json"); + }); + + Application::httpSvr->getEndpoint("/photo", [](const httplib::Request &req, httplib::Response &res) + { + nlohmann::json j; + try + { + auto sn = req.get_param_value("sn"); + auto photograph = Application::deviceHolder->getVidicon(sn)->photograph(); + j["结果"] = photograph; + } catch (std::exception &e) + { + LOGGER_ERROR("错误", e.what()); + j["结果"] = "错误"; + } + catch (...) + { + LOGGER_ERROR("错误"); + j["结果"] = "错误"; + } + res.set_content(j.dump(), "application/json"); + }); + } +} diff --git a/src/controller/test.h b/src/controller/test.h new file mode 100644 index 0000000..7e8efe5 --- /dev/null +++ b/src/controller/test.h @@ -0,0 +1,15 @@ +#ifndef TEST_H +#define TEST_H + + +namespace zsy +{ + class Test + { + public: + Test(); + }; +} + + +#endif //TEST_H diff --git a/src/device_holder.cpp b/src/device_holder.cpp index 8a1ee09..05a77fd 100644 --- a/src/device_holder.cpp +++ b/src/device_holder.cpp @@ -3,8 +3,8 @@ #include "barrier/generic_barrier.h" #include "platform_scale/generic_platform_scale.h" #include "sound_column/generic_sound_column.h" -// #include "vidicon/generic_vidicon.h" -#include "vidicon/barrier_vidicon.h" +#include "vidicon/generic_vidicon.h" +// #include "vidicon/barrier_vidicon.h" namespace zsy { void DeviceHolder::initBarriers(std::vector &configs) @@ -39,8 +39,8 @@ namespace zsy { { for (auto &config: configs) { - // vidicons.emplace(config.sn, std::make_shared(config)); - vidicons.emplace(config.sn, std::make_shared(config)); + vidicons.emplace(config.sn, std::make_shared(config)); + // vidicons.emplace(config.sn, std::make_shared(config)); } } diff --git a/src/vidicon/barrier_vidicon.cpp b/src/vidicon/barrier_vidicon.cpp index 4b54734..e3554e0 100644 --- a/src/vidicon/barrier_vidicon.cpp +++ b/src/vidicon/barrier_vidicon.cpp @@ -112,7 +112,6 @@ namespace zsy auto cli = Application::mqttCliHolder->localCli(); if (cli) { - // std::unique_lock lock(mtx); hasNewContent.store(false); imageContent = ""; cli->publish(topic, data, 0); @@ -126,11 +125,8 @@ namespace zsy const auto filename = "car_body_" + Snowflake::genIdStr() + ".jpg"; std::string path = SysUtil::HOME_DIR + "\\temp\\" + filename; - LOGGER_INFO("解码前:{}", imageContent); auto content = SysUtil::base64_decode(imageContent); - LOGGER_INFO("解码后:{}", content); bool res = saveImageToFile(content, path); - LOGGER_INFO("文件保存结果:{}", res); return path; } else { diff --git a/src/vidicon/barrier_vidicon.h b/src/vidicon/barrier_vidicon.h index f7ce193..fbc54c1 100644 --- a/src/vidicon/barrier_vidicon.h +++ b/src/vidicon/barrier_vidicon.h @@ -39,7 +39,7 @@ namespace zsy public: ~BarrierVidicon() override; - BarrierVidicon(const VidiconProperties &config); + explicit BarrierVidicon(const VidiconProperties &config); std::string photograph() override; }; diff --git a/src/vidicon/test_vidicon.cpp b/src/vidicon/test_vidicon.cpp new file mode 100644 index 0000000..0f9ad3d --- /dev/null +++ b/src/vidicon/test_vidicon.cpp @@ -0,0 +1,8 @@ +#include "test_vidicon.h" + +namespace zsy { + std::string TestVidicon::photograph() + { + return ""; + } +} // zsy \ No newline at end of file diff --git a/src/vidicon/test_vidicon.h b/src/vidicon/test_vidicon.h new file mode 100644 index 0000000..3b3d813 --- /dev/null +++ b/src/vidicon/test_vidicon.h @@ -0,0 +1,14 @@ +#ifndef TEST_VIDICON_H +#define TEST_VIDICON_H +#include "vidicon.h" + +namespace zsy +{ + class TestVidicon : public Vidicon + { + public: + std::string photograph() override; + }; +} // zsy + +#endif //TEST_VIDICON_H