修复问题

master
lzq 2025-07-15 13:48:43 +08:00
parent 02685679ef
commit 62ba7d9a11
16 changed files with 251 additions and 194 deletions

View File

@ -27,6 +27,7 @@ set(SOURCE_DIRS
src/barrier
src/sound_column
src/http
src/controller
src/mqtt
src/oss
src/serial_port

View File

@ -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": ""
}
]
}

View File

@ -16,6 +16,8 @@ namespace zsy
std::shared_ptr<DeviceHolder> Application::deviceHolder = nullptr;
std::shared_ptr<Test> 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>();
threadPool = std::make_shared<ThreadPool>(4);
eventManager = std::make_shared<EventManager>(threadPool);
@ -122,6 +124,8 @@ namespace zsy
reportSvr = std::make_shared<ReportSvr>(config);
deviceHolder = std::make_shared<DeviceHolder>(config);
test = std::make_shared<Test>();
}
void Application::run()

View File

@ -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> deviceHolder;
static std::shared_ptr<Test> test;
Application(const std::string &configFile);
static void run();

View File

@ -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<std::string, long long> 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::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
auto key = serialno + license;
long long time = ms + 60000 * 5;
{
std::lock_guard<std::mutex> 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)

View File

@ -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, &current_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()

View File

@ -15,7 +15,6 @@ namespace zsy
{
class DailyAndSizeSink final : public spdlog::sinks::base_sink<std::mutex>
{
bool init;
std::string basePath;
std::string baseFilename;
std::string baseExt;

View File

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

View File

@ -8,14 +8,17 @@ namespace zsy
{
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<void()> task;
{
std::unique_lock lock(queueMutex);
taskCondition.wait(lock, st, [this]
@ -28,8 +31,9 @@ namespace zsy
LOGGER_INFO("已停止线程");
return;
}
std::function<void()> &task = tasks.front();
task = std::move(tasks.front());
tasks.pop();
}
try
{
if (task) task();
@ -41,13 +45,9 @@ namespace zsy
{
LOGGER_ERROR("未知异常,任务执行失败");
}
tasks.pop();
}
}
});
}
status = 1;
}
ThreadPool::~ThreadPool()

View File

@ -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");
});
}
}

View File

@ -0,0 +1,15 @@
#ifndef TEST_H
#define TEST_H
namespace zsy
{
class Test
{
public:
Test();
};
}
#endif //TEST_H

View File

@ -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<BarrierProperties> &configs)
@ -39,8 +39,8 @@ namespace zsy {
{
for (auto &config: configs)
{
// vidicons.emplace(config.sn, std::make_shared<GenericVidicon>(config));
vidicons.emplace(config.sn, std::make_shared<BarrierVidicon>(config));
vidicons.emplace(config.sn, std::make_shared<GenericVidicon>(config));
// vidicons.emplace(config.sn, std::make_shared<BarrierVidicon>(config));
}
}

View File

@ -112,7 +112,6 @@ namespace zsy
auto cli = Application::mqttCliHolder->localCli();
if (cli)
{
// std::unique_lock<std::mutex> 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
{

View File

@ -39,7 +39,7 @@ namespace zsy
public:
~BarrierVidicon() override;
BarrierVidicon(const VidiconProperties &config);
explicit BarrierVidicon(const VidiconProperties &config);
std::string photograph() override;
};

View File

@ -0,0 +1,8 @@
#include "test_vidicon.h"
namespace zsy {
std::string TestVidicon::photograph()
{
return "";
}
} // zsy

View File

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