Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create data class to update data via threads #1710

Merged
merged 1 commit into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions backend/systembridgebackend/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""System Bridge: Data"""
import asyncio
from collections.abc import Callable
from threading import Thread

from systembridgeshared.base import Base
from systembridgeshared.database import Database

from systembridgebackend.modules.update import Update


class UpdateThread(Thread):
"""Update thread"""

def __init__(
self,
database: Database,
updated_callback: Callable,
) -> None:
"""Initialize"""
super().__init__()
self._database = database
self._update = Update(self._database)
self._updated_callback = updated_callback

def run(self) -> None:
"""Run"""
asyncio.run(self._update.update_data(self._updated_callback))


class UpdateFrequentThread(Thread):
"""Update frequent thread"""

def __init__(
self,
database: Database,
updated_callback: Callable,
) -> None:
"""Initialize"""
super().__init__()
self._database = database
self._update = Update(self._database)
self._updated_callback = updated_callback

def run(self) -> None:
"""Run"""
asyncio.run(self._update.update_frequent_data(self._updated_callback))


class Data(Base):
"""Data"""

def __init__(
self,
database: Database,
updated_callback: Callable,
) -> None:
"""Initialize"""
super().__init__()
self._database = database
self._updated_callback = updated_callback

def request_update_data(self) -> None:
"""Request update data"""
thread = UpdateThread(
self._database,
self._updated_callback,
)
thread.start()

def request_update_frequent_data(self) -> None:
"""Request update frequent data"""
thread = UpdateFrequentThread(
self._database,
self._updated_callback,
)
thread.start()
9 changes: 4 additions & 5 deletions backend/systembridgebackend/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys

from sanic import Sanic
from sanic.models.handler_types import ListenerType
from sanic.request import Request
from sanic.response import HTTPResponse, json
from sanic_scheduler import SanicScheduler, task
Expand All @@ -16,9 +15,9 @@
from systembridgeshared.database import Database
from systembridgeshared.settings import Settings

from systembridgebackend.data import Data
from systembridgebackend.gui import GUIAttemptsExceededException, start_gui_threaded
from systembridgebackend.modules.listeners import Listeners
from systembridgebackend.modules.update import Update
from systembridgebackend.server.auth import ApiKeyAuthentication
from systembridgebackend.server.keyboard import handler_keyboard
from systembridgebackend.server.mdns import MDNSAdvertisement
Expand Down Expand Up @@ -67,7 +66,7 @@ def __init__(

SanicScheduler(self._server, utc=True)
self._listeners = Listeners(self._database, implemented_modules)
self._update = Update(self._database)
self._data = Data(self._database, self._data_updated)

auth = ApiKeyAuthentication(
app=self._server,
Expand Down Expand Up @@ -95,15 +94,15 @@ async def _after_startup(_) -> None:
)
async def _update_data(_) -> None:
"""Update data"""
await self._update.update_data(self._data_updated)
self._data.request_update_data()

@task(
start=timedelta(seconds=10),
period=timedelta(seconds=30),
)
async def _update_frequent_data(_) -> None:
"""Update frequent data"""
await self._update.update_frequent_data(self._data_updated)
self._data.request_update_frequent_data()

@auth.key_required
async def _handler_data_all(
Expand Down