43 import roslaunch.parent
46 from std_srvs.srv
import Empty, EmptyResponse
48 from .app
import AppDefinition, load_AppDefinition_by_name
49 from .exceptions
import LaunchException, AppException, InvalidAppException, NotFoundException
50 from .master_sync
import MasterSync
51 from .msg
import App, AppList, StatusCodes, AppStatus, AppInstallationState, ExchangeApp
52 from .srv
import StartApp, StopApp, ListApps, ListAppsResponse, StartAppResponse, StopAppResponse, InstallApp, UninstallApp, GetInstallationState, UninstallAppResponse, InstallAppResponse, GetInstallationStateResponse, GetAppDetails, GetAppDetailsResponse
56 def __init__(self, robot_name, interface_master, app_list, exchange):
63 rospy.loginfo(
"Starting app manager for %s"%self.
_robot_name)
69 self.
scoped_name(
'application/app_status'), AppStatus,
70 latch=
True, queue_size=1)
73 latch=
True, queue_size=1)
93 local_service_names=service_names,
94 local_pub_names=pub_names)
99 roslaunch.pmon._init_signal_handlers()
102 self._exchange.update_local()
104 self._app_list.update()
110 self._api_sync.stop()
112 self._launch.shutdown()
113 self._interface_sync.stop()
124 self._list_apps_pub.publish([app], self._app_list.get_app_list())
126 self._list_apps_pub.publish([], self._app_list.get_app_list())
129 return rosgraph.names.canonicalize_name(
'/%s/%s'%(self.
_robot_name, rospy.remap_name(name)))
132 return GetAppDetailsResponse(app=self._exchange.get_app_details(req.name))
137 if (req.remote_update):
139 if (
not self._exchange.update()):
141 i_apps = self._exchange.get_installed_apps()
142 a_apps = self._exchange.get_available_apps()
143 return GetInstallationStateResponse(installed_apps=i_apps, available_apps=a_apps)
147 self._list_apps_pub.publish([self.
_current_app], self._app_list.get_app_list())
149 self._list_apps_pub.publish([], self._app_list.get_app_list())
154 i_apps = self._exchange.get_installed_apps()
155 a_apps = self._exchange.get_available_apps()
156 self._exchange_list_apps_pub.publish(i_apps, a_apps)
160 if (self._exchange.install_app(appname)):
161 self._app_list.update()
164 return InstallAppResponse(installed=
True, message=
"app [%s] installed"%(appname))
166 return InstallAppResponse(installed=
False, message=
"app [%s] could not be installed"%(appname))
170 if (self._exchange.uninstall_app(appname)):
171 self._app_list.update()
174 return UninstallAppResponse(uninstalled=
True, message=
"app [%s] uninstalled"%(appname))
176 return UninstallAppResponse(uninstalled=
False, message=
"app [%s] could not be uninstalled"%(appname))
179 rospy.loginfo(
"Listing apps")
182 running_apps = [current]
185 self._app_list.update()
186 rospy.loginfo(
"done listing apps")
187 return ListAppsResponse(running_apps=running_apps, available_apps=self._app_list.get_app_list())
190 rospy.loginfo(
"start_app: %s"%(req.name))
192 if self._current_app_definition.name == req.name:
193 return StartAppResponse(started=
True, message=
"app [%s] already started"%(req.name), namespace=self.
_app_interface)
195 self.
stop_app(self._current_app_definition.name)
201 rospy.loginfo(
"Loading app: %s"%(appname))
204 except ValueError
as e:
205 return StartAppResponse(started=
False, message=str(e), error_code=StatusCodes.BAD_REQUEST)
206 except InvalidAppException
as e:
207 return StartAppResponse(started=
False, message=str(e), error_code=StatusCodes.INTERNAL_ERROR)
208 except NotFoundException
as e:
209 return StartAppResponse(started=
False, message=str(e), error_code=StatusCodes.NOT_FOUND)
214 rospy.loginfo(
"Launching: %s"%(app.launch))
215 self._status_pub.publish(AppStatus(AppStatus.INFO,
'launching %s'%(app.display_name)))
218 self.
_launch = roslaunch.parent.ROSLaunchParent(rospy.get_param(
"/run_id"),
219 [app.launch], is_core=
False,
220 process_listeners=())
221 self._launch._load_config()
224 for N
in self._launch.config.nodes:
225 for t
in app.interface.published_topics.keys():
227 for t
in app.interface.subscribed_topics.keys():
231 fp = [self.
_app_interface +
'/' + x
for x
in app.interface.subscribed_topics.keys()]
232 lp = [self.
_app_interface +
'/' + x
for x
in app.interface.published_topics.keys()]
238 return StartAppResponse(started=
True, message=
"app [%s] started"%(appname), namespace=self.
_app_interface)
240 except Exception
as e:
246 self._status_pub.publish(AppStatus(AppStatus.INFO,
'app start failed'))
247 rospy.logerr(
"app start failed")
248 return StartAppResponse(started=
False, message=
"internal error [%s]"%(str(e)), error_code=StatusCodes.INTERNAL_ERROR)
252 self._launch.shutdown()
256 self._interface_sync.stop()
261 rospy.loginfo(
"handle stop app: %s"%(req.name))
266 self._app_list.update()
269 rospy.loginfo(
"app list is reloaded")
270 except Exception
as e:
271 rospy.logerr(
"Failed to reload app list: %s" % e)
272 return EmptyResponse()
283 self.
stop_app(self._current_app_definition.name)
289 resp = StopAppResponse(stopped=
False)
294 if app
is not None and appname ==
'*':
297 if app
is None or app.name != appname:
298 rospy.loginfo(
"handle stop app: app [%s] is not running [x]"%(appname))
299 resp.error_code = StatusCodes.NOT_RUNNING
300 resp.message =
"app %s is not running"%(appname)
304 rospy.loginfo(
"handle stop app: stopping app [%s]"%(appname))
305 self._status_pub.publish(AppStatus(AppStatus.INFO,
'stopping %s'%(app.display_name)))
307 rospy.loginfo(
"handle stop app: app [%s] stopped"%(appname))
309 resp.message =
"%s stopped"%(appname)
311 rospy.loginfo(
"handle stop app: app [%s] is not running"%(appname))
312 resp.message =
"app [%s] is not running"%(appname)
313 resp.error_code = StatusCodes.NOT_RUNNING
318 except Exception
as e:
319 rospy.logerr(
"handle stop app: internal error %s"%(e))
320 resp.error_code = StatusCodes.INTERNAL_ERROR
321 resp.message =
"internal error: %s"%(str(e))
def handle_list_apps(self, req)
def handle_install_app(self, req)
def handle_stop_app(self, req)
def handle_list_exchange_apps(self, req)
def handle_uninstall_app(self, req)
def publish_list_apps(self)
def scoped_name(self, name)
def _set_current_app(self, app, app_definition)
def _get_current_app(self)
def publish_exchange_list_apps(self)
def __init__(self, robot_name, interface_master, app_list, exchange)
def handle_reload_app_list(self, req=None)
def load_AppDefinition_by_name(appname)
def handle_start_app(self, req)
def stop_app(self, appname)
def handle_get_app_details(self, req)