2019-01-18 10:53:23 +01:00
|
|
|
"""
|
|
|
|
General projector code.
|
|
|
|
|
|
|
|
Functions that handel the registration of projector elements and the rendering
|
|
|
|
of the data to present it on the projector.
|
|
|
|
"""
|
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
from typing import Any, Callable, Dict, List
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
from .cache import element_cache
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2016-02-27 20:25:06 +01:00
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
AllData = Dict[str, Dict[int, Dict[str, Any]]]
|
|
|
|
ProjectorElementCallable = Callable[[Dict[str, Any], AllData], Dict[str, Any]]
|
2016-02-27 20:25:06 +01:00
|
|
|
|
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
projector_elements: Dict[str, ProjectorElementCallable] = {}
|
2015-06-12 21:08:57 +02:00
|
|
|
|
2017-08-30 00:07:54 +02:00
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
def register_projector_element(name: str, element: ProjectorElementCallable) -> None:
|
2017-08-30 00:07:54 +02:00
|
|
|
"""
|
2018-12-23 11:05:38 +01:00
|
|
|
Registers a projector element.
|
2017-08-30 00:07:54 +02:00
|
|
|
|
|
|
|
Has to be called in the app.ready method.
|
|
|
|
"""
|
2018-12-23 11:05:38 +01:00
|
|
|
projector_elements[name] = element
|
2017-08-30 00:07:54 +02:00
|
|
|
|
|
|
|
|
2019-01-10 15:06:10 +01:00
|
|
|
async def get_projector_data(
|
2018-12-23 11:05:38 +01:00
|
|
|
projector_ids: List[int] = None
|
2019-01-18 19:11:22 +01:00
|
|
|
) -> Dict[int, List[Dict[str, Any]]]:
|
2018-12-23 11:05:38 +01:00
|
|
|
"""
|
2019-01-18 19:11:22 +01:00
|
|
|
Calculates and returns the data for one or all projectors.
|
2019-01-18 10:53:23 +01:00
|
|
|
|
|
|
|
The keys of the returned data are the projector ids as int. When converted
|
|
|
|
to json, the numbers will changed to strings like "1".
|
|
|
|
|
2019-01-18 19:11:22 +01:00
|
|
|
The data for each projector is a list of elements.
|
2019-01-18 10:53:23 +01:00
|
|
|
|
2019-01-18 19:11:22 +01:00
|
|
|
Each element is a dict where the keys are "elements", "data". "elements"
|
|
|
|
contains the projector elements. It is the same as the projector elements in
|
|
|
|
the database. "data" contains all necessary data to render the projector
|
2019-01-18 10:53:23 +01:00
|
|
|
element. The key can also be "error" if there is a generall error for the
|
2019-01-18 19:11:22 +01:00
|
|
|
slide. In this case the values "elements" and "data" are optional.
|
2019-01-18 10:53:23 +01:00
|
|
|
|
|
|
|
The returned value looks like this:
|
|
|
|
|
|
|
|
projector_data = {
|
2019-01-18 19:11:22 +01:00
|
|
|
1: [
|
|
|
|
{
|
|
|
|
"element": {
|
2019-01-18 10:53:23 +01:00
|
|
|
"name": "agenda/item-list",
|
|
|
|
},
|
|
|
|
"data": {
|
|
|
|
"items": []
|
|
|
|
},
|
|
|
|
},
|
2019-01-18 19:11:22 +01:00
|
|
|
],
|
2019-01-18 10:53:23 +01:00
|
|
|
}
|
2017-08-30 00:07:54 +02:00
|
|
|
"""
|
2018-12-23 11:05:38 +01:00
|
|
|
if projector_ids is None:
|
|
|
|
projector_ids = []
|
|
|
|
|
|
|
|
all_data = await element_cache.get_all_full_data_ordered()
|
2019-01-18 19:11:22 +01:00
|
|
|
projector_data: Dict[int, List[Dict[str, Any]]] = {}
|
2018-12-23 11:05:38 +01:00
|
|
|
|
|
|
|
for projector_id, projector in all_data.get("core/projector", {}).items():
|
|
|
|
if projector_ids and projector_id not in projector_ids:
|
|
|
|
# only render the projector in question.
|
|
|
|
continue
|
|
|
|
|
2019-01-18 19:11:22 +01:00
|
|
|
if not projector["elements"]:
|
|
|
|
# Skip empty elements.
|
2018-12-23 11:05:38 +01:00
|
|
|
continue
|
|
|
|
|
2019-01-18 19:11:22 +01:00
|
|
|
projector_data[projector_id] = []
|
|
|
|
for element in projector["elements"]:
|
|
|
|
projector_element = projector_elements[element["name"]]
|
|
|
|
projector_data[projector_id].append(
|
|
|
|
{"data": projector_element(element, all_data), "element": element}
|
|
|
|
)
|
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
return projector_data
|
|
|
|
|
|
|
|
|
|
|
|
def get_config(all_data: AllData, key: str) -> Any:
|
2017-08-30 00:07:54 +02:00
|
|
|
"""
|
2019-01-18 10:53:23 +01:00
|
|
|
Returns a config value from all_data.
|
2018-12-23 11:05:38 +01:00
|
|
|
"""
|
|
|
|
from ..core.config import config
|
|
|
|
|
|
|
|
return all_data[config.get_collection_string()][config.get_key_to_id()[key]][
|
|
|
|
"value"
|
|
|
|
]
|