Browse Source

#application

zxs 3 tháng trước cách đây
mục cha
commit
59d3256dd0
4 tập tin đã thay đổi với 263 bổ sung220 xóa
  1. 3 3
      CMakeLists.txt
  2. 52 0
      robot/robotics/application.hpp
  3. 208 215
      robot/robotics/nexus_net_client.hpp
  4. 0 2
      robot/robotics/utils.hpp

+ 3 - 3
CMakeLists.txt

@@ -21,7 +21,7 @@ elseif(UNIX)
     add_definitions(-DLINUX_BUILD)
 endif()
 # 包含子项目。
-add_subdirectory ("robot")
+# add_subdirectory ("robot")
 add_subdirectory ("logger")
-add_subdirectory ("drivers")
-add_subdirectory ("generator")
+# add_subdirectory ("drivers")
+# add_subdirectory ("generator")

+ 52 - 0
robot/robotics/application.hpp

@@ -0,0 +1,52 @@
+/**
+*
+*  @file application.hpp
+*  @author zxw
+*  Copyright 2024, yjrobotics.  All rights reserved.
+*
+*  robotics
+*
+*/
+#pragma once
+//stl
+#include <iostream>
+//robotics
+#include "utils.hpp"
+#include "logger.hpp"
+
+namespace robotics::v3 {
+    class application_base {
+    public:
+        virtual int run() = 0;
+        virtual int debug() = 0;
+    };
+    template<typename _Type>
+    class application {
+    public:
+        static int run(int argc, char* argv[]) {
+            SET_CONSOLE_UTF8;
+            int result = -2;
+            if (argc <= 0) {
+                result = -1;
+            }
+            else if (0 == strcmp(argv[argc - 1], "run")) {
+                LOG_INSTALL;
+                result = app_->run();
+            }
+            else if (0 == strcmp(argv[argc - 1], "debug") || 0 == strcmp(argv[argc - 1], "cmakedebug")) {
+                result = app_->debug();
+            }
+            return result;
+        }
+    private:
+        application() {
+            app_ = std::make_shared<_Type>();
+        }
+        static application* instance() {
+            static application g_application;
+            return &application;
+        }
+    private:
+        std::shared_ptr<application_base> app_;
+    };
+}

+ 208 - 215
robot/robotics/nexus_net_client.hpp

@@ -1,6 +1,8 @@
 #pragma once
 //stl
 #include <iostream>
