Commit 58eae7a9 authored by yannick legoc's avatar yannick legoc
Browse files

First version of WinDeta Dielectrics

parent a9ab80ad
......@@ -20,7 +20,11 @@
#include "WindetaMessages.pb.h"
#include <boost/lexical_cast.hpp>
#include <common/base/ServerProperties.h>
#include <common/base/FileUtilities.h>
#include "controllers/common/family/Families.h"
#include "InstrumentManager/InstrumentManager.h"
#include "controllers/common/acquisition/ExperimentData.h"
#include <boost/filesystem/operations.hpp>
#include <thread>
#include <fstream>
#include <iostream>
......@@ -36,9 +40,14 @@ const std::string RemoteWindeta::TYPE = "dielectrics_remote_windeta";
const std::string RemoteWindeta::WINDETA_APPLICATION = "dielectrics-windeta";
const std::string RemoteWindeta::RESPONDER_NAME = "dielectrics_controller";
const std::string RemoteWindeta::PUBLISHER_NAME = "dielectrics_pub";
const std::string RemoteWindeta::INDEPENDENT_PREFIX = "windeta-";
const std::string RemoteWindeta::INDEPENDENT_VALUE = "independent";
const std::string RemoteWindeta::NUMOR_VALUE = "numor";
const std::string RemoteWindeta::NUMOR_1_VALUE = "numor+1";
RemoteWindeta::RemoteWindeta(const std::string& name) :
ExperimentController(name), controller::Stoppable(this), controller::Raz(this), Test(this), m_stopped(false) {
ExperimentController(name), controller::Stoppable(this), controller::Raz(this), Test(this), m_independentDataFileId(0), m_stopped(false) {
setFamily(family::ACQUISITION, family::POLARIZATIONS);
......@@ -50,11 +59,32 @@ RemoteWindeta::RemoteWindeta(const std::string& name) :
numberOfPoints.init(this, NOSAVE | SPY, "number_of_points", "Points");
startTimeout.init(this, SAVE, "start_timeout");
dataFilenameFormat.init(this, SAVE, "data_filename_format", "Filename");
dataFilenameFormatSize.init(this, NOSAVE, "data_filename_format_size");
dataFilenameFormatValues.init(this, NOSAVE, "data_filename_format_values");
dataFilenameFormatLabels.init(this, NOSAVE, "data_filename_format_labels");
dataFilenameFormatSize = 3;
dataFilenameFormatValues.resize(dataFilenameFormatSize());
dataFilenameFormatValues.set(0, NUMOR_VALUE);
dataFilenameFormatValues.set(1, NUMOR_1_VALUE);
dataFilenameFormatValues.set(2, INDEPENDENT_VALUE);
dataFilenameFormatLabels.resize(dataFilenameFormatSize());
dataFilenameFormatLabels.set(0, "Numor");
dataFilenameFormatLabels.set(1, "Numor+1");
dataFilenameFormatLabels.set(2, "Independent");
dataFilenameFormat.setEnumeratedValues(dataFilenameFormatValues);
dataFilenameFormat.setEnumeratedLabels(dataFilenameFormatLabels);
m_driver.init(this, "driver");
initIndependentDataFileId();
}
RemoteWindeta::RemoteWindeta(const RemoteWindeta& controller) :
ExperimentController(controller), controller::Stoppable(this), controller::Raz(this), Test(this), m_stopped(false) {
ExperimentController(controller), controller::Stoppable(this), controller::Raz(this), Test(this), m_independentDataFileId(0), m_stopped(false) {
}
RemoteWindeta::~RemoteWindeta() {
......@@ -219,16 +249,19 @@ void RemoteWindeta::start() {
m_stopped = false;
// Set the data filename.
setDataFilename();
// The status must pass to running within a timeout.
int i = 0;
int timeout = startTimeout();
while (!commandStatus.isRunning() && i < timeout) {
while (!commandStatus.isRunning() && i < timeout && !m_stopped) {
std::this_thread::sleep_for(std::chrono::seconds(1));
i++;
}
if (!commandStatus.isRunning()) {
log(Level::s_Error) << name << "preset " << preset() << " start timeout" << endlog;
log(Level::s_Error) << name << "preset " << preset() << " to " << m_dataFilename << " start timeout" << endlog;
return;
}
......@@ -238,13 +271,13 @@ void RemoteWindeta::start() {
}
if (m_stopped) {
log(Level::s_Info) << name << "preset " << preset() << " stopped" << endlog;
log(Level::s_Info) << name << "preset " << preset() << " to " << m_dataFilename << " stopped" << endlog;
} else if (commandStatus.isError()) {
log(Level::s_Error) << name << "preset " << preset() << " on error" << endlog;
log(Level::s_Error) << name << "preset " << preset() << " to " << m_dataFilename << " on error" << endlog;
}
else {
log(Level::s_Info) << name << "preset " << preset() << " finished" << endlog;
log(Level::s_Info) << name << "preset " << preset() << " to " << m_dataFilename << " finished" << endlog;
}
}
......@@ -328,6 +361,99 @@ void RemoteWindeta::stopApplication() {
cout << "Windeta application terminated with state " << application::toString(applicationState()) << endl;
}
void RemoteWindeta::initIndependentDataFileId() {
string dataPath = ServerProperties::getInstance()->getNomadDataPath();
vector<filesystem::path> files = FileUtilities::getFilePathsWithExtension(dataPath, "diel");
for (auto f : files) {
string filename = f.filename().string();
if (filename.find(INDEPENDENT_PREFIX) != string::npos) {
int length = filename.length() - 5 - INDEPENDENT_PREFIX.length();
string idString = filename.substr(INDEPENDENT_PREFIX.length(), length);
int id = lexical_cast<int>(idString);
if (id > m_independentDataFileId) {
m_independentDataFileId = id;
}
}
}
cout << "Independent data file id is " << m_independentDataFileId << endl;
}
void RemoteWindeta::setDataFilename() {
// Find the experiment data controller.
acquisition::ExperimentData* experimentData = nullptr;
try {
string controllerName = InstrumentManager::getInstance()->getControllersWithType("title").at(0);
experimentData = dynamic_cast<acquisition::ExperimentData*>(InstrumentManager::getInstance()->getExperimentController(controllerName));
}
catch (...) {
log(Level::s_Error) << name << "cannot find a name for data file associated to a numor" << endlog;
}
// Set the filename according to the format.
if (dataFilenameFormat() == INDEPENDENT_VALUE || experimentData == nullptr) {
m_independentDataFileId++;
m_dataFilename = ServerProperties::getInstance()->getNomadDataPath() + INDEPENDENT_PREFIX + lexical_cast<string>(m_independentDataFileId) + ".diel";
cout << "Independent data filename " << m_dataFilename << endl;
}
else {
int numor = experimentData->numor();
if (dataFilenameFormat() == NUMOR_1_VALUE) {
numor++;
}
stringstream numorValue;
numorValue << boost::format("%06d") % numor;
string dataPath = ServerProperties::getInstance()->getNomadDataPath();
string baseDataFilename = dataPath + numorValue.str();
string dataFilename = baseDataFilename + ".diel";
filesystem::path file(dataFilename);
if (filesystem::exists(file)) {
int i = 1;
while (true) {
dataFilename = baseDataFilename + "-" + lexical_cast<string>(i) + ".diel";
filesystem::path file(dataFilename);
if (!filesystem::exists(file)) {
break;
}
i++;
}
}
m_dataFilename = dataFilename;
cout << "Numor data filename " << m_dataFilename << endl;
}
}
void RemoteWindeta::saveData(const std::string& data) {
ofstream dataFile(m_dataFilename.c_str());
if (dataFile.is_open()) {
dataFile << data;
dataFile.close();
}
else {
log(Level::s_Error) << name << "preset " << preset() << " to " << m_dataFilename << " fails to write data" << endlog;
}
}
void RemoteWindeta::subscriberLoop() {
// Loop on events
......@@ -363,6 +489,9 @@ void RemoteWindeta::subscriberLoop() {
windeta::Data data;
data.ParseFromString(data2);
numberOfPoints = data.points();
saveData(data.data());
cout << "Data " << data.points() << " points" << endl;
}
}
......
......@@ -72,15 +72,29 @@ public:
Property<int32> numberOfPoints;
Property<int32> startTimeout;
Property<std::string> dataFilenameFormat;
Property<int32> dataFilenameFormatSize;
DynamicProperty<std::string> dataFilenameFormatValues;
DynamicProperty<std::string> dataFilenameFormatLabels;
private:
void reset();
bool initApplication();
void stopApplication();
void subscriberLoop();
void initIndependentDataFileId();
void setDataFilename();
void saveData(const std::string& data);
static const std::string WINDETA_APPLICATION;
static const std::string RESPONDER_NAME;
static const std::string PUBLISHER_NAME;
static const std::string INDEPENDENT_PREFIX;
static const std::string INDEPENDENT_VALUE;
static const std::string NUMOR_VALUE;
static const std::string NUMOR_1_VALUE;
std::unique_ptr<cameo::Server> m_server;
std::unique_ptr<cameo::application::Instance> m_windetaApplication;
......@@ -90,7 +104,9 @@ private:
DriverPtr<driver::NullDriver> m_driver;
int m_independentDataFileId;
bool m_stopped;
std::string m_dataFilename;
};
}
......
......@@ -2,4 +2,5 @@ dielectrics_remote_windeta.razPrefix=Stop application
dielectrics_remote_windeta.remote_application=Remote application
dielectrics_remote_windeta.serverPrefix=Server endpoint
dielectrics_remote_windeta.presetPrefix=Preset
dielectrics_remote_windeta.startTimeoutPrefix=Start timeout
\ No newline at end of file
dielectrics_remote_windeta.startTimeoutPrefix=Start timeout
dielectrics_remote_windeta.dataFilenameFormatPrefix=Data Filename
\ No newline at end of file
......@@ -3,5 +3,6 @@
<composite>
<text role="dielectrics_remote_windeta1" property="preset" prefix="dielectrics_remote_windeta.presetPrefix"/>
<property_combo role="dielectrics_remote_windeta1" property="data_filename_format" prefix="dielectrics_remote_windeta.dataFilenameFormatPrefix"/>
</composite>
</plugin>
......@@ -6,5 +6,9 @@
<property name="preset" type="string" max_length="30"/>
<property name="number_of_points" type="int32"/>
<property name="start_timeout" type="int32"/>
<property name="data_filename_format" type="string"/>
<property name="data_filename_format_size" type="int32"/>
<dynamic_property name="data_filename_format_values" type="string" size_property="data_filename_format_size"/>
<dynamic_property name="data_filename_format_labels" type="string" size_property="data_filename_format_size"/>
</controller>
<plugin>
<controller type="dielectrics_remote_windeta" role="dielectrics_remote_windeta1"/>
<simple_label prefix="dielectrics_remote_windeta.serverPrefix" font_size="9" hAlignment="left"/>
<text role="dielectrics_remote_windeta1" property="cameo_server"/>
<button role="dielectrics_remote_windeta1" command="raz" prefix="dielectrics_remote_windeta.razPrefix"/>
<text role="dielectrics_remote_windeta1" property="start_timeout" prefix="dielectrics_remote_windeta.startTimeoutPrefix"/>
<group title="dielectrics_remote_windeta.remote_application">
<simple_label prefix="dielectrics_remote_windeta.serverPrefix" font_size="9" hAlignment="left"/>
<text role="dielectrics_remote_windeta1" property="cameo_server"/>
<button role="dielectrics_remote_windeta1" command="raz" prefix="dielectrics_remote_windeta.razPrefix"/>
</group>
<newLine/>
<newLine/>
<text role="dielectrics_remote_windeta1" property="preset" prefix="dielectrics_remote_windeta.presetPrefix"/>
<table_composite nbColumns="2">
<simple_label prefix="dielectrics_remote_windeta.startTimeoutPrefix"/>
<text role="dielectrics_remote_windeta1" property="start_timeout"/>
<simple_label prefix="dielectrics_remote_windeta.presetPrefix"/>
<text role="dielectrics_remote_windeta1" property="preset"/>
<simple_label prefix="dielectrics_remote_windeta.dataFilenameFormatPrefix"/>
<property_combo role="dielectrics_remote_windeta1" property="data_filename_format"/>
</table_composite>
</plugin>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment