def __init__(self): rospy.loginfo("Starting wifi manager...") self.hotspot_ssid = rospy.get_param("~hotspot_ssid") # add robot unique identifier to ssid to make it unique and recognizable self.hotspot_ssid += " " self.hotspot_ssid += str(self.get_robot_unique_identifier()) self.hotspot_password = rospy.get_param("~hotspot_password") self.filename_robot_name = rospy.get_param("~filename_robot_name") # Set filename for robot name set_filename_robot_name(self.filename_robot_name) # Get robot name self.robot_name = read_robot_name() if self.robot_name != '': self.hotspot_ssid = self.robot_name # Start set robot name service server self.set_robot_name_server = rospy.Service( '/niryo_one/wifi/set_robot_name', SetString, self.callback_set_robot_name) # Set Niryo One hotspot ssid and password set_hotspot_ssid(self.hotspot_ssid) set_hotspot_password(self.hotspot_password) # Start broadcast broadcast_thread = Thread(target=self.start_broadcast) broadcast_thread.setDaemon(True) broadcast_thread.start() # Check if connected to Wi-Fi. If not, start hotspot mode current_ssid = niryo_one_wifi.get_current_ssid() rospy.loginfo("Current ssid : " + str(current_ssid)) if not niryo_one_wifi.is_connected_to_wifi(): niryo_one_wifi.hard_enable_hotspot_with_ssid( self.hotspot_ssid, self.hotspot_password) else: rospy.loginfo("Already connected to a Wifi or in Hotspot mode") # Start Flask app flask_thread = Thread(target=self.run_flask_server) flask_thread.setDaemon(True) flask_thread.start() # Start wifi status publisher self.hotspot_state_publisher = rospy.Publisher( '/niryo_one/wifi/hotspot', Bool, queue_size=2) rospy.Timer(rospy.Duration(1), self.send_hotspot_state) # Start hotspot subscriber (from button) self.activate_hotspot_server = rospy.Service( '/niryo_one/wifi/set_hotspot', SetInt, self.callback_activate_hotspot) rospy.loginfo("Wifi manager started")
def callback_activate_hotspot(self, req): rospy.loginfo("Switch to hotspot mode") if niryo_one_wifi.get_current_ssid() == self.hotspot_ssid: return self.service_response(200, "Hotspot mode already activated") success = niryo_one_wifi.hard_enable_hotspot_with_ssid(self.hotspot_ssid, self.hotspot_password) if success: return self.service_response(200, "Hotspot mode activated") return self.service_response(400, "Failed to activate hotspot mode")