<filename>ouster_ros/scripts/ptp_check.py
#!/usr/bin/env python

import rospy
from sensor_msgs.msg import PointCloud2
from time import sleep
import tf
import sys
import roslib
import numpy as np
import copy
import time
from std_msgs.msg import Time
import os
import subprocess
from std_msgs.msg import Time
import shlex
import signal

password = sys.argv[1]

class ptp_check_status():

    # constructor class
    def __init__(self):

        #init node
        rospy.init_node('ptp_check', anonymous=True)

        # variables
        self.password = password
        self.command1 = 'systemctl daemon-reload'.split()
        self.command_restart = 'systemctl restart'.split()
        self.command_enable = 'systemctl enable'.split()
        self.command_ptp = 'ptp4l'.split()
        self.command_sys = 'phc2sys'.split()
        self.command_shm = 'phc2shm'.split()
        self.command_c = 'chrony'.split()

        self.time = 30.0 # in seconds

        # initializatio
        self.initialization()

        # ptp check
        self.check_loop()


    def ptp_status_lidar_check(self): 
        #  inserir um while aqui ver sem ser subprocesso

        a  = str(os.popen('wget -q -nv -O- http://192.168.1.61/api/v1/time/ptp').readlines())        
        status = a.find('"port_state": "UNCALIBRATED"')

        if status != -1:
            return True
        else:
            return False

    def initialization(self):
        cmd1 = subprocess.Popen(['echo',self.password], stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command1, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd3 = subprocess.Popen(['sudo','-S'] + self.command_enable + self.command_ptp, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd4 = subprocess.Popen(['sudo','-S'] + self.command_restart + self.command_ptp, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        
        cmd1 = subprocess.Popen(['sudo','-S'] + self.command1, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command_enable + self.command_sys, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd3 = subprocess.Popen(['sudo','-S'] + self.command_restart + self.command_sys, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        
        cmd1 = subprocess.Popen(['sudo','-S'] + self.command1, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command_enable + self.command_shm, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd3 = subprocess.Popen(['sudo','-S'] + self.command_restart + self.command_shm, stdin=cmd1.stdout, stdout=subprocess.PIPE)

        cmd1 = subprocess.Popen(['sudo','-S'] + self.command1, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command_restart + self.command_c, stdin=cmd1.stdout, stdout=subprocess.PIPE)

    print("\nInitialization of services 'ptp4l', 'phc2sys', 'phc2shm' and 'chorny'!!!")


    def restart_ptp(self):
        # restart ptp protocol
        cmd1 = subprocess.Popen(['echo',self.password], stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command1, stdin=cmd1.stdout, stdout=subprocess.PIPE)

        cmd1 = subprocess.Popen(['echo',self.password], stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command_restart + self.command_ptp, stdin=cmd1.stdout, stdout=subprocess.PIPE)

        return cmd2
        
            
    def check_loop(self):
        # rate
        rate = rospy.Rate(1/self.time)

        while (not rospy.is_shutdown()):
            # ptp check
            status = self.ptp_status_lidar_check()
            # ptp restart
            if (status):
                # restart
                process = self.restart_ptp()

                print("PTP Restart!")
                
            # sleep
            rate.sleep()
        

if __name__ == '__main__':
    try:
        ptp_check_status()   
        
    except rospy.ROSInterruptException:
        pass
    
        

    

<filename>ouster_ros/scripts/ptp_check.py
#!/usr/bin/env python

import rospy
from sensor_msgs.msg import PointCloud2
from time import sleep
import tf
import sys
import roslib
import numpy as np
import copy
import time
from std_msgs.msg import Time
import os
import subprocess
from std_msgs.msg import Time
import shlex
import signal

password = sys.argv[1]

class ptp_check_status():

    # constructor class
    def __init__(self):

        #init node
        rospy.init_node('ptp_check', anonymous=True)

        # variables
        self.password = password
        self.command1 = 'systemctl daemon-reload'.split()
        self.command_restart = 'systemctl restart'.split()
        self.command_enable = 'systemctl enable'.split()
        self.command_ptp = 'ptp4l'.split()
        self.command_sys = 'phc2sys'.split()
        self.command_shm = 'phc2shm'.split()
        self.command_c = 'chrony'.split()

        self.time = 30.0 # in seconds

        # initializatio
        self.initialization()

        # ptp check
        self.check_loop()


    def ptp_status_lidar_check(self): 
        #  inserir um while aqui ver sem ser subprocesso

        a  = str(os.popen('wget -q -nv -O- http://192.168.1.61/api/v1/time/ptp').readlines())        
        status = a.find('"port_state": "UNCALIBRATED"')

        if status != -1:
            return True
        else:
            return False

    def initialization(self):
        cmd1 = subprocess.Popen(['echo',self.password], stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command1, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd3 = subprocess.Popen(['sudo','-S'] + self.command_enable + self.command_ptp, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd4 = subprocess.Popen(['sudo','-S'] + self.command_restart + self.command_ptp, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        
        cmd1 = subprocess.Popen(['sudo','-S'] + self.command1, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command_enable + self.command_sys, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd3 = subprocess.Popen(['sudo','-S'] + self.command_restart + self.command_sys, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        
        cmd1 = subprocess.Popen(['sudo','-S'] + self.command1, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command_enable + self.command_shm, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd3 = subprocess.Popen(['sudo','-S'] + self.command_restart + self.command_shm, stdin=cmd1.stdout, stdout=subprocess.PIPE)

        cmd1 = subprocess.Popen(['sudo','-S'] + self.command1, stdin=cmd1.stdout, stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command_restart + self.command_c, stdin=cmd1.stdout, stdout=subprocess.PIPE)

    print("\nInitialization of services 'ptp4l', 'phc2sys', 'phc2shm' and 'chorny'!!!")


    def restart_ptp(self):
        # restart ptp protocol
        cmd1 = subprocess.Popen(['echo',self.password], stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command1, stdin=cmd1.stdout, stdout=subprocess.PIPE)

        cmd1 = subprocess.Popen(['echo',self.password], stdout=subprocess.PIPE)
        cmd2 = subprocess.Popen(['sudo','-S'] + self.command_restart + self.command_ptp, stdin=cmd1.stdout, stdout=subprocess.PIPE)

        return cmd2
        
            
    def check_loop(self):
        # rate
        rate = rospy.Rate(1/self.time)

        while (not rospy.is_shutdown()):
            # ptp check
            status = self.ptp_status_lidar_check()
            # ptp restart
            if (status):
                # restart
                process = self.restart_ptp()

                print("PTP Restart!")
                
            # sleep
            rate.sleep()
        

if __name__ == '__main__':
    try:
        ptp_check_status()   
        
    except rospy.ROSInterruptException:
        pass
    
        

    

#!/usr/bin/env python # constructor class #init node # variables # in seconds # initializatio # ptp check #  inserir um while aqui ver sem ser subprocesso # restart ptp protocol # rate # ptp check # ptp restart # restart # sleep