from artiq.experiment import *
from time import sleep

class SchedulerInterruptTest(EnvExperiment):
    """Scheduler/Interruption Test"""
    def build(self):
        self.setattr_device("scheduler")
        self.N = 0

    def run(self):
        expid_1 = {
                "file": "Experiments/turn_lasers_on.py",
                "class_name": "TurnOnLasers",
                "arguments": {},
                "log_level": self.scheduler.expid["log_level"],
                "repo_rev": self.scheduler.expid["repo_rev"],
                }
        try:
            while True:
                ## Opcion 1: Input en terminal
                # self.turn_on()
                # self.wait_for_go()

                ## Opcion 2: Scheduler
                if not self.scheduler.check_pause():
                    self.turn_on()
                    self.scheduler.submit("main", expid_1, priority=10)
                    sleep(0.5) # Ojo con esto, que podes programar muchas corridas a futuro
                    self.scheduler.pause()
        except TerminationRequested:
            print("\t\t OFF WITH THE LASERS")

    def turn_on(self):
        print(f">>>>>> Medicion {self.N}")
        self.N += 1
        # sleep(1)

    def wait_for_go(self):
        while input("GO? ") != 'go':
            pass
        self.scheduler.pause()
