Commit ac68b40f authored by paopao's avatar paopao

add branch

parent 17741475
{ {
"mqtt_server_ip": "192.168.9.198", "mqtt_server_ip": "192.168.9.101",
"mqtt_server_port": 1883, "mqtt_server_port": 1883,
"mqtt_server_user": "admin/waytous", "mqtt_server_user": "admin",
"mqtt_server_pwd": "2023", "mqtt_server_pwd": "waytous/2023",
"self_id": "1", "self_id": "PSZ-001",
"uart": "/dev/ttyUSB0", "uart": "/dev/ttyUSB0",
"report_topic": "waytous/barrier_gate/1", "report_topic": "/PSZ-001/self_status/report",
"control_topic": "waytous/barrier_gate/1/control" "control_topic": "/PSZ-001/self_control/request",
"barrier_num_map":
[
{"id": 0}
]
} }
...@@ -23,20 +23,33 @@ void printf_hex(const std::vector<uint8_t>& data) ...@@ -23,20 +23,33 @@ void printf_hex(const std::vector<uint8_t>& data)
} }
std::cout << ss.str() << std::endl; std::cout << ss.str() << std::endl;
} }
BarrierGateControl::BarrierGateControl(nlohmann::json& config) BarrierGateControl::BarrierGateControl(nlohmann::json& config,const std::map<int, std::string>& numbarrier_info_config_map)
: self_id(config["self_id"]) : self_id(config["self_id"])
, uart_name_(config["uart"]) , uart_name_(config["uart"])
, report_topic_(config["report_topic"]) , report_topic_(config["report_topic"])
, control_topic_(config["control_topic"]) , control_topic_(config["control_topic"])
,numbarrier_config_map(numbarrier_info_config_map)
{ {
init(); init();
} }
BarrierGateControl::BarrierGateControl(nlohmann::json& config,const std::map<int, std::string>& numbarrier_info_config_map, const std::string& command)
: self_id(config["self_id"])
, uart_name_(config["uart"])
, report_topic_(config["report_topic"])
, control_topic_(config["control_topic"])
, numbarrier_config_map(numbarrier_info_config_map)
, command_(command)
{
init();
}
void BarrierGateControl::exec() void BarrierGateControl::exec()
{ {
__m_running = true; __m_running = true;
uint16_t gate_code = 0; std::string barrier_gate_status = cvt_gate_status(current_gate_);
std::string barrier_gate_status = cvt_gate_status(GateState::OFF); uint16_t gate_code ;
auto start = std::chrono::steady_clock::now(); auto start = std::chrono::steady_clock::now();
while (__m_running) { while (__m_running) {
...@@ -50,13 +63,14 @@ void BarrierGateControl::exec() ...@@ -50,13 +63,14 @@ void BarrierGateControl::exec()
// send read input status request to gate // send read input status request to gate
{ {
std::cout << "read gate input status..." << std::endl; // std::cout << "read gate input status..." << std::endl;
std::vector<uint8_t> request_cmd = {0x01, 0x03, 0x06, 0x00, 0x00, 0x01}; // std::vector<uint8_t> request_cmd = {0x01, 0x03, 0x06, 0x00, 0x00, 0x01};
auto crc = tools::crc16_modbus::crc16_code(request_cmd); // auto crc = tools::crc16_modbus::crc16_code(request_cmd);
request_cmd.insert(request_cmd.end(), crc.begin(), crc.end()); // request_cmd.insert(request_cmd.end(), crc.begin(), crc.end());
std::cout << "send request status to gate: "; // std::cout << "send request status to gate: ";
printf_hex(request_cmd); // printf_hex(request_cmd);
uart_->write(request_cmd.data(), request_cmd.size()); // uart_->write(request_cmd.data(), request_cmd.size());
// gate_control(0,current_gate_);
} }
// publish barrier gate states // publish barrier gate states
...@@ -64,8 +78,8 @@ void BarrierGateControl::exec() ...@@ -64,8 +78,8 @@ void BarrierGateControl::exec()
nlohmann::json j = nlohmann::json::object(); nlohmann::json j = nlohmann::json::object();
j.emplace("self_id", self_id); j.emplace("self_id", self_id);
j.emplace("relay_code", gate_code); j.emplace("relay_code", gate_code);
j.emplace("status", barrier_gate_status); j.emplace("status", cvt_gate_status(current_gate_));
j.emplace("lightsource",2);
auto containts = j.dump(); auto containts = j.dump();
mqtt_dataio::subscriber_basic::DataBuffer buffer; mqtt_dataio::subscriber_basic::DataBuffer buffer;
buffer.resize(containts.size()); buffer.resize(containts.size());
...@@ -92,19 +106,22 @@ void BarrierGateControl::exec() ...@@ -92,19 +106,22 @@ void BarrierGateControl::exec()
std::cout << "crc check ok!" << std::endl; std::cout << "crc check ok!" << std::endl;
uint8_t func_code = buffer[1]; uint8_t func_code = buffer[1];
gate_code = (buffer[len - 4] << 8 | buffer[len - 3]);
if(func_code == 0x03) if(func_code == 0x03)
{ {
gate_code = (buffer[len - 4] << 8 | buffer[len - 3]); //此处属于执行指令成功
barrier_gate_status = cvt_gate_status(gate_code); Exc_Barrier(3,gate_code);
std::cout << "gate code: " << (int)gate_code << ", barrier gate status: " << barrier_gate_status << std::endl;
} }
else if(func_code == 0x0f) else if(func_code == 0x0f)
{ {
// TODO: this is gate control command ack package. // TODO: this is gate control command ack package.
Exc_Barrier(0,gate_code); // 失败
} }
else else
{ {
std::cout << "unknown func code: " << (int)func_code << std::endl; std::cout << "unknown func code: " << (int)func_code << std::endl;
Exc_Barrier(0,gate_code);// 失败
} }
} }
else else
...@@ -139,7 +156,9 @@ void BarrierGateControl::init() ...@@ -139,7 +156,9 @@ void BarrierGateControl::init()
// on start up, do nothing // on start up, do nothing
// current_gate_ = GateState::OFF; // current_gate_ = GateState::OFF;
// gate_control(0, current_gate_); current_gate_= cvt_gate_status(command_);
gate_control(0, current_gate_);
std::cerr << "init gate_control ..........."<<command_<<"current_gate_=" <<current_gate_ <<std::endl;
} }
void BarrierGateControl::on_control_request(const mqtt_dataio::subscriber_basic::DataBuffer& msg) void BarrierGateControl::on_control_request(const mqtt_dataio::subscriber_basic::DataBuffer& msg)
...@@ -167,12 +186,46 @@ void BarrierGateControl::on_control_request(const mqtt_dataio::subscriber_basic: ...@@ -167,12 +186,46 @@ void BarrierGateControl::on_control_request(const mqtt_dataio::subscriber_basic:
void BarrierGateControl::gate_control(const int channel, const GateState gate) void BarrierGateControl::gate_control(const int channel, const GateState gate)
{ {
std::vector<uint8_t> relay_cmd = {0x01, 0x06, 0x05, 0x05, 0x00, static_cast<uint8_t>(gate)}; curExcBarrierQueue=std::queue<roadside_info_barrier>();
for (auto it = numbarrier_config_map.begin(); it != numbarrier_config_map.end(); ++it)
{
roadside_info_barrier _info_barrier;
_info_barrier.Id=it->first;
_info_barrier.gatestate=gate;
curExcBarrierQueue.push(_info_barrier); // 将键存入
}
//第一次执行按失败计算
Exc_Barrier(0,0);
}
void BarrierGateControl::Exc_Barrier(int stateCode,uint16_t changeCode )
{
roadside_info_barrier _info_barrier;
if(stateCode==3)//成功取下一个值
{
if(curExcBarrierQueue.empty())
{
// 所有得切换成功 ,提示状态
current_gate_=(GateState)changeCode;
std::string barrier_gate_status = cvt_gate_status(changeCode);
std::cout << "gate code: " << (int)changeCode << ", barrier gate status: " << barrier_gate_status << std::endl;
return;
}
//成功删除第一个值,取下一个值
curExcBarrierQueue.pop();
_info_barrier=curExcBarrierQueue.front();
}
else //不成功执行当前值
{
_info_barrier=curExcBarrierQueue.front();
}
std::vector<uint8_t> relay_cmd = {_info_barrier.Id, 0x06, 0x05, 0x05, 0x00, static_cast<uint8_t>(_info_barrier.gatestate)};
auto pkg = tools::crc16_modbus::crc16_code(relay_cmd); auto pkg = tools::crc16_modbus::crc16_code(relay_cmd);
relay_cmd.insert(relay_cmd.end(), pkg.begin(), pkg.end()); relay_cmd.insert(relay_cmd.end(), pkg.begin(), pkg.end());
std::cout << "send relay control cmd: "; std::cout << "exc send relay control cmd: ";
printf_hex(relay_cmd); printf_hex(relay_cmd);
uart_->write(relay_cmd.data(), relay_cmd.size()); uart_->write(relay_cmd.data(), relay_cmd.size());
} }
const BarrierGateControl::GateState BarrierGateControl::cvt_gate_status(const std::string& status) const BarrierGateControl::GateState BarrierGateControl::cvt_gate_status(const std::string& status)
...@@ -180,15 +233,15 @@ const BarrierGateControl::GateState BarrierGateControl::cvt_gate_status(const st ...@@ -180,15 +233,15 @@ const BarrierGateControl::GateState BarrierGateControl::cvt_gate_status(const st
// lower status string // lower status string
std::string s = status; std::string s = status;
std::transform(s.begin(), s.end(), s.begin(), ::tolower); std::transform(s.begin(), s.end(), s.begin(), ::tolower);
if(s == "off") if(s == "red")
{ {
return GateState::OFF; return GateState::OFF;
} }
else if(s == "on") else if(s == "green")
{ {
return GateState::ON; return GateState::ON;
} }
else if(s == "pause") else if(s == "yellow")
{ {
return GateState::PAUSE; return GateState::PAUSE;
} }
...@@ -199,11 +252,11 @@ const std::string BarrierGateControl::cvt_gate_status(const GateState& status) ...@@ -199,11 +252,11 @@ const std::string BarrierGateControl::cvt_gate_status(const GateState& status)
switch(status) switch(status)
{ {
case GateState::ON: case GateState::ON:
return "on"; return "green";
case GateState::OFF: case GateState::OFF:
return "off"; return "red";
case GateState::PAUSE: case GateState::PAUSE:
return "pause"; return "yellow";
default: default:
return "unknown"; return "unknown";
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "mqtt/publisher.hpp" #include "mqtt/publisher.hpp"
#include "uart/uart.hpp" #include "uart/uart.hpp"
#include "3th-part/json.hpp" #include "3th-part/json.hpp"
#include <queue>
struct traffic_light_config struct traffic_light_config
{ {
...@@ -43,7 +44,8 @@ struct traffic_mode_config ...@@ -43,7 +44,8 @@ struct traffic_mode_config
class BarrierGateControl class BarrierGateControl
{ {
public: public:
BarrierGateControl(nlohmann::json& config); BarrierGateControl(nlohmann::json& config,const std::map<int, std::string>& numbarrier_info_config_map);
BarrierGateControl(nlohmann::json& config, const std::map<int, std::string>& numbarrier_info_config_map, const std::string& command);
~BarrierGateControl(); ~BarrierGateControl();
void exec(); void exec();
...@@ -56,12 +58,20 @@ public: ...@@ -56,12 +58,20 @@ public:
OFF = 3, OFF = 3,
}; };
struct roadside_info_barrier
{
int Id;
GateState gatestate;
};
const GateState cvt_gate_status(const std::string& status); const GateState cvt_gate_status(const std::string& status);
const std::string cvt_gate_status(const GateState& status); const std::string cvt_gate_status(const GateState& status);
const std::string cvt_gate_status(const uint16_t& status); const std::string cvt_gate_status(const uint16_t& status);
void gate_control(const int channel, const GateState state); void gate_control(const int channel, const GateState state);
void Exc_Barrier(int stateCode,uint16_t changeCode );
private: private:
void init(); void init();
...@@ -69,10 +79,11 @@ private: ...@@ -69,10 +79,11 @@ private:
const std::string uart_name_; const std::string uart_name_;
const std::string report_topic_; const std::string report_topic_;
const std::string control_topic_; const std::string control_topic_;
const std::string command_ = "off";
std::shared_ptr<awe::Uart> uart_; std::shared_ptr<awe::Uart> uart_;
GateState current_gate_; GateState current_gate_=GateState::OFF;
std::shared_ptr<mqtt_dataio::publisher<mqtt_dataio::subscriber_basic::DataBuffer>> __m_status_puber; std::shared_ptr<mqtt_dataio::publisher<mqtt_dataio::subscriber_basic::DataBuffer>> __m_status_puber;
std::shared_ptr<mqtt_dataio::subscriber<mqtt_dataio::subscriber_basic::DataBuffer>> __m_control_suber; std::shared_ptr<mqtt_dataio::subscriber<mqtt_dataio::subscriber_basic::DataBuffer>> __m_control_suber;
...@@ -84,4 +95,6 @@ private: ...@@ -84,4 +95,6 @@ private:
uint8_t __m_expand_size; uint8_t __m_expand_size;
uint16_t __m_offset_do_addr; uint16_t __m_offset_do_addr;
std::map<int, std::string> numbarrier_config_map;
std::queue<BarrierGateControl::roadside_info_barrier>curExcBarrierQueue;
}; };
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <csignal>
#include "3th-part/json.hpp" #include "3th-part/json.hpp"
#include "mqtt/mqtt_client.hpp" #include "mqtt/mqtt_client.hpp"
#include "barrier_gate.hpp" #include "barrier_gate.hpp"
void logExit() {
std::ofstream logfile("exit_log.txt", std::ios::app);
if (logfile.is_open()) {
logfile << "Program exited normally." << std::endl;
logfile.close();
} else {
std::cerr << "Failed to open log file!" << std::endl;
}
}
void signalHandler(int signal) {
std::ofstream logfile("exit_log.txt", std::ios::app);
if (logfile.is_open()) {
logfile << "Program exited due to signal: " << signal << std::endl;
logfile.close();
} else {
std::cerr << "Failed to open log file!" << std::endl;
}
std::exit(signal); // 确保程序退出
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
// 注册正常退出日志函数
std::atexit(logExit);
// 注册信号处理函数
std::signal(SIGINT, signalHandler); // Ctrl+C
std::signal(SIGTERM, signalHandler); // kill 命令
std::signal(SIGSEGV, signalHandler); // 段错误
if (argc < 2) if (argc < 2)
{ {
std::cout << "please input config file, such as config.json" << std::endl; std::cout << "please input config file, such as config.json" << std::endl;
...@@ -26,6 +57,13 @@ int main(int argc, char *argv[]) ...@@ -26,6 +57,13 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
std::string command = "red"; // off/on/
if(argc == 3)
{
command = argv[2];
std::cout << "command: file" << command << std::endl;
}
std::ostringstream ss; std::ostringstream ss;
ss << file.rdbuf(); ss << file.rdbuf();
std::string content = ss.str(); std::string content = ss.str();
...@@ -44,10 +82,21 @@ int main(int argc, char *argv[]) ...@@ -44,10 +82,21 @@ int main(int argc, char *argv[])
uint16_t port = j["mqtt_server_port"]; uint16_t port = j["mqtt_server_port"];
std::string user = j["mqtt_server_user"]; std::string user = j["mqtt_server_user"];
std::string pwd = j["mqtt_server_pwd"]; std::string pwd = j["mqtt_server_pwd"];
auto num_config = j["barrier_num_map"];
std::map<int, std::string> numbarrier_config;
for (auto tmp : num_config)
{
std::stringstream ss;
// 将整数转换为字符串
ss << tmp["id"];
std::string str_num = ss.str();
numbarrier_config.insert({tmp["id"], str_num});
}
mqtt_dataio::client::ins().init(ip, port, user, pwd); mqtt_dataio::client::ins().init(ip, port, user, pwd);
auto node = std::make_shared<BarrierGateControl>(j); auto node = std::make_shared<BarrierGateControl>(j,numbarrier_config,command);
node->exec(); node->exec();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment