Added new device-type: DeviceGroup

This commit is contained in:
Micke Prag 2009-01-09 16:05:57 +00:00
parent e4e6da2918
commit e5fa83566f
7 changed files with 207 additions and 0 deletions

View file

@ -5,6 +5,7 @@ INCLUDE( ${QT_USE_FILE} )
SET( telldus-core_SRCS
Controller.cpp
Device.cpp
DeviceGroup.cpp
DeviceIkea.cpp
DeviceNexa.cpp
DeviceSartano.cpp

View file

@ -0,0 +1,135 @@
#include <stdlib.h>
#include "DeviceGroup.h"
#include "Manager.h"
using namespace TelldusCore;
const int ALL_METHODS =
TELLSTICK_TURNON |
TELLSTICK_TURNOFF |
TELLSTICK_BELL |
TELLSTICK_DIM;
/*
* Constructor
*/
DeviceGroup::DeviceGroup(int model, const std::string &strDevices)
:Device(model)
{
if (strDevices.length() > 0) {
char *tempDevices = new char[strDevices.size()+1];
#ifdef _WINDOWS
strcpy_s(tempDevices, strDevices.size()+1, strDevices.c_str());
#else
strcpy(tempDevices, strDevices.c_str());
#endif
Manager *manager = Manager::getInstance();
char *strToken = strtok(tempDevices, ",");
do {
int intDevice = atoi(strToken);
Device *device = manager->getDevice(intDevice);
if (device != NULL) {
deviceList.push_back( device );
}
} while ( (strToken = strtok(NULL, ",")) != NULL );
free(tempDevices);
}
}
/*
* Destructor
*/
DeviceGroup::~DeviceGroup(void) {
}
/*
* Turn on this device
*/
int DeviceGroup::turnOn(void) {
int retVal = TELLSTICK_ERROR_UNKNOWN;
for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) {
int methods = (*it)->methods(ALL_METHODS);
if (ALL_METHODS & TELLSTICK_TURNON) {
int success = (*it)->turnOn();
if (retVal != TELLSTICK_SUCCESS) {
retVal = success;
}
}
}
return retVal;
}
/*
* Turn off this device
*/
int DeviceGroup::turnOff(void) {
int retVal = TELLSTICK_ERROR_UNKNOWN;
for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) {
int methods = (*it)->methods(ALL_METHODS);
if (ALL_METHODS & TELLSTICK_TURNOFF) {
int success = (*it)->turnOff();
if (retVal != TELLSTICK_SUCCESS) {
retVal = success;
}
}
}
return retVal;
}
/*
* Send a bell
*/
int DeviceGroup::bell(void){
int retVal = TELLSTICK_ERROR_UNKNOWN;
for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) {
int methods = (*it)->methods(ALL_METHODS);
if (ALL_METHODS & TELLSTICK_BELL) {
int success = (*it)->bell();
if (retVal != TELLSTICK_SUCCESS) {
retVal = success;
}
}
}
return retVal;
}
/*
* Turn off this device
*/
int DeviceGroup::dim(unsigned char level){
int retVal = TELLSTICK_ERROR_UNKNOWN;
for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) {
int methods = (*it)->methods(ALL_METHODS);
if (ALL_METHODS & TELLSTICK_DIM) {
int success = (*it)->dim(level);
if (retVal != TELLSTICK_SUCCESS) {
retVal = success;
}
}
}
return retVal;
}
/*
* Has the device got the method?
*/
int DeviceGroup::methods(int supportedMethods){
int retVal = 0;
for (DeviceList::const_iterator it = deviceList.begin(); it != deviceList.end(); ++it) {
retVal = retVal | (*it)->methods(supportedMethods);
}
return retVal;
}
std::string DeviceGroup::getProtocol() const {
return "group";
}

View file

