jcsyshc пре 2 година
родитељ
комит
ca4a73c28d

+ 1 - 1
app/data/tka_ar_navigation.json

@@ -244,7 +244,7 @@
       "type": "ndi_interface",
       "name": "ndi",
       "init_config": {
-        "address_type": "serial",
+        "address_type": "ethernet",
         "ip": "10.0.0.5",
         "tcp_port": 8765,
         "com_port": "/dev/ttyUSB0",

+ 2 - 1
src/core/basic_obj_types.hpp

@@ -270,12 +270,13 @@ namespace sophiar {
     inline void raw_pointer_fill_from(ReaderType &reader, void *raw_ptr, int var_type_index) {
         LOOP_BASIC_VAR_TYPE(
                 {
+                    using ObjType = std::remove_cvref_t<decltype(*real_ptr)>;
                     if (reader.empty()) [[unlikely]] {
                         real_ptr = nullptr;
                         return;
                     }
                     if (real_ptr == nullptr) [[unlikely]] {
-                        real_ptr = real_ptr->new_instance();
+                        real_ptr = ObjType::new_instance();
                     }
                     real_ptr->fill_from(reader);
                     return;

+ 35 - 3
src/core/local_connection.cpp

@@ -15,6 +15,7 @@
 #include <atomic>
 #include <condition_variable>
 #include <coroutine>
+#include <fstream>
 #include <map>
 #include <thread>
 
@@ -85,6 +86,7 @@ namespace sophiar {
             spin_lock mu;
             callback_token_type cb_token;
             attach_token_type bind_token;
+            bool disposal = false;
         };
 
         std::map<std::string, variable_store_info> variable_pool;
@@ -219,8 +221,15 @@ namespace sophiar {
 
         std::any query_variable(const std::string &name) {
             auto store = get_variable_store(name);
-            auto lock = std::lock_guard{store->mu};
-            return store->value;
+            std::any ret;
+            {
+                auto lock = std::lock_guard{store->mu};
+                ret = store->value;
+                if (store->disposal) {
+                    store->value = {};
+                }
+            }
+            return ret;
         }
 
         void update_variable(const std::string &name, const std::any &value) {
@@ -231,12 +240,13 @@ namespace sophiar {
                 LOOP_BASIC_VAR_TYPE(
                         {
                             using ValueType = decltype(real_ptr->value);
+                            using ObjType = std::remove_cvref_t<decltype(*real_ptr)>;
                             if (!value.has_value()) [[unlikely]] {
                                 real_ptr = nullptr;
                                 return;
                             }
                             if (real_ptr == nullptr) [[unlikely]] {
-                                real_ptr = real_ptr->new_instance();
+                                real_ptr = ObjType::new_instance();
                             }
                             real_ptr->value = std::any_cast<ValueType>(value);
                             return;
@@ -244,6 +254,11 @@ namespace sophiar {
             });
         }
 
+        void mark_variable_disposal(const std::string &name) {
+            auto store = get_variable_store(name);
+            store->disposal = true;
+        }
+
     };
 
     local_connection::local_connection()
@@ -287,4 +302,21 @@ namespace sophiar {
         pimpl->update_variable(name, value);
     }
 
+    void local_connection::mark_variable_disposal(const std::string &name) {
+        pimpl->mark_variable_disposal(name);
+    }
+
+    void run_sophiar(const std::string &config_path) {
+        auto config_file = std::ifstream{config_path};
+        assert(config_file.is_open());
+        auto config = nlohmann::json::parse(config_file);
+        auto ok = initialize(config);
+        assert(ok);
+        global_context->run();
+    }
+
+    void stop_sophiar() {
+        global_context->stop();
+    }
+
 }

+ 10 - 1
src/core/local_connection.h

@@ -13,6 +13,12 @@
 
 namespace sophiar {
 
+    // block until stop
+    void run_sophiar(const std::string &config_path);
+
+    // should be called from another thread
+    void stop_sophiar();
+
     class local_connection {
     public:
 
@@ -60,7 +66,7 @@ namespace sophiar {
         void update_##type##_variable(const std::string &name, \
                                   const std::optional<type##_obj_value_type> &value) { \
             if (value.has_value()) { \
-                update_variable_impl(name, value); \
+                update_variable_impl(name, value.value()); \
             } else { \
                 update_variable_impl(name, {}); \
             } \
@@ -82,6 +88,9 @@ namespace sophiar {
 
 #undef VARIABLE_FUNC
 
+        // variable will be set to {} after query
+        void mark_variable_disposal(const std::string &name);
+
     private:
         struct impl;
         std::unique_ptr<impl> pimpl;