Added functions ControllerMessage::msgClass(), ControllerMessage::model(), ControllerMessage::getIntMessage() and ControllerMessage::hasParameter()

This commit is contained in:
Micke Prag 2011-05-12 14:49:31 +00:00
parent 0b12c53794
commit 183cc7ca0d
2 changed files with 60 additions and 22 deletions

View file

@ -2,12 +2,14 @@
#include "Device.h"
#include "Strings.h"
#include "common.h"
#include <map>
class ControllerMessage::PrivateData {
public:
std::map<std::string, std::string> parameters;
std::string protocol;
std::string protocol, model, msgClass;
int method;
};
@ -24,8 +26,12 @@ ControllerMessage::ControllerMessage(const std::string &message) {
if (delim == std::string::npos) {
break;
}
if (param.substr(0, delim).compare("protocol") == 0) {
if (param.substr(0, delim).compare("class") == 0) {
d->msgClass = param.substr(delim+1, param.length()-delim);
} else if (param.substr(0, delim).compare("protocol") == 0) {
d->protocol = param.substr(delim+1, param.length()-delim);
} else if (param.substr(0, delim).compare("model") == 0) {
d->model = param.substr(delim+1, param.length()-delim);
} else if (param.substr(0, delim).compare("method") == 0) {
d->method = Device::methodId(param.substr(delim+1, param.length()-delim));
} else {
@ -39,6 +45,10 @@ ControllerMessage::~ControllerMessage(){
delete d;
}
std::string ControllerMessage::msgClass() const {
return d->msgClass;
}
int ControllerMessage::method() const {
return d->method;
}
@ -47,10 +57,33 @@ std::wstring ControllerMessage::protocol() const {
return TelldusCore::charToWstring(d->protocol.c_str());
}
std::string ControllerMessage::getParameter(const std::string &key){
std::wstring ControllerMessage::model() const {
return TelldusCore::charToWstring(d->model.c_str());
}
int ControllerMessage::getIntParameter(const std::string &key) const {
std::string strValue = getParameter(key);
if (strValue.compare("") == 0) {
return -1;
}
if (strValue.substr(0,2).compare("0x") == 0) {
return strtol(strValue.c_str(), NULL, 16);
}
return strtol(strValue.c_str(), NULL, 10);
}
std::string ControllerMessage::getParameter(const std::string &key) const {
std::map<std::string, std::string>::iterator it = d->parameters.find(key);
if (it == d->parameters.end()) {
return "";
}
return d->parameters[key];
}
}
bool ControllerMessage::hasParameter(const std::string &key) const {
std::map<std::string, std::string>::iterator it = d->parameters.find(key);
if (it == d->parameters.end()) {
return false;
}
return true;
}

View file

@ -1,20 +1,25 @@
#ifndef CONTROLLERMESSAGE_H
#define CONTROLLERMESSAGE_H
#include <string>
class ControllerMessage {
public:
ControllerMessage(const std::string &rawMessage);
virtual ~ControllerMessage();
std::string getParameter(const std::string &key);
int method() const;
std::wstring protocol() const;
private:
class PrivateData;
PrivateData *d;
};
#define CONTROLLERMESSAGE_H
#include <string>
class ControllerMessage {
public:
ControllerMessage(const std::string &rawMessage);
virtual ~ControllerMessage();
std::string msgClass() const;
int getIntParameter(const std::string &key) const;
std::string getParameter(const std::string &key) const;
int method() const;
std::wstring protocol() const;
std::wstring model() const;
bool hasParameter(const std::string &key) const;
private:
class PrivateData;
PrivateData *d;
};
#endif //CONTROLLERMESSAGE_H