Beispiel #1
0
def get_timezone() -> str:
    return run_get_cmd(
        settings.system.timezone.get_cmd,
        cmd_name='timezone',
        exc_class=DateError,
        required_fields=['timezone']
    )['timezone']
Beispiel #2
0
def get_interface() -> Optional[str]:
    global _interface
    global _interface_time

    ap_settings = settings.slaves.discover.ap
    if ap_settings.interface:
        return ap_settings.interface

    if ap_settings.interface_cmd:
        now = time.time()
        if now - _interface_time > _INTERFACE_CACHE_TIMEOUT:
            _interface = None

        if _interface is not None:
            return _interface[0]

        result = cmd_utils.run_get_cmd(
            ap_settings.interface_cmd,
            cmd_name='AP client discover interface',
            exc_class=DiscoverException,
            required_fields=['interface']
        )

        _interface = (result.get('interface') or None,)
        _interface_time = now

        return _interface[0]
Beispiel #3
0
def get_wifi_config() -> Dict[str, str]:
    return run_get_cmd(settings.system.net.wifi.get_cmd,
                       cmd_name='WiFi config',
                       log_values=False,
                       exc_class=NetError,
                       required_fields=[
                           'ssid', 'psk', 'bssid', 'bssid_current',
                           'rssi_current'
                       ])
Beispiel #4
0
def get_ip_config() -> Dict[str, str]:
    return run_get_cmd(settings.system.net.ip.get_cmd,
                       cmd_name='IP config',
                       exc_class=NetError,
                       required_fields=[
                           'address', 'netmask', 'gateway', 'dns',
                           'address_current', 'netmask_current',
                           'gateway_current', 'dns_current'
                       ])
Beispiel #5
0
def get_battery_level() -> Optional[int]:
    if settings.system.battery.get_cmd:
        return int(run_get_cmd(
            settings.system.battery.get_cmd,
            cmd_name='battery',
            exc_class=BatteryError,
            required_fields=['level']
        )['level'])

    if _has_sensors_battery:
        return int(psutil.sensors_battery().percent)
Beispiel #6
0
def get_temperature() -> Optional[int]:
    if settings.system.temperature.get_cmd:
        return int(run_get_cmd(
            settings.system.temperature.get_cmd,
            cmd_name='temperature',
            exc_class=TemperatureError,
            required_fields=['value']
        )['value'])

    if settings.system.temperature.sensor_name:
        temperatures = psutil.sensors_temperatures()
        temp_info_list = temperatures[settings.system.temperature.sensor_name]
        temp_info = temp_info_list[settings.system.temperature.sensor_index]

        return int(temp_info.current)
Beispiel #7
0
def load() -> None:
    data = persist.get_value('device', {})

    # Attributes
    persisted_attrs = []
    for name, value in device_attrs.ATTRDEFS.items():
        persisted = value.get('persisted', value.get('modifiable'))
        if callable(persisted):
            persisted = persisted()

        if persisted:
            persisted_attrs.append(name)

    for name in persisted_attrs:
        if name.endswith('password') and hasattr(device_attrs, name + '_hash'):
            name += '_hash'

        if name not in data:
            continue

        value = data[name]

        # A few attributes may carry sensitive information, so treat them separately and do not log their values
        if name.count('password'):
            logger.debug('loaded %s', name)

        elif name == 'network_wifi':
            logger.debug('loaded %s = [hidden]', name)

        else:
            logger.debug('loaded %s = %s', name, json_utils.dumps(value))

        setattr(device_attrs, name, value)

    # Device name
    if settings.device_name.get_cmd:
        result = run_get_cmd(settings.device_name.get_cmd,
                             cmd_name='device name',
                             required_fields=['name'])
        device_attrs.name = result['name']

    # Hash empty passwords
    if not device_attrs.admin_password_hash:
        device_attrs.admin_password_hash = device_attrs.EMPTY_PASSWORD_HASH
    if not device_attrs.normal_password_hash:
        device_attrs.normal_password_hash = device_attrs.EMPTY_PASSWORD_HASH
    if not device_attrs.viewonly_password_hash:
        device_attrs.viewonly_password_hash = device_attrs.EMPTY_PASSWORD_HASH
Beispiel #8
0
def get_ip_config() -> Dict[str, str]:
    return run_get_cmd(settings.system.net.ip.get_cmd,
                       cmd_name='IP config',
                       exc_class=NetError,
                       required_fields=['ip', 'mask', 'gw', 'dns'])