Skip to content

Commit

Permalink
Merge pull request #123 from Negusbuk/master
Browse files Browse the repository at this point in the history
first test version of pump station control software
  • Loading branch information
Negusbuk authored Jun 29, 2017
2 parents 4f21cdb + 7c0375f commit 38e5e03
Show file tree
Hide file tree
Showing 40 changed files with 1,873 additions and 493 deletions.
5 changes: 5 additions & 0 deletions .pydevproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
</pydev_project>
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ifeq ($(NOPLASMA),0)
qtsubdirs += plasma
endif
ifeq ($(NOPUMPSTATION),0)
qtsubdirs += pumpstation/daemon pumpstation/controller pumpstation/app
qtsubdirs += pumpstation/daemon pumpstation/controller pumpstation/app pumpstation/analysis
endif

installsubdirs = devices external common pumpstation
Expand Down
6 changes: 3 additions & 3 deletions common/ApplicationConfigReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ void ApplicationConfigReader::fill(std::multimap<std::string,std::string> &keyva

std::istringstream iss(buffer.c_str(), std::istringstream::in);
iss >> Key;
iss >> Value;

keyvalueMap.insert(std::make_pair(Key, Value));
while (iss >> Value) {
keyvalueMap.insert(std::make_pair(Key, Value));
}
}

file.close();
Expand Down
154 changes: 90 additions & 64 deletions common/ConradModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ ConradModel::ConradModel(QObject * /* parent */)
setControlsEnabled(true);
}

ConradModel::ConradModel(const char* port, QObject * /* parent */)
: QObject(),
AbstractDeviceModel(),
port_(port),
switchStates_(8, OFF)
{
setDeviceEnabled(true);
setControlsEnabled(true);
}

