Exemplo n.º 1
0
    def start_delay(self):
        """
        Delay up to config seconds, waiting for boot conditions to be true

        :return: None
        """
        # we only delay if at least ONE condition is still not satisfied
        wait_conditions = 0

        time_duration = TimeDuration()

        # start with the defaults
        # use time_duration to handle "300" or "5 min"
        time_duration.parse_time_duration_to_seconds(self.DEF_BOOT_DELAY_SEC)
        delay_seconds = time_duration.get_seconds()
        delay_for_valid_time = self.DEF_DELAY_FOR_TIME
        delay_for_uplink = self.DEF_DELAY_FOR_WAN

        if self.SECTION_STARTUP in self.settings:
            # then we do have a [startup] section in settings.json

            if self.SET_BOOT_DELAY_SEC in self.settings[self.SECTION_STARTUP]:
                # how many seconds to wait for boot conditions to be satisfied
                time_duration.parse_time_duration_to_seconds(self.settings[
                    self.SECTION_STARTUP][self.SET_BOOT_DELAY_SEC])
                delay_seconds = time_duration.get_seconds()

            if self.SET_DELAY_FOR_TIME in self.settings[self.SECTION_STARTUP]:
                # see if we delay until time.time() is returning valid info,
                # which prevents initial time-series data from being
                # generated with bogus 1-1-1970 time-stamps
                delay_for_valid_time = \
                    self.settings[self.SECTION_STARTUP][self.SET_DELAY_FOR_TIME]

            if self.SET_DELAY_FOR_WAN in self.settings[self.SECTION_STARTUP]:
                # see if we delay until router has a valid WAN uplink, which
                # prevents cloud clients from mistakenly flipping into
                # FAULT/RECOVERY modes because they tried to connect to fast
                delay_for_uplink = self.settings[self.SECTION_STARTUP][
                    self.SET_DELAY_FOR_WAN]

        if delay_for_valid_time:
            wait_conditions += 1

        if delay_for_uplink:
            wait_conditions += 1

        # we cannot use clock() because under Linux it means nothing
        # TODO - what happens when time() jumps?
        start_time = time.time()
        while (wait_conditions > 0) and \
                (time.time() - start_time) < delay_seconds:
            # loop until end of time period, or all conditions are okay

            if delay_for_valid_time and hw_status.router_time_is_valid():
                # then check on time, if okay neutralize our conditions
                delay_for_valid_time = False
                wait_conditions -= 1
            else:
                logging.debug("Delay - waiting for valid time")

            if delay_for_uplink and hw_status.router_wan_online():
                # then check on wan-uplink, is okay neutralize our conditions
                delay_for_uplink = False
                wait_conditions -= 1
            else:
                logging.debug("Delay - waiting for WAN uplink")

            if wait_conditions > 0:
                time.sleep(self.BOOT_DELAY_SLEEP)
            # else we'll break / leave the WHILE loop

        return
    def test_parse_time_duration_plus_minus(self):

        obj = TimeDuration()
        # check the signs - +/- to allow things like "do 5 minutes before X"

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('+1 ms'), 0.001)
        self.assertEqual(obj.get_period_as_string(), '1 ms')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('-1 ms'), -0.001)
        self.assertEqual(obj.get_period_as_string(), '-1 ms')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('+1 sec'), 1.0)
        self.assertEqual(obj.get_period_as_string(), '1 sec')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds(b'-1 sec'), -1.0)
        self.assertEqual(obj.get_period_as_string(), '-1 sec')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('+1 min'), 60.0)
        self.assertEqual(obj.get_period_as_string(), '1 min')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('-5 min'), -300.0)
        self.assertEqual(obj.get_period_as_string(), '-5 min')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('+2 hr'), 7200.0)
        self.assertEqual(obj.get_period_as_string(), '2 hr')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('-3 hr'), -10800.0)
        self.assertEqual(obj.get_period_as_string(), '-3 hr')

        # confirm the UTC 'decoration' is ignored,
        # including ('z', 'zulu', 'gm', 'utc', 'uct')
        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('+1 ms utc'),
                         0.001)
        self.assertEqual(obj.get_period_as_string(), '1 ms')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('-1 ms UTC'),
                         -0.001)
        self.assertEqual(obj.get_period_as_string(), '-1 ms')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('+1 sec z'), 1.0)
        self.assertEqual(obj.get_period_as_string(), '1 sec')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds(b'-1 sec Z'), -1.0)
        self.assertEqual(obj.get_period_as_string(), '-1 sec')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('+1 min gm'), 60.0)
        self.assertEqual(obj.get_period_as_string(), '1 min')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('-5 min GM'),
                         -300.0)
        self.assertEqual(obj.get_period_as_string(), '-5 min')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('+2 hr uct'),
                         7200.0)
        self.assertEqual(obj.get_period_as_string(), '2 hr')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('-3 hr zulu'),
                         -10800.0)
        self.assertEqual(obj.get_period_as_string(), '-3 hr')

        return
    def test_parse_time_duration(self):

        obj = TimeDuration()

        self.assertEqual(obj.parse_time_duration_to_seconds(1), 1.0)
        self.assertEqual(obj.get_period_as_string(), "1 sec")

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1'), 1.0)
        self.assertEqual(obj.get_period_as_string(), "1 sec")

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('01'), 1.0)
        self.assertEqual(obj.get_period_as_string(), "1 sec")

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('0x01'), 1.0)
        self.assertEqual(obj.get_period_as_string(), "1 sec")

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 ms'), 0.001)
        self.assertEqual(obj.get_period_as_string(), '1 ms')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 msec'), 0.001)
        self.assertEqual(obj.get_period_as_string(), '1 ms')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 sec'), 1.0)
        self.assertEqual(obj.get_period_as_string(), '1 sec')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds(b'1 sec'), 1.0)
        self.assertEqual(obj.get_period_as_string(), '1 sec')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 second'), 1.0)
        self.assertEqual(obj.get_period_as_string(), '1 sec')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 min'), 60.0)
        self.assertEqual(obj.get_period_as_string(), '1 min')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 minute'), 60.0)
        self.assertEqual(obj.get_period_as_string(), '1 min')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 minutes'), 60.0)
        self.assertEqual(obj.get_period_as_string(), '1 min')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 hr'), 3600.0)
        self.assertEqual(obj.get_period_as_string(), '1 hr')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 HR'), 3600.0)
        self.assertEqual(obj.get_period_as_string(), '1 hr')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 Hours'), 3600.0)
        self.assertEqual(obj.get_period_as_string(), '1 hr')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 day'), 86400.0)
        self.assertEqual(obj.get_period_as_string(), '1 day')

        # note: these handled, but have NO 'seconds' result
        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 month'), None)
        self.assertEqual(obj.get_period_as_string(), '1 mon')

        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds('1 year'), None)
        self.assertEqual(obj.get_period_as_string(), '1 yr')

        # repeat with more than 1 - a random value
        seed = random.randint(101, 999)
        source = "{0} ms".format(seed)
        expect_sec = seed * 0.001
        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds(source),
                         expect_sec)
        self.assertEqual(obj.get_period_as_string(), source)

        seed = random.randint(2, 59)
        source = "{0} sec".format(seed)
        expect_sec = seed * 1.0
        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds(source),
                         expect_sec)
        self.assertEqual(obj.get_period_as_string(), source)

        seed = random.randint(2, 59)
        source = "{0} min".format(seed)
        expect_sec = seed * 60.0
        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds(source),
                         expect_sec)
        self.assertEqual(obj.get_period_as_string(), source)

        seed = random.randint(2, 23)
        source = "{0} hr".format(seed)
        expect_sec = seed * 3600.0
        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds(source),
                         expect_sec)
        self.assertEqual(obj.get_period_as_string(), source)

        seed = random.randint(2, 9)
        source = "{0} day".format(seed)
        expect_sec = seed * 86400.0
        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds(source),
                         expect_sec)
        self.assertEqual(obj.get_period_as_string(), source)

        # note: these handled, but have NO 'seconds' result
        seed = random.randint(2, 9)
        source = "{0} mon".format(seed)
        expect_sec = None
        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds(source),
                         expect_sec)
        self.assertEqual(obj.get_period_as_string(), source)

        seed = random.randint(2, 9)
        source = "{0} yr".format(seed)
        expect_sec = None
        obj.reset()
        self.assertEqual(obj.parse_time_duration_to_seconds(source),
                         expect_sec)
        self.assertEqual(obj.get_period_as_string(), source)

        return
Exemplo n.º 4
0
    def __init__(self, name, app_base):
        """
        prep our thread, but do not start yet

        :param str name: name for the thread
        :param CradlepointAppBase app_base: prepared resources: logger, etc
        """
        threading.Thread.__init__(self, name=name)

        self.app_base = app_base
        self.app_base.logger.info("started INIT")

        # how long to delay between checking the GPIO
        self.loop_delay = self.app_base.settings["power_loss"].get(
            "check_input_delay", 15)
        # support things like '1 min' or 15
        duration = TimeDuration(self.loop_delay)
        self.loop_delay = float(duration.get_seconds())

        # how long to wait, to double-check LOSS
        self.loss_delay = self.app_base.settings["power_loss"].get(
            "loss_delay", 1)
        self.loss_delay = duration.parse_time_duration_to_seconds(
            self.loss_delay)

        # how long to wait, to double-check RESTORE
        self.restore_delay = self.app_base.settings["power_loss"].get(
            "restore_delay", 1)
        self.restore_delay = duration.parse_time_duration_to_seconds(
            self.restore_delay)

        # when GPIO matches this state, then power is lost
        self.state_in_alarm = self.app_base.settings["power_loss"].get(
            "match_on_power_loss", False)
        # support 'true', '1' etc - but finally is True/False
        self.state_in_alarm = parse_boolean(self.state_in_alarm)

        # when 'power is lost', send to LED
        self.led_in_alarm = self.app_base.settings["power_loss"].get(
            "led_on_power_loss", None)
        try:
            # see if the setting is None, to disable
            self.led_in_alarm = parse_none(self.led_in_alarm)

        except ValueError:
            # support 'true', '1' etc - but finally is True/False
            self.led_in_alarm = parse_boolean(self.led_in_alarm)

        # when GPIO matches this state, then power is lost
        self.site_name = self.app_base.settings["power_loss"].get(
            "site_name", "My Site")

        # create an event to manage our stopping
        # (Note: on CP router, this isn't strictly true, as when the parent is
        # stopped/halted, the child dies as well. However, you may want
        # your sub task to clean up before it exists
        self.keep_running = threading.Event()
        self.keep_running.set()

        # hold the .get_power_loss_status()
        self.last_state = None

        # special tweak to announce 'first poll' more smartly
        self.starting_up = True

        self.email_settings = dict()
        self.prep_email_settings()

        return