-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_builder.cpp
More file actions
157 lines (127 loc) · 7.14 KB
/
xml_builder.cpp
File metadata and controls
157 lines (127 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Created by pengx on 2025/9/26.
//
#include "xml_builder.hpp"
#include <sstream>
#include <iomanip>
#include <iostream>
#include "pugixml.hpp"
/**
* 添加通用 XML 声明: <?xml version="1.0" encoding="GB2312"?>
* @param doc pugi::xml_document
*/
static void add_xml_declaration(pugi::xml_document& doc) {
pugi::xml_node declaration = doc.prepend_child(pugi::node_declaration);
declaration.append_attribute("version") = "1.0";
declaration.append_attribute("encoding") = "GB2312";
}
std::string XmlBuilder::buildDeviceInfo(const std::string& sn, const std::string& device_code,
const std::string& device_name,
const std::string& serial_number) {
std::cout << "┌──────────────────────────────────────┐" << std::endl;
std::cout << "│ 构建设备信息 XML" << std::endl;
std::cout << "│ CmdType: DeviceInfo" << std::endl;
std::cout << "│ SN: " << sn.c_str() << std::endl;
std::cout << "│ DeviceID: " << device_code.c_str() << std::endl;
std::cout << "└──────────────────────────────────────┘" << std::endl;
pugi::xml_document xml;
// 添加 XML 声明:<?xml version="1.0" encoding="GB2312"?>
add_xml_declaration(xml);
// 根节点 <Response>
pugi::xml_node root = xml.append_child("Response");
root.append_child("CmdType").text() = "DeviceInfo";
root.append_child("SN").text() = sn.c_str();
root.append_child("DeviceID").text() = device_code.c_str();
root.append_child("DeviceName").text() = device_name.c_str();
root.append_child("Manufacturer").text() = "CasicGBDevice";
root.append_child("Model").text() = "GBDevice";
root.append_child("Firmware").text() = "1.0.0";
root.append_child("SerialNumber").text() = serial_number.c_str();
root.append_child("Status").text() = "ON";
// 保存到字符串,使用 2 空格缩进,保持可读性
std::ostringstream oss;
xml.save(oss, " ", pugi::format_default, pugi::encoding_utf8);
std::string result = oss.str();
std::cout << "┌─────────── DeviceInfo XML ───────────┐" << std::endl;
std::cout << result.c_str() << std::endl;
std::cout << "└──────────────────────────────────────┘" << std::endl;
return result;
}
std::string XmlBuilder::buildCatalog(const std::string& sn, const std::string& device_code,
const std::string& server_domain, double longitude, double latitude) {
std::cout << "┌──────────────────────────────────────┐" << std::endl;
std::cout << "│ 构建设备目录 XML" << std::endl;
std::cout << "│ CmdType: Catalog" << std::endl;
std::cout << "│ SN: " << sn.c_str() << std::endl;
std::cout << "│ DeviceID: " << device_code.c_str() << std::endl;
std::cout << "└──────────────────────────────────────┘" << std::endl;
pugi::xml_document xml;
// 添加 XML 声明
add_xml_declaration(xml);
// 根节点 <Response>
pugi::xml_node root = xml.append_child("Response");
root.append_child("CmdType").text() = "Catalog";
root.append_child("SN").text() = sn.c_str();
root.append_child("DeviceID").text() = device_code.c_str();
// SumNum 和 DeviceList
root.append_child("SumNum").text() = "1";
pugi::xml_node deviceList = root.append_child("DeviceList");
deviceList.append_attribute("Num") = "1";
// 添加通道 Item
pugi::xml_node item = deviceList.append_child("Item");
// 正确生成通道ID:设备ID + "0001" (子设备编号)
std::string channel_id = device_code.substr(0, 16) + "0001";
item.append_child("DeviceID").text() = channel_id.c_str();
item.append_child("Name").text() = "Channel01";
item.append_child("Manufacturer").text() = "CasicGBDevice";
item.append_child("Model").text() = "GBDevice";
item.append_child("Owner").text() = "Pengxh";
item.append_child("CivilCode").text() = server_domain.c_str();
item.append_child("Address").text() = "";
item.append_child("ParentID").text() = device_code.c_str();
item.append_child("Parental").text() = "1";
item.append_child("SafetyWay").text() = "0";
item.append_child("RegisterWay").text() = "1";
item.append_child("Secrecy").text() = "0";
item.append_child("Status").text() = "ON";
// 使用 setprecision 控制浮点数输出精度
std::ostringstream lon_stream, lat_stream;
lon_stream << std::fixed << std::setprecision(6) << longitude;
lat_stream << std::fixed << std::setprecision(6) << latitude;
item.append_child("Longitude").text() = lon_stream.str().c_str();
item.append_child("Latitude").text() = lat_stream.str().c_str();
item.append_child("Altitude").text() = "0";
// 保存为格式化字符串(带缩进)
std::ostringstream oss;
xml.save(oss, " ", pugi::format_default, pugi::encoding_utf8);
std::string result = oss.str();
std::cout << "┌──────────── Catalog XML ─────────────┐" << std::endl;
std::cout << result.c_str() << std::endl;
std::cout << "└──────────────────────────────────────┘" << std::endl;
return result;
}
std::string XmlBuilder::buildHeartbeat(const std::string& sn, const std::string& device_code) {
std::cout << "┌──────────────────────────────────────┐" << std::endl;
std::cout << "│ 构建心跳 XML" << std::endl;
std::cout << "│ CmdType: Keepalive" << std::endl;
std::cout << "│ SN: " << sn.c_str() << std::endl;
std::cout << "│ DeviceID: " << device_code.c_str() << std::endl;
std::cout << "└──────────────────────────────────────┘" << std::endl;
pugi::xml_document xml;
// 添加 XML 声明
add_xml_declaration(xml);
// 根节点 <Notify>
pugi::xml_node root = xml.append_child("Notify");
root.append_child("CmdType").text() = "Keepalive";
root.append_child("SN").text() = sn.c_str();
root.append_child("DeviceID").text() = device_code.c_str();
root.append_child("Status").text() = "OK";
// 保存到字符串,使用 2 空格缩进,保持可读性
std::ostringstream oss;
xml.save(oss, " ", pugi::format_default, pugi::encoding_utf8);
std::string result = oss.str();
std::cout << "┌─────────── Keepalive XML ────────────┐" << std::endl;
std::cout << result.c_str() << std::endl;
std::cout << "└──────────────────────────────────────┘" << std::endl;
return result;
}