Skip to content

Commit fe5c9c0

Browse files
authored
Use config server for OAV config files (#1748)
* Use config server for OAV config json * Convert OAV test config files to json * Read OAV config correctly * Fix for github.com/DiamondLightSource/daq-config-server/pull/156 * Inject config client into OAVConfig and OAVConfigBeamCentre * Rename test file * Dont use get_beamline_parameters in device factories * Fix tests
1 parent af1430f commit fe5c9c0

21 files changed

Lines changed: 329 additions & 241 deletions

src/dodal/beamlines/aithre.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from functools import cache
2+
3+
from daq_config_server import ConfigClient
4+
15
from dodal.device_manager import DeviceManager
26
from dodal.devices.aithre_lasershaping.goniometer import Goniometer
37
from dodal.devices.aithre_lasershaping.laser_robot import LaserRobot
@@ -14,6 +18,12 @@
1418
devices = DeviceManager()
1519

1620

21+
@devices.fixture
22+
@cache
23+
def config_client() -> ConfigClient:
24+
return ConfigClient()
25+
26+
1727
@devices.factory()
1828
def goniometer() -> Goniometer:
1929
return Goniometer(
@@ -31,12 +41,16 @@ def robot() -> LaserRobot:
3141

3242

3343
@devices.factory()
34-
def oav(params: OAVConfigBeamCentre | None = None) -> OAVBeamCentreFile:
44+
def oav(
45+
config_client: ConfigClient, params: OAVConfigBeamCentre | None = None
46+
) -> OAVBeamCentreFile:
3547
config = (
3648
params
3749
if params is not None
3850
else OAVConfigBeamCentre(
39-
zoom_params_file=ZOOM_PARAMS_FILE, display_config_file=DISPLAY_CONFIG
51+
zoom_params_file=ZOOM_PARAMS_FILE,
52+
display_config_file=DISPLAY_CONFIG,
53+
config_client=config_client,
4054
)
4155
)
4256
return OAVBeamCentreFile(

src/dodal/beamlines/i03.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from ophyd_async.fastcs.panda import HDFPanda
77
from yarl import URL
88

9-
from dodal.common.beamlines.beamline_parameters import get_beamline_parameters
109
from dodal.common.beamlines.beamline_utils import set_beamline as set_utils_beamline
1110
from dodal.common.beamlines.beamline_utils import set_config_client, set_path_provider
1211
from dodal.common.beamlines.commissioning_mode import set_commissioning_signal
@@ -69,6 +68,9 @@
6968
"/dls_sw/i03/software/gda/configurations/i03-config/xml/jCameraManZoomLevels.xml"
7069
)
7170
DISPLAY_CONFIG = "/dls_sw/i03/software/gda_versions/var/display.configuration"
71+
BEAMLINE_PARAMETERS_PATH = (
72+
"/dls_sw/i03/software/daq_configuration/domain/beamlineParameters"
73+
)
7274
DAQ_CONFIGURATION_PATH = "/dls_sw/i03/software/daq_configuration"
7375
I03_CONFIG_SERVER_ENDPOINT = "https://i03-daq-config.diamond.ac.uk"
7476

@@ -108,8 +110,9 @@ def daq_configuration_path() -> str:
108110

109111

110112
@devices.factory()
111-
def aperture_scatterguard() -> ApertureScatterguard:
112-
params = get_beamline_parameters(BL)
113+
def aperture_scatterguard(config_client: ConfigClient) -> ApertureScatterguard:
114+
print(BEAMLINE_PARAMETERS_PATH)
115+
params = config_client.get_file_contents(BEAMLINE_PARAMETERS_PATH, dict)
113116
return ApertureScatterguard(
114117
aperture_prefix=f"{PREFIX.beamline_prefix}-MO-MAPT-01:",
115118
scatterguard_prefix=f"{PREFIX.beamline_prefix}-MO-SCAT-01:",
@@ -127,10 +130,12 @@ def attenuator() -> BinaryFilterAttenuator:
127130

128131

129132
@devices.factory()
130-
def beamstop() -> Beamstop:
133+
def beamstop(config_client: ConfigClient) -> Beamstop:
131134
return Beamstop(
132135
prefix=f"{PREFIX.beamline_prefix}-MO-BS-01:",
133-
beamline_parameters=get_beamline_parameters(BL),
136+
beamline_parameters=config_client.get_file_contents(
137+
BEAMLINE_PARAMETERS_PATH, dict
138+
),
134139
)
135140

136141

@@ -205,11 +210,13 @@ def panda_fast_grid_scan() -> PandAFastGridScan:
205210

206211
@devices.factory()
207212
def oav(
213+
config_client: ConfigClient,
208214
params: OAVConfigBeamCentre | None = None,
209215
) -> OAVBeamCentreFile:
210216
return OAVBeamCentreFile(
211217
prefix=f"{PREFIX.beamline_prefix}-DI-OAV-01:",
212-
config=params or OAVConfigBeamCentre(ZOOM_PARAMS_FILE, DISPLAY_CONFIG),
218+
config=params
219+
or OAVConfigBeamCentre(ZOOM_PARAMS_FILE, DISPLAY_CONFIG, config_client),
213220
)
214221

215222

@@ -368,11 +375,13 @@ def fluorescence_det_motion() -> FluorescenceDetector:
368375

369376

370377
@devices.factory()
371-
def scintillator(aperture_scatterguard: ApertureScatterguard) -> Scintillator:
378+
def scintillator(
379+
aperture_scatterguard: ApertureScatterguard, config_client: ConfigClient
380+
) -> Scintillator:
372381
return Scintillator(
373382
f"{PREFIX.beamline_prefix}-MO-SCIN-01:",
374383
Reference(aperture_scatterguard),
375-
get_beamline_parameters(BL),
384+
config_client.get_file_contents(BEAMLINE_PARAMETERS_PATH, dict),
376385
)
377386

378387

src/dodal/beamlines/i04.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from daq_config_server import ConfigClient
44
from ophyd_async.core import Reference
55

6-
from dodal.common.beamlines.beamline_parameters import get_beamline_parameters
76
from dodal.common.beamlines.beamline_utils import set_beamline as set_utils_beamline
87
from dodal.common.beamlines.beamline_utils import set_config_client
98
from dodal.device_manager import DeviceManager
@@ -60,6 +59,9 @@
6059
# Use BlueAPI scratch until https://github.com/DiamondLightSource/mx-bluesky/issues/1097 is done
6160
ZOOM_PARAMS_FILE = "/dls_sw/i04/software/bluesky/scratch/jCameraManZoomLevels.xml"
6261
DISPLAY_CONFIG = "/dls_sw/i04/software/bluesky/scratch/display.configuration"
62+
BEAMLINE_PARAMETERS_PATH = (
63+
"/dls_sw/i04/software/daq_configuration/domain/beamlineParameters"
64+
)
6365
DAQ_CONFIGURATION_PATH = "/dls_sw/i04/software/daq_configuration"
6466
I04_CONFIG_SERVER_ENDPOINT = "https://i04-daq-config.diamond.ac.uk"
6567

@@ -108,10 +110,12 @@ def ipin() -> IPin:
108110

109111

110112
@devices.factory()
111-
def beamstop() -> Beamstop:
113+
def beamstop(config_client: ConfigClient) -> Beamstop:
112114
return Beamstop(
113115
f"{PREFIX.beamline_prefix}-MO-BS-01:",
114-
beamline_parameters=get_beamline_parameters(BL),
116+
beamline_parameters=config_client.get_file_contents(
117+
BEAMLINE_PARAMETERS_PATH, dict
118+
),
115119
)
116120

117121

@@ -159,8 +163,8 @@ def backlight() -> Backlight:
159163

160164

161165
@devices.factory()
162-
def aperture_scatterguard() -> ApertureScatterguard:
163-
params = get_beamline_parameters(BL)
166+
def aperture_scatterguard(config_client: ConfigClient) -> ApertureScatterguard:
167+
params = config_client.get_file_contents(BEAMLINE_PARAMETERS_PATH, dict)
164168
return ApertureScatterguard(
165169
aperture_prefix=f"{PREFIX.beamline_prefix}-MO-MAPT-01:",
166170
scatterguard_prefix=f"{PREFIX.beamline_prefix}-MO-SCAT-01:",
@@ -216,18 +220,22 @@ def zebra() -> Zebra:
216220

217221

218222
@devices.factory()
219-
def oav(params: OAVConfig | None = None) -> OAVBeamCentrePV:
223+
def oav(
224+
config_client: ConfigClient, params: OAVConfig | None = None
225+
) -> OAVBeamCentrePV:
220226
return OAVBeamCentrePV(
221227
prefix=f"{PREFIX.beamline_prefix}-DI-OAV-01:",
222-
config=params or OAVConfig(ZOOM_PARAMS_FILE),
228+
config=params or OAVConfig(ZOOM_PARAMS_FILE, config_client),
223229
)
224230

225231

226232
@devices.factory()
227-
def oav_full_screen(params: OAVConfig | None = None) -> OAVBeamCentrePV:
233+
def oav_full_screen(
234+
config_client: ConfigClient, params: OAVConfig | None = None
235+
) -> OAVBeamCentrePV:
228236
return OAVBeamCentrePV(
229237
prefix=f"{PREFIX.beamline_prefix}-DI-OAV-01:",
230-
config=params or OAVConfig(ZOOM_PARAMS_FILE),
238+
config=params or OAVConfig(ZOOM_PARAMS_FILE, config_client),
231239
overlay_channel=3,
232240
mjpeg_prefix="XTAL",
233241
)
@@ -292,11 +300,13 @@ def pin_tip_detection() -> PinTipDetection:
292300

293301

294302
@devices.factory()
295-
def scintillator(aperture_scatterguard: ApertureScatterguard) -> Scintillator:
303+
def scintillator(
304+
aperture_scatterguard: ApertureScatterguard, config_client: ConfigClient
305+
) -> Scintillator:
296306
return Scintillator(
297307
f"{PREFIX.beamline_prefix}-MO-SCIN-01:",
298308
Reference(aperture_scatterguard),
299-
get_beamline_parameters(BL),
309+
config_client.get_file_contents(BEAMLINE_PARAMETERS_PATH, dict),
300310
)
301311

302312

src/dodal/beamlines/i19_1.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from functools import cache
2+
3+
from daq_config_server import ConfigClient
4+
15
from dodal.common.beamlines.beamline_utils import (
26
set_beamline as set_utils_beamline,
37
)
@@ -48,6 +52,12 @@
4852
devices = DeviceManager()
4953

5054

55+
@devices.fixture
56+
@cache
57+
def config_client() -> ConfigClient:
58+
return ConfigClient()
59+
60+
5161
@devices.factory()
5262
def attenuator_motor_squad() -> AttenuatorMotorSquad:
5363
return AttenuatorMotorSquad(
@@ -62,7 +72,7 @@ def beamstop() -> BeamStop:
6272

6373
@devices.fixture
6474
def oav_config() -> OAVConfigBeamCentre:
65-
return OAVConfigBeamCentre(ZOOM_PARAMS_FILE, DISPLAY_CONFIG)
75+
return OAVConfigBeamCentre(ZOOM_PARAMS_FILE, DISPLAY_CONFIG, config_client())
6676

6777

6878
@devices.factory()

src/dodal/beamlines/i24.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ def path_provider() -> PathProvider:
6868
)
6969

7070

71+
@devices.fixture
72+
@cache
73+
def config_client() -> ConfigClient:
74+
return ConfigClient()
75+
76+
7177
@devices.factory()
7278
def attenuator() -> EnumFilterAttenuator:
7379
return EnumFilterAttenuator(
@@ -110,10 +116,10 @@ def pmac() -> PMAC:
110116

111117

112118
@devices.factory()
113-
def oav() -> OAVBeamCentreFile:
119+
def oav(config_client) -> OAVBeamCentreFile:
114120
return OAVBeamCentreFile(
115121
prefix=f"{PREFIX.beamline_prefix}-DI-OAV-01:",
116-
config=OAVConfigBeamCentre(ZOOM_PARAMS_FILE, DISPLAY_CONFIG),
122+
config=OAVConfigBeamCentre(ZOOM_PARAMS_FILE, DISPLAY_CONFIG, config_client),
117123
)
118124

119125

src/dodal/devices/oav/oav_parameters.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import json
21
from abc import abstractmethod
32
from collections import ChainMap
43
from dataclasses import dataclass
54
from typing import Any, Generic, TypeVar
6-
from xml.etree import ElementTree
75
from xml.etree.ElementTree import Element
86

7+
from daq_config_server import ConfigClient
8+
from daq_config_server.models import DisplayConfig
9+
910
# GDA currently assumes this aspect ratio for the OAV window size.
1011
# For some beamline this doesn't affect anything as the actual OAV aspect ratio
1112
# matches. Others need to take it into account to rescale the values stored in
@@ -34,20 +35,24 @@ def __init__(
3435
):
3536
self.oav_config_json: str = oav_config_json
3637
self.context = context
38+
config_server = ConfigClient(url="https://daq-config.diamond.ac.uk")
3739

38-
self.global_params, self.context_dicts = self.load_json(self.oav_config_json)
40+
self.global_params, self.context_dicts = self.load_json(
41+
config_server, self.oav_config_json
42+
)
3943
self.active_params: ChainMap = ChainMap(
4044
self.context_dicts[self.context], self.global_params
4145
)
4246
self.update_self_from_current_context()
4347

4448
@staticmethod
45-
def load_json(filename: str) -> tuple[dict[str, Any], dict[str, dict]]:
46-
"""Loads the json from the specified file, and returns a dict with all the
49+
def load_json(
50+
config_server: ConfigClient, filename: str
51+
) -> tuple[dict[str, Any], dict[str, dict]]:
52+
"""Loads the specified file from the config server, and returns a dict with all the
4753
individual top-level k-v pairs, and one with all the subdicts.
4854
"""
49-
with open(filename) as f:
50-
raw_params: dict[str, Any] = json.load(f)
55+
raw_params: dict[str, Any] = config_server.get_file_contents(filename, dict)
5156
global_params = {
5257
k: raw_params.pop(k)
5358
for k, v in list(raw_params.items())
@@ -116,21 +121,20 @@ class ZoomParamsCrosshair(ZoomParams):
116121

117122

118123
class OAVConfigBase(Generic[ParamType]):
119-
def __init__(self, zoom_params_file: str):
120-
self.zoom_params = self._get_zoom_params(zoom_params_file)
121-
122-
def _get_zoom_params(self, zoom_params_file: str):
123-
tree = ElementTree.parse(zoom_params_file)
124-
root = tree.getroot()
125-
return root.findall(".//zoomLevel")
124+
def __init__(self, zoom_params_file: str, config_client: ConfigClient):
125+
self.zoom_params = config_client.get_file_contents(zoom_params_file, dict)[
126+
"JCameraManSettings"
127+
]
126128

127129
def _read_zoom_params(self) -> dict:
128130
um_per_pix = {}
129-
for node in self.zoom_params:
130-
zoom = str(_get_element_as_float(node, "level"))
131-
um_pix_x = _get_element_as_float(node, "micronsPerXPixel")
132-
um_pix_y = _get_element_as_float(node, "micronsPerYPixel")
133-
um_per_pix[zoom] = (um_pix_x, um_pix_y)
131+
zoom_levels: list[dict] = self.zoom_params["levels"]["zoomLevel"]
132+
for level in zoom_levels:
133+
zoom = level["level"]
134+
um_per_pix[zoom] = (
135+
float(level["micronsPerXPixel"]),
136+
float(level["micronsPerYPixel"]),
137+
)
134138
return um_per_pix
135139

136140
@abstractmethod
@@ -157,23 +161,17 @@ def __init__(
157161
self,
158162
zoom_params_file: str,
159163
display_config_file: str,
164+
config_client: ConfigClient,
160165
):
161-
self.display_config = self._get_display_config(display_config_file)
162-
super().__init__(zoom_params_file)
163-
164-
def _get_display_config(self, display_config_file: str):
165-
with open(display_config_file) as f:
166-
file_lines = f.readlines()
167-
return file_lines
166+
self.display_config = config_client.get_file_contents(
167+
display_config_file, DisplayConfig
168+
)
169+
super().__init__(zoom_params_file, config_client)
168170

169171
def _read_display_config(self) -> dict:
170172
crosshairs = {}
171-
for i in range(len(self.display_config)):
172-
if self.display_config[i].startswith("zoomLevel"):
173-
zoom = self.display_config[i].split(" = ")[1].strip()
174-
x = int(self.display_config[i + 1].split(" = ")[1])
175-
y = int(self.display_config[i + 2].split(" = ")[1])
176-
crosshairs[zoom] = (x, y)
173+
for zoom, values in self.display_config.zoom_levels.items():
174+
crosshairs[str(zoom)] = (values.crosshair_x, values.crosshair_y)
177175
return crosshairs
178176

179177
def get_parameters(self) -> dict[str, ZoomParamsCrosshair]:

system_tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# Add run_engine to be used in tests
2-
pytest_plugins = ["dodal.testing.fixtures.run_engine"]
2+
pytest_plugins = [
3+
"dodal.testing.fixtures.run_engine",
4+
"dodal.testing.fixtures.config_server",
5+
]

0 commit comments

Comments
 (0)