Skip to content

Commit

Permalink
Merge pull request #174 from robotics-in-concert/admin_app_patch
Browse files Browse the repository at this point in the history
Admin app patch
  • Loading branch information
jihoonl committed Jan 20, 2015
2 parents b504e0c + 1a13f9d commit cd0d6d0
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 98 deletions.
119 changes: 66 additions & 53 deletions concert_admin_app/src/concert_admin_app/admin_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,76 @@
##############################################################################
# Imports
##############################################################################
#system
# system
from __future__ import division
import os
#pyqt
# pyqt
from python_qt_binding import loadUi
from python_qt_binding.QtCore import Qt, Signal, SIGNAL,SLOT
from python_qt_binding.QtCore import Qt, Signal, SIGNAL, SLOT
from python_qt_binding.QtGui import QWidget, QTreeWidgetItem, QTextEdit, QPushButton, QPlainTextEdit, QGridLayout, QLabel

#ros
# ros
import rospkg
#rqt
# rqt
from qt_gui.plugin import Plugin
import rocon_qt_library.utils as utils

from .admin_app_interface import AdminAppInterface
from rocon_python_comms.exceptions import NotFoundException


##############################################################################
# Admin App
##############################################################################

class AdminApp(Plugin):

_refresh_service_list_signal=Signal()
_refresh_service_list_signal = Signal()

def __init__(self, context):
self._context=context
self._context = context
super(AdminApp, self).__init__(context)
self.initialised=False
self.initialised = False
self.setObjectName('Admin App')

self.is_setting_dlg_live=False;
self._widget=QWidget()
self.is_setting_dlg_live = False
self._widget = QWidget()

rospack=rospkg.RosPack()
ui_file=os.path.join(rospack.get_path('concert_admin_app'), 'ui', 'admin_app.ui')

rospack = rospkg.RosPack()
ui_file = os.path.join(rospack.get_path('concert_admin_app'), 'ui', 'admin_app.ui')
self._widget.setObjectName('AdminApphUi')
loadUi(ui_file, self._widget, {})

if context.serial_number() > 1:
self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))


self.admin_app_interface = AdminAppInterface()
self.admin_app_interface._reg_event_callback(self._refresh_service)
self.current_service = None

self.params_layout = None
self.params_layout_items = []

self._init_admin_app_interface()
self._init_event()
self._init_widget()
context.add_widget(self._widget)


def _init_admin_app_interface(self):
self.admin_app_interface = AdminAppInterface()
try:
self.admin_app_interface._init_admin_app_interface()
self.admin_app_interface._reg_event_callback(self._refresh_service)
except NotFoundException as e:
self.emit(SIGNAL("show_message"), self._widget, "Failed", e)

def _init_event(self):
self._widget.enable_disable_btn.pressed.connect(self._toggle_service)
self._widget.refresh_btn.pressed.connect(self._refresh_service)
self._widget.save_configuration_btn.pressed.connect(self._save_parmeters)
self._widget.service_tree_widget.itemClicked.connect(self._select_service_tree_item) #concert item click event
self._widget.service_tree_widget.itemClicked.connect(self._select_service_tree_item) # concert item click event
self._refresh_service_list_signal.connect(self._update_service_list)
self.connect(self, SIGNAL("show_message"), utils.show_message)

def _init_widget(self):
self.params_layout = self._widget.findChildren(QGridLayout, "params_layout")[0]
self.params_layout_items = []
Expand All @@ -85,8 +95,8 @@ def _save_parmeters(self):
msg = "service is already enabled!"
elif self.params_layout_items:
for item in self.params_layout_items:
params[item[0].text()] = item[1].toPlainText().strip()
result = self.admin_app_interface.set_srv_parameters(params)
params[item[0].text()] = item[1].toPlainText().strip()
result = self.admin_app_interface.set_srv_parameters(self.current_service['name'], params)
else:
msg = "No params infomation"

Expand All @@ -97,55 +107,60 @@ def _save_parmeters(self):

def _toggle_service(self):
if self.current_service['enabled']:
print "Disable Service: %s"%self.current_service['name']
self.admin_app_interface.disable_service(self.current_service['name'])
print "Disable Service: %s" % self.current_service['name']
(success, message) = self.admin_app_interface.disable_service(self.current_service['name'])
self._set_enable_params_layout(True)
else:
print "Enable Service: %s"%self.current_service['name']
self.admin_app_interface.eable_service(self.current_service['name'])
print "Enable Service: %s" % self.current_service['name']
(success, message) = self.admin_app_interface.eable_service(self.current_service['name'])
self._set_enable_params_layout(False)