/// Shuts down all the devices before closing down.
ConradModel::~ConradModel()
{
Expand Down Expand Up @@ -66,85 +76,101 @@ void ConradModel::initialize( void )

#else

// create a list with all available ttyUSB device (system) files
QStringList filters("ttyUSB*");

QDir devDir("/dev");
devDir.setNameFilters(filters);
devDir.setFilter(QDir::System);
QStringList list = devDir.entryList();

// Only loop over list when not empty
if (!list.empty()) {

// browse and try initialize() until conradController_ gets an answer
QStringList::const_iterator it = list.begin();
QString port = QString( "/dev/" ) + *it;

renewController( port );

while (it < list.end() && !controller_->initialize()) {

// Compose full absolute location of USB device
port = QString( "/dev/" ) + *it;
if (port_.isEmpty()) {
// create a list with all available ttyUSB device (system) files
QStringList filters("ttyUSB*");

QDir devDir("/dev");
devDir.setNameFilters(filters);
devDir.setFilter(QDir::System);
QStringList list = devDir.entryList();

// Only loop over list when not empty
if (!list.empty()) {

// browse and try initialize() until conradController_ gets an answer
QStringList::const_iterator it = list.begin();
QString port = QString( "/dev/" ) + *it;

renewController( port );

while (it < list.end() && !controller_->initialize()) {

++it;
}

// check communication; if it is not at the end, switch was found
if (it != list.end()) {
// Compose full absolute location of USB device
port = QString( "/dev/" ) + *it;
renewController( port );

// read and init status
std::vector<bool> status = controller_->queryStatus();

// Announce new states
if (status.size() == 8) {

setAllSwitchesReady( status );
setDeviceState( READY );

// FIXME Redirect to central logger
// if( debugLevel_ >= 1 )
NQLog("ConradModel::initialize()", NQLog::Message)
++it;
}

// check communication; if it is not at the end, switch was found
if (it != list.end()) {

// read and init status
std::vector<bool> status = controller_->queryStatus();

// Announce new states
if (status.size() == 8) {

setAllSwitchesReady( status );
setDeviceState( READY );

// FIXME Redirect to central logger
// if( debugLevel_ >= 1 )
NQLog("ConradModel::initialize()", NQLog::Message)
<< "connection to conrad via: "
<< port.toStdString() << ".";

} else {

/*
would be 0 if query failed (according to ConradController::queryStatus)
This means device malfunction, so set state accordingly
*/

setDeviceFullOff();

NQLog("ConradModel::initialize()", NQLog::Fatal)
} else {
/*
would be 0 if query failed (according to ConradController::queryStatus)
This means device malfunction, so set state accordingly
*/
setDeviceFullOff();
NQLog("ConradModel::initialize()", NQLog::Fatal)
<< "** ERROR: received malformed state vector.";


}
} else {
// if not successful; i.e. DEVICE NOT FOUND
setDeviceFullOff();
// TODO Log why it failed
NQLog("ConradModel::initialize()", NQLog::Fatal)
<< "Cannot connect to Conrad. Make sure that";
NQLog("ConradModel::initialize()", NQLog::Fatal)
<< "/dev/ttyUSB* is present and readable and no other process";
NQLog("ConradModel::initialize()", NQLog::Fatal)
<< "is connecting to the device.";
}
} else {
// if not successful; i.e. DEVICE NOT FOUND

setDeviceFullOff();
// TODO Log why it failed

NQLog("ConradModel::initialize()", NQLog::Fatal)
<< "Cannot connect to Conrad. Make sure that";
<< "Cannot connect to Conrad. Make sure that";
NQLog("ConradModel::initialize()", NQLog::Fatal)
<< "/dev/ttyUSB* is present and readable and no other process";
<< "/dev/ttyUSB* is present and readable and no other process";
NQLog("ConradModel::initialize()", NQLog::Fatal)
<< "is connecting to the device.";
<< "is connecting to the device.";

}
} else {

setDeviceFullOff();

NQLog("ConradModel::initialize()", NQLog::Fatal)
<< "Cannot connect to Conrad. Make sure that";
NQLog("ConradModel::initialize()", NQLog::Fatal)
<< "/dev/ttyUSB* is present and readable and no other process";
NQLog("ConradModel::initialize()", NQLog::Fatal)
<< "is connecting to the device.";

renewController(port_);

controller_->initialize();

std::vector<bool> status = controller_->queryStatus();

// Announce new states
if (status.size() == 8) {

setAllSwitchesReady(status);
setDeviceState( READY );
}
}
#endif
}
Expand Down
5 changes: 5 additions & 0 deletions common/ConradModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class ConradModel :

// TODO Add initialisation parameters
explicit ConradModel(QObject *parent = 0);
explicit ConradModel(const char* port,
QObject *parent = 0);
virtual ~ConradModel();

// Methods for power control and status querying of the devices connected to
Expand All @@ -58,6 +60,9 @@ public slots:
void setSwitchEnabled(int device, bool enabled);

protected:

QString port_;

void initialize();
void close();

Expand Down
83 changes: 83 additions & 0 deletions common/Fifo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#ifndef FIFO_H
#define FIFO_H

#include <iostream>

#include <QVector>

template <class T> class Fifo
{
public:

typedef T value_type;

Fifo(int N) {
buffer_.resize(N);
size_ = 0;
capacity_ = N;
currentFirstIndex_ = 0;
}

int size() const {
return size_;
}

virtual void push(const T& value) {
if (size_<capacity_) {
buffer_[size_] = value;
size_++;
currentFirstIndex_ = size_ - 1;
} else {
currentFirstIndex_++;
if (currentFirstIndex_>=capacity_) currentFirstIndex_ = 0;
buffer_[currentFirstIndex_] = value;
}
}

const T & at(int i) const {
if (i>=size_) return last();
int idx = currentFirstIndex_ - i;
if (idx<0) idx += size_;
return buffer_[idx];
}

T & first() {
return buffer_[currentFirstIndex_];
}

const T & first() const {
return buffer_[currentFirstIndex_];
}

T & last() {
int idx = currentFirstIndex_;
if (size_<capacity_) {
idx = 0;
} else {
idx++;
if (idx>=capacity_) idx = 0;
}
return buffer_[idx];
}

const T & last() const {
int idx = currentFirstIndex_;
if (size_<capacity_) {
idx = 0;
} else {
idx++;
if (idx>=capacity_) idx = 0;
}
return buffer_[idx];
}

protected:

int size_;
int capacity_;
int currentFirstIndex_;

QVector<T> buffer_;
};

#endif // RINGBUFFER_H
Loading

0 comments on commit 38e5e03

Please sign in to comment.