278 lines
11 KiB
C++
278 lines
11 KiB
C++
// Copyright (2019-2023) Paul Scherrer Institute
|
|
|
|
#include "JFJochBroker.h"
|
|
|
|
#define GRPC_RUN(x) try { {x;} return grpc::Status::OK; } catch (const std::exception& e) { logger.ErrorException(e); return {grpc::StatusCode::ABORTED, e.what()}; }
|
|
|
|
|
|
inline DataProcessingSettings Convert(const JFJochProtoBuf::DataProcessingSettings &input) {
|
|
DataProcessingSettings ret;
|
|
ret.signal_to_noise_threshold = input.signal_to_noise_threshold();
|
|
ret.photon_count_threshold = input.photon_count_threshold();
|
|
ret.min_pix_per_spot = input.min_pix_per_spot();
|
|
ret.max_pix_per_spot = input.max_pix_per_spot();
|
|
ret.local_bkg_size = input.local_bkg_size();
|
|
ret.high_resolution_limit = input.high_resolution_limit();
|
|
ret.low_resolution_limit = input.low_resolution_limit();
|
|
ret.bkg_estimate_low_q = input.bkg_estimate_low_q();
|
|
ret.bkg_estimate_high_q = input.bkg_estimate_high_q();
|
|
ret.preview_indexed_only = input.preview_indexed_only();
|
|
return ret;
|
|
}
|
|
|
|
inline JFJochProtoBuf::DataProcessingSettings Convert(const DataProcessingSettings &input) {
|
|
JFJochProtoBuf::DataProcessingSettings ret;
|
|
ret.set_signal_to_noise_threshold(input.signal_to_noise_threshold);
|
|
ret.set_photon_count_threshold(input.photon_count_threshold);
|
|
ret.set_min_pix_per_spot(input.min_pix_per_spot);
|
|
ret.set_max_pix_per_spot(input.max_pix_per_spot);
|
|
ret.set_local_bkg_size(input.local_bkg_size);
|
|
ret.set_high_resolution_limit(input.high_resolution_limit);
|
|
ret.set_low_resolution_limit(input.low_resolution_limit);
|
|
ret.set_bkg_estimate_low_q(input.bkg_estimate_low_q);
|
|
ret.set_bkg_estimate_high_q(input.bkg_estimate_high_q);
|
|
ret.set_preview_indexed_only(input.preview_indexed_only);
|
|
return ret;
|
|
}
|
|
|
|
inline PlotRequest Convert(const JFJochProtoBuf::PlotRequest& request) {
|
|
PlotRequest ret;
|
|
ret.binning = request.binning();
|
|
switch (request.type()) {
|
|
case JFJochProtoBuf::BKG_ESTIMATE:
|
|
ret.type = PlotType::BkgEstimate;
|
|
break;
|
|
case JFJochProtoBuf::RAD_INT:
|
|
ret.type = PlotType::RadInt;
|
|
break;
|
|
case JFJochProtoBuf::SPOT_COUNT:
|
|
ret.type = PlotType::SpotCount;
|
|
break;
|
|
case JFJochProtoBuf::INDEXING_RATE:
|
|
ret.type = PlotType::IndexingRate;
|
|
break;
|
|
case JFJochProtoBuf::INDEXING_RATE_PER_FILE:
|
|
ret.type = PlotType::IndexingRatePerFile;
|
|
break;
|
|
default:
|
|
case JFJochProtoBuf::ADU_HISTOGRAM:
|
|
ret.type = PlotType::ADUHistorgram;
|
|
break;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
inline JFJochProtoBuf::Plot Convert(const Plot& input) {
|
|
JFJochProtoBuf::Plot output;
|
|
if (!input.x.empty())
|
|
*output.mutable_x() = {input.x.begin(), input.x.end()};
|
|
if (!input.y.empty())
|
|
*output.mutable_y() = {input.y.begin(), input.y.end()};
|
|
return output;
|
|
}
|
|
|
|
inline JFJochProtoBuf::RadialIntegrationProfiles Convert(const RadialIntegrationProfiles& input) {
|
|
JFJochProtoBuf::RadialIntegrationProfiles output;
|
|
for (const auto &i: input.profiles) {
|
|
auto tmp = output.add_profiles();
|
|
tmp->set_title(i.title);
|
|
*tmp->mutable_plot() = Convert(i.plot);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
inline JFJochProtoBuf::JFCalibrationStatistics Convert(const std::vector<JFCalibrationModuleStatistics>& input) {
|
|
JFJochProtoBuf::JFCalibrationStatistics output;
|
|
for (const auto& i: input) {
|
|
auto ret = output.add_module_statistics();
|
|
ret->set_module_number(i.module_number);
|
|
ret->set_masked_pixels(i.masked_pixels);
|
|
ret->set_storage_cell_number(i.storage_cell_number);
|
|
ret->set_gain_g0_mean(i.gain_g0_mean);
|
|
ret->set_gain_g1_mean(i.gain_g1_mean);
|
|
ret->set_gain_g2_mean(i.gain_g2_mean);
|
|
ret->set_pedestal_g0_mean(i.pedestal_g0_mean);
|
|
ret->set_pedestal_g1_mean(i.pedestal_g1_mean);
|
|
ret->set_pedestal_g2_mean(i.pedestal_g2_mean);
|
|
}
|
|
return output;
|
|
}
|
|
inline JFJochProtoBuf::BrokerStatus Convert(const BrokerStatus& input) {
|
|
JFJochProtoBuf::BrokerStatus ret;
|
|
|
|
switch (input.broker_state) {
|
|
case JFJochState::Inactive:
|
|
ret.set_broker_state(JFJochProtoBuf::NOT_INITIALIZED);
|
|
break;
|
|
case JFJochState::Idle:
|
|
ret.set_broker_state(JFJochProtoBuf::IDLE);
|
|
break;
|
|
case JFJochState::Measuring:
|
|
ret.set_broker_state(JFJochProtoBuf::DATA_COLLECTION);
|
|
break;
|
|
case JFJochState::Error:
|
|
ret.set_broker_state(JFJochProtoBuf::ERROR);
|
|
break;
|
|
case JFJochState::Busy:
|
|
ret.set_broker_state(JFJochProtoBuf::BUSY);
|
|
break;
|
|
case JFJochState::Pedestal:
|
|
ret.set_broker_state(JFJochProtoBuf::PEDESTAL);
|
|
break;
|
|
}
|
|
|
|
ret.set_progress(input.progress);
|
|
ret.set_indexing_rate(input.indexing_rate);
|
|
ret.set_receiver_send_buffers_avail(input.receiver_send_buffers_avail);
|
|
return ret;
|
|
}
|
|
|
|
JFJochProtoBuf::DetectorList Convert(const DetectorList &input) {
|
|
JFJochProtoBuf::DetectorList ret;
|
|
for (const auto &i: input.detector) {
|
|
auto tmp = ret.add_detector();
|
|
tmp->set_id(i.id);
|
|
tmp->set_nmodules(i.nmodules);
|
|
tmp->set_description(i.description);
|
|
}
|
|
ret.set_current_description(input.current_description);
|
|
ret.set_current_id(input.current_id);
|
|
return ret;
|
|
}
|
|
|
|
JFJochProtoBuf::MeasurementStatistics Convert(const MeasurementStatistics &input) {
|
|
JFJochProtoBuf::MeasurementStatistics ret{};
|
|
|
|
ret.set_file_prefix(input.file_prefix);
|
|
ret.set_images_collected(input.images_collected);
|
|
ret.set_max_image_number_sent(input.max_image_number_sent);
|
|
ret.set_collection_efficiency(input.collection_efficiency);
|
|
ret.set_compression_ratio(input.compression_ratio);
|
|
|
|
ret.set_cancelled(input.cancelled);
|
|
ret.set_max_receive_delay(input.max_receive_delay);
|
|
|
|
ret.set_indexing_rate(input.indexing_rate);
|
|
|
|
ret.set_detector_width(input.detector_width);
|
|
ret.set_detector_height(input.detector_height);
|
|
ret.set_detector_pixel_depth(input.detector_pixel_depth);
|
|
|
|
ret.set_bkg_estimate(input.bkg_estimate);
|
|
|
|
return ret;
|
|
}
|
|
|
|
JFJochBroker::JFJochBroker(const DiffractionExperiment &experiment) {
|
|
state_machine.NotThreadSafe_Experiment() = experiment;
|
|
}
|
|
|
|
grpc::Status JFJochBroker::Start(grpc::ServerContext *context, const JFJochProtoBuf::DatasetSettings *request,
|
|
JFJochProtoBuf::Empty *response) {
|
|
GRPC_RUN( state_machine.Start(*request) );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::Stop(grpc::ServerContext *context, const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::Empty *response) {
|
|
GRPC_RUN( state_machine.Stop() );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::Pedestal(grpc::ServerContext *context, const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::Empty *response) {
|
|
GRPC_RUN( state_machine.Pedestal() );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::Initialize(grpc::ServerContext *context, const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::Empty *response) {
|
|
GRPC_RUN( state_machine.Initialize() );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::Cancel(grpc::ServerContext *context, const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::Empty *response) {
|
|
GRPC_RUN( state_machine.Cancel() );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::Deactivate(grpc::ServerContext *context, const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::Empty *response) {
|
|
GRPC_RUN( state_machine.Deactivate() );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::Trigger(grpc::ServerContext *context, const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::Empty *response) {
|
|
GRPC_RUN( state_machine.Trigger() );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::GetStatus(grpc::ServerContext *context, const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::BrokerStatus *response) {
|
|
GRPC_RUN( *response = Convert(state_machine.GetStatus()) );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::GetPlots(grpc::ServerContext *context, const JFJochProtoBuf::PlotRequest *request,
|
|
JFJochProtoBuf::Plot *response) {
|
|
GRPC_RUN( *response = Convert(state_machine.GetPlots(Convert(*request))) );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::GetRadialIntegrationProfiles(grpc::ServerContext *context, const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::RadialIntegrationProfiles *response) {
|
|
GRPC_RUN( *response = Convert(state_machine.GetRadialIntegrationProfiles()) );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::GetDetectorSettings(grpc::ServerContext *context, const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::DetectorSettings *response) {
|
|
GRPC_RUN( *response = state_machine.GetDetectorSettings() );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::GetCalibrationStatistics(grpc::ServerContext *context, const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::JFCalibrationStatistics *response) {
|
|
GRPC_RUN( *response = Convert(state_machine.GetCalibrationStatistics()) );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::PutDetectorSettings(grpc::ServerContext *context,
|
|
const JFJochProtoBuf::DetectorSettings *request,
|
|
JFJochProtoBuf::Empty *response) {
|
|
GRPC_RUN( state_machine.SetDetectorSettings(*request) );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::GetDataProcessingSettings(::grpc::ServerContext *context, const ::JFJochProtoBuf::Empty *request,
|
|
::JFJochProtoBuf::DataProcessingSettings *response) {
|
|
GRPC_RUN( *response = Convert(state_machine.GetDataProcessingSettings()) );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::PutDataProcessingSettings(::grpc::ServerContext *context,
|
|
const ::JFJochProtoBuf::DataProcessingSettings *request,
|
|
::JFJochProtoBuf::Empty *response) {
|
|
GRPC_RUN( state_machine.SetDataProcessingSettings(Convert(*request)) );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::GetMeasurementStatistics(grpc::ServerContext *context, const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::MeasurementStatistics *response) {
|
|
GRPC_RUN({
|
|
auto stat = state_machine.GetMeasurementStatistics();
|
|
if (stat)
|
|
*response = Convert(stat.value());
|
|
else
|
|
*response = JFJochProtoBuf::MeasurementStatistics();
|
|
});
|
|
}
|
|
|
|
JFJochServices &JFJochBroker::Services() {
|
|
return services;
|
|
}
|
|
|
|
void JFJochBroker::AddDetectorSetup(const DetectorSetup &setup) {
|
|
state_machine.AddDetectorSetup(setup);
|
|
logger.Info("Added detector {}", setup.GetDescription());
|
|
}
|
|
|
|
grpc::Status JFJochBroker::GetDetectorList(grpc::ServerContext *context,
|
|
const JFJochProtoBuf::Empty *request,
|
|
JFJochProtoBuf::DetectorList *response) {
|
|
GRPC_RUN(*response = Convert(state_machine.GetDetectorsList()) );
|
|
}
|
|
|
|
grpc::Status JFJochBroker::SelectDetector(grpc::ServerContext *context,
|
|
const JFJochProtoBuf::DetectorSelection *request,
|
|
JFJochProtoBuf::Empty *response) {
|
|
GRPC_RUN(state_machine.SelectDetector(request->id()));
|
|
}
|