ActionRecipeHandler.java
Go to the documentation of this file.
00001 /* \file ActionRecipeHandler.java
00002  * \brief Action recipe handler
00003  *
00004  * The action recipe handler provides the callbacks for ROS services related 
00005  * to action recipes.
00006  * 
00007  * This file is part of the RoboEarth ROS re_comm_core package.
00008  * 
00009  * It was originally created for <a href="http://www.roboearth.org/">RoboEarth</a>.
00010  * The research leading to these results has received funding from the 
00011  * European Union Seventh Framework Programme FP7/2007-2013 
00012  * under grant agreement no248942 RoboEarth.
00013  *
00014  * Copyright (C) 2010 by 
00015  * <a href=" mailto:perzylo@cs.tum.edu">Alexander Perzylo</a>
00016  * Technische Universitaet Muenchen
00017  * 
00018  * Redistribution and use in source and binary forms, with or without
00019  * modification, are permitted provided that the following conditions are met:
00020  * 
00021  *    <UL>
00022  *     <LI> Redistributions of source code must retain the above copyright
00023  *       notice, this list of conditions and the following disclaimer.
00024  *     <LI> Redistributions in binary form must reproduce the above copyright
00025  *       notice, this list of conditions and the following disclaimer in the
00026  *       documentation and/or other materials provided with the distribution.
00027  *     <LI> Neither the name of Willow Garage, Inc. nor the names of its
00028  *       contributors may be used to endorse or promote products derived from
00029  *       this software without specific prior written permission.
00030  *    </UL>
00031  * 
00032  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00033  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00034  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00035  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00036  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00037  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00038  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00039  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00040  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00041  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00042  * POSSIBILITY OF SUCH DAMAGE.
00043  *
00044  * \author Alexander Perzylo
00045  * \version 1.0
00046  * \date 2010
00047  * \image html http://www.roboearth.org/sites/default/files/RoboEarth.org_logo.gif
00048  * \image latex http://www.roboearth.org/sites/default/files/RoboEarth.org_logo.gif
00049  */
00050 package roboearth.wp5.module;
00051 
00052 import java.util.ArrayList;
00053 
00054 import roboearth.wp5.conn.REInterface;
00055 import ros.NodeHandle;
00056 import ros.Ros;
00057 import ros.RosException;
00058 import ros.ServiceServer;
00059 import ros.pkg.re_srvs.srv.DelActionRecipe;
00060 import ros.pkg.re_srvs.srv.GetActionRecipe;
00061 import ros.pkg.re_srvs.srv.QueryActionRecipes;
00062 import ros.pkg.re_srvs.srv.SearchActionRecipes;
00063 import ros.pkg.re_srvs.srv.SetActionRecipe;
00064 import ros.pkg.re_srvs.srv.UpdateActionRecipe;
00065 
00066 public class ActionRecipeHandler extends AbstractHandler {
00067 
00074         public ActionRecipeHandler(Ros ros, NodeHandle n) throws RosException {
00075 
00076                 super(ros, n);
00077 
00078                 this.n.advertiseService("/re_comm/get_action_recipe", new GetActionRecipe(), new GetActionRecipeCallback());
00079                 this.n.advertiseService("/re_comm/set_action_recipe", new SetActionRecipe(), new SetActionRecipeCallback());
00080                 this.n.advertiseService("/re_comm/del_action_recipe", new DelActionRecipe(), new DelActionRecipeCallback());
00081                 this.n.advertiseService("/re_comm/update_action_recipe", new UpdateActionRecipe(), new UpdateActionRecipeCallback());
00082                 this.n.advertiseService("/re_comm/search_action_recipes", new SearchActionRecipes(), new SearchActionRecipesCallback());
00083                 this.n.advertiseService("/re_comm/query_action_recipes", new QueryActionRecipes(), new QueryActionRecipesCallback());
00084 
00085                 ros.logInfo("Module 'ActionRecipeHandler' loaded.");
00086 
00087         }
00088 
00096         class GetActionRecipeCallback implements ServiceServer.Callback<GetActionRecipe.Request, GetActionRecipe.Response> {
00097 
00098                 @Override
00099                 public GetActionRecipe.Response call(GetActionRecipe.Request req) {
00100 
00101                         GetActionRecipe.Response res = new GetActionRecipe.Response();
00102                         REInterface con = getREConnection(guestInterfaceKey);
00103                         String ont = con.requestActionRecipe(req.recipeUID);
00104                         if (ont != null) {
00105                                 res.recipe = ont;
00106                                 res.success = true;
00107                                 ros.logInfo("GetActionRecipe (UID: " + req.recipeUID + "): Done");
00108                         } else {
00109                                 res.success = false;
00110                                 ros.logInfo("GetActionRecipe (UID: " + req.recipeUID + "): Failed");
00111                         }
00112 
00113                         return res;
00114 
00115                 }
00116 
00117         }
00118 
00126         class SetActionRecipeCallback implements ServiceServer.Callback<SetActionRecipe.Request, SetActionRecipe.Response> {
00127 
00128                 @Override
00129                 public SetActionRecipe.Response call(SetActionRecipe.Request req) {
00130 
00131                         SetActionRecipe.Response res = new SetActionRecipe.Response();
00132                         res.success = false;
00133 
00134                         if (req.apiKey != null && req.apiKey.length() > 0) {
00135 
00136                                 REInterface con = getREConnection(req.apiKey);
00137                                 res.success = con.submitActionRecipe(req.recipe, req.cls, req.id, req.description);
00138                                 if (res.success) {
00139                                         ros.logInfo("SetActionRecipe (UID: " + req.cls+"."+req.id + "): Done");
00140                                 } else {
00141                                         ros.logInfo("SetActionRecipe (UID: " + req.cls+"."+req.id + "): Failed");
00142                                 }
00143 
00144                         } else {
00145                                 ros.logError("SetActionRecipe (UID: " + req.cls+"."+req.id + "): API key is missing!");
00146                         }
00147 
00148                         return res;
00149 
00150                 }
00151 
00152         }
00153 
00161         class DelActionRecipeCallback implements ServiceServer.Callback<DelActionRecipe.Request, DelActionRecipe.Response> {
00162 
00163                 @Override
00164                 public DelActionRecipe.Response call(DelActionRecipe.Request req) {
00165 
00166                         DelActionRecipe.Response res = new DelActionRecipe.Response();
00167                         res.success = false;
00168 
00169                         if (req.apiKey != null && req.apiKey.length() > 0) {
00170 
00171                                 REInterface con = getREConnection(req.apiKey);
00172                                 res.success = con.deleteActionRecipe(req.recipeUID);
00173                                 if (res.success) {
00174                                         ros.logInfo("DelActionRecipe (UID: " + req.recipeUID + "): Done");
00175                                 } else {
00176                                         ros.logInfo("DelActionRecipe (UID: " + req.recipeUID + "): Failed");
00177                                 }
00178 
00179                         } else {
00180                                 ros.logError("DelActionRecipe (UID: " + req.recipeUID + "): API key is missing!");
00181                         }
00182 
00183                         return res;
00184 
00185                 }
00186 
00187         }
00188 
00196         class UpdateActionRecipeCallback implements ServiceServer.Callback<UpdateActionRecipe.Request, UpdateActionRecipe.Response> {
00197 
00198                 @Override
00199                 public UpdateActionRecipe.Response call(UpdateActionRecipe.Request req) {
00200 
00201                         UpdateActionRecipe.Response res = new UpdateActionRecipe.Response();
00202                         res.success = false;
00203 
00204                         if (req.apiKey != null && req.apiKey.length() > 0) {
00205 
00206                                 REInterface con = getREConnection(req.apiKey);
00207                                 res.success = con.updateActionRecipe(req.uid, req.recipe, req.description);
00208                                 if (res.success) {
00209                                         ros.logInfo("UpdateActionRecipe (UID: " + req.uid + "): Done");
00210                                 } else {
00211                                         ros.logInfo("UpdateActionRecipe (UID: " + req.uid + "): Failed");
00212                                 }
00213 
00214                         } else {
00215                                 ros.logError("UpdateActionRecipe (UID: " + req.uid + "): API key is missing!");
00216                         }
00217 
00218                         return res;
00219 
00220                 }
00221 
00222         }
00223 
00231         class SearchActionRecipesCallback implements ServiceServer.Callback<SearchActionRecipes.Request, SearchActionRecipes.Response> {
00232 
00233                 @Override
00234                 public SearchActionRecipes.Response call(SearchActionRecipes.Request req) {
00235 
00236                         SearchActionRecipes.Response res = new SearchActionRecipes.Response();
00237                         res.success = false;
00238                         
00239                         REInterface con = getREConnection(guestInterfaceKey);
00240                         ArrayList<String> uids = new ArrayList<String>();
00241                         String[] onts = con.searchActionRecipes(req.searchID, uids);
00242                         if (onts != null) {
00243                                 for (String ont : onts) {
00244                                         res.recipes.add(ont);   
00245                                 }
00246                                 res.uids = uids;
00247                                 res.success = true;
00248                                 ros.logInfo("SearchActionRecipes (search: " + req.searchID + "): Done");                                
00249                         } else {
00250                                 res.success = false;
00251                                 ros.logInfo("SearchActionRecipes (search: " + req.searchID + "): Failed");
00252                         }
00253 
00254                         return res;
00255 
00256                 }
00257 
00258         }
00259         
00267         class QueryActionRecipesCallback implements ServiceServer.Callback<QueryActionRecipes.Request, QueryActionRecipes.Response> {
00268 
00269                 @Override
00270                 public QueryActionRecipes.Response call(QueryActionRecipes.Request req) {
00271 
00272                         QueryActionRecipes.Response res = new QueryActionRecipes.Response();
00273                         res.result = "";
00274 
00275                         REInterface con = getREConnection(guestInterfaceKey);
00276                         String result = con.queryActionRecipeDB(req.query);
00277                         if (result != null) {
00278                                 res.result = result;
00279                                 ros.logInfo("QueryActionRecipe (query:\n" + req.query + "): Done");
00280                         } else {
00281                                 ros.logInfo("QueryActionRecipe (query:\n" + req.query + "): Failed");
00282                         }
00283 
00284                         return res;
00285 
00286                 }
00287 
00288         }
00289         
00290 }


re_comm_core
Author(s): Alexander Perzylo
autogenerated on Sun Jan 5 2014 11:29:33