207 lines
7.5 KiB
C++
207 lines
7.5 KiB
C++
// Copyright (2019-2023) Paul Scherrer Institute
|
|
|
|
#include <grpcpp/grpcpp.h>
|
|
#include "JFJochReceiverClient.h"
|
|
#include "../common/JFJochException.h"
|
|
|
|
void JFJochReceiverClient::Connect(const std::string& addr)
|
|
{
|
|
if (addr.empty()) _stub.reset();
|
|
else {
|
|
grpc::ChannelArguments ch_args;
|
|
ch_args.SetMaxReceiveMessageSize(GRPC_MAX_MESSAGE_SIZE);
|
|
auto ch = grpc::CreateCustomChannel(addr, grpc::InsecureChannelCredentials(), ch_args);
|
|
_stub = std::make_unique<JFJochProtoBuf::gRPC_JFJochReceiver::Stub>(ch);
|
|
}
|
|
};
|
|
|
|
void JFJochReceiverClient::Start(const DiffractionExperiment &experiment, const JFCalibration *calibration) {
|
|
JFJochProtoBuf::ReceiverInput receiver_input;
|
|
|
|
*receiver_input.mutable_jungfraujoch_settings() = experiment;
|
|
if (calibration != nullptr)
|
|
*receiver_input.mutable_calibration() = *calibration;
|
|
|
|
if (_stub) {
|
|
grpc::ClientContext context;
|
|
JFJochProtoBuf::Empty empty;
|
|
auto status = _stub->Start(&context, receiver_input, &empty);
|
|
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
|
|
"JFJochReceiver: " + status.error_message());
|
|
}
|
|
};
|
|
|
|
void JFJochReceiverClient::Cancel() {
|
|
if (_stub) {
|
|
grpc::ClientContext context;
|
|
JFJochProtoBuf::Empty empty;
|
|
auto status = _stub->Cancel(&context, empty, &empty);
|
|
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
|
|
"JFJochReceiver: " + status.error_message());
|
|
}
|
|
};
|
|
|
|
void JFJochReceiverClient::Abort() {
|
|
if (_stub) {
|
|
grpc::ClientContext context;
|
|
JFJochProtoBuf::Empty empty;
|
|
auto status = _stub->Abort(&context, empty, &empty);
|
|
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
|
|
"JFJochReceiver: " + status.error_message());
|
|
}
|
|
};
|
|
|
|
JFJochProtoBuf::ReceiverOutput JFJochReceiverClient::Stop() {
|
|
JFJochProtoBuf::ReceiverOutput ret;
|
|
if (_stub) {
|
|
grpc::ClientContext context;
|
|
JFJochProtoBuf::Empty empty;
|
|
auto status = _stub->Stop(&context, empty, &ret);
|
|
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
|
|
"JFJochReceiver: " + status.error_message());
|
|
}
|
|
return ret;
|
|
};
|
|
|
|
JFJochProtoBuf::ReceiverStatus JFJochReceiverClient::GetStatus() {
|
|
JFJochProtoBuf::ReceiverStatus ret;
|
|
if (_stub) {
|
|
grpc::ClientContext context;
|
|
JFJochProtoBuf::Empty empty;
|
|
auto status = _stub->GetStatus(&context, empty, &ret);
|
|
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
|
|
"JFJochReceiver: " + status.error_message());
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
void JFJochReceiverClient::SetDataProcessingSettings(const JFJochProtoBuf::DataProcessingSettings &settings) {
|
|
JFJochProtoBuf::Empty ret;
|
|
if (_stub) {
|
|
grpc::ClientContext context;
|
|
auto status = _stub->SetDataProcessingSettings(&context, settings, &ret);
|
|
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
|
|
"JFJochReceiver: " + status.error_message());
|
|
}
|
|
}
|
|
|
|
JFJochProtoBuf::PreviewFrame JFJochReceiverClient::GetPreviewFrame() {
|
|
JFJochProtoBuf::PreviewFrame ret;
|
|
if (_stub) {
|
|
grpc::ClientContext context;
|
|
JFJochProtoBuf::Empty empty;
|
|
auto status = _stub->GetPreviewFrame(&context, empty, &ret);
|
|
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
|
|
"JFJochReceiver: " + status.error_message());
|
|
} else {
|
|
std::vector<uint16_t> fake_image(640*480);
|
|
for (int i = 0; i < 640 * 480; i++)
|
|
fake_image[i] = i % 65536;
|
|
|
|
ret.set_image_number(0);
|
|
ret.set_width(640);
|
|
ret.set_height(480);
|
|
ret.set_pixel_depth(2);
|
|
ret.set_data(fake_image.data(), fake_image.size() * sizeof(uint16_t));
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
JFJochProtoBuf::ReceiverNetworkConfig JFJochReceiverClient::GetNetworkConfig() {
|
|
JFJochProtoBuf::ReceiverNetworkConfig ret;
|
|
if (_stub) {
|
|
grpc::ClientContext context;
|
|
JFJochProtoBuf::Empty empty;
|
|
auto status = _stub->GetNetworkConfig(&context, empty, &ret);
|
|
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
|
|
"JFJochReceiver: " + status.error_message());
|
|
} else {
|
|
// For tests to work, dummy receiver needs to replay with nonsense MAC addresses
|
|
auto d1 = ret.add_device();
|
|
d1->set_mac_addr("00:00:00:00:00:00");
|
|
d1->set_ipv4_addr("10.10.50.1");
|
|
d1->set_udp_port(1234);
|
|
|
|
auto d2 = ret.add_device();
|
|
d2->set_mac_addr("00:00:00:00:00:01");
|
|
d2->set_ipv4_addr("10.10.50.2");
|
|
d2->set_udp_port(1234);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
JFJochProtoBuf::Plot JFJochReceiverClient::GetPlots(const JFJochProtoBuf::PlotRequest &request) {
|
|
JFJochProtoBuf::Plot ret;
|
|
if (_stub) {
|
|
grpc::ClientContext context;
|
|
JFJochProtoBuf::Empty empty;
|
|
auto status = _stub->GetDataProcessingPlots(&context, request, &ret);
|
|
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
|
|
"JFJochReceiver: " + status.error_message());
|
|
} else {
|
|
// TODO: Write some dummy plots
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
JFJochProtoBuf::RadialIntegrationProfiles JFJochReceiverClient::GetRadialIntegrationProfiles() {
|
|
JFJochProtoBuf::RadialIntegrationProfiles ret;
|
|
if (_stub) {
|
|
grpc::ClientContext context;
|
|
JFJochProtoBuf::Empty empty;
|
|
auto status = _stub->GetRadialIntegrationProfiles(&context, empty, &ret);
|
|
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
|
|
"JFJochReceiver: " + status.error_message());
|
|
} else {
|
|
auto p = ret.add_profiles();
|
|
p->set_title("dataset");
|
|
*p->mutable_plot() = GenerateGaussianPlot(50, 0.1, 2.5, 0.2);
|
|
|
|
p = ret.add_profiles();
|
|
p->set_title("file0");
|
|
*p->mutable_plot() = GenerateGaussianPlot(50, 0.1, 3.0, 0.2);
|
|
|
|
p = ret.add_profiles();
|
|
p->set_title("file1");
|
|
*p->mutable_plot() = GenerateGaussianPlot(50, 0.1, 2.0, 0.2);
|
|
|
|
p = ret.add_profiles();
|
|
p->set_title("file2");
|
|
*p->mutable_plot() = GenerateGaussianPlot(50, 0.1, 2.5, 0.1);
|
|
|
|
p = ret.add_profiles();
|
|
p->set_title("file3");
|
|
*p->mutable_plot() = GenerateGaussianPlot(50, 0.1, 2.5, 0.5);
|
|
|
|
for (int i = 4; i < 16; i++) {
|
|
p = ret.add_profiles();
|
|
p->set_title("file" + std::to_string(i));
|
|
*p->mutable_plot() = GenerateGaussianPlot(50, 0.1, 2.3, 0.1 + 0.02 * i);
|
|
}
|
|
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
JFJochProtoBuf::Plot JFJochReceiverClient::GenerateGaussianPlot(uint64_t n_elements, float spacing, float mean, float std) {
|
|
|
|
std::vector<float> x(n_elements);
|
|
std::vector<float> y(n_elements);
|
|
|
|
constexpr float inv_sqrt_2pi = 0.3989422804;
|
|
|
|
for (int i = 0; i < n_elements; i++) {
|
|
x[i] = spacing * i;
|
|
float a = (x[i] - mean) / std;
|
|
y[i] = inv_sqrt_2pi / std * expf(-0.5f * a * a);;
|
|
}
|
|
|
|
JFJochProtoBuf::Plot ret;
|
|
if (n_elements > 0) {
|
|
*ret.mutable_x() = {x.begin(), x.end()};
|
|
*ret.mutable_y() = {y.begin(), y.end()};
|
|
}
|
|
return ret;
|
|
}
|
|
|