239a441ee6
Build Packages / Unit tests (push) Successful in 1h20m34s
Build Packages / build:rpm (rocky8) (push) Successful in 13m32s
Build Packages / Generate python client (push) Successful in 24s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 13m6s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 11m32s
Build Packages / XDS test (durin plugin) (push) Successful in 10m49s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 14m8s
Build Packages / DIALS test (push) Successful in 14m57s
Build Packages / Build documentation (push) Successful in 47s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 13m30s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 14m23s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 14m40s
Build Packages / Create release (push) Has been skipped
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 13m14s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 11m55s
Build Packages / build:rpm (rocky9) (push) Successful in 14m23s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 9m48s
Build Packages / XDS test (neggia plugin) (push) Successful in 7m10s
This is an UNSTABLE release. The release has significant modifications and bug fixes, if things go wrong, it is better to revert to 1.0.0-rc.132. * jfjoch_broker: For DECTRIS detectors, ZeroMQ link is persistent, to save time for establishing new connection * jfjoch_broker: Minor bug fixes for rare conditions Reviewed-on: #50
373 lines
15 KiB
C++
373 lines
15 KiB
C++
// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "JFJochHttpReader.h"
|
|
|
|
#include <httplib.h>
|
|
#include <nlohmann/json.hpp>
|
|
#include "../frame_serialize/CBORStream2Deserializer.h"
|
|
#include "../broker/gen/model/Image_buffer_status.h"
|
|
#include "../broker/gen/model/Plots.h"
|
|
#include "../broker/gen/model/Broker_status.h"
|
|
#include "../image_analysis/bragg_integration/CalcISigma.h"
|
|
|
|
void JFJochHttpReader::Close() {
|
|
std::unique_lock ul(http_mutex);
|
|
addr = "";
|
|
SetStartMessage({});
|
|
last_image_buffer_counter = {};
|
|
last_op_http_sync = false;
|
|
}
|
|
|
|
ImageBufferStatus JFJochHttpReader::GetImageBufferStatus() const {
|
|
httplib::Client cli_cmd(addr);
|
|
|
|
auto res = cli_cmd.Get("/image_buffer/status");
|
|
if (!res || res->status != httplib::StatusCode::OK_200)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Could not get image buffer status");
|
|
|
|
try {
|
|
org::openapitools::server::model::Image_buffer_status status = nlohmann::json::parse(res->body);
|
|
ImageBufferStatus ret{};
|
|
ret.max_image_number = status.getMaxImageNumber();
|
|
ret.min_image_number = status.getMinImageNumber();
|
|
ret.available_slots = status.getAvailableSlots();
|
|
ret.total_slots = status.getTotalSlots();
|
|
ret.images_in_the_buffer = status.getImageNumbers();
|
|
if (status.currentCounterIsSet())
|
|
ret.current_counter = status.getCurrentCounter();
|
|
return ret;
|
|
} catch (std::exception &e) {
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Could not parse image buffer status");
|
|
}
|
|
}
|
|
|
|
BrokerStatus JFJochHttpReader::GetBrokerStatus() const {
|
|
httplib::Client cli_cmd(addr);
|
|
|
|
auto res = cli_cmd.Get("/status");
|
|
if (!res || res->status != httplib::StatusCode::OK_200)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Could not get image buffer status");
|
|
|
|
try {
|
|
org::openapitools::server::model::Broker_status input = nlohmann::json::parse(res->body);
|
|
BrokerStatus ret{};
|
|
ret.broker_version = input.getBrokerVersion();
|
|
ret.gpu_count = input.getGpuCount();
|
|
if (input.progressIsSet())
|
|
ret.progress = input.getProgress();
|
|
|
|
if (input.getState() == "Inactive")
|
|
ret.state = JFJochState::Inactive;
|
|
else if (input.getState() == "Idle")
|
|
ret.state = JFJochState::Idle;
|
|
else if (input.getState() == "Measuring")
|
|
ret.state = JFJochState::Measuring;
|
|
else if (input.getState() == "Error")
|
|
ret.state = JFJochState::Error;
|
|
else if (input.getState() == "Busy")
|
|
ret.state = JFJochState::Busy;
|
|
else if (input.getState() == "Pedestal")
|
|
ret.state = JFJochState::Calibration;
|
|
|
|
if (input.getMessageSeverity() == "info")
|
|
ret.message_severity = BrokerStatus::MessageSeverity::Info;
|
|
else if (input.getMessageSeverity() == "success")
|
|
ret.message_severity = BrokerStatus::MessageSeverity::Success;
|
|
else if (input.getMessageSeverity() == "warning")
|
|
ret.message_severity = BrokerStatus::MessageSeverity::Warning;
|
|
else if (input.getMessageSeverity() == "error")
|
|
ret.message_severity = BrokerStatus::MessageSeverity::Error;
|
|
if (input.brokerVersionIsSet())
|
|
ret.broker_version = input.getBrokerVersion();
|
|
|
|
return ret;
|
|
} catch (std::exception &e) {
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Could not parse image buffer status");
|
|
}
|
|
}
|
|
|
|
uint64_t JFJochHttpReader::GetNumberOfImages() const {
|
|
std::unique_lock ul(http_mutex);
|
|
|
|
if (addr.empty())
|
|
return 0;
|
|
auto status = GetImageBufferStatus();
|
|
return status.max_image_number + 1;
|
|
}
|
|
|
|
std::shared_ptr<JFJochReaderDataset> JFJochHttpReader::UpdateDataset_i() {
|
|
httplib::Client cli_cmd(addr);
|
|
|
|
auto res = cli_cmd.Get("/image_buffer/start.cbor");
|
|
|
|
if (!res || (res->status != httplib::StatusCode::OK_200
|
|
&& res->status != httplib::StatusCode::NotFound_404))
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Could not get image buffer status");
|
|
|
|
if (res->status == httplib::StatusCode::NotFound_404)
|
|
return {};
|
|
if (res->body.empty())
|
|
return {};
|
|
|
|
try {
|
|
auto msg = CBORStream2Deserialize(res->body);
|
|
|
|
if (msg->msg_type != CBORImageType::START)
|
|
return {};
|
|
|
|
auto dataset = std::make_shared<JFJochReaderDataset>();
|
|
dataset->arm_date = msg->start_message->arm_date;
|
|
|
|
dataset->experiment = default_experiment;
|
|
|
|
// JFJochReader is always using int32_t
|
|
dataset->experiment.BitDepthImage(32);
|
|
dataset->experiment.PixelSigned(true);
|
|
dataset->experiment.FilePrefix(msg->start_message->file_prefix);
|
|
dataset->experiment.BeamX_pxl(msg->start_message->beam_center_x);
|
|
dataset->experiment.BeamY_pxl(msg->start_message->beam_center_y);
|
|
dataset->experiment.DetectorDistance_mm(msg->start_message->detector_distance * 1000.0);
|
|
dataset->experiment.DetectIceRings(msg->start_message->detect_ice_rings.value_or(false));
|
|
dataset->experiment.PoniRot1_rad(msg->start_message->poni_rot1.value_or(0.0));
|
|
dataset->experiment.PoniRot2_rad(msg->start_message->poni_rot2.value_or(0.0));
|
|
dataset->experiment.PoniRot3_rad(msg->start_message->poni_rot3.value_or(0.0));
|
|
|
|
dataset->az_int_bin_to_q = msg->start_message->az_int_bin_to_q;
|
|
dataset->az_int_bin_to_phi = msg->start_message->az_int_bin_to_phi;
|
|
dataset->q_bins = msg->start_message->az_int_q_bin_count.value_or(0);
|
|
dataset->azimuthal_bins = msg->start_message->az_int_phi_bin_count.value_or(0);
|
|
dataset->jfjoch_release = msg->start_message->jfjoch_release;
|
|
|
|
DetectorSetup detector = DetDECTRIS(msg->start_message->image_size_x, msg->start_message->image_size_y,
|
|
msg->start_message->detector_description, {});
|
|
detector.PixelSize_um(msg->start_message->pixel_size_x * 1e6);
|
|
detector.SaturationLimit(msg->start_message->saturation_value);
|
|
detector.MinFrameTime(std::chrono::microseconds(0));
|
|
detector.MinCountTime(std::chrono::microseconds(0));
|
|
detector.ReadOutTime(std::chrono::microseconds (0));
|
|
dataset->experiment.Detector(detector);
|
|
|
|
dataset->experiment.FrameTime(
|
|
std::chrono::microseconds(std::lround(msg->start_message->frame_time * 1e6)),
|
|
std::chrono::microseconds(std::lround(msg->start_message->count_time * 1e6))
|
|
);
|
|
|
|
if (!msg->start_message->pixel_mask.empty())
|
|
dataset->pixel_mask = PixelMask(msg->start_message->pixel_mask.begin()->second);
|
|
|
|
dataset->experiment.NumTriggers(1);
|
|
dataset->experiment.ImagesPerTrigger(msg->start_message->number_of_images);
|
|
dataset->experiment.SampleName(msg->start_message->sample_name);
|
|
dataset->experiment.SampleTemperature_K(msg->start_message->sample_temperature_K);
|
|
dataset->experiment.RingCurrent_mA(msg->start_message->ring_current_mA);
|
|
dataset->experiment.IncidentEnergy_keV(msg->start_message->incident_energy / 1000.0);
|
|
|
|
dataset->experiment.FluorescenceSpectrum(msg->start_message->fluorescence_spectrum);
|
|
|
|
dataset->bkg_estimate = GetPlot_i("bkg_estimate");
|
|
dataset->spot_count = GetPlot_i("spot_count");
|
|
dataset->spot_count_ice_rings = GetPlot_i("spot_count_ice");
|
|
dataset->spot_count_low_res = GetPlot_i("spot_count_low_res");
|
|
dataset->spot_count_indexed = GetPlot_i("spot_count_indexed");
|
|
|
|
dataset->indexing_result = GetPlot_i("indexing_rate");
|
|
dataset->profile_radius = GetPlot_i("profile_radius");
|
|
dataset->mosaicity_deg = GetPlot_i("mosaicity");
|
|
dataset->b_factor = GetPlot_i("b_factor");
|
|
dataset->resolution_estimate = GetPlot_i("resolution_estimate");
|
|
dataset->efficiency = GetPlot_i("image_collection_efficiency");
|
|
|
|
if (msg->start_message->goniometer)
|
|
dataset->experiment.Goniometer(msg->start_message->goniometer);
|
|
else if (msg->start_message->grid_scan)
|
|
dataset->experiment.GridScan(msg->start_message->grid_scan);
|
|
|
|
return dataset;
|
|
} catch (std::exception &e) {
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
std::string("Could not load dataset: ") + std::string(e.what()));
|
|
}
|
|
}
|
|
|
|
void JFJochHttpReader::ReadURL(const std::string &url) {
|
|
std::unique_lock ul(http_mutex);
|
|
addr = url;
|
|
if (url.empty())
|
|
SetStartMessage({});
|
|
else
|
|
SetStartMessage(UpdateDataset_i());
|
|
}
|
|
|
|
bool JFJochHttpReader::LoadImage_i(std::shared_ptr<JFJochReaderDataset> &dataset,
|
|
DataMessage &message,
|
|
std::vector<uint8_t> &buffer,
|
|
int64_t image_number,
|
|
bool update_dataset) {
|
|
std::unique_lock ul(http_mutex);
|
|
|
|
if (addr.empty())
|
|
return false;
|
|
|
|
httplib::Client cli_cmd(addr);
|
|
|
|
bool buffer_changed = false;
|
|
|
|
// For autoupdate - if buffer didn't change don't update dataset
|
|
auto status = GetImageBufferStatus();
|
|
if (!last_image_buffer_counter.has_value() // No information on the previous buffer state - always assume it changed
|
|
|| !status.current_counter.has_value() // No information on the current buffer state - e.g. old version of software
|
|
|| last_image_buffer_counter.value() != status.current_counter.value()) // current counter value different from previous
|
|
buffer_changed = true;
|
|
last_image_buffer_counter = status.current_counter;
|
|
|
|
if (image_number == -1) {
|
|
if (last_op_http_sync && !buffer_changed)
|
|
return false;
|
|
last_op_http_sync = true;
|
|
} else {
|
|
last_op_http_sync = false;
|
|
}
|
|
|
|
// Always update dataset, as it might have changed from the last time
|
|
if (buffer_changed && update_dataset)
|
|
dataset = UpdateDataset_i();
|
|
if (!dataset)
|
|
return false;
|
|
|
|
auto res = cli_cmd.Get("/image_buffer/image.cbor?id=" + std::to_string(image_number));
|
|
|
|
if (!res || res->status != httplib::StatusCode::OK_200)
|
|
return false;
|
|
|
|
if (res->body.empty()) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
buffer.resize(res->body.size());
|
|
memcpy(buffer.data(), res->body.data(), res->body.size());
|
|
|
|
auto msg = CBORStream2Deserialize(buffer);
|
|
|
|
if (msg->msg_type != CBORImageType::IMAGE)
|
|
return false;
|
|
|
|
message = *msg->data_message;
|
|
|
|
CalcISigma(message);
|
|
CalcWilsonBFactor(message);
|
|
|
|
return true;
|
|
} catch (std::exception &e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<JFJochReaderRawImage> JFJochHttpReader::GetRawImage(int64_t image_number) {
|
|
if (addr.empty())
|
|
return {};
|
|
|
|
httplib::Client cli_cmd(addr);
|
|
auto res = cli_cmd.Get("/image_buffer/image.cbor?id=" + std::to_string(image_number));
|
|
|
|
if (!res || res->status != httplib::StatusCode::OK_200 || res->body.empty())
|
|
return {};
|
|
|
|
try {
|
|
auto msg =
|
|
CBORStream2Deserialize(reinterpret_cast<uint8_t *>(res->body.data()), res->body.size());
|
|
|
|
if (msg->msg_type != CBORImageType::IMAGE)
|
|
return {};
|
|
|
|
std::shared_ptr<JFJochReaderRawImage> image = std::make_shared<JFJochReaderRawImage>();
|
|
image->image_buffer.resize(msg->data_message->image.GetCompressedSize());
|
|
memcpy(image->image_buffer.data(),
|
|
msg->data_message->image.GetCompressed(),
|
|
msg->data_message->image.GetCompressedSize());
|
|
image->image = CompressedImage(
|
|
image->image_buffer.data(),
|
|
image->image_buffer.size(),
|
|
msg->data_message->image.GetWidth(),
|
|
msg->data_message->image.GetHeight(),
|
|
msg->data_message->image.GetMode(),
|
|
msg->data_message->image.GetCompressionAlgorithm()
|
|
);
|
|
|
|
return image;
|
|
} catch (std::exception &e) {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
std::vector<float> JFJochHttpReader::GetPlot_i(const std::string &plot_type, float fill_value) const {
|
|
if (addr.empty())
|
|
return {};
|
|
|
|
httplib::Client cli_cmd(addr);
|
|
|
|
auto res_bin = cli_cmd.Get("/preview/plot.bin?type=" + plot_type);
|
|
if (res_bin && res_bin->status == httplib::StatusCode::OK_200) {
|
|
if (res_bin->body.size() % sizeof(float) != 0) {
|
|
throw std::runtime_error("Input size is not a multiple of sizeof(float)");
|
|
}
|
|
std::vector<float> v(res_bin->body.size() / sizeof(float));
|
|
std::memcpy(v.data(), res_bin->body.data(), res_bin->body.size());
|
|
return v;
|
|
}
|
|
|
|
auto res = cli_cmd.Get("/preview/plot?binning=1&experimental_coord=false&fill="
|
|
+ std::to_string(fill_value) + "&type=" + plot_type);
|
|
|
|
if (!res || res->status != httplib::StatusCode::OK_200)
|
|
return {};
|
|
|
|
try {
|
|
org::openapitools::server::model::Plots plots = nlohmann::json::parse(res->body);
|
|
auto plot_v = plots.getPlot();
|
|
if (plot_v.size() == 1)
|
|
return plot_v[0].getY();
|
|
else
|
|
return {};
|
|
} catch (nlohmann::json::parse_error &e) {
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Could not parse image buffer status");
|
|
}
|
|
}
|
|
|
|
void JFJochHttpReader::UploadUserMask(const std::vector<uint32_t>& mask) {
|
|
std::unique_lock ul(http_mutex);
|
|
|
|
if (addr.empty())
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"HTTP address not set. Call ReadURL() first.");
|
|
|
|
if (mask.empty())
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"User mask is empty.");
|
|
|
|
httplib::Client cli_cmd(addr);
|
|
|
|
const char* data_ptr = reinterpret_cast<const char*>(mask.data());
|
|
const size_t byte_size = mask.size() * sizeof(uint32_t);
|
|
|
|
auto res = cli_cmd.Put("/config/user_mask",
|
|
data_ptr,
|
|
byte_size,
|
|
"application/octet-stream");
|
|
|
|
if (!res)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Failed to connect to server to upload user mask");
|
|
|
|
if (res->status != httplib::StatusCode::OK_200)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Server rejected user mask upload");
|
|
}
|