mirror of
https://github.com/paulscherrerinstitute/sf_daq_buffer.git
synced 2026-04-23 11:50:44 +02:00
Create start endpoint
This commit is contained in:
+49
-88
@@ -57,101 +57,62 @@ void RestApi::start_rest_api(WriterManager& writer_manager, uint16_t port)
|
||||
return result;
|
||||
});
|
||||
|
||||
CROW_ROUTE (app, "/parameters").methods("GET"_method, "POST"_method) ([&](const crow::request& req){
|
||||
crow::json::wvalue result;
|
||||
CROW_ROUTE (app, "/start").methods("POST"_method) ([&](const crow::request& req){
|
||||
auto parameters_type = writer_manager.get_parameters_type();
|
||||
|
||||
if (req.method == "GET"_method) {
|
||||
auto request_parameters = crow::json::load(req.body);
|
||||
std::unordered_map<std::string, boost::any> parameters;
|
||||
|
||||
for (const auto& item : writer_manager.get_parameters()) {
|
||||
auto parameter_name = item.first;
|
||||
auto parameter_value = item.second;
|
||||
|
||||
try {
|
||||
auto parameter_type = parameters_type.at(parameter_name);
|
||||
|
||||
if (parameter_type == NX_FLOAT || parameter_type == NX_NUMBER) {
|
||||
result[parameter_name] = boost::any_cast<double>(parameter_value);
|
||||
|
||||
} else if (parameter_type == NX_CHAR || parameter_type == NXnote || parameter_type == NX_DATE_TIME) {
|
||||
result[parameter_name] = boost::any_cast<string>(parameter_value);
|
||||
|
||||
} else if (parameter_type == NX_INT) {
|
||||
result[parameter_name] = boost::any_cast<int>(parameter_value);
|
||||
}
|
||||
|
||||
} catch (const boost::bad_any_cast& exception) {
|
||||
stringstream error_message;
|
||||
using namespace date;
|
||||
error_message << "[" << std::chrono::system_clock::now() << "]";
|
||||
error_message << "[RestApi::parameters(get)] Cannot cast parameter " << parameter_name << " into specified type." << endl;
|
||||
|
||||
throw runtime_error(error_message.str());
|
||||
|
||||
} catch (const out_of_range& exception){
|
||||
stringstream error_message;
|
||||
using namespace date;
|
||||
error_message << "[" << std::chrono::system_clock::now() << "]";
|
||||
error_message << "[RestApi::parameters(get)] No type mapping for parameter " << parameter_name << " in file format."<< endl;
|
||||
|
||||
throw runtime_error(error_message.str());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
auto request_parameters = crow::json::load(req.body);
|
||||
std::unordered_map<std::string, boost::any> new_parameters;
|
||||
|
||||
for (const auto& item : request_parameters) {
|
||||
string parameter_name = item.key();
|
||||
|
||||
try{
|
||||
auto parameter_type = parameters_type.at(parameter_name);
|
||||
|
||||
if (parameter_type == NX_FLOAT || parameter_type == NX_NUMBER) {
|
||||
new_parameters[parameter_name] = double(item.d());
|
||||
} else if (parameter_type == NX_INT) {
|
||||
new_parameters[parameter_name] = int(item.i());
|
||||
} else if (parameter_type == NX_CHAR) {
|
||||
new_parameters[parameter_name] = string(item.s());
|
||||
} else if (parameter_type == NX_DATE_TIME) {
|
||||
new_parameters[parameter_name] = string(item.s());
|
||||
} else {
|
||||
stringstream error_message;
|
||||
using namespace date;
|
||||
error_message << "[" << std::chrono::system_clock::now() << "]";
|
||||
error_message << "[RestApi::parameters(post)] No NX type mapping for parameter " << parameter_name << endl;
|
||||
|
||||
throw runtime_error(error_message.str());
|
||||
}
|
||||
|
||||
} catch (const out_of_range& exception){
|
||||
stringstream error_message;
|
||||
using namespace date;
|
||||
error_message << "[" << std::chrono::system_clock::now() << "]";
|
||||
error_message << "[RestApi::parameters(post)] No type mapping for received parameter " << parameter_name << " in file format."<< endl;
|
||||
|
||||
throw runtime_error(error_message.str());
|
||||
|
||||
} catch (const boost::bad_any_cast& exception) {
|
||||
stringstream error_message;
|
||||
using namespace date;
|
||||
error_message << "[" << std::chrono::system_clock::now() << "]";
|
||||
error_message << "[RestApi::parameters(post)] Cannot cast parameter " << parameter_name << " into specified type." << endl;
|
||||
|
||||
throw runtime_error(error_message.str());
|
||||
|
||||
}
|
||||
}
|
||||
for (const auto& item : request_parameters) {
|
||||
string parameter_name = item.key();
|
||||
|
||||
writer_manager.set_parameters(new_parameters);
|
||||
try{
|
||||
auto parameter_type = parameters_type.at(parameter_name);
|
||||
|
||||
result["message"] = "Parameters set.";
|
||||
return result;
|
||||
if (parameter_type == NX_FLOAT || parameter_type == NX_NUMBER) {
|
||||
parameters[parameter_name] = double(item.d());
|
||||
} else if (parameter_type == NX_INT) {
|
||||
parameters[parameter_name] = int(item.i());
|
||||
} else if (parameter_type == NX_CHAR) {
|
||||
parameters[parameter_name] = string(item.s());
|
||||
} else if (parameter_type == NX_DATE_TIME) {
|
||||
parameters[parameter_name] = string(item.s());
|
||||
} else {
|
||||
stringstream error_message;
|
||||
using namespace date;
|
||||
error_message << "[" << std::chrono::system_clock::now() << "]";
|
||||
error_message << "[RestApi::start(post)] No NX type mapping for parameter " << parameter_name << endl;
|
||||
|
||||
throw runtime_error(error_message.str());
|
||||
}
|
||||
|
||||
} catch (const out_of_range& exception){
|
||||
stringstream error_message;
|
||||
using namespace date;
|
||||
error_message << "[" << std::chrono::system_clock::now() << "]";
|
||||
error_message << "[RestApi::start(post)] No type mapping for received parameter " << parameter_name << " in file format."<< endl;
|
||||
|
||||
throw runtime_error(error_message.str());
|
||||
|
||||
} catch (const boost::bad_any_cast& exception) {
|
||||
stringstream error_message;
|
||||
using namespace date;
|
||||
error_message << "[" << std::chrono::system_clock::now() << "]";
|
||||
error_message << "[RestApi::start(post)] Cannot cast parameter " << parameter_name << " into specified type." << endl;
|
||||
|
||||
throw runtime_error(error_message.str());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
writer_manager.start(parameters);
|
||||
|
||||
crow::json::wvalue result;
|
||||
result["message"] = "Writer started.";
|
||||
result["status"] = writer_manager.get_status();
|
||||
return result;
|
||||
});
|
||||
|
||||
app.loglevel(crow::LogLevel::ERROR);
|
||||
app.port(port).run();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user