Skip to content

Commit

Permalink
Add dynamic parameter generation for all Subsystems
Browse files Browse the repository at this point in the history
  • Loading branch information
TBThomas56 committed Nov 8, 2023
1 parent ea24eaf commit ffa92f3
Showing 1 changed file with 67 additions and 49 deletions.
116 changes: 67 additions & 49 deletions src/eiger_fastcs/eiger_controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
from dataclasses import dataclass
from typing import Any
from typing import Any, Mapping
from attr import Attribute

from fastcs.attributes import AttrR, AttrRW, AttrW
from fastcs.connections import HTTPConnection, IPConnectionSettings
Expand Down Expand Up @@ -30,7 +31,7 @@ async def update(
response = await controller.connection.get(self.name)
await attr.set(response["value"])
except Exception as e:
print(e)
print(f"update loop failed:{e}")


class EigerController(Controller):
Expand All @@ -50,53 +51,70 @@ async def initialise(self) -> None:
connection = HTTPConnection(
self._ip_settings, headers={"Content-Type": "application/json"}
)
detector_status = await connection.get("detector/api/1.8.0/status/keys")
requests = [
connection.get(f"detector/api/1.8.0/status/{item}")
for item in detector_status["value"]
]
values = await asyncio.gather(*requests)

for i in range(len(detector_status["value"])):
# FastCS Types
match values[i]["value_type"]:
case "float":
datatype = Float()
case "int":
datatype = Int()
case "bool":
datatype = Bool()
case "str" | "datetime" | "State":
datatype = String()

# append the names of criteria to the values list
values[i] = {**{"name": detector_status["value"][i]}, **values[i]}

# Set Attributes
match values[i]["access_mode"]:
case "rw":
setattr(
self,
values[i]["name"],
AttrRW(
datatype,
handler=EigerHandler(
f'detector/api/1.8.0/status/{values[i]["name"]}'
),
),
)

case "r":
setattr(
self,
values[i]["name"],
AttrR(
datatype,
handler=EigerHandler(
f'detector/api/1.8.0/status/{values[i]["name"]}'
),
),
)
subsystems = ["detector", "stream", "monitor"]
modes = ["status", "config"]
PVs_compl = {}
attributes: Mapping[str, Attribute] = {}

for index, subsystem in enumerate(subsystems):
for mode in modes:
response = await connection.get(f"{subsystem}/api/1.8.0/{mode}/keys")
subsystem_parameters = response["value"]
requests = [
connection.get(f"{subsystem}/api/1.8.0/{mode}/{item}")
for item in subsystem_parameters
]
values = await asyncio.gather(*requests)

for parameter_name, parameter in zip(subsystem_parameters, values):
# FastCS Types
match parameter["value_type"]:
case "float":
datatype = Float()
case "int":
datatype = Int()
case "bool":
datatype = Bool()
case "string" | "datetime" | "State" | "string[]":
datatype = String()
case _:
print(f"Could not process {parameter_name}")

# finding appropriate naming to ensure repeats are not ovewritten
if parameter_name in list(attributes.keys()):
# Adding original instance of the duplicate into dictionary to rename original instance in attributes later
if parameter_name not in list(PVs_compl.keys()):
PVs_compl[
parameter_name
] = f"{subsystems[index-1]}_{parameter_name}"
name = f"{subsystem}_{parameter_name}"
else:
name = parameter_name

# mapping attributes using access mode metadata
match parameter["access_mode"]:
case "r":
attributes[name] = AttrR(
datatype,
handler=EigerHandler(
f"{subsystem}/api/1.8.0/{mode}/{parameter_name}"
),
)
case "rw":
attributes[name] = AttrRW(
datatype,
handler=EigerHandler(
f"{subsystem}/api/1.8.0/{mode}/{parameter_name}"
),
)

# Renaming original instance of duplicate in Attribute
for dupl in PVs_compl:
attributes[PVs_compl[dupl]] = attributes.pop(dupl)

for names in list(attributes.keys()):
setattr(self, names, attributes[names])

await connection.close()

async def close(self) -> None:
Expand Down

0 comments on commit ffa92f3

Please sign in to comment.