@ -0,0 +1,30 @@
#pragma once
#include "Device.h"
#include <string.h>
#include <list>
class Controller;
namespace TelldusCore {
typedef std::list<Device *> DeviceList;
class DeviceGroup : public Device
{
public:
DeviceGroup(int model, const std::string &strDevices);
virtual int turnOn(void);
virtual int turnOff(void);
virtual int bell(void);
virtual int dim(unsigned char level);
virtual int methods(int methodsSupported);
virtual std::string getProtocol() const;
public:
~DeviceGroup(void);
private:
DeviceList deviceList;
};
}

View file

@ -12,6 +12,7 @@
#include "Manager.h"
#include "Device.h"
#include "DeviceGroup.h"
#include "DeviceNexa.h"
#include "DeviceWaveman.h"
#include "DeviceSartano.h"
@ -65,6 +66,10 @@ Device *Manager::getDevice(int intDeviceId){
std::string strCode = settings.getDeviceParameter(intDeviceId, "nexa_unit");
dev = new DeviceNexa(intModel, strHouse, strCode);
} else if (strcasecmp(protocol.c_str(), "group") == 0) {
std::string strDevices = settings.getDeviceParameter(intDeviceId, "devices");
dev = new DeviceGroup(intModel, strDevices);
} else if (strcasecmp(protocol.c_str(), "Waveman") == 0) {
std::string strHouse = settings.getDeviceParameter(intDeviceId, "nexa_house");
std::string strCode = settings.getDeviceParameter(intDeviceId, "nexa_unit");

View file

@ -229,6 +229,9 @@ bool readConfig(cfg_t **cfg) {
};
cfg_opt_t device_parameter_opts[] = {
//Groups
CFG_STR("devices", 0, CFGF_NONE),
//Nexa
CFG_STR("nexa_house", 0, CFGF_NONE),
CFG_STR("nexa_unit", 0, CFGF_NONE),

View file

@ -11,6 +11,7 @@
#include "telldus-core.h"
#include "Manager.h"
#include "Device.h"
#include "DeviceGroup.h"
#include <vector>
#include <iostream>
#include <fstream>
@ -39,6 +40,12 @@ inline char *wrapStdString( const std::string &string);
* @def TELLSTICK_DIM
* Device-flag for devices supporting the tdDim() call.
*
* @def TELLSTICK_TYPE_DEVICE
* Device type of a single device.
*
* @def TELLSTICK_TYPE_GROUP
* Device type of a device which contains other devices.
*
* @def TELLSTICK_SUCCESS
* Error code. Returned when the command succeeded.
*
@ -253,6 +260,27 @@ int WINAPI tdGetDeviceId(int intDeviceIndex){
return intReturn;
}
/**
* Returns which type the device is. The device could be either
* TELLSTICK_TYPE_DEVICE or TELLSTICK_TYPE_GROUP
*/
int WINAPI tdGetDeviceType(int intDeviceId) {
try{
Manager *manager = Manager::getInstance();
Device* dev = manager->getDevice(intDeviceId);
if (dev != NULL) {
DeviceGroup *deviceGroup = dynamic_cast<DeviceGroup*>(dev);
if (deviceGroup) {
return TELLSTICK_TYPE_GROUP;
}
}
}
catch(exception e){
handleException(e);
}
return TELLSTICK_TYPE_DEVICE;
}
/**
* Query a device for it's name.
* @param intDeviceId The unique id of the device to query

View file

@ -33,6 +33,7 @@ extern "C" {
TELLSTICK_API int WINAPI tdGetNumberOfDevices();
TELLSTICK_API int WINAPI tdGetDeviceId(int intDeviceIndex);
TELLSTICK_API int WINAPI tdGetDeviceType(int intDeviceIndex);
TELLSTICK_API char * WINAPI tdGetErrorString(int intErrorNo);
@ -67,6 +68,10 @@ extern "C" {
#define TELLSTICK_ERROR_METHOD_NOT_SUPPORTED -4
#define TELLSTICK_ERROR_UNKNOWN -99
//Device typedef
#define TELLSTICK_TYPE_DEVICE 1
#define TELLSTICK_TYPE_GROUP 2
//Protocol Nexa
#define TELLSTICK_DEVICE_YCR3500 1
#define TELLSTICK_DEVICE_YCR300D 2