diff --git a/Drone/client.py b/Drone/client.py index 8b5d6fe..f824e5e 100644 --- a/Drone/client.py +++ b/Drone/client.py @@ -175,10 +175,14 @@ class Client(object): ConfigOption("SERVER", "port", self.server_port), ConfigOption("SERVER", "host", self.server_host)) logger.info("Binding to new IP: {}:{}".format(self.server_host, self.server_port)) + self.on_broadcast_bind() break finally: broadcast_client.close() + def on_broadcast_bind(self): + pass + def _process_connections(self): while True: events = self.selector.select(timeout=1) diff --git a/Drone/copter_client.py b/Drone/copter_client.py index ac22c1f..e12855f 100644 --- a/Drone/copter_client.py +++ b/Drone/copter_client.py @@ -41,6 +41,10 @@ class CopterClient(client.Client): self.USE_LEDS = self.config.getboolean('PRIVATE', 'use_leds') self.LED_PIN = self.config.getint('PRIVATE', 'led_pin') + def on_broadcast_bind(self): + configure_chrony_ip(self.server_host) + restart_service("chrony") + def start(self, task_manager_instance): client.logger.info("Init ROS node") rospy.init_node('Swarm_client', anonymous=True) @@ -52,6 +56,43 @@ class CopterClient(client.Client): super(CopterClient, self).start() +def restart_service(name): + os.system("systemctl restart {}".format(name)) + + +def configure_chrony_ip(ip, path="/etc/chrony/chrony.conf", ip_index=1): + try: + with open(path, 'r') as f: + raw_content = f.read() + except IOError as e: + print("Reading error {}".format(e)) + return False + + content = raw_content.split(" ") + + try: + current_ip = content[ip_index] + except IndexError: + print("Something wrong with config") + return False + + if "." not in current_ip: + print("That's not ip!") + return False + + if current_ip != ip: + content[ip_index] = ip + + try: + with open(path, 'w') as f: + f.write(" ".join(content)) + except IOError: + print("Error writing") + return False + + return True + + @messaging.request_callback("selfcheck") def _response_selfcheck(): check = FlightLib.selfcheck() @@ -75,12 +116,13 @@ def _response_cell(): @messaging.message_callback("test") def _command_test(**kwargs): - print("test") + logger.info("logging info test") + print("stdout test") @messaging.message_callback("service_restart") def _command_service_restart(**kwargs): - os.system("systemctl restart {}".format(kwargs["name"])) + restart_service(kwargs["name"]) @messaging.message_callback("led_test") @@ -193,7 +235,7 @@ def _play_animation(**kwargs): frame_time = rfp_time + client.active_client.RFP_TIME frame_delay = 0.125 # TODO from animation file for frame in frames: - point, color = animation.convert_frame(frame) # TODO add param to calculate delta + point, color, yaw = animation.convert_frame(frame) # TODO add param to calculate delta task_manager.add_task(frame_time, 0, animation.execute_frame, task_kwargs={ "point": point,