Exemple #1
0
void handle_execute(web::http::http_request& request) {

	json::value response;
	concurrency::streams::istream body = request.body();
	uint64_t content_lenght = request.headers().content_length();
	LOG(debug)<<"Content lenght of request "<<content_lenght;
	if (content_lenght == 0) {
		LOG(error)<<"Bad request! Empty body";
		response["error_msg"] = json::value::string("Bad request.Empty body!");
		response["status"] = json::value::string("ERROR");
		request.reply(status_codes::BadRequest, response).wait();
		return;

	}
	uint8_t * waveData = new uint8_t[content_lenght];
	Concurrency::streams::rawptr_buffer<uint8_t> buffer(waveData,
			content_lenght);
	body.read(buffer, content_lenght).get();
	WaveFile wave(waveData, content_lenght);
	delete[] waveData;
	Samples waveSamples(wave);
	jsonextend wavePropertiesJSON = g_processAndAnlyze.getSummary(waveSamples);

	std::map<jsonextend, std::string> list;
	list = g_mainDB->getAllKV();
	auto tmpFun = g_policies[DEFAULT_POLICY];

	std::string command = tmpFun(list, wavePropertiesJSON);

	if (!command.empty()) {

		web::json::value cmdResult = Runner::run(command, " ");
		response["status"] = json::value::string("OK");
		response["command"] = json::value::string(command);
		response["command_ret"] = cmdResult;
		request.reply(status_codes::OK, response).wait();
		return;
	}

	response["status"] = json::value::string("NOT_FOUND");

	request.reply(status_codes::OK, response).wait();

}
Exemple #2
0
void handle_add(web::http::http_request& request) {

	json::value response;
	std::map<std::string, std::string> querymap = uri::split_query(
			request.relative_uri().query());
	if (querymap.find("name") == querymap.end()
			|| querymap.find("command") == querymap.end()) {
		LOG(error)<<"Bad request";
		response["error_msg"] = json::value::string("Bad request. Read doc for info. Missing name or command field in request.");
		response["status"] = json::value::string("ERROR");
		request.reply(status_codes::BadRequest, response).wait();
		return;

	}
	std::string commandName = uri::decode(querymap["name"]);
	std::string commandString = uri::decode(querymap["command"]);
	LOG(debug)<<"Add command name "<<commandName<<" command string: "<<commandString;
	concurrency::streams::istream body = request.body();
	uint64_t content_lenght = request.headers().content_length();
	LOG(debug)<<"Content lenght of request "<<content_lenght;
	if (content_lenght == 0) {
		LOG(error)<<"Bad request! Empty body";
		response["error_msg"] = json::value::string("Bad request.Empty body!");
		response["status"] = json::value::string("ERROR");
		request.reply(status_codes::BadRequest, response).wait();
		return;

	}
	try {
		uint8_t * waveData = new uint8_t[content_lenght];
		Concurrency::streams::rawptr_buffer<uint8_t> buffer(waveData,
				content_lenght);
		//body.read_to_end(buffer).get();
		body.read(buffer, content_lenght).get();
		WaveFile wave(waveData, content_lenght);
		delete[] waveData;
		Samples waveSamples(wave);
		jsonextend wavepropertiesJSON = g_processAndAnlyze.getSummary(waveSamples);
		wavepropertiesJSON["name"] =  web::json::value::string(commandName);

		try {
			LOG(debug)<<"Saving "<<wavepropertiesJSON.to_string();
			g_mainDB->put(wavepropertiesJSON, commandString);
		} catch (std::exception const & ex) {
			LOG(error)<<"Error "<<ex.what();
			response["status"] = json::value::string("ERROR");
			response["error_msg"] = json::value::string(ex.what());
			request.reply(status_codes::InternalError, response).wait();
			return;
		}
		response["status"] = json::value::string("OK");
		response["command"] = json::value::string(commandString);
		request.reply(status_codes::OK, response).wait();
	} catch (std::exception &e) {

		LOG(error)<<"Error "<<e.what();
		response["status"] = json::value::string("ERROR");
		response["command"] = json::value::string(e.what());
		request.reply(status_codes::BadRequest, response).wait();
	}

}