if success:
self.emit(SIGNAL("show_message"), self._widget, "Success", message)
else:
self.emit(SIGNAL("show_message"), self._widget, "Failure", message)

self.current_service = None
self._widget.enable_disable_btn.setDisabled(True)
self._widget.enable_disable_btn.setText("Enable/Disable")

def _refresh_service(self):
self._refresh_service_list_signal.emit()

def _update_service_list(self):
self._widget.service_tree_widget.clear()
self._widget.service_info_text.clear()
self._widgetitem_service_pair = {}
service_list = self.admin_app_interface.service_list

for k in service_list.values():
#Top service
service_item=QTreeWidgetItem(self._widget.service_tree_widget)
#service_item=QTreeWidgetItem()
service_item.setText (0, k['name'])
#set Top Level Font
font=service_item.font(0)
# Top service
service_item = QTreeWidgetItem(self._widget.service_tree_widget)
# service_item=QTreeWidgetItem()
service_item.setText(0, k['name'])

# set Top Level Font
font = service_item.font(0)
font.setPointSize(20)
font.setBold(True)
service_item.setFont(0,font)
service_item.setFont(0, font)

self._widgetitem_service_pair[service_item] = k
def _set_service_info(self,service_name):

def _set_service_info(self, service_name):
service_list = self.admin_app_interface.service_list
self._widget.service_info_text.clear()
self._widget.service_info_text.appendHtml(service_list[service_name]['context'])
def _select_service_tree_item(self,item):

def _select_service_tree_item(self, item):
if item.parent() == None:
selected_service = self._widgetitem_service_pair[item]
print '_select_service: '+ selected_service['name']

print '_select_service: ' + selected_service['name']
self._set_service_info(selected_service['name'])
self.current_service = selected_service
self._set_parameter_layout()

if self.current_service['enabled']:
self._widget.enable_disable_btn.setText("Disable")
self._widget.enable_disable_btn.setDisabled(False)
Expand All @@ -154,26 +169,26 @@ def _select_service_tree_item(self,item):
self._widget.enable_disable_btn.setDisabled(False)

def _set_parameter_layout(self):
params = self.admin_app_interface.get_srv_parameters(self.current_service['parameters'])

params = self.admin_app_interface.get_srv_parameters(self.current_service['parameters_detail'])
is_enabled = self.current_service['enabled']

if self.params_layout_items:
for item in self.params_layout_items:
self.params_layout.removeWidget(item[0])
self.params_layout.removeWidget(item[1])
item[0].setParent(None)
item[1].setParent(None)
self.params_layout_items = []

if params:
self.params_layout.setColumnStretch (1, 0)
self.params_layout.setRowStretch (2, 0)
self.params_layout.setColumnStretch(1, 0)
self.params_layout.setRowStretch(2, 0)
for param in params.keys():
label = QLabel(param)
label = QLabel(param)
self.params_layout.addWidget(label)
value = QTextEdit(str(params[param]))
value.setMaximumHeight(30)
value.setMaximumHeight(30)
self.params_layout.addWidget(value)
self.params_layout_items.append((label, value))

Expand All @@ -185,5 +200,3 @@ def _set_enable_params_layout(self, enable):
for item in self.params_layout_items:
item[0].setEnabled(enable)
item[1].setEnabled(enable)


110 changes: 65 additions & 45 deletions concert_admin_app/src/concert_admin_app/admin_app_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,94 @@
# License: BSD
# https://raw.github.com/robotics-in-concert/rocon_qt_gui/license/LICENSE
#
import yaml
import rospy
import rocon_python_utils
import rocon_python_comms

import concert_msgs.msg as concert_msgs
import rocon_std_msgs.msg as rocon_std_msgs

from concert_msgs.srv import EnableService
from concert_msgs.srv import UpdateServiceConfig

from rocon_python_comms.exceptions import NotFoundException
##############################################################################
# Graph
##############################################################################


class AdminAppInterface(object):

def __init__(self):
self._event_callback = None
self.full_params_path = ""
self.full_params_path = ""
self.service_list = {}
# should make use of concert_msgs/Strings here.
rospy.Subscriber("concert/services/list", concert_msgs.Services, self.update_service_list)
self.ros_subscribers = {}
self.ros_services = {}

