class CYCData: findData() added; various cleanup

This commit is contained in:
Roland Jax
2014-04-18 19:45:05 +02:00
parent fdc52ee122
commit e9a4721c8f
7 changed files with 74 additions and 31 deletions
+66 -18
View File
@@ -19,9 +19,23 @@
#include "cycdata.h"
#include "logger.h"
#include <sstream>
#include <iomanip>
extern LogInstance& L;
CYCData::CYCData(EBusLoop* ebusloop, Commands* commands)
: m_ebusloop(ebusloop), m_commands(commands), m_stop(false)
{
for (size_t index = 0; index < commands->size(); index++) {
if (strcasecmp((*m_commands)[index][0].c_str(),"cyc") == 0) {
Command* cmd = new Command(index, (*m_commands)[index]);
m_cycDB.insert(pair_t(index, cmd));
}
}
}
CYCData::~CYCData()
{
for (mapCI_t iter = m_cycDB.begin(); iter != m_cycDB.end(); ++iter)
@@ -37,8 +51,8 @@ void* CYCData::run()
if (skipfirst == true) {
L.log(cyc, trace, "%s", data.c_str());
int index = m_commands->findData(data);
int index = findData(data);
if (index >= 0) {
std::string tmp;
@@ -61,22 +75,6 @@ void* CYCData::run()
return NULL;
}
void CYCData::storeData(int index, std::string data)
{
mapCI_t iter = m_cycDB.find(index);
if (iter != m_cycDB.end()) {
iter->second->setData(data);
L.log(cyc, debug, " [%d] %s -> replaced", index, data.c_str());
} else {
Command* cmd = new Command(index, (*m_commands)[index], data);
m_cycDB.insert(pair_t(index, cmd));
L.log(cyc, debug, " [%d] %s -> inserted", index, data.c_str());
}
L.log(cyc, debug, " cycDB entries: %d", m_cycDB.size());
}
std::string CYCData::getData(int index)
{
mapCI_t iter = m_cycDB.find(index);
@@ -86,3 +84,53 @@ std::string CYCData::getData(int index)
return "";
}
int CYCData::findData(const std::string& data) const
{
// no commands definend
if (m_cycDB.size() == 0)
return -2;
// skip to small search string length
if (data.length() < 10)
return -3;
// preapre string for searching command
std::string search(data.substr(2, 8 + atoi(data.substr(8,2).c_str()) * 2));
std::size_t index;
mapCI_t i = m_cycDB.begin();
// walk through commands
for (index = 0; i != m_cycDB.end(); i++, index++) {
cmd_t cmd = i->second->getCommand();
// prepare string for defined command
std::string command(cmd[5]);
command += cmd[6];
std::stringstream sstr;
sstr << std::setw(2) << std::hex << std::setfill('0') << cmd[7];
command += sstr.str();
command += cmd[8];
// skip wrong search string length
if (command.length() > search.length())
continue;
if (strcasecmp(command.c_str(), search.substr(0,command.length()).c_str()) == 0)
return i->first;
}
// command not found
return -1;
}
void CYCData::storeData(int index, std::string data)
{
mapCI_t iter = m_cycDB.find(index);
if (iter != m_cycDB.end()) {
iter->second->setData(data);
L.log(cyc, debug, " [%d] data saved", index);
}
}