|
|
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
|
|
|
//
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
// You may obtain a copy of the License at
|
|
|
//
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
//
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
// See the License for the specific language governing permissions and
|
|
|
// limitations under the License.
|
|
|
|
|
|
/*!
|
|
|
* @file main.cxx
|
|
|
* This file acts as a main entry point to the application.
|
|
|
*
|
|
|
* This file was generated by the tool fastddsgen.
|
|
|
*/
|
|
|
|
|
|
#include <csignal>
|
|
|
#include <cstring>
|
|
|
#include <functional>
|
|
|
#include <iostream>
|
|
|
#include <stdexcept>
|
|
|
#include <thread>
|
|
|
#include <queue>
|
|
|
|
|
|
#include <fastdds/dds/log/Log.hpp>
|
|
|
|
|
|
#include "Subscriber.hpp"
|
|
|
#include "msg.hpp"
|
|
|
#include "DEBUG.hpp"
|
|
|
#include "utils.hpp"
|
|
|
#include "YQNetCom.h"
|
|
|
|
|
|
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";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static const std::string base64_chars =
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
"0123456789+/";
|
|
|
|
|
|
static inline bool is_base64(unsigned char c) {
|
|
|
return (isalnum(c) || (c == '+') || (c == '/'));
|
|
|
}
|
|
|
|
|
|
std::string base64_encode(const std::string &input)
|
|
|
{
|
|
|
std::string ret;
|
|
|
int i = 0;
|
|
|
int j = 0;
|
|
|
unsigned char char_array_3[3];
|
|
|
unsigned char char_array_4[4];
|
|
|
|
|
|
size_t in_len = input.size();
|
|
|
const char *bytes_to_encode = input.c_str();
|
|
|
|
|
|
while (in_len--)
|
|
|
{
|
|
|
char_array_3[i++] = *(bytes_to_encode++);
|
|
|
if (i == 3)
|
|
|
{
|
|
|
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
|
|
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
|
|
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
|
|
char_array_4[3] = char_array_3[2] & 0x3f;
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
{
|
|
|
ret += base64_chars[char_array_4[i]];
|
|
|
}
|
|
|
i = 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (i)
|
|
|
{
|
|
|
for (j = i; j < 3; j++)
|
|
|
{
|
|
|
char_array_3[j] = '\0';
|
|
|
}
|
|
|
|
|
|
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
|
|
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
|
|
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
|
|
|
|
|
for (j = 0; j < i + 1; j++)
|
|
|
{
|
|
|
ret += base64_chars[char_array_4[j]];
|
|
|
}
|
|
|
|
|
|
while (i++ < 3)
|
|
|
{
|
|
|
ret += '=';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
void send_text(std::string ip_addr, int port, std::string text)
|
|
|
{
|
|
|
char *user_name = "guest", *user_pwd = "guest";
|
|
|
int dynamic_type = 1;
|
|
|
int display_effects = 0;
|
|
|
int display_speed = 16;
|
|
|
int stay_time = 10;
|
|
|
int gif_flag = 0;
|
|
|
char *bg_color = "0xff000000";
|
|
|
char *color = "0xffffffff";
|
|
|
char *font_attributes = "normal";
|
|
|
char *font = "SimSun";
|
|
|
char *align_h = "0";
|
|
|
char *align_v = "0";
|
|
|
char *ali = "";
|
|
|
int err;
|
|
|
|
|
|
std::vector<char> ip(ip_addr.begin(), ip_addr.end());
|
|
|
ip.push_back('\0');
|
|
|
|
|
|
std::string wcstring = base64_encode(text);
|
|
|
|
|
|
int b = check_time(ip.data(), port, user_name, user_pwd);
|
|
|
DEBUG("check_time==" << b);
|
|
|
unsigned long play_list = create_playlist(128, 128, 8280);
|
|
|
printf("create_playlist: %p\n", play_list);
|
|
|
char *program_name = "program_0";
|
|
|
unsigned long program = create_program(program_name, bg_color);
|
|
|
printf("create_program: %p\n", program);
|
|
|
|
|
|
unsigned long dynamic_area = create_dynamic();
|
|
|
printf("create_dynamic: %p\n", dynamic_area);
|
|
|
err = add_dynamic_unit(dynamic_area, dynamic_type, display_effects, display_speed, stay_time, wcstring.data(), gif_flag, bg_color, 14, font, color, font_attributes, align_h, align_v, 0, 0, 0, "", "", 0);
|
|
|
DEBUG("add_dynamic_unit:" << err);
|
|
|
err = add_dynamic(program, dynamic_area, 0, 0, 0, 128, 128, ali, 0, ali, 100);
|
|
|
DEBUG("add_dynamic:" << err);
|
|
|
delete_dynamic(dynamic_area);
|
|
|
|
|
|
err = add_program_in_playlist(play_list, program, 0, 10, ali, ali, ali, ali, 127);
|
|
|
DEBUG("add_program_in_playlist:" << err);
|
|
|
err = update_dynamic_small(ip.data(), port, user_name, user_pwd, play_list, "", 1, 0);
|
|
|
if (err != 0)
|
|
|
{
|
|
|
DEBUG(ip.data() << "update_dynamic_small:" << err);
|
|
|
}
|
|
|
|
|
|
delete_playlist(play_list);
|
|
|
}
|
|
|
|
|
|
void send_file(std::string ip_addr, int port, std::string wcstring)
|
|
|
{
|
|
|
char *user_name = "guest", *user_pwd = "guest";
|
|
|
int dynamic_type = 1;
|
|
|
int display_effects = 0;
|
|
|
int display_speed = 16;
|
|
|
int stay_time = 10;
|
|
|
int gif_flag = 0;
|
|
|
char *bg_color = "0xff000000";
|
|
|
char *color = "0xffffffff";
|
|
|
char *font_attributes = "normal";
|
|
|
char *font = "SimSun";
|
|
|
char *align_h = "0";
|
|
|
char *align_v = "0";
|
|
|
char *ali = "";
|
|
|
int err;
|
|
|
|
|
|
std::vector<char> ip(ip_addr.begin(), ip_addr.end());
|
|
|
ip.push_back('\0');
|
|
|
|
|
|
int b = check_time(ip.data(), port, user_name, user_pwd);
|
|
|
DEBUG("check_time==" << b);
|
|
|
unsigned long play_list = create_playlist(128, 128, 8280);
|
|
|
printf("create_playlist: %p\n", play_list);
|
|
|
char *program_name = "program_0";
|
|
|
unsigned long program = create_program(program_name, bg_color);
|
|
|
printf("create_program: %p\n", program);
|
|
|
|
|
|
unsigned long dynamic_area = create_dynamic();
|
|
|
printf("create_dynamic: %p\n", dynamic_area);
|
|
|
err = add_dynamic_unit(dynamic_area, dynamic_type, display_effects, display_speed, stay_time, wcstring.data(), gif_flag, bg_color, 14, font, color, font_attributes, align_h, align_v, 0, 0, 0, "", "", 0);
|
|
|
DEBUG("add_dynamic_unit:" << err);
|
|
|
err = add_dynamic(program, dynamic_area, 0, 0, 0, 128, 128, ali, 0, ali, 100);
|
|
|
DEBUG("add_dynamic:" << err);
|
|
|
delete_dynamic(dynamic_area);
|
|
|
|
|
|
err = add_program_in_playlist(play_list, program, 0, 10, ali, ali, ali, ali, 127);
|
|
|
DEBUG("add_program_in_playlist:" << err);
|
|
|
err = update_dynamic(ip.data(), port, user_name, user_pwd, play_list, "", 1, 0);
|
|
|
if (err != 0)
|
|
|
{
|
|
|
DEBUG(ip.data() << "update_dynamic:" << err);
|
|
|
}
|
|
|
|
|
|
delete_playlist(play_list);
|
|
|
}
|
|
|
|
|
|
std::string readfile(std::string name)
|
|
|
{
|
|
|
auto exeDir = getExeDir();
|
|
|
auto configPath = exeDir / name;
|
|
|
|
|
|
// 检查文件是否存在
|
|
|
if (!std::filesystem::exists(configPath))
|
|
|
{
|
|
|
std::cerr << "文件不存在: " << configPath << std::endl;
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
std::ifstream file(configPath);
|
|
|
if (!file.is_open())
|
|
|
{
|
|
|
std::cerr << "无法打开文件: " << configPath << std::endl;
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
// 方法1:使用 stringstream 读取整个文件
|
|
|
std::stringstream buffer;
|
|
|
buffer << file.rdbuf(); // 读取所有内容
|
|
|
std::string config = buffer.str();
|
|
|
|
|
|
file.close();
|
|
|
return config;
|
|
|
}
|
|
|
|
|
|
std::queue<WeighingSystem::ScaleInfo> DdsMsgData::ScaleInfo_queue_;
|
|
|
std::queue<WeighingSystem::WeightInfoOk> DdsMsgData::WeightInfoOk_queue_;
|
|
|
std::queue<WeighingSystem::WeightInfoError> DdsMsgData::WeightInfoError_queue_;
|
|
|
std::queue<WeighingSystem::LicenseSnapUpdate> DdsMsgData::LicenseSnapUpdate_queue_;
|
|
|
std::mutex DdsMsgData::queue_cv_mtx_;
|
|
|
|
|
|
std::atomic<bool> running(true);
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
{
|
|
|
auto ret = EXIT_SUCCESS;
|
|
|
std::shared_ptr<SubscriberApp> sub;
|
|
|
|
|
|
int domain_id = 0;
|
|
|
int port = 80;
|
|
|
int type = 0;
|
|
|
std::string addr = "192.168.1.100";
|
|
|
uint8_t clear_led = 1;
|
|
|
uint8_t weight_stable = 0;
|
|
|
std::string license = "";
|
|
|
|
|
|
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], "--addr") == 0 && i + 1 < argc)
|
|
|
{
|
|
|
addr = argv[++i];
|
|
|
}
|
|
|
else if (strcmp(argv[i], "--port") == 0 && i + 1 < argc)
|
|
|
{
|
|
|
port = atoi(argv[++i]);
|
|
|
}
|
|
|
else if (strcmp(argv[i], "--type") == 0 && i + 1 < argc)
|
|
|
{
|
|
|
type = atoi(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"
|
|
|
<< " --addr Set led addr (e.g., 192.168.1.1)\n"
|
|
|
<< " --port Set port name (e.g., 80)\n"
|
|
|
<< " --type Set port name (0: in car, 1: not in car)\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"
|
|
|
<< " --addr Set led addr (e.g., 192.168.1.1)\n"
|
|
|
<< " --port Set port name (e.g., 80)\n"
|
|
|
<< " --type Set port name (0: in car, 1: not in car)\n"
|
|
|
<< " --version Show software Version\n"
|
|
|
<< " --help Show this help message\n";
|
|
|
return EXIT_FAILURE;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
sub = std::make_shared<SubscriberApp>(domain_id);
|
|
|
|
|
|
DEBUG("Program is running. Please press Ctrl+C to stop at any time.");
|
|
|
|
|
|
auto formatFloat = [](float value) -> std::string
|
|
|
{
|
|
|
std::ostringstream oss;
|
|
|
oss << std::fixed << std::setprecision(2) << value;
|
|
|
return oss.str();
|
|
|
};
|
|
|
|
|
|
std::string default_text = readfile("led.txt");
|
|
|
send_text(addr, port, default_text);
|
|
|
|
|
|
while(running.load())
|
|
|
{
|
|
|
std::unique_lock<std::mutex> lock(DdsMsgData::queue_cv_mtx_, std::try_to_lock);
|
|
|
if (lock.owns_lock())
|
|
|
{
|
|
|
if(!DdsMsgData::WeightInfoOk_queue_.empty())
|
|
|
{
|
|
|
WeighingSystem::WeightInfoOk info = std::move(DdsMsgData::WeightInfoOk_queue_.front());
|
|
|
DdsMsgData::WeightInfoOk_queue_.pop();
|
|
|
lock.unlock();
|
|
|
|
|
|
send_text(addr, port, info.Warning());
|
|
|
clear_led = 0;
|
|
|
}
|
|
|
else if (!DdsMsgData::WeightInfoError_queue_.empty())
|
|
|
{
|
|
|
WeighingSystem::WeightInfoError info = std::move(DdsMsgData::WeightInfoError_queue_.front());
|
|
|
DdsMsgData::WeightInfoError_queue_.pop();
|
|
|
lock.unlock();
|
|
|
|
|
|
send_text(addr, port, info.Warning());
|
|
|
clear_led = 0;
|
|
|
}
|
|
|
else if (!DdsMsgData::ScaleInfo_queue_.empty())
|
|
|
{
|
|
|
WeighingSystem::ScaleInfo info = std::move(DdsMsgData::ScaleInfo_queue_.front());
|
|
|
DdsMsgData::ScaleInfo_queue_.pop();
|
|
|
lock.unlock();
|
|
|
|
|
|
if (info.HasVehicle() == false)
|
|
|
{
|
|
|
if (clear_led == 0)
|
|
|
{
|
|
|
send_text(addr, port, default_text);
|
|
|
clear_led = 1;
|
|
|
license.clear();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (info.WeightOK() == true)
|
|
|
{
|
|
|
weight_stable = 1;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
weight_stable = 0;
|
|
|
}
|
|
|
}
|
|
|
else if (!DdsMsgData::LicenseSnapUpdate_queue_.empty())
|
|
|
{
|
|
|
WeighingSystem::LicenseSnapUpdate info = std::move(DdsMsgData::LicenseSnapUpdate_queue_.front());
|
|
|
DdsMsgData::LicenseSnapUpdate_queue_.pop();
|
|
|
lock.unlock();
|
|
|
|
|
|
license = info.License();
|
|
|
weight_stable = 0;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
lock.unlock();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (type == 1)
|
|
|
{
|
|
|
if ((weight_stable == 1) && (!license.empty()))
|
|
|
{
|
|
|
std::string text = license + std::string(",司机请下车等候,切勿停留在车上!");
|
|
|
send_text(addr, port, text);
|
|
|
clear_led = 0;
|
|
|
weight_stable = 0;
|
|
|
license.clear();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
}
|
|
|
|
|
|
stop_handler = [&](int signum)
|
|
|
{
|
|
|
std::cout << "\n" << parse_signal(signum) << " received, stopping " << argv[1]
|
|
|
<< " execution." << std::endl;
|
|
|
running.store(false);
|
|
|
};
|
|
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
#ifndef _WIN32
|
|
|
signal(SIGQUIT, signal_handler);
|
|
|
signal(SIGHUP, signal_handler);
|
|
|
#endif // _WIN32
|
|
|
|
|
|
Log::Reset();
|
|
|
return ret;
|
|
|
}
|