nscapi::settings_proxy::string_list nscapi::settings_proxy::get_keys(std::string path) {
	nscapi::settings_proxy::string_list ret;
	Plugin::SettingsRequestMessage request;
	nscapi::protobuf::functions::create_simple_header(request.mutable_header());
	Plugin::SettingsRequestMessage::Request *payload = request.add_payload();
	payload->set_plugin_id(plugin_id_);
	Plugin::SettingsRequestMessage::Request::Query *item = payload->mutable_query();
	item->mutable_node()->set_path(path);
	item->set_type(Plugin::Common_DataType_LIST);
	item->set_recursive(false);

	std::string response_string;
	core_->settings_query(request.SerializeAsString(), response_string);
	Plugin::SettingsResponseMessage response;
	response.ParseFromString(response_string);


	if (response.payload_size() != 1 || !response.payload(0).has_query()) {
		return ret;
	}

	const ::Plugin::Common_AnyDataType value = response.payload(0).query().value();

	for (int i=0;i<value.list_data_size();++i) {
		ret.push_back(value.list_data(i));
	}
	return ret;
}
Exemple #2
0
NSCAPI::errorReturn NSCAPIProtobuf2Json(const char* object, const char* request_buffer, unsigned int request_buffer_len, char ** response_buffer, unsigned int *response_buffer_len) {
	std::string request(request_buffer, request_buffer_len), response, obj(object);
	try {
		json_spirit::Object root;
		if (obj == "SettingsResponseMessage") {
			Plugin::SettingsResponseMessage message;
			message.ParseFromString(request);
			root = json_pb::Plugin::SettingsResponseMessage::to_json(message);
		} else if (obj == "RegistryResponseMessage") {
			Plugin::RegistryResponseMessage message;
			message.ParseFromString(request);
			root = json_pb::Plugin::RegistryResponseMessage::to_json(message);
		} else if (obj == "QueryResponseMessage") {
			Plugin::QueryResponseMessage message;
			message.ParseFromString(request);
			root = json_pb::Plugin::QueryResponseMessage::to_json(message);
		} else {
			LOG_ERROR_STD("Invalid type: " + obj);
			return NSCAPI::hasFailed;
		}
		std::string response = json_spirit::write(root);
		*response_buffer_len = static_cast<unsigned int>(response.size());
		if (response.empty())
			*response_buffer = NULL;
		else {
			*response_buffer = new char[*response_buffer_len + 10];
			memcpy(*response_buffer, response.c_str(), *response_buffer_len);
		}
	} catch (const json_spirit::ParseError &e) {
		LOG_ERROR_STD("Failed to parse JSON: " + e.reason_);
		return NSCAPI::hasFailed;
	}
	return NSCAPI::isSuccess;
}
void nscapi::settings_proxy::set_string(std::string path, std::string key, std::string value) {
	Plugin::SettingsRequestMessage request;
	nscapi::protobuf::functions::create_simple_header(request.mutable_header());
	Plugin::SettingsRequestMessage::Request *payload = request.add_payload();
	payload->set_plugin_id(plugin_id_);
	Plugin::SettingsRequestMessage::Request::Update *item = payload->mutable_update();
	item->mutable_node()->set_key(key);
	item->mutable_node()->set_path(path);
	item->mutable_value()->set_string_data(value);

	std::string response_string;
	core_->settings_query(request.SerializeAsString(), response_string);
	Plugin::SettingsResponseMessage response;
	response.ParseFromString(response_string);
	report_errors(response, core_, "update " + path + "." + key);
}
void nscapi::settings_proxy::save(const std::string context) {
	Plugin::SettingsRequestMessage request;
	nscapi::protobuf::functions::create_simple_header(request.mutable_header());
	Plugin::SettingsRequestMessage::Request *payload = request.add_payload();
	payload->set_plugin_id(plugin_id_);
	Plugin::SettingsRequestMessage::Request::Control *item = payload->mutable_control();
	item->set_command(Plugin::Settings_Command_SAVE);
	if (!context.empty()) {
		item->set_context(context);
	}

	std::string response_string;
	core_->settings_query(request.SerializeAsString(), response_string);
	Plugin::SettingsResponseMessage response;
	response.ParseFromString(response_string);
	report_errors(response, core_, "save " + context);
}
void nscapi::settings_proxy::register_path(std::string path, std::string title, std::string description, bool advanced, bool sample) {
	Plugin::SettingsRequestMessage request;
	nscapi::protobuf::functions::create_simple_header(request.mutable_header());
	Plugin::SettingsRequestMessage::Request *payload = request.add_payload();
	payload->set_plugin_id(plugin_id_);
	Plugin::SettingsRequestMessage::Request::Registration *regitem = payload->mutable_registration();
	regitem->mutable_node()->set_path(path);
	regitem->mutable_info()->set_title(title);
	regitem->mutable_info()->set_description(description);
	regitem->mutable_info()->set_advanced(advanced);
	regitem->mutable_info()->set_sample(advanced);
	std::string response_string;
	core_->settings_query(request.SerializeAsString(), response_string);
	Plugin::SettingsResponseMessage response;
	if (!response.ParseFromString(response_string)) {
		core_->log(NSCAPI::log_level::error, __FILE__, __LINE__, "Failed to de-serialize the payload for " + path);
	}
	report_errors(response, core_, "register" + path);
}
void nscapi::settings_proxy::register_key(std::string path, std::string key, int type, std::string title, std::string description, std::string defValue, bool advanced, bool sample) {
	Plugin::SettingsRequestMessage request;
	nscapi::protobuf::functions::create_simple_header(request.mutable_header());
	Plugin::SettingsRequestMessage::Request *payload = request.add_payload();
	payload->set_plugin_id(plugin_id_);
	Plugin::SettingsRequestMessage::Request::Registration *regitem = payload->mutable_registration();
	regitem->mutable_node()->set_key(key);
	regitem->mutable_node()->set_path(path);
	regitem->mutable_info()->set_title(title);
	regitem->mutable_info()->set_description(description);
	regitem->mutable_info()->mutable_default_value()->set_string_data(defValue);
	regitem->mutable_info()->set_advanced(advanced);
	regitem->mutable_info()->set_sample(sample);
	std::string response_string;
	core_->settings_query(request.SerializeAsString(), response_string);
	Plugin::SettingsResponseMessage response;
	response.ParseFromString(response_string);
	report_errors(response, core_, "register" + path + "." + key);
}
int nscapi::settings_proxy::get_int(std::string path, std::string key, int def) {
	Plugin::SettingsRequestMessage request;
	nscapi::protobuf::functions::create_simple_header(request.mutable_header());
	Plugin::SettingsRequestMessage::Request *payload = request.add_payload();
	payload->set_plugin_id(plugin_id_);
	Plugin::SettingsRequestMessage::Request::Query *item = payload->mutable_query();
	item->mutable_node()->set_key(key);
	item->mutable_node()->set_path(path);
	item->set_type(Plugin::Common_DataType_INT);
	item->set_recursive(false);
	item->mutable_default_value()->set_int_data(def);

	std::string response_string;
	core_->settings_query(request.SerializeAsString(), response_string);
	Plugin::SettingsResponseMessage response;
	response.ParseFromString(response_string);
	if (response.payload_size() != 1 || !response.payload(0).has_query()) {
		return def;
	}
	return response.payload(0).query().value().int_data();
}