How to use the ledfx.api.RestEndpoint function in LedFx

To help you get started, weโ€™ve selected a few LedFx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ahodges9 / LedFx / ledfx / api / info.py View on Github external
from ledfx.api import RestEndpoint
from ledfx.consts import PROJECT_VERSION
from aiohttp import web
import logging
import json

_LOGGER = logging.getLogger(__name__)

class InfoEndpoint(RestEndpoint):

    ENDPOINT_PATH = "/api/info"

    async def get(self) -> web.Response:
        response = {
            'url': self._ledfx.base_url,
            'name': 'LedFx Controller',
            'version': PROJECT_VERSION,
            'debug_mode': True
        }

        return web.Response(text=json.dumps(response), status=200)
github ahodges9 / LedFx / ledfx / api / websocket.py View on Github external
vol.Required('id'): vol.Coerce(int),
    vol.Required('type'): str,
}, extra=vol.ALLOW_EXTRA)

# TODO: Have a more well defined registration and a more componetized solution.
# Could do something like have Device actually provide the handler for Device 
# related functionality. This would allow easy access to internal workings and
# events. 
websocket_handlers = {}
def websocket_handler(type):
    def function(func):
        websocket_handlers[type] = func
        return func
    return function

class WebsocketEndpoint(RestEndpoint):

    ENDPOINT_PATH = "/api/websocket"

    async def get(self, request) -> web.Response:
        return await WebsocketConnection(self._ledfx).handle(request)

class WebsocketConnection:

    def __init__(self, ledfx):
        self._ledfx = ledfx
        self._socket = None
        self._listeners = {}
        self._receiver_task = None
        self._sender_task = None
        self._sender_queue = asyncio.Queue(maxsize=MAX_PENDING_MESSAGES, loop=ledfx.loop)
github ahodges9 / LedFx / ledfx / api / device_effects.py View on Github external
from ledfx.api import RestEndpoint
from aiohttp import web
import logging
import json

_LOGGER = logging.getLogger(__name__)

class EffectsEndpoint(RestEndpoint):

    ENDPOINT_PATH = "/api/devices/{device_id}/effects"

    async def get(self, device_id) -> web.Response:
        device = self._ledfx.devices.get(device_id)
        if device is None:
            response = { 'not found': 404 }
            return web.Response(text=json.dumps(response), status=404)

        # Get the active effect
        response = { 'effect' : {}}
        if device.active_effect:
            effect_response = {}
            effect_response['config'] = device.active_effect.config
            effect_response['name'] = device.active_effect.name
            effect_response['type'] = device.active_effect.type
github ahodges9 / LedFx / ledfx / api / devices.py View on Github external
from ledfx.config import save_config
from ledfx.api import RestEndpoint
from ledfx.utils import generate_id
from aiohttp import web
import logging
import json

_LOGGER = logging.getLogger(__name__)

class DevicesEndpoint(RestEndpoint):
    """REST end-point for querying and managing devices"""

    ENDPOINT_PATH = "/api/devices"

    async def get(self) -> web.Response:
        response = { 'status' : 'success' , 'devices' : {}}
        for device in self._ledfx.devices.values():
            response['devices'][device.id] = { 'config': device.config, 'id': device.id, 'type': device.type, 'effect' : {} }
            if device.active_effect:
                effect_response = {}
                effect_response['config'] = device.active_effect.config
                effect_response['name'] = device.active_effect.name
                effect_response['type'] = device.active_effect.type
                response['devices'][device.id]['effect'] = effect_response

        return web.Response(text=json.dumps(response), status=200)
github ahodges9 / LedFx / ledfx / api / schema.py View on Github external
from ledfx.api import RestEndpoint
from aiohttp import web
from ledfx.api.utils import convertToJsonSchema
import logging
import json

_LOGGER = logging.getLogger(__name__)

