1414 * limitations under the License.
1515 */
1616
17-
18- #include < modelbox/modelbox.h>
1917#include < modelbox/base/config.h>
18+ #include < modelbox/modelbox.h>
2019
20+ #include < fstream>
21+ #include < iostream>
2122#include < nlohmann/json.hpp>
2223
2324#include " modelbox/common/flowunit_info.h"
@@ -64,6 +65,14 @@ Status FlowUnitInfo::Init(const std::shared_ptr<Configuration> &config) {
6465 return {status, " init flowunit manager failed." };
6566 }
6667
68+ if (config_->GetSubConfig (" driver" ) != nullptr ) {
69+ auto paths = config_->GetSubConfig (" driver" )->GetStrings (DRIVER_DIR);
70+ for (const auto &search_path : paths) {
71+ modelbox::ListSubDirectoryFiles (search_path, " *.toml" ,
72+ &flowunits_from_files_);
73+ }
74+ }
75+
6776 return STATUS_OK;
6877}
6978
@@ -75,10 +84,91 @@ std::shared_ptr<FlowUnitManager> FlowUnitInfo::GetFlowUnitManager() {
7584 return flowunit_;
7685}
7786
78- std::shared_ptr<Drivers> FlowUnitInfo::GetDriverManager () {
79- return drivers_;
80- }
87+ std::shared_ptr<Drivers> FlowUnitInfo::GetDriverManager () { return drivers_; }
88+
89+ Status GetInfoInFromTomlFile (const std::string &file, nlohmann::json &json) {
90+ MBLOG_DEBUG << " flowunit from file: " << file;
91+ std::string json_data;
92+ std::ifstream infile (file);
93+ if (infile.fail ()) {
94+ return {modelbox::STATUS_NOTFOUND,
95+ " Get file failed" + modelbox::StrError (errno)};
96+ }
97+ Defer { infile.close (); };
98+
99+ std::string data ((std::istreambuf_iterator<char >(infile)),
100+ std::istreambuf_iterator<char >());
101+ if (data.length () <= 0 ) {
102+ return {modelbox::STATUS_BADCONF, " toml file is invalid." };
103+ }
104+
105+ auto ret = modelbox::TomlToJson (data, &json_data);
106+ if (!ret) {
107+ MBLOG_WARN << " Get flowunit info failed. " << ret.WrapErrormsgs ();
108+ return {STATUS_BADCONF, " Get flowunit info failed." };
109+ }
81110
111+ try {
112+ auto json_flowunit = nlohmann::json::parse (json_data);
113+ // only add c++ virtual flowunit
114+ if (json_flowunit.contains (" base" ) == false ) {
115+ return {STATUS_BADCONF, " not a flowunit toml file" };
116+ }
117+
118+ if (json_flowunit[" base" ].contains (" type" ) == false ) {
119+ return {STATUS_BADCONF, " not a flowunit toml file" };
120+ }
121+
122+ if (json_flowunit[" base" ][" type" ] != " c++" ) {
123+ return {STATUS_BADCONF, " not a flowunit toml file" };
124+ }
125+
126+ nlohmann::json json_inputs = nlohmann::json::array ();
127+ nlohmann::json json_outputs = nlohmann::json::array ();
128+ nlohmann::json json_options = nlohmann::json::array ();
129+
130+ json = json_flowunit[" base" ];
131+ json[" type" ] = json_flowunit[" base" ][" device" ];
132+ json.erase (" device" );
133+ json[" group" ] = json_flowunit[" base" ][" group_type" ];
134+ json.erase (" group_type" );
135+ if (json_flowunit.contains (" input" )) {
136+ for (auto &input : json_flowunit[" input" ]) {
137+ nlohmann::json json_input;
138+ json_input[" name" ] = input[" name" ];
139+ json_input[" port_type" ] = input[" type" ];
140+ json_input[" device_type" ] = input[" device" ];
141+ json_inputs.push_back (json_input);
142+ }
143+ json[" inputports" ] = json_inputs;
144+ }
145+
146+ if (json_flowunit.contains (" output" )) {
147+ for (auto &output : json_flowunit[" output" ]) {
148+ nlohmann::json json_output;
149+ json_output[" name" ] = output[" name" ];
150+ json_output[" port_type" ] = output[" type" ];
151+ json_output[" device_type" ] = output[" device" ];
152+ json_outputs.push_back (json_output);
153+ }
154+ json[" outputports" ] = json_outputs;
155+ }
156+
157+ if (json_flowunit.contains (" options" )) {
158+ for (auto &output : json_flowunit[" options" ]) {
159+ json_outputs.push_back (output);
160+ }
161+ json[" options" ] = json_options;
162+ }
163+ } catch (const std::exception &e) {
164+ std::string errmsg = " Get flowunit info failed. " ;
165+ errmsg += e.what ();
166+ MBLOG_WARN << errmsg;
167+ return {STATUS_BADCONF, errmsg};
168+ }
169+
170+ return STATUS_OK;
171+ }
82172
83173Status FlowUnitInfo::GetInfoInJson (std::string *result) {
84174 nlohmann::json result_json;
@@ -98,12 +188,13 @@ Status FlowUnitInfo::GetInfoInJson(std::string *result) {
98188 json[" name" ] = itr_device.first ;
99189 json[" type" ] = desc->GetDeviceType ();
100190 json[" version" ] = desc->GetDeviceVersion ();
101- json[" descryption " ] = desc->GetDeviceDesc ();
191+ json[" description " ] = desc->GetDeviceDesc ();
102192 devices.push_back (json);
103193 }
104194 }
105-
195+
106196 auto flow_list = flowunit_->GetAllFlowUnitDesc ();
197+ std::map<std::string, bool > flowunit_map;
107198 for (const auto &flow : flow_list) {
108199 nlohmann::json json;
109200 nlohmann::json json_inputs = nlohmann::json::array ();
@@ -114,7 +205,7 @@ Status FlowUnitInfo::GetInfoInJson(std::string *result) {
114205 json[" name" ] = flow->GetFlowUnitName ();
115206 json[" type" ] = driverdesc->GetType ();
116207 json[" version" ] = driverdesc->GetVersion ();
117- json[" descryption " ] = flow->GetDescription ();
208+ json[" description " ] = flow->GetDescription ();
118209 json[" group" ] = [&]() -> std::string {
119210 auto type = flow->GetGroupType ();
120211 if (type.empty ()) {
@@ -164,9 +255,40 @@ Status FlowUnitInfo::GetInfoInJson(std::string *result) {
164255 }
165256 json[" options" ] = json_options;
166257
258+ std::string key = json[" name" ];
259+ key += " :" ;
260+ key += json[" type" ];
261+ key += " :" ;
262+ key += json[" version" ];
263+ flowunit_map[key] = true ;
264+
167265 flowunits.push_back (json);
168266 }
169267
268+ for (const auto &f : flowunits_from_files_) {
269+ nlohmann::json json_flowunit;
270+ auto ret = GetInfoInFromTomlFile (f, json_flowunit);
271+ if (!ret) {
272+ if (ret == STATUS_BADCONF) {
273+ continue ;
274+ }
275+
276+ MBLOG_WARN << " Get flowunit info failed. " << ret.WrapErrormsgs ();
277+ continue ;
278+ }
279+
280+ std::string key = json_flowunit[" name" ];
281+ key += " :" ;
282+ key += json_flowunit[" type" ];
283+ key += " :" ;
284+ key += json_flowunit[" version" ];
285+ if (flowunit_map.find (key) != flowunit_map.end ()) {
286+ continue ;
287+ }
288+
289+ flowunits.push_back (json_flowunit);
290+ }
291+
170292 result_json[" flowunits" ] = flowunits;
171293 result_json[" devices" ] = devices;
172294 } catch (const std::exception &e) {
0 commit comments