赞
踩
- #include<ros/ros.h>
- #include<std_msgs/String.h>
- #include<iostream>
- #include<sstream>
- #include<jsoncpp/json/json.h>
- #include<curl/curl.h>
- #include<string>
- #include<exception>
-
- using namespace std;
-
- int flag = 0;
- string result;
-
- int writer(char *data, size_t size, size_t nmemb, string *writerData)
- {
- unsigned long sizes = size * nmemb;
- if (writerData == NULL)
- return -1;
-
- writerData->append(data, sizes);
-
- return sizes;
- }
-
- int parseJsonResonse(string input)
- {
- Json::Value root;
- Json::Reader reader;
- bool parsingSuccessful = reader.parse(input, root);
- if(!parsingSuccessful)
- {
- std::cout<<"!!! Failed to parse the response data"<< std::endl;
- return -1;
- }
- const Json::Value code = root["code"];
- const Json::Value text = root["text"];
- result = text.asString();
- flag = 1;
-
- std::cout<< "response code:" << code << std::endl;
- std::cout<< "response text:" << result << std::endl;
-
- return 0;
- }
-
- int HttpPostRequest(string input)
- {
- string buffer;
-
- std::string strJson = "{";
- strJson += "\"key\" : \"093f05b003c643bbae919b353c3a12ff\","; //双引号前加/防转仪
- strJson += "\"info\" : ";
- strJson += "\"";
- strJson += input;
- strJson += "\"";
- strJson += "}";
-
- std::cout<<"post json string: " << strJson << std::endl;
-
- try
- {
- CURL *pCurl = NULL;
- CURLcode res;
- // In windows, this will init the winsock stuff
- curl_global_init(CURL_GLOBAL_ALL);
-
- // get a curl handle
- pCurl = curl_easy_init();
- if (NULL != pCurl)
- {
- // 设置超时时间为10秒
- curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 10);
-
- // First set the URL that is about to receive our POST.
- // This URL can just as well be a
- // https:// URL if that is what should receive the data.
- curl_easy_setopt(pCurl, CURLOPT_URL, "http://openapi.tuling123.com/openapi/api");
- //curl_easy_setopt(pCurl, CURLOPT_URL, "http://192.168.0.2/posttest.cgi");
-
- // 设置http发送的内容类型为JSON
- curl_slist *plist = curl_slist_append(NULL,"Content-Type:application/json;charset=UTF-8");
- curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, plist);
-
- // 设置要POST的JSON数据
- curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, strJson.c_str());
-
- curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, writer);
-
- curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &buffer);
-
- // Perform the request, res will get the return code
- res = curl_easy_perform(pCurl);
- // Check for errors
- if (res != CURLE_OK)
- {
- printf("curl_easy_perform() failed:%s\n", curl_easy_strerror(res));
- }
- // always cleanup
- curl_easy_cleanup(pCurl);
- }
- curl_global_cleanup();
- }
- catch (std::exception &ex)
- {
- printf("curl exception %s.\n", ex.what());
- }
- if(buffer.empty())
- {
- std::cout<< "!!! ERROR The Tuling sever response NULL" << std::endl;
- }
- else
- {
- parseJsonResonse(buffer);
- }
-
- return 0;
-
- }
- void arvCallBack(const std_msgs::String::ConstPtr &msg)
- {
- std::cout<<"your quesion is: " << msg->data << std::endl;
- HttpPostRequest(msg->data);
- }
-
- int main(int argc, char **argv)
- {
- ros::init(argc, argv,"tuling_nlu_node");
- ros::NodeHandle nd;
-
- ros::Subscriber sub = nd.subscribe("voice/tuling_nlu_topic", 10, arvCallBack);
- ros::Publisher pub = nd.advertise<std_msgs::String>("/voice/xf_tts_topic", 10);
- ros::Rate loop_rate(10);
-
- while(ros::ok())
- {
- if(flag)
- {
- std_msgs::String msg;
- msg.data = result;
- pub.publish(msg);
- flag = 0;
- }
- ros::spinOnce();
- loop_rate.sleep();
- }
-
-
- }

- add_executable(tuling_nlu_node src/tuling_nlu.cpp)
- target_link_libraries(tuling_nlu_node ${catkin_LIBRARIES} -lcurl -ljsoncpp)
- cmake_minimum_required(VERSION 2.8.3)
- project(voice_system)
-
-
- find_package(catkin REQUIRED COMPONENTS
- roscpp
- rospy
- std_msgs
- )
-
-
- include_directories(
- include
- ${catkin_INCLUDE_DIRS}
-
- )
-
-
- add_executable(xf_tts_node src/xf_tts.cpp)
- target_link_libraries(xf_tts_node ${catkin_LIBRARIES} -lmsc -lrt -ldl -lpthread)
- add_executable(tuling_nlu_node src/tuling_nlu.cpp)
- target_link_libraries(tuling_nlu_node ${catkin_LIBRARIES} -lcurl -ljsoncpp)

该文章主要是对自己学习的一些总结,方便以后学习,也对学习该方面的人提供一些帮助,如有问题请指出。
同时该文章也借鉴了ros小课堂的一些内容。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。