14
14
* limitations under the License.
15
15
*/
16
16
17
-
18
- #include < modelbox/modelbox.h>
19
17
#include < modelbox/base/config.h>
18
+ #include < modelbox/modelbox.h>
20
19
20
+ #include < fstream>
21
+ #include < iostream>
21
22
#include < nlohmann/json.hpp>
22
23
23
24
#include " modelbox/common/flowunit_info.h"
@@ -64,6 +65,14 @@ Status FlowUnitInfo::Init(const std::shared_ptr<Configuration> &config) {
64
65
return {status, " init flowunit manager failed." };
65
66
}
66
67
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
+
67
76
return STATUS_OK;
68
77
}
69
78
@@ -75,10 +84,91 @@ std::shared_ptr<FlowUnitManager> FlowUnitInfo::GetFlowUnitManager() {
75
84
return flowunit_;
76
85
}
77
86
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
+ }
81
110
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
+ }
82
172
83
173
Status FlowUnitInfo::GetInfoInJson (std::string *result) {
84
174
nlohmann::json result_json;
@@ -98,12 +188,13 @@ Status FlowUnitInfo::GetInfoInJson(std::string *result) {
98
188
json[" name" ] = itr_device.first ;
99
189
json[" type" ] = desc->GetDeviceType ();
100
190
json[" version" ] = desc->GetDeviceVersion ();
101
- json[" descryption " ] = desc->GetDeviceDesc ();
191
+ json[" description " ] = desc->GetDeviceDesc ();
102
192
devices.push_back (json);
103
193
}
104
194
}
105
-
195
+
106
196
auto flow_list = flowunit_->GetAllFlowUnitDesc ();
197
+ std::map<std::string, bool > flowunit_map;
107
198
for (const auto &flow : flow_list) {
108
199
nlohmann::json json;
109
200
nlohmann::json json_inputs = nlohmann::json::array ();
@@ -114,7 +205,7 @@ Status FlowUnitInfo::GetInfoInJson(std::string *result) {
114
205
json[" name" ] = flow->GetFlowUnitName ();
115
206
json[" type" ] = driverdesc->GetType ();
116
207
json[" version" ] = driverdesc->GetVersion ();
117
- json[" descryption " ] = flow->GetDescription ();
208
+ json[" description " ] = flow->GetDescription ();
118
209
json[" group" ] = [&]() -> std::string {
119
210
auto type = flow->GetGroupType ();
120
211
if (type.empty ()) {
@@ -164,9 +255,40 @@ Status FlowUnitInfo::GetInfoInJson(std::string *result) {
164
255
}
165
256
json[" options" ] = json_options;
166
257
258
+ std::string key = json[" name" ];
259
+ key += " :" ;
260
+ key += json[" type" ];
261
+ key += " :" ;
262
+ key += json[" version" ];
263
+ flowunit_map[key] = true ;
264
+
167
265
flowunits.push_back (json);
168
266
}
169
267
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
+
170
292
result_json[" flowunits" ] = flowunits;
171
293
result_json[" devices" ] = devices;
172
294
} catch (const std::exception &e) {
0 commit comments