def onDisConnect(request, action): ''' Removes the robot specified via url like '/disconnect/ROBOT_ID' from ROS-Publisher and Ros-Subscriber We only are here when the URl is like: '/robot/disconnect/ROBOT_ID' TODO DL RosConfigurator is here used? ''' partURL = request.path # If at the end is a slash we remove it simply if "/" is partURL[-1]: partURL = partURL[:-1] # Get ROBOT_ID, which is the last element robotID = partURL.split("/")[-1] Log("INFO", "Disconnecting robot '{}'".format(robotID)) # Iterate through every topic and unregister, then delete it if robotID in ROS_PUBLISHER: for topic in ROS_PUBLISHER[robotID]: ROS_PUBLISHER[robotID][topic].unregister() del ROS_PUBLISHER[robotID] RosConfigurator.removeRobot(robotID) if robotID in ROS_SUBSCRIBER: for topic in ROS_SUBSCRIBER[robotID]: ROS_SUBSCRIBER[robotID][topic].unregister() del ROS_SUBSCRIBER[robotID] RosConfigurator.removeRobot(robotID) # Return success end_request(request, None, 200, "")
def onDisConnect(request, action): ''' Removes the robot specified via url like '/disconnect/ROBOT_ID' from ROS-Publisher and Ros-Subscriber We only are here when the URl is like: '/disconnect/ROBOT_ID' ''' partURL = request.path # If at the end is a slash we remove it simply if "/" is partURL[-1]: partURL = partURL[:-1] # Get ROBOT_ID, which is the last element topic = partURL[11:] # Get everything after "/disconnect" # Iterate through every topic and unregister, then delete it if topic in ROS_PUBLISHER: ROS_PUBLISHER[topic].unregister() del ROS_PUBLISHER[topic] Log("INFO", "Disconnecting publisher on '{}'".format(topic)) RosConfigurator.removeTopic(topic) if topic in ROS_SUBSCRIBER: ROS_SUBSCRIBER[topic].unregister() del ROS_SUBSCRIBER[topic] Log("INFO", "Disconnecting subscriber on '{}'".format(topic)) RosConfigurator.removeTopic(topic) # Return success end_request(request, None, 200, "")
def robotDisconnection(data): ## \brief Handle robot diconnection # \param robot data dict (name) robot_name = data.data Log("INFO", "Disconnected robot: " + robot_name) if robot_name in ROBOT_TOPICS: CloudSubscriber.deleteEntity(robot_name, DEFAULT_CONTEXT_TYPE) CloudSubscriber.disconnect(robot_name, True) for topic in ROBOT_TOPICS[robot_name]["publisher"]: ROBOT_TOPICS[robot_name]["publisher"][topic]["publisher"].unregister() for topic in ROBOT_TOPICS[robot_name]["subscriber"]: ROBOT_TOPICS[robot_name]["subscriber"][topic]["subscriber"].unregister() RosConfigurator.removeRobot(robot_name)
def getRobots(refresh=False, withJson=True): ## \brief Get robots managed by firos # \param Refresh the robot list # \param Merge the robots with the configurtation JSON try: robots = copy.deepcopy(RosConfigurator.systemTopics(refresh)) if withJson: robots_json = getRobotsByJson() for robot_name in robots_json: robot_name = str(robot_name) if robot_name not in robots: robots[robot_name] = {"topics": {}} for topic_name in robots_json[robot_name]["topics"]: topic = robots_json[robot_name]["topics"][topic_name] robots[robot_name]["topics"][str(topic_name)] = { "msg": str(topic["msg"]) if type(topic["msg"]) is str else topic["msg"], "type": str(topic["type"]) } return robots except Exception as e: traceback.print_exc() Log("ERROR", e) return {}
def onConnect(request, action): ## \brief Launch robot search and connection # \param client request # \param action Log("INFO", "Connecting robots") loadMsgHandlers(RosConfigurator.systemTopics(True)) request.send_response(200) request.end_headers() request.wfile.write("")
def getRobots(refresh=False): ''' This retrieves the current configuration from FIROS. Here we load the `robots.json` and the 'whitelist.json' In case the `whitelist.json` is Empty: We do get the current robots from ROS and are adding them as subscribers (by default). In a small ROS-World this usually is no problem. But in an environment with many robots we might send a lot of data. ''' try: # Retrieves the whitelist.json. If it does not exists, it returns all topics. topics_regex = copy.deepcopy(RosConfigurator.systemTopics(refresh)) # Retrieves the robots.json. topics_json = getTopicsByJson() if len(topics_json) == 0: Log( "ERROR", "The file 'topics.json' is either empty or does not exist!\n\nExiting" ) sys.exit(1) # check if structure is as needed for key in topics_json.keys(): if len(topics_json[key]) != 2: Log( "ERROR", "The topic: '{}', does not have a list of length 2 (topics.json)! \n\nExiting" .format(key)) sys.exit(1) if not key.startswith("/"): Log( "ERROR", "The topic: '{}', does not start with '/' (topics.json)! \n\nExiting" .format(key)) sys.exit(1) if topics_json[key][1] not in ["publisher", "subscriber"]: Log( "ERROR", "The topic: '{}', does not specify publisher or subscriber (topics.json)! \n\nExiting" .format(key)) sys.exit(1) # Merge both dictionaties: # Here topics_json overrides entries in topics_regex: topics_regex.update(topics_json) topics = topics_regex return topics except Exception as e: traceback.print_exc() Log("ERROR", e) return {}
def onDisConnect(request, action): ## \brief Disconnect robot # \param client request # \param action robot_name = pathParams(request, action["regexp"])[0] Log("INFO", "Disconnecting robot" + robot_name) if robot_name in ROBOT_TOPICS: CloudSubscriber.deleteEntity(robot_name, DEFAULT_CONTEXT_TYPE) CloudSubscriber.disconnect(robot_name, True) for topic in ROBOT_TOPICS[robot_name]["publisher"]: ROBOT_TOPICS[robot_name]["publisher"][topic][ "publisher"].unregister() for topic in ROBOT_TOPICS[robot_name]["subscriber"]: ROBOT_TOPICS[robot_name]["subscriber"][topic][ "subscriber"].unregister() RosConfigurator.removeRobot(robot_name) request.send_response(200) request.end_headers() request.wfile.write("")
def onConnect(request, action): ''' This resets firos into its original state TODO DL reset, instead of connect? TODO DL Add real connect for only one Robot? ''' Log("INFO", "Connecting topics") loadMsgHandlers(RosConfigurator.systemTopics(True)) # Return Success end_request(request, None, 200, "")
def mapPublisher(): ## \brief Obtain map topics and publsh their link into context broker maps = RosConfigurator.getMapTopics() cb_maps = [ { "name": "websocket", "type": "connection", "value": "ws://{}:{}".format(IP, ROSBRIDGE_PORT) } ] if(MAP_SERVER_PORT): cb_maps.append({ "name": "socketio", "type": "connection", "value": "http://{}:{}".format(IP, MAP_SERVER_PORT) }) for map_topic in maps: CloudPublisher.publishMap(map_topic, cb_maps)
def getRobots(refresh=False): ''' This retrieves the current configuration from FIROS. Here we load the `robots.json` and the 'whitelist.json' In case the `whitelist.json` is Empty: We do get the current robots from ROS and are adding them as subscribers (by default). In a small ROS-World this usually is no problem. But in an environment with many robots we might send a lot of data. ''' try: # Retrieves the whitelist.json. If it does not exists, it returns all topics. robots = copy.deepcopy(RosConfigurator.systemTopics(refresh)) # Retrieves the robots.json. robots_json = getRobotsByJson() if len(robots_json) == 0: Log( "ERROR", "The file 'robots.json' is either empty or does not exist!\n\nExiting" ) sys.exit(1) # Merge robots.json into whitelist.json (overwrite if neccessary) for robot_name in robots_json: robot_name = str(robot_name) if robot_name not in robots: robots[robot_name] = {"topics": {}} for topic_name in robots_json[robot_name]["topics"]: topic = robots_json[robot_name]["topics"][topic_name] # Overwrite or add! robots[robot_name]["topics"][str(topic_name)] = { "msg": str(topic["msg"]), "type": str(topic["type"]) } return robots except Exception as e: traceback.print_exc() Log("ERROR", e) return {}
def _robotConnection(data): ## \brief Handle robot connection # \param robot data (Not neccessary) robot_name = data.data Log("INFO", "Connected robot: " + robot_name) loadMsgHandlers(RosConfigurator.systemTopics(True))
def mapRemover(): ## \brief Delete map topics from context broker maps = RosConfigurator.getMapTopics() for map_topic in maps: CloudSubscriber.deleteEntity(map_topic, "MAP", False)
def onWhitelistRestore(request, action): RosConfigurator.setWhiteList(None, None, True) end_request(request, None, 200, "")
def onWhitelistRemove(request, action): data = getPostParams(request) RosConfigurator.setWhiteList(None, data) end_request(request, None, 200, "")
def onWhitelistWrite(request, action): data = getPostParams(request) RosConfigurator.setWhiteList(data, None) end_request(request, None, 200, "")