interactive_world_model_fetcher.cpp
Go to the documentation of this file.
00001 #include <interactive_world_tools/interactive_world_model_fetcher.hpp>
00002 #include <curl/curl.h>
00003 
00004 using namespace std;
00005 
00006 interactive_world_model_fetcher::interactive_world_model_fetcher() : private_("~")
00007 {
00008   // grab params
00009   private_.param("host", host_, string(DEFAULT_RMS_HOST));
00010 
00011   load_models_ = private_.advertiseService("load_models", &interactive_world_model_fetcher::load_models_cb, this);
00012 
00013   ROS_INFO("Interactive World Model Fetcher Initialized");
00014 }
00015 
00016 bool interactive_world_model_fetcher::load_models_cb(interactive_world_msgs::LoadModels::Request &req, interactive_world_msgs::LoadModels::Response &resp)
00017 {
00018   string buffer;
00019   bool success = true;
00020 
00021   // make a request via cURL
00022   CURL *curl = curl_easy_init();
00023   // create the URL
00024   stringstream ss;
00025   ss << "http://" << host_ << "/iwmodels/view/" << req.condition_id;
00026   curl_easy_setopt(curl, CURLOPT_URL, ss.str().c_str());
00027   // follow redirects
00028   curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
00029   // set up the read
00030   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_cb);
00031   curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
00032 
00033   // perform the request
00034   CURLcode result = curl_easy_perform(curl);
00035   if (result != CURLE_OK)
00036   {
00037     ROS_ERROR("Interactive World Model Fetch Failed: %s", curl_easy_strerror(result));
00038     success = false;
00039   } else
00040   {
00041     // check if we had a valid response
00042     long code;
00043     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
00044     if (code != 200)
00045     {
00046       ROS_ERROR("Interactive World Model Fetch Failed (Return Code %i)", (int) code);
00047       success = false;
00048     } else
00049     {
00050       // parse the JSON to get the to other JSON value
00051       Json::Value json1;
00052       Json::Reader reader1;
00053       reader1.parse(buffer, json1, false);
00054       Json::Value entity = json1["Iwmodel"];
00055       string value = entity["value"].asString();
00056 
00057       Json::Value json2;
00058       Json::Reader reader2;
00059       reader2.parse(value, json2, false);
00060 
00061       // convert to a message
00062       Json::Value models = json2["models"];
00063       for (int i = 0; i < models.size(); i++)
00064       {
00065         // parse the model
00066         Json::Value cur_model = models[i];
00067         interactive_world_msgs::Model cur_model_msg;
00068         cur_model_msg.decision_value = cur_model["decision_value"].asDouble();
00069         cur_model_msg.sigma_x = cur_model["sigma_x"].asDouble();
00070         cur_model_msg.sigma_y = cur_model["sigma_y"].asDouble();
00071         cur_model_msg.sigma_z = cur_model["sigma_z"].asDouble();
00072         cur_model_msg.sigma_theta = cur_model["sigma_theta"].asDouble();
00073 
00074         // parse the placement
00075         Json::Value placement = cur_model["placement"];
00076         cur_model_msg.placement.rotation = placement["rotation"].asDouble();
00077         cur_model_msg.placement.reference_frame_id = placement["reference_frame_id"].asString();
00078         cur_model_msg.placement.position = parse_point(placement["position"]);
00079 
00080         // parse the item
00081         Json::Value item = placement["item"];
00082         cur_model_msg.placement.item.name = item["name"].asString();
00083         cur_model_msg.placement.item.width = item["width"].asDouble();
00084         cur_model_msg.placement.item.height = item["height"].asDouble();
00085 
00086         // parse the room
00087         Json::Value room = placement["room"];
00088         cur_model_msg.placement.room.name = room["name"].asString();
00089         cur_model_msg.placement.room.width = room["width"].asDouble();
00090         cur_model_msg.placement.room.height = room["height"].asDouble();
00091         cur_model_msg.placement.room.pose = parse_pose(room["pose"]);
00092 
00093         // parse the room surfaces
00094         Json::Value room_surfaces = room["surfaces"];
00095         for (int j = 0; j < room_surfaces.size(); j++)
00096         {
00097           cur_model_msg.placement.room.surfaces.push_back(parse_surface(room_surfaces[j]));
00098         }
00099 
00100         // parse the placement surface
00101         cur_model_msg.placement.surface = parse_surface(placement["surface"]);
00102 
00103         resp.models.models.push_back(cur_model_msg);
00104       }
00105     }
00106   }
00107 
00108   curl_easy_cleanup(curl);
00109   return success;
00110 }
00111 
00112 interactive_world_msgs::Surface interactive_world_model_fetcher::parse_surface(Json::Value &json)
00113 {
00114   interactive_world_msgs::Surface surface;
00115   surface.name = json["name"].asString();
00116   surface.width = json["width"].asDouble();
00117   surface.height = json["height"].asDouble();
00118   surface.pose = parse_pose(json["pose"]);
00119 
00120   // parse POIs
00121 
00122   Json::Value pois = json["pois"];
00123   for (int i = 0; i < pois.size(); i++)
00124   {
00125     surface.pois.push_back(parse_poi(pois[i]));
00126   }
00127   return surface;
00128 }
00129 
00130 
00131 interactive_world_msgs::PointOfInterest interactive_world_model_fetcher::parse_poi(Json::Value &json)
00132 {
00133   interactive_world_msgs::PointOfInterest poi;
00134   poi.name = json["name"].asString();
00135   poi.width = json["width"].asDouble();
00136   poi.height = json["height"].asDouble();
00137   poi.pose = parse_pose(json["pose"]);
00138   return poi;
00139 }
00140 
00141 geometry_msgs::Pose interactive_world_model_fetcher::parse_pose(Json::Value &json)
00142 {
00143   geometry_msgs::Pose pose;
00144   pose.position = parse_point(json["position"]);
00145   Json::Value orientation = json["orientation"];
00146   pose.orientation.x = orientation["x"].asDouble();
00147   pose.orientation.y = orientation["y"].asDouble();
00148   pose.orientation.z = orientation["z"].asDouble();
00149   pose.orientation.w = orientation["w"].asDouble();
00150   return pose;
00151 }
00152 
00153 geometry_msgs::Point interactive_world_model_fetcher::parse_point(Json::Value &json)
00154 {
00155   geometry_msgs::Point point;
00156   point.x = json["x"].asDouble();
00157   point.y = json["y"].asDouble();
00158   point.z = json["z"].asDouble();
00159   return point;
00160 }
00161 
00162 static size_t curl_write_cb(void *contents, size_t size, size_t nmemb, void *userp)
00163 {
00164   // store the result in the buffer
00165   ((string *) userp)->append((char *) contents, size * nmemb);
00166   return size * nmemb;
00167 }
00168 
00169 int main(int argc, char **argv)
00170 {
00171   // initialize ROS and the node
00172   ros::init(argc, argv, "interactive_world_model_fetcher");
00173   interactive_world_model_fetcher fetcher;
00174   ros::spin();
00175 }


interactive_world_tools
Author(s): Russell Toris
autogenerated on Thu Jun 6 2019 21:34:15