1- import json
21from abc import abstractmethod
32from collections import ChainMap
43from dataclasses import dataclass
54from typing import Any , Generic , TypeVar
6- from xml .etree import ElementTree
75from 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
118123class 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 ]:
0 commit comments