Exemplo n.º 1
0
    async def remove(self, persisted_data: bool = True) -> None:
        await self.cleanup()

        self.debug('removing port')
        _ports_by_id.pop(self._id, None)

        if persisted_data:
            self.debug('removing persisted data')
            await persist.remove(self.PERSIST_COLLECTION,
                                 filt={'id': self._id})
            if core_history.is_enabled():
                await core_history.remove_samples([self], background=True)

        await self.trigger_remove()
Exemplo n.º 2
0
async def post_reset(request: core_api.APIRequest,
                     params: GenericJSONDict) -> None:
    core_api_schema.validate(params, core_api_schema.POST_RESET)

    factory = params.get('factory')

    if factory:
        core_api.logger.info('resetting to factory defaults')

        await core_ports.reset()
        await core_vports.reset()
        await core_device.reset()
        if settings.webhooks.enabled:
            await core_webhooks.reset()
        if settings.reverse.enabled:
            await core_reverse.reset()
        if settings.slaves.enabled:
            await slaves.reset_ports()
            await slaves.reset_slaves()
        if core_history.is_enabled():
            await core_history.reset()

    main.loop.call_later(2, system.reboot)
Exemplo n.º 3
0
def get_attrs() -> Attributes:
    from qtoggleserver.core import api as core_api
    from qtoggleserver.core import history as core_history

    attrs = {
        'name':
        name,
        'display_name':
        display_name,
        'version':
        version.VERSION,
        'api_version':
        core_api.API_VERSION,
        'vendor':
        version.VENDOR,
        'uptime':
        system.uptime(),

        # Never disclose passwords
        'admin_password':
        '' if admin_password_hash == EMPTY_PASSWORD_HASH else 'set',
        'normal_password':
        '' if normal_password_hash == EMPTY_PASSWORD_HASH else 'set',
        'viewonly_password':
        '' if viewonly_password_hash == EMPTY_PASSWORD_HASH else 'set'
    }

    flags = ['expressions']
    if settings.system.fwupdate.driver:
        flags.append('firmware')

    if settings.core.backup_support:
        flags.append('backup')

    if core_history.is_enabled():
        flags.append('history')

    if settings.core.listen_support:
        flags.append('listen')

    if settings.slaves.enabled:
        flags.append('master')

    if settings.reverse.enabled:
        flags.append('reverse')

    if settings.core.sequences_support:
        flags.append('sequences')

    if settings.core.ssl_support:
        flags.append('ssl')

    if settings.webhooks.enabled:
        flags.append('webhooks')

    attrs['flags'] = flags

    if settings.core.virtual_ports:
        attrs['virtual_ports'] = settings.core.virtual_ports

    if system.date.has_real_date_time():
        attrs['date'] = int(time.time())

    if system.date.has_timezone_support():
        attrs['timezone'] = system.date.get_timezone()

    if system.net.has_wifi_support():
        wifi_config = system.net.get_wifi_config()
        attrs['wifi_ssid'] = wifi_config['ssid']
        attrs['wifi_key'] = wifi_config['psk']
        attrs['wifi_bssid'] = wifi_config['bssid']

        if wifi_config['bssid_current']:
            attrs['wifi_bssid_current'] = wifi_config['bssid_current']

        rssi = wifi_config['rssi_current']
        if rssi:
            rssi = int(rssi)
            if rssi >= WIFI_RSSI_EXCELLENT:
                strength = 3

            elif rssi >= WIFI_RSSI_GOOD:
                strength = 2

            elif rssi >= WIFI_RSSI_FAIR:
                strength = 1

            else:
                strength = 0

            attrs['wifi_signal_strength'] = strength

    if system.net.has_ip_support():
        ip_config = system.net.get_ip_config()
        attrs['ip_address'] = ip_config['address']
        attrs['ip_netmask'] = int(ip_config['netmask'] or 0)
        attrs['ip_gateway'] = ip_config['gateway']
        attrs['ip_dns'] = ip_config['dns']

        if 'address_current' in ip_config:
            attrs['ip_address_current'] = ip_config['address_current']
        if 'netmask_current' in ip_config:
            attrs['ip_netmask_current'] = int(ip_config['netmask_current']
                                              or 0)
        if 'gateway_current' in ip_config:
            attrs['ip_gateway_current'] = ip_config['gateway_current']
        if 'dns_current' in ip_config:
            attrs['ip_dns_current'] = ip_config['dns_current']

    attrs['cpu_usage'] = system.get_cpu_usage()
    attrs['mem_usage'] = system.get_mem_usage()

    if system.storage.has_storage_support():
        attrs['storage_usage'] = system.storage.get_storage_usage()

    if system.temperature.has_temperature_support():
        attrs['temperature'] = system.temperature.get_temperature()

    if system.battery.has_battery_support():
        attrs['battery_level'] = system.battery.get_battery_level()

    return attrs
Exemplo n.º 4
0
    'virtual': {
        'type': 'boolean',
        'optional': True
    },
    'online': {
        'type': 'boolean',
        'optional': True
    },
    'history_interval': {
        'type': 'number',
        'integer': True,
        'min': -1,
        'max': 2147483647,
        'optional': True,
        'modifiable': True,
        'enabled': lambda p: core_history.is_enabled()
    },
    'history_retention': {
        'type': 'number',
        'integer': True,
        'min': 0,
        'max': 2147483647,
        'optional': True,
        'modifiable': True,
        'enabled': lambda p: core_history.is_enabled()
    }
}


class PortError(Exception):
    pass
Exemplo n.º 5
0
async def cleanup_history() -> None:
    if history.is_enabled():
        logger.info('cleaning history')
        await history.cleanup()
Exemplo n.º 6
0
async def init_history() -> None:
    if history.is_enabled():
        logger.info('initializing history')
        await history.init()
Exemplo n.º 7
0
def _make_routing_table() -> List[URLSpec]:
    handlers_list = []

    # Frontend

    if settings.frontend.enabled:
        handlers_list += qui_tornado.make_routing_table()

        handlers_list += [
            URLSpec(r'^/api/frontend/dashboard/panels/?$', handlers.DashboardPanelsHandler),
            URLSpec(r'^/api/frontend/prefs/?$', handlers.PrefsHandler),
            URLSpec(r'^/api/frontend/?$', handlers.FrontendHandler),
        ]

    handlers_list += [
        # Device management
        URLSpec(r'^/api/device/?$', handlers.DeviceHandler),
        URLSpec(r'^/api/reset/?$', handlers.ResetHandler),
        URLSpec(r'^/api/access/?$', handlers.AccessHandler),

        # Port management
        URLSpec(r'^/api/ports/?$', handlers.PortsHandler),
        URLSpec(r'^/api/ports/(?P<port_id>[A-Za-z0-9_.-]+)/?$', handlers.PortHandler),

        # Port values
        URLSpec(r'^/api/ports/(?P<port_id>[A-Za-z0-9_.-]+)/value/?$', handlers.PortValueHandler),
    ]

    if settings.core.sequences_support:
        handlers_list += [
            URLSpec(r'^/api/ports/(?P<port_id>[A-Za-z0-9_.-]+)/sequence/?$', handlers.PortSequenceHandler)
        ]

    if history.is_enabled():
        handlers_list += [
            URLSpec(r'^/api/ports/(?P<port_id>[A-Za-z0-9_.-]+)/history/?$', handlers.PortHistoryHandler),
        ]

    if settings.core.backup_support:
        handlers_list += [
            URLSpec(r'^/api/backup/endpoints/?$', handlers.BackupEndpointsHandler)
        ]

    # Firmware

    if settings.system.fwupdate.driver:
        handlers_list += [
            URLSpec(r'^/api/firmware/?$', handlers.FirmwareHandler)
        ]

    # Slave devices management

    if settings.slaves.enabled:
        handlers_list += [
            URLSpec(r'^/api/devices/?$', handlers.SlaveDevicesHandler),
            URLSpec(r'^/api/devices/(?P<name>[A-Za-z0-9_-]+)/?$', handlers.SlaveDeviceHandler),
            URLSpec(r'^/api/devices/(?P<name>[A-Za-z0-9_-]+)/events/?$', handlers.SlaveDeviceEventsHandler),
            URLSpec(r'^/api/devices/(?P<name>[A-Za-z0-9_-]+)/forward/(?P<path>.+)$', handlers.SlaveDeviceForwardHandler)
        ]

        if is_discover_enabled():
            handlers_list += [
                URLSpec(r'^/api/discovered/?$', handlers.DiscoveredHandler),
                URLSpec(r'^/api/discovered/(?P<name>[A-Za-z0-9_-]+)/?$', handlers.DiscoveredDeviceHandler)
            ]

    # Notifications

    if settings.webhooks.enabled:
        handlers_list += [
            URLSpec(r'^/api/webhooks/?$', handlers.WebhooksHandler)
        ]

    if settings.core.listen_support:
        handlers_list += [
            URLSpec(r'^/api/listen/?$', handlers.ListenHandler)
        ]

    # Reverse API calls

    if settings.reverse.enabled:
        handlers_list += [
            URLSpec(r'^/api/reverse/?$', handlers.ReverseHandler)
        ]

    # System API calls

    if system.conf.can_write_conf_file():
        handlers_list += [
            URLSpec(r'^/api/system/?$', handlers.SystemHandler)
        ]

    # Introspection API

    if settings.debug:
        handlers_list += [
            URLSpec(r'^/api/introspect/?$', handlers.IntrospectHandler)
        ]

    # Default 404 API handler

    handlers_list += [
        URLSpec(r'^/api/.*$', handlers.NoSuchFunctionHandler)
    ]

    return handlers_list