91 lines
2.5 KiB
C++
91 lines
2.5 KiB
C++
|
|
#include "SimMsg.h"
|
||
|
|
#include "test6.h"
|
||
|
|
|
||
|
|
#include <iostream>
|
||
|
|
#include <string>
|
||
|
|
#include <vector>
|
||
|
|
#include <cstring>
|
||
|
|
|
||
|
|
|
||
|
|
using namespace std;
|
||
|
|
|
||
|
|
void test6WriteLog(const std::string &msg)
|
||
|
|
{
|
||
|
|
std::cout << msg<<std::endl;
|
||
|
|
}
|
||
|
|
|
||
|
|
string topic_name_cmd = "Command";
|
||
|
|
string topic_name_tlm = "Telemetry";
|
||
|
|
|
||
|
|
SimMsg* test6_part = nullptr;
|
||
|
|
string servername = "test_Service";
|
||
|
|
|
||
|
|
void generate_test_data(uint8_t* buffer, size_t size) {
|
||
|
|
memset(buffer, 0, size);
|
||
|
|
for(size_t i = 0; i < 10 && i < size; i++) {
|
||
|
|
buffer[i] = (uint8_t)i;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//初始化
|
||
|
|
void test6_init(uint8_t domainid, std::string appname)
|
||
|
|
{
|
||
|
|
std::vector<std::string> parameters;
|
||
|
|
string expression = "src = '"+ servername + "'";
|
||
|
|
if (nullptr == test6_part)
|
||
|
|
{
|
||
|
|
test6_part=new SimMsg(domainid, 3000, appname, test6WriteLog);
|
||
|
|
test6_part->create_pub(topic_name_cmd);
|
||
|
|
test6_part->create_pub(topic_name_tlm);
|
||
|
|
|
||
|
|
test6_part->create_sub(topic_name_cmd, command_callback, expression, parameters);
|
||
|
|
test6_part->create_sub(topic_name_tlm, telemetry_callback, expression, parameters);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 遥控接收回调函数
|
||
|
|
void command_callback(std::string src, std::string dest, std::string type,
|
||
|
|
std::string reserve1, std::string reserve2,
|
||
|
|
std::vector<uint8_t>& data) {
|
||
|
|
|
||
|
|
uint8_t test6_data[256] = {5};
|
||
|
|
|
||
|
|
generate_test_data(test6_data, 256);
|
||
|
|
uint16_t data_len = 256;
|
||
|
|
|
||
|
|
// 直接传递数组,不需要.data()
|
||
|
|
test6_command_Pub(test6_data, dest, data_len); // 使用传入的dest参数
|
||
|
|
}
|
||
|
|
|
||
|
|
// 遥测接收回调函数
|
||
|
|
void telemetry_callback(std::string src, std::string dest, std::string type,
|
||
|
|
std::string reserve1, std::string reserve2,
|
||
|
|
std::vector<uint8_t>& data) {
|
||
|
|
// 打印接收到的数据
|
||
|
|
std::cout << "Telemetry received from " << src << " to " << dest << std::endl;
|
||
|
|
std::cout << "Type: " << type << std::endl;
|
||
|
|
std::cout << "Data (" << data.size() << " bytes): ";
|
||
|
|
for (size_t i = 0; i < data.size(); ++i) {
|
||
|
|
std::cout << std::hex << std::uppercase << (int)data[i];
|
||
|
|
if (i < data.size() - 1) {
|
||
|
|
std::cout << ", ";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
std::cout << std::dec << std::endl;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// AD硬件服务化遥控发布
|
||
|
|
void test6_command_Pub(uint8_t* data, string dest, uint16_t len)
|
||
|
|
{
|
||
|
|
test6_part->publish(topic_name_cmd, "test6_Service", dest, "command", data, len);
|
||
|
|
}
|
||
|
|
|
||
|
|
// AD硬件服务化遥测发布
|
||
|
|
void test6_telemetry_Pub(uint8_t* data, string dest, uint16_t len)
|
||
|
|
{
|
||
|
|
test6_part->publish(topic_name_tlm, "test6_Service", dest, "telemetry", data, len);
|
||
|
|
}
|
||
|
|
|
||
|
|
|