// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #include "DectrisDetectorWrapper.h" DetectorState DectrisDetectorWrapper::GetState() const { if (!simplon) return DetectorState::NOT_CONNECTED; SimplonState simplon_state; try { simplon_state = simplon->GetState(); } catch (const JFJochException &e) { logger.Warning("Cannot read detector state: {}", e.what()); return DetectorState::NOT_CONNECTED; } switch (simplon_state) { case SimplonState::Ready: return DetectorState::WAITING; case SimplonState::Initialize: case SimplonState::Configure: case SimplonState::Acquire: case SimplonState::Test: return DetectorState::BUSY; case SimplonState::Idle: return DetectorState::IDLE; default: case SimplonState::Error: case SimplonState::Na: return DetectorState::ERROR; } } void DectrisDetectorWrapper::CheckBusy() { switch(GetState()) { case DetectorState::BUSY: case DetectorState::WAITING: throw JFJochException(JFJochExceptionCategory::Detector, "Detector busy"); case DetectorState::NOT_CONNECTED: throw JFJochException(JFJochExceptionCategory::Detector, "Detector not connected"); default: break; } } void DectrisDetectorWrapper::CheckBusyOrError() { switch(GetState()) { case DetectorState::ERROR: throw JFJochCriticalException("Detector in error state"); case DetectorState::BUSY: case DetectorState::WAITING: throw JFJochException(JFJochExceptionCategory::Detector, "Detector busy"); case DetectorState::NOT_CONNECTED: throw JFJochException(JFJochExceptionCategory::Detector, "Detector not connected"); default: break; } } void DectrisDetectorWrapper::Initialize(DiffractionExperiment &experiment, const std::vector &net_config) { if (experiment.GetDetectorType() != DetectorType::DECTRIS) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "DectrisDetectorWrapper needs DECTRIS detector"); if (experiment.GetDetectorModuleHostname().empty()) { simplon.reset(); return; } if (experiment.GetDetectorModuleHostname().size() != 1) throw JFJochException(JFJochExceptionCategory::Detector, "DectrisDetectorWrapper needs DECTRIS detector"); simplon = std::make_unique(experiment.GetDetectorSetup().GetDetectorModuleHostname()[0]); CheckBusy(); simplon->InitializeDetector(experiment.Detector()); } void DectrisDetectorWrapper::Configure(const DiffractionExperiment &experiment) { CheckBusyOrError(); if (simplon) simplon->ConfigureDetector(experiment); } void DectrisDetectorWrapper::Start(const DiffractionExperiment &experiment) { CheckBusyOrError(); if (simplon) simplon->StartAcquisition(experiment); } void DectrisDetectorWrapper::Stop() { if (simplon) simplon->EndAcquisitionFinished(); } void DectrisDetectorWrapper::Trigger() { if (simplon) simplon->TriggerAcquisition(); } void DectrisDetectorWrapper::Deactivate() { CheckBusyOrError(); } DetectorStatus DectrisDetectorWrapper::GetStatus() const { if (!simplon) return {.detector_state = DetectorState::NOT_CONNECTED}; return { .detector_state = GetState() }; } void DectrisDetectorWrapper::LoadPixelMask(PixelMask &mask) { if (simplon) mask.LoadDECTRISBadPixelMask(simplon->GetPixelMask()); } std::string DectrisDetectorWrapper::GetErrorDescription() const { if (!simplon) return {}; std::string ret; try { ret = "SIMPLON state " + to_string(simplon->GetState()); } catch (const JFJochException &e) { ret = std::string("SIMPLON state unreadable (") + e.what() + ")"; } try { ret += ", errors " + simplon->GetErrorStatus(); } catch (const JFJochException &e) { ret += std::string(", errors unreadable (") + e.what() + ")"; } return ret; }