Files
Jungfraujoch/broker/JFJochServices.cpp

152 lines
4.4 KiB
C++

// Copyright (2019-2023) Paul Scherrer Institute
#include "JFJochServices.h"
#include "../common/JFJochException.h"
JFJochServices::JFJochServices(Logger &in_logger) : logger(in_logger) {}
void JFJochServices::Start(const DiffractionExperiment& experiment, const JFCalibration &calibration) {
logger.Info("Measurement start for: {}", experiment.GetFilePrefix());
if (receiver != nullptr) {
logger.Info(" ... receiver start");
if (experiment.GetDetectorMode() == DetectorMode::Conversion)
receiver->Start(experiment, &calibration);
else
receiver->Start(experiment, nullptr);
}
if (detector && !experiment.IsUsingInternalPacketGen()) {
logger.Info(" ... detector start");
detector->Start(experiment);
}
logger.Info(" Done!");
}
void JFJochServices::Off() {
if (detector)
detector->Deactivate();
}
void JFJochServices::On(const DiffractionExperiment &x) {
logger.Info("Detector on");
if (detector && (receiver != nullptr))
detector->Configure(x, receiver->GetNetworkConfig());
logger.Info(" ... done");
}
JFJochServicesOutput JFJochServices::Stop(const JFCalibration &calibration) {
JFJochServicesOutput ret;
std::unique_ptr<JFJochException> exception;
if (receiver != nullptr) {
try {
logger.Info("Wait for receiver done");
ret.receiver_output = receiver->Stop();
logger.Info(" ... Receiver efficiency: {} % Max delay: {} Compression ratio {}x",
static_cast<int>(ret.receiver_output.efficiency * 100.0),
ret.receiver_output.max_receive_delay,
static_cast<int>(std::round(ret.receiver_output.compressed_ratio)));
if (ret.receiver_output.efficiency < 1.0) {
for (int i = 0; i < ret.receiver_output.received_packets.size(); i++) {
if (ret.receiver_output.received_packets[i] != ret.receiver_output.expected_packets[i])
logger.Info(" ... Module: {} Packets received: {} out of {}", i,
ret.receiver_output.received_packets[i], ret.receiver_output.expected_packets[i]);
}
}
} catch (const JFJochException &e) {
logger.Error(" ... finished with error {}", e.what());
exception = std::make_unique<JFJochException>(e);
}
logger.Info("Receiver finished with success");
}
if (detector) {
logger.Info("Stopping detector");
try {
detector->Stop();
logger.Info(" ... done");
} catch (JFJochException &e) {
logger.Error(" ... finished with error {}", e.what());
exception = std::make_unique<JFJochException>(e);
}
}
if (exception)
throw JFJochException(*exception);
return ret;
}
void JFJochServices::Abort() {
// Abort should try to achieve the best outcome possible
// but it is OK if things fail (for example lost connection)
try {
if (receiver != nullptr)
receiver->Abort();
} catch (const std::exception &e) {
logger.Error(e.what());
}
}
void JFJochServices::Cancel() {
if (detector)
detector->Stop();
if (receiver != nullptr)
receiver->Cancel();
}
JFJochServices &JFJochServices::Receiver(JFJochReceiverService *input) {
receiver = input;
return *this;
}
JFJochServices &JFJochServices::Detector() {
detector = std::make_unique<DetectorWrapper>();
logger.Info("Using detector service");
return *this;
}
JFJochReceiverStatus JFJochServices::GetReceiverStatus() {
if (receiver == nullptr)
return {};
return receiver->GetStatus();
}
Plot JFJochServices::GetPlots(const PlotRequest &request) {
if (receiver == nullptr)
return {};
try {
return receiver->GetDataProcessingPlot(request);
} catch (...) {
return {};
}
}
RadialIntegrationProfiles JFJochServices::GetRadialIntegrationProfiles() {
if (receiver == nullptr)
return {};
try {
return receiver->GetRadialIntegrationProfiles();
} catch (...) {
return {};
}
}
void JFJochServices::SetDataProcessingSettings(const DataProcessingSettings &settings) {
if (receiver)
receiver->SetDataProcessingSettings(settings);
}
void JFJochServices::Trigger() {
if (detector)
detector->Trigger();
}