def _init_admin_app_interface(self):
self._init_ros_subscriber()
self._init_ros_service()

def _init_ros_subscriber(self):
try:
subscriber_topic_name = rocon_python_comms.find_topic('concert_msgs/Services', timeout=rospy.rostime.Duration(5.0), unique=True)
self.ros_subscribers['service_list'] = rospy.Subscriber(subscriber_topic_name, concert_msgs.Services, self.update_service_list)
except NotFoundException as e:
raise e

def _init_ros_service(self):
try:
enable_service = rocon_python_comms.find_service('concert_msgs/EnableService', timeout=rospy.rostime.Duration(5.0), unique=True)
self.ros_services['enable_service'] = rospy.ServiceProxy(enable_service, EnableService)
update_service_config = rocon_python_comms.find_service('concert_msgs/UpdateServiceConfig', timeout=rospy.rostime.Duration(5.0), unique=True)
self.ros_services['update_service_config'] = rospy.ServiceProxy(update_service_config, UpdateServiceConfig)
except NotFoundException as e:
raise e

def _reg_event_callback(self, func):
self._event_callback = func


def eable_service(self, srv_name):
service = "/concert/services/enable"
service_handle=rospy.ServiceProxy(service, EnableService)
call_result=service_handle(srv_name,True)
print call_result

# todo
call_result = self.ros_services['enable_service'](srv_name, True)
return call_result.success, call_result.error_message

def disable_service(self, srv_name):
service = "/concert/services/enable"
service_handle=rospy.ServiceProxy(service, EnableService)
call_result=service_handle(srv_name,False)
print call_result
# todo
call_result = self.ros_services['enable_service'](srv_name, False)
return call_result.success, call_result.error_message

def get_srv_parameters(self, parameters_path):
def update_service_config(self, srv_profile):
# todo
call_result = self.ros_services['update_service_config'](srv_profile)
return call_result.success, call_result.error_message

def get_srv_parameters(self, parameters_detail):
params = None
if parameters_path:
self.full_params_path = rocon_python_utils.ros.resources.find_resource_from_string(parameters_path+".parameters")
try:
stream = open(self.full_params_path, 'r')
params = yaml.load(stream)

except Exception, e:
rospy.loginfo(e)
else:
return params

def set_srv_parameters(self, data):
params = {}
for param in parameters_detail:
params[param.key] = param.value
return params

def set_srv_parameters(self, srv_name, param_data):
result = False
try:
if not self.full_params_path:
return False
if not data:
return False
stream = open(self.full_params_path, 'r')
bak_data = yaml.load(stream)
bak_stream = file(self.full_params_path+'.bak', 'w')
yaml.safe_dump(bak_data, bak_stream, default_flow_style=False)
new_stream = file(self.full_params_path, 'w')
yaml.safe_dump(data, new_stream, default_flow_style=False)
srv_profile_msg = concert_msgs.ServiceProfile()
srv_profile_msg.name = srv_name
parameter_detail = []
for key in param_data.keys():
parameter = rocon_std_msgs.KeyValue(key, param_data[key])
parameter_detail.append(parameter)
srv_profile_msg.parameters_detail = parameter_detail
result = self.update_service_config(srv_profile_msg)
except Exception, e:
rospy.loginfo(e)
return False
else:
return True
result = False

return result

def update_service_list(self, data):

self.service_list = {}
for k in data.services:
service_name = k.name
Expand All @@ -86,14 +105,15 @@ def update_service_list(self, data):
self.service_list[service_name]['launcher'] = k.launcher
self.service_list[service_name]['interactions'] = k.interactions
self.service_list[service_name]['parameters'] = k.parameters
self.service_list[service_name]['parameters_detail'] = k.parameters_detail
self.service_list[service_name]['uuid'] = k.uuid
self.service_list[service_name]['status'] = k.status
self.service_list[service_name]['enabled'] = k.enabled

#html
# html
service_context = "<html>"
for key in self.service_list[service_name].keys():
service_context += "<p><b>"+key+": </b>" + str(self.service_list[service_name][key]) + "</p>"
service_context += "<p><b>" + key + ": </b>" + str(self.service_list[service_name][key]) + "</p>"
service_context += "</html>"
self.service_list[service_name]['context'] = service_context

Expand Down

0 comments on commit cd0d6d0

Please sign in to comment.