+//asio2
+#include <asio2/asio2.hpp>
 //boost
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/xml_parser.hpp>
@@ -20,18 +22,18 @@ namespace robotics::v3 {
     class match_role {
     public:
         /**
-         * @brief 默认构造
+         * @brief 榛樿�鏋勯€�
          */
         explicit match_role() {}
         /**
-         * @brief 构造
+         * @brief 鏋勯€�
          * @param session
          */
         explicit match_role(std::shared_ptr<_Type> const& session) :
             session_(session) {
         }
         /**
-         * @brief 解包
+         * @brief 瑙e寘
          * @tparam Iterator
          * @param begin
          * @param end
@@ -41,13 +43,13 @@ namespace robotics::v3 {
         std::pair<Iterator, bool> operator()(Iterator begin, Iterator end) const {
             Iterator p = begin;
             while (p != end) {
-                //判断协议头
+                //鍒ゆ柇鍗忚�澶�
                 if (*p != 0x02) {
                     asio2::error_code ec;
                     session_->socket().close(ec);
                     break;
                 }
-                //获取数据长度
+                //鑾峰彇鏁版嵁闀垮害
                 size_t size = size_t(std::uint8_t(*(++p))) << 24;
                 size += size_t(std::uint8_t(*(++p))) << 16;
                 size += size_t(std::uint8_t(*(++p))) << 8;
@@ -67,7 +69,7 @@ namespace robotics::v3 {
             return std::pair(begin, false);
         }
         /**
-         * @brief 初始化
+         * @brief 鍒濆�鍖�
          * @param session
          */
         void init(std::shared_ptr<asio2::tcp_session>& session) {
@@ -75,103 +77,103 @@ namespace robotics::v3 {
         }
     private:
         /**
-         * @brief 客户端
+         * @brief 瀹㈡埛绔�
          */
         std::shared_ptr<_Type> session_;
     };
-
-    template<> struct asio::is_match_condition<match_role<asio2::tcp_client>> : public  std::true_type {};
-
-    template<> struct asio::is_match_condition<match_role<asio2::tcp_session>> : public std::true_type {};
+}
+template<> struct asio::is_match_condition<robotics::v3::match_role<asio2::tcp_client>> : public  std::true_type {};
+template<> struct asio::is_match_condition<robotics::v3::match_role<asio2::tcp_session>> : public std::true_type {};
+namespace robotics::v3 {
     /**
-     * @brief 消息类型
+     * @brief 娑堟伅绫诲瀷
      */
     enum class nexus_net_msg_type_enum {
         /**
-         * @brief 身份认证
+         * @brief 韬�唤璁よ瘉
          */
         AUTHENTICATE = 1,
         /**
-         * @brief 身份认证响应
+         * @brief 韬�唤璁よ瘉鍝嶅簲
         */
         REPAUTHENTICATE,
         /**
-         * @brief 订阅
+         * @brief 璁㈤槄
          */
         SUBSCRIBE,
         /**
-         * @brief 删除订阅
+         * @brief 鍒犻櫎璁㈤槄
          */
         REMOVESUBSCRIBE,
         /**
-         * @brief 发布
+         * @brief 鍙戝竷
          */
         PUBLISHER,
         /**
-         * @brief 发布响应
+         * @brief 鍙戝竷鍝嶅簲
          */
         REPPUBLISHER,
         /**
-         * @brief 心跳
+         * @brief 蹇冭烦
          */
         HEARTBEAT,
         /**
-         * @brief 详情
+         * @brief 璇︽儏
          */
         DETAILS,
         /**
-         * @brief 详情响应
+         * @brief 璇︽儏鍝嶅簲
          */
         REPDETAILS
     };
     /**
-     * @brief 消息
+     * @brief 娑堟伅
      */
     class nexus_net_message {
     public:
         /**
-         * @brief 构造
+         * @brief 鏋勯€�
          */
         nexus_net_message() {}
         /**
-         * @brief 构造
+         * @brief 鏋勯€�
          * @param sv
          */
         nexus_net_message(std::string_view sv) {
             set_data(sv);
         }
         /**
-         * @brief 构造
+         * @brief 鏋勯€�
          * @param data
          */
         nexus_net_message(std::vector<std::uint8_t>const& data) {
             set_data(data);
         }
         /**
-         * @brief 设置
+         * @brief 璁剧疆
          * @param sv
          */
         void set_data(std::string_view sv) {
             set_data(std::vector<std::uint8_t>(sv.begin(), sv.end()));
         }
         /**
-         * @brief 设置
+         * @brief 璁剧疆
          * @param data
          */
         void set_data(std::vector<std::uint8_t>const& data) {
-            //消息类型
+            //娑堟伅绫诲瀷
             msg_type = data[5];
-            //消息ID
+            //娑堟伅ID
             size_t size = data[6];
             size_t begin = 7;
             size_t end = begin + size;
             msg_id.assign(data.begin() + begin, data.begin() + end);
-            //路由
+            //璺�敱
             size = data[end];
             begin = end + 1;
             end = begin + size;
             route.assign(data.begin() + begin, data.begin() + end);
-            //路由列表
+            //璺�敱鍒楄〃
             size = data[end] << 8;
             size += data[++end];
             begin = end + 1;
@@ -180,7 +182,7 @@ namespace robotics::v3 {
                 std::string str_routes(data.begin() + begin, data.begin() + end);
                 routes = v3::utils::split(str_routes, 0x0d);
             }
-            //参数
+            //鍙傛暟
             size = (data[end] << 24);
             size += (data[++end] << 16);
             size += (data[++end] << 8);
@@ -190,27 +192,27 @@ namespace robotics::v3 {
             args.add(std::vector<std::uint8_t>(data.begin() + begin, data.begin() + end));
         }
         /**
-         * @brief 获取
+         * @brief 鑾峰彇
          * @return
          */
         std::vector<std::uint8_t> get_data() {
             std::vector<std::uint8_t> result;
-            //协议头
+            //鍗忚�澶�
             result.push_back(0x02);
-            //字节数量
+            //瀛楄妭鏁伴噺
             result.push_back(0x00);
             result.push_back(0x00);
             result.push_back(0x00);
             result.push_back(0x00);
-            //消息类型
+            //娑堟伅绫诲瀷
             result.push_back(msg_type);
-            //消息ID
+            //娑堟伅ID
             result.push_back(msg_id.size());
             result.insert(result.end(), msg_id.begin(), msg_id.end());
-            //路由
+            //璺�敱
             result.push_back(route.size());
             result.insert(result.end(), route.begin(), route.end());
-            //路由列表
+            //璺�敱鍒楄〃
             std::vector<std::uint8_t> vec_routes;
             for (size_t i = 0; i < routes.size(); ++i) {
                 if (i + 1 == routes.size()) {
@@ -224,14 +226,14 @@ namespace robotics::v3 {
             result.push_back(vec_routes.size() >> 8);
             result.push_back(vec_routes.size() >> 0);
             result.insert(result.end(), vec_routes.begin(), vec_routes.end());
-            //参数
+            //鍙傛暟
             std::vector<std::uint8_t> vec_args = args.data();
             result.push_back(vec_args.size() >> 24);
             result.push_back(vec_args.size() >> 16);
             result.push_back(vec_args.size() >> 8);
             result.push_back(vec_args.size() >> 0);
             result.insert(result.end(), vec_args.begin(), vec_args.end());
-            //协议尾
+            //鍗忚�灏�
             result.push_back(0x03);
 
             result[1] = (result.size() >> 24);
@@ -242,66 +244,66 @@ namespace robotics::v3 {
         }
     public:
         /**
-         * @brief 消息类型
+         * @brief 娑堟伅绫诲瀷
          */
         std::uint8_t msg_type = 0;
         /**
-         * @brief 消息ID
+         * @brief 娑堟伅ID
          */
         std::string msg_id;
         /**
-         * @brief 路由
+         * @brief 璺�敱
          */
         std::string route;
         /**
-         * @brief 路由列表
+         * @brief 璺�敱鍒楄〃
          */
         std::vector<std::string> routes;
         /**
-         * @brief 参数
+         * @brief 鍙傛暟
          */
         v3::archive::stream args;
     };
     /**
-     * @brief 日志类型
+     * @brief 鏃ュ織绫诲瀷
      */
     struct nexus_net_logger_config_info {
         /**
-         * @brief 日志类型
+         * @brief 鏃ュ織绫诲瀷
          */
         int type = 0;
         /**
-         * @brief 启用
+         * @brief 鍚�敤
          */
         bool enable = false;
         /**
-         * @brief 备注
+         * @brief 澶囨敞
          */
         std::string remarks;
     };
     /**
-     * @brief 父节点配置
+     * @brief 鐖惰妭鐐归厤缃�
      */
     struct nexus_net_client_config_info {
         /**
-         * @brief IP地址
+         * @brief IP鍦板潃
          */
         std::string ip;
         /**
-         * @brief 端口
+         * @brief 绔�彛
          */
         int port = 0;
         /**
-         * @brief 启用
+         * @brief 鍚�敤
          */
         bool enable = false;
         /**
-         * @brief 备注
+         * @brief 澶囨敞
          */
         std::string remarks;
     };
     /**
-     * @brief 订阅配置
+     * @brief 璁㈤槄閰嶇疆
      */
     struct nexus_net_subscribe_config_info {
         /**
@@ -309,20 +311,20 @@ namespace robotics::v3 {
          */
         std::string key;
         /**
-         * @brief 路由
+         * @brief 璺�敱
          */
         std::string route;
         /**
-         * @brief 启用
+         * @brief 鍚�敤
          */
         bool enable = false;
         /**
-         * @brief 备注
+         * @brief 澶囨敞
          */
         std::string remarks;
     };
     /**
-     * @brief 发布配置
+     * @brief 鍙戝竷閰嶇疆
      */
     struct nexus_net_publisher_config_info {
         /**
@@ -330,70 +332,74 @@ namespace robotics::v3 {
          */
         std::string key;
         /**
-         * @brief 节点
+         * @brief 鑺傜偣
          */
         std::string route;
         /**
-         * @brief 启用
+         * @brief 鍚�敤
          */
         bool enable = false;
         /**
-         * @brief 备注
+         * @brief 澶囨敞
          */
         std::string remarks;
     };
     /**
-     * @brief 用户信息
+     * @brief 鐢ㄦ埛淇℃伅
      */
     struct nexus_net_user_config_info {
         /**
-         * @brief 名称
+         * @brief 鍚嶇О
          */
         std::string name;
         /**
-         * @brief 密码
+         * @brief 浠g爜
+         */
+        std::string code;
+        /**
+         * @brief 瀵嗙爜
          */
         std::string password;
         /**
-         * @brief 启用
+         * @brief 鍚�敤
          */
         bool enable = false;
         /**
-         * @brief 备注
+         * @brief 澶囨敞
          */
         std::string remarks;
     };
     /**
-     * @brief 配置
+     * @brief 閰嶇疆
      */
     struct nexus_net_config_info {
         /**
-         * @brief 身份
+         * @brief 韬�唤
          */
         nexus_net_user_config_info user;
         /**
-         * @brief 日志
+         * @brief 鏃ュ織
          */
         std::vector<nexus_net_logger_config_info> loggers;
         /**
-         * @brief 客户端
+         * @brief 瀹㈡埛绔�
          */
         nexus_net_client_config_info client;
         /**
-         * @brief 订阅
+         * @brief 璁㈤槄
          */
-        std::vector<nexus_net_subscribe_config_info> subscribes;
+        std::map<std::string, nexus_net_subscribe_config_info> subscribes;
         /**
-         * @brief 发布
+         * @brief 鍙戝竷
          */
-        std::vector<nexus_net_publisher_config_info> publishers;
+        std::map<std::string, nexus_net_publisher_config_info> publishers;
     };
     /**
-     * @brief nexusnet配置
+     * @brief nexusnet閰嶇疆
      */
     class nexus_net_config {
     public:
-        static nexus_net_config_info read() {
+        static nexus_net_config_info& read() {
             static std::shared_ptr<nexus_net_config_info> result;
             static std::mutex mutex;
             std::lock_guard<std::mutex> locker(mutex);
@@ -401,38 +407,39 @@ namespace robotics::v3 {
                 return *result;
             result.reset(new nexus_net_config_info());
             boost::property_tree::ptree root;
-            read_xml("config/nexus_net_config.xml", root);
-            //身份信息
+            read_xml(v3::config::read<std::string>("NEXUS_NET", "PATH", "./config/nexus_net_config.xml"), root);
+            //韬�唤淇℃伅
             for (auto& it : root.get_child("root.user")) {
                 if (it.first != "item" || !it.second.get<bool>("<xmlattr>.enable", false))
                     continue;
                 result->user.name = it.second.get<std::string>("<xmlattr>.name", "admin");
+                result->user.code = it.second.get<std::string>("<xmlattr>.code", "admin");
                 result->user.password = it.second.get<std::string>("<xmlattr>.password", "123456");
                 result->user.enable = it.second.get<bool>("<xmlattr>.enable", true);
-                result->user.remarks = it.second.get<std::string>("<xmlattr>.remarks", "身份信息");
+                result->user.remarks = it.second.get<std::string>("<xmlattr>.remarks", "韬�唤淇℃伅");
                 break;
             }
-            //日志
+            //鏃ュ織
             for (auto& it : root.get_child("root.logger")) {
                 if (it.first != "item" || !it.second.get<bool>("<xmlattr>.enable", false))
                     continue;
                 nexus_net_logger_config_info item;
                 item.type = it.second.get<int>("<xmlattr>.type", 0);
                 item.enable = it.second.get<bool>("<xmlattr>.enable", true);
-                item.remarks = it.second.get<std::string>("<xmlattr>.remarks", "日志");
+                item.remarks = it.second.get<std::string>("<xmlattr>.remarks", "鏃ュ織");
                 result->loggers.push_back(item);
             }
-            //客户端
+            //瀹㈡埛绔�
             for (auto& it : root.get_child("root.client")) {
                 if (it.first != "item" || !it.second.get<bool>("<xmlattr>.enable", false))
                     continue;
-                result->client.ip = it.second.get<std::string>("<xmlattr>.ip", "0.0.0.0");
+                result->client.ip = it.second.get<std::string>("<xmlattr>.ip", "127.0.0.1");
                 result->client.port = it.second.get<int>("<xmlattr>.port", 20001);
                 result->client.enable = it.second.get<bool>("<xmlattr>.enable", false);
-                result->client.remarks = it.second.get<std::string>("<xmlattr>.remarks", "客户");
+                result->client.remarks = it.second.get<std::string>("<xmlattr>.remarks", "瀹㈡埛");
                 break;
             }
-            //订阅
+            //璁㈤槄
             for (auto& it : root.get_child("root.subscribe")) {
                 if (it.first != "item" || !it.second.get<bool>("<xmlattr>.enable", false))
                     continue;
@@ -440,10 +447,10 @@ namespace robotics::v3 {
                 item.key = it.second.get<std::string>("<xmlattr>.key", "");
                 item.route = it.second.get<std::string>("<xmlattr>.route", "");
                 item.enable = it.second.get<bool>("<xmlattr>.enable", false);
-                item.remarks = it.second.get<std::string>("<xmlattr>.remarks", "白名单");
-                result->subscribes.push_back(item);
+                item.remarks = it.second.get<std::string>("<xmlattr>.remarks", "鐧藉悕鍗�");
+                result->subscribes[item.key] = item;
             }
-            //发布
+            //鍙戝竷
             for (auto& it : root.get_child("root.publisher")) {
                 if (it.first != "item" || !it.second.get<bool>("<xmlattr>.enable", false))
                     continue;
@@ -451,38 +458,38 @@ namespace robotics::v3 {
                 item.key = it.second.get<std::string>("<xmlattr>.key", "");
                 item.route = it.second.get<std::string>("<xmlattr>.route", "");
                 item.enable = it.second.get<bool>("<xmlattr>.enable", false);
-                item.remarks = it.second.get<std::string>("<xmlattr>.remarks", "白名单");
-                result->publishers.push_back(item);
+                item.remarks = it.second.get<std::string>("<xmlattr>.remarks", "鐧藉悕鍗�");
+                result->publishers[item.key] = item;
             }
             return *result;
         }
         static void logger(bool success, bool is_out, nexus_net_message value) {
             static std::map<std::uint8_t, std::string> g_msg_type = {
-                {(std::uint8_t)nexus_net_msg_type_enum::AUTHENTICATE,     "身份认证"},
-                {(std::uint8_t)nexus_net_msg_type_enum::REPAUTHENTICATE,  "身份认证响应"},
-                {(std::uint8_t)nexus_net_msg_type_enum::SUBSCRIBE,        "订阅"},
-                {(std::uint8_t)nexus_net_msg_type_enum::REMOVESUBSCRIBE,  "删除订阅"},
-                {(std::uint8_t)nexus_net_msg_type_enum::PUBLISHER,        "发布"},
-                {(std::uint8_t)nexus_net_msg_type_enum::REPPUBLISHER,     "发布响应"},
-                {(std::uint8_t)nexus_net_msg_type_enum::HEARTBEAT,        "心跳"},
-                {(std::uint8_t)nexus_net_msg_type_enum::DETAILS,          "详情"},
-                {(std::uint8_t)nexus_net_msg_type_enum::REPDETAILS,       "详情响应"}
+                {(std::uint8_t)nexus_net_msg_type_enum::AUTHENTICATE,     "韬�唤璁よ瘉"},
+                {(std::uint8_t)nexus_net_msg_type_enum::REPAUTHENTICATE,  "韬�唤璁よ瘉鍝嶅簲"},
+                {(std::uint8_t)nexus_net_msg_type_enum::SUBSCRIBE,        "璁㈤槄"},
+                {(std::uint8_t)nexus_net_msg_type_enum::REMOVESUBSCRIBE,  "鍒犻櫎璁㈤槄"},
+                {(std::uint8_t)nexus_net_msg_type_enum::PUBLISHER,        "鍙戝竷"},
+                {(std::uint8_t)nexus_net_msg_type_enum::REPPUBLISHER,     "鍙戝竷鍝嶅簲"},
+                {(std::uint8_t)nexus_net_msg_type_enum::HEARTBEAT,        "蹇冭烦"},
+                {(std::uint8_t)nexus_net_msg_type_enum::DETAILS,          "璇︽儏"},
+                {(std::uint8_t)nexus_net_msg_type_enum::REPDETAILS,       "璇︽儏鍝嶅簲"}
             };
             auto config = read();
             auto it = std::find_if(config.loggers.begin(), config.loggers.end(), [=](auto& it) {
                 return it.type == value.msg_type && it.enable; });
             if (it != config.loggers.end()) {
                 if (success) {
-                    LOG_INFO << (is_out ? "接收成功" : "发送成功") << ",消息类型:" << g_msg_type[value.msg_type] << " 消息ID:" << value.msg_id << " 事件:" << value.route << " 路由:" << value.routes;
+                    LOG_INFO << (is_out ? "鎺ユ敹鎴愬姛" : "鍙戦€佹垚鍔�") << ",娑堟伅绫诲瀷:" << g_msg_type[value.msg_type] << " 娑堟伅ID:" << value.msg_id << " 浜嬩欢:" << value.route << " 璺�敱:" << value.routes;
                 }
                 else {
-                    LOG_ERROR << (is_out ? "接收失败" : "发送失败") << ",消息类型:" << g_msg_type[value.msg_type] << " 消息ID:" << value.msg_id << " 事件:" << value.route << " 路由:" << value.routes;
+                    LOG_ERROR << (is_out ? "鎺ユ敹澶辫触" : "鍙戦€佸け璐�") << ",娑堟伅绫诲瀷:" << g_msg_type[value.msg_type] << " 娑堟伅ID:" << value.msg_id << " 浜嬩欢:" << value.route << " 璺�敱:" << value.routes;
                 }
             }
         }
     };
     /**
-     * @brief 客户端
+     * @brief 瀹㈡埛绔�
      */
     class nexus_net_tcp_client {
     public:
@@ -518,13 +525,13 @@ namespace robotics::v3 {
         std::shared_ptr<asio2::tcp_client> client_;
     };
     /**
-     * @brief 客户端
+     * @brief 瀹㈡埛绔�
      */
     class nexus_net_client : public v3::thread_pool<nexus_net_client> {
     private:
         class message_manage_gc;
         /**
-         * @brief 消息管理
+         * @brief 娑堟伅绠$悊
          */
         class message_manage {
         private:
@@ -532,7 +539,7 @@ namespace robotics::v3 {
             typedef std::map<std::string, std::pair<std::shared_ptr<v3::event_wait_for>, nexus_net_message>> message_list_type;
         public:
             /**
-             * @brief 添加
+             * @brief 娣诲姞
              * @param msg_id
              * @return
              */
@@ -542,7 +549,7 @@ namespace robotics::v3 {
                 return msg_list_[msg_id].first;
             }
             /**
-             * @brief 获取数据
+             * @brief 鑾峰彇鏁版嵁
              * @param msg_id
              * @return
              */
@@ -551,7 +558,7 @@ namespace robotics::v3 {
                 return msg_list_[msg_id].second;
             }
             /**
-             * @brief 设置数据
+             * @brief 璁剧疆鏁版嵁
              * @param msg_id
              * @param data
              */
@@ -561,7 +568,7 @@ namespace robotics::v3 {
                 msg_list_[msg_id].first->notify();
             }
             /**
-             * @brief 查找数据
+             * @brief 鏌ユ壘鏁版嵁
              * @param msg_id
              * @return
              */
@@ -570,7 +577,7 @@ namespace robotics::v3 {
                 return msg_list_.find(msg_id) != msg_list_.end();
             }
             /**
-             * @brief 移出
+             * @brief 绉诲嚭
              * @param msg_id
              */
             void remove(std::string const& msg_id) {
@@ -582,12 +589,12 @@ namespace robotics::v3 {
             std::mutex          mutex_;
         };
         /**
-         * @brief 消息管理GC
+         * @brief 娑堟伅绠$悊GC
          */
         class message_manage_gc {
         public:
             /**
-             * @brief 构造
+             * @brief 鏋勯€�
              * @param data
              * @param msg_id
              */
@@ -597,17 +604,17 @@ namespace robotics::v3 {
                 wait_ = message_list_.add(msg_id_);
             }
             /**
-             * @brief 析构
+             * @brief 鏋愭瀯
              */
             ~message_manage_gc() {
                 message_list_.remove(msg_id_);
             }
-            //添加
+            //娣诲姞
             bool loop(int millisecond) {
                 return wait_->wait(millisecond);
             }
             /**
-             * @brief 设置数据
+             * @brief 璁剧疆鏁版嵁
              * @return
              */
             nexus_net_message& get_data() {
@@ -620,24 +627,21 @@ namespace robotics::v3 {
         };
     public:
         /**
-         * @brief 构造
+         * @brief 鍗曚緥
+         * @return 
          */
-        nexus_net_client() :
-            v3::thread_pool<nexus_net_client>(5) {
-            client_.connect_event.bind(&nexus_net_client::on_connect, this);
-            client_.disconnect_event.bind(&nexus_net_client::on_disconnect, this);
-            client_.recv_event.bind(&nexus_net_client::do_work_nexus_net_message, this);
-            timer_.timeout_event.bind(&nexus_net_client::on_timeout, this);
-            timer_.start(5000);
+        static nexus_net_client* instance() {
+            static nexus_net_client g_nexus_net_client;
+            return &g_nexus_net_client;
         }
         /**
-         * @brief 析构
+         * @brief 鏋愭瀯
          */
         ~nexus_net_client() {
             stop();
         }
         /**
-         * @brief 启动
+         * @brief 鍚�姩
          */
         void start() {
             auto config = nexus_net_config::read();
@@ -646,46 +650,47 @@ namespace robotics::v3 {
             }
         }
         /**
-         * @brief 停止
+         * @brief 鍋滄�
          */
         void stop() {
             client_.stop();
         }
         /**
-         * @brief 获取父节点名称
-         * @return 
-         */
-        std::string get_parent_name() {
-            return parent_name_;
-        }
-        /**
-         * @brief 订阅
+         * @brief 娣诲姞璁㈤槄
          * @tparam _Fn 
          * @tparam _This 
-         * @param route 
+         * @param key 
          * @param fn 
          * @param ths 
+         * @return 
          */
         template<typename _Fn, typename _This>
-        void subscribe(std::string const& route, _Fn&& fn, _This&& ths) {
-            functions_.bind(route, std::forward<_Fn>(fn), std::forward<_This>(ths));
-            nexus_net_message msg;
-            msg.msg_type = (std::uint8_t)nexus_net_msg_type_enum::SUBSCRIBE;
-            msg.route = route;
-            subscribes_.push_back(msg);
+        bool subscribe(std::string const& key, _Fn&& fn, _This&& ths) {
+            if (nexus_net_config::read().subscribes.contains(key)) {
+                functions_.bind(nexus_net_config::read().subscribes[key].route, std::forward<_Fn>(fn), std::forward<_This>(ths));
+                nexus_net_message msg;
+                msg.msg_type = (std::uint8_t)nexus_net_msg_type_enum::SUBSCRIBE;
+                msg.route = nexus_net_config::read().subscribes[key].route;
+                subscribes_.push_back(msg);
+                return true;
+            }
+            return false;
         }
         /**
-         * @brief 发布
+         * @brief 鍙戝竷
          * @tparam _Ret 
          * @tparam ..._Args 
-         * @param route 
+         * @param key 
          * @param ...args 
          * @return 
          */
         template<typename _Ret, typename ..._Args>
-        _Ret publisher(std::string const& route, _Args&&...args) {
+        _Ret publisher(std::string const& key, _Args&&...args) {
+            auto& config = nexus_net_config::read();
+            if(!config.publishers.contains(key))
+                throw std::runtime_error("key涓嶅瓨鍦�!");
             nexus_net_message msg;
-            msg.route = route;
+            msg.route = config.publishers[key].route;
             msg.msg_type = (std::uint8_t)nexus_net_msg_type_enum::PUBLISHER;
             msg.args << std::make_tuple(std::forward<_Args>(args)...);
             if constexpr (!std::is_same<void, _Ret>::value) {
@@ -693,7 +698,7 @@ namespace robotics::v3 {
                 message_manage_gc gc(message_manage_, msg.msg_id);
                 send(msg);
                 if (!gc.loop(2000))
-                    throw std::exception("请求超时");
+                    throw std::runtime_error("璇锋眰瓒呮椂");
                 _Ret result;
                 gc.get_data().args >> result;
                 return result;
@@ -703,7 +708,7 @@ namespace robotics::v3 {
             }
         }
         /**
-         * @brief 发布
+         * @brief 鍙戝竷
          * @tparam _Ret 
          * @tparam ..._Args 
          * @param timeout 
@@ -712,9 +717,12 @@ namespace robotics::v3 {
          * @return 
          */
         template<typename _Ret, typename ..._Args>
-        _Ret publisher(int timeout, std::string const& route, _Args&&...args) {
+        _Ret publisher(int timeout, std::string const& key, _Args&&...args) {
+            auto& config = nexus_net_config::read();
+            if (!config.publishers.contains(key))
+                throw std::runtime_error("key涓嶅瓨鍦�!");
             nexus_net_message msg;
-            msg.route = route;
+            msg.route = config.publishers[key].route;
             msg.msg_type = (std::uint8_t)nexus_net_msg_type_enum::PUBLISHER;
             msg.args << std::make_tuple(std::forward<_Args>(args)...);
             if constexpr (!std::is_same<void, _Ret>::value) {
@@ -722,7 +730,7 @@ namespace robotics::v3 {
                 message_manage_gc gc(message_manage_, msg.msg_id);
                 send(msg);
                 if (!gc.loop(timeout))
-                    throw std::exception("请求超时");
+                    throw std::runtime_error("璇锋眰瓒呮椂");
                 _Ret result;
                 gc.get_data().args >> result;
                 return result;
@@ -732,44 +740,39 @@ namespace robotics::v3 {
             }
         }
         /**
-         * @brief 获取节点信息
-         * @param route 
-         * @param timeout 
-         * @return 
+         * @brief 鎺堟潈鐘舵€�
          */
-        std::pair<nexus_net_response_details_info, std::string> details(std::string const& route, int timeout = 2000) {
-            std::pair<nexus_net_response_details_info, std::string> result;
-            nexus_net_message msg;
-            msg.route = route;
-            msg.msg_type = (std::uint8_t)nexus_net_msg_type_enum::DETAILS;
-            msg.msg_id = v3::utils::uuid();
-            message_manage_gc gc(message_manage_, msg.msg_id);
-            send(msg);
-            if (!gc.loop(timeout))
-                throw std::exception("请求超时");
-            gc.get_data().args >> result.first;
-            result.second = gc.get_data().route;
-            return result;
-        }
+        v3::delegates<bool,std::string const&, std::string const&> authorized_event;
         /**
-         * @brief 授权状态
+         * @brief 杩炴帴鏂�紑
          */
-        v3::delegates<bool> authorized_event;
+        v3::delegates<>                                             disconnect_event;
         /**
-         * @brief 连接断开
+         * @brief 杩炴帴鐘舵€�
          */
-        v3::delegates<> disconnect_event;
+        v3::delegates<bool>                                         connect_event;
+    private:
         /**
-         * @brief 连接状态
+         * @brief 鏋勯€�
+         */
+        nexus_net_client() :
+            v3::thread_pool<nexus_net_client>(5) {
+            client_.connect_event.bind(&nexus_net_client::on_connect, this);
+            client_.disconnect_event.bind(&nexus_net_client::on_disconnect, this);
+            client_.recv_event.bind(&nexus_net_client::do_work_nexus_net_message, this);
+            timer_.timeout_event.bind(&nexus_net_client::on_timeout, this);
+            timer_.start(5000);
+        }
+        /**
+         * @brief 鍙戦€�
+         * @param data 
          */
-        v3::delegates<bool> connect_event;
-    private:
         void send(nexus_net_message data) {
             if (data.msg_type != (std::uint8_t)nexus_net_msg_type_enum::REPPUBLISHER) {
                 auto config = nexus_net_config::read();
-                data.routes.push_back(config.user.name);
+                data.routes.push_back(config.user.code);
                 if (data.msg_type == (std::uint8_t)nexus_net_msg_type_enum::SUBSCRIBE) {
-                    data.route = fmt::format("{}/{}/{}", parent_name_, config.user.name, data.route);
+                    data.route = fmt::format("{}/{}/{}", parent_code_, config.user.code, data.route);
                 }
             }
             else if (data.msg_type == (std::uint8_t)nexus_net_msg_type_enum::REPPUBLISHER && !data.routes.empty()) {
@@ -779,19 +782,19 @@ namespace robotics::v3 {
                 nexus_net_config::logger(!asio2::get_last_error(), false, data); });
         }
         /**
-         * @brief 身份认证
+         * @brief 韬�唤璁よ瘉
          */
         void authenticate() {
             auto config = nexus_net_config::read();
             if (config.user.enable) {
                 nexus_net_message data;
                 data.msg_type = (std::uint8_t)nexus_net_msg_type_enum::AUTHENTICATE;
-                data.args << config.user.name << config.user.password;
+                data.args << config.user.code << config.user.password << config.user.name << 1;
                 send(data);
             }
         }
         /**
-         * @brief 心跳
+         * @brief 蹇冭烦
          */
         void heartbeat() {
             if (!is_heartbeat_)
@@ -801,7 +804,7 @@ namespace robotics::v3 {
             send(msg);
         }
         /**
-         * @brief 订阅
+         * @brief 璁㈤槄
          */
         void subscribe() {
             for (auto& it : subscribes_) {
@@ -813,25 +816,26 @@ namespace robotics::v3 {
             if (success) {
                 connect_event(true);
                 authenticate();
-                LOG_INFO << "连接成功!";
             }
             else {
                 connect_event(false);
-                LOG_ERROR << "连接失败!";
             }
         }
         void on_disconnect() {
             disconnect_event();
             is_heartbeat_ = false;
-            LOG_INFO << "连接断开!";
         }
         void do_work_nexus_net_message(nexus_net_message data) {
             nexus_net_config::logger(true, true, data);
-            switch ((nexus_net_msg_type_enum)data.msg_type) {
-            case nexus_net_msg_type_enum::REPAUTHENTICATE:  on_authenticate(data);  break;
-            case nexus_net_msg_type_enum::PUBLISHER:        on_publisher(data);     break;
-            case nexus_net_msg_type_enum::REPPUBLISHER:     on_reppublisher(data);  break;
-            case nexus_net_msg_type_enum::REPDETAILS:       on_repdetails(data);    break;
+            try {
+                switch ((nexus_net_msg_type_enum)data.msg_type) {
+                case nexus_net_msg_type_enum::REPAUTHENTICATE:  on_authenticate(data);  break;
+                case nexus_net_msg_type_enum::PUBLISHER:        on_publisher(data);     break;
+                case nexus_net_msg_type_enum::REPPUBLISHER:     on_reppublisher(data);  break;
+                }
+            }
+            catch (std::exception const& ec) {
+                LOG_ERROR << ec;
             }
         }
         void on_timeout() {
@@ -839,20 +843,18 @@ namespace robotics::v3 {
         }
     private:
         /**
-         * @brief 授权响应
+         * @brief 鎺堟潈鍝嶅簲
          * @param data
          */
         void on_authenticate(nexus_net_message data) {
             try {
-                data.args >> is_heartbeat_ >> parent_name_;
+                data.args >> is_heartbeat_ >> parent_code_ >> parent_name_;
                 if (is_heartbeat_) {
-                    authorized_event(true);
+                    authorized_event(true, parent_code_, parent_name_);
                     subscribe();
-                    LOG_INFO << "身份验证成功!";
                 }
                 else {
-                    authorized_event(false);
-                    LOG_ERROR << "身份验证失败!";
+                    authorized_event(false, "", "");
                 }
             }
             catch (std::exception const& ec) {
@@ -860,7 +862,7 @@ namespace robotics::v3 {
             }
         }
         /**
-         * @brief 发布
+         * @brief 鍙戝竷
          * @param data
          */
         void on_publisher(nexus_net_message data) {
@@ -876,7 +878,7 @@ namespace robotics::v3 {
             }
         }
         /**
-         * @brief 发布响应
+         * @brief 鍙戝竷鍝嶅簲
          * @param data
          */
         void on_reppublisher(nexus_net_message data) {
@@ -884,24 +886,15 @@ namespace robotics::v3 {
                 message_manage_.set_data(data.msg_id, data);
             }
         }
-        /**
-         * @brief 详情响应
-         * @param data
-         */
-        void on_repdetails(nexus_net_message data) {
-            if (message_manage_.find(data.msg_id)) {
-                message_manage_.set_data(data.msg_id, data);
-            }
-        }
     private:
-        v3::timer timer_;
-        std::thread work_thread_;
-        std::string parent_name_;
-        bool is_heartbeat_ = false;
-        nexus_net_tcp_client client_;
-        message_manage message_manage_;
-        std::vector<nexus_net_message> subscribes_;
-        v3::archive::function_manage<std::string> functions_;
+        v3::timer                                   timer_;
+        std::string                                 parent_code_;
+        std::string                                 parent_name_;
+        bool                                        is_heartbeat_ = false;
+        nexus_net_tcp_client                        client_;
+        message_manage                              message_manage_;
+        std::vector<nexus_net_message>              subscribes_;
+        v3::archive::function_manage<std::string>   functions_;
     };
 
 }

+ 0 - 2
robot/robotics/utils.hpp

@@ -46,8 +46,6 @@
 #define SET_CONSOLE_UTF8
 #endif
 
-
-
 namespace robotics::v3 {
 	namespace utils {
 		template<typename _Type>