You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
7.0 KiB
C++

#include <csignal>
#include <cstring>
#include <functional>
#include <iostream>
#include <stdexcept>
#include <thread>
#include <fastdds/dds/log/Log.hpp>
#include "Subscriber.hpp"
#include "Publisher.hpp"
#include "SerialMsgHandler.hpp"
#include "msg.hpp"
#include "DEBUG.hpp"
using eprosima::fastdds::dds::Log;
std::function<void(int)> stop_handler;
void signal_handler(
int signum)
{
stop_handler(signum);
}
std::string parse_signal(
const int& signum)
{
switch (signum)
{
case SIGINT:
return "SIGINT";
case SIGTERM:
return "SIGTERM";
#ifndef _WIN32
case SIGQUIT:
return "SIGQUIT";
case SIGHUP:
return "SIGHUP";
#endif // _WIN32
default:
return "UNKNOWN SIGNAL";
}
}
std::queue<WeighingSystem::ReadCardReq> MsgData::ReadCardReq_queue_;
std::mutex MsgData::queue_cv_mtx_;
std::queue<WeighingSystem::ScaleInfo> MsgData::ScaleInfo_queue_;
std::queue<WeighingSystem::InfraredUpdate> MsgData::InfraredUpdate_queue_;
std::mutex MsgData::infrared_mtx_;
bool MsgData::front_blocked_ = false;
bool MsgData::back_blocked_ = false;
bool MsgData::infrared_ready_ = false;
int main(int argc, char** argv)
{
auto ret = EXIT_SUCCESS;
std::shared_ptr<SubscriberApp> sub;
std::shared_ptr<PublisherApp> pub;
std::shared_ptr<MsgHandler> dev;
int domain_id = 0;
const char* interface = "serial";
const char* port = "ttyS1";
const char* baudrate = "9600";
const char* device = "RC522";
// std::map<uint8_t, std::vector<uint8_t>> password = {
// {0, {0x41, 0x55, 0x53, 0x45, 0x46, 0x54}}, // AUSEFT
// {1, {0x41, 0x55, 0x53, 0x45, 0x46, 0x54}},
// {2, {0x41, 0x55, 0x53, 0x45, 0x46, 0x54}},
// {3, {0x41, 0x55, 0x53, 0x45, 0x46, 0x54}},
// {4, {0x41, 0x55, 0x53, 0x45, 0x46, 0x54}},
// {5, {0x41, 0x55, 0x53, 0x45, 0x46, 0x54}},
// {6, {0x41, 0x55, 0x53, 0x45, 0x46, 0x54}},
// {10, {0x41, 0x55, 0x53, 0x45, 0x46, 0x54}},
// };
std::map<uint8_t, std::vector<uint8_t>> password = {
{0, {0x42, 0x37, 0x4D, 0x4C, 0x35, 0x46}}, // B7ML5F
{1, {0x42, 0x37, 0x4D, 0x4C, 0x35, 0x46}},
{2, {0x42, 0x37, 0x4D, 0x4C, 0x35, 0x46}},
{3, {0x42, 0x37, 0x4D, 0x4C, 0x35, 0x46}},
{4, {0x42, 0x37, 0x4D, 0x4C, 0x35, 0x46}},
{5, {0x42, 0x37, 0x4D, 0x4C, 0x35, 0x46}},
//{6, {0x42, 0x37, 0x4D, 0x4C, 0x35, 0x46}},
{6, {0xB1, 0x0A, 0x23, 0xD1, 0x8F, 0x20}}, // B10A23D18F20
{10, {0x42, 0x37, 0x4D, 0x4C, 0x35, 0x46}},
};
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "--domain") == 0 && i + 1 < argc)
{
domain_id = atoi(argv[++i]);
}
else if (strcmp(argv[i], "--interface") == 0 && i + 1 < argc)
{
interface = argv[++i];
}
else if (strcmp(argv[i], "--port") == 0 && i + 1 < argc)
{
port = argv[++i];
}
else if (strcmp(argv[i], "--baudrate") == 0 && i + 1 < argc)
{
baudrate = argv[++i];
}
else if (strcmp(argv[i], "--device") == 0 && i + 1 < argc)
{
device = argv[++i];
}
else if (strcmp(argv[i], "--version") == 0)
{
std::cout << "Vesrion: " << PROGRAM_VERSION << "\n";
return EXIT_SUCCESS;
}
else if (strcmp(argv[i], "--help") == 0)
{
std::cout << "Usage: [options]\n"
<< "Options:\n"
<< " --domain Set domain ID\n"
<< " --interface Set interface (e.g., serial)\n"
<< " --baudrate Set baudrate (e.g., 9600)\n"
<< " --port Set port name (e.g., ttyS1)\n"
<< " --device Set device name (e.g., RC522)\n"
<< " --version Show software Version\n"
<< " --help Show this help message\n";
return EXIT_SUCCESS;
}
else
{
std::cerr << "Unknown option: " << argv[i] << "\n";
std::cout << "Usage: [options]\n"
<< "Options:\n"
<< " --domain Set domain ID\n"
<< " --interface Set interface (e.g., serial)\n"
<< " --baudrate Set baudrate (e.g., 9600)\n"
<< " --port Set port name (e.g., ttyS1)\n"
<< " --device Set device name (e.g., RC522)\n"
<< " --version Show software Version\n"
<< " --help Show this help message\n";
return EXIT_FAILURE;
}
}
sub = std::make_shared<SubscriberApp>(domain_id);
pub = std::make_shared<PublisherApp>(domain_id);
dev = std::make_shared<SerialMsgHandler>();
dev->OpenPort(port, baudrate);
dev->SetDevice(device);
dev->StorePasswd(password);
// ========== 新增:保存端口名,供 SkipCurrentBlock 使用 ==========
dev->m_Port = port;
// ========== 设置卡检测回调,触发 SummaryUpdate 发布 ==========
dev->OnCardDetected = [pub](const std::string& cardId) {
pub->PublishSummaryUpdate(cardId, 0.0f); // StableValue 暂时用 0.0f,后续从称重系统获取
};
if (!dev->InitSequence())
{
DEBUG("Device initialization failed, exiting.");
return EXIT_FAILURE;
}
std::thread sub_thread(&SubscriberApp::run, sub, dev);
std::thread pub_thread(&PublisherApp::run, pub, dev);
DEBUG("Program is running. Please press Ctrl+C to stop at any time.");
stop_handler = [&](int signum)
{
std::cout << "\n" << parse_signal(signum) << " received, stopping execution." << std::endl;
sub->stop();
pub->stop();
};
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
#ifndef _WIN32
signal(SIGQUIT, signal_handler);
signal(SIGHUP, signal_handler);
#endif // _WIN32
sub_thread.join();
pub_thread.join();
Log::Reset();
return ret;
}