Added parameter int supportedMethods in tdLastSentCommand(), closes #36

This commit is contained in:
Micke Prag 2009-08-19 15:51:33 +00:00
parent 33d3e60ebf
commit fbbb117a05
25 changed files with 71 additions and 94 deletions

View file

@ -22,6 +22,9 @@ Device::~Device(void) {
int Device::switchState( int newState, const std::string &value ) { int Device::switchState( int newState, const std::string &value ) {
int retVal = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED; int retVal = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
if (!Device::maskUnsupportedMethods(this->methods(), newState)) {
return retVal;
}
std::string stateValue = ""; std::string stateValue = "";
switch (newState) { switch (newState) {
@ -128,3 +131,12 @@ bool Device::setName(const std::string & newName) {
} }
return false; return false;
} }
int TelldusCore::Device::maskUnsupportedMethods(int methods, int supportedMethods) {
// Bell -> On
if ((methods & TELLSTICK_BELL) && !(supportedMethods & TELLSTICK_BELL)) {
methods |= TELLSTICK_TURNON;
}
//Cut of the rest of the unsupported methods we donät have a fallback for
return methods & supportedMethods;
}

View file

@ -6,12 +6,6 @@
namespace TelldusCore { namespace TelldusCore {
const int ALL_METHODS =
TELLSTICK_TURNON |
TELLSTICK_TURNOFF |
TELLSTICK_BELL |
TELLSTICK_DIM;
class Device class Device
{ {
public: public:
@ -19,7 +13,7 @@ namespace TelldusCore {
virtual ~Device(void); virtual ~Device(void);
int switchState( int newState, const std::string &value = "" ); int switchState( int newState, const std::string &value = "" );
virtual int methods(int methodsSupported) = 0; virtual int methods() = 0;
virtual std::string getProtocol() const = 0; virtual std::string getProtocol() const = 0;
virtual bool parameterMatches( const std::string &name, const std::string &value ) const = 0; virtual bool parameterMatches( const std::string &name, const std::string &value ) const = 0;
bool setParameter(const std::string &strName, const std::string &strValue); bool setParameter(const std::string &strName, const std::string &strValue);
@ -30,6 +24,7 @@ namespace TelldusCore {
bool setName( const std::string &newName ); bool setName( const std::string &newName );
static int methodId( const std::string &methodName ); static int methodId( const std::string &methodName );
static int maskUnsupportedMethods( int methods, int supportedMethods );
#ifdef _LINUX #ifdef _LINUX
void setDevice(const std::string &device); void setDevice(const std::string &device);

View file

@ -87,7 +87,7 @@ bool DeviceBrateck::parameterMatches( const std::string &name, const std::string
/* /*
* Has the device got the method? * Has the device got the method?
*/ */
int DeviceBrateck::methods(int){ int DeviceBrateck::methods(){
return (TELLSTICK_TURNON | TELLSTICK_TURNOFF); return (TELLSTICK_TURNON | TELLSTICK_TURNOFF);
} }

View file

@ -10,7 +10,7 @@ namespace TelldusCore {
DeviceBrateck(int id, const std::string &model, const std::string &name); DeviceBrateck(int id, const std::string &model, const std::string &name);
virtual ~DeviceBrateck(void); virtual ~DeviceBrateck(void);
virtual int methods(int methodsSupported); virtual int methods();
virtual std::string getProtocol() const; virtual std::string getProtocol() const;
virtual bool parameterMatches( const std::string &name, const std::string &value ) const; virtual bool parameterMatches( const std::string &name, const std::string &value ) const;

View file

@ -57,7 +57,7 @@ int DeviceGroup::turnOn(void) {
int retVal = TELLSTICK_ERROR_UNKNOWN; int retVal = TELLSTICK_ERROR_UNKNOWN;
for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) { for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) {
int methods = (*it)->methods(ALL_METHODS); int methods = (*it)->methods();
if (methods & TELLSTICK_TURNON) { if (methods & TELLSTICK_TURNON) {
int success = (*it)->switchState( TELLSTICK_TURNON ); int success = (*it)->switchState( TELLSTICK_TURNON );
if (retVal != TELLSTICK_SUCCESS) { if (retVal != TELLSTICK_SUCCESS) {
@ -75,7 +75,7 @@ int DeviceGroup::turnOff(void) {
int retVal = TELLSTICK_ERROR_UNKNOWN; int retVal = TELLSTICK_ERROR_UNKNOWN;
for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) { for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) {
int methods = (*it)->methods(ALL_METHODS); int methods = (*it)->methods();
if (methods & TELLSTICK_TURNOFF) { if (methods & TELLSTICK_TURNOFF) {
int success = (*it)->switchState( TELLSTICK_TURNOFF ); int success = (*it)->switchState( TELLSTICK_TURNOFF );
if (retVal != TELLSTICK_SUCCESS) { if (retVal != TELLSTICK_SUCCESS) {
@ -93,7 +93,7 @@ int DeviceGroup::bell(void){
int retVal = TELLSTICK_ERROR_UNKNOWN; int retVal = TELLSTICK_ERROR_UNKNOWN;
for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) { for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) {
int methods = (*it)->methods(ALL_METHODS); int methods = (*it)->methods();
if (methods & TELLSTICK_BELL) { if (methods & TELLSTICK_BELL) {
int success = (*it)->switchState( TELLSTICK_BELL ); int success = (*it)->switchState( TELLSTICK_BELL );
if (retVal != TELLSTICK_SUCCESS) { if (retVal != TELLSTICK_SUCCESS) {
@ -111,7 +111,7 @@ int DeviceGroup::dim(unsigned char level){
int retVal = TELLSTICK_ERROR_UNKNOWN; int retVal = TELLSTICK_ERROR_UNKNOWN;
for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) { for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) {
int methods = (*it)->methods(ALL_METHODS); int methods = (*it)->methods();
if (methods & TELLSTICK_DIM) { if (methods & TELLSTICK_DIM) {
int success = (*it)->switchState( TELLSTICK_DIM, (char*)&level); int success = (*it)->switchState( TELLSTICK_DIM, (char*)&level);
if (retVal != TELLSTICK_SUCCESS) { if (retVal != TELLSTICK_SUCCESS) {
@ -129,11 +129,11 @@ bool DeviceGroup::parameterMatches( const std::string &name, const std::string &
/* /*
* Has the device got the method? * Has the device got the method?
*/ */
int DeviceGroup::methods(int supportedMethods){ int DeviceGroup::methods(){
int retVal = 0; int retVal = 0;
for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) { for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) {
retVal = retVal | (*it)->methods(supportedMethods); retVal = retVal | (*it)->methods();
} }
return retVal; return retVal;

View file

@ -15,7 +15,7 @@ namespace TelldusCore {
DeviceGroup(int id, const std::string &model, const std::string &name); DeviceGroup(int id, const std::string &model, const std::string &name);
~DeviceGroup(void); ~DeviceGroup(void);
virtual int methods(int methodsSupported); virtual int methods();
virtual std::string getProtocol() const; virtual std::string getProtocol() const;
virtual bool parameterMatches( const std::string &name, const std::string &value ) const; virtual bool parameterMatches( const std::string &name, const std::string &value ) const;

View file

@ -224,7 +224,7 @@ bool DeviceIkea::parameterMatches( const std::string &name, const std::string &v
/* /*
* Has the device got the method? * Has the device got the method?
*/ */
int DeviceIkea::methods(int){ int DeviceIkea::methods(){
return (TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM); return (TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM);
} }

View file

@ -10,7 +10,7 @@ namespace TelldusCore {
DeviceIkea(int id, const std::string &model, const std::string &name); DeviceIkea(int id, const std::string &model, const std::string &name);
virtual ~DeviceIkea(void); virtual ~DeviceIkea(void);
virtual int methods(int methodsSupported); virtual int methods();
virtual std::string getProtocol() const; virtual std::string getProtocol() const;
virtual bool parameterMatches( const std::string &name, const std::string &value ) const; virtual bool parameterMatches( const std::string &name, const std::string &value ) const;

View file

@ -63,7 +63,9 @@ bool DeviceNexa::setDeviceParameter(const std::string &strName, const std::strin
* Turn on this device * Turn on this device
*/ */
int DeviceNexa::turnOn(void){ int DeviceNexa::turnOn(void){
if (strcasecmp(this->getModel().c_str(), "bell") == 0) {
return bell();
}
try{ try{
std::string strCode = ""; std::string strCode = "";
if (isDimmer()) { if (isDimmer()) {
@ -243,7 +245,7 @@ bool DeviceNexa::parameterMatches( const std::string &name, const std::string &v
/* /*
* Has the device got the method? * Has the device got the method?
*/ */
int DeviceNexa::methods(int methodsSupported){ int DeviceNexa::methods(){
std::string strModel = this->getModel(); std::string strModel = this->getModel();
if ( strcasecmp(strModel.c_str(), "codeswitch") == 0 if ( strcasecmp(strModel.c_str(), "codeswitch") == 0
@ -254,11 +256,7 @@ int DeviceNexa::methods(int methodsSupported){
return (TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM); return (TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM);
} else if (strcasecmp(strModel.c_str(), "bell") == 0) { } else if (strcasecmp(strModel.c_str(), "bell") == 0) {
if (methodsSupported & TELLSTICK_BELL) { return TELLSTICK_BELL;
return TELLSTICK_BELL;
} else if (methodsSupported & TELLSTICK_TURNON) {
return TELLSTICK_TURNON;
}
} }
return 0; return 0;
} }

View file

@ -8,7 +8,7 @@ namespace TelldusCore {
{ {
public: public:
DeviceNexa(int id, const std::string &model, const std::string &name); DeviceNexa(int id, const std::string &model, const std::string &name);
virtual int methods(int methodsSupported); virtual int methods();
virtual std::string getProtocol() const; virtual std::string getProtocol() const;
virtual bool parameterMatches( const std::string &name, const std::string &value ) const; virtual bool parameterMatches( const std::string &name, const std::string &value ) const;

View file

@ -127,7 +127,7 @@ bool DeviceRisingSun::parameterMatches( const std::string &name, const std::stri
/* /*
* Has the device got the method? * Has the device got the method?
*/ */
int DeviceRisingSun::methods(int methodsSupported){ int DeviceRisingSun::methods(){
return (TELLSTICK_TURNON | TELLSTICK_TURNOFF); return (TELLSTICK_TURNON | TELLSTICK_TURNOFF);
} }

View file

@ -8,7 +8,7 @@ namespace TelldusCore {
{ {
public: public:
DeviceRisingSun(int id, const std::string &model, const std::string &name); DeviceRisingSun(int id, const std::string &model, const std::string &name);
virtual int methods(int methodsSupported); virtual int methods();
virtual std::string getProtocol() const; virtual std::string getProtocol() const;
virtual bool parameterMatches( const std::string &name, const std::string &value ) const; virtual bool parameterMatches( const std::string &name, const std::string &value ) const;

View file

@ -77,7 +77,7 @@ bool DeviceSartano::parameterMatches( const std::string &name, const std::string
/* /*
* Has the device got the method? * Has the device got the method?
*/ */
int DeviceSartano::methods(int){ int DeviceSartano::methods(){
return (TELLSTICK_TURNON | TELLSTICK_TURNOFF); return (TELLSTICK_TURNON | TELLSTICK_TURNOFF);
} }

View file

@ -10,7 +10,7 @@ namespace TelldusCore {
DeviceSartano(int id, const std::string &model, const std::string &name); DeviceSartano(int id, const std::string &model, const std::string &name);
virtual ~DeviceSartano(void); virtual ~DeviceSartano(void);
virtual int methods(int methodsSupported); virtual int methods();
virtual std::string getProtocol() const; virtual std::string getProtocol() const;
virtual bool parameterMatches( const std::string &name, const std::string &value ) const; virtual bool parameterMatches( const std::string &name, const std::string &value ) const;

View file

@ -27,7 +27,7 @@ bool DeviceUndefined::parameterMatches( const std::string &name, const std::stri
/* /*
* Has the device got the method? * Has the device got the method?
*/ */
int DeviceUndefined::methods(int methodsSupported){ int DeviceUndefined::methods(){
return 0; return 0;
} }

View file

@ -8,7 +8,7 @@ namespace TelldusCore {
{ {
public: public:
DeviceUndefined(int id, const std::string &model, const std::string &name); DeviceUndefined(int id, const std::string &model, const std::string &name);
virtual int methods(int methodsSupported); virtual int methods();
virtual std::string getProtocol() const; virtual std::string getProtocol() const;
virtual bool parameterMatches( const std::string &name, const std::string &value ) const; virtual bool parameterMatches( const std::string &name, const std::string &value ) const;

View file

@ -102,7 +102,7 @@ bool DeviceUpm::parameterMatches( const std::string &name, const std::string &va
/* /*
* Has the device got the method? * Has the device got the method?
*/ */
int DeviceUpm::methods(int){ int DeviceUpm::methods(){
return (TELLSTICK_TURNON | TELLSTICK_TURNOFF); return (TELLSTICK_TURNON | TELLSTICK_TURNOFF);
} }

View file

@ -10,7 +10,7 @@ namespace TelldusCore {
DeviceUpm(int id, const std::string &model, const std::string &name); DeviceUpm(int id, const std::string &model, const std::string &name);
virtual ~DeviceUpm(void); virtual ~DeviceUpm(void);
virtual int methods(int methodsSupported); virtual int methods();
virtual std::string getProtocol() const; virtual std::string getProtocol() const;
virtual bool parameterMatches( const std::string &name, const std::string &value ) const; virtual bool parameterMatches( const std::string &name, const std::string &value ) const;

View file

@ -39,6 +39,6 @@ int DeviceWaveman::turnOff(void){
/* /*
* Has the device got the method? * Has the device got the method?
*/ */
int DeviceWaveman::methods(int){ int DeviceWaveman::methods(){
return (TELLSTICK_TURNON | TELLSTICK_TURNOFF); return (TELLSTICK_TURNON | TELLSTICK_TURNOFF);
} }

View file

@ -8,7 +8,7 @@ namespace TelldusCore {
{ {
public: public:
DeviceWaveman(int id, const std::string &model, const std::string &name); DeviceWaveman(int id, const std::string &model, const std::string &name);
virtual int methods(int methodsSupported); virtual int methods();
protected: protected:
virtual int turnOff(void); virtual int turnOff(void);

View file

@ -193,7 +193,7 @@ bool DeviceX10::parameterMatches( const std::string &name, const std::string &va
/* /*
* Has the device got the method? * Has the device got the method?
*/ */
int DeviceX10::methods(int methodsSupported){ int DeviceX10::methods(){
return (TELLSTICK_TURNON | TELLSTICK_TURNOFF); return (TELLSTICK_TURNON | TELLSTICK_TURNOFF);
} }

View file

@ -8,7 +8,7 @@ namespace TelldusCore {
{ {
public: public:
DeviceX10(int id, const std::string &model, const std::string &name); DeviceX10(int id, const std::string &model, const std::string &name);
virtual int methods(int methodsSupported); virtual int methods();
virtual std::string getProtocol() const; virtual std::string getProtocol() const;
virtual bool parameterMatches( const std::string &name, const std::string &value ) const; virtual bool parameterMatches( const std::string &name, const std::string &value ) const;

View file

@ -237,7 +237,7 @@ void Manager::parseMessage( const std::string &message ) {
if (it->second->getProtocol().compare(protocol) != 0) { if (it->second->getProtocol().compare(protocol) != 0) {
continue; continue;
} }
if (! (it->second->methods(ALL_METHODS) & method)) { if (! (it->second->methods() & method)) {
continue; continue;
} }
bool found = true; bool found = true;

View file

@ -106,17 +106,7 @@ int WINAPI tdTurnOn(int intDeviceId){
Manager *manager = Manager::getInstance(); Manager *manager = Manager::getInstance();
Device* dev = manager->getDevice(intDeviceId); Device* dev = manager->getDevice(intDeviceId);
if(dev != NULL){ if(dev != NULL){
int methods = dev->methods( TELLSTICK_TURNON ); return dev->switchState(TELLSTICK_TURNON);
int retval = 0;
if ( !(methods & TELLSTICK_TURNON) ) {
retval = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
} else {
retval = dev->switchState(TELLSTICK_TURNON);
}
return retval;
} else{ } else{
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
@ -139,18 +129,8 @@ int WINAPI tdTurnOff(int intDeviceId){
Manager *manager = Manager::getInstance(); Manager *manager = Manager::getInstance();
Device* dev = manager->getDevice(intDeviceId); Device* dev = manager->getDevice(intDeviceId);
if(dev != NULL){ if(dev != NULL){
int methods = dev->methods( TELLSTICK_TURNOFF ); return dev->switchState(TELLSTICK_TURNOFF);
int retval = 0; } else {
if ( !(methods & TELLSTICK_TURNOFF) ) {
retval = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
} else {
retval = dev->switchState(TELLSTICK_TURNOFF);
}
return retval;
}
else{
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
} }
@ -171,19 +151,9 @@ int WINAPI tdBell(int intDeviceId){
try{ try{
Manager *manager = Manager::getInstance(); Manager *manager = Manager::getInstance();
Device* dev = manager->getDevice(intDeviceId); Device* dev = manager->getDevice(intDeviceId);
if(dev != NULL){ if (dev != NULL){
int methods = dev->methods( TELLSTICK_BELL ); return dev->switchState( TELLSTICK_BELL );
int retval = 0; } else {
if ( !(methods & TELLSTICK_BELL) ) {
retval = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
} else {
retval = dev->switchState( TELLSTICK_BELL );
}
return retval;
}
else{
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
} }
@ -205,24 +175,17 @@ int WINAPI tdDim(int intDeviceId, unsigned char level){
Manager *manager = Manager::getInstance(); Manager *manager = Manager::getInstance();
Device* dev = manager->getDevice(intDeviceId); Device* dev = manager->getDevice(intDeviceId);
if(dev != NULL){ if(dev != NULL){
int methods = dev->methods( TELLSTICK_DIM );
int retval = 0; int retval = 0;
if ( !(methods & TELLSTICK_DIM) ) { if (level == 0) {
retval = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED; retval = dev->switchState( TELLSTICK_TURNOFF );
} else if (level == 255) {
retval = dev->switchState( TELLSTICK_TURNON );
} else { } else {
if (level == 0) { retval = dev->switchState( TELLSTICK_DIM, (char *)&level);
retval = dev->switchState( TELLSTICK_TURNOFF );
} else if (level == 255) {
retval = dev->switchState( TELLSTICK_TURNON );
} else {
retval = dev->switchState( TELLSTICK_DIM, (char *)&level);
}
} }
return retval; return retval;
} } else {
else{
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
} }
@ -235,12 +198,21 @@ int WINAPI tdDim(int intDeviceId, unsigned char level){
/** /**
* Returns the last sent command to a specific device * Returns the last sent command to a specific device
* @param intDeviceId The device id to query * @param intDeviceId The device id to query
* @param methodsSupported The methods supported by the client. See tdMethods() for more information.
* @returns the last sent command as integer, example TELLSTICK_TURNON or TELLSTICK_TURNOFF * @returns the last sent command as integer, example TELLSTICK_TURNON or TELLSTICK_TURNOFF
*/ */
int WINAPI tdLastSentCommand( int intDeviceId ) { int WINAPI tdLastSentCommand( int intDeviceId, int methodsSupported ) {
Manager *manager = Manager::getInstance(); Manager *manager = Manager::getInstance();
int lastSentCommand = manager->getDeviceState( intDeviceId ); int lastSentCommand = Device::maskUnsupportedMethods(manager->getDeviceState( intDeviceId ), methodsSupported);
return (lastSentCommand > 0 ? lastSentCommand : TELLSTICK_TURNOFF );
if (lastSentCommand == TELLSTICK_BELL) {
//Bell is not a state
lastSentCommand = TELLSTICK_TURNOFF;
}
if (lastSentCommand == 0) {
lastSentCommand = TELLSTICK_TURNOFF;
}
return lastSentCommand;
} }
/** /**
@ -496,14 +468,14 @@ int WINAPI tdMethods(int id, int methodsSupported){
Manager *manager = Manager::getInstance(); Manager *manager = Manager::getInstance();
Device* dev = manager->getDevice(id); Device* dev = manager->getDevice(id);
if (dev != NULL) { if (dev != NULL) {
intMethods = dev->methods(methodsSupported); intMethods = dev->methods();
} }
} }
catch(exception e){ catch(exception e){
intMethods = 0; intMethods = 0;
handleException(e); handleException(e);
} }
intMethods = intMethods & methodsSupported; //Strip the methods not supported by client. intMethods = Device::maskUnsupportedMethods(intMethods, methodsSupported); //Strip the methods not supported by client.
return intMethods; return intMethods;
} }

View file

@ -40,7 +40,7 @@ extern "C" {
TELLSTICK_API int WINAPI tdBell(int intDeviceId); TELLSTICK_API int WINAPI tdBell(int intDeviceId);
TELLSTICK_API int WINAPI tdDim(int intDeviceId, unsigned char level); TELLSTICK_API int WINAPI tdDim(int intDeviceId, unsigned char level);
TELLSTICK_API int WINAPI tdMethods(int id, int methodsSupported); TELLSTICK_API int WINAPI tdMethods(int id, int methodsSupported);
TELLSTICK_API int WINAPI tdLastSentCommand( int intDeviceId ); TELLSTICK_API int WINAPI tdLastSentCommand( int intDeviceId, int methodsSupported );
TELLSTICK_API char *WINAPI tdLastSentValue( int intDeviceId ); TELLSTICK_API char *WINAPI tdLastSentValue( int intDeviceId );
TELLSTICK_API int WINAPI tdGetNumberOfDevices(); TELLSTICK_API int WINAPI tdGetNumberOfDevices();