Skip to content
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
114 changes: 3 additions & 111 deletions src/fastcs/transports/epics/ca/ioc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from collections import Counter
from collections.abc import Awaitable
from typing import Any, Literal
from typing import Any

from softioc import alarm, builder, softioc
from softioc.asyncio_dispatcher import AsyncioDispatcher
Expand Down Expand Up @@ -41,10 +41,6 @@ def __init__(self, controller_apis: list[ControllerAPI], aliases: dict[str, str]

self._controller_apis = controller_apis
for controller_api in controller_apis:
root_pv_prefix = pv_prefix_from_path(controller_api.path)
_add_pvi_info(f"{root_pv_prefix}:PVI")
_add_sub_controller_pvi_info(controller_api)

_create_and_link_attribute_pvs(controller_api, aliases)
_create_and_link_command_pvs(controller_api, aliases)

Expand All @@ -54,69 +50,7 @@ def run(
) -> None:
dispatcher = AsyncioDispatcher(loop) # Needs running loop
builder.LoadDatabase()
softioc.iocInit(dispatcher)


def _add_pvi_info(
pvi: str,
parent_pvi: str = "",
name: str = "",
):
"""Add PVI metadata for a controller.

Args:
pvi: PVI PV of controller
parent_pvi: PVI PV of parent controller
name: Name to register controller with parent as

"""
# Create a record to attach the info tags to
record = builder.longStringIn(
f"{pvi}_PV",
initial_value=pvi,
DESC="The records in this controller",
)

# Create PVI PV in preparation for adding attribute info tags to it
q_group = {
pvi: {
"+id": "epics:nt/NTPVI:1.0",
"display.description": {"+type": "plain", "+channel": "DESC"},
"": {"+type": "meta", "+channel": "VAL"},
}
}
# If this controller has a parent, add a link in the parent to this controller
if parent_pvi and name:
q_group.update(
{
parent_pvi: {
f"value.{name}.d": {
"+channel": "VAL",
"+type": "plain",
"+trigger": f"value.{name}.d",
}
}
}
)

record.add_info("Q:group", q_group)


def _add_sub_controller_pvi_info(parent: ControllerAPI):
"""Add PVI references from controller to its sub controllers, recursively."""
parent_pvi = f"{pv_prefix_from_path(parent.path)}:PVI"

for child in parent.sub_apis.values():
child_pvi = f"{pv_prefix_from_path(child.path)}:PVI"
child_name = (
f"__{child.path[-1]}" # Sub-Controller of ControllerVector
if child.path[-1].isdigit()
else child.path[-1]
)

_add_pvi_info(child_pvi, parent_pvi, child_name.lower())

_add_sub_controller_pvi_info(child)
softioc.iocInit(dispatcher, enable_pva=False)
Comment thread
shihab-dls marked this conversation as resolved.


def _create_and_link_attribute_pvs(
Expand Down Expand Up @@ -164,30 +98,26 @@ def _create_and_link_attribute_pvs(
_create_and_link_read_pv(
pv_prefix,
f"{pv_name}{RBV_SUFFIX}",
attr_name,
alias_rbv,
attribute,
)
_create_and_link_write_pv(
pv_prefix,
pv_name,
attr_name,
alias,
attribute,
)
case AttrR():
_create_and_link_read_pv(
pv_prefix,
pv_name,
attr_name,
alias,
attribute,
)
case AttrW():
_create_and_link_write_pv(
pv_prefix,
pv_name,
attr_name,
alias,
attribute,
)
Expand All @@ -196,7 +126,6 @@ def _create_and_link_attribute_pvs(
def _create_and_link_read_pv(
pv_prefix: str,
pv_name: str,
attr_name: str,
alias: str | None,
attribute: AttrR[DType_T],
) -> None:
Expand All @@ -213,15 +142,12 @@ async def async_record_set(value: DType_T):

_add_alias(record, alias)

_add_attr_pvi_info(record, pv_prefix, attr_name, "r")

attribute.add_on_update_callback(async_record_set)


def _create_and_link_write_pv(
pv_prefix: str,
pv_name: str,
attr_name: str,
alias: str | None,
attribute: AttrW[DType_T],
):
Expand All @@ -244,8 +170,6 @@ async def set_setpoint_without_process(value: DType_T):

_add_alias(record, alias)

_add_attr_pvi_info(record, pv_prefix, attr_name, "w")

attribute.add_sync_setpoint_callback(set_setpoint_without_process)


Expand All @@ -269,14 +193,13 @@ def _create_and_link_command_pvs(
_create_and_link_command_pv(
pv_prefix,
pv_name,
attr_name,
alias,
method,
)


def _create_and_link_command_pv(
pv_prefix: str, pv_name: str, attr_name: str, alias: str | None, method: Command
pv_prefix: str, pv_name: str, alias: str | None, method: Command
) -> None:
pv = f"{pv_prefix}:{pv_name}"

Expand All @@ -295,37 +218,6 @@ async def wrapped_method(_: Any):

_add_alias(record, alias)

_add_attr_pvi_info(record, pv_prefix, attr_name, "x")


def _add_attr_pvi_info(
record: RecordWrapper,
prefix: str,
name: str,
access_mode: Literal["r", "w", "rw", "x"],
):
"""Add an info tag to a record to include it in the PVI for the controller.

Args:
record: Record to add info tag to
prefix: PV prefix of controller
name: Name of parameter to add to PVI
access_mode: Access mode of parameter

"""
record.add_info(
"Q:group",
{
f"{prefix}:PVI": {
f"value.{name}.{access_mode}": {
"+channel": "NAME",
"+type": "plain",
"+trigger": f"value.{name}.{access_mode}",
}
}
},
)


def _add_alias(record: RecordWrapper, alias: str | None):
if alias is not None:
Expand Down
Loading
Loading