Skip to content

Commit

Permalink
Add Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
TBThomas56 committed Nov 23, 2023
1 parent fa3a6c9 commit cbe7faa
Showing 1 changed file with 87 additions and 7 deletions.
94 changes: 87 additions & 7 deletions src/eiger_fastcs/eiger_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

@dataclass
class EigerHandler:
"""
Handler for FastCS Attribute Creation
Dataclass that is called using the AttrR, AttrRW function.
Handler uses uri of detector to collect data for PVs
"""

name: str
update_period: float = 0.2

Expand All @@ -29,16 +36,44 @@ async def update(
attr: AttrR,
) -> None:
try:
# TODO: Async sleep?
response = await controller.connection.get(self.name)
await attr.set(response["value"])
except Exception as e:
print(f"update loop failed:{e}")


@dataclass
class LogicHandler:
"""
Handler for FastCS Attribute Creation
Dataclass that is called using the AttrR, AttrRW function.
Used for dynamically created attributes that are added for additional logic
"""

name: str

async def put(
self,
controller: "EigerController",
attr: AttrW,
value: Any,
) -> None:
await attr.set(value)

Check warning on line 63 in src/eiger_fastcs/eiger_controller.py

View check run for this annotation

Codecov / codecov/patch

src/eiger_fastcs/eiger_controller.py#L63

Added line #L63 was not covered by tests


class EigerController(Controller):
detector_state = AttrR(
String(),
handler=EigerHandler("detector/api/1.8.0/status/state"),
"""
Controller Class for Eiger Detector
Used for dynamic creation of variables useed in logic of the EigerFastCS backend.
Sets up all connections with the Simplon API to send and receive information
"""

manual_trigger = AttrRW(
Bool(),
handler=LogicHandler("manual trigger"),
)

def __init__(self, settings: IPConnectionSettings) -> None:
Expand All @@ -48,11 +83,31 @@ def __init__(self, settings: IPConnectionSettings) -> None:
asyncio.run(self.initialise())

async def connect(self) -> None:
"""Connection settigns with Eiger Detector using HTTP"""
self.connection = HTTPConnection(
self._ip_settings, headers={"Content-Type": "application/json"}
)

async def initialise(self) -> None:
"""
Method to create all PVs from the tickit-devices simulator/device at startup
Initialises the detector at the end of PV creation
Where to find PVs created in tickit-devices.
detector-status: eiger_status.py
detector-config: eiger_settings.py
stream-status: stream_status.py
stream-config: stream_config.py
monitor-status: monitor_status.py
monitor-config: monitor_config.py
Populates all PVs set in simulator/device in attributes dictionary
(Name of PV, FastCS Attribute) after checking for any clashes with
other subsystems and if the PV has already been created in the self
dictionary keys.
Initialises detector on startup.
"""
# Adding extra loop prior to backend loop creating the Attributes to be PVs
connection = HTTPConnection(
self._ip_settings, headers={"Content-Type": "application/json"}
Expand Down Expand Up @@ -145,28 +200,53 @@ async def initialise(self) -> None:
await connection.close()

async def close(self) -> None:
"""Closing HTTP connection with device"""
await self.connection.close()

async def arm(self):
"""Arming Detector called by the start acquisition button"""
await self.connection.put("detector/api/1.8.0/command/arm", "")

Check warning on line 208 in src/eiger_fastcs/eiger_controller.py

View check run for this annotation

Codecov / codecov/patch

src/eiger_fastcs/eiger_controller.py#L208

Added line #L208 was not covered by tests

@command
async def initialize(self):
"""Command to initialize Detector - will create a PVI button"""
await self.connection.put("detector/api/1.8.0/command/initialize", "")

@command
async def disarm(self):
"""Command to disarm Detector - will create a PVI button"""
await self.connection.put("detector/api/1.8.0/command/disarm", "")

@command
async def abort(self):
"""Command to abort any tasks Detector - will create a PVI button"""
await self.connection.put("detector/api/1.8.0/command/abort", "")

@command
async def arm(self):
await self.connection.put("detector/api/1.8.0/command/arm", "")

@command
async def cancel(self):
"""Command to cancel readings from Detector - will create a PVI button"""
await self.connection.put("detector/api/1.8.0/command/cancel", "")

@command
async def trigger(self):
"""
Command to trigger Detector when manual triggering is switched on.
will create a PVI button
"""
await self.connection.put("detector/api/1.8.0/command/trigger", "")

@command
async def start_acquisition(self):
"""
Command to start acquiring detector image.
Iterates through arming and triggering the detector if manual trigger off.
If manual triggers are on, it arms the detector, ready for triggering.
"""
await self.arm()

Check warning on line 246 in src/eiger_fastcs/eiger_controller.py

View check run for this annotation

Codecov / codecov/patch

src/eiger_fastcs/eiger_controller.py#L246

Added line #L246 was not covered by tests
# Current functionality is for ints triggering and multiple ntriggers for
# automatic triggering
if not self.manual_trigger._value:
for i in range(self.ntrigger._value):
print(f"Acquisition number: {i+1}")
await self.trigger()

Check warning on line 252 in src/eiger_fastcs/eiger_controller.py

View check run for this annotation

Codecov / codecov/patch

src/eiger_fastcs/eiger_controller.py#L249-L252

Added lines #L249 - L252 were not covered by tests

0 comments on commit cbe7faa

Please sign in to comment.