Commit 333ff79c authored by legoc's avatar legoc
Browse files

First implementation of NSXToolController with plot of reduced data

parent b753bb8e
<module name="nsxtool">
<controller class="nsxtool::NSXToolController"/>
<include path="$(NOMAD_HOME)/src/contrib"/>
<include path="$(NOMAD_HOME)/../NomadModules/src"/>
</module>
/*
* Nomad Instrument Control Software
*
* Copyright 2011 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
#include "NSXToolController.h"
#include "controllers/common/family/Families.h"
#include "rapidjson/document.h"
#include "rapidjson/rapidjson.h"
#include <boost/lexical_cast.hpp>
#include <common/base/Date.h>
#include <fstream>
#include <iostream>
#include <napi.h>
namespace nsxtool {
using namespace std;
using namespace common;
using namespace boost;
using namespace cameo;
using namespace rapidjson;
const std::string NSXToolController::TYPE = "nsxtool_controller";
const std::string NSXToolController::REMOTE_APPLICATION = "responder-server";
const std::string NSXToolController::RESPONDER = "responder";
NSXToolController::NSXToolController(const std::string& name) :
ExperimentController(name), controller::Stoppable(this) {
setFamily(family::ACQUISITION, family::COUNT);
serverEndpoint.init(this, SAVE, "cameo_server");
initialized.init(this, NOSAVE, "initialized");
fileDirectory.init(this, SAVE, "file_directory");
filename.init(this, SAVE, "filename");
x.init(this, NOSAVE, "x");
y.init(this, NOSAVE, "y");
data.init(this, NOSAVE, "data");
}
NSXToolController::NSXToolController(const NSXToolController& controller) :
ExperimentController(controller), controller::Stoppable(this) {
}
NSXToolController::~NSXToolController() {
if (m_remoteApplication.get() != NULL) {
// Stop the remote application.
m_remoteApplication->stop();
// Wait for the termination
application::State state = m_remoteApplication->waitFor();
cout << "Remote application " << m_remoteApplication->getName() << " terminated with state " << application::toString(state) << endl;
}
}
void NSXToolController::postConfiguration() {
if (serverEndpoint() == "") {
m_server.reset(new Server(application::This::getServer().getEndpoint()));
} else {
m_server.reset(new Server(serverEndpoint()));
}
initialized = initApplication();
}
bool NSXToolController::initApplication() {
// Do not initialize if it is already done
if (initialized()) {
return true;
}
// Start the remote server
if (!m_server->isAvailable(1000)) {
cout << "Remote server is not available" << endl;
return false;
}
cout << "Remote server is connected" << endl;
m_remoteApplication = m_server->connect(REMOTE_APPLICATION);
if (m_remoteApplication->exists()) {
// The application exists from a previous server session
m_remoteApplication->kill();
application::State state = m_remoteApplication->waitFor();
cout << "Terminated remote application with state " << application::toString(state) << endl;
}
m_remoteApplication = m_server->start(REMOTE_APPLICATION);
if (!m_remoteApplication->exists()) {
cout << "No remote application" << endl;
return false;
}
// Application initialized
initialized = true;
return true;
}
void NSXToolController::readFile(const std::string& nexusFileName, std::string& fileContent) {
streampos size;
ifstream file(nexusFileName.c_str(), ios::in | ios::binary | ios::ate);
if (file.is_open()) {
size = file.tellg();
// Reserve the string.
fileContent.resize(size);
// Get the allocated array.
char * array = (char *) fileContent.data();
file.seekg(0, ios::beg);
file.read(array, size);
file.close();
} else {
cerr << "File " << nexusFileName << " cannot be read." << endl;
}
}
void NSXToolController::readProcessedData(const std::string& response) {
//cout << "response " << endl << response << endl;
Document document;
document.Parse(response.c_str());
// Get the array.
Document::Array array = document.GetArray();
// Check y size.
int ySize = array.Size();
if (ySize == 0) {
return;
}
// We resize the y data.
resizeData(y, ySize);
// Check that all the lines have the same number of elements.
int xSize = array[0].GetArray().Size();
// Iterate the lines.
for (int i = 1; i < ySize; ++i) {
Document::Array lineArray = array[i].GetArray();
if (lineArray.Size() != xSize) {
cerr << "Bad number of elements when reading response from NSXTool" << endl;
return;
}
}
// We can resize the x data and the z data.
resizeData(x, xSize);
resizeData(data, xSize * ySize);
// Reading the data by iterating the lines.
for (int i = 0; i < ySize; ++i) {
Document::Array lineArray = array[i].GetArray();
// Copy the elements.
for (int j = 0; j < xSize; ++j) {
data()[i * xSize + j] = lineArray[j].GetDouble();
}
}
x.sendEvent();
y.sendEvent();
data.sendEvent();
cout << "Processing finished." << endl;
}
void NSXToolController::start() {
string test = "[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0, 12.0]]";
readProcessedData(test);
if (!initApplication()) {
return;
}
// Send a simple message with the content of the Nexus data file.
string fileContent;
// Read the file.
readFile(fileDirectory() + "/" + filename(), fileContent);
// Create the requester
auto_ptr<application::Requester> requester = application::Requester::create(*m_remoteApplication, RESPONDER);
if (requester.get() == 0) {
cout << "requester error" << endl;
return;
}
// Start date.
Date begin;
// Send the file content to the server.
requester->sendBinary(fileContent);
// Wait for the response from the server.
string response;
requester->receive(response);
// Print the duration.
Date end;
double ms = (end - begin).getMilliseconds();
cout << "File processed in " << ms << " ms" << endl;
// Read the file.
readProcessedData(response);
}
void NSXToolController::stop() {
}
void NSXToolController::resizeData(ArrayProperty<float64> & property, int size) {
if (size != property.getSize()) {
delete [] property.get();
double * values = new double[size];
for (int i = 0; i < size; ++i) {
values[i] = static_cast<float64>(i);
}
property.set(values);
property.setSize(size);
}
}
}
/*
* Nomad Instrument Control Software
*
* Copyright 2011 Institut Laue-Langevin
*
* Licensed under the EUPL, Version 1.1 only (the "License");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
#ifndef NSXTOOL_NSXTOOLCONTROLLER_H
#define NSXTOOL_NSXTOOLCONTROLLER_H
#include <Controller.h>
#include <cameo/cameo.h>
namespace nsxtool {
class NSXToolController : public ExperimentController,
public controller::Stoppable {
public:
//! Type of controller
static const std::string TYPE;
NSXToolController(const std::string& name);
NSXToolController(const NSXToolController& controller);
virtual ~NSXToolController();
virtual void postConfiguration();
virtual void start();
virtual void stop();
Property<std::string> serverEndpoint;
Property<bool> initialized;
Property<string> fileDirectory;
Property<string> filename;
ArrayProperty<float64> x;
ArrayProperty<float64> y;
ArrayProperty<float64> data;
private:
bool initApplication();
void readFile(const std::string& nexusFileName, std::string& fileContent);
void readProcessedData(const std::string& response);
void resizeData(ArrayProperty<float64> & property, int size);
static const std::string REMOTE_APPLICATION;
static const std::string RESPONDER;
std::auto_ptr<cameo::Server> m_server;
std::auto_ptr<cameo::application::Instance> m_remoteApplication;
};
}
#endif
<plugin>
<controller type="nsxtool_controller" role="nsxtool_controller1"/>
<number_of_lines nb_lines="1"/>
<text role="nsxtool_controller1" property="filename" prefix="Filename"/>
</plugin>
<plotdatas>
<plotdata key="data" color="1F96C0" plugins="SETUP">
<dataX p_role="nsxtool_controller1" p_name="x"/>
<dataY p_role="nsxtool_controller1" p_name="y"/>
<dataZ p_role="nsxtool_controller1" p_name="data"/>
</plotdata>
</plotdatas>
<controller_plugin_config type="nsxtool_controller">
<image key="ACQUISITION"/>
<settings view="nsxtool_controllerView.xml"/>
<command view="nsxtool_controllerCommandView.xml"/>
</controller_plugin_config>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<controller type="nsxtool_controller">
<property name="x" type="doublearray">
</property>
<property name="y" type="doublearray">
</property>
<property name="data" type="doublearray">
</property>
</controller>
<plugin>
<controller type="nsxtool_controller" role="nsxtool_controller1"/>
<text role="nsxtool_controller1" property="filename" prefix="Filename"/>
<newLine/>
<plot_launcher role="nsxtool_controller1" data="data" title="data" height="20"/>
</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