Example #1
0
    def setUpClass(cls):
        # Get DBus interface to the board and attempt a recovery if it fails.
        cls.iface = get_ck2_pro_hat_interface()
        if not cls.iface:
            cls._attempt_to_recover_daemon()

        cls.iface = get_ck2_pro_hat_interface()
        if not cls.iface:
            pass  # TODO: Stop here, no point going forward. THIS SHOULD NOT HAPPEN!
Example #2
0
def is_power_hat_plugged(with_dbus=True, retry_count=5):
    """Check if the Kano PowerHat board is plugged in.

    NOTE: Running this function with_dbus=False must be done when the daemon is
          certainly not running. Otherwise, bad things might happen.

    Args:
        with_dbus (bool): Whether to run the detection through the central dbus
            kano-boards-daemon, or bypass to use the underlying library
        retry_count: See
            :func:`~kano_peripherals.ck2_pro_hat.driver.high_level.get_ck2_pro_hat_interface`

    Returns:
        bool: Whether the PowerHat is plugged in or not
    """
    is_plugged = False

    try:
        if with_dbus:
            ck2prohat_iface = get_ck2_pro_hat_interface(
                retry_count=retry_count)
            is_plugged = bool(ck2prohat_iface and ck2prohat_iface.detect())
        else:
            is_plugged = CK2ProHatService.quick_detect()
    except:
        logger.error('Unexpected error occured:\n{}'.format(
            traceback.format_exc()))

    return is_plugged
Example #3
0
def verify_kit_is_plugged():
    """
    On Computer Kit 2 Pro, verify that the battery is plugged in and not depleting.
    For now, we rely on the user to tell us.

    Returns:
        is_plugged - bool whether the kit is plugged in (and battery isn't low) or not.
    """
    has_battery = False
    is_battery_low = False
    is_plugged = True

    try:
        from kano_peripherals.wrappers.detection import is_ck2_pro, is_ckt
        has_battery = is_ck2_pro(retry_count=0) or is_ckt(retry_count=0)
    except:
        # Kano Peripherals doesn't support this function
        pass

    if has_battery:
        from kano.gtk3.kano_dialog import KanoDialog
        # Run the first dialog asking the user a question.
        dialog = KanoDialog(
            title_text=_("Power Required"),
            description_text=_("Is your computer plugged in?"),
            button_dict={
                _("Yes"): {'color': 'green', 'return_value': True},
                _("No"): {'color': 'red', 'return_value': False}
            }
        )
        is_plugged = dialog.run()

        try:
            from kano_peripherals.ck2_pro_hat.driver.high_level import get_ck2_pro_hat_interface
            ck2pro_iface = get_ck2_pro_hat_interface(retry_count=0)
            is_battery_low = (ck2pro_iface and ck2pro_iface.is_battery_low())
        except:
            # Kano Peripherals doesn't support this function
            pass

        header = ""
        if is_battery_low:
            header = _("Low Battery")
        else:
            header = _("Power Required")

        # If the answer was negative, show another message.
        if not is_plugged or is_battery_low:
            KanoDialog(
                title_text=header,
                description_text=_(
                    "Sorry! You cannot update unless your computer is plugged in.\n"
                    "Plug it in and try again!"
                ),
                button_dict={
                    _("Continue"): {'color': 'green'}
                }
            ).run()

    return is_plugged and not is_battery_low
Example #4
0
def set_power_button(enabled):
    """
    Enables or disables any power button that might be attached via a Kano hat.
    This is for when we return to the OS and not reboot / shutdown.
    """
    try:
        pihat_iface = get_pihat_interface(retry_count=0)
        if pihat_iface:
            pihat_iface.set_power_button_enabled(enabled)

        pro_hat_iface = get_ck2_pro_hat_interface(retry_count=0)
        if pro_hat_iface:
            pro_hat_iface.set_power_button_enabled(enabled)
    except Exception:
        # Kano Peripherals doesn't support this function
        pass