Skip to content

Commit

Permalink
Merge pull request #124 from Negusbuk/master
Browse files Browse the repository at this point in the history
working version of pump station software
  • Loading branch information
Negusbuk authored Jul 11, 2017
2 parents 38e5e03 + 12e0754 commit 50ff8ee
Show file tree
Hide file tree
Showing 25 changed files with 613 additions and 268 deletions.
1 change: 1 addition & 0 deletions common/ApplicationConfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public:

template <class aType> void setValue(std::string const& key, aType value) {
QVariant variant(value);
keyvalueMap_.erase(key);
keyvalueMap_.insert(std::make_pair(key, variant.toString().toStdString()));
}

Expand Down
156 changes: 101 additions & 55 deletions common/ApplicationConfigWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,66 +28,112 @@ void ApplicationConfigWriter::write(std::multimap<std::string,std::string> &keyv

void ApplicationConfigWriter::writeMerge(std::multimap<std::string,std::string> &keyvalueMap)
{
std::multimap<std::string,std::string> tmap = keyvalueMap;
std::ostringstream ostream;

std::ifstream file( outputFileName_.c_str(), std::ios::in );
if( !file.good() ) {
std::cerr << " [ApplicationConfigWriter::openAndCheckFile] ** ERROR: failed to open file: "
<< outputFileName_ << "." << std::endl;
QMessageBox::critical( 0, tr("[ApplicationConfigWriter::fill]"),
QString("Failed to open configuration file: \"%1\". No chance!").arg(QString(outputFileName_.c_str())),
QMessageBox::Abort );
throw; // must abort
}
std::map<std::string,std::string> keyMap;
std::multimap<std::string,std::string> tmap = keyvalueMap;
std::ostringstream ostream;

std::string Key;
std::string Value;
std::string buffer;

while (std::getline(file, buffer)) {
while (buffer[0]==' ') buffer = buffer.substr(1, buffer.length());
if (buffer[0]=='\0' || buffer[0]=='\n' || buffer[0]=='#') {
ostream << buffer << std::endl;
continue;
}

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

std::map<std::string,std::string>::iterator it = tmap.find(Key);
if (it!=tmap.end()) {
Value = it->second;
tmap.erase(it);
}
ostream.fill(' ');
ostream.width(25);
ostream << std::left << Key;
ostream << Value << std::endl;
}

for (auto v : tmap) {
ostream.fill(' ');
ostream.width(25);
ostream << std::left << v.first;
ostream << v.second << std::endl;
}

file.close();

std::ofstream ofile(outputFileName_.c_str(), std::ios::trunc);
ofile << ostream.str() << std::endl;
ofile.close();
std::ifstream file( outputFileName_.c_str(), std::ios::in );
if( !file.good() ) {
std::cerr << " [ApplicationConfigWriter::openAndCheckFile] ** ERROR: failed to open file: "
<< outputFileName_ << "." << std::endl;
QMessageBox::critical( 0, tr("[ApplicationConfigWriter::fill]"),
QString("Failed to open configuration file: \"%1\". No chance!").arg(QString(outputFileName_.c_str())),
QMessageBox::Abort );
throw; // must abort
}

int count;
std::string Key;
std::string Value;
std::string buffer;

while (std::getline(file, buffer)) {
while (buffer[0]==' ') buffer = buffer.substr(1, buffer.length());
if (buffer[0]=='\0' || buffer[0]=='\n' || buffer[0]=='#') {
ostream << buffer << std::endl;
continue;
}

std::istringstream iss(buffer.c_str(), std::istringstream::in);
iss >> Key;
std::map<std::string,std::string>::iterator itFind = keyMap.find(Key);
std::map<std::string,std::string>::iterator it = tmap.find(Key);
if (it!=tmap.end()) {
if (itFind==keyMap.end()) {
ostream.fill(' ');
ostream.width(25);
ostream << std::left << it->first;

count = 0;
auto range = tmap.equal_range(it->first);
for (auto i = range.first; i != range.second; ++i) {
if (count>0) ostream << " ";
ostream << i->second;
count++;
}
ostream << "\n";

keyMap.insert(std::make_pair(it->first, it->first));
}
}
}

for (std::map<std::string,std::string>::iterator it = keyMap.begin();
it!=keyMap.end();
++it) {
tmap.erase(it->first);
}

for (auto v = tmap.begin();
v!=tmap.end();
++v) {

std::map<std::string,std::string>::iterator itFind = keyMap.find(v->first);
if (itFind==keyMap.end()) {
ostream.fill(' ');
ostream.width(25);
ostream << std::left << v->first;

count = 0;
auto range = tmap.equal_range(v->first);
for (auto i = range.first; i != range.second; ++i) {
if (count>0) ostream << " ";
ostream << i->second;
count++;
}
ostream << "\n";

keyMap.insert(std::make_pair(v->first, v->first));
}
}

file.close();

std::ofstream ofile(outputFileName_.c_str(), std::ios::trunc);
ofile << ostream.str() << std::endl;
ofile.close();
}

void ApplicationConfigWriter::writeNew(std::multimap<std::string,std::string> &keyvalueMap)
{
std::map<std::string,std::string> keyMap;
std::ofstream file(outputFileName_.c_str(), std::ios::out);
for (auto v : keyvalueMap) {
file.fill(' ');
file.width(25);
file << std::left << v.first;
file << v.second << std::endl;
for (auto v = keyvalueMap.begin();
v!=keyvalueMap.end();
++v) {

std::map<std::string,std::string>::iterator itFind = keyMap.find(v->first);
if (itFind==keyMap.end()) {
file.fill(' ');
file.width(25);
file << std::left << v->first;

auto range = keyvalueMap.equal_range(v->first);
for (auto i = range.first; i != range.second; ++i) {
file << i->second << " ";
}

keyMap.insert(std::make_pair(v->first, v->first));
}
}
}
188 changes: 96 additions & 92 deletions common/ConradModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ ConradModel::ConradModel(QObject * /* parent */)
{
setDeviceEnabled(true);
setControlsEnabled(true);

NQLog("ConradModel") << "constructed";
}

ConradModel::ConradModel(const char* port, QObject * /* parent */)
Expand Down Expand Up @@ -70,107 +72,109 @@ void ConradModel::initialize( void )
// Announce new states
if (status.size() == 8) {

setAllSwitchesReady(status);
setDeviceState( READY );
setAllSwitchesReady(status);
setDeviceState( READY );
}

#else

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()) {

// Compose full absolute location of USB device
port = QString( "/dev/" ) + *it;
renewController( port );

++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 {
/*
// 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;
renewController( port );

++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)
<< "** 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 {
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.";
}
*/

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 {

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.";

}
} else {

renewController(port_);

controller_->initialize();

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

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

setAllSwitchesReady(status);
setDeviceState( READY );
}
NQLog("ConradModel") << "initialize() " << port_;

renewController(port_);

controller_->initialize();

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

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

setAllSwitchesReady(status);
setDeviceState( READY );
}
}
#endif
}
Expand Down
1 change: 1 addition & 0 deletions common_test/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
common_test
common_test.pro
testout.cfg
*.o
.qmake.stash
.qmake.cache
Loading

0 comments on commit 50ff8ee

Please sign in to comment.