1
votes

Good day all. I am trying to set an IF function with time, where it will check if something is working for a set amount of time and IF it isn't working then it will reset/restart a script.

if ledBlue.off() >= 10 seconds
command="sudo python3 project-2.py"

However, I get the feeling that I would be using a counter instead But I am definitely not sure how I would go about this.

The code that I currently have for this section is as follows:

if failed:
   ledBlue.off()
   if ledBlue.off() >= 10 seconds
       command="sudo python3 project.py"
       time.sleep(2)
       command="sudo restart service"
       os.system(command)
else:
   ledBlue.on()

If what I am thinking about the timer is true I would have to implement a time.counter() function to see how long ledBlue.off() is before it implements the code.

How do I go about getting this right please...

I am not trying to pause the script for a period of time but to see how long the ledBlue.off() has been running then implement a script if it has been off for more than 10 seconds...

1
@TrebuchetMS this sure is not a duplicate to that link as what I am trying is the opposite of time.sleep(). time.sleep() pauses for the duration of time specified inside the brackets, where as I am trying to get ledBlue.off() to equal a certain amount of time it is off, before it restarts a program/script/codeuser92651

1 Answers

0
votes

I've made a similar script for forcing my kid's Roku tv off if its on. I will keep it on all night just to make sure my child doesn't watch tv at night. (She has a TCL Roku tv). If I refactor the code for you use, it would look something like

#import required libs
import keyboard
...
count = 0
while True:
    if ledBlue.off() = true
        sleep(1)
        count += 1
        if count == 10:
            run_py="sudo python3 project.py"
            os.system(run_py)
            time.sleep(2)
            restart_service="sudo restart service"
            os.system(restart_service)
            count = 0 
        elif keyboard.is_pressed('q'):
            break
        else:
            pass

this is a bit hacky, but yeah, knowing the method inside ledBlue() could be used to optimize this. I added the keyboard libs because this script will keep running, so you will be able to press "q" to break from the loop.

here is my original code:

    def DenyRemote(self):
        state = self.device_pw_state()
        while True:
            sleep(10)
            if state == "PowerOn":
                self.roku._post('/keypress/Power')
            elif keyboard.is_pressed('q'):
                break
            else:
                pass