{ should almost always be at the end of the previous line, according to Google style guidelines "whitespace/braces"

This commit is contained in:
Micke Prag 2012-02-28 22:19:02 +01:00
parent 78444e20b9
commit e1608f94a8
58 changed files with 323 additions and 403 deletions

View file

@ -12,8 +12,7 @@
using namespace TelldusCore; using namespace TelldusCore;
TDEventDispatcher::TDEventDispatcher(EventDataRef cbd, CallbackStruct *cb, EventRef cbDone) TDEventDispatcher::TDEventDispatcher(EventDataRef cbd, CallbackStruct *cb, EventRef cbDone)
:Thread(), doneRunning(false), callbackData(cbd), callback(cb), callbackExecuted(cbDone) :Thread(), doneRunning(false), callbackData(cbd), callback(cb), callbackExecuted(cbDone) {
{
this->startAndLock(&callback->mutex); this->startAndLock(&callback->mutex);
} }

View file

@ -29,8 +29,7 @@ public:
}; };
CallbackMainDispatcher::CallbackMainDispatcher() CallbackMainDispatcher::CallbackMainDispatcher()
:Thread() :Thread() {
{
d = new PrivateData; d = new PrivateData;
d->stopEvent = d->eventHandler.addEvent(); d->stopEvent = d->eventHandler.addEvent();
d->generalCallbackEvent = d->eventHandler.addEvent(); d->generalCallbackEvent = d->eventHandler.addEvent();
@ -39,7 +38,7 @@ CallbackMainDispatcher::CallbackMainDispatcher()
d->lastCallbackId = 0; d->lastCallbackId = 0;
} }
CallbackMainDispatcher::~CallbackMainDispatcher(void){ CallbackMainDispatcher::~CallbackMainDispatcher(void) {
d->stopEvent->signal(); d->stopEvent->signal();
wait(); wait();
{ {
@ -48,7 +47,7 @@ CallbackMainDispatcher::~CallbackMainDispatcher(void){
delete d; delete d;
} }
EventRef CallbackMainDispatcher::retrieveCallbackEvent(){ EventRef CallbackMainDispatcher::retrieveCallbackEvent() {
return d->generalCallbackEvent; return d->generalCallbackEvent;
} }
@ -88,14 +87,13 @@ int CallbackMainDispatcher::unregisterCallback(int callbackId) {
return TELLSTICK_ERROR_NOT_FOUND; return TELLSTICK_ERROR_NOT_FOUND;
} }
void CallbackMainDispatcher::run(){ void CallbackMainDispatcher::run() {
while(!d->stopEvent->isSignaled()) {
while(!d->stopEvent->isSignaled()){
if (!d->eventHandler.waitForAny()) { if (!d->eventHandler.waitForAny()) {
continue; continue;
} }
if(d->generalCallbackEvent->isSignaled()){ if(d->generalCallbackEvent->isSignaled()) {
EventDataRef eventData = d->generalCallbackEvent->takeSignal(); EventDataRef eventData = d->generalCallbackEvent->takeSignal();
CallbackData *cbd = dynamic_cast<CallbackData *>(eventData.get()); CallbackData *cbd = dynamic_cast<CallbackData *>(eventData.get());

View file

@ -29,8 +29,7 @@ public:
Client *Client::instance = 0; Client *Client::instance = 0;
Client::Client() Client::Client()
: Thread() : Thread() {
{
d = new PrivateData; d = new PrivateData;
d->running = true; d->running = true;
d->sensorCached = false; d->sensorCached = false;
@ -83,16 +82,15 @@ int Client::registerEvent( CallbackStruct::CallbackType type, void *eventFunctio
return d->callbackMainDispatcher.registerCallback(type, eventFunction, context ); return d->callbackMainDispatcher.registerCallback(type, eventFunction, context );
} }
void Client::run(){ void Client::run() {
//listen here // listen here
d->eventSocket.connect(L"TelldusEvents"); d->eventSocket.connect(L"TelldusEvents");
while(d->running){ while(d->running) {
if(!d->eventSocket.isConnected()) {
if(!d->eventSocket.isConnected()){ d->eventSocket.connect(L"TelldusEvents"); // try to reconnect to service
d->eventSocket.connect(L"TelldusEvents"); //try to reconnect to service if(!d->eventSocket.isConnected()) {
if(!d->eventSocket.isConnected()){ // reconnect didn't succeed, wait a while and try again
//reconnect didn't succeed, wait a while and try again
msleep(2000); msleep(2000);
continue; continue;
} }
@ -100,30 +98,30 @@ void Client::run(){
std::wstring clientMessage = d->eventSocket.read(1000); //testing 5 second timeout std::wstring clientMessage = d->eventSocket.read(1000); //testing 5 second timeout
while(clientMessage != L""){ while(clientMessage != L"") {
//a message arrived // a message arrived
std::wstring type = Message::takeString(&clientMessage); std::wstring type = Message::takeString(&clientMessage);
if(type == L"TDDeviceChangeEvent"){ if(type == L"TDDeviceChangeEvent") {
DeviceChangeEventCallbackData *data = new DeviceChangeEventCallbackData(); DeviceChangeEventCallbackData *data = new DeviceChangeEventCallbackData();
data->deviceId = Message::takeInt(&clientMessage); data->deviceId = Message::takeInt(&clientMessage);
data->changeEvent = Message::takeInt(&clientMessage); data->changeEvent = Message::takeInt(&clientMessage);
data->changeType = Message::takeInt(&clientMessage); data->changeType = Message::takeInt(&clientMessage);
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data); d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data);
} else if(type == L"TDDeviceEvent"){ } else if(type == L"TDDeviceEvent") {
DeviceEventCallbackData *data = new DeviceEventCallbackData(); DeviceEventCallbackData *data = new DeviceEventCallbackData();
data->deviceId = Message::takeInt(&clientMessage); data->deviceId = Message::takeInt(&clientMessage);
data->deviceState = Message::takeInt(&clientMessage); data->deviceState = Message::takeInt(&clientMessage);
data->deviceStateValue = TelldusCore::wideToString(Message::takeString(&clientMessage)); data->deviceStateValue = TelldusCore::wideToString(Message::takeString(&clientMessage));
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data); d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data);
} else if(type == L"TDRawDeviceEvent"){ } else if(type == L"TDRawDeviceEvent") {
RawDeviceEventCallbackData *data = new RawDeviceEventCallbackData(); RawDeviceEventCallbackData *data = new RawDeviceEventCallbackData();
data->data = TelldusCore::wideToString(Message::takeString(&clientMessage)); data->data = TelldusCore::wideToString(Message::takeString(&clientMessage));
data->controllerId = Message::takeInt(&clientMessage); data->controllerId = Message::takeInt(&clientMessage);
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data); d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data);
} else if(type == L"TDSensorEvent"){ } else if(type == L"TDSensorEvent") {
SensorEventCallbackData *data = new SensorEventCallbackData(); SensorEventCallbackData *data = new SensorEventCallbackData();
data->protocol = TelldusCore::wideToString(Message::takeString(&clientMessage)); data->protocol = TelldusCore::wideToString(Message::takeString(&clientMessage));
data->model = TelldusCore::wideToString(Message::takeString(&clientMessage)); data->model = TelldusCore::wideToString(Message::takeString(&clientMessage));
@ -152,9 +150,9 @@ std::wstring Client::sendToService(const Message &msg) {
int tries = 0; int tries = 0;
std::wstring readData; std::wstring readData;
while(tries < 20){ while(tries < 20) {
tries++; tries++;
if(tries == 20){ if(tries == 20) {
TelldusCore::Message msg; TelldusCore::Message msg;
msg.addArgument(TELLSTICK_ERROR_CONNECTING_SERVICE); msg.addArgument(TELLSTICK_ERROR_CONNECTING_SERVICE);
return msg; return msg;
@ -170,8 +168,8 @@ std::wstring Client::sendToService(const Message &msg) {
msleep(500); msleep(500);
continue; //retry continue; //retry
} }
readData = s.read(8000); //TODO changed to 10000 from 5000, how much does this do...? readData = s.read(8000); // TODO changed to 10000 from 5000, how much does this do...?
if(readData == L""){ if(readData == L"") {
msleep(500); msleep(500);
continue; //TODO can we be really sure it SHOULD be anything? continue; //TODO can we be really sure it SHOULD be anything?
//TODO perhaps break here instead? //TODO perhaps break here instead?
@ -187,7 +185,7 @@ std::wstring Client::sendToService(const Message &msg) {
return readData; return readData;
} }
void Client::stopThread(){ void Client::stopThread() {
d->running = false; d->running = false;
d->eventSocket.stopReadWait(); d->eventSocket.stopReadWait();
} }

View file

@ -13,8 +13,7 @@
#include "common/Thread.h" #include "common/Thread.h"
namespace TelldusCore { namespace TelldusCore {
class Client : public Thread class Client : public Thread {
{
public: public:
~Client(void); ~Client(void);

View file

@ -480,7 +480,7 @@ void WINAPI tdReleaseString(char *thestring) {
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
int WINAPI tdTurnOn(int intDeviceId){ int WINAPI tdTurnOn(int intDeviceId) {
Message msg(L"tdTurnOn"); Message msg(L"tdTurnOn");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
return Client::getIntegerFromService(msg); return Client::getIntegerFromService(msg);
@ -498,7 +498,7 @@ int WINAPI tdTurnOn(int intDeviceId){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
int WINAPI tdTurnOff(int intDeviceId){ int WINAPI tdTurnOff(int intDeviceId) {
Message msg(L"tdTurnOff"); Message msg(L"tdTurnOff");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
return Client::getIntegerFromService(msg); return Client::getIntegerFromService(msg);
@ -516,7 +516,7 @@ int WINAPI tdTurnOff(int intDeviceId){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
int WINAPI tdBell(int intDeviceId){ int WINAPI tdBell(int intDeviceId) {
Message msg(L"tdBell"); Message msg(L"tdBell");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
return Client::getIntegerFromService(msg); return Client::getIntegerFromService(msg);
@ -536,7 +536,7 @@ int WINAPI tdBell(int intDeviceId){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
int WINAPI tdDim(int intDeviceId, unsigned char level){ int WINAPI tdDim(int intDeviceId, unsigned char level) {
Message msg(L"tdDim"); Message msg(L"tdDim");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
msg.addArgument(level); msg.addArgument(level);
@ -555,7 +555,7 @@ int WINAPI tdDim(int intDeviceId, unsigned char level){
* *
* @since Version 2.1.0 * @since Version 2.1.0
**/ **/
int WINAPI tdExecute(int intDeviceId){ int WINAPI tdExecute(int intDeviceId) {
Message msg(L"tdExecute"); Message msg(L"tdExecute");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
return Client::getIntegerFromService(msg); return Client::getIntegerFromService(msg);
@ -573,7 +573,7 @@ int WINAPI tdExecute(int intDeviceId){
* *
* @since Version 2.1.0 * @since Version 2.1.0
**/ **/
int WINAPI tdUp(int intDeviceId){ int WINAPI tdUp(int intDeviceId) {
Message msg(L"tdUp"); Message msg(L"tdUp");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
return Client::getIntegerFromService(msg); return Client::getIntegerFromService(msg);
@ -591,7 +591,7 @@ int WINAPI tdUp(int intDeviceId){
* *
* @since Version 2.1.0 * @since Version 2.1.0
**/ **/
int WINAPI tdDown(int intDeviceId){ int WINAPI tdDown(int intDeviceId) {
Message msg(L"tdDown"); Message msg(L"tdDown");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
return Client::getIntegerFromService(msg); return Client::getIntegerFromService(msg);
@ -609,7 +609,7 @@ int WINAPI tdDown(int intDeviceId){
* *
* @since Version 2.1.0 * @since Version 2.1.0
*/ */
int WINAPI tdStop(int intDeviceId){ int WINAPI tdStop(int intDeviceId) {
Message msg(L"tdStop"); Message msg(L"tdStop");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
return Client::getIntegerFromService(msg); return Client::getIntegerFromService(msg);
@ -683,7 +683,7 @@ char * WINAPI tdLastSentValue( int intDeviceId ) {
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
int WINAPI tdGetNumberOfDevices(void){ int WINAPI tdGetNumberOfDevices(void) {
return Client::getIntegerFromService(Message(L"tdGetNumberOfDevices")); return Client::getIntegerFromService(Message(L"tdGetNumberOfDevices"));
} }
@ -707,7 +707,7 @@ int WINAPI tdGetNumberOfDevices(void){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
int WINAPI tdGetDeviceId(int intDeviceIndex){ int WINAPI tdGetDeviceId(int intDeviceIndex) {
Message msg(L"tdGetDeviceId"); Message msg(L"tdGetDeviceId");
msg.addArgument(intDeviceIndex); msg.addArgument(intDeviceIndex);
return Client::getIntegerFromService(msg); return Client::getIntegerFromService(msg);
@ -744,7 +744,7 @@ int WINAPI tdGetDeviceType(int intDeviceId) {
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
char * WINAPI tdGetName(int intDeviceId){ char * WINAPI tdGetName(int intDeviceId) {
Message msg(L"tdGetName"); Message msg(L"tdGetName");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
std::wstring strReturn = Client::getWStringFromService(msg); std::wstring strReturn = Client::getWStringFromService(msg);
@ -765,7 +765,7 @@ char * WINAPI tdGetName(int intDeviceId){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
bool WINAPI tdSetName(int intDeviceId, const char* strNewName){ bool WINAPI tdSetName(int intDeviceId, const char* strNewName) {
Message msg(L"tdSetName"); Message msg(L"tdSetName");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
msg.addArgument(strNewName); msg.addArgument(strNewName);
@ -784,7 +784,7 @@ bool WINAPI tdSetName(int intDeviceId, const char* strNewName){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
char* WINAPI tdGetProtocol(int intDeviceId){ char* WINAPI tdGetProtocol(int intDeviceId) {
Message msg(L"tdGetProtocol"); Message msg(L"tdGetProtocol");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
std::wstring strReturn = Client::getWStringFromService(msg); std::wstring strReturn = Client::getWStringFromService(msg);
@ -808,7 +808,7 @@ char* WINAPI tdGetProtocol(int intDeviceId){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
bool WINAPI tdSetProtocol(int intDeviceId, const char* strProtocol){ bool WINAPI tdSetProtocol(int intDeviceId, const char* strProtocol) {
Message msg(L"tdSetProtocol"); Message msg(L"tdSetProtocol");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
msg.addArgument(strProtocol); msg.addArgument(strProtocol);
@ -827,7 +827,7 @@ bool WINAPI tdSetProtocol(int intDeviceId, const char* strProtocol){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
char* WINAPI tdGetModel(int intDeviceId){ char* WINAPI tdGetModel(int intDeviceId) {
Message msg(L"tdGetModel"); Message msg(L"tdGetModel");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
std::wstring strReturn = Client::getWStringFromService(msg); std::wstring strReturn = Client::getWStringFromService(msg);
@ -848,7 +848,7 @@ char* WINAPI tdGetModel(int intDeviceId){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
bool WINAPI tdSetModel(int intDeviceId, const char *strModel){ bool WINAPI tdSetModel(int intDeviceId, const char *strModel) {
Message msg(L"tdSetModel"); Message msg(L"tdSetModel");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
msg.addArgument(strModel); msg.addArgument(strModel);
@ -871,7 +871,7 @@ bool WINAPI tdSetModel(int intDeviceId, const char *strModel){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
bool WINAPI tdSetDeviceParameter(int intDeviceId, const char *strName, const char *strValue){ bool WINAPI tdSetDeviceParameter(int intDeviceId, const char *strName, const char *strValue) {
Message msg(L"tdSetDeviceParameter"); Message msg(L"tdSetDeviceParameter");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
msg.addArgument(strName); msg.addArgument(strName);
@ -896,7 +896,7 @@ bool WINAPI tdSetDeviceParameter(int intDeviceId, const char *strName, const cha
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
char * WINAPI tdGetDeviceParameter(int intDeviceId, const char *strName, const char *defaultValue){ char * WINAPI tdGetDeviceParameter(int intDeviceId, const char *strName, const char *defaultValue) {
Message msg(L"tdGetDeviceParameter"); Message msg(L"tdGetDeviceParameter");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
msg.addArgument(strName); msg.addArgument(strName);
@ -916,7 +916,7 @@ char * WINAPI tdGetDeviceParameter(int intDeviceId, const char *strName, const c
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
int WINAPI tdAddDevice(){ int WINAPI tdAddDevice() {
Message msg(L"tdAddDevice"); Message msg(L"tdAddDevice");
return Client::getIntegerFromService(msg); return Client::getIntegerFromService(msg);
} }
@ -932,7 +932,7 @@ int WINAPI tdAddDevice(){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
bool WINAPI tdRemoveDevice(int intDeviceId){ bool WINAPI tdRemoveDevice(int intDeviceId) {
Message msg(L"tdRemoveDevice"); Message msg(L"tdRemoveDevice");
msg.addArgument(intDeviceId); msg.addArgument(intDeviceId);
return Client::getBoolFromService(msg); return Client::getBoolFromService(msg);
@ -973,7 +973,7 @@ bool WINAPI tdRemoveDevice(int intDeviceId){
* *
* @since Version 2.0.0 * @since Version 2.0.0
**/ **/
int WINAPI tdMethods(int id, int methodsSupported){ int WINAPI tdMethods(int id, int methodsSupported) {
Message msg(L"tdMethods"); Message msg(L"tdMethods");
msg.addArgument(id); msg.addArgument(id);
msg.addArgument(methodsSupported); msg.addArgument(methodsSupported);

View file

@ -48,12 +48,12 @@ EventRef EventHandler::addEvent() {
return event; return event;
} }
bool EventHandler::listIsSignalled(){ bool EventHandler::listIsSignalled() {
TelldusCore::MutexLocker locker(&d->listMutex); TelldusCore::MutexLocker locker(&d->listMutex);
std::list<EventRef>::const_iterator it = d->eventList.begin(); std::list<EventRef>::const_iterator it = d->eventList.begin();
for(; it != d->eventList.end(); ++it) { for(; it != d->eventList.end(); ++it) {
if((*it)->isSignaled()){ if((*it)->isSignaled()) {
return true; return true;
} }
} }

View file

@ -15,7 +15,7 @@ public:
}; };
Event::Event(EventHandler *handler) Event::Event(EventHandler *handler)
:EventBase(handler){ :EventBase(handler) {
d = new PrivateData; d = new PrivateData;
} }

View file

@ -15,13 +15,11 @@ using namespace TelldusCore;
Message::Message() Message::Message()
: std::wstring() : std::wstring() {
{
} }
Message::Message(const std::wstring &functionName) Message::Message(const std::wstring &functionName)
:std::wstring() :std::wstring() {
{
this->addArgument(functionName); this->addArgument(functionName);
} }

View file

@ -77,8 +77,7 @@ void LoggedMutex::unlock() {
} }
MutexLocker::MutexLocker(Mutex *m) MutexLocker::MutexLocker(Mutex *m)
:mutex(m) :mutex(m) {
{
mutex->lock(); mutex->lock();
} }

View file

@ -17,8 +17,7 @@
#endif #endif
namespace TelldusCore { namespace TelldusCore {
class Socket class Socket {
{
public: public:
Socket(); Socket();
explicit Socket(SOCKET_T hPipe); explicit Socket(SOCKET_T hPipe);

View file

@ -39,8 +39,7 @@ Socket::Socket() {
FD_ZERO(&d->infds); FD_ZERO(&d->infds);
} }
Socket::Socket(SOCKET_T socket) Socket::Socket(SOCKET_T socket) {
{
d = new PrivateData; d = new PrivateData;
d->socket = socket; d->socket = socket;
FD_ZERO(&d->infds); FD_ZERO(&d->infds);
@ -48,7 +47,7 @@ Socket::Socket(SOCKET_T socket)
} }
Socket::~Socket(void) { Socket::~Socket(void) {
if(d->socket){ if(d->socket) {
close(d->socket); close(d->socket);
} }
delete d; delete d;
@ -74,7 +73,7 @@ void Socket::connect(const std::wstring &server) {
d->connected = true; d->connected = true;
} }
bool Socket::isConnected(){ bool Socket::isConnected() {
TelldusCore::MutexLocker locker(&d->mutex); TelldusCore::MutexLocker locker(&d->mutex);
return d->connected; return d->connected;
} }
@ -102,10 +101,10 @@ std::wstring Socket::read(int timeout) {
} }
int received = BUFSIZE; int received = BUFSIZE;
while(received >= (BUFSIZE - 1)){ while(received >= (BUFSIZE - 1)) {
memset(inbuf, '\0', sizeof(inbuf)); memset(inbuf, '\0', sizeof(inbuf));
received = recv(d->socket, inbuf, BUFSIZE - 1, 0); received = recv(d->socket, inbuf, BUFSIZE - 1, 0);
if(received > 0){ if(received > 0) {
msg.append(std::string(inbuf)); msg.append(std::string(inbuf));
} }
} }
@ -119,7 +118,7 @@ std::wstring Socket::read(int timeout) {
return TelldusCore::charToWstring(msg.c_str()); return TelldusCore::charToWstring(msg.c_str());
} }
void Socket::stopReadWait(){ void Socket::stopReadWait() {
TelldusCore::MutexLocker locker(&d->mutex); TelldusCore::MutexLocker locker(&d->mutex);
d->connected = false; d->connected = false;
//TODO somehow signal the socket here? //TODO somehow signal the socket here?

View file

@ -73,7 +73,7 @@ std::wstring TelldusCore::charToWstring(const char *value) {
#endif #endif
} }
int TelldusCore::charToInteger(const char *input){ int TelldusCore::charToInteger(const char *input) {
std::stringstream inputstream; std::stringstream inputstream;
inputstream << input; inputstream << input;
int retval; int retval;
@ -150,7 +150,7 @@ std::wstring TelldusCore::intToWStringSafe(int value){
} }
*/ */
uint64_t TelldusCore::hexTo64l(const std::string data){ uint64_t TelldusCore::hexTo64l(const std::string data) {
#ifdef _WINDOWS #ifdef _WINDOWS
return _strtoui64(data.c_str(), NULL, 16); return _strtoui64(data.c_str(), NULL, 16);
#elif defined(_MACOSX) #elif defined(_MACOSX)
@ -160,7 +160,7 @@ uint64_t TelldusCore::hexTo64l(const std::string data){
#endif #endif
} }
int TelldusCore::wideToInteger(const std::wstring &input){ int TelldusCore::wideToInteger(const std::wstring &input) {
std::wstringstream inputstream; std::wstringstream inputstream;
inputstream << input; inputstream << input;
int retval; int retval;

View file

@ -41,7 +41,7 @@ inline void dlog(const char *fmt, ...) {
fflush(stdout); fflush(stdout);
} }
inline void debuglog(const int intMessage, const std::string strMessage){ inline void debuglog(const int intMessage, const std::string strMessage) {
#ifdef _WINDOWS #ifdef _WINDOWS
static bool firstRun = true; static bool firstRun = true;
std::ofstream file; std::ofstream file;

View file

@ -20,13 +20,11 @@ public:
ControllerManager *controllerManager; ControllerManager *controllerManager;
}; };
ClientCommunicationHandler::ClientCommunicationHandler(){ ClientCommunicationHandler::ClientCommunicationHandler() {
} }
ClientCommunicationHandler::ClientCommunicationHandler(TelldusCore::Socket *clientSocket, TelldusCore::EventRef event, DeviceManager *deviceManager, TelldusCore::EventRef deviceUpdateEvent, ControllerManager *controllerManager) ClientCommunicationHandler::ClientCommunicationHandler(TelldusCore::Socket *clientSocket, TelldusCore::EventRef event, DeviceManager *deviceManager, TelldusCore::EventRef deviceUpdateEvent, ControllerManager *controllerManager)
:Thread() :Thread() {
{
d = new PrivateData; d = new PrivateData;
d->clientSocket = clientSocket; d->clientSocket = clientSocket;
d->event = event; d->event = event;
@ -36,15 +34,14 @@ ClientCommunicationHandler::ClientCommunicationHandler(TelldusCore::Socket *clie
d->controllerManager = controllerManager; d->controllerManager = controllerManager;
} }
ClientCommunicationHandler::~ClientCommunicationHandler(void) ClientCommunicationHandler::~ClientCommunicationHandler(void) {
{
wait(); wait();
delete(d->clientSocket); delete(d->clientSocket);
delete d; delete d;
} }
void ClientCommunicationHandler::run(){ void ClientCommunicationHandler::run() {
//run thread // run thread
std::wstring clientMessage = d->clientSocket->read(2000); std::wstring clientMessage = d->clientSocket->read(2000);
@ -55,10 +52,10 @@ void ClientCommunicationHandler::run(){
TelldusCore::Message msg; TelldusCore::Message msg;
if(strReturn == L""){ if(strReturn == L"") {
msg.addArgument(intReturn); msg.addArgument(intReturn);
} }
else{ else {
msg.addArgument(strReturn); msg.addArgument(strReturn);
} }
msg.append(L"\n"); msg.append(L"\n");
@ -69,13 +66,12 @@ void ClientCommunicationHandler::run(){
d->event->signal(); d->event->signal();
} }
bool ClientCommunicationHandler::isDone(){ bool ClientCommunicationHandler::isDone() {
return d->done; return d->done;
} }
void ClientCommunicationHandler::parseMessage(const std::wstring &clientMessage, int *intReturn, std::wstring *wstringReturn){ void ClientCommunicationHandler::parseMessage(const std::wstring &clientMessage, int *intReturn, std::wstring *wstringReturn) {
(*intReturn) = 0; (*intReturn) = 0;
(*wstringReturn) = L""; (*wstringReturn) = L"";
std::wstring msg(clientMessage); //Copy std::wstring msg(clientMessage); //Copy
@ -127,8 +123,7 @@ void ClientCommunicationHandler::parseMessage(const std::wstring &clientMessage,
int deviceId = TelldusCore::Message::takeInt(&msg); int deviceId = TelldusCore::Message::takeInt(&msg);
(*wstringReturn) = d->deviceManager->getDeviceStateValue(deviceId); (*wstringReturn) = d->deviceManager->getDeviceStateValue(deviceId);
} else if(function == L"tdGetNumberOfDevices"){ } else if(function == L"tdGetNumberOfDevices") {
(*intReturn) = d->deviceManager->getNumberOfDevices(); (*intReturn) = d->deviceManager->getNumberOfDevices();
} else if (function == L"tdGetDeviceId") { } else if (function == L"tdGetDeviceId") {
@ -159,7 +154,7 @@ void ClientCommunicationHandler::parseMessage(const std::wstring &clientMessage,
int oldMethods = d->deviceManager->getDeviceMethods(deviceId); int oldMethods = d->deviceManager->getDeviceMethods(deviceId);
(*intReturn) = d->deviceManager->setDeviceProtocol(deviceId, protocol); (*intReturn) = d->deviceManager->setDeviceProtocol(deviceId, protocol);
sendDeviceSignal(deviceId, TELLSTICK_DEVICE_CHANGED, TELLSTICK_CHANGE_PROTOCOL); sendDeviceSignal(deviceId, TELLSTICK_DEVICE_CHANGED, TELLSTICK_CHANGE_PROTOCOL);
if(oldMethods != d->deviceManager->getDeviceMethods(deviceId)){ if(oldMethods != d->deviceManager->getDeviceMethods(deviceId)) {
sendDeviceSignal(deviceId, TELLSTICK_DEVICE_CHANGED, TELLSTICK_CHANGE_METHOD); sendDeviceSignal(deviceId, TELLSTICK_DEVICE_CHANGED, TELLSTICK_CHANGE_METHOD);
} }
@ -173,7 +168,7 @@ void ClientCommunicationHandler::parseMessage(const std::wstring &clientMessage,
int oldMethods = d->deviceManager->getDeviceMethods(deviceId); int oldMethods = d->deviceManager->getDeviceMethods(deviceId);
(*intReturn) = d->deviceManager->setDeviceModel(deviceId, model); (*intReturn) = d->deviceManager->setDeviceModel(deviceId, model);
sendDeviceSignal(deviceId, TELLSTICK_DEVICE_CHANGED, TELLSTICK_CHANGE_MODEL); sendDeviceSignal(deviceId, TELLSTICK_DEVICE_CHANGED, TELLSTICK_CHANGE_MODEL);
if(oldMethods != d->deviceManager->getDeviceMethods(deviceId)){ if(oldMethods != d->deviceManager->getDeviceMethods(deviceId)) {
sendDeviceSignal(deviceId, TELLSTICK_DEVICE_CHANGED, TELLSTICK_CHANGE_METHOD); sendDeviceSignal(deviceId, TELLSTICK_DEVICE_CHANGED, TELLSTICK_CHANGE_METHOD);
} }
@ -189,20 +184,20 @@ void ClientCommunicationHandler::parseMessage(const std::wstring &clientMessage,
std::wstring value = TelldusCore::Message::takeString(&msg); std::wstring value = TelldusCore::Message::takeString(&msg);
int oldMethods = d->deviceManager->getDeviceMethods(deviceId); int oldMethods = d->deviceManager->getDeviceMethods(deviceId);
(*intReturn) = d->deviceManager->setDeviceParameter(deviceId, name, value); (*intReturn) = d->deviceManager->setDeviceParameter(deviceId, name, value);
if(oldMethods != d->deviceManager->getDeviceMethods(deviceId)){ if(oldMethods != d->deviceManager->getDeviceMethods(deviceId)) {
sendDeviceSignal(deviceId, TELLSTICK_DEVICE_CHANGED, TELLSTICK_CHANGE_METHOD); sendDeviceSignal(deviceId, TELLSTICK_DEVICE_CHANGED, TELLSTICK_CHANGE_METHOD);
} }
} else if (function == L"tdAddDevice") { } else if (function == L"tdAddDevice") {
(*intReturn) = d->deviceManager->addDevice(); (*intReturn) = d->deviceManager->addDevice();
if((*intReturn) >= 0){ if((*intReturn) >= 0) {
sendDeviceSignal((*intReturn), TELLSTICK_DEVICE_ADDED, 0); sendDeviceSignal((*intReturn), TELLSTICK_DEVICE_ADDED, 0);
} }
} else if (function == L"tdRemoveDevice") { } else if (function == L"tdRemoveDevice") {
int deviceId = TelldusCore::Message::takeInt(&msg); int deviceId = TelldusCore::Message::takeInt(&msg);
(*intReturn) = d->deviceManager->removeDevice(deviceId); (*intReturn) = d->deviceManager->removeDevice(deviceId);
if((*intReturn) == TELLSTICK_SUCCESS){ if((*intReturn) == TELLSTICK_SUCCESS) {
sendDeviceSignal(deviceId, TELLSTICK_DEVICE_REMOVED, 0); sendDeviceSignal(deviceId, TELLSTICK_DEVICE_REMOVED, 0);
} }
@ -261,8 +256,7 @@ void ClientCommunicationHandler::parseMessage(const std::wstring &clientMessage,
} }
} }
void ClientCommunicationHandler::sendDeviceSignal(int deviceId, int eventDeviceChanges, int eventChangeType){ void ClientCommunicationHandler::sendDeviceSignal(int deviceId, int eventDeviceChanges, int eventChangeType) {
EventUpdateData *eventData = new EventUpdateData(); EventUpdateData *eventData = new EventUpdateData();
eventData->messageType = L"TDDeviceChangeEvent"; eventData->messageType = L"TDDeviceChangeEvent";
eventData->deviceId = deviceId; eventData->deviceId = deviceId;

View file

@ -14,8 +14,7 @@
#include "service/DeviceManager.h" #include "service/DeviceManager.h"
#include "service/ControllerManager.h" #include "service/ControllerManager.h"
class ClientCommunicationHandler : public TelldusCore::Thread class ClientCommunicationHandler : public TelldusCore::Thread {
{
public: public:
ClientCommunicationHandler(); ClientCommunicationHandler();
ClientCommunicationHandler( ClientCommunicationHandler(

View file

@ -25,8 +25,7 @@ public:
bool running; bool running;
}; };
ConnectionListener::ConnectionListener(const std::wstring &name, TelldusCore::EventRef waitEvent) ConnectionListener::ConnectionListener(const std::wstring &name, TelldusCore::EventRef waitEvent) {
{
d = new PrivateData; d = new PrivateData;
d->waitEvent = waitEvent; d->waitEvent = waitEvent;
@ -43,7 +42,7 @@ ConnectionListener::~ConnectionListener(void) {
delete d; delete d;
} }
void ConnectionListener::run(){ void ConnectionListener::run() {
struct timeval tv = { 0, 0 }; struct timeval tv = { 0, 0 };
//Timeout for select //Timeout for select

View file

@ -17,7 +17,7 @@ public:
int id, firmwareVersion; int id, firmwareVersion;
}; };
Controller::Controller(int id, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent){ Controller::Controller(int id, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent) {
d = new PrivateData; d = new PrivateData;
d->event = event; d->event = event;
d->updateEvent = updateEvent; d->updateEvent = updateEvent;
@ -25,7 +25,7 @@ Controller::Controller(int id, TelldusCore::EventRef event, TelldusCore::EventRe
d->firmwareVersion = 0; d->firmwareVersion = 0;
} }
Controller::~Controller(){ Controller::~Controller() {
delete d; delete d;
} }
@ -40,7 +40,7 @@ void Controller::decodePublishData(const std::string &data) const {
std::list<std::string> msgList = Protocol::decodeData(data); std::list<std::string> msgList = Protocol::decodeData(data);
for (std::list<std::string>::iterator msgIt = msgList.begin(); msgIt != msgList.end(); ++msgIt){ for (std::list<std::string>::iterator msgIt = msgList.begin(); msgIt != msgList.end(); ++msgIt) {
this->publishData(*msgIt); this->publishData(*msgIt);
} }
} }

View file

@ -36,7 +36,7 @@ public:
TelldusCore::Mutex mutex; TelldusCore::Mutex mutex;
}; };
ControllerManager::ControllerManager(TelldusCore::EventRef event, TelldusCore::EventRef updateEvent){ ControllerManager::ControllerManager(TelldusCore::EventRef event, TelldusCore::EventRef updateEvent) {
d = new PrivateData; d = new PrivateData;
d->lastControllerId = 0; d->lastControllerId = 0;
d->event = event; d->event = event;
@ -171,8 +171,8 @@ void ControllerManager::loadControllers() {
bool isNew = false; bool isNew = false;
if (!controllerId) { if (!controllerId) {
controllerId = d->settings.addNode(Settings::Controller); controllerId = d->settings.addNode(Settings::Controller);
if(controllerId < 0){ if(controllerId < 0) {
//TODO: How to handle this? // TODO: How to handle this?
continue; continue;
} }
isNew = true; isNew = true;
@ -212,8 +212,7 @@ void ControllerManager::loadStoredControllers() {
} }
} }
void ControllerManager::queryControllerStatus(){ void ControllerManager::queryControllerStatus() {
std::list<TellStick *> tellStickControllers; std::list<TellStick *> tellStickControllers;
{ {
@ -233,18 +232,18 @@ void ControllerManager::queryControllerStatus(){
std::string noop = "N+"; std::string noop = "N+";
for(std::list<TellStick *>::iterator it = tellStickControllers.begin(); it != tellStickControllers.end(); ++it) { for(std::list<TellStick *>::iterator it = tellStickControllers.begin(); it != tellStickControllers.end(); ++it) {
int success = (*it)->send(noop); int success = (*it)->send(noop);
if(success == TELLSTICK_ERROR_BROKEN_PIPE){ if(success == TELLSTICK_ERROR_BROKEN_PIPE) {
Log::warning("TellStick query: Error in communication with TellStick, resetting USB"); Log::warning("TellStick query: Error in communication with TellStick, resetting USB");
resetController(*it); resetController(*it);
} }
if(success == TELLSTICK_ERROR_BROKEN_PIPE || success == TELLSTICK_ERROR_NOT_FOUND){ if(success == TELLSTICK_ERROR_BROKEN_PIPE || success == TELLSTICK_ERROR_NOT_FOUND) {
reloadControllers = true; reloadControllers = true;
} }
} }
if(!tellStickControllers.size() || reloadControllers){ if(!tellStickControllers.size() || reloadControllers) {
//no tellstick at all found, or controller was reset // no tellstick at all found, or controller was reset
Log::debug("TellStick query: Rescanning USB ports"); //only log as debug, since this will happen all the time if no TellStick is connected Log::debug("TellStick query: Rescanning USB ports"); // only log as debug, since this will happen all the time if no TellStick is connected
loadControllers(); loadControllers();
} }
} }

View file

@ -47,7 +47,7 @@ ControllerMessage::ControllerMessage(const std::string &message) {
} }
} }
ControllerMessage::~ControllerMessage(){ ControllerMessage::~ControllerMessage() {
delete d; delete d;
} }

View file

@ -23,8 +23,7 @@ public:
}; };
Device::Device(int id) Device::Device(int id)
:Mutex() :Mutex() {
{
d = new PrivateData; d = new PrivateData;
d->protocol = 0; d->protocol = 0;
d->preferredControllerId = 0; d->preferredControllerId = 0;
@ -40,8 +39,7 @@ Device::~Device(void) {
* Get-/Set-methods * Get-/Set-methods
*/ */
int Device::getLastSentCommand(int methodsSupported){ int Device::getLastSentCommand(int methodsSupported) {
int lastSentCommand = Device::maskUnsupportedMethods(d->state, methodsSupported); int lastSentCommand = Device::maskUnsupportedMethods(d->state, methodsSupported);
if (lastSentCommand == TELLSTICK_BELL) { if (lastSentCommand == TELLSTICK_BELL) {
@ -63,32 +61,32 @@ int Device::getMethods() const {
return 0; return 0;
} }
void Device::setLastSentCommand(int command, std::wstring value){ void Device::setLastSentCommand(int command, std::wstring value) {
d->state = command; d->state = command;
d->stateValue = value; d->stateValue = value;
} }
std::wstring Device::getModel(){ std::wstring Device::getModel() {
return d->model; return d->model;
} }
void Device::setModel(const std::wstring &model){ void Device::setModel(const std::wstring &model) {
if(d->protocol){ if(d->protocol) {
delete(d->protocol); delete(d->protocol);
d->protocol = 0; d->protocol = 0;
} }
d->model = model; d->model = model;
} }
std::wstring Device::getName(){ std::wstring Device::getName() {
return d->name; return d->name;
} }
void Device::setName(const std::wstring &name){ void Device::setName(const std::wstring &name) {
d->name = name; d->name = name;
} }
std::wstring Device::getParameter(const std::wstring &key){ std::wstring Device::getParameter(const std::wstring &key) {
ParameterMap::iterator it = d->parameterList.find(key); ParameterMap::iterator it = d->parameterList.find(key);
if (it == d->parameterList.end()) { if (it == d->parameterList.end()) {
return L""; return L"";
@ -100,18 +98,18 @@ std::list<std::string> Device::getParametersForProtocol() const {
return Protocol::getParametersForProtocol(getProtocolName()); return Protocol::getParametersForProtocol(getProtocolName());
} }
void Device::setParameter(const std::wstring &key, const std::wstring &value){ void Device::setParameter(const std::wstring &key, const std::wstring &value) {
d->parameterList[key] = value; d->parameterList[key] = value;
if(d->protocol){ if(d->protocol) {
d->protocol->setParameters(d->parameterList); d->protocol->setParameters(d->parameterList);
} }
} }
int Device::getPreferredControllerId(){ int Device::getPreferredControllerId() {
return d->preferredControllerId; return d->preferredControllerId;
} }
void Device::setPreferredControllerId(int controllerId){ void Device::setPreferredControllerId(int controllerId) {
d->preferredControllerId = controllerId; d->preferredControllerId = controllerId;
} }
@ -119,23 +117,23 @@ std::wstring Device::getProtocolName() const {
return d->protocolName; return d->protocolName;
} }
void Device::setProtocolName(const std::wstring &protocolName){ void Device::setProtocolName(const std::wstring &protocolName) {
if(d->protocol){ if(d->protocol) {
delete(d->protocol); delete(d->protocol);
d->protocol = 0; d->protocol = 0;
} }
d->protocolName = protocolName; d->protocolName = protocolName;
} }
std::wstring Device::getStateValue(){ std::wstring Device::getStateValue() {
return d->stateValue; return d->stateValue;
} }
int Device::getType(){ int Device::getType() {
if(d->protocolName == L"group"){ if(d->protocolName == L"group") {
return TELLSTICK_TYPE_GROUP; return TELLSTICK_TYPE_GROUP;
} }
else if(d->protocolName == L"scene"){ else if(d->protocolName == L"scene") {
return TELLSTICK_TYPE_SCENE; return TELLSTICK_TYPE_SCENE;
} }
return TELLSTICK_TYPE_DEVICE; return TELLSTICK_TYPE_DEVICE;
@ -199,7 +197,7 @@ Protocol* Device::retrieveProtocol() const {
} }
d->protocol = Protocol::getProtocolInstance(d->protocolName); d->protocol = Protocol::getProtocolInstance(d->protocolName);
if(d->protocol){ if(d->protocol) {
d->protocol->setModel(d->model); d->protocol->setModel(d->model);
d->protocol->setParameters(d->parameterList); d->protocol->setParameters(d->parameterList);
return d->protocol; return d->protocol;

View file

@ -13,8 +13,7 @@
#include <string> #include <string>
#include <list> #include <list>
class Device : public TelldusCore::Mutex class Device : public TelldusCore::Mutex {
{
public: public:
explicit Device(int id); explicit Device(int id);
~Device(void); ~Device(void);

View file

@ -30,7 +30,7 @@ public:
TelldusCore::EventRef deviceUpdateEvent; TelldusCore::EventRef deviceUpdateEvent;
}; };
DeviceManager::DeviceManager(ControllerManager *controllerManager, TelldusCore::EventRef deviceUpdateEvent){ DeviceManager::DeviceManager(ControllerManager *controllerManager, TelldusCore::EventRef deviceUpdateEvent) {
d = new PrivateData; d = new PrivateData;
d->controllerManager = controllerManager; d->controllerManager = controllerManager;
d->deviceUpdateEvent = deviceUpdateEvent; d->deviceUpdateEvent = deviceUpdateEvent;
@ -52,7 +52,7 @@ DeviceManager::~DeviceManager(void) {
delete d; delete d;
} }
void DeviceManager::fillDevices(){ void DeviceManager::fillDevices() {
int numberOfDevices = d->set.getNumberOfNodes(Settings::Device); int numberOfDevices = d->set.getNumberOfNodes(Settings::Device);
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
@ -74,7 +74,7 @@ void DeviceManager::fillDevices(){
} }
} }
int DeviceManager::getDeviceLastSentCommand(int deviceId, int methodsSupported){ int DeviceManager::getDeviceLastSentCommand(int deviceId, int methodsSupported) {
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
@ -87,8 +87,7 @@ int DeviceManager::getDeviceLastSentCommand(int deviceId, int methodsSupported){
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
int DeviceManager::setDeviceLastSentCommand(int deviceId, int command, const std::wstring &value) int DeviceManager::setDeviceLastSentCommand(int deviceId, int command, const std::wstring &value) {
{
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
@ -99,13 +98,13 @@ int DeviceManager::setDeviceLastSentCommand(int deviceId, int command, const std
d->set.setDeviceState(deviceId, command,value); d->set.setDeviceState(deviceId, command,value);
it->second->setLastSentCommand(command, value); it->second->setLastSentCommand(command, value);
} }
else{ else {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
return TELLSTICK_SUCCESS; return TELLSTICK_SUCCESS;
} }
std::wstring DeviceManager::getDeviceStateValue(int deviceId){ std::wstring DeviceManager::getDeviceStateValue(int deviceId) {
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return L"UNKNOWN"; return L"UNKNOWN";
@ -127,7 +126,7 @@ int DeviceManager::getDeviceMethods(int deviceId) {
return DeviceManager::getDeviceMethods(deviceId, duplicateDeviceIds); return DeviceManager::getDeviceMethods(deviceId, duplicateDeviceIds);
} }
int DeviceManager::getDeviceMethods(int deviceId, std::set<int> &duplicateDeviceIds){ int DeviceManager::getDeviceMethods(int deviceId, std::set<int> &duplicateDeviceIds) {
int type = 0; int type = 0;
int methods = 0; int methods = 0;
std::wstring deviceIds; std::wstring deviceIds;
@ -152,29 +151,28 @@ int DeviceManager::getDeviceMethods(int deviceId, std::set<int> &duplicateDevice
} }
} }
} }
if(type == 0){ if(type == 0) {
return 0; return 0;
} }
if(type == TELLSTICK_TYPE_GROUP){ if(type == TELLSTICK_TYPE_GROUP) {
// get all methods that some device in the groups supports
//get all methods that some device in the groups supports
std::wstring deviceIdBuffer; std::wstring deviceIdBuffer;
std::wstringstream devicesstream(deviceIds); std::wstringstream devicesstream(deviceIds);
methods = 0; methods = 0;
duplicateDeviceIds.insert(deviceId); duplicateDeviceIds.insert(deviceId);
while(std::getline(devicesstream, deviceIdBuffer, L',')){ while(std::getline(devicesstream, deviceIdBuffer, L',')) {
int deviceIdInGroup = TelldusCore::wideToInteger(deviceIdBuffer); int deviceIdInGroup = TelldusCore::wideToInteger(deviceIdBuffer);
if(duplicateDeviceIds.count(deviceIdInGroup) == 1){ if(duplicateDeviceIds.count(deviceIdInGroup) == 1) {
//action for device already executed, or will execute, do nothing to avoid infinite loop // action for device already executed, or will execute, do nothing to avoid infinite loop
continue; continue;
} }
duplicateDeviceIds.insert(deviceIdInGroup); duplicateDeviceIds.insert(deviceIdInGroup);
int deviceMethods = getDeviceMethods(deviceIdInGroup, duplicateDeviceIds); int deviceMethods = getDeviceMethods(deviceIdInGroup, duplicateDeviceIds);
if(deviceMethods > 0){ if(deviceMethods > 0) {
methods |= deviceMethods; methods |= deviceMethods;
} }
} }
@ -182,8 +180,7 @@ int DeviceManager::getDeviceMethods(int deviceId, std::set<int> &duplicateDevice
return methods; return methods;
} }
std::wstring DeviceManager::getDeviceModel(int deviceId){ std::wstring DeviceManager::getDeviceModel(int deviceId) {
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return L"UNKNOWN"; return L"UNKNOWN";
@ -196,8 +193,7 @@ std::wstring DeviceManager::getDeviceModel(int deviceId){
return L"UNKNOWN"; return L"UNKNOWN";
} }
int DeviceManager::setDeviceModel(int deviceId, const std::wstring &model) int DeviceManager::setDeviceModel(int deviceId, const std::wstring &model) {
{
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
@ -211,15 +207,14 @@ int DeviceManager::setDeviceModel(int deviceId, const std::wstring &model)
} }
it->second->setModel(model); it->second->setModel(model);
} }
else{ else {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
return TELLSTICK_SUCCESS; return TELLSTICK_SUCCESS;
} }
std::wstring DeviceManager::getDeviceName(int deviceId){ std::wstring DeviceManager::getDeviceName(int deviceId) {
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return L"UNKNOWN"; return L"UNKNOWN";
@ -232,8 +227,7 @@ std::wstring DeviceManager::getDeviceName(int deviceId){
return L"UNKNOWN"; return L"UNKNOWN";
} }
int DeviceManager::setDeviceName(int deviceId, const std::wstring &name){ int DeviceManager::setDeviceName(int deviceId, const std::wstring &name) {
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
@ -247,32 +241,30 @@ int DeviceManager::setDeviceName(int deviceId, const std::wstring &name){
} }
it->second->setName(name); it->second->setName(name);
} }
else{ else {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
return TELLSTICK_SUCCESS; return TELLSTICK_SUCCESS;
} }
std::wstring DeviceManager::getDeviceParameter(int deviceId, const std::wstring &name, const std::wstring &defaultValue){ std::wstring DeviceManager::getDeviceParameter(int deviceId, const std::wstring &name, const std::wstring &defaultValue) {
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return defaultValue; return defaultValue;
} }
DeviceMap::iterator it = d->devices.find(deviceId); DeviceMap::iterator it = d->devices.find(deviceId);
if (it != d->devices.end()){ if (it != d->devices.end()) {
TelldusCore::MutexLocker deviceLocker(it->second); TelldusCore::MutexLocker deviceLocker(it->second);
std::wstring returnString = it->second->getParameter(name); std::wstring returnString = it->second->getParameter(name);
if(returnString != L""){ if(returnString != L"") {
return returnString; return returnString;
} }
} }
return defaultValue; return defaultValue;
} }
int DeviceManager::setDeviceParameter(int deviceId, const std::wstring &name, const std::wstring &value) int DeviceManager::setDeviceParameter(int deviceId, const std::wstring &name, const std::wstring &value) {
{
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
@ -286,15 +278,14 @@ int DeviceManager::setDeviceParameter(int deviceId, const std::wstring &name, co
} }
it->second->setParameter(name, value); it->second->setParameter(name, value);
} }
else{ else {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
return TELLSTICK_SUCCESS; return TELLSTICK_SUCCESS;
} }
std::wstring DeviceManager::getDeviceProtocol(int deviceId){ std::wstring DeviceManager::getDeviceProtocol(int deviceId) {
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return L"UNKNOWN"; return L"UNKNOWN";
@ -307,8 +298,7 @@ std::wstring DeviceManager::getDeviceProtocol(int deviceId){
return L"UNKNOWN"; return L"UNKNOWN";
} }
int DeviceManager::setDeviceProtocol(int deviceId, const std::wstring &protocol) int DeviceManager::setDeviceProtocol(int deviceId, const std::wstring &protocol) {
{
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
@ -322,28 +312,27 @@ int DeviceManager::setDeviceProtocol(int deviceId, const std::wstring &protocol)
} }
it->second->setProtocolName(protocol); it->second->setProtocolName(protocol);
} }
else{ else {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
return TELLSTICK_SUCCESS; return TELLSTICK_SUCCESS;
} }
int DeviceManager::getNumberOfDevices(){ int DeviceManager::getNumberOfDevices() {
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
return (int)d->devices.size(); return (int)d->devices.size();
} }
int DeviceManager::addDevice(){ int DeviceManager::addDevice() {
int id = d->set.addNode(Settings::Device); int id = d->set.addNode(Settings::Device);
if(id < 0){ if(id < 0) {
return id; return id;
} }
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
d->devices[id] = new Device(id); d->devices[id] = new Device(id);
if(!d->devices[id]){ if(!d->devices[id]) {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
return id; return id;
@ -353,8 +342,7 @@ int DeviceManager::getDeviceId(int deviceIndex) {
return d->set.getNodeId(Settings::Device, deviceIndex); return d->set.getNodeId(Settings::Device, deviceIndex);
} }
int DeviceManager::getDeviceType(int deviceId){ int DeviceManager::getDeviceType(int deviceId) {
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
@ -367,8 +355,7 @@ int DeviceManager::getDeviceType(int deviceId){
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
int DeviceManager::getPreferredControllerId(int deviceId){ int DeviceManager::getPreferredControllerId(int deviceId) {
TelldusCore::MutexLocker deviceListLocker(&d->lock); TelldusCore::MutexLocker deviceListLocker(&d->lock);
if (!d->devices.size()) { if (!d->devices.size()) {
@ -382,15 +369,15 @@ int DeviceManager::getPreferredControllerId(int deviceId){
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
void DeviceManager::connectTellStickController(int vid, int pid, const std::string &serial){ void DeviceManager::connectTellStickController(int vid, int pid, const std::string &serial) {
d->controllerManager->deviceInsertedOrRemoved(vid, pid, serial, true); d->controllerManager->deviceInsertedOrRemoved(vid, pid, serial, true);
} }
void DeviceManager::disconnectTellStickController(int vid, int pid, const std::string &serial){ void DeviceManager::disconnectTellStickController(int vid, int pid, const std::string &serial) {
d->controllerManager->deviceInsertedOrRemoved(vid, pid, serial, false); d->controllerManager->deviceInsertedOrRemoved(vid, pid, serial, false);
} }
int DeviceManager::doAction(int deviceId, int action, unsigned char data){ int DeviceManager::doAction(int deviceId, int action, unsigned char data) {
Device *device = 0; Device *device = 0;
//On the stack and will be released if we have a device lock. //On the stack and will be released if we have a device lock.
std::auto_ptr<TelldusCore::MutexLocker> deviceLocker(0); std::auto_ptr<TelldusCore::MutexLocker> deviceLocker(0);
@ -412,7 +399,7 @@ int DeviceManager::doAction(int deviceId, int action, unsigned char data){
int retval = TELLSTICK_ERROR_UNKNOWN; int retval = TELLSTICK_ERROR_UNKNOWN;
if(device->getType() == TELLSTICK_TYPE_GROUP || device->getType() == TELLSTICK_TYPE_SCENE){ if(device->getType() == TELLSTICK_TYPE_GROUP || device->getType() == TELLSTICK_TYPE_SCENE) {
std::wstring devices = device->getParameter(L"devices"); std::wstring devices = device->getParameter(L"devices");
deviceLocker = std::auto_ptr<TelldusCore::MutexLocker>(0); deviceLocker = std::auto_ptr<TelldusCore::MutexLocker>(0);
std::set<int> *duplicateDeviceIds = new std::set<int>; std::set<int> *duplicateDeviceIds = new std::set<int>;
@ -436,26 +423,26 @@ int DeviceManager::doAction(int deviceId, int action, unsigned char data){
device = it->second; device = it->second;
} //devicelist unlocked } //devicelist unlocked
} }
else{ else {
Controller *controller = d->controllerManager->getBestControllerById(device->getPreferredControllerId()); Controller *controller = d->controllerManager->getBestControllerById(device->getPreferredControllerId());
if(!controller){ if(!controller) {
Log::warning("Trying to execute action, but no controller found. Rescanning USB ports"); Log::warning("Trying to execute action, but no controller found. Rescanning USB ports");
//no controller found, scan for one, and retry once //no controller found, scan for one, and retry once
d->controllerManager->loadControllers(); d->controllerManager->loadControllers();
controller = d->controllerManager->getBestControllerById(device->getPreferredControllerId()); controller = d->controllerManager->getBestControllerById(device->getPreferredControllerId());
} }
if(controller){ if(controller) {
retval = device->doAction(action, data, controller); retval = device->doAction(action, data, controller);
if(retval == TELLSTICK_ERROR_BROKEN_PIPE){ if(retval == TELLSTICK_ERROR_BROKEN_PIPE) {
Log::warning("Error in communication with TellStick when executing action. Resetting USB"); Log::warning("Error in communication with TellStick when executing action. Resetting USB");
d->controllerManager->resetController(controller); d->controllerManager->resetController(controller);
} }
if(retval == TELLSTICK_ERROR_BROKEN_PIPE || retval == TELLSTICK_ERROR_NOT_FOUND){ if(retval == TELLSTICK_ERROR_BROKEN_PIPE || retval == TELLSTICK_ERROR_NOT_FOUND) {
Log::warning("Rescanning USB ports"); Log::warning("Rescanning USB ports");
d->controllerManager->loadControllers(); d->controllerManager->loadControllers();
controller = d->controllerManager->getBestControllerById(device->getPreferredControllerId()); controller = d->controllerManager->getBestControllerById(device->getPreferredControllerId());
if(!controller){ if(!controller) {
Log::error("No contoller (TellStick) found, even after reset. Giving up."); Log::error("No contoller (TellStick) found, even after reset. Giving up.");
return TELLSTICK_ERROR_NOT_FOUND; return TELLSTICK_ERROR_NOT_FOUND;
} }
@ -477,19 +464,18 @@ int DeviceManager::doAction(int deviceId, int action, unsigned char data){
return retval; return retval;
} }
int DeviceManager::doGroupAction(const std::wstring devices, const int action, const unsigned char data, const int type, const int groupDeviceId, std::set<int> *duplicateDeviceIds){ int DeviceManager::doGroupAction(const std::wstring devices, const int action, const unsigned char data, const int type, const int groupDeviceId, std::set<int> *duplicateDeviceIds) {
int retval = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED; int retval = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
std::wstring singledevice; std::wstring singledevice;
std::wstringstream devicesstream(devices); std::wstringstream devicesstream(devices);
duplicateDeviceIds->insert(groupDeviceId); duplicateDeviceIds->insert(groupDeviceId);
while(std::getline(devicesstream, singledevice, L',')){ while(std::getline(devicesstream, singledevice, L',')) {
int deviceId = TelldusCore::wideToInteger(singledevice); int deviceId = TelldusCore::wideToInteger(singledevice);
if(duplicateDeviceIds->count(deviceId) == 1){ if(duplicateDeviceIds->count(deviceId) == 1) {
//action for device already executed, or will execute, do nothing to avoid infinite loop // action for device already executed, or will execute, do nothing to avoid infinite loop
continue; continue;
} }
@ -497,20 +483,20 @@ int DeviceManager::doGroupAction(const std::wstring devices, const int action, c
int deviceReturnValue = TELLSTICK_SUCCESS; int deviceReturnValue = TELLSTICK_SUCCESS;
if(type == TELLSTICK_TYPE_SCENE && (action == TELLSTICK_TURNON || action == TELLSTICK_EXECUTE)){ if(type == TELLSTICK_TYPE_SCENE && (action == TELLSTICK_TURNON || action == TELLSTICK_EXECUTE)) {
deviceReturnValue = executeScene(singledevice, groupDeviceId); deviceReturnValue = executeScene(singledevice, groupDeviceId);
} }
else if(type == TELLSTICK_TYPE_GROUP){ else if(type == TELLSTICK_TYPE_GROUP) {
if(deviceId != 0){ if(deviceId != 0) {
int childType = DeviceManager::getDeviceType(deviceId); int childType = DeviceManager::getDeviceType(deviceId);
if(childType == TELLSTICK_TYPE_DEVICE){ if(childType == TELLSTICK_TYPE_DEVICE) {
deviceReturnValue = doAction(deviceId, action, data); deviceReturnValue = doAction(deviceId, action, data);
} }
else if(childType == TELLSTICK_TYPE_SCENE){ else if(childType == TELLSTICK_TYPE_SCENE) {
deviceReturnValue = doGroupAction(DeviceManager::getDeviceParameter(deviceId, L"devices", L""), action, data, childType, deviceId, duplicateDeviceIds); //TODO make scenes infinite loops-safe deviceReturnValue = doGroupAction(DeviceManager::getDeviceParameter(deviceId, L"devices", L""), action, data, childType, deviceId, duplicateDeviceIds); // TODO make scenes infinite loops-safe
} }
else{ else {
//group (in group) // group (in group)
deviceReturnValue = doGroupAction(DeviceManager::getDeviceParameter(deviceId, L"devices", L""), action, data, childType, deviceId, duplicateDeviceIds); deviceReturnValue = doGroupAction(DeviceManager::getDeviceParameter(deviceId, L"devices", L""), action, data, childType, deviceId, duplicateDeviceIds);
if(deviceReturnValue == TELLSTICK_SUCCESS) { if(deviceReturnValue == TELLSTICK_SUCCESS) {
@ -522,15 +508,15 @@ int DeviceManager::doGroupAction(const std::wstring devices, const int action, c
} }
} }
} }
else{ else {
deviceReturnValue = TELLSTICK_ERROR_DEVICE_NOT_FOUND; //Probably incorrectly formatted parameter deviceReturnValue = TELLSTICK_ERROR_DEVICE_NOT_FOUND; // Probably incorrectly formatted parameter
} }
} }
if(deviceReturnValue != TELLSTICK_ERROR_METHOD_NOT_SUPPORTED){ if(deviceReturnValue != TELLSTICK_ERROR_METHOD_NOT_SUPPORTED) {
//if error(s), return the last error, but still try to continue the action with the other devices // if error(s), return the last error, but still try to continue the action with the other devices
//if the error is a method not supported we igore is since there might be others supporting it // if the error is a method not supported we igore is since there might be others supporting it
//If no devices support the method the default value will be returned (method not supported) // If no devices support the method the default value will be returned (method not supported)
retval = deviceReturnValue; retval = deviceReturnValue;
} }
@ -538,45 +524,43 @@ int DeviceManager::doGroupAction(const std::wstring devices, const int action, c
return retval; return retval;
} }
int DeviceManager::executeScene(std::wstring singledevice, int groupDeviceId){ int DeviceManager::executeScene(std::wstring singledevice, int groupDeviceId) {
std::wstringstream devicestream(singledevice); std::wstringstream devicestream(singledevice);
const int deviceParameterLength = 3; const int deviceParameterLength = 3;
std::wstring deviceParts[deviceParameterLength] = {L"", L"", L""}; std::wstring deviceParts[deviceParameterLength] = {L"", L"", L""};
std::wstring devicePart = L""; std::wstring devicePart = L"";
int i = 0; int i = 0;
while(std::getline(devicestream, devicePart, L':') && i < deviceParameterLength){ while(std::getline(devicestream, devicePart, L':') && i < deviceParameterLength) {
deviceParts[i] = devicePart; deviceParts[i] = devicePart;
i++; i++;
} }
if(deviceParts[0] == L"" || deviceParts[1] == L""){ if(deviceParts[0] == L"" || deviceParts[1] == L"") {
return TELLSTICK_ERROR_UNKNOWN; //malformed or missing parameter return TELLSTICK_ERROR_UNKNOWN; // malformed or missing parameter
} }
int deviceId = TelldusCore::wideToInteger(deviceParts[0]); int deviceId = TelldusCore::wideToInteger(deviceParts[0]);
if(deviceId == groupDeviceId){ if(deviceId == groupDeviceId) {
return TELLSTICK_ERROR_UNKNOWN; //the scene itself has been added to its devices, avoid infinite loop return TELLSTICK_ERROR_UNKNOWN; // the scene itself has been added to its devices, avoid infinite loop
} }
int method = Device::methodId(TelldusCore::wideToString(deviceParts[1])); //support methodparts both in the form of integers (e.g. TELLSTICK_TURNON) or text (e.g. "turnon") int method = Device::methodId(TelldusCore::wideToString(deviceParts[1])); // support methodparts both in the form of integers (e.g. TELLSTICK_TURNON) or text (e.g. "turnon")
if(method == 0){ if(method == 0) {
method = TelldusCore::wideToInteger(deviceParts[1]); method = TelldusCore::wideToInteger(deviceParts[1]);
} }
unsigned char devicedata = 0; unsigned char devicedata = 0;
if(deviceParts[2] != L""){ if(deviceParts[2] != L"") {
devicedata = TelldusCore::wideToInteger(deviceParts[2]); devicedata = TelldusCore::wideToInteger(deviceParts[2]);
} }
if(deviceId > 0 && method > 0){ //check for format error in parameter "devices" if(deviceId > 0 && method > 0) { // check for format error in parameter "devices"
return doAction(deviceId, method, devicedata); return doAction(deviceId, method, devicedata);
} }
return TELLSTICK_ERROR_UNKNOWN; return TELLSTICK_ERROR_UNKNOWN;
} }
int DeviceManager::removeDevice(int deviceId){ int DeviceManager::removeDevice(int deviceId) {
Device *device = 0; Device *device = 0;
{ {
int ret = d->set.removeNode(Settings::Device, deviceId); //remove from register/settings int ret = d->set.removeNode(Settings::Device, deviceId); //remove from register/settings
@ -593,7 +577,7 @@ int DeviceManager::removeDevice(int deviceId){
device = it->second; device = it->second;
d->devices.erase(it); //remove from list, keep reference d->devices.erase(it); //remove from list, keep reference
} }
else{ else {
return TELLSTICK_ERROR_DEVICE_NOT_FOUND; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
} }
@ -679,14 +663,14 @@ void DeviceManager::handleControllerMessage(const ControllerEventData &eventData
std::list<std::string> parameters = it->second->getParametersForProtocol(); std::list<std::string> parameters = it->second->getParametersForProtocol();
bool thisDevice = true; bool thisDevice = true;
for (std::list<std::string>::iterator paramIt = parameters.begin(); paramIt != parameters.end(); ++paramIt){ for (std::list<std::string>::iterator paramIt = parameters.begin(); paramIt != parameters.end(); ++paramIt) {
if(!TelldusCore::comparei(it->second->getParameter(TelldusCore::charToWstring((*paramIt).c_str())), TelldusCore::charToWstring(msg.getParameter(*paramIt).c_str()))){ if(!TelldusCore::comparei(it->second->getParameter(TelldusCore::charToWstring((*paramIt).c_str())), TelldusCore::charToWstring(msg.getParameter(*paramIt).c_str()))) {
thisDevice = false; thisDevice = false;
break; break;
} }
} }
if(!thisDevice){ if(!thisDevice) {
continue; continue;
} }
@ -744,26 +728,25 @@ void DeviceManager::setSensorValueAndSignal( const std::string &dataType, int da
d->deviceUpdateEvent->signal(eventData); d->deviceUpdateEvent->signal(eventData);
} }
int DeviceManager::sendRawCommand(const std::wstring &command, int reserved){ int DeviceManager::sendRawCommand(const std::wstring &command, int reserved) {
Controller *controller = d->controllerManager->getBestControllerById(-1); Controller *controller = d->controllerManager->getBestControllerById(-1);
if(!controller){ if(!controller) {
//no controller found, scan for one, and retry once // no controller found, scan for one, and retry once
d->controllerManager->loadControllers(); d->controllerManager->loadControllers();
controller = d->controllerManager->getBestControllerById(-1); controller = d->controllerManager->getBestControllerById(-1);
} }
int retval = TELLSTICK_ERROR_UNKNOWN; int retval = TELLSTICK_ERROR_UNKNOWN;
if(controller){ if(controller) {
retval = controller->send(TelldusCore::wideToString(command)); retval = controller->send(TelldusCore::wideToString(command));
if(retval == TELLSTICK_ERROR_BROKEN_PIPE){ if(retval == TELLSTICK_ERROR_BROKEN_PIPE) {
d->controllerManager->resetController(controller); d->controllerManager->resetController(controller);
} }
if(retval == TELLSTICK_ERROR_BROKEN_PIPE || retval == TELLSTICK_ERROR_NOT_FOUND){ if(retval == TELLSTICK_ERROR_BROKEN_PIPE || retval == TELLSTICK_ERROR_NOT_FOUND) {
d->controllerManager->loadControllers(); d->controllerManager->loadControllers();
controller = d->controllerManager->getBestControllerById(-1); controller = d->controllerManager->getBestControllerById(-1);
if(!controller){ if(!controller) {
return TELLSTICK_ERROR_NOT_FOUND; return TELLSTICK_ERROR_NOT_FOUND;
} }
retval = controller->send(TelldusCore::wideToString(command)); //retry one more time retval = controller->send(TelldusCore::wideToString(command)); //retry one more time

View file

@ -16,8 +16,7 @@
class Sensor; class Sensor;
class DeviceManager class DeviceManager {
{
public: public:
DeviceManager(ControllerManager *controllerManager, TelldusCore::EventRef deviceUpdateEvent); DeviceManager(ControllerManager *controllerManager, TelldusCore::EventRef deviceUpdateEvent);
~DeviceManager(void); ~DeviceManager(void);

View file

@ -25,8 +25,7 @@ public:
}; };
EventUpdateManager::EventUpdateManager() EventUpdateManager::EventUpdateManager()
:Thread() :Thread() {
{
d = new PrivateData; d = new PrivateData;
d->stopEvent = d->eventHandler.addEvent(); d->stopEvent = d->eventHandler.addEvent();
d->updateEvent = d->eventHandler.addEvent(); d->updateEvent = d->eventHandler.addEvent();
@ -46,63 +45,60 @@ EventUpdateManager::~EventUpdateManager(void) {
delete d; delete d;
} }
TelldusCore::EventRef EventUpdateManager::retrieveUpdateEvent(){ TelldusCore::EventRef EventUpdateManager::retrieveUpdateEvent() {
return d->updateEvent; return d->updateEvent;
} }
void EventUpdateManager::run(){ void EventUpdateManager::run() {
while(!d->stopEvent->isSignaled()) {
while(!d->stopEvent->isSignaled()){
if (!d->eventHandler.waitForAny()) { if (!d->eventHandler.waitForAny()) {
continue; continue;
} }
if(d->clientConnectEvent->isSignaled()){ if(d->clientConnectEvent->isSignaled()) {
//new client added // new client added
TelldusCore::EventDataRef eventData = d->clientConnectEvent->takeSignal(); TelldusCore::EventDataRef eventData = d->clientConnectEvent->takeSignal();
ConnectionListenerEventData *data = reinterpret_cast<ConnectionListenerEventData*>(eventData.get()); ConnectionListenerEventData *data = reinterpret_cast<ConnectionListenerEventData*>(eventData.get());
if(data){ if(data) {
d->clients.push_back(data->socket); d->clients.push_back(data->socket);
} }
} }
else if(d->updateEvent->isSignaled()){ else if(d->updateEvent->isSignaled()) {
//device event, signal all clients // device event, signal all clients
TelldusCore::EventDataRef eventData = d->updateEvent->takeSignal(); TelldusCore::EventDataRef eventData = d->updateEvent->takeSignal();
EventUpdateData *data = reinterpret_cast<EventUpdateData*>(eventData.get()); EventUpdateData *data = reinterpret_cast<EventUpdateData*>(eventData.get());
if(data){ if(data) {
sendMessageToClients(data); sendMessageToClients(data);
} }
} }
} }
} }
void EventUpdateManager::sendMessageToClients(EventUpdateData *data){ void EventUpdateManager::sendMessageToClients(EventUpdateData *data) {
int connected = 0; int connected = 0;
for(SocketList::iterator it = d->clients.begin(); it != d->clients.end();){ for(SocketList::iterator it = d->clients.begin(); it != d->clients.end();) {
if((*it)->isConnected()){ if((*it)->isConnected()) {
connected++; connected++;
TelldusCore::Message msg; TelldusCore::Message msg;
if(data->messageType == L"TDDeviceEvent"){ if(data->messageType == L"TDDeviceEvent") {
msg.addArgument("TDDeviceEvent"); msg.addArgument("TDDeviceEvent");
msg.addArgument(data->deviceId); msg.addArgument(data->deviceId);
msg.addArgument(data->eventState); msg.addArgument(data->eventState);
msg.addArgument(data->eventValue); //string msg.addArgument(data->eventValue); //string
} }
else if(data->messageType == L"TDDeviceChangeEvent"){ else if(data->messageType == L"TDDeviceChangeEvent") {
msg.addArgument("TDDeviceChangeEvent"); msg.addArgument("TDDeviceChangeEvent");
msg.addArgument(data->deviceId); msg.addArgument(data->deviceId);
msg.addArgument(data->eventDeviceChanges); msg.addArgument(data->eventDeviceChanges);
msg.addArgument(data->eventChangeType); msg.addArgument(data->eventChangeType);
} }
else if(data->messageType == L"TDRawDeviceEvent"){ else if(data->messageType == L"TDRawDeviceEvent") {
msg.addArgument("TDRawDeviceEvent"); msg.addArgument("TDRawDeviceEvent");
msg.addArgument(data->eventValue); //string msg.addArgument(data->eventValue); //string
msg.addArgument(data->controllerId); msg.addArgument(data->controllerId);
} }
else if(data->messageType == L"TDSensorEvent"){ else if(data->messageType == L"TDSensorEvent") {
msg.addArgument("TDSensorEvent"); msg.addArgument("TDSensorEvent");
msg.addArgument(data->protocol); msg.addArgument(data->protocol);
msg.addArgument(data->model); msg.addArgument(data->model);
@ -123,8 +119,8 @@ void EventUpdateManager::sendMessageToClients(EventUpdateData *data){
it++; it++;
} }
else{ else {
//connection is dead, remove it // connection is dead, remove it
delete *it; delete *it;
it = d->clients.erase(it); it = d->clients.erase(it);
} }

View file

@ -29,8 +29,7 @@ public:
int timestamp; int timestamp;
}; };
class EventUpdateManager : public TelldusCore::Thread class EventUpdateManager : public TelldusCore::Thread {
{
public: public:
EventUpdateManager(void); EventUpdateManager(void);
~EventUpdateManager(void); ~EventUpdateManager(void);

View file

@ -31,8 +31,7 @@ public:
Log *Log::PrivateData::instance = 0; Log *Log::PrivateData::instance = 0;
Log::Log() Log::Log()
:d(new PrivateData) :d(new PrivateData) {
{
#if defined(_LINUX) #if defined(_LINUX)
setlogmask(LOG_UPTO(LOG_INFO)); setlogmask(LOG_UPTO(LOG_INFO));
openlog("telldusd", LOG_CONS, LOG_USER); openlog("telldusd", LOG_CONS, LOG_USER);

View file

@ -37,8 +37,7 @@ public:
std::wstring model; std::wstring model;
}; };
Protocol::Protocol(){ Protocol::Protocol() {
d = new PrivateData; d = new PrivateData;
} }
@ -57,11 +56,11 @@ std::wstring Protocol::model() const {
return strModel; return strModel;
} }
void Protocol::setModel(const std::wstring &model){ void Protocol::setModel(const std::wstring &model) {
d->model = model; d->model = model;
} }
void Protocol::setParameters(ParameterMap &parameterList){ void Protocol::setParameters(ParameterMap &parameterList) {
d->parameterList = parameterList; d->parameterList = parameterList;
} }
@ -96,9 +95,8 @@ bool Protocol::checkBit(int data, int bitno) {
} }
Protocol *Protocol::getProtocolInstance(const std::wstring &protocolname){ Protocol *Protocol::getProtocolInstance(const std::wstring &protocolname) {
if(TelldusCore::comparei(protocolname, L"arctech")) {
if(TelldusCore::comparei(protocolname, L"arctech")){
return new ProtocolNexa(); return new ProtocolNexa();
} else if (TelldusCore::comparei(protocolname, L"brateck")) { } else if (TelldusCore::comparei(protocolname, L"brateck")) {
@ -153,7 +151,7 @@ Protocol *Protocol::getProtocolInstance(const std::wstring &protocolname){
std::list<std::string> Protocol::getParametersForProtocol(const std::wstring &protocolName) { std::list<std::string> Protocol::getParametersForProtocol(const std::wstring &protocolName) {
std::list<std::string> parameters; std::list<std::string> parameters;
if(TelldusCore::comparei(protocolName, L"arctech")){ if(TelldusCore::comparei(protocolName, L"arctech")) {
parameters.push_back("house"); parameters.push_back("house");
parameters.push_back("unit"); parameters.push_back("unit");

View file

@ -16,8 +16,7 @@ typedef std::map<std::wstring, std::wstring> ParameterMap;
class Controller; class Controller;
class Protocol class Protocol {
{
public: public:
Protocol(); Protocol();
virtual ~Protocol(void); virtual ~Protocol(void);

View file

@ -10,8 +10,7 @@
#include <string> #include <string>
#include "service/Protocol.h" #include "service/Protocol.h"
class ProtocolBrateck : public Protocol class ProtocolBrateck : public Protocol {
{
public: public:
int methods() const; int methods() const;
virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller);

View file

@ -94,8 +94,7 @@ unsigned int ProtocolEverflourish::calculateChecksum(unsigned int x) {
return res; return res;
} }
std::string ProtocolEverflourish::decodeData(ControllerMessage &dataMsg) std::string ProtocolEverflourish::decodeData(ControllerMessage &dataMsg) {
{
std::string data = dataMsg.getParameter("data"); std::string data = dataMsg.getParameter("data");
unsigned int allData; unsigned int allData;
unsigned int house = 0; unsigned int house = 0;
@ -113,20 +112,20 @@ std::string ProtocolEverflourish::decodeData(ControllerMessage &dataMsg)
method = allData & 0xF; method = allData & 0xF;
if(house < 0 || house > 16383 || unit < 1 || unit > 4){ if(house < 0 || house > 16383 || unit < 1 || unit > 4) {
//not everflourish // not everflourish
return ""; return "";
} }
std::stringstream retString; std::stringstream retString;
retString << "class:command;protocol:everflourish;model:selflearning;house:" << house << ";unit:" << unit << ";method:"; retString << "class:command;protocol:everflourish;model:selflearning;house:" << house << ";unit:" << unit << ";method:";
if(method == 0){ if(method == 0) {
retString << "turnoff;"; retString << "turnoff;";
} }
else if(method == 15){ else if(method == 15) {
retString << "turnon;"; retString << "turnon;";
} }
else if(method == 10){ else if(method == 10) {
retString << "learn;"; retString << "learn;";
} }
else { else {

View file

@ -11,8 +11,7 @@
#include "service/Protocol.h" #include "service/Protocol.h"
#include "service/ControllerMessage.h" #include "service/ControllerMessage.h"
class ProtocolEverflourish : public Protocol class ProtocolEverflourish : public Protocol {
{
public: public:
int methods() const; int methods() const;
virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller);

View file

@ -10,8 +10,7 @@
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
std::string ProtocolFineoffset::decodeData(ControllerMessage &dataMsg) std::string ProtocolFineoffset::decodeData(ControllerMessage &dataMsg) {
{
std::string data = dataMsg.getParameter("data"); std::string data = dataMsg.getParameter("data");
if (data.length() < 8) { if (data.length() < 8) {
return ""; return "";

View file

@ -11,8 +11,7 @@
#include "service/Protocol.h" #include "service/Protocol.h"
#include "service/ControllerMessage.h" #include "service/ControllerMessage.h"
class ProtocolFineoffset : public Protocol class ProtocolFineoffset : public Protocol {
{
public: public:
static std::string decodeData(ControllerMessage &dataMsg); static std::string decodeData(ControllerMessage &dataMsg);
}; };

View file

@ -10,8 +10,7 @@
#include <string> #include <string>
#include "service/Protocol.h" #include "service/Protocol.h"
class ProtocolFuhaote : public Protocol class ProtocolFuhaote : public Protocol {
{
public: public:
int methods() const; int methods() const;
virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller);

View file

@ -10,8 +10,7 @@
#include <string> #include <string>
#include "service/Protocol.h" #include "service/Protocol.h"
class ProtocolHasta : public Protocol class ProtocolHasta : public Protocol {
{
public: public:
int methods() const; int methods() const;
virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller);

View file

@ -10,8 +10,7 @@
#include <string> #include <string>
#include "service/Protocol.h" #include "service/Protocol.h"
class ProtocolIkea : public Protocol class ProtocolIkea : public Protocol {
{
public: public:
int methods() const; int methods() const;
virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller);

View file

@ -10,8 +10,7 @@
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
std::string ProtocolMandolyn::decodeData(ControllerMessage &dataMsg) std::string ProtocolMandolyn::decodeData(ControllerMessage &dataMsg) {
{
std::string data = dataMsg.getParameter("data"); std::string data = dataMsg.getParameter("data");
uint32_t value = (uint32_t)TelldusCore::hexTo64l(data); uint32_t value = (uint32_t)TelldusCore::hexTo64l(data);

View file

@ -11,8 +11,7 @@
#include "service/Protocol.h" #include "service/Protocol.h"
#include "service/ControllerMessage.h" #include "service/ControllerMessage.h"
class ProtocolMandolyn : public Protocol class ProtocolMandolyn : public Protocol {
{
public: public:
static std::string decodeData(ControllerMessage &dataMsg); static std::string decodeData(ControllerMessage &dataMsg);
}; };

View file

@ -163,23 +163,22 @@ std::string ProtocolNexa::getStringSelflearningForCode(int intHouse, int intCode
return strMessage; return strMessage;
} }
std::string ProtocolNexa::decodeData(ControllerMessage& dataMsg) std::string ProtocolNexa::decodeData(ControllerMessage& dataMsg) {
{
unsigned long allData = 0; unsigned long allData = 0;
sscanf(dataMsg.getParameter("data").c_str(), "%lx", &allData); sscanf(dataMsg.getParameter("data").c_str(), "%lx", &allData);
if(TelldusCore::comparei(dataMsg.model(), L"selflearning")){ if(TelldusCore::comparei(dataMsg.model(), L"selflearning")) {
//selflearning // selflearning
return decodeDataSelfLearning(allData); return decodeDataSelfLearning(allData);
} }
else{ else {
//codeswitch // codeswitch
return decodeDataCodeSwitch(allData); return decodeDataCodeSwitch(allData);
} }
} }
std::string ProtocolNexa::decodeDataSelfLearning(long allData){ std::string ProtocolNexa::decodeDataSelfLearning(long allData) {
unsigned int house = 0; unsigned int house = 0;
unsigned int unit = 0; unsigned int unit = 0;
unsigned int group = 0; unsigned int group = 0;
@ -197,17 +196,17 @@ std::string ProtocolNexa::decodeDataSelfLearning(long allData){
unit = allData & 0xF; unit = allData & 0xF;
unit++; unit++;
if(house < 1 || house > 67108863 || unit < 1 || unit > 16){ if(house < 1 || house > 67108863 || unit < 1 || unit > 16) {
//not arctech selflearning // not arctech selflearning
return ""; return "";
} }
std::stringstream retString; std::stringstream retString;
retString << "class:command;protocol:arctech;model:selflearning;house:" << house << ";unit:" << unit << ";group:" << group << ";method:"; retString << "class:command;protocol:arctech;model:selflearning;house:" << house << ";unit:" << unit << ";group:" << group << ";method:";
if(method == 1){ if(method == 1) {
retString << "turnon;"; retString << "turnon;";
} }
else if(method == 0){ else if(method == 0) {
retString << "turnoff;"; retString << "turnoff;";
} }
else { else {
@ -218,8 +217,7 @@ std::string ProtocolNexa::decodeDataSelfLearning(long allData){
return retString.str(); return retString.str();
} }
std::string ProtocolNexa::decodeDataCodeSwitch(long allData){ std::string ProtocolNexa::decodeDataCodeSwitch(long allData) {
unsigned int house = 0; unsigned int house = 0;
unsigned int unit = 0; unsigned int unit = 0;
unsigned int method = 0; unsigned int method = 0;
@ -233,33 +231,33 @@ std::string ProtocolNexa::decodeDataCodeSwitch(long allData){
house = allData & 0xF; house = allData & 0xF;
if(house < 0 || house > 16 || unit < 1 || unit > 16){ if(house < 0 || house > 16 || unit < 1 || unit > 16) {
//not arctech codeswitch // not arctech codeswitch
return ""; return "";
} }
house = house + 'A'; //house from A to P house = house + 'A'; //house from A to P
if(method != 6 && lastArctecCodeSwitchWasTurnOff == 1){ if(method != 6 && lastArctecCodeSwitchWasTurnOff == 1) {
lastArctecCodeSwitchWasTurnOff = 0; lastArctecCodeSwitchWasTurnOff = 0;
return ""; //probably a stray turnon or bell (perhaps: only certain time interval since last, check that it's the same house/unit... Will lose return ""; //probably a stray turnon or bell (perhaps: only certain time interval since last, check that it's the same house/unit... Will lose
//one turnon/bell, but it's better than the alternative... //one turnon/bell, but it's better than the alternative...
} }
if(method == 6){ if(method == 6) {
lastArctecCodeSwitchWasTurnOff = 1; lastArctecCodeSwitchWasTurnOff = 1;
} }
std::stringstream retString; std::stringstream retString;
retString << "class:command;protocol:arctech;model:codeswitch;house:" << char(house); retString << "class:command;protocol:arctech;model:codeswitch;house:" << char(house);
if(method == 6){ if(method == 6) {
retString << ";unit:" << unit << ";method:turnoff;"; retString << ";unit:" << unit << ";method:turnoff;";
} }
else if(method == 14){ else if(method == 14) {
retString << ";unit:" << unit << ";method:turnon;"; retString << ";unit:" << unit << ";method:turnon;";
} }
else if(method == 15){ else if(method == 15) {
retString << ";method:bell;"; retString << ";method:bell;";
} }
else { else {

View file

@ -10,8 +10,7 @@
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
std::string ProtocolOregon::decodeData(ControllerMessage &dataMsg) std::string ProtocolOregon::decodeData(ControllerMessage &dataMsg) {
{
std::string data = dataMsg.getParameter("data"); std::string data = dataMsg.getParameter("data");
std::wstring model = dataMsg.model(); std::wstring model = dataMsg.model();

View file

@ -11,8 +11,7 @@
#include "service/Protocol.h" #include "service/Protocol.h"
#include "service/ControllerMessage.h" #include "service/ControllerMessage.h"
class ProtocolOregon : public Protocol class ProtocolOregon : public Protocol {
{
public: public:
static std::string decodeData(ControllerMessage &dataMsg); static std::string decodeData(ControllerMessage &dataMsg);

View file

@ -10,8 +10,7 @@
#include <string> #include <string>
#include "service/Protocol.h" #include "service/Protocol.h"
class ProtocolRisingSun : public Protocol class ProtocolRisingSun : public Protocol {
{
public: public:
int methods() const; int methods() const;
virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller);

View file

@ -41,8 +41,7 @@ std::string ProtocolSartano::getStringForCode(const std::wstring &strCode, int m
} }
std::string ProtocolSartano::decodeData(ControllerMessage &dataMsg) std::string ProtocolSartano::decodeData(ControllerMessage &dataMsg) {
{
std::string data = dataMsg.getParameter("data"); std::string data = dataMsg.getParameter("data");
signed int allDataIn; signed int allDataIn;
signed int allData = 0; signed int allData = 0;
@ -54,9 +53,9 @@ std::string ProtocolSartano::decodeData(ControllerMessage &dataMsg)
sscanf(data.c_str(), "%X", &allDataIn); sscanf(data.c_str(), "%X", &allDataIn);
unsigned long mask = (1<<11); unsigned long mask = (1<<11);
for(int i=0;i<12;++i){ for(int i=0;i<12;++i) {
allData >>= 1; allData >>= 1;
if((allDataIn & mask) == 0){ if((allDataIn & mask) == 0) {
allData |= (1<<11); allData |= (1<<11);
} }
mask >>= 1; mask >>= 1;
@ -70,39 +69,39 @@ std::string ProtocolSartano::decodeData(ControllerMessage &dataMsg)
method2 = allData & 0x1; method2 = allData & 0x1;
if(method1 == 0 && method2 == 1){ if(method1 == 0 && method2 == 1) {
method = 0; //off method = 0; // off
} }
else if(method1 == 1 && method2 == 0){ else if(method1 == 1 && method2 == 0) {
method = 1; //on method = 1; // on
} }
else{ else {
return ""; return "";
} }
if(code < 0 || code > 1023){ if(code < 0 || code > 1023) {
//not sartano // not sartano
return ""; return "";
} }
std::stringstream retString; std::stringstream retString;
retString << "class:command;protocol:sartano;model:codeswitch;code:"; retString << "class:command;protocol:sartano;model:codeswitch;code:";
mask = (1<<9); mask = (1<<9);
for(int i=0;i<10;i++){ for(int i=0;i<10;i++) {
if((code & mask) != 0){ if((code & mask) != 0) {
retString << 1; retString << 1;
} }
else{ else {
retString << 0; retString << 0;
} }
mask >>= 1; mask >>= 1;
} }
retString << ";method:"; retString << ";method:";
if(method == 0){ if(method == 0) {
retString << "turnoff;"; retString << "turnoff;";
} }
else{ else {
retString << "turnon;"; retString << "turnon;";
} }

View file

@ -11,8 +11,7 @@
#include "service/Protocol.h" #include "service/Protocol.h"
#include "service/ControllerMessage.h" #include "service/ControllerMessage.h"
class ProtocolSartano : public Protocol class ProtocolSartano : public Protocol {
{
public: public:
int methods() const; int methods() const;
virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller);

View file

@ -10,8 +10,7 @@
#include <string> #include <string>
#include "service/Protocol.h" #include "service/Protocol.h"
class ProtocolSilvanChip : public Protocol class ProtocolSilvanChip : public Protocol {
{
public: public:
int methods() const; int methods() const;
virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller);

View file

@ -10,8 +10,7 @@
#include <string> #include <string>
#include "service/Protocol.h" #include "service/Protocol.h"
class ProtocolUpm : public Protocol class ProtocolUpm : public Protocol {
{
public: public:
int methods() const; int methods() const;
virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller);

View file

@ -22,8 +22,7 @@ std::string ProtocolWaveman::getOffCode() const {
return "$k$k$k$k$k$k$k$k$k+"; return "$k$k$k$k$k$k$k$k$k+";
} }
std::string ProtocolWaveman::decodeData(ControllerMessage& dataMsg) std::string ProtocolWaveman::decodeData(ControllerMessage& dataMsg) {
{
unsigned long allData = 0; unsigned long allData = 0;
unsigned int house = 0; unsigned int house = 0;
unsigned int unit = 0; unsigned int unit = 0;
@ -40,30 +39,30 @@ std::string ProtocolWaveman::decodeData(ControllerMessage& dataMsg)
house = allData & 0xF; house = allData & 0xF;
if(house < 0 || house > 16 || unit < 1 || unit > 16){ if(house < 0 || house > 16 || unit < 1 || unit > 16) {
//not waveman // not waveman
return ""; return "";
} }
house = house + 'A'; //house from A to P house = house + 'A'; //house from A to P
if(method != 6 && lastArctecCodeSwitchWasTurnOff == 1){ if(method != 6 && lastArctecCodeSwitchWasTurnOff == 1) {
lastArctecCodeSwitchWasTurnOff = 0; lastArctecCodeSwitchWasTurnOff = 0;
return ""; //probably a stray turnon or bell (perhaps: only certain time interval since last, check that it's the same house/unit... Will lose return ""; //probably a stray turnon or bell (perhaps: only certain time interval since last, check that it's the same house/unit... Will lose
//one turnon/bell, but it's better than the alternative... //one turnon/bell, but it's better than the alternative...
} }
if(method == 6){ if(method == 6) {
lastArctecCodeSwitchWasTurnOff = 1; lastArctecCodeSwitchWasTurnOff = 1;
} }
std::stringstream retString; std::stringstream retString;
retString << "class:command;protocol:waveman;model:codeswitch;house:" << char(house); retString << "class:command;protocol:waveman;model:codeswitch;house:" << char(house);
if(method == 0){ if(method == 0) {
retString << ";unit:" << unit << ";method:turnoff;"; retString << ";unit:" << unit << ";method:turnoff;";
}
else if(method == 14){ } else if(method == 14) {
retString << ";unit:" << unit << ";method:turnon;"; retString << ";unit:" << unit << ";method:turnon;";
} }
else { else {

View file

@ -171,7 +171,7 @@ std::string ProtocolX10::decodeData(ControllerMessage& dataMsg) {
retString << "house:" << (char)('A' + intHouse); retString << "house:" << (char)('A' + intHouse);
retString << ";unit:" << unit+1; retString << ";unit:" << unit+1;
retString << ";method:"; retString << ";method:";
if(method == 0){ if(method == 0) {
retString << "turnon;"; retString << "turnon;";
} else { } else {
retString << "turnoff;"; retString << "turnoff;";

View file

@ -11,8 +11,7 @@
#include "service/Protocol.h" #include "service/Protocol.h"
#include "service/ControllerMessage.h" #include "service/ControllerMessage.h"
class ProtocolX10 : public Protocol class ProtocolX10 : public Protocol {
{
public: public:
int methods() const; int methods() const;
virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller);

View file

@ -10,8 +10,7 @@
#include <string> #include <string>
#include "service/ProtocolSartano.h" #include "service/ProtocolSartano.h"
class ProtocolYidong : public ProtocolSartano class ProtocolYidong : public ProtocolSartano {
{
public: public:
virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller);
}; };

View file

@ -18,8 +18,7 @@ public:
}; };
Sensor::Sensor(const std::wstring &protocol, const std::wstring &model, int id) Sensor::Sensor(const std::wstring &protocol, const std::wstring &model, int id)
:Mutex() :Mutex() {
{
d = new PrivateData; d = new PrivateData;
d->protocol = protocol; d->protocol = protocol;
d->model = model; d->model = model;

View file

@ -10,8 +10,7 @@
#include "common/Mutex.h" #include "common/Mutex.h"
#include <string> #include <string>
class Sensor : public TelldusCore::Mutex class Sensor : public TelldusCore::Mutex {
{
public: public:
Sensor(const std::wstring &protocol, const std::wstring &model, int id); Sensor(const std::wstring &protocol, const std::wstring &model, int id);
~Sensor(); ~Sensor();

View file

@ -20,7 +20,7 @@ std::wstring Settings::getName(Node type, int intNodeId) const {
/* /*
* Set the name of the device * Set the name of the device
*/ */
int Settings::setName(Node type, int intDeviceId, const std::wstring &strNewName){ int Settings::setName(Node type, int intDeviceId, const std::wstring &strNewName) {
TelldusCore::MutexLocker locker(&mutex); TelldusCore::MutexLocker locker(&mutex);
return setStringSetting(type, intDeviceId, L"name", strNewName, false); return setStringSetting(type, intDeviceId, L"name", strNewName, false);
} }
@ -36,7 +36,7 @@ std::wstring Settings::getProtocol(int intDeviceId) const {
/* /*
* Set the device vendor * Set the device vendor
*/ */
int Settings::setProtocol(int intDeviceId, const std::wstring &strVendor){ int Settings::setProtocol(int intDeviceId, const std::wstring &strVendor) {
TelldusCore::MutexLocker locker(&mutex); TelldusCore::MutexLocker locker(&mutex);
return setStringSetting(Device, intDeviceId, L"protocol", strVendor, false); return setStringSetting(Device, intDeviceId, L"protocol", strVendor, false);
} }
@ -52,7 +52,7 @@ std::wstring Settings::getModel(int intDeviceId) const {
/* /*
* Set the device model * Set the device model
*/ */
int Settings::setModel(int intDeviceId, const std::wstring &strModel){ int Settings::setModel(int intDeviceId, const std::wstring &strModel) {
TelldusCore::MutexLocker locker(&mutex); TelldusCore::MutexLocker locker(&mutex);
return setStringSetting(Device, intDeviceId, L"model", strModel, false); return setStringSetting(Device, intDeviceId, L"model", strModel, false);
} }
@ -60,7 +60,7 @@ int Settings::setModel(int intDeviceId, const std::wstring &strModel){
/* /*
* Set device argument * Set device argument
*/ */
int Settings::setDeviceParameter(int intDeviceId, const std::wstring &strName, const std::wstring &strValue){ int Settings::setDeviceParameter(int intDeviceId, const std::wstring &strName, const std::wstring &strValue) {
TelldusCore::MutexLocker locker(&mutex); TelldusCore::MutexLocker locker(&mutex);
return setStringSetting(Device, intDeviceId, strName, strValue, true); return setStringSetting(Device, intDeviceId, strName, strValue, true);
} }
@ -76,7 +76,7 @@ std::wstring Settings::getDeviceParameter(int intDeviceId, const std::wstring &s
/* /*
* Set preferred controller id * Set preferred controller id
*/ */
int Settings::setPreferredControllerId(int intDeviceId, int value){ int Settings::setPreferredControllerId(int intDeviceId, int value) {
TelldusCore::MutexLocker locker(&mutex); TelldusCore::MutexLocker locker(&mutex);
return setIntSetting(Device, intDeviceId, L"controller", value, false); return setIntSetting(Device, intDeviceId, L"controller", value, false);
} }

View file

@ -32,8 +32,7 @@ const char* VAR_CONFIG_FILE = VAR_CONFIG_PATH "/telldus-core.conf";
/* /*
* Constructor * Constructor
*/ */
Settings::Settings(void) Settings::Settings(void) {
{
TelldusCore::MutexLocker locker(&mutex); TelldusCore::MutexLocker locker(&mutex);
d = new PrivateData; d = new PrivateData;
readConfig(&d->cfg); readConfig(&d->cfg);
@ -43,8 +42,7 @@ Settings::Settings(void)
/* /*
* Destructor * Destructor
*/ */
Settings::~Settings(void) Settings::~Settings(void) {
{
TelldusCore::MutexLocker locker(&mutex); TelldusCore::MutexLocker locker(&mutex);
if (d->cfg > 0) { if (d->cfg > 0) {
cfg_free(d->cfg); cfg_free(d->cfg);
@ -100,7 +98,7 @@ int Settings::getNodeId(Node type, int intDeviceIndex) const {
/* /*
* Add a new node * Add a new node
*/ */
int Settings::addNode(Node type){ int Settings::addNode(Node type) {
TelldusCore::MutexLocker locker(&mutex); TelldusCore::MutexLocker locker(&mutex);
int intNodeId = getNextNodeId(type); int intNodeId = getNextNodeId(type);
@ -148,7 +146,7 @@ int Settings::getNextNodeId(Node type) const {
/* /*
* Remove a device * Remove a device
*/ */
int Settings::removeNode(Node type, int intNodeId){ int Settings::removeNode(Node type, int intNodeId) {
TelldusCore::MutexLocker locker(&mutex); TelldusCore::MutexLocker locker(&mutex);
FILE *fp = fopen(CONFIG_FILE, "w"); FILE *fp = fopen(CONFIG_FILE, "w");
if (!fp) { if (!fp) {
@ -199,7 +197,7 @@ bool Settings::setDeviceState( int intDeviceId, int intDeviceState, const std::w
cfg_setstr(cfg_device, "stateValue", TelldusCore::wideToString(strDeviceStateValue).c_str()); cfg_setstr(cfg_device, "stateValue", TelldusCore::wideToString(strDeviceStateValue).c_str());
FILE *fp = fopen(VAR_CONFIG_FILE, "w"); FILE *fp = fopen(VAR_CONFIG_FILE, "w");
if(fp == 0){ if(fp == 0) {
return false; return false;
} }
cfg_print(d->var_cfg, fp); cfg_print(d->var_cfg, fp);

View file

@ -40,8 +40,7 @@ public:
}; };
TellStick::TellStick(int controllerId, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent, const TellStickDescriptor &td ) TellStick::TellStick(int controllerId, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent, const TellStickDescriptor &td )
:Controller(controllerId, event, updateEvent) :Controller(controllerId, event, updateEvent) {
{
d = new PrivateData; d = new PrivateData;
d->open = false; d->open = false;
d->vid = td.vid; d->vid = td.vid;
@ -140,10 +139,10 @@ void TellStick::processData( const std::string &data ) {
} }
} }
int TellStick::reset(){ int TellStick::reset() {
int success = ftdi_usb_reset( &d->ftHandle ); int success = ftdi_usb_reset( &d->ftHandle );
if(success < 0){ if(success < 0) {
return TELLSTICK_ERROR_UNKNOWN; //-1 = FTDI reset failed, -2 = USB device unavailable return TELLSTICK_ERROR_UNKNOWN; // -1 = FTDI reset failed, -2 = USB device unavailable
} }
return TELLSTICK_SUCCESS; return TELLSTICK_SUCCESS;
} }
@ -209,18 +208,18 @@ int TellStick::send( const std::string &strMessage ) {
delete[] tempMessage; delete[] tempMessage;
if(!c){ if(!c) {
Log::debug("Broken pipe on send"); Log::debug("Broken pipe on send");
return TELLSTICK_ERROR_BROKEN_PIPE; return TELLSTICK_ERROR_BROKEN_PIPE;
} }
if(strMessage.compare("N+") == 0 && ((pid() == 0x0C31 && firmwareVersion() < 5) || (pid() == 0x0C30 && firmwareVersion() < 6))){ if(strMessage.compare("N+") == 0 && ((pid() == 0x0C31 && firmwareVersion() < 5) || (pid() == 0x0C30 && firmwareVersion() < 6))) {
//these firmware versions doesn't implement ack to noop, just check that the noop can be sent correctly // these firmware versions doesn't implement ack to noop, just check that the noop can be sent correctly
return TELLSTICK_SUCCESS; return TELLSTICK_SUCCESS;
} }
if(d->ignoreControllerConfirmation){ if(d->ignoreControllerConfirmation) {
//allow TellStick to finish its air-sending // allow TellStick to finish its air-sending
msleep(1000); msleep(1000);
return TELLSTICK_SUCCESS; return TELLSTICK_SUCCESS;
} }

View file

@ -25,8 +25,7 @@ public:
TelldusCore::EventRef stopEvent, controllerChangeEvent; TelldusCore::EventRef stopEvent, controllerChangeEvent;
}; };
TelldusMain::TelldusMain(void) TelldusMain::TelldusMain(void) {
{
d = new PrivateData; d = new PrivateData;
d->stopEvent = d->eventHandler.addEvent(); d->stopEvent = d->eventHandler.addEvent();
d->controllerChangeEvent = d->eventHandler.addEvent(); d->controllerChangeEvent = d->eventHandler.addEvent();
@ -121,8 +120,8 @@ void TelldusMain::start(void) {
if (handlerEvent->isSignaled()) { if (handlerEvent->isSignaled()) {
handlerEvent->popSignal(); handlerEvent->popSignal();
for ( std::list<ClientCommunicationHandler *>::iterator it = clientCommunicationHandlerList.begin(); it != clientCommunicationHandlerList.end(); ){ for ( std::list<ClientCommunicationHandler *>::iterator it = clientCommunicationHandlerList.begin(); it != clientCommunicationHandlerList.end(); ) {
if ((*it)->isDone()){ if ((*it)->isDone()) {
delete *it; delete *it;
it = clientCommunicationHandlerList.erase(it); it = clientCommunicationHandlerList.erase(it);
@ -143,6 +142,6 @@ void TelldusMain::start(void) {
supervisor.stop(); supervisor.stop();
} }
void TelldusMain::stop(void){ void TelldusMain::stop(void) {
d->stopEvent->signal(); d->stopEvent->signal();
} }

View file

@ -7,8 +7,7 @@
#ifndef TELLDUS_CORE_SERVICE_TELLDUSMAIN_H_ #ifndef TELLDUS_CORE_SERVICE_TELLDUSMAIN_H_
#define TELLDUS_CORE_SERVICE_TELLDUSMAIN_H_ #define TELLDUS_CORE_SERVICE_TELLDUSMAIN_H_
class TelldusMain class TelldusMain {
{
public: public:
TelldusMain(void); TelldusMain(void);
~TelldusMain(void); ~TelldusMain(void);

View file

@ -28,8 +28,7 @@ public:
}; };
Timer::Timer(TelldusCore::EventRef event) Timer::Timer(TelldusCore::EventRef event)
:TelldusCore::Thread(), d(new PrivateData) :TelldusCore::Thread(), d(new PrivateData) {
{
d->event = event; d->event = event;
#ifdef _WINDOWS #ifdef _WINDOWS
d->cond = CreateEventW(NULL, false, false, NULL); d->cond = CreateEventW(NULL, false, false, NULL);