Go to the documentation of this file.00001 #ifndef UPLOAD_H
00002 #define UPLOAD_H
00003
00004 #include <ros/ros.h>
00005 #include <string>
00006 #include <iostream>
00007 #include <re_srvs/SetObject.h>
00008 #include <re_srvs/GetObject.h>
00009 #include <re_srvs/UpdateObjectBinaryFile.h>
00010
00011 class UploadBase {
00012 public:
00013 UploadBase() {}
00014 virtual bool waitForService() = 0;
00015 virtual bool check() = 0;
00016 virtual bool upload() = 0;
00017 };
00018
00019 template <class T>
00020 class UploadBaseHelper : public UploadBase {
00021 public:
00022 UploadBaseHelper(const std::string& topic) : UploadBase() {
00023 client = nh.serviceClient<T>(topic);
00024 get_object_client = nh.serviceClient<re_srvs::GetObject>("re_comm/get_object");
00025 }
00026
00027 virtual bool waitForService() {
00028 if (!client.exists()) {
00029 std::cout << "waiting 5s for re_comm service to start up..." << std::endl;
00030 client.waitForExistence(ros::Duration(5.0));
00031 }
00032 if (!client.exists()) {
00033 std::cerr << "re_comm service not found" << std::endl;
00034 return false;
00035 }
00036 return true;
00037 }
00038
00039 virtual bool upload() {
00040 bool servicecall = client.call(request, response);
00041 if (!servicecall) {
00042 std::cerr << "upload service call failed" << std::endl;
00043 return false;
00044 }
00045 return response.success;
00046 }
00047
00048 bool checkObjectExistence(std::string uid, bool& ok) {
00049 re_srvs::GetObjectRequest req;
00050 re_srvs::GetObjectResponse resp;
00051 req.objectUID = uid;
00052 ok = get_object_client.call(req, resp);
00053
00054 return resp.success;
00055 }
00056
00057 protected:
00058 ros::NodeHandle nh;
00059 ros::ServiceClient client, get_object_client;
00060
00061 typedef typename T::Request Request;
00062 typedef typename T::Response Response;
00063 Request request;
00064 Response response;
00065 };
00066
00067 class UploadSetNewObject : public UploadBaseHelper<re_srvs::SetObject> {
00068 public:
00069 UploadSetNewObject() : UploadBaseHelper<re_srvs::SetObject>("re_comm/set_object") {
00070 }
00071
00072 void fillRequest(std::string apikey, std::string name, std::string clss, std::string owldesc, std::string description, const std::vector<re_msgs::File>& files) {
00073 request.id = name;
00074 request.apiKey = apikey;
00075 request.cls = clss;
00076 request.description = description;
00077 request.object = owldesc;
00078 request.files = files;
00079 }
00080
00081 bool check() {
00082 bool checkOK = false;
00083 bool exists = checkObjectExistence(request.cls + "." + request.id, checkOK);
00084 if (exists) {
00085 std::cerr << "ERROR: object is already in DB" << std::endl;
00086 return false;
00087 }
00088 if (!checkOK) {
00089 std::cerr << "WARNING: could not check whether object is already in DB" << std::endl;
00090 }
00091 return true;
00092 }
00093 };
00094
00095 class UpdateBinary : public UploadBaseHelper<re_srvs::UpdateObjectBinaryFile> {
00096 public:
00097 UpdateBinary() : UploadBaseHelper<re_srvs::UpdateObjectBinaryFile>("re_comm/update_object_binary_file") {
00098 }
00099
00100 void fillRequest(std::string apikey, std::string objectUID, const re_msgs::File& fileMsg) {
00101 request.apiKey = apikey;
00102 request.file = fileMsg;
00103 request.objectUID = objectUID;
00104 }
00105
00106 bool check() {
00107 bool checkOK = false;
00108 bool exists = checkObjectExistence(request.objectUID, checkOK);
00109 if (!exists) {
00110 std::cerr << "ERROR: object is not in DB" << std::endl;
00111 return false;
00112 }
00113 if (!checkOK) {
00114 std::cerr << "WARNING: could not check whether object is already in DB" << std::endl;
00115 }
00116 return true;
00117 }
00118 };
00119
00120 #endif // UPLOAD_H