class SchemaEndpoint(RestEndpoint):

    ENDPOINT_PATH = "/api/schema"

    async def get(self) -> web.Response:
        response = {
            'devices': {},
            'effects': {}
        }

        # Generate all the device schema
        for device_type, device in self._ledfx.devices.classes().items():
            response['devices'][device_type] = {
                'schema': convertToJsonSchema(device.schema()),
                'id': device_type
            }
github ahodges9 / LedFx / ledfx / api / device.py View on Github external
from ledfx.config import save_config
from ledfx.api import RestEndpoint
from aiohttp import web
import logging
import json

_LOGGER = logging.getLogger(__name__)

class DeviceEndpoint(RestEndpoint):
    """REST end-point for querying and managing devices"""

    ENDPOINT_PATH = "/api/devices/{device_id}"

    async def get(self, device_id) -> web.Response:
        device = self._ledfx.devices.get(device_id)
        if device is None:
            response = { 'not found': 404 }
            return web.Response(text=json.dumps(response), status=404)

        response = device.config
        return web.Response(text=json.dumps(response), status=200)

    async def put(self, device_id, request) -> web.Response:
        device = self._ledfx.devices.get(device_id)
        if device is None:
github ahodges9 / LedFx / ledfx / api / config.py View on Github external
from ledfx.api import RestEndpoint
from aiohttp import web
import logging
import json

_LOGGER = logging.getLogger(__name__)

class ConfigEndpoint(RestEndpoint):

    ENDPOINT_PATH = "/api/config"

    async def get(self) -> web.Response:
        response = {
            'config': self._ledfx._config
        }

        return web.Response(text=json.dumps(response), status=200)
github ahodges9 / LedFx / ledfx / api / devices.py View on Github external
from ledfx.config import save_config
from ledfx.api import RestEndpoint
from ledfx.utils import generate_id
from aiohttp import web
import logging
import json

_LOGGER = logging.getLogger(__name__)

class DevicesEndpoint(RestEndpoint):
    """REST end-point for querying and managing devices"""

    ENDPOINT_PATH = "/api/devices"

    async def get(self) -> web.Response:
        response = { 'status' : 'success' , 'devices' : {}}
        for device in self._ledfx.devices.values():
            response['devices'][device.id] = { 'config': device.config, 'id': device.id, 'type': device.type }

        return web.Response(text=json.dumps(response), status=200)

    async def post(self, request) -> web.Response:
        data = await request.json()

        device_config = data.get('config')
        if device_config is None:
github ahodges9 / LedFx / ledfx / api / effect.py View on Github external
from ledfx.api import RestEndpoint
from aiohttp import web
import logging
import json

_LOGGER = logging.getLogger(__name__)

class EffectEndpoint(RestEndpoint):

    ENDPOINT_PATH = "/api/effects/{effect_id}"

    async def get(self, effect_id) -> web.Response:
        effect = self._ledfx.effects.get_class(effect_id)
        if effect is None:
            response = { 'not found': 404 }
            return web.Response(text=json.dumps(response), status=404)

        response = { 'schema' : str(effect.schema()) }
        return web.Response(text=json.dumps(response), status=200)
github ahodges9 / LedFx / ledfx / api / device.py View on Github external
from ledfx.config import save_config
from ledfx.api import RestEndpoint
from aiohttp import web
import logging
import json

_LOGGER = logging.getLogger(__name__)

class DeviceEndpoint(RestEndpoint):
    """REST end-point for querying and managing devices"""

    ENDPOINT_PATH = "/api/devices/{device_id}"

    async def get(self, device_id) -> web.Response:
        device = self._ledfx.devices.get(device_id)
        if device is None:
            response = { 'not found': 404 }
            return web.Response(text=json.dumps(response), status=404)

        response = device.config
        return web.Response(text=json.dumps(response), status=200)

    async def put(self, device_id, request) -> web.Response:
        device = self._ledfx.devices.get(device_id)
        if device is None: