commit
bdd95b8d78
52 changed files with 1524 additions and 737 deletions
|
@ -11,83 +11,61 @@
|
|||
|
||||
using namespace TelldusCore;
|
||||
|
||||
TDDeviceEventDispatcher::TDDeviceEventDispatcher(CallbackStruct<TDDeviceEvent> *data, int id, int m, const std::string &strD, TelldusCore::EventRef cbDone)
|
||||
:Thread(), d(data), deviceId(id), method(m), strData(strD), doneRunning(false), callbackExecuted(cbDone)
|
||||
TDEventDispatcher::TDEventDispatcher(EventDataRef cbd, CallbackStruct *cb, EventRef cbDone)
|
||||
:Thread(), doneRunning(false), callbackData(cbd), callback(cb), callbackExecuted(cbDone)
|
||||
{
|
||||
this->startAndLock(&d->mutex);
|
||||
this->startAndLock(&callback->mutex);
|
||||
}
|
||||
|
||||
TDDeviceEventDispatcher::~TDDeviceEventDispatcher() {
|
||||
TDEventDispatcher::~TDEventDispatcher() {
|
||||
this->wait();
|
||||
}
|
||||
|
||||
bool TDDeviceEventDispatcher::done() const {
|
||||
bool TDEventDispatcher::done() const {
|
||||
return doneRunning;
|
||||
}
|
||||
|
||||
void TDDeviceEventDispatcher::run() {
|
||||
char *str = wrapStdString(strData);
|
||||
d->event(deviceId, method, strData.c_str(), d->id, d->context);
|
||||
void TDEventDispatcher::run() {
|
||||
this->fireEvent();
|
||||
doneRunning = true;
|
||||
callbackExecuted->signal();
|
||||
}
|
||||
|
||||
TDDeviceChangeEventDispatcher::TDDeviceChangeEventDispatcher(CallbackStruct<TDDeviceChangeEvent> *data, int id, int event, int type, TelldusCore::EventRef cbDone)
|
||||
:Thread(), d(data), deviceId(id), changeEvent(event), changeType(type), doneRunning(false), callbackExecuted(cbDone)
|
||||
{
|
||||
this->startAndLock(&d->mutex);
|
||||
}
|
||||
void TDEventDispatcher::fireEvent() {
|
||||
if (callback->type == CallbackStruct::DeviceEvent) {
|
||||
DeviceEventCallbackData *data = dynamic_cast<DeviceEventCallbackData *>(callbackData.get());
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
((TDDeviceEvent)callback->event)(data->deviceId, data->deviceState, data->deviceStateValue.c_str(), callback->id, callback->context);
|
||||
|
||||
TDDeviceChangeEventDispatcher::~TDDeviceChangeEventDispatcher() {
|
||||
this->wait();
|
||||
}
|
||||
} else if (callback->type == CallbackStruct::DeviceChangeEvent) {
|
||||
DeviceChangeEventCallbackData *data = dynamic_cast<DeviceChangeEventCallbackData *>(callbackData.get());
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
((TDDeviceChangeEvent)callback->event)(data->deviceId, data->changeEvent, data->changeType, callback->id, callback->context);
|
||||
|
||||
bool TDDeviceChangeEventDispatcher::done() const {
|
||||
return doneRunning;
|
||||
}
|
||||
} else if (callback->type == CallbackStruct::RawDeviceEvent) {
|
||||
RawDeviceEventCallbackData *data = dynamic_cast<RawDeviceEventCallbackData *>(callbackData.get());
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
((TDRawDeviceEvent)callback->event)(data->data.c_str(), data->controllerId, callback->id, callback->context);
|
||||
|
||||
void TDDeviceChangeEventDispatcher::run() {
|
||||
d->event(deviceId, changeEvent, changeType, d->id, d->context);
|
||||
doneRunning = true;
|
||||
callbackExecuted->signal();
|
||||
}
|
||||
} else if (callback->type == CallbackStruct::SensorEvent) {
|
||||
SensorEventCallbackData *data = dynamic_cast<SensorEventCallbackData *>(callbackData.get());
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
((TDSensorEvent)callback->event)(data->protocol.c_str(), data->model.c_str(), data->id, data->dataType, data->value.c_str(), data->timestamp, callback->id, callback->context);
|
||||
|
||||
TDRawDeviceEventDispatcher::TDRawDeviceEventDispatcher( CallbackStruct<TDRawDeviceEvent> *data, const std::string &strD, int id, TelldusCore::EventRef cbDone)
|
||||
:Thread(), d(data), controllerId(id), strData(strD), doneRunning(false), callbackExecuted(cbDone)
|
||||
{
|
||||
this->startAndLock(&d->mutex);
|
||||
}
|
||||
} else if (callback->type == CallbackStruct::ControllerEvent) {
|
||||
ControllerEventCallbackData *data = dynamic_cast<ControllerEventCallbackData *>(callbackData.get());
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
((TDControllerEvent)callback->event)(data->controllerId, data->changeEvent, data->changeType, data->newValue.c_str(), callback->id, callback->context);
|
||||
|
||||
TDRawDeviceEventDispatcher::~TDRawDeviceEventDispatcher() {
|
||||
this->wait();
|
||||
}
|
||||
|
||||
bool TDRawDeviceEventDispatcher::done() const {
|
||||
return doneRunning;
|
||||
}
|
||||
|
||||
void TDRawDeviceEventDispatcher::run() {
|
||||
d->event(strData.c_str(), controllerId, d->id, d->context);
|
||||
doneRunning = true;
|
||||
callbackExecuted->signal();
|
||||
}
|
||||
|
||||
TDSensorEventDispatcher::TDSensorEventDispatcher( CallbackStruct<TDSensorEvent> *data, const std::string &p, const std::string &m, int id, int type, const std::string &v, int t, TelldusCore::EventRef cbDone)
|
||||
:Thread(), d(data), protocol(p), model(m), sensorId(id), dataType(type), value(v), timestamp(t), doneRunning(false), callbackExecuted(cbDone)
|
||||
{
|
||||
this->startAndLock(&d->mutex);
|
||||
}
|
||||
|
||||
TDSensorEventDispatcher::~TDSensorEventDispatcher() {
|
||||
this->wait();
|
||||
}
|
||||
|
||||
bool TDSensorEventDispatcher::done() const {
|
||||
return doneRunning;
|
||||
}
|
||||
|
||||
void TDSensorEventDispatcher::run() {
|
||||
d->event(protocol.c_str(), model.c_str(), sensorId, dataType, value.c_str(), timestamp, d->id, d->context);
|
||||
doneRunning = true;
|
||||
callbackExecuted->signal();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,71 +17,85 @@
|
|||
#include "telldus-core.h"
|
||||
|
||||
namespace TelldusCore {
|
||||
template <typename T> struct CallbackStruct {
|
||||
|
||||
/*template <typename T> struct CallbackStruct {
|
||||
T event;
|
||||
int id;
|
||||
void *context;
|
||||
TelldusCore::Mutex mutex;
|
||||
};*/
|
||||
struct CallbackStruct {
|
||||
enum CallbackType { DeviceEvent, DeviceChangeEvent, RawDeviceEvent, SensorEvent, ControllerEvent };
|
||||
CallbackType type;
|
||||
void *event;
|
||||
int id;
|
||||
void *context;
|
||||
TelldusCore::Mutex mutex;
|
||||
};
|
||||
/*typedef CallbackStruct<TDDeviceChangeEvent> DeviceChangeEvent;
|
||||
typedef CallbackStruct<TDRawDeviceEvent> RawDeviceEvent;
|
||||
typedef CallbackStruct<TDSensorEvent> SensorEvent;*/
|
||||
|
||||
class CallbackData: public EventDataBase {
|
||||
public:
|
||||
explicit CallbackData(CallbackStruct::CallbackType t) : EventDataBase(), type(t) {}
|
||||
CallbackStruct::CallbackType type;
|
||||
};
|
||||
|
||||
class TDDeviceEventDispatcher : public Thread {
|
||||
class DeviceEventCallbackData : public CallbackData {
|
||||
public:
|
||||
TDDeviceEventDispatcher(CallbackStruct<TDDeviceEvent> *data, int deviceId, int method, const std::string &strData, TelldusCore::EventRef cbDone);
|
||||
virtual ~TDDeviceEventDispatcher();
|
||||
bool done() const;
|
||||
protected:
|
||||
virtual void run();
|
||||
bool doneRunning;
|
||||
private:
|
||||
CallbackStruct<TDDeviceEvent> *d;
|
||||
int deviceId, method;
|
||||
std::string strData;
|
||||
TelldusCore::EventRef callbackExecuted;
|
||||
DeviceEventCallbackData() : CallbackData(CallbackStruct::DeviceEvent) {}
|
||||
int deviceId;
|
||||
int deviceState;
|
||||
std::string deviceStateValue;
|
||||
};
|
||||
class TDDeviceChangeEventDispatcher : public Thread {
|
||||
class DeviceChangeEventCallbackData : public CallbackData {
|
||||
public:
|
||||
TDDeviceChangeEventDispatcher(CallbackStruct<TDDeviceChangeEvent> *data, int deviceId, int changeEvent, int changeType, TelldusCore::EventRef cbDone);
|
||||
virtual ~TDDeviceChangeEventDispatcher();
|
||||
bool done() const;
|
||||
protected:
|
||||
virtual void run();
|
||||
bool doneRunning;
|
||||
private:
|
||||
CallbackStruct<TDDeviceChangeEvent> *d;
|
||||
int deviceId, changeEvent, changeType;
|
||||
TelldusCore::EventRef callbackExecuted;
|
||||
DeviceChangeEventCallbackData() : CallbackData(CallbackStruct::DeviceChangeEvent) {}
|
||||
int deviceId;
|
||||
int changeEvent;
|
||||
int changeType;
|
||||
};
|
||||
class TDRawDeviceEventDispatcher : public Thread {
|
||||
|
||||
class RawDeviceEventCallbackData : public CallbackData {
|
||||
public:
|
||||
TDRawDeviceEventDispatcher( CallbackStruct<TDRawDeviceEvent> *data, const std::string &strData, int controllerId, TelldusCore::EventRef cbDone);
|
||||
virtual ~TDRawDeviceEventDispatcher();
|
||||
bool done() const;
|
||||
protected:
|
||||
virtual void run();
|
||||
bool doneRunning;
|
||||
private:
|
||||
CallbackStruct<TDRawDeviceEvent> *d;
|
||||
RawDeviceEventCallbackData() : CallbackData(CallbackStruct::RawDeviceEvent) {}
|
||||
std::string data;
|
||||
int controllerId;
|
||||
std::string strData;
|
||||
TelldusCore::EventRef callbackExecuted;
|
||||
};
|
||||
class TDSensorEventDispatcher : public Thread {
|
||||
|
||||
class SensorEventCallbackData : public CallbackData {
|
||||
public:
|
||||
TDSensorEventDispatcher( CallbackStruct<TDSensorEvent> *data, const std::string &protocol, const std::string &model, int id, int dataType, const std::string &value, int timestamp, TelldusCore::EventRef cbDone);
|
||||
virtual ~TDSensorEventDispatcher();
|
||||
bool done() const;
|
||||
protected:
|
||||
virtual void run();
|
||||
bool doneRunning;
|
||||
private:
|
||||
CallbackStruct<TDSensorEvent> *d;
|
||||
SensorEventCallbackData() : CallbackData(CallbackStruct::SensorEvent) {}
|
||||
std::string protocol;
|
||||
std::string model;
|
||||
int sensorId;
|
||||
int id;
|
||||
int dataType;
|
||||
std::string value;
|
||||
int timestamp;
|
||||
TelldusCore::EventRef callbackExecuted;
|
||||
};
|
||||
class ControllerEventCallbackData : public CallbackData {
|
||||
public:
|
||||
ControllerEventCallbackData() : CallbackData(CallbackStruct::ControllerEvent) {}
|
||||
int controllerId;
|
||||
int changeEvent;
|
||||
int changeType;
|
||||
std::string newValue;
|
||||
};
|
||||
|
||||
class TDEventDispatcher : public Thread {
|
||||
public:
|
||||
TDEventDispatcher(EventDataRef callbackData, CallbackStruct *callback, TelldusCore::EventRef cbDone);
|
||||
virtual ~TDEventDispatcher();
|
||||
bool done() const;
|
||||
protected:
|
||||
virtual void run();
|
||||
bool doneRunning;
|
||||
private:
|
||||
void fireEvent();
|
||||
EventDataRef callbackData;
|
||||
CallbackStruct *callback;
|
||||
EventRef callbackExecuted;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -13,17 +13,19 @@
|
|||
|
||||
using namespace TelldusCore;
|
||||
|
||||
typedef std::list<CallbackStruct *> CallbackList;
|
||||
|
||||
class CallbackMainDispatcher::PrivateData {
|
||||
public:
|
||||
EventHandler eventHandler;
|
||||
EventRef stopEvent, generalCallbackEvent, janitor;
|
||||
|
||||
std::list<std::tr1::shared_ptr<TDDeviceEventDispatcher> > deviceEventThreadList;
|
||||
std::list<std::tr1::shared_ptr<TDDeviceChangeEventDispatcher> > deviceChangeEventThreadList;
|
||||
std::list<std::tr1::shared_ptr<TDRawDeviceEventDispatcher> > rawDeviceEventThreadList;
|
||||
std::list<std::tr1::shared_ptr<TDSensorEventDispatcher> > sensorEventThreadList;
|
||||
|
||||
Mutex mutex;
|
||||
std::list<std::tr1::shared_ptr<TelldusCore::TDEventDispatcher> > eventThreadList;
|
||||
|
||||
CallbackList callbackList;
|
||||
|
||||
int lastCallbackId;
|
||||
};
|
||||
|
||||
CallbackMainDispatcher::CallbackMainDispatcher()
|
||||
|
@ -33,6 +35,8 @@ CallbackMainDispatcher::CallbackMainDispatcher()
|
|||
d->stopEvent = d->eventHandler.addEvent();
|
||||
d->generalCallbackEvent = d->eventHandler.addEvent();
|
||||
d->janitor = d->eventHandler.addEvent(); //Used for cleanups
|
||||
|
||||
d->lastCallbackId = 0;
|
||||
}
|
||||
|
||||
CallbackMainDispatcher::~CallbackMainDispatcher(void){
|
||||
|
@ -48,6 +52,42 @@ EventRef CallbackMainDispatcher::retrieveCallbackEvent(){
|
|||
return d->generalCallbackEvent;
|
||||
}
|
||||
|
||||
int CallbackMainDispatcher::registerCallback(CallbackStruct::CallbackType type, void *eventFunction, void *context) {
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
int id = ++d->lastCallbackId;
|
||||
CallbackStruct *callback = new CallbackStruct;
|
||||
callback->type = type;
|
||||
callback->event = eventFunction;
|
||||
callback->id = id;
|
||||
callback->context = context;
|
||||
d->callbackList.push_back(callback);
|
||||
return id;
|
||||
}
|
||||
|
||||
bool CallbackMainDispatcher::unregisterCallback(int callbackId) {
|
||||
CallbackList newEventList;
|
||||
{
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
for(CallbackList::iterator callback_it = d->callbackList.begin(); callback_it != d->callbackList.end(); ++callback_it) {
|
||||
if ( (*callback_it)->id != callbackId ) {
|
||||
continue;
|
||||
}
|
||||
newEventList.splice(newEventList.begin(), d->callbackList, callback_it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (newEventList.size()) {
|
||||
CallbackList::iterator it = newEventList.begin();
|
||||
{ //Lock and unlock to make sure no one else uses the object
|
||||
TelldusCore::MutexLocker locker( &(*it)->mutex );
|
||||
}
|
||||
delete (*it);
|
||||
newEventList.erase(it);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CallbackMainDispatcher::run(){
|
||||
|
||||
while(!d->stopEvent->isSignaled()){
|
||||
|
@ -57,37 +97,17 @@ void CallbackMainDispatcher::run(){
|
|||
|
||||
if(d->generalCallbackEvent->isSignaled()){
|
||||
EventDataRef eventData = d->generalCallbackEvent->takeSignal();
|
||||
|
||||
DeviceEventCallbackData *decd = dynamic_cast<DeviceEventCallbackData*>(eventData.get());
|
||||
if(decd){
|
||||
std::tr1::shared_ptr<TDDeviceEventDispatcher> ptr(new TDDeviceEventDispatcher(decd->data, decd->deviceId, decd->deviceState, decd->deviceStateValue, d->janitor));
|
||||
MutexLocker locker(&d->mutex);
|
||||
d->deviceEventThreadList.push_back(ptr);
|
||||
continue;
|
||||
}
|
||||
|
||||
DeviceChangeEventCallbackData *dcecd = dynamic_cast<DeviceChangeEventCallbackData*>(eventData.get());
|
||||
if(dcecd){
|
||||
std::tr1::shared_ptr<TDDeviceChangeEventDispatcher> ptr(new TDDeviceChangeEventDispatcher(dcecd->data, dcecd->deviceId, dcecd->eventDeviceChanges, dcecd->eventChangeType, d->janitor));
|
||||
MutexLocker locker(&d->mutex);
|
||||
d->deviceChangeEventThreadList.push_back(ptr);
|
||||
CallbackData *cbd = dynamic_cast<CallbackData *>(eventData.get());
|
||||
if (!cbd) {
|
||||
continue;
|
||||
}
|
||||
|
||||
RawDeviceEventCallbackData *rdecd = dynamic_cast<RawDeviceEventCallbackData*>(eventData.get());
|
||||
if(rdecd){
|
||||
std::tr1::shared_ptr<TDRawDeviceEventDispatcher> ptr(new TDRawDeviceEventDispatcher(rdecd->data, rdecd->command, rdecd->controllerId, d->janitor));
|
||||
MutexLocker locker(&d->mutex);
|
||||
d->rawDeviceEventThreadList.push_back(ptr);
|
||||
continue;
|
||||
}
|
||||
|
||||
SensorEventCallbackData *secd = dynamic_cast<SensorEventCallbackData*>(eventData.get());
|
||||
if(secd){
|
||||
std::tr1::shared_ptr<TDSensorEventDispatcher> ptr(new TDSensorEventDispatcher(secd->data, secd->protocol, secd->model, secd->id, secd->dataType, secd->value, secd->timestamp, d->janitor));
|
||||
MutexLocker locker(&d->mutex);
|
||||
d->sensorEventThreadList.push_back(ptr);
|
||||
continue;
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
for(CallbackList::iterator callback_it = d->callbackList.begin(); callback_it != d->callbackList.end(); ++callback_it) {
|
||||
if ( (*callback_it)->type == cbd->type ) {
|
||||
std::tr1::shared_ptr<TelldusCore::TDEventDispatcher> ptr(new TelldusCore::TDEventDispatcher(eventData, *callback_it, d->janitor));
|
||||
d->eventThreadList.push_back(ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (d->janitor->isSignaled()) {
|
||||
|
@ -107,55 +127,13 @@ void CallbackMainDispatcher::cleanupCallbacks() {
|
|||
do {
|
||||
again = false;
|
||||
MutexLocker locker(&d->mutex);
|
||||
std::list<std::tr1::shared_ptr<TDDeviceEventDispatcher> >::iterator it = d->deviceEventThreadList.begin();
|
||||
for (;it != d->deviceEventThreadList.end(); ++it) {
|
||||
std::list<std::tr1::shared_ptr<TDEventDispatcher> >::iterator it = d->eventThreadList.begin();
|
||||
for (;it != d->eventThreadList.end(); ++it) {
|
||||
if ((*it)->done()) {
|
||||
d->deviceEventThreadList.erase(it);
|
||||
d->eventThreadList.erase(it);
|
||||
again = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (again);
|
||||
|
||||
//Device Change Event
|
||||
do {
|
||||
again = false;
|
||||
MutexLocker locker(&d->mutex);
|
||||
std::list<std::tr1::shared_ptr<TDDeviceChangeEventDispatcher> >::iterator it = d->deviceChangeEventThreadList.begin();
|
||||
for (;it != d->deviceChangeEventThreadList.end(); ++it) {
|
||||
if ((*it)->done()) {
|
||||
d->deviceChangeEventThreadList.erase(it);
|
||||
again = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (again);
|
||||
|
||||
//Raw Device Event
|
||||
do {
|
||||
again = false;
|
||||
MutexLocker locker(&d->mutex);
|
||||
std::list<std::tr1::shared_ptr<TDRawDeviceEventDispatcher> >::iterator it = d->rawDeviceEventThreadList.begin();
|
||||
for (;it != d->rawDeviceEventThreadList.end(); ++it) {
|
||||
if ((*it)->done()) {
|
||||
d->rawDeviceEventThreadList.erase(it);
|
||||
again = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (again);
|
||||
|
||||
//Sensor Event
|
||||
do {
|
||||
again = false;
|
||||
MutexLocker locker(&d->mutex);
|
||||
std::list<std::tr1::shared_ptr<TDSensorEventDispatcher> >::iterator it = d->sensorEventThreadList.begin();
|
||||
for (;it != d->sensorEventThreadList.end(); ++it) {
|
||||
if ((*it)->done()) {
|
||||
d->sensorEventThreadList.erase(it);
|
||||
again = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (again);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,39 +16,6 @@
|
|||
#include "EventHandler.h"
|
||||
|
||||
namespace TelldusCore {
|
||||
class DeviceChangeEventCallbackData : public EventDataBase {
|
||||
public:
|
||||
CallbackStruct<TDDeviceChangeEvent> *data;
|
||||
int deviceId;
|
||||
int eventDeviceChanges;
|
||||
int eventChangeType;
|
||||
};
|
||||
|
||||
class DeviceEventCallbackData : public EventDataBase {
|
||||
public:
|
||||
CallbackStruct<TDDeviceEvent> *data;
|
||||
int deviceId;
|
||||
int deviceState;
|
||||
std::string deviceStateValue;
|
||||
};
|
||||
|
||||
class RawDeviceEventCallbackData : public EventDataBase {
|
||||
public:
|
||||
CallbackStruct<TDRawDeviceEvent> *data;
|
||||
int controllerId;
|
||||
std::string command;
|
||||
};
|
||||
|
||||
class SensorEventCallbackData : public EventDataBase {
|
||||
public:
|
||||
CallbackStruct<TDSensorEvent> *data;
|
||||
std::string protocol;
|
||||
std::string model;
|
||||
int id;
|
||||
int dataType;
|
||||
std::string value;
|
||||
int timestamp;
|
||||
};
|
||||
|
||||
class CallbackMainDispatcher : public Thread
|
||||
{
|
||||
|
@ -57,7 +24,10 @@ namespace TelldusCore {
|
|||
~CallbackMainDispatcher(void);
|
||||
|
||||
EventRef retrieveCallbackEvent();
|
||||
|
||||
|
||||
int registerCallback( TelldusCore::CallbackStruct::CallbackType type, void *eventFunction, void *context );
|
||||
bool unregisterCallback( int callbackId );
|
||||
|
||||
protected:
|
||||
void run();
|
||||
|
||||
|
|
|
@ -9,25 +9,11 @@
|
|||
|
||||
using namespace TelldusCore;
|
||||
|
||||
typedef CallbackStruct<TDDeviceEvent> DeviceEvent;
|
||||
typedef CallbackStruct<TDDeviceChangeEvent> DeviceChangeEvent;
|
||||
typedef CallbackStruct<TDRawDeviceEvent> RawDeviceEvent;
|
||||
typedef CallbackStruct<TDSensorEvent> SensorEvent;
|
||||
typedef std::list<DeviceEvent *> DeviceEventList;
|
||||
typedef std::list<DeviceChangeEvent *> DeviceChangeList;
|
||||
typedef std::list<RawDeviceEvent *> RawDeviceEventList;
|
||||
typedef std::list<SensorEvent *> SensorEventList;
|
||||
|
||||
class Client::PrivateData {
|
||||
public:
|
||||
int lastCallbackId;
|
||||
DeviceEventList deviceEventList;
|
||||
DeviceChangeList deviceChangeEventList;
|
||||
Socket eventSocket;
|
||||
RawDeviceEventList rawDeviceEventList;
|
||||
SensorEventList sensorEventList;
|
||||
bool running, sensorCached;
|
||||
std::wstring sensorCache;
|
||||
bool running, sensorCached, controllerCached;
|
||||
std::wstring sensorCache, controllerCache;
|
||||
TelldusCore::Mutex mutex;
|
||||
CallbackMainDispatcher callbackMainDispatcher;
|
||||
|
||||
|
@ -39,9 +25,9 @@ Client::Client()
|
|||
: Thread()
|
||||
{
|
||||
d = new PrivateData;
|
||||
d->lastCallbackId = 0;
|
||||
d->running = true;
|
||||
d->sensorCached = false;
|
||||
d->controllerCached = false;
|
||||
d->callbackMainDispatcher.start();
|
||||
start();
|
||||
}
|
||||
|
@ -69,63 +55,6 @@ Client *Client::getInstance() {
|
|||
return Client::instance;
|
||||
}
|
||||
|
||||
void Client::callbackDeviceEvent(int deviceId, int deviceState, const std::wstring &deviceStateValue){
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
for(DeviceEventList::iterator callback_it = d->deviceEventList.begin(); callback_it != d->deviceEventList.end(); ++callback_it) {
|
||||
DeviceEventCallbackData *deviceEventCallbackData = new DeviceEventCallbackData();
|
||||
deviceEventCallbackData->data = *callback_it;
|
||||
deviceEventCallbackData->deviceId = deviceId;
|
||||
deviceEventCallbackData->deviceState = deviceState;
|
||||
deviceEventCallbackData->deviceStateValue = TelldusCore::wideToString(deviceStateValue);
|
||||
|
||||
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(deviceEventCallbackData);
|
||||
}
|
||||
}
|
||||
|
||||
void Client::callbackDeviceChangeEvent(int deviceId, int eventDeviceChanges, int eventChangeType){
|
||||
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
for(DeviceChangeList::iterator callback_it = d->deviceChangeEventList.begin(); callback_it != d->deviceChangeEventList.end(); ++callback_it) {
|
||||
DeviceChangeEventCallbackData *deviceChangeEventCallbackData = new DeviceChangeEventCallbackData();
|
||||
deviceChangeEventCallbackData->data = *callback_it;
|
||||
deviceChangeEventCallbackData->deviceId = deviceId;
|
||||
deviceChangeEventCallbackData->eventDeviceChanges = eventDeviceChanges;
|
||||
deviceChangeEventCallbackData->eventChangeType = eventChangeType;
|
||||
|
||||
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(deviceChangeEventCallbackData);
|
||||
}
|
||||
}
|
||||
|
||||
void Client::callbackRawEvent(std::wstring command, int controllerId) {
|
||||
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
for(RawDeviceEventList::iterator callback_it = d->rawDeviceEventList.begin(); callback_it != d->rawDeviceEventList.end(); ++callback_it) {
|
||||
RawDeviceEventCallbackData *rawDeviceEventCallbackData = new RawDeviceEventCallbackData();
|
||||
rawDeviceEventCallbackData->data = *callback_it;
|
||||
rawDeviceEventCallbackData->controllerId = controllerId;
|
||||
rawDeviceEventCallbackData->command = TelldusCore::wideToString(command);
|
||||
|
||||
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(rawDeviceEventCallbackData);
|
||||
}
|
||||
}
|
||||
|
||||
void Client::callbackSensorEvent(const std::wstring &protocol, const std::wstring &model, int id, int dataType, const std::wstring &value, int timestamp) {
|
||||
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
for(SensorEventList::iterator callback_it = d->sensorEventList.begin(); callback_it != d->sensorEventList.end(); ++callback_it) {
|
||||
SensorEventCallbackData *sensorEventCallbackData = new SensorEventCallbackData();
|
||||
sensorEventCallbackData->data = *callback_it;
|
||||
sensorEventCallbackData->protocol = TelldusCore::wideToString(protocol);
|
||||
sensorEventCallbackData->model = TelldusCore::wideToString(model);
|
||||
sensorEventCallbackData->id = id;
|
||||
sensorEventCallbackData->dataType = dataType;
|
||||
sensorEventCallbackData->value = TelldusCore::wideToString(value);
|
||||
sensorEventCallbackData->timestamp = timestamp;
|
||||
|
||||
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(sensorEventCallbackData);
|
||||
}
|
||||
}
|
||||
|
||||
bool Client::getBoolFromService(const Message &msg) {
|
||||
return getIntegerFromService(msg) == TELLSTICK_SUCCESS;
|
||||
}
|
||||
|
@ -143,48 +72,8 @@ std::wstring Client::getWStringFromService(const Message &msg) {
|
|||
return Message::takeString(&response);
|
||||
}
|
||||
|
||||
int Client::registerDeviceEvent( TDDeviceEvent eventFunction, void *context ) {
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
int id = ++d->lastCallbackId;
|
||||
DeviceEvent *callback = new DeviceEvent;
|
||||
callback->event = eventFunction;
|
||||
callback->id = id;
|
||||
callback->context = context;
|
||||
d->deviceEventList.push_back(callback);
|
||||
return id;
|
||||
}
|
||||
|
||||
int Client::registerDeviceChangeEvent( TDDeviceChangeEvent eventFunction, void *context ) {
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
int id = ++d->lastCallbackId;
|
||||
DeviceChangeEvent *callback = new DeviceChangeEvent;
|
||||
callback->event = eventFunction;
|
||||
callback->id = id;
|
||||
callback->context = context;
|
||||
d->deviceChangeEventList.push_back(callback);
|
||||
return id;
|
||||
}
|
||||
|
||||
int Client::registerRawDeviceEvent( TDRawDeviceEvent eventFunction, void *context ) {
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
int id = ++d->lastCallbackId;
|
||||
RawDeviceEvent *callback = new RawDeviceEvent;
|
||||
callback->event = eventFunction;
|
||||
callback->id = id;
|
||||
callback->context = context;
|
||||
d->rawDeviceEventList.push_back(callback);
|
||||
return id;
|
||||
}
|
||||
|
||||
int Client::registerSensorEvent( TDSensorEvent eventFunction, void *context ) {
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
int id = ++d->lastCallbackId;
|
||||
SensorEvent *callback = new SensorEvent;
|
||||
callback->event = eventFunction;
|
||||
callback->id = id;
|
||||
callback->context = context;
|
||||
d->sensorEventList.push_back(callback);
|
||||
return id;
|
||||
int Client::registerEvent( CallbackStruct::CallbackType type, void *eventFunction, void *context ) {
|
||||
return d->callbackMainDispatcher.registerCallback(type, eventFunction, context );
|
||||
}
|
||||
|
||||
void Client::run(){
|
||||
|
@ -203,37 +92,49 @@ void Client::run(){
|
|||
}
|
||||
|
||||
std::wstring clientMessage = d->eventSocket.read(1000); //testing 5 second timeout
|
||||
|
||||
|
||||
while(clientMessage != L""){
|
||||
//a message arrived
|
||||
std::wstring type = Message::takeString(&clientMessage);
|
||||
if(type == L"TDDeviceChangeEvent"){
|
||||
int deviceId = Message::takeInt(&clientMessage);
|
||||
int eventDeviceChanges = Message::takeInt(&clientMessage);
|
||||
int eventChangeType = Message::takeInt(&clientMessage);
|
||||
callbackDeviceChangeEvent(deviceId, eventDeviceChanges, eventChangeType);
|
||||
}
|
||||
else if(type == L"TDDeviceEvent"){
|
||||
int deviceId = Message::takeInt(&clientMessage);
|
||||
int eventState = Message::takeInt(&clientMessage);
|
||||
std::wstring eventValue = Message::takeString(&clientMessage);
|
||||
callbackDeviceEvent(deviceId, eventState, eventValue);
|
||||
}
|
||||
else if(type == L"TDRawDeviceEvent"){
|
||||
std::wstring command = Message::takeString(&clientMessage);
|
||||
int controllerId = Message::takeInt(&clientMessage);
|
||||
callbackRawEvent(command, controllerId);
|
||||
}
|
||||
else if(type == L"TDSensorEvent"){
|
||||
std::wstring protocol = Message::takeString(&clientMessage);
|
||||
std::wstring model = Message::takeString(&clientMessage);
|
||||
int sensorId = Message::takeInt(&clientMessage);
|
||||
int dataType = Message::takeInt(&clientMessage);
|
||||
std::wstring value = Message::takeString(&clientMessage);
|
||||
int timestamp = Message::takeInt(&clientMessage);
|
||||
callbackSensorEvent(protocol, model, sensorId, dataType, value, timestamp);
|
||||
}
|
||||
else{
|
||||
DeviceChangeEventCallbackData *data = new DeviceChangeEventCallbackData();
|
||||
data->deviceId = Message::takeInt(&clientMessage);
|
||||
data->changeEvent = Message::takeInt(&clientMessage);
|
||||
data->changeType = Message::takeInt(&clientMessage);
|
||||
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data);
|
||||
|
||||
} else if(type == L"TDDeviceEvent"){
|
||||
DeviceEventCallbackData *data = new DeviceEventCallbackData();
|
||||
data->deviceId = Message::takeInt(&clientMessage);
|
||||
data->deviceState = Message::takeInt(&clientMessage);
|
||||
data->deviceStateValue = TelldusCore::wideToString(Message::takeString(&clientMessage));
|
||||
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data);
|
||||
|
||||
} else if(type == L"TDRawDeviceEvent"){
|
||||
RawDeviceEventCallbackData *data = new RawDeviceEventCallbackData();
|
||||
data->data = TelldusCore::wideToString(Message::takeString(&clientMessage));
|
||||
data->controllerId = Message::takeInt(&clientMessage);
|
||||
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data);
|
||||
|
||||
} else if(type == L"TDSensorEvent"){
|
||||
SensorEventCallbackData *data = new SensorEventCallbackData();
|
||||
data->protocol = TelldusCore::wideToString(Message::takeString(&clientMessage));
|
||||
data->model = TelldusCore::wideToString(Message::takeString(&clientMessage));
|
||||
data->id = Message::takeInt(&clientMessage);
|
||||
data->dataType = Message::takeInt(&clientMessage);
|
||||
data->value = TelldusCore::wideToString(Message::takeString(&clientMessage));
|
||||
data->timestamp = Message::takeInt(&clientMessage);
|
||||
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data);
|
||||
|
||||
} else if(type == L"TDControllerEvent") {
|
||||
ControllerEventCallbackData *data = new ControllerEventCallbackData();
|
||||
data->controllerId = Message::takeInt(&clientMessage);
|
||||
data->changeEvent = Message::takeInt(&clientMessage);
|
||||
data->changeType = Message::takeInt(&clientMessage);
|
||||
data->newValue = TelldusCore::wideToString(Message::takeString(&clientMessage));
|
||||
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data);
|
||||
|
||||
} else {
|
||||
clientMessage = L""; //cleanup, if message contained garbage/unhandled data
|
||||
}
|
||||
}
|
||||
|
@ -241,7 +142,7 @@ void Client::run(){
|
|||
}
|
||||
|
||||
std::wstring Client::sendToService(const Message &msg) {
|
||||
|
||||
|
||||
int tries = 0;
|
||||
std::wstring readData;
|
||||
while(tries < 20){
|
||||
|
@ -268,7 +169,7 @@ std::wstring Client::sendToService(const Message &msg) {
|
|||
continue; //TODO can we be really sure it SHOULD be anything?
|
||||
//TODO perhaps break here instead?
|
||||
}
|
||||
|
||||
|
||||
if (!s.isConnected()) { //Connection failed sometime during operation...
|
||||
msleep(500);
|
||||
continue; //retry
|
||||
|
@ -285,85 +186,7 @@ void Client::stopThread(){
|
|||
}
|
||||
|
||||
bool Client::unregisterCallback( int callbackId ) {
|
||||
DeviceEventList newDEList;
|
||||
{
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
for(DeviceEventList::iterator callback_it = d->deviceEventList.begin(); callback_it != d->deviceEventList.end(); ++callback_it) {
|
||||
if ( (*callback_it)->id != callbackId ) {
|
||||
continue;
|
||||
}
|
||||
newDEList.splice(newDEList.begin(), d->deviceEventList, callback_it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (newDEList.size()) {
|
||||
DeviceEventList::iterator it = newDEList.begin();
|
||||
{ //Lock and unlock to make sure no one else uses the object
|
||||
TelldusCore::MutexLocker locker( &(*it)->mutex );
|
||||
}
|
||||
delete (*it);
|
||||
newDEList.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
DeviceChangeList newDCList;
|
||||
{
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
for(DeviceChangeList::iterator callback_it = d->deviceChangeEventList.begin(); callback_it != d->deviceChangeEventList.end(); ++callback_it) {
|
||||
if ( (*callback_it)->id != callbackId ) {
|
||||
continue;
|
||||
}
|
||||
newDCList.splice(newDCList.begin(), d->deviceChangeEventList, callback_it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (newDCList.size()) {
|
||||
DeviceChangeList::iterator it = newDCList.begin();
|
||||
{TelldusCore::MutexLocker locker( &(*it)->mutex );}
|
||||
delete (*it);
|
||||
newDCList.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
RawDeviceEventList newRDEList;
|
||||
{
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
for(RawDeviceEventList::iterator callback_it = d->rawDeviceEventList.begin(); callback_it != d->rawDeviceEventList.end(); ++callback_it) {
|
||||
if ( (*callback_it)->id != callbackId ) {
|
||||
continue;
|
||||
}
|
||||
newRDEList.splice(newRDEList.begin(), d->rawDeviceEventList, callback_it );
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (newRDEList.size()) {
|
||||
RawDeviceEventList::iterator it = newRDEList.begin();
|
||||
{TelldusCore::MutexLocker locker( &(*it)->mutex );}
|
||||
delete (*it);
|
||||
newRDEList.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
SensorEventList newSEList;
|
||||
{
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
for(SensorEventList::iterator callback_it = d->sensorEventList.begin(); callback_it != d->sensorEventList.end(); ++callback_it) {
|
||||
if ( (*callback_it)->id != callbackId ) {
|
||||
continue;
|
||||
}
|
||||
newSEList.splice(newSEList.begin(), d->sensorEventList, callback_it );
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (newSEList.size()) {
|
||||
SensorEventList::iterator it = newSEList.begin();
|
||||
{TelldusCore::MutexLocker locker( &(*it)->mutex );}
|
||||
delete (*it);
|
||||
newSEList.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return d->callbackMainDispatcher.unregisterCallback(callbackId);
|
||||
}
|
||||
|
||||
int Client::getSensor(char *protocol, int protocolLen, char *model, int modelLen, int *sensorId, int *dataTypes) {
|
||||
|
@ -403,3 +226,41 @@ int Client::getSensor(char *protocol, int protocolLen, char *model, int modelLen
|
|||
|
||||
return TELLSTICK_SUCCESS;
|
||||
}
|
||||
|
||||
int Client::getController(int *controllerId, int *controllerType, char *name, int nameLen, int *available) {
|
||||
if (!d->controllerCached) {
|
||||
Message msg(L"tdController");
|
||||
std::wstring response = Client::getWStringFromService(msg);
|
||||
int count = Message::takeInt(&response);
|
||||
d->controllerCached = true;
|
||||
d->controllerCache = L"";
|
||||
if (count > 0) {
|
||||
d->controllerCache = response;
|
||||
}
|
||||
}
|
||||
|
||||
if (d->controllerCache == L"") {
|
||||
d->controllerCached = false;
|
||||
return TELLSTICK_ERROR_NOT_FOUND;
|
||||
}
|
||||
|
||||
int id = Message::takeInt(&d->controllerCache);
|
||||
int type = Message::takeInt(&d->controllerCache);
|
||||
std::wstring n = Message::takeString(&d->controllerCache);
|
||||
int a = Message::takeInt(&d->controllerCache);
|
||||
|
||||
if (controllerId) {
|
||||
(*controllerId) = id;
|
||||
}
|
||||
if (controllerType) {
|
||||
(*controllerType) = type;
|
||||
}
|
||||
if (name && nameLen) {
|
||||
strncpy(name, TelldusCore::wideToString(n).c_str(), nameLen);
|
||||
}
|
||||
if (available) {
|
||||
(*available) = a;
|
||||
}
|
||||
|
||||
return TELLSTICK_SUCCESS;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "Message.h"
|
||||
#include "telldus-core.h"
|
||||
#include "Thread.h"
|
||||
#include "CallbackDispatcher.h"
|
||||
|
||||
namespace TelldusCore {
|
||||
class Client : public Thread
|
||||
|
@ -14,18 +15,12 @@ namespace TelldusCore {
|
|||
static Client *getInstance();
|
||||
static void close();
|
||||
|
||||
void callbackDeviceEvent(int deviceId, int deviceState, const std::wstring &deviceStateValue);
|
||||
void callbackDeviceChangeEvent(int deviceId, int eventDeviceChanges, int eventChangeType);
|
||||
void callbackRawEvent(std::wstring command, int controllerId);
|
||||
void callbackSensorEvent(const std::wstring &protocol, const std::wstring &model, int id, int dataType, const std::wstring &value, int timestamp);
|
||||
int registerDeviceEvent( TDDeviceEvent eventFunction, void *context );
|
||||
int registerDeviceChangeEvent( TDDeviceChangeEvent eventFunction, void *context );
|
||||
int registerRawDeviceEvent( TDRawDeviceEvent eventFunction, void *context );
|
||||
int registerSensorEvent( TDSensorEvent eventFunction, void *context );
|
||||
int registerEvent(CallbackStruct::CallbackType type, void *eventFunction, void *context );
|
||||
void stopThread(void);
|
||||
bool unregisterCallback( int callbackId );
|
||||
|
||||
int getSensor(char *protocol, int protocolLen, char *model, int modelLen, int *id, int *dataTypes);
|
||||
int getController(int *controllerId, int *controllerType, char *name, int nameLen, int *available);
|
||||
|
||||
static bool getBoolFromService(const Message &msg);
|
||||
static int getIntegerFromService(const Message &msg);
|
||||
|
@ -37,7 +32,7 @@ namespace TelldusCore {
|
|||
private:
|
||||
Client();
|
||||
static std::wstring sendToService(const Message &msg);
|
||||
|
||||
|
||||
class PrivateData;
|
||||
PrivateData *d;
|
||||
static Client *instance;
|
||||
|
|
|
@ -52,4 +52,9 @@ EXPORTS
|
|||
tdRegisterSensorEvent @37
|
||||
tdSensor @38
|
||||
tdSensorValue @39
|
||||
|
||||
|
||||
tdController @40
|
||||
tdControllerValue @41
|
||||
tdSetControllerValue @42
|
||||
tdRemoveController @43
|
||||
tdRegisterControllerEvent @44
|
||||
|
|
|
@ -110,8 +110,8 @@ void WINAPI tdInit(void) {
|
|||
* Added in version 2.0.0.
|
||||
**/
|
||||
int WINAPI tdRegisterDeviceEvent( TDDeviceEvent eventFunction, void *context ) {
|
||||
Client *client = Client::getInstance();
|
||||
return client->registerDeviceEvent( eventFunction, context );
|
||||
eventFunction; Client *client = Client::getInstance();
|
||||
return client->registerEvent( CallbackStruct::DeviceEvent, (void *)eventFunction, context );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -119,7 +119,7 @@ int WINAPI tdRegisterDeviceEvent( TDDeviceEvent eventFunction, void *context ) {
|
|||
**/
|
||||
int WINAPI tdRegisterRawDeviceEvent( TDRawDeviceEvent eventFunction, void *context ) {
|
||||
Client *client = Client::getInstance();
|
||||
return client->registerRawDeviceEvent( eventFunction, context );
|
||||
return client->registerEvent( CallbackStruct::RawDeviceEvent, (void *)eventFunction, context );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -127,7 +127,7 @@ int WINAPI tdRegisterRawDeviceEvent( TDRawDeviceEvent eventFunction, void *conte
|
|||
**/
|
||||
int WINAPI tdRegisterDeviceChangeEvent( TDDeviceChangeEvent eventFunction, void *context) {
|
||||
Client *client = Client::getInstance();
|
||||
return client->registerDeviceChangeEvent( eventFunction, context );
|
||||
return client->registerEvent( CallbackStruct::DeviceChangeEvent, (void *)eventFunction, context );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -135,7 +135,15 @@ int WINAPI tdRegisterDeviceChangeEvent( TDDeviceChangeEvent eventFunction, void
|
|||
**/
|
||||
int WINAPI tdRegisterSensorEvent( TDSensorEvent eventFunction, void *context) {
|
||||
Client *client = Client::getInstance();
|
||||
return client->registerSensorEvent( eventFunction, context );
|
||||
return client->registerEvent( CallbackStruct::SensorEvent, (void *)eventFunction, context );
|
||||
}
|
||||
|
||||
/**
|
||||
* Added in version 2.1.2.
|
||||
**/
|
||||
int WINAPI tdRegisterControllerEvent( TDControllerEvent eventFunction, void *context) {
|
||||
Client *client = Client::getInstance();
|
||||
return client->registerEvent( CallbackStruct::ControllerEvent, (void *)eventFunction, context );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -697,5 +705,83 @@ int WINAPI tdSensorValue(const char *protocol, const char *model, int id, int da
|
|||
return TELLSTICK_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this function to iterate over all controllers. Iterate until
|
||||
* TELLSTICK_SUCCESS is not returned
|
||||
*
|
||||
* Added in version 2.1.2.
|
||||
* @param controllerId A byref int where the id of the controller will be placed
|
||||
* @param controllerType A byref int where the type of the controller will be placed
|
||||
* @param name A byref string where the name of the controller will be placed
|
||||
* @param nameLen The length of the \c name parameter
|
||||
* @param available A byref int if the controller is currently available or maybe disconnected
|
||||
* @returns TELLSTICK_SUCCESS if there is more sensors to be fetched
|
||||
* @sa TELLSTICK_CONTROLLER_TELLSTICK
|
||||
* @sa TELLSTICK_CONTROLLER_TELLSTICK_DUO
|
||||
* @sa TELLSTICK_CONTROLLER_TELLSTICK_NET
|
||||
**/
|
||||
int WINAPI tdController(int *controllerId, int *controllerType, char *name, int nameLen, int *available) {
|
||||
Client *client = Client::getInstance();
|
||||
return client->getController(controllerId, controllerType, name, nameLen, available);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function gets a parameter on a controller.
|
||||
* Valid parameters are: \c serial \c and firmware
|
||||
*
|
||||
* Added in version 2.1.2.
|
||||
* @param controllerId The controller to change
|
||||
* @param name The parameter to get.
|
||||
* @param value A byref string where the value of the parameter will be placed
|
||||
**/
|
||||
int WINAPI tdControllerValue(int controllerId, const char *name, char *value, int valueLen) {
|
||||
Message msg(L"tdControllerValue");
|
||||
msg.addArgument(controllerId);
|
||||
msg.addArgument(name);
|
||||
std::wstring retval = Client::getWStringFromService(msg);
|
||||
if (retval.length() == 0) {
|
||||
return TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if (value && valueLen) {
|
||||
strncpy(value, TelldusCore::wideToString(retval).c_str(), valueLen);
|
||||
}
|
||||
return TELLSTICK_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function sets a parameter on a controller.
|
||||
* Valid parameters are: \c name
|
||||
*
|
||||
* Added in version 2.1.2.
|
||||
* @param controllerId The controller to change
|
||||
* @param name The parameter to change.
|
||||
* @param value The new value for the parameter.
|
||||
**/
|
||||
int WINAPI tdSetControllerValue(int controllerId, const char *name, const char *value) {
|
||||
Message msg(L"tdSetControllerValue");
|
||||
msg.addArgument(controllerId);
|
||||
msg.addArgument(name);
|
||||
msg.addArgument(value);
|
||||
return Client::getIntegerFromService(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function removes a controller from the list
|
||||
* of controllers. The controller must not be
|
||||
* available (disconnected) for this to work.
|
||||
*
|
||||
* Added in version 2.1.2.
|
||||
* @param controllerId The controller to remove
|
||||
* @returns TELLSTICK_SUCCESS if the controller was
|
||||
* removed, TELLSTICK_ERROR_NOT_FOUND if the controller was
|
||||
* not found, and TELLSTICK_ERROR_PERMISSION_DENIED if the
|
||||
* controller is still connected.
|
||||
**/
|
||||
int WINAPI tdRemoveController(int controllerId) {
|
||||
Message msg(L"tdRemoveController");
|
||||
msg.addArgument(controllerId);
|
||||
return Client::getIntegerFromService(msg);
|
||||
}
|
||||
|
||||
/* @} */
|
||||
|
|
|
@ -29,6 +29,7 @@ typedef void (WINAPI *TDDeviceEvent)(int deviceId, int method, const char *data,
|
|||
typedef void (WINAPI *TDDeviceChangeEvent)(int deviceId, int changeEvent, int changeType, int callbackId, void *context);
|
||||
typedef void (WINAPI *TDRawDeviceEvent)(const char *data, int controllerId, int callbackId, void *context);
|
||||
typedef void (WINAPI *TDSensorEvent)(const char *protocol, const char *model, int id, int dataType, const char *value, int timestamp, int callbackId, void *context);
|
||||
typedef void (WINAPI *TDControllerEvent)(int controllerId, int changeEvent, int changeType, const char *newValue, int callbackId, void *context);
|
||||
|
||||
#ifndef __cplusplus
|
||||
#define bool char
|
||||
|
@ -42,6 +43,7 @@ extern "C" {
|
|||
TELLSTICK_API int WINAPI tdRegisterDeviceChangeEvent( TDDeviceChangeEvent eventFunction, void *context);
|
||||
TELLSTICK_API int WINAPI tdRegisterRawDeviceEvent( TDRawDeviceEvent eventFunction, void *context );
|
||||
TELLSTICK_API int WINAPI tdRegisterSensorEvent( TDSensorEvent eventFunction, void *context );
|
||||
TELLSTICK_API int WINAPI tdRegisterControllerEvent( TDControllerEvent eventFunction, void *context);
|
||||
TELLSTICK_API int WINAPI tdUnregisterCallback( int callbackId );
|
||||
TELLSTICK_API void WINAPI tdClose(void);
|
||||
TELLSTICK_API void WINAPI tdReleaseString(char *string);
|
||||
|
@ -86,6 +88,11 @@ extern "C" {
|
|||
TELLSTICK_API int WINAPI tdSensor(char *protocol, int protocolLen, char *model, int modelLen, int *id, int *dataTypes);
|
||||
TELLSTICK_API int WINAPI tdSensorValue(const char *protocol, const char *model, int id, int dataType, char *value, int len, int *timestamp);
|
||||
|
||||
TELLSTICK_API int WINAPI tdController(int *controllerId, int *controllerType, char *name, int nameLen, int *available);
|
||||
TELLSTICK_API int WINAPI tdControllerValue(int controllerId, const char *name, char *value, int valueLen);
|
||||
TELLSTICK_API int WINAPI tdSetControllerValue(int controllerId, const char *name, const char *value);
|
||||
TELLSTICK_API int WINAPI tdRemoveController(int controllerId);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -125,6 +132,11 @@ extern "C" {
|
|||
#define TELLSTICK_TYPE_GROUP 2
|
||||
#define TELLSTICK_TYPE_SCENE 3
|
||||
|
||||
//Controller typedef
|
||||
#define TELLSTICK_CONTROLLER_TELLSTICK 1
|
||||
#define TELLSTICK_CONTROLLER_TELLSTICK_DUO 2
|
||||
#define TELLSTICK_CONTROLLER_TELLSTICK_NET 3
|
||||
|
||||
//Device changes
|
||||
#define TELLSTICK_DEVICE_ADDED 1
|
||||
#define TELLSTICK_DEVICE_CHANGED 2
|
||||
|
@ -136,5 +148,7 @@ extern "C" {
|
|||
#define TELLSTICK_CHANGE_PROTOCOL 2
|
||||
#define TELLSTICK_CHANGE_MODEL 3
|
||||
#define TELLSTICK_CHANGE_METHOD 4
|
||||
#define TELLSTICK_CHANGE_AVAILABLE 5
|
||||
#define TELLSTICK_CHANGE_FIRMWARE 6
|
||||
|
||||
#endif
|
||||
|
|
|
@ -5,6 +5,9 @@
|
|||
//
|
||||
//
|
||||
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#ifdef _WINDOWS
|
||||
#include <windows.h>
|
||||
#include <ole2.h>
|
||||
|
@ -75,3 +78,5 @@ inline char *wrapStdString( const std::string &string) {
|
|||
inline char *wrapStdWstring( const std::wstring &wstring) {
|
||||
return wrapStdString(TelldusCore::wideToString(wstring));
|
||||
}
|
||||
|
||||
#endif //COMMON_H
|
||||
|
|
|
@ -10,13 +10,14 @@ public:
|
|||
TelldusCore::EventRef event, deviceUpdateEvent;
|
||||
bool done;
|
||||
DeviceManager *deviceManager;
|
||||
ControllerManager *controllerManager;
|
||||
};
|
||||
|
||||
ClientCommunicationHandler::ClientCommunicationHandler(){
|
||||
|
||||
}
|
||||
|
||||
ClientCommunicationHandler::ClientCommunicationHandler(TelldusCore::Socket *clientSocket, TelldusCore::EventRef event, DeviceManager *deviceManager, TelldusCore::EventRef deviceUpdateEvent)
|
||||
ClientCommunicationHandler::ClientCommunicationHandler(TelldusCore::Socket *clientSocket, TelldusCore::EventRef event, DeviceManager *deviceManager, TelldusCore::EventRef deviceUpdateEvent, ControllerManager *controllerManager)
|
||||
:Thread()
|
||||
{
|
||||
d = new PrivateData;
|
||||
|
@ -25,7 +26,7 @@ ClientCommunicationHandler::ClientCommunicationHandler(TelldusCore::Socket *clie
|
|||
d->done = false;
|
||||
d->deviceManager = deviceManager;
|
||||
d->deviceUpdateEvent = deviceUpdateEvent;
|
||||
|
||||
d->controllerManager = controllerManager;
|
||||
}
|
||||
|
||||
ClientCommunicationHandler::~ClientCommunicationHandler(void)
|
||||
|
@ -230,8 +231,25 @@ void ClientCommunicationHandler::parseMessage(const std::wstring &clientMessage,
|
|||
int dataType = TelldusCore::Message::takeInt(&msg);
|
||||
(*wstringReturn) = d->deviceManager->getSensorValue(protocol, model, id, dataType);
|
||||
|
||||
}
|
||||
else{
|
||||
} else if (function == L"tdController") {
|
||||
(*wstringReturn) = d->controllerManager->getControllers();
|
||||
|
||||
} else if (function == L"tdControllerValue") {
|
||||
int id = TelldusCore::Message::takeInt(&msg);
|
||||
std::wstring name = TelldusCore::Message::takeString(&msg);
|
||||
(*wstringReturn) = d->controllerManager->getControllerValue(id, name);
|
||||
|
||||
} else if (function == L"tdSetControllerValue") {
|
||||
int id = TelldusCore::Message::takeInt(&msg);
|
||||
std::wstring name = TelldusCore::Message::takeString(&msg);
|
||||
std::wstring value = TelldusCore::Message::takeString(&msg);
|
||||
(*intReturn) = d->controllerManager->setControllerValue(id, name, value);
|
||||
|
||||
} else if (function == L"tdRemoveController") {
|
||||
int controllerId = TelldusCore::Message::takeInt(&msg);
|
||||
(*intReturn) = d->controllerManager->removeController(controllerId);
|
||||
|
||||
} else{
|
||||
(*intReturn) = TELLSTICK_ERROR_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,13 @@ class ClientCommunicationHandler : public TelldusCore::Thread
|
|||
{
|
||||
public:
|
||||
ClientCommunicationHandler();
|
||||
ClientCommunicationHandler(TelldusCore::Socket *clientSocket, TelldusCore::EventRef event, DeviceManager *deviceManager, TelldusCore::EventRef deviceUpdateEvent);
|
||||
ClientCommunicationHandler(
|
||||
TelldusCore::Socket *clientSocket,
|
||||
TelldusCore::EventRef event,
|
||||
DeviceManager *deviceManager,
|
||||
TelldusCore::EventRef deviceUpdateEvent,
|
||||
ControllerManager *controllerManager
|
||||
);
|
||||
~ClientCommunicationHandler(void);
|
||||
|
||||
bool isDone();
|
||||
|
|
|
@ -1,17 +1,20 @@
|
|||
#include "Controller.h"
|
||||
#include "Protocol.h"
|
||||
#include <stdio.h> //TODO DEBUG
|
||||
#include "EventUpdateManager.h"
|
||||
#include "Strings.h"
|
||||
|
||||
class Controller::PrivateData {
|
||||
public:
|
||||
TelldusCore::Event *event;
|
||||
int id;
|
||||
TelldusCore::EventRef event, updateEvent;
|
||||
int id, firmwareVersion;
|
||||
};
|
||||
|
||||
Controller::Controller(int id, TelldusCore::Event *event){
|
||||
Controller::Controller(int id, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent){
|
||||
d = new PrivateData;
|
||||
d->event = event;
|
||||
d->updateEvent = updateEvent;
|
||||
d->id = id;
|
||||
d->firmwareVersion = 0;
|
||||
}
|
||||
|
||||
Controller::~Controller(){
|
||||
|
@ -33,3 +36,18 @@ void Controller::decodePublishData(const std::string &data) const {
|
|||
this->publishData(*msgIt);
|
||||
}
|
||||
}
|
||||
|
||||
int Controller::firmwareVersion() const {
|
||||
return d->firmwareVersion;
|
||||
}
|
||||
|
||||
void Controller::setFirmwareVersion(int version) {
|
||||
d->firmwareVersion = version;
|
||||
EventUpdateData *eventData = new EventUpdateData();
|
||||
eventData->messageType = L"TDControllerEvent";
|
||||
eventData->controllerId = d->id;
|
||||
eventData->eventState = TELLSTICK_DEVICE_CHANGED;
|
||||
eventData->eventChangeType = TELLSTICK_CHANGE_FIRMWARE;
|
||||
eventData->eventValue = TelldusCore::intToWstring(version);
|
||||
d->updateEvent->signal(eventData);
|
||||
}
|
||||
|
|
|
@ -14,14 +14,15 @@ class Controller {
|
|||
public:
|
||||
virtual ~Controller();
|
||||
|
||||
virtual int firmwareVersion() = 0;
|
||||
virtual int firmwareVersion() const;
|
||||
virtual int send( const std::string &message ) = 0;
|
||||
virtual int reset() = 0;
|
||||
|
||||
protected:
|
||||
Controller(int id, TelldusCore::Event *event);
|
||||
Controller(int id, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent);
|
||||
void publishData(const std::string &data) const;
|
||||
void decodePublishData(const std::string &data) const;
|
||||
void setFirmwareVersion(int version);
|
||||
|
||||
private:
|
||||
class PrivateData;
|
||||
|
|
|
@ -3,31 +3,47 @@
|
|||
#include "Mutex.h"
|
||||
#include "TellStick.h"
|
||||
#include "Log.h"
|
||||
#include "Message.h"
|
||||
#include "Strings.h"
|
||||
#include "Settings.h"
|
||||
#include "EventUpdateManager.h"
|
||||
#include "../client/telldus-core.h"
|
||||
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef std::map<int, Controller *> ControllerMap;
|
||||
class ControllerDescriptor {
|
||||
public:
|
||||
std::wstring name, serial;
|
||||
int type;
|
||||
Controller *controller;
|
||||
};
|
||||
|
||||
typedef std::map<int, ControllerDescriptor> ControllerMap;
|
||||
|
||||
class ControllerManager::PrivateData {
|
||||
public:
|
||||
int lastControllerId;
|
||||
Settings settings;
|
||||
ControllerMap controllers;
|
||||
TelldusCore::Event *event;
|
||||
TelldusCore::EventRef event, updateEvent;
|
||||
TelldusCore::Mutex mutex;
|
||||
};
|
||||
|
||||
ControllerManager::ControllerManager(TelldusCore::Event *event){
|
||||
ControllerManager::ControllerManager(TelldusCore::EventRef event, TelldusCore::EventRef updateEvent){
|
||||
d = new PrivateData;
|
||||
d->lastControllerId = 0;
|
||||
d->event = event;
|
||||
d->updateEvent = updateEvent;
|
||||
this->loadStoredControllers();
|
||||
this->loadControllers();
|
||||
}
|
||||
|
||||
ControllerManager::~ControllerManager() {
|
||||
for (ControllerMap::iterator it = d->controllers.begin(); it != d->controllers.end(); ++it) {
|
||||
delete( it->second );
|
||||
if (it->second.controller) {
|
||||
delete( it->second.controller );
|
||||
}
|
||||
}
|
||||
delete d;
|
||||
}
|
||||
|
@ -41,8 +57,9 @@ void ControllerManager::deviceInsertedOrRemoved(int vid, int pid, const std::str
|
|||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
while(d->controllers.size()) {
|
||||
ControllerMap::iterator it = d->controllers.begin();
|
||||
delete it->second;
|
||||
d->controllers.erase(it);
|
||||
delete it->second.controller;
|
||||
it->second.controller = 0;
|
||||
signalControllerEvent(it->first, TELLSTICK_DEVICE_STATE_CHANGED, TELLSTICK_CHANGE_AVAILABLE, L"0");
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
@ -58,31 +75,29 @@ void ControllerManager::deviceInsertedOrRemoved(int vid, int pid, const std::str
|
|||
} else {
|
||||
//Autodetect which has been disconnected
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
bool again = true;
|
||||
while(again) {
|
||||
again = false;
|
||||
for(ControllerMap::iterator it = d->controllers.begin(); it != d->controllers.end(); ++it) {
|
||||
TellStick *tellstick = reinterpret_cast<TellStick*>(it->second);
|
||||
if (!tellstick) {
|
||||
continue;
|
||||
}
|
||||
if (serial.compare("") != 0) {
|
||||
TellStickDescriptor tsd;
|
||||
tsd.vid = vid;
|
||||
tsd.pid = pid;
|
||||
tsd.serial = serial;
|
||||
if (!tellstick->isSameAsDescriptor(tsd)) {
|
||||
continue;
|
||||
}
|
||||
} else if (tellstick->stillConnected()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
d->controllers.erase(it);
|
||||
delete tellstick;
|
||||
again=true;
|
||||
break;
|
||||
for(ControllerMap::iterator it = d->controllers.begin(); it != d->controllers.end(); ++it) {
|
||||
if (!it->second.controller) {
|
||||
continue;
|
||||
}
|
||||
TellStick *tellstick = reinterpret_cast<TellStick*>(it->second.controller);
|
||||
if (!tellstick) {
|
||||
continue;
|
||||
}
|
||||
if (serial.compare("") != 0) {
|
||||
TellStickDescriptor tsd;
|
||||
tsd.vid = vid;
|
||||
tsd.pid = pid;
|
||||
tsd.serial = serial;
|
||||
if (!tellstick->isSameAsDescriptor(tsd)) {
|
||||
continue;
|
||||
}
|
||||
} else if (tellstick->stillConnected()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
it->second.controller = 0;
|
||||
delete tellstick;
|
||||
signalControllerEvent(it->first, TELLSTICK_DEVICE_STATE_CHANGED, TELLSTICK_CHANGE_AVAILABLE, L"0");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -93,10 +108,16 @@ Controller *ControllerManager::getBestControllerById(int id) {
|
|||
return 0;
|
||||
}
|
||||
ControllerMap::const_iterator it = d->controllers.find(id);
|
||||
if (it != d->controllers.end()) {
|
||||
return it->second;
|
||||
if (it != d->controllers.end() && it->second.controller) {
|
||||
return it->second.controller;
|
||||
}
|
||||
return d->controllers.begin()->second;
|
||||
//Find first available controller
|
||||
for(it = d->controllers.begin(); it != d->controllers.end(); ++it) {
|
||||
if (it->second.controller) {
|
||||
return it->second.controller;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
@ -112,7 +133,10 @@ void ControllerManager::loadControllers() {
|
|||
bool found = false;
|
||||
ControllerMap::const_iterator cit = d->controllers.begin();
|
||||
for(; cit != d->controllers.end(); ++cit) {
|
||||
TellStick *tellstick = reinterpret_cast<TellStick*>(cit->second);
|
||||
if (!cit->second.controller) {
|
||||
continue;
|
||||
}
|
||||
TellStick *tellstick = reinterpret_cast<TellStick*>(cit->second.controller);
|
||||
if (!tellstick) {
|
||||
continue;
|
||||
}
|
||||
|
@ -125,14 +149,59 @@ void ControllerManager::loadControllers() {
|
|||
continue;
|
||||
}
|
||||
|
||||
int controllerId = d->lastControllerId-1;
|
||||
TellStick *controller = new TellStick(controllerId, d->event, *it);
|
||||
int type = TELLSTICK_CONTROLLER_TELLSTICK;
|
||||
if ((*it).pid == 0x0c31) {
|
||||
type = TELLSTICK_CONTROLLER_TELLSTICK_DUO;
|
||||
}
|
||||
int controllerId = 0;
|
||||
//See if the controller matches one of the loaded, non available controllers
|
||||
std::wstring serial = TelldusCore::charToWstring((*it).serial.c_str());
|
||||
for(cit = d->controllers.begin(); cit != d->controllers.end(); ++cit) {
|
||||
if (cit->second.type == type && cit->second.serial.compare(serial) == 0) {
|
||||
controllerId = cit->first;
|
||||
break;
|
||||
}
|
||||
}
|
||||
bool isNew = false;
|
||||
if (!controllerId) {
|
||||
controllerId = d->settings.addNode(Settings::Controller);
|
||||
if(controllerId < 0){
|
||||
//TODO: How to handle this?
|
||||
continue;
|
||||
}
|
||||
isNew = true;
|
||||
d->controllers[controllerId].type = type;
|
||||
d->settings.setControllerType(controllerId, type);
|
||||
d->controllers[controllerId].serial = TelldusCore::charToWstring((*it).serial.c_str());
|
||||
d->settings.setControllerSerial(controllerId, d->controllers[controllerId].serial);
|
||||
}
|
||||
|
||||
//int controllerId = d->lastControllerId+1;
|
||||
TellStick *controller = new TellStick(controllerId, d->event, d->updateEvent, *it);
|
||||
if (!controller->isOpen()) {
|
||||
delete controller;
|
||||
continue;
|
||||
}
|
||||
d->lastControllerId = controllerId;
|
||||
d->controllers[d->lastControllerId] = controller;
|
||||
d->controllers[controllerId].controller = controller;
|
||||
if (isNew) {
|
||||
signalControllerEvent(controllerId, TELLSTICK_DEVICE_ADDED, type, L"");
|
||||
} else {
|
||||
signalControllerEvent(controllerId, TELLSTICK_DEVICE_STATE_CHANGED, TELLSTICK_CHANGE_AVAILABLE, L"1");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ControllerManager::loadStoredControllers() {
|
||||
int numberOfControllers = d->settings.getNumberOfNodes(Settings::Controller);
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
|
||||
for (int i = 0; i < numberOfControllers; ++i) {
|
||||
int id = d->settings.getNodeId(Settings::Controller, i);
|
||||
d->controllers[id].controller = NULL;
|
||||
d->controllers[id].name = d->settings.getName(Settings::Controller, id);
|
||||
d->controllers[id].type = d->settings.getControllerType(id);
|
||||
d->controllers[id].serial = d->settings.getControllerSerial(id);
|
||||
signalControllerEvent(id, TELLSTICK_DEVICE_ADDED, 0, L"");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,7 +212,10 @@ void ControllerManager::queryControllerStatus(){
|
|||
{
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
for(ControllerMap::iterator it = d->controllers.begin(); it != d->controllers.end(); ++it) {
|
||||
TellStick *tellstick = reinterpret_cast<TellStick*>(it->second);
|
||||
if (!it->second.controller) {
|
||||
continue;
|
||||
}
|
||||
TellStick *tellstick = reinterpret_cast<TellStick*>(it->second.controller);
|
||||
if (tellstick) {
|
||||
tellStickControllers.push_back(tellstick);
|
||||
}
|
||||
|
@ -179,3 +251,87 @@ int ControllerManager::resetController(Controller *controller) {
|
|||
deviceInsertedOrRemoved(tellstick->vid(), tellstick->pid(), tellstick->serial(), false); //remove from list and delete
|
||||
return success;
|
||||
}
|
||||
|
||||
std::wstring ControllerManager::getControllers() const {
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
|
||||
TelldusCore::Message msg;
|
||||
|
||||
msg.addArgument((int)d->controllers.size());
|
||||
|
||||
for(ControllerMap::iterator it = d->controllers.begin(); it != d->controllers.end(); ++it) {
|
||||
msg.addArgument(it->first);
|
||||
msg.addArgument(it->second.type);
|
||||
msg.addArgument(it->second.name.c_str());
|
||||
msg.addArgument(it->second.controller ? 1 : 0);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
std::wstring ControllerManager::getControllerValue(int id, const std::wstring &name) {
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
|
||||
ControllerMap::iterator it = d->controllers.find(id);
|
||||
if (it == d->controllers.end()) {
|
||||
return L"";
|
||||
}
|
||||
if (name == L"serial") {
|
||||
return it->second.serial;
|
||||
} else if (name == L"firmware") {
|
||||
if (!it->second.controller) {
|
||||
return L"-1";
|
||||
}
|
||||
return TelldusCore::intToWstring(it->second.controller->firmwareVersion());
|
||||
}
|
||||
return L"";
|
||||
}
|
||||
|
||||
int ControllerManager::removeController(int id) {
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
|
||||
ControllerMap::iterator it = d->controllers.find(id);
|
||||
if (it == d->controllers.end()) {
|
||||
return TELLSTICK_ERROR_NOT_FOUND;
|
||||
}
|
||||
if (it->second.controller) {
|
||||
//Still connected
|
||||
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
||||
}
|
||||
|
||||
int ret = d->settings.removeNode(Settings::Controller, id);
|
||||
if (ret != TELLSTICK_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
d->controllers.erase(it);
|
||||
|
||||
signalControllerEvent(id, TELLSTICK_DEVICE_REMOVED, 0, L"");
|
||||
return TELLSTICK_SUCCESS;
|
||||
}
|
||||
|
||||
int ControllerManager::setControllerValue(int id, const std::wstring &name, const std::wstring &value) {
|
||||
TelldusCore::MutexLocker locker(&d->mutex);
|
||||
|
||||
ControllerMap::iterator it = d->controllers.find(id);
|
||||
if (it == d->controllers.end()) {
|
||||
return TELLSTICK_ERROR_NOT_FOUND;
|
||||
}
|
||||
if (name == L"name") {
|
||||
it->second.name = value;
|
||||
d->settings.setName(Settings::Controller, id, value);
|
||||
signalControllerEvent(id, TELLSTICK_DEVICE_CHANGED, TELLSTICK_CHANGE_NAME, value);
|
||||
} else {
|
||||
return TELLSTICK_ERROR_SYNTAX; //TODO: Is this the best error?
|
||||
}
|
||||
return TELLSTICK_SUCCESS;
|
||||
}
|
||||
|
||||
void ControllerManager::signalControllerEvent(int controllerId, int changeEvent, int changeType, const std::wstring &newValue) {
|
||||
EventUpdateData *eventData = new EventUpdateData();
|
||||
eventData->messageType = L"TDControllerEvent";
|
||||
eventData->controllerId = controllerId;
|
||||
eventData->eventState = changeEvent;
|
||||
eventData->eventChangeType = changeType;
|
||||
eventData->eventValue = newValue;
|
||||
d->updateEvent->signal(eventData);
|
||||
}
|
||||
|
|
|
@ -1,26 +1,31 @@
|
|||
#ifndef CONTROLLERMANAGER_H
|
||||
#define CONTROLLERMANAGER_H
|
||||
|
||||
#include "Event.h"
|
||||
class Controller;
|
||||
namespace TelldusCore {
|
||||
class Event;
|
||||
}
|
||||
|
||||
#include <string>
|
||||
|
||||
class ControllerManager {
|
||||
public:
|
||||
ControllerManager(TelldusCore::Event *event);
|
||||
ControllerManager(TelldusCore::EventRef event, TelldusCore::EventRef updateEvent);
|
||||
~ControllerManager(void);
|
||||
|
||||
void deviceInsertedOrRemoved(int vid, int pid, const std::string &serial, bool inserted);
|
||||
|
||||
Controller *getBestControllerById(int id);
|
||||
void loadControllers();
|
||||
void loadStoredControllers();
|
||||
void queryControllerStatus();
|
||||
int resetController(Controller *controller);
|
||||
|
||||
std::wstring getControllers() const;
|
||||
std::wstring getControllerValue(int id, const std::wstring &name);
|
||||
int removeController(int id);
|
||||
int setControllerValue(int id, const std::wstring &name, const std::wstring &value);
|
||||
|
||||
private:
|
||||
void signalControllerEvent(int controllerId, int changeEvent, int changeType, const std::wstring &newValue);
|
||||
class PrivateData;
|
||||
PrivateData *d;
|
||||
};
|
||||
|
|
|
@ -47,13 +47,13 @@ DeviceManager::~DeviceManager(void) {
|
|||
}
|
||||
|
||||
void DeviceManager::fillDevices(){
|
||||
int numberOfDevices = d->set.getNumberOfDevices();
|
||||
int numberOfDevices = d->set.getNumberOfNodes(Settings::Device);
|
||||
TelldusCore::MutexLocker deviceListLocker(&d->lock);
|
||||
|
||||
for (int i = 0; i < numberOfDevices; ++i) {
|
||||
int id = d->set.getDeviceId(i);
|
||||
int id = d->set.getNodeId(Settings::Device, i);
|
||||
d->devices[id] = new Device(id);
|
||||
d->devices[id]->setName(d->set.getName(id));
|
||||
d->devices[id]->setName(d->set.getName(Settings::Device, id));
|
||||
d->devices[id]->setModel(d->set.getModel(id));
|
||||
d->devices[id]->setProtocolName(d->set.getProtocol(id));
|
||||
d->devices[id]->setPreferredControllerId(d->set.getPreferredControllerId(id));
|
||||
|
@ -235,7 +235,7 @@ int DeviceManager::setDeviceName(int deviceId, const std::wstring &name){
|
|||
DeviceMap::iterator it = d->devices.find(deviceId);
|
||||
if (it != d->devices.end()) {
|
||||
TelldusCore::MutexLocker deviceLocker(it->second);
|
||||
int ret = d->set.setName(deviceId, name);
|
||||
int ret = d->set.setName(Settings::Device, deviceId, name);
|
||||
if (ret != TELLSTICK_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
@ -330,7 +330,7 @@ int DeviceManager::getNumberOfDevices(){
|
|||
|
||||
int DeviceManager::addDevice(){
|
||||
|
||||
int id = d->set.addDevice();
|
||||
int id = d->set.addNode(Settings::Device);
|
||||
if(id < 0){
|
||||
return id;
|
||||
}
|
||||
|
@ -344,7 +344,7 @@ int DeviceManager::addDevice(){
|
|||
}
|
||||
|
||||
int DeviceManager::getDeviceId(int deviceIndex) {
|
||||
return d->set.getDeviceId(deviceIndex);
|
||||
return d->set.getNodeId(Settings::Device, deviceIndex);
|
||||
}
|
||||
|
||||
int DeviceManager::getDeviceType(int deviceId){
|
||||
|
@ -573,7 +573,7 @@ int DeviceManager::removeDevice(int deviceId){
|
|||
|
||||
Device *device = 0;
|
||||
{
|
||||
int ret = d->set.removeDevice(deviceId); //remove from register/settings
|
||||
int ret = d->set.removeNode(Settings::Device, deviceId); //remove from register/settings
|
||||
if (ret != TELLSTICK_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -105,6 +105,13 @@ void EventUpdateManager::sendMessageToClients(EventUpdateData *data){
|
|||
msg.addArgument(data->value);
|
||||
msg.addArgument(data->timestamp);
|
||||
}
|
||||
else if(data->messageType == L"TDControllerEvent") {
|
||||
msg.addArgument("TDControllerEvent");
|
||||
msg.addArgument(data->controllerId);
|
||||
msg.addArgument(data->eventState);
|
||||
msg.addArgument(data->eventChangeType);
|
||||
msg.addArgument(data->eventValue);
|
||||
}
|
||||
|
||||
(*it)->write(msg);
|
||||
|
||||
|
|
|
@ -1,19 +1,21 @@
|
|||
#include "Settings.h"
|
||||
|
||||
TelldusCore::Mutex Settings::mutex;
|
||||
|
||||
/*
|
||||
* Get the name of the device
|
||||
*/
|
||||
std::wstring Settings::getName(int intDeviceId) const {
|
||||
std::wstring Settings::getName(Node type, int intNodeId) const {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return getStringSetting(intDeviceId, L"name", false);
|
||||
return getStringSetting(type, intNodeId, L"name", false);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the name of the device
|
||||
*/
|
||||
int Settings::setName(int intDeviceId, const std::wstring &strNewName){
|
||||
int Settings::setName(Node type, int intDeviceId, const std::wstring &strNewName){
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return setStringSetting(intDeviceId, L"name", strNewName, false);
|
||||
return setStringSetting(type, intDeviceId, L"name", strNewName, false);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -21,7 +23,7 @@ int Settings::setName(int intDeviceId, const std::wstring &strNewName){
|
|||
*/
|
||||
std::wstring Settings::getProtocol(int intDeviceId) const {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return getStringSetting(intDeviceId, L"protocol", false);
|
||||
return getStringSetting(Device, intDeviceId, L"protocol", false);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -29,7 +31,7 @@ std::wstring Settings::getProtocol(int intDeviceId) const {
|
|||
*/
|
||||
int Settings::setProtocol(int intDeviceId, const std::wstring &strVendor){
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return setStringSetting(intDeviceId, L"protocol", strVendor, false);
|
||||
return setStringSetting(Device, intDeviceId, L"protocol", strVendor, false);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -37,7 +39,7 @@ int Settings::setProtocol(int intDeviceId, const std::wstring &strVendor){
|
|||
*/
|
||||
std::wstring Settings::getModel(int intDeviceId) const {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return getStringSetting(intDeviceId, L"model", false);
|
||||
return getStringSetting(Device, intDeviceId, L"model", false);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -45,7 +47,7 @@ std::wstring Settings::getModel(int intDeviceId) const {
|
|||
*/
|
||||
int Settings::setModel(int intDeviceId, const std::wstring &strModel){
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return setStringSetting(intDeviceId, L"model", strModel, false);
|
||||
return setStringSetting(Device, intDeviceId, L"model", strModel, false);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -53,7 +55,7 @@ int Settings::setModel(int intDeviceId, const std::wstring &strModel){
|
|||
*/
|
||||
int Settings::setDeviceParameter(int intDeviceId, const std::wstring &strName, const std::wstring &strValue){
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return setStringSetting(intDeviceId, strName, strValue, true);
|
||||
return setStringSetting(Device, intDeviceId, strName, strValue, true);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -61,7 +63,7 @@ int Settings::setDeviceParameter(int intDeviceId, const std::wstring &strName, c
|
|||
*/
|
||||
std::wstring Settings::getDeviceParameter(int intDeviceId, const std::wstring &strName) const {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return getStringSetting(intDeviceId, strName, true);
|
||||
return getStringSetting(Device, intDeviceId, strName, true);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -69,7 +71,7 @@ std::wstring Settings::getDeviceParameter(int intDeviceId, const std::wstring &s
|
|||
*/
|
||||
int Settings::setPreferredControllerId(int intDeviceId, int value){
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return setIntSetting(intDeviceId, L"controller", value, false);
|
||||
return setIntSetting(Device, intDeviceId, L"controller", value, false);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -77,26 +79,54 @@ int Settings::setPreferredControllerId(int intDeviceId, int value){
|
|||
*/
|
||||
int Settings::getPreferredControllerId(int intDeviceId) {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return getIntSetting(intDeviceId, L"controller", false);
|
||||
return getIntSetting(Device, intDeviceId, L"controller", false);
|
||||
}
|
||||
|
||||
std::wstring Settings::getControllerSerial(int intControllerId) const {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return getStringSetting(Controller, intControllerId, L"serial", false);
|
||||
}
|
||||
|
||||
int Settings::setControllerSerial(int intControllerId, const std::wstring &serial) {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return setStringSetting(Controller, intControllerId, L"serial", serial, false);
|
||||
}
|
||||
|
||||
int Settings::getControllerType(int intControllerId) const {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return getIntSetting(Controller, intControllerId, L"type", false);
|
||||
}
|
||||
|
||||
int Settings::setControllerType(int intControllerId, int type) {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return setIntSetting(Controller, intControllerId, L"type", type, false);
|
||||
}
|
||||
|
||||
std::string Settings::getNodeString(Settings::Node type) const {
|
||||
if (type == Device) {
|
||||
return "device";
|
||||
} else if (type == Controller) {
|
||||
return "controller";
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef _CONFUSE
|
||||
|
||||
bool Settings::setDeviceState( int intDeviceId, int intDeviceState, const std::wstring &strDeviceStateValue ) {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
bool retval = setIntSetting( intDeviceId, L"state", intDeviceState, true );
|
||||
setStringSetting( intDeviceId, L"stateValue", strDeviceStateValue, true );
|
||||
bool retval = setIntSetting( Settings::Device, intDeviceId, L"state", intDeviceState, true );
|
||||
setStringSetting( Settings::Device, intDeviceId, L"stateValue", strDeviceStateValue, true );
|
||||
return retval;
|
||||
}
|
||||
|
||||
int Settings::getDeviceState( int intDeviceId ) const {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return getIntSetting( intDeviceId, L"state", true );
|
||||
return getIntSetting( Settings::Device, intDeviceId, L"state", true );
|
||||
}
|
||||
|
||||
std::wstring Settings::getDeviceStateValue( int intDeviceId ) const {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
return getStringSetting( intDeviceId, L"stateValue", true );
|
||||
return getStringSetting( Settings::Device, intDeviceId, L"stateValue", true );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,13 +6,15 @@
|
|||
|
||||
class Settings {
|
||||
public:
|
||||
enum Node { Device, Controller };
|
||||
|
||||
Settings(void);
|
||||
virtual ~Settings(void);
|
||||
|
||||
std::wstring getSetting(const std::wstring &strName) const;
|
||||
int getNumberOfDevices(void) const;
|
||||
std::wstring getName(int intDeviceId) const;
|
||||
int setName(int intDeviceId, const std::wstring &strNewName);
|
||||
int getNumberOfNodes(Node type) const;
|
||||
std::wstring getName(Node type, int intNodeId) const;
|
||||
int setName(Node type, int intDeviceId, const std::wstring &strNewName);
|
||||
std::wstring getProtocol(int intDeviceId) const;
|
||||
int setProtocol(int intDeviceId, const std::wstring &strVendor);
|
||||
std::wstring getModel(int intDeviceId) const;
|
||||
|
@ -25,22 +27,28 @@ public:
|
|||
int getPreferredControllerId(int intDeviceId);
|
||||
int setPreferredControllerId(int intDeviceId, int value);
|
||||
|
||||
int addDevice();
|
||||
int getDeviceId(int intDeviceIndex) const;
|
||||
int removeDevice(int intDeviceId);
|
||||
int addNode(Node type);
|
||||
int getNodeId(Node type, int intDeviceIndex) const;
|
||||
int removeNode(Node type, int intNodeId);
|
||||
|
||||
std::wstring getControllerSerial(int intControllerId) const;
|
||||
int setControllerSerial(int intControllerId, const std::wstring &serial);
|
||||
int getControllerType(int intControllerId) const;
|
||||
int setControllerType(int intControllerId, int type);
|
||||
|
||||
protected:
|
||||
std::wstring getStringSetting(int intDeviceId, const std::wstring &name, bool parameter) const;
|
||||
int setStringSetting(int intDeviceId, const std::wstring &name, const std::wstring &value, bool parameter);
|
||||
int getIntSetting(int intDeviceId, const std::wstring &name, bool parameter) const;
|
||||
int setIntSetting(int intDeviceId, const std::wstring &name, int value, bool parameter);
|
||||
std::wstring getStringSetting(Node type, int intNodeId, const std::wstring &name, bool parameter) const;
|
||||
int setStringSetting(Node type, int intDeviceId, const std::wstring &name, const std::wstring &value, bool parameter);
|
||||
int getIntSetting(Node type, int intDeviceId, const std::wstring &name, bool parameter) const;
|
||||
int setIntSetting(Node type, int intDeviceId, const std::wstring &name, int value, bool parameter);
|
||||
|
||||
private:
|
||||
int getNextDeviceId() const;
|
||||
int getNextNodeId(Node type) const;
|
||||
std::string getNodeString(Node type) const;
|
||||
|
||||
class PrivateData;
|
||||
PrivateData *d;
|
||||
mutable TelldusCore::Mutex mutex;
|
||||
static TelldusCore::Mutex mutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -76,86 +76,107 @@ std::wstring Settings::getSetting(const std::wstring &strName) const {
|
|||
/*
|
||||
* Return the number of stored devices
|
||||
*/
|
||||
int Settings::getNumberOfDevices(void) const {
|
||||
int Settings::getNumberOfNodes(Node node) const {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
if (d->cfg > 0) {
|
||||
return cfg_size(d->cfg, "device");
|
||||
if (node == Device) {
|
||||
return cfg_size(d->cfg, "device");
|
||||
} else if (node == Controller) {
|
||||
return cfg_size(d->cfg, "controller");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Settings::getDeviceId(int intDeviceIndex) const {
|
||||
if (intDeviceIndex >= getNumberOfDevices()) { //Out of bounds
|
||||
int Settings::getNodeId(Node type, int intDeviceIndex) const {
|
||||
if (intDeviceIndex >= getNumberOfNodes(type)) { //Out of bounds
|
||||
return -1;
|
||||
}
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
cfg_t *cfg_device = cfg_getnsec(d->cfg, "device", intDeviceIndex);
|
||||
int id = cfg_getint(cfg_device, "id");
|
||||
cfg_t *cfg_node;
|
||||
if (type == Device) {
|
||||
cfg_node = cfg_getnsec(d->cfg, "device", intDeviceIndex);
|
||||
} else if (type == Controller) {
|
||||
cfg_node = cfg_getnsec(d->cfg, "controller", intDeviceIndex);
|
||||
}
|
||||
int id = cfg_getint(cfg_node, "id");
|
||||
return id;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add a new device
|
||||
* Add a new node
|
||||
*/
|
||||
int Settings::addDevice(){
|
||||
int Settings::addNode(Node type){
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
int intDeviceId = getNextDeviceId();
|
||||
int intNodeId = getNextNodeId(type);
|
||||
|
||||
FILE *fp = fopen(CONFIG_FILE, "w");
|
||||
if (!fp) {
|
||||
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
||||
}
|
||||
cfg_print(d->cfg, fp); //Print the config-file
|
||||
fprintf(fp, "device {\n id=%d\n}\n", intDeviceId); //Print the new device
|
||||
if (type == Device) {
|
||||
fprintf(fp, "device {\n id=%d\n}\n", intNodeId); //Print the new device
|
||||
} else if (type == Controller) {
|
||||
fprintf(fp, "controller {\n id=%d\n}\n", intNodeId); //Print the new controller
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
//Re-read config-file
|
||||
cfg_free(d->cfg);
|
||||
readConfig(&d->cfg);
|
||||
return intDeviceId;
|
||||
return intNodeId;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get next available device id
|
||||
* Get next available node id
|
||||
*/
|
||||
int Settings::getNextDeviceId() const {
|
||||
int Settings::getNextNodeId(Node type) const {
|
||||
//Private, no locks needed
|
||||
int intDeviceId = 0;
|
||||
cfg_t *cfg_device;
|
||||
for (int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
||||
if (cfg_getint(cfg_device, "id") >= intDeviceId) {
|
||||
intDeviceId = cfg_getint(cfg_device, "id");
|
||||
int intNodeId = 0;
|
||||
cfg_t *cfg_node;
|
||||
std::string strType;
|
||||
if (type == Device) {
|
||||
strType = "device";
|
||||
} else if (type == Controller) {
|
||||
strType = "controller";
|
||||
}
|
||||
for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
|
||||
cfg_node = cfg_getnsec(d->cfg, strType.c_str(), i);
|
||||
if (cfg_getint(cfg_node, "id") >= intNodeId) {
|
||||
intNodeId = cfg_getint(cfg_node, "id");
|
||||
}
|
||||
}
|
||||
intDeviceId++;
|
||||
return intDeviceId;
|
||||
intNodeId++;
|
||||
return intNodeId;
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove a device
|
||||
*/
|
||||
int Settings::removeDevice(int intDeviceId){
|
||||
int Settings::removeNode(Node type, int intNodeId){
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
FILE *fp = fopen(CONFIG_FILE, "w");
|
||||
if (!fp) {
|
||||
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
||||
}
|
||||
|
||||
std::string strType = getNodeString(type);
|
||||
|
||||
// Print all opts
|
||||
for(int i = 0; d->cfg->opts[i].name; i++) {
|
||||
|
||||
// Check if it isn't a device section
|
||||
if (strcmp(d->cfg->opts[i].name, "device") != 0) {
|
||||
if (strcmp(d->cfg->opts[i].name, strType.c_str()) != 0) {
|
||||
cfg_opt_print(&d->cfg->opts[i], fp);
|
||||
} else {
|
||||
// Print all sections except the one to remove
|
||||
cfg_t *cfg_device;
|
||||
for (int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
||||
if (cfg_getint(cfg_device, "id") != intDeviceId) { //This isn't the one to skip
|
||||
fprintf(fp, "device {\n");
|
||||
cfg_print_indent(cfg_device, fp, 1);
|
||||
cfg_t *cfg_node;
|
||||
for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
|
||||
cfg_node = cfg_getnsec(d->cfg, strType.c_str(), i);
|
||||
if (cfg_getint(cfg_node, "id") != intNodeId) { //This isn't the one to skip
|
||||
fprintf(fp, "%s {\n", strType.c_str());
|
||||
cfg_print_indent(cfg_node, fp, 1);
|
||||
fprintf(fp, "}\n");
|
||||
}
|
||||
}
|
||||
|
@ -244,15 +265,17 @@ std::wstring Settings::getDeviceStateValue( int intDeviceId ) const {
|
|||
return L"";
|
||||
}
|
||||
|
||||
std::wstring Settings::getStringSetting(int intDeviceId, const std::wstring &name, bool parameter) const {
|
||||
std::wstring Settings::getStringSetting(Node type, int intNodeId, const std::wstring &name, bool parameter) const {
|
||||
//already locked
|
||||
if (d->cfg == 0) {
|
||||
return L"";
|
||||
}
|
||||
std::string strType = getNodeString(type);
|
||||
|
||||
cfg_t *cfg_device;
|
||||
for (int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
||||
if (cfg_getint(cfg_device, "id") == intDeviceId) {
|
||||
for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
|
||||
cfg_device = cfg_getnsec(d->cfg, strType.c_str(), i);
|
||||
if (cfg_getint(cfg_device, "id") == intNodeId) {
|
||||
if (parameter) {
|
||||
cfg_device = cfg_getsec(cfg_device, "parameters");
|
||||
}
|
||||
|
@ -267,14 +290,15 @@ std::wstring Settings::getStringSetting(int intDeviceId, const std::wstring &nam
|
|||
return L"";
|
||||
}
|
||||
|
||||
int Settings::setStringSetting(int intDeviceId, const std::wstring &name, const std::wstring &value, bool parameter) {
|
||||
int Settings::setStringSetting(Node type, int intDeviceId, const std::wstring &name, const std::wstring &value, bool parameter) {
|
||||
//already locked
|
||||
if (d->cfg == 0) {
|
||||
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
||||
}
|
||||
std::string strType = getNodeString(type);
|
||||
cfg_t *cfg_device;
|
||||
for (int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
||||
for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
|
||||
cfg_device = cfg_getnsec(d->cfg, strType.c_str(), i);
|
||||
if (cfg_getint(cfg_device, "id") == intDeviceId) {
|
||||
std::string newValue = TelldusCore::wideToString(value);
|
||||
if (parameter) {
|
||||
|
@ -295,32 +319,34 @@ int Settings::setStringSetting(int intDeviceId, const std::wstring &name, const
|
|||
return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
|
||||
}
|
||||
|
||||
int Settings::getIntSetting(int intDeviceId, const std::wstring &name, bool parameter) const {
|
||||
int Settings::getIntSetting(Node type, int intDeviceId, const std::wstring &name, bool parameter) const {
|
||||
//already locked
|
||||
if (d->cfg == 0) {
|
||||
return 0;
|
||||
}
|
||||
cfg_t *cfg_device;
|
||||
for(int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
||||
if (cfg_getint(cfg_device, "id") == intDeviceId) {
|
||||
std::string strType = getNodeString(type);
|
||||
cfg_t *cfg_node;
|
||||
for(int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
|
||||
cfg_node = cfg_getnsec(d->cfg, strType.c_str(), i);
|
||||
if (cfg_getint(cfg_node, "id") == intDeviceId) {
|
||||
if (parameter) {
|
||||
cfg_device = cfg_getsec(cfg_device, "parameters");
|
||||
cfg_node = cfg_getsec(cfg_node, "parameters");
|
||||
}
|
||||
return cfg_getint(cfg_device, TelldusCore::wideToString(name).c_str());
|
||||
return cfg_getint(cfg_node, TelldusCore::wideToString(name).c_str());
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Settings::setIntSetting(int intDeviceId, const std::wstring &name, int value, bool parameter) {
|
||||
int Settings::setIntSetting(Node type, int intDeviceId, const std::wstring &name, int value, bool parameter) {
|
||||
//already locked
|
||||
if (d->cfg == 0) {
|
||||
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
||||
}
|
||||
std::string strType = getNodeString(type);
|
||||
cfg_t *cfg_device;
|
||||
for (int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
||||
for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
|
||||
cfg_device = cfg_getnsec(d->cfg, strType.c_str(), i);
|
||||
if (cfg_getint(cfg_device, "id") == intDeviceId) {
|
||||
if (parameter) {
|
||||
cfg_t *cfg_parameters = cfg_getsec(cfg_device, "parameters");
|
||||
|
@ -345,6 +371,11 @@ bool readConfig(cfg_t **cfg) {
|
|||
//All the const_cast keywords is to remove the compiler warnings generated by the C++-compiler.
|
||||
cfg_opt_t controller_opts[] = {
|
||||
CFG_INT(const_cast<char *>("id"), -1, CFGF_NONE),
|
||||
CFG_STR(const_cast<char *>("name"), const_cast<char *>(""), CFGF_NONE),
|
||||
CFG_INT(const_cast<char *>("type"), 0, CFGF_NONE),
|
||||
CFG_STR(const_cast<char *>("serial"), const_cast<char *>(""), CFGF_NONE),
|
||||
|
||||
CFG_END()
|
||||
};
|
||||
|
||||
cfg_opt_t device_parameter_opts[] = {
|
||||
|
@ -377,6 +408,7 @@ bool readConfig(cfg_t **cfg) {
|
|||
CFG_STR(const_cast<char *>("deviceNode"), const_cast<char *>("/dev/tellstick"), CFGF_NONE),
|
||||
CFG_STR(const_cast<char *>("ignoreControllerConfirmation"), const_cast<char *>("false"), CFGF_NONE),
|
||||
CFG_SEC(const_cast<char *>("device"), device_opts, CFGF_MULTI),
|
||||
CFG_SEC(const_cast<char *>("controller"), controller_opts, CFGF_MULTI),
|
||||
CFG_END()
|
||||
};
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//
|
||||
// C++ Implementation: telldussettingsconfuse
|
||||
//
|
||||
// Description:
|
||||
// Description:
|
||||
//
|
||||
//
|
||||
// Author: Micke Prag <micke.prag@telldus.se>, (C) 2008
|
||||
|
@ -30,7 +30,7 @@ public:
|
|||
CFStringRef userName;
|
||||
CFStringRef hostName;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
|
@ -60,22 +60,26 @@ std::wstring Settings::getSetting(const std::wstring &strName) const {
|
|||
/*
|
||||
* Return the number of stored devices
|
||||
*/
|
||||
int Settings::getNumberOfDevices(void) const {
|
||||
int Settings::getNumberOfNodes(Node type) const {
|
||||
CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName );
|
||||
if (!cfarray) return 0;
|
||||
CFIndex size = CFArrayGetCount( cfarray );
|
||||
int devices = 0;
|
||||
int nodes = 0;
|
||||
for (CFIndex k = 0; k < size; ++k) {
|
||||
CFStringRef key = (CFStringRef) CFArrayGetValueAtIndex(cfarray, k);
|
||||
if (CFStringHasPrefix( key, CFSTR("devices.") ) &&
|
||||
CFStringHasSuffix( key, CFSTR(".name") ) ) {
|
||||
devices++;
|
||||
if (!CFStringHasSuffix( key, CFSTR(".name") )) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return devices;
|
||||
if (type == Device && CFStringHasPrefix( key, CFSTR("devices.") )) {
|
||||
++nodes;
|
||||
} else if (type == Controller && CFStringHasPrefix( key, CFSTR("controllers.") )) {
|
||||
++nodes;
|
||||
}
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
int Settings::getDeviceId(int intDeviceIndex) const {
|
||||
int Settings::getNodeId(Node type, int intNodeIndex) const {
|
||||
CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName );
|
||||
if (!cfarray) return 0;
|
||||
CFIndex size = CFArrayGetCount( cfarray );
|
||||
|
@ -83,63 +87,73 @@ int Settings::getDeviceId(int intDeviceIndex) const {
|
|||
int id = 0;
|
||||
for (CFIndex k = 0; k < size; ++k) {
|
||||
CFStringRef key = (CFStringRef) CFArrayGetValueAtIndex(cfarray, k);
|
||||
if (CFStringHasPrefix( key, CFSTR("devices.") ) &&
|
||||
CFStringHasSuffix( key, CFSTR(".name") ) ) {
|
||||
|
||||
if (index == intDeviceIndex) {
|
||||
CFArrayRef split = CFStringCreateArrayBySeparatingStrings( 0, key, CFSTR(".") );
|
||||
if ( !split || CFArrayGetCount( split ) != 3 ) continue;
|
||||
|
||||
// This code crashes!
|
||||
//CFNumberRef cfid = (CFNumberRef) CFArrayGetValueAtIndex( split, 1 );
|
||||
//if (cfid)
|
||||
// CFNumberGetValue( cfid, kCFNumberIntType, &id);
|
||||
|
||||
CFStringRef cfid = (CFStringRef) CFArrayGetValueAtIndex( split, 1 );
|
||||
char *cp = NULL;
|
||||
CFIndex size = CFStringGetMaximumSizeForEncoding( CFStringGetLength( cfid ), kCFStringEncodingUTF8) + 1;
|
||||
cp = (char *)malloc(size);
|
||||
CFStringGetCString( cfid, cp, size, kCFStringEncodingUTF8 );
|
||||
char *newcp = (char *)realloc( cp, strlen(cp) + 1);
|
||||
if (newcp != NULL) {
|
||||
cp = newcp;
|
||||
id = atoi(cp);
|
||||
} else {
|
||||
//Should not happen
|
||||
id = 0;
|
||||
}
|
||||
free(cp);
|
||||
|
||||
CFRelease(key);
|
||||
CFRelease(split);
|
||||
CFRelease(cfid);
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
if (!CFStringHasSuffix( key, CFSTR(".name") )) {
|
||||
CFRelease( key );
|
||||
continue;
|
||||
}
|
||||
CFRelease( key );
|
||||
if ( type == Device && !CFStringHasPrefix(key, CFSTR("devices.")) ) {
|
||||
CFRelease( key );
|
||||
continue;
|
||||
}
|
||||
if ( type == Controller && !CFStringHasPrefix(key, CFSTR("controllers.")) ) {
|
||||
CFRelease( key );
|
||||
continue;
|
||||
}
|
||||
if (index == intNodeIndex) {
|
||||
CFArrayRef split = CFStringCreateArrayBySeparatingStrings( 0, key, CFSTR(".") );
|
||||
if ( !split || CFArrayGetCount( split ) != 3 ) continue;
|
||||
|
||||
// This code crashes!
|
||||
//CFNumberRef cfid = (CFNumberRef) CFArrayGetValueAtIndex( split, 1 );
|
||||
//if (cfid)
|
||||
// CFNumberGetValue( cfid, kCFNumberIntType, &id);
|
||||
|
||||
CFStringRef cfid = (CFStringRef) CFArrayGetValueAtIndex( split, 1 );
|
||||
char *cp = NULL;
|
||||
CFIndex size = CFStringGetMaximumSizeForEncoding( CFStringGetLength( cfid ), kCFStringEncodingUTF8) + 1;
|
||||
cp = (char *)malloc(size);
|
||||
CFStringGetCString( cfid, cp, size, kCFStringEncodingUTF8 );
|
||||
char *newcp = (char *)realloc( cp, strlen(cp) + 1);
|
||||
if (newcp != NULL) {
|
||||
cp = newcp;
|
||||
id = atoi(cp);
|
||||
} else {
|
||||
//Should not happen
|
||||
id = 0;
|
||||
}
|
||||
free(cp);
|
||||
|
||||
CFRelease(key);
|
||||
CFRelease(split);
|
||||
CFRelease(cfid);
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add a new device
|
||||
* Add a new node
|
||||
*/
|
||||
int Settings::addDevice() {
|
||||
int id = getNextDeviceId();
|
||||
setStringSetting( id, L"name", L"", false ); //Create a empty name so the device has an entry
|
||||
setStringSetting( id, L"model", L"", false );
|
||||
int Settings::addNode(Node type) {
|
||||
int id = getNextNodeId(type);
|
||||
setStringSetting( type, id, L"name", L"", false ); //Create a empty name so the node has an entry
|
||||
if (type == Device) {
|
||||
//Is there a reason we do this?
|
||||
setStringSetting( type, id, L"model", L"", false );
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get next available device id
|
||||
* Get next available node id
|
||||
*/
|
||||
int Settings::getNextDeviceId() const {
|
||||
int Settings::getNextNodeId(Node type) const {
|
||||
int id = 0, max = 0;
|
||||
int numberOfDevices = getNumberOfDevices();
|
||||
for( int i = 0; i < numberOfDevices; i++) {
|
||||
id = getDeviceId( i );
|
||||
int numberOfNodes = getNumberOfNodes(type);
|
||||
for( int i = 0; i < numberOfNodes; i++) {
|
||||
id = getNodeId( type, i );
|
||||
if (id > max) {
|
||||
max = id;
|
||||
}
|
||||
|
@ -151,9 +165,9 @@ int Settings::getNextDeviceId() const {
|
|||
/*
|
||||
* Remove a device
|
||||
*/
|
||||
int Settings::removeDevice(int intDeviceId){
|
||||
int Settings::removeNode(Node type, int intNodeId){
|
||||
int ret = TELLSTICK_ERROR_DEVICE_NOT_FOUND;
|
||||
CFStringRef filterKey = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d."), intDeviceId); // The key to search for
|
||||
CFStringRef filterKey = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d."), getNodeString(type).c_str(), intNodeId); // The key to search for
|
||||
|
||||
CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName );
|
||||
if (!cfarray) {
|
||||
|
@ -167,29 +181,29 @@ int Settings::removeDevice(int intDeviceId){
|
|||
ret = TELLSTICK_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CFPreferencesSynchronize( d->app_ID, d->userName, d->hostName );
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::wstring Settings::getStringSetting(int intDeviceId, const std::wstring &wname, bool parameter) const {
|
||||
std::wstring Settings::getStringSetting(Node type, int intNodeId, const std::wstring &wname, bool parameter) const {
|
||||
std::string name(TelldusCore::wideToString(wname));
|
||||
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
||||
|
||||
|
||||
CFStringRef key;
|
||||
if (parameter) {
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.parameters.%@"), intDeviceId, cfname);
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.parameters.%@"), getNodeString(type).c_str(), intNodeId, cfname);
|
||||
} else {
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.%@"), intDeviceId, cfname);
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.%@"), getNodeString(type).c_str(), intNodeId, cfname);
|
||||
}
|
||||
|
||||
|
||||
CFStringRef value;
|
||||
|
||||
|
||||
value = (CFStringRef)CFPreferencesCopyValue(key, d->app_ID, d->userName, d->hostName);
|
||||
if (!value) {
|
||||
return L"";
|
||||
}
|
||||
|
||||
|
||||
std::wstring retval;
|
||||
char *cp = NULL;
|
||||
CFIndex size = CFStringGetMaximumSizeForEncoding( CFStringGetLength( value ), kCFStringEncodingUTF8) + 1;
|
||||
|
@ -204,22 +218,22 @@ std::wstring Settings::getStringSetting(int intDeviceId, const std::wstring &wna
|
|||
retval = L"";
|
||||
}
|
||||
free(cp);
|
||||
|
||||
|
||||
CFRelease(value);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int Settings::setStringSetting(int intDeviceId, const std::wstring &wname, const std::wstring &wvalue, bool parameter) {
|
||||
int Settings::setStringSetting(Node type, int intNodeId, const std::wstring &wname, const std::wstring &wvalue, bool parameter) {
|
||||
std::string name(TelldusCore::wideToString(wname));
|
||||
std::string value(TelldusCore::wideToString(wvalue));
|
||||
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
||||
CFStringRef cfvalue = CFStringCreateWithCString( 0, value.c_str(), kCFStringEncodingUTF8 );
|
||||
|
||||
|
||||
CFStringRef key;
|
||||
if (parameter) {
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.parameters.%@"), intDeviceId, cfname);
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.parameters.%@"), getNodeString(type).c_str(), intNodeId, cfname);
|
||||
} else {
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.%@"), intDeviceId, cfname);
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.%@"), getNodeString(type).c_str(), intNodeId, cfname);
|
||||
}
|
||||
|
||||
CFPreferencesSetValue( key, cfvalue, d->app_ID, d->userName, d->hostName );
|
||||
|
@ -227,19 +241,19 @@ int Settings::setStringSetting(int intDeviceId, const std::wstring &wname, const
|
|||
return TELLSTICK_SUCCESS;
|
||||
}
|
||||
|
||||
int Settings::getIntSetting(int intDeviceId, const std::wstring &wname, bool parameter) const {
|
||||
int Settings::getIntSetting(Node type, int intNodeId, const std::wstring &wname, bool parameter) const {
|
||||
int retval = 0;
|
||||
std::string name(TelldusCore::wideToString(wname));
|
||||
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
||||
CFNumberRef cfvalue;
|
||||
|
||||
|
||||
CFStringRef key;
|
||||
if (parameter) {
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.parameters.%@"), intDeviceId, cfname);
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.parameters.%@"), getNodeString(type).c_str(), intNodeId, cfname);
|
||||
} else {
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.%@"), intDeviceId, cfname);
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.%@"), getNodeString(type).c_str(), intNodeId, cfname);
|
||||
}
|
||||
|
||||
|
||||
cfvalue = (CFNumberRef)CFPreferencesCopyValue(key, d->app_ID, d->userName, d->hostName);
|
||||
|
||||
// If the preference exists, use it.
|
||||
|
@ -254,23 +268,23 @@ int Settings::getIntSetting(int intDeviceId, const std::wstring &wname, bool par
|
|||
retval = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
int Settings::setIntSetting(int intDeviceId, const std::wstring &wname, int value, bool parameter) {
|
||||
int Settings::setIntSetting(Node type, int intNodeId, const std::wstring &wname, int value, bool parameter) {
|
||||
std::string name(TelldusCore::wideToString(wname));
|
||||
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
||||
CFNumberRef cfvalue = CFNumberCreate(NULL, kCFNumberIntType, &value);
|
||||
|
||||
|
||||
CFStringRef key;
|
||||
if (parameter) {
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.parameters.%@"), intDeviceId, cfname);
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.parameters.%@"), getNodeString(type).c_str(), intNodeId, cfname);
|
||||
} else {
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.%@"), intDeviceId, cfname);
|
||||
key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.%@"), getNodeString(type).c_str(), intNodeId, cfname);
|
||||
}
|
||||
|
||||
|
||||
CFPreferencesSetValue( key, cfvalue, d->app_ID, d->userName, d->hostName );
|
||||
CFPreferencesSynchronize( d->app_ID, d->userName, d->hostName );
|
||||
return TELLSTICK_SUCCESS;
|
||||
return TELLSTICK_SUCCESS;
|
||||
}
|
||||
|
|
|
@ -15,16 +15,24 @@ const int intMaxRegValueLength = 1000;
|
|||
class Settings::PrivateData {
|
||||
public:
|
||||
HKEY rootKey;
|
||||
std::wstring strRegPathDevice;
|
||||
std::wstring strRegPath;
|
||||
std::wstring strRegPath;
|
||||
std::wstring getNodePath(Settings::Node type);
|
||||
};
|
||||
|
||||
std::wstring Settings::PrivateData::getNodePath(Settings::Node type) {
|
||||
if (type == Settings::Device) {
|
||||
return L"SOFTWARE\\Telldus\\Devices\\";
|
||||
} else if (type == Settings::Controller) {
|
||||
return L"SOFTWARE\\Telldus\\Controllers\\";
|
||||
}
|
||||
return L"";
|
||||
}
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
Settings::Settings(void) {
|
||||
d = new PrivateData();
|
||||
d->strRegPathDevice = L"SOFTWARE\\Telldus\\Devices\\";
|
||||
d->strRegPath = L"SOFTWARE\\Telldus\\";
|
||||
d->rootKey = HKEY_LOCAL_MACHINE;
|
||||
}
|
||||
|
@ -39,41 +47,41 @@ Settings::~Settings(void) {
|
|||
/*
|
||||
* Return the number of stored devices
|
||||
*/
|
||||
int Settings::getNumberOfDevices(void) const {
|
||||
int Settings::getNumberOfNodes(Node type) const {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
|
||||
int intNumberOfDevices = 0;
|
||||
int intNumberOfNodes = 0;
|
||||
HKEY hk;
|
||||
|
||||
long lnExists = RegOpenKeyEx(d->rootKey, d->strRegPathDevice.c_str(), 0, KEY_QUERY_VALUE, &hk);
|
||||
|
||||
long lnExists = RegOpenKeyEx(d->rootKey, d->getNodePath(type).c_str(), 0, KEY_QUERY_VALUE, &hk);
|
||||
|
||||
if(lnExists == ERROR_SUCCESS){
|
||||
|
||||
std::wstring strNumSubKeys;
|
||||
DWORD dNumSubKeys;
|
||||
RegQueryInfoKey(hk, NULL, NULL, NULL, &dNumSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
|
||||
|
||||
intNumberOfDevices = (int)dNumSubKeys;
|
||||
intNumberOfNodes = (int)dNumSubKeys;
|
||||
|
||||
RegCloseKey(hk);
|
||||
}
|
||||
return intNumberOfDevices;
|
||||
return intNumberOfNodes;
|
||||
}
|
||||
|
||||
|
||||
int Settings::getDeviceId(int intDeviceIndex) const {
|
||||
int Settings::getNodeId(Node type, int intNodeIndex) const {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
|
||||
int intReturn = -1;
|
||||
HKEY hk;
|
||||
|
||||
long lnExists = RegOpenKeyEx(d->rootKey, d->strRegPathDevice.c_str(), 0, KEY_READ, &hk);
|
||||
long lnExists = RegOpenKeyEx(d->rootKey, d->getNodePath(type).c_str(), 0, KEY_READ, &hk);
|
||||
|
||||
if(lnExists == ERROR_SUCCESS){
|
||||
|
||||
wchar_t* Buff = new wchar_t[intMaxRegValueLength];
|
||||
DWORD size = intMaxRegValueLength;
|
||||
if (RegEnumKeyEx(hk, intDeviceIndex, (LPWSTR)Buff, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) {
|
||||
if (RegEnumKeyEx(hk, intNodeIndex, (LPWSTR)Buff, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) {
|
||||
intReturn = _wtoi(Buff);
|
||||
}
|
||||
|
||||
|
@ -84,43 +92,43 @@ int Settings::getDeviceId(int intDeviceIndex) const {
|
|||
}
|
||||
|
||||
/*
|
||||
* Add a new device
|
||||
* Add a new node
|
||||
*/
|
||||
int Settings::addDevice() {
|
||||
int Settings::addNode(Node type) {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
|
||||
int intDeviceId = -1;
|
||||
int intNodeId = -1;
|
||||
HKEY hk;
|
||||
|
||||
DWORD dwDisp;
|
||||
intDeviceId = getNextDeviceId();
|
||||
intNodeId = getNextNodeId(type);
|
||||
|
||||
std::wstring strCompleteRegPath = d->strRegPathDevice;
|
||||
strCompleteRegPath.append(TelldusCore::intToWstring(intDeviceId));
|
||||
std::wstring strCompleteRegPath = d->getNodePath(type);
|
||||
strCompleteRegPath.append(TelldusCore::intToWstring(intNodeId));
|
||||
|
||||
if (RegCreateKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hk, &dwDisp)) {
|
||||
//fail
|
||||
intDeviceId = -1;
|
||||
intNodeId = -1;
|
||||
}
|
||||
|
||||
RegCloseKey(hk);
|
||||
return intDeviceId;
|
||||
return intNodeId;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get next available device id
|
||||
*/
|
||||
int Settings::getNextDeviceId() const {
|
||||
int Settings::getNextNodeId(Node type) const {
|
||||
//Private, no locks needed
|
||||
int intReturn = -1;
|
||||
HKEY hk;
|
||||
DWORD dwDisp;
|
||||
|
||||
long lnExists = RegCreateKeyEx(d->rootKey, d->strRegPathDevice.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hk, &dwDisp); //create or open if already created
|
||||
long lnExists = RegCreateKeyEx(d->rootKey, d->getNodePath(type).c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hk, &dwDisp); //create or open if already created
|
||||
|
||||
if(lnExists == ERROR_SUCCESS){
|
||||
|
||||
DWORD dwLength;
|
||||
DWORD dwLength = sizeof(DWORD);
|
||||
DWORD nResult(0);
|
||||
|
||||
long lngStatus = RegQueryValueEx(hk, L"LastUsedId", NULL, NULL, reinterpret_cast<LPBYTE>(&nResult), &dwLength);
|
||||
|
@ -141,11 +149,11 @@ int Settings::getNextDeviceId() const {
|
|||
/*
|
||||
* Remove a device
|
||||
*/
|
||||
int Settings::removeDevice(int intDeviceId) {
|
||||
int Settings::removeNode(Node type, int intNodeId) {
|
||||
TelldusCore::MutexLocker locker(&mutex);
|
||||
|
||||
std::wstring strCompleteRegPath = d->strRegPathDevice;
|
||||
strCompleteRegPath.append(TelldusCore::intToWstring(intDeviceId));
|
||||
std::wstring strCompleteRegPath = d->getNodePath(type);
|
||||
strCompleteRegPath.append(TelldusCore::intToWstring(intNodeId));
|
||||
|
||||
long lngSuccess = RegDeleteKey(d->rootKey, strCompleteRegPath.c_str());
|
||||
|
||||
|
@ -184,12 +192,12 @@ std::wstring Settings::getSetting(const std::wstring &strName) const{
|
|||
return strReturn;
|
||||
}
|
||||
|
||||
std::wstring Settings::getStringSetting(int intDeviceId, const std::wstring &name, bool parameter) const {
|
||||
std::wstring Settings::getStringSetting(Node type, int intNodeId, const std::wstring &name, bool parameter) const {
|
||||
std::wstring strReturn;
|
||||
HKEY hk;
|
||||
|
||||
std::wstring strCompleteRegPath = d->strRegPathDevice;
|
||||
strCompleteRegPath.append(TelldusCore::intToWstring(intDeviceId));
|
||||
std::wstring strCompleteRegPath = d->getNodePath(type);
|
||||
strCompleteRegPath.append(TelldusCore::intToWstring(intNodeId));
|
||||
long lnExists = RegOpenKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, KEY_QUERY_VALUE, &hk);
|
||||
|
||||
if(lnExists == ERROR_SUCCESS){
|
||||
|
@ -212,14 +220,14 @@ std::wstring Settings::getStringSetting(int intDeviceId, const std::wstring &nam
|
|||
return strReturn;
|
||||
}
|
||||
|
||||
int Settings::setStringSetting(int intDeviceId, const std::wstring &name, const std::wstring &value, bool parameter) {
|
||||
int Settings::setStringSetting(Node type, int intNodeId, const std::wstring &name, const std::wstring &value, bool parameter) {
|
||||
|
||||
HKEY hk;
|
||||
int ret = TELLSTICK_SUCCESS;
|
||||
|
||||
std::wstring bla = TelldusCore::intToWstring(intDeviceId);
|
||||
std::wstring strCompleteRegPath = d->strRegPathDevice;
|
||||
strCompleteRegPath.append(bla);
|
||||
std::wstring strNodeId = TelldusCore::intToWstring(intNodeId);
|
||||
std::wstring strCompleteRegPath = d->getNodePath(type);
|
||||
strCompleteRegPath.append(strNodeId);
|
||||
long lnExists = RegOpenKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, KEY_WRITE, &hk);
|
||||
|
||||
if (lnExists == ERROR_SUCCESS){
|
||||
|
@ -234,23 +242,23 @@ int Settings::setStringSetting(int intDeviceId, const std::wstring &name, const
|
|||
|
||||
}
|
||||
|
||||
int Settings::getIntSetting(int intDeviceId, const std::wstring &name, bool parameter) const {
|
||||
int Settings::getIntSetting(Node type, int intNodeId, const std::wstring &name, bool parameter) const {
|
||||
int intReturn = 0;
|
||||
|
||||
std::wstring strSetting = getStringSetting(intDeviceId, name, parameter);
|
||||
std::wstring strSetting = getStringSetting(type, intNodeId, name, parameter);
|
||||
if (strSetting.length()) {
|
||||
intReturn = (int)strSetting[0];
|
||||
intReturn = (int)strSetting[0]; //TODO: do real conversion instead
|
||||
}
|
||||
|
||||
return intReturn;
|
||||
}
|
||||
|
||||
int Settings::setIntSetting(int intDeviceId, const std::wstring &name, int value, bool parameter) {
|
||||
int Settings::setIntSetting(Node type, int intNodeId, const std::wstring &name, int value, bool parameter) {
|
||||
int intReturn = TELLSTICK_ERROR_UNKNOWN;
|
||||
HKEY hk;
|
||||
|
||||
std::wstring strCompleteRegPath = d->strRegPathDevice;
|
||||
strCompleteRegPath.append(TelldusCore::intToWstring(intDeviceId));
|
||||
std::wstring strCompleteRegPath = d->getNodePath(type);
|
||||
strCompleteRegPath.append(TelldusCore::intToWstring(intNodeId));
|
||||
long lnExists = RegOpenKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, KEY_WRITE, &hk);
|
||||
if (lnExists == ERROR_SUCCESS) {
|
||||
DWORD dwVal = value;
|
||||
|
|
|
@ -24,10 +24,9 @@ public:
|
|||
|
||||
class TellStick : public Controller, public TelldusCore::Thread {
|
||||
public:
|
||||
TellStick(int controllerId, TelldusCore::Event *event, const TellStickDescriptor &d);
|
||||
TellStick(int controllerId, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent, const TellStickDescriptor &d);
|
||||
virtual ~TellStick();
|
||||
|
||||
virtual int firmwareVersion();
|
||||
virtual int pid() const;
|
||||
virtual int vid() const;
|
||||
virtual std::string serial() const;
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
class TellStick::PrivateData {
|
||||
public:
|
||||
bool open, running, ignoreControllerConfirmation;
|
||||
int vid, pid, fwVersion;
|
||||
int vid, pid;
|
||||
std::string serial, message;
|
||||
FT_HANDLE ftHandle;
|
||||
TelldusCore::Mutex mutex;
|
||||
|
@ -40,8 +40,8 @@ public:
|
|||
#endif
|
||||
};
|
||||
|
||||
TellStick::TellStick(int controllerId, TelldusCore::Event *event, const TellStickDescriptor &td )
|
||||
:Controller(controllerId, event)
|
||||
TellStick::TellStick(int controllerId, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent, const TellStickDescriptor &td )
|
||||
:Controller(controllerId, event, updateEvent)
|
||||
{
|
||||
d = new PrivateData;
|
||||
#ifdef _WINDOWS
|
||||
|
@ -54,7 +54,6 @@ TellStick::TellStick(int controllerId, TelldusCore::Event *event, const TellStic
|
|||
d->running = false;
|
||||
d->vid = td.vid;
|
||||
d->pid = td.pid;
|
||||
d->fwVersion = 0;
|
||||
d->serial = td.serial;
|
||||
Settings set;
|
||||
d->ignoreControllerConfirmation = set.getSetting(L"ignoreControllerConfirmation")==L"true";
|
||||
|
@ -109,10 +108,6 @@ void TellStick::setBaud( int baud ) {
|
|||
FT_SetBaudRate(d->ftHandle, baud);
|
||||
}
|
||||
|
||||
int TellStick::firmwareVersion() {
|
||||
return d->fwVersion;
|
||||
}
|
||||
|
||||
int TellStick::pid() const {
|
||||
return d->pid;
|
||||
}
|
||||
|
@ -148,7 +143,7 @@ void TellStick::processData( const std::string &data ) {
|
|||
continue;
|
||||
} else if (data[i] == 10) { // \n found
|
||||
if (d->message.substr(0,2).compare("+V") == 0) {
|
||||
d->fwVersion = TelldusCore::charToInteger(d->message.substr(2).c_str());
|
||||
setFirmwareVersion(TelldusCore::charToInteger(d->message.substr(2).c_str()));
|
||||
} else if (d->message.substr(0,2).compare("+R") == 0) {
|
||||
this->publishData(d->message.substr(2));
|
||||
} else if(d->message.substr(0,2).compare("+W") == 0) {
|
||||
|
|
|
@ -34,7 +34,7 @@ typedef int DWORD;
|
|||
class TellStick::PrivateData {
|
||||
public:
|
||||
bool open, ignoreControllerConfirmation;
|
||||
int vid, pid, fwVersion;
|
||||
int vid, pid;
|
||||
std::string serial, message;
|
||||
ftdi_context ftHandle;
|
||||
EVENT_HANDLE eh;
|
||||
|
@ -42,14 +42,13 @@ public:
|
|||
TelldusCore::Mutex mutex;
|
||||
};
|
||||
|
||||
TellStick::TellStick(int controllerId, TelldusCore::Event *event, const TellStickDescriptor &td )
|
||||
:Controller(controllerId, event)
|
||||
TellStick::TellStick(int controllerId, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent, const TellStickDescriptor &td )
|
||||
:Controller(controllerId, event, updateEvent)
|
||||
{
|
||||
d = new PrivateData;
|
||||
d->open = false;
|
||||
d->vid = td.vid;
|
||||
d->pid = td.pid;
|
||||
d->fwVersion = 0;
|
||||
d->serial = td.serial;
|
||||
d->running = false;
|
||||
|
||||
|
@ -96,10 +95,6 @@ TellStick::~TellStick() {
|
|||
delete d;
|
||||
}
|
||||
|
||||
int TellStick::firmwareVersion() {
|
||||
return d->fwVersion;
|
||||
}
|
||||
|
||||
int TellStick::pid() const {
|
||||
return d->pid;
|
||||
}
|
||||
|
@ -135,7 +130,7 @@ void TellStick::processData( const std::string &data ) {
|
|||
continue;
|
||||
} else if (data[i] == 10) { // \n found
|
||||
if (d->message.substr(0,2).compare("+V") == 0) {
|
||||
d->fwVersion = TelldusCore::charToInteger(d->message.substr(2).c_str());
|
||||
setFirmwareVersion(TelldusCore::charToInteger(d->message.substr(2).c_str()));
|
||||
} else if (d->message.substr(0,2).compare("+R") == 0) {
|
||||
this->publishData(d->message.substr(2));
|
||||
} else if(d->message.substr(0,2).compare("+W") == 0) {
|
||||
|
|
|
@ -64,10 +64,10 @@ void TelldusMain::start(void) {
|
|||
supervisor.setInterval(60); //Once every minute
|
||||
supervisor.start();
|
||||
|
||||
ControllerManager controllerManager(dataEvent.get());
|
||||
EventUpdateManager eventUpdateManager;
|
||||
TelldusCore::EventRef deviceUpdateEvent = eventUpdateManager.retrieveUpdateEvent();
|
||||
eventUpdateManager.start();
|
||||
ControllerManager controllerManager(dataEvent, deviceUpdateEvent);
|
||||
DeviceManager deviceManager(&controllerManager, deviceUpdateEvent);
|
||||
|
||||
ConnectionListener clientListener(L"TelldusClient", clientEvent);
|
||||
|
@ -91,7 +91,7 @@ void TelldusMain::start(void) {
|
|||
TelldusCore::EventDataRef eventDataRef = clientEvent->takeSignal();
|
||||
ConnectionListenerEventData *data = reinterpret_cast<ConnectionListenerEventData*>(eventDataRef.get());
|
||||
if (data) {
|
||||
ClientCommunicationHandler *clientCommunication = new ClientCommunicationHandler(data->socket, handlerEvent, &deviceManager, deviceUpdateEvent);
|
||||
ClientCommunicationHandler *clientCommunication = new ClientCommunicationHandler(data->socket, handlerEvent, &deviceManager, deviceUpdateEvent, &controllerManager);
|
||||
clientCommunication->start();
|
||||
clientCommunicationHandlerList.push_back(clientCommunication);
|
||||
}
|
||||
|
|
|
@ -8,11 +8,12 @@ IF (BUILD_LIBTELLDUS-GUI)
|
|||
SET(BUILD_PLUGIN_SYSTRAY TRUE CACHE BOOL "Build plugin 'Systray'")
|
||||
ENDIF (BUILD_LIBTELLDUS-GUI)
|
||||
|
||||
SET(BUILD_PLUGIN_CONTROLLERS FALSE CACHE BOOL "Build plugin 'Controllers admin plugin'")
|
||||
SET(BUILD_PLUGIN_DBUS FALSE CACHE BOOL "Build plugin 'DBus'")
|
||||
SET(BUILD_PLUGIN_LIVE FALSE CACHE BOOL "Build plugin 'Telldus Live!'")
|
||||
SET(BUILD_PLUGIN_XPL FALSE CACHE BOOL "Build plugin 'xPL'")
|
||||
SET(BUILD_PLUGIN_SCHEDULERGUISIMPLE FALSE CACHE BOOL "Build plugin 'Simple Scheduler GUI'")
|
||||
SET(BUILD_PLUGIN_SENSORS TRUE CACHE BOOL "Build plugin 'Sensors'")
|
||||
SET(BUILD_PLUGIN_XPL FALSE CACHE BOOL "Build plugin 'xPL'")
|
||||
|
||||
ADD_SUBDIRECTORY(telldus)
|
||||
|
||||
|
@ -28,6 +29,10 @@ IF(BUILD_PLUGIN_SYSTRAY)
|
|||
ADD_SUBDIRECTORY(Systray)
|
||||
ENDIF(BUILD_PLUGIN_SYSTRAY)
|
||||
|
||||
IF(BUILD_PLUGIN_CONTROLLERS)
|
||||
ADD_SUBDIRECTORY(Controllers)
|
||||
ENDIF()
|
||||
|
||||
IF(BUILD_PLUGIN_DBUS)
|
||||
ADD_SUBDIRECTORY(DBus)
|
||||
ENDIF(BUILD_PLUGIN_DBUS)
|
||||
|
|
42
telldus-gui/Plugins/Controllers/CMakeLists.txt
Normal file
42
telldus-gui/Plugins/Controllers/CMakeLists.txt
Normal file
|
@ -0,0 +1,42 @@
|
|||
SET(REQUIRE_PLUGIN_QML TRUE PARENT_SCOPE)
|
||||
#SET(REQUIRE_PLUGIN_SETTINGS TRUE PARENT_SCOPE)
|
||||
|
||||
SET(QT_USE_QTDECLARATIVE TRUE)
|
||||
|
||||
SET( Plugin_NAME "Controllers" )
|
||||
|
||||
|
||||
SET( Plugin_SRCS
|
||||
controller.cpp
|
||||
controllerlist.cpp
|
||||
controllersplugin.cpp
|
||||
)
|
||||
|
||||
SET( Plugin_HDRS
|
||||
controllersplugin.h
|
||||
)
|
||||
|
||||
SET( Plugin_MOC_HDRS
|
||||
controller.h
|
||||
controllerlist.h
|
||||
)
|
||||
|
||||
SET( Plugin_PATH "com.telldus.controllers" )
|
||||
|
||||
SET( Plugin_EXTRA
|
||||
btn_action_remove.png
|
||||
ControllerView.qml
|
||||
header_bg.png
|
||||
HeaderTitle.qml
|
||||
icon.png
|
||||
main.qml
|
||||
qmldir
|
||||
row_bg.png
|
||||
tellstick.png
|
||||
tellstick_duo.png
|
||||
)
|
||||
|
||||
FIND_PACKAGE(TelldusCore REQUIRED)
|
||||
SET( Plugin_LIBRARIES ${TELLDUSCORE_LIBRARY} )
|
||||
|
||||
INCLUDE( ../TelldusCenterPlugin.cmake NO_POLICY_SCOPE )
|
96
telldus-gui/Plugins/Controllers/ControllerView.qml
Normal file
96
telldus-gui/Plugins/Controllers/ControllerView.qml
Normal file
|
@ -0,0 +1,96 @@
|
|||
import QtQuick 1.1
|
||||
import QtDesktop 0.1
|
||||
|
||||
BorderImage {
|
||||
source: "row_bg.png"
|
||||
border.left: 5; border.top: 5
|
||||
border.right: 5; border.bottom: 5
|
||||
width: parent.width
|
||||
height: content.height + content.anchors.margins*2
|
||||
|
||||
Item {
|
||||
id: content
|
||||
anchors.top: parent.top
|
||||
anchors.left: parent.left
|
||||
anchors.margins: 5
|
||||
height: childrenRect.height
|
||||
width: childrenRect.width
|
||||
|
||||
Row {
|
||||
spacing: 10
|
||||
Image {
|
||||
source: icon(controller.type)
|
||||
width: 50
|
||||
smooth: true
|
||||
fillMode: Image.PreserveAspectFit
|
||||
opacity: controller.available ? 1 : 0.5
|
||||
}
|
||||
|
||||
Column {
|
||||
Text {
|
||||
color: "#004275"
|
||||
text: productName(controller.type)
|
||||
font.pixelSize: 15
|
||||
}
|
||||
TextField {
|
||||
//id: nameEdit
|
||||
text: controller.name;
|
||||
placeholderText: 'Enter a name for this controller'
|
||||
onTextChanged: controller.name = text
|
||||
}
|
||||
}
|
||||
Loader {
|
||||
sourceComponent: tellstick
|
||||
}
|
||||
Image {
|
||||
source: "btn_action_remove.png"
|
||||
visible: !controller.available
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
onClicked: controller.tryRemove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Component {
|
||||
id: tellstick
|
||||
Grid {
|
||||
spacing: 3
|
||||
columns: 2
|
||||
Text {
|
||||
color: "#004275"
|
||||
text: "Serial:"
|
||||
}
|
||||
Text {
|
||||
color: "#004275"
|
||||
text: controller.serial
|
||||
}
|
||||
Text {
|
||||
color: "#004275"
|
||||
text: "Firmware version:"
|
||||
}
|
||||
Text {
|
||||
color: "#004275"
|
||||
text: controller.firmware
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function icon(type) {
|
||||
if (type == 1) {
|
||||
return "tellstick.png";
|
||||
} else if (type == 2) {
|
||||
return "tellstick_duo.png";
|
||||
}
|
||||
return "tellstick.png";
|
||||
}
|
||||
function productName(type) {
|
||||
if (type == 1) {
|
||||
return "TellStick";
|
||||
} else if (type == 2) {
|
||||
return "TellStick Duo";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
10
telldus-gui/Plugins/Controllers/HeaderTitle.qml
Normal file
10
telldus-gui/Plugins/Controllers/HeaderTitle.qml
Normal file
|
@ -0,0 +1,10 @@
|
|||
import Qt 4.7
|
||||
|
||||
Text {
|
||||
id: headerTitle
|
||||
text: "Name"
|
||||
color: "white"
|
||||
font.weight: Font.Bold
|
||||
height: parent.height
|
||||
verticalAlignment: Text.AlignVCenter
|
||||
}
|
25
telldus-gui/Plugins/Controllers/__init__.js
Normal file
25
telldus-gui/Plugins/Controllers/__init__.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
/** Sensors **/
|
||||
__setupPackage__( __extension__ );
|
||||
|
||||
__postInit__ = function() {
|
||||
application.allDoneLoading.connect( com.telldus.controllers.init );
|
||||
}
|
||||
|
||||
com.telldus.controllers = function() {
|
||||
var view = null;
|
||||
|
||||
function init() {
|
||||
view = new com.telldus.qml.view({});
|
||||
view.setProperty('controllerModel', com.telldus.controllers.list);
|
||||
view.load("main.qml");
|
||||
view.sizeRootObjectToView(true);
|
||||
application.configuration.addPage('Controllers', view, 'icon.png');
|
||||
com.telldus.controllers.list.changed.connect(application.configuration.valueChanged)
|
||||
application.configuration.save.connect(com.telldus.controllers.list.save)
|
||||
}
|
||||
|
||||
return { //Public functions
|
||||
init:init
|
||||
}
|
||||
|
||||
}();
|
BIN
telldus-gui/Plugins/Controllers/btn_action_remove.png
Normal file
BIN
telldus-gui/Plugins/Controllers/btn_action_remove.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
102
telldus-gui/Plugins/Controllers/controller.cpp
Normal file
102
telldus-gui/Plugins/Controllers/controller.cpp
Normal file
|
@ -0,0 +1,102 @@
|
|||
#include "controller.h"
|
||||
#include <telldus-core.h>
|
||||
#include <QMessageBox>
|
||||
#include <QDebug>
|
||||
|
||||
class Controller::PrivateData {
|
||||
public:
|
||||
bool available, nameChanged;
|
||||
int id, type;
|
||||
QString name, serial, firmware;
|
||||
};
|
||||
|
||||
Controller::Controller(int id, int type, const QString &name, QObject *parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
d = new PrivateData;
|
||||
d->id = id;
|
||||
d->type = type;
|
||||
d->available = false;
|
||||
d->nameChanged = false;
|
||||
d->name = name;
|
||||
|
||||
const int DATA_LENGTH = 255;
|
||||
char buff[DATA_LENGTH];
|
||||
if (tdControllerValue(id, "serial", buff, DATA_LENGTH) == TELLSTICK_SUCCESS) {
|
||||
d->serial = QString::fromUtf8(buff);
|
||||
}
|
||||
if (tdControllerValue(id, "firmware", buff, DATA_LENGTH) == TELLSTICK_SUCCESS) {
|
||||
d->firmware = QString::fromUtf8(buff);
|
||||
}
|
||||
}
|
||||
|
||||
Controller::~Controller() {
|
||||
delete d;
|
||||
}
|
||||
|
||||
bool Controller::available() const {
|
||||
return d->available;
|
||||
}
|
||||
|
||||
void Controller::setAvailable(bool available) {
|
||||
d->available = available;
|
||||
emit availableChanged();
|
||||
emit firmwareChanged();
|
||||
}
|
||||
|
||||
QString Controller::firmware() const {
|
||||
if (!d->available) {
|
||||
return "?";
|
||||
}
|
||||
return d->firmware;
|
||||
}
|
||||
|
||||
void Controller::setFirmware(const QString &version) {
|
||||
d->firmware = version;
|
||||
emit firmwareChanged();
|
||||
}
|
||||
|
||||
int Controller::id() const {
|
||||
return d->id;
|
||||
}
|
||||
|
||||
QString Controller::name() const {
|
||||
return d->name;
|
||||
}
|
||||
|
||||
void Controller::setName(const QString &name) {
|
||||
if (name == d->name) {
|
||||
return;
|
||||
}
|
||||
d->nameChanged = true;
|
||||
d->name = name;
|
||||
emit nameChanged();
|
||||
}
|
||||
|
||||
void Controller::save() {
|
||||
if (d->nameChanged) {
|
||||
tdSetControllerValue(d->id, "name", d->name.toUtf8());
|
||||
d->nameChanged = false;
|
||||
}
|
||||
}
|
||||
|
||||
QString Controller::serial() const {
|
||||
return d->serial;
|
||||
}
|
||||
|
||||
void Controller::tryRemove() {
|
||||
QMessageBox msgBox;
|
||||
msgBox.setText( tr("Are you sure you want to remove the selected controller?") );
|
||||
msgBox.setInformativeText( tr("If you connect it again at a later point it will be readded automatically.") );
|
||||
msgBox.setIcon( QMessageBox::Warning );
|
||||
msgBox.setStandardButtons( QMessageBox::Yes | QMessageBox::No );
|
||||
msgBox.setDefaultButton( QMessageBox::No );
|
||||
if ( msgBox.exec() == QMessageBox::Yes) {
|
||||
tdRemoveController(d->id);
|
||||
}
|
||||
}
|
||||
|
||||
int Controller::type() const {
|
||||
return d->type;
|
||||
}
|
||||
|
55
telldus-gui/Plugins/Controllers/controller.h
Normal file
55
telldus-gui/Plugins/Controllers/controller.h
Normal file
|
@ -0,0 +1,55 @@
|
|||
#ifndef CONTROLLER_H
|
||||
#define CONTROLLER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QMetaType>
|
||||
|
||||
class Controller : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool available READ available NOTIFY availableChanged)
|
||||
Q_PROPERTY(QString firmware READ firmware NOTIFY firmwareChanged)
|
||||
Q_PROPERTY(int id READ id NOTIFY idChanged)
|
||||
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
|
||||
Q_PROPERTY(QString serial READ serial NOTIFY serialChanged)
|
||||
Q_PROPERTY(int type READ type NOTIFY typeChanged())
|
||||
|
||||
public:
|
||||
explicit Controller(int id = 0, int type = 1, const QString &name = "", QObject *parent = 0);
|
||||
~Controller();
|
||||
|
||||
bool available() const;
|
||||
void setAvailable(bool available);
|
||||
|
||||
QString firmware() const;
|
||||
void setFirmware(const QString &version);
|
||||
|
||||
int id() const;
|
||||
|
||||
QString name() const;
|
||||
void setName(const QString &name);
|
||||
|
||||
void save();
|
||||
|
||||
QString serial() const;
|
||||
|
||||
Q_INVOKABLE void tryRemove();
|
||||
|
||||
int type() const;
|
||||
|
||||
signals:
|
||||
void availableChanged();
|
||||
void firmwareChanged();
|
||||
void idChanged();
|
||||
void nameChanged();
|
||||
void serialChanged();
|
||||
void typeChanged();
|
||||
|
||||
private:
|
||||
class PrivateData;
|
||||
PrivateData *d;
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(Controller*)
|
||||
|
||||
#endif // CONTROLLER_H
|
102
telldus-gui/Plugins/Controllers/controllerlist.cpp
Normal file
102
telldus-gui/Plugins/Controllers/controllerlist.cpp
Normal file
|
@ -0,0 +1,102 @@
|
|||
#include "controllerlist.h"
|
||||
#include "controller.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
class ControllerList::PrivateData {
|
||||
public:
|
||||
QList<Controller *> list;
|
||||
int callbackId;
|
||||
};
|
||||
|
||||
ControllerList::ControllerList(QObject *parent) :
|
||||
QAbstractListModel(parent)
|
||||
{
|
||||
d = new PrivateData;
|
||||
|
||||
QHash<int, QByteArray> roles;
|
||||
roles[Qt::UserRole+1] = "controller";
|
||||
setRoleNames(roles);
|
||||
|
||||
connect(this, SIGNAL(controllerEventSignal(int,int,int,QString)), this, SLOT(controllerEventSlot(int,int,int,QString)), Qt::QueuedConnection);
|
||||
d->callbackId = tdRegisterControllerEvent(&ControllerList::controllerEvent, this);
|
||||
|
||||
const int DATA_LENGTH = 255;
|
||||
char name[DATA_LENGTH];
|
||||
int available, controllerId, type;
|
||||
while(tdController(&controllerId, &type, name, DATA_LENGTH, &available) == TELLSTICK_SUCCESS) {
|
||||
Controller *controller = new Controller(controllerId, type, QString::fromUtf8(name), this);
|
||||
controller->setAvailable(available);
|
||||
connect(controller, SIGNAL(nameChanged()), this, SIGNAL(changed()));
|
||||
d->list.append(controller);
|
||||
}
|
||||
}
|
||||
|
||||
ControllerList::~ControllerList() {
|
||||
tdUnregisterCallback(d->callbackId);
|
||||
delete d;
|
||||
}
|
||||
|
||||
QVariant ControllerList::data(const QModelIndex &index, int role) const {
|
||||
return QVariant::fromValue(d->list.at(index.row()));
|
||||
}
|
||||
|
||||
int ControllerList::rowCount(const QModelIndex &parent) const {
|
||||
return d->list.size();
|
||||
}
|
||||
|
||||
void ControllerList::save() {
|
||||
for(int i = 0; i < d->list.size(); ++i) {
|
||||
d->list.at(i)->save();
|
||||
}
|
||||
}
|
||||
|
||||
void ControllerList::controllerEventSlot(int controllerId, int changeEvent, int changeType, const QString &newValue) {
|
||||
if (changeEvent == TELLSTICK_DEVICE_STATE_CHANGED) {
|
||||
for(int i = 0; i < d->list.size(); ++i) {
|
||||
if (d->list.at(i)->id() != controllerId) {
|
||||
continue;
|
||||
}
|
||||
if (changeType == TELLSTICK_CHANGE_AVAILABLE) {
|
||||
if (newValue == "1") {
|
||||
d->list.at(i)->setAvailable(true);
|
||||
} else if (newValue == "0") {
|
||||
d->list.at(i)->setAvailable(false);
|
||||
}
|
||||
} else if (changeType == TELLSTICK_CHANGE_FIRMWARE) {
|
||||
d->list.at(i)->setFirmware(newValue);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (changeEvent == TELLSTICK_DEVICE_ADDED) {
|
||||
beginInsertRows( QModelIndex(), d->list.size(), d->list.size() );
|
||||
Controller *controller = new Controller(controllerId, changeType, "", this);
|
||||
controller->setAvailable(true);
|
||||
connect(controller, SIGNAL(nameChanged()), this, SIGNAL(changed()));
|
||||
d->list.append(controller);
|
||||
endInsertRows();
|
||||
return;
|
||||
}
|
||||
|
||||
if (changeEvent == TELLSTICK_DEVICE_REMOVED) {
|
||||
for(int i = 0; i < d->list.size(); ++i) {
|
||||
if (d->list.at(i)->id() != controllerId) {
|
||||
continue;
|
||||
}
|
||||
beginRemoveRows( QModelIndex(), i, i );
|
||||
d->list.takeAt(i);
|
||||
endRemoveRows();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void WINAPI ControllerList::controllerEvent( int controllerId, int changeEvent, int changeType, const char *newValue, int callbackId, void *context) {
|
||||
ControllerList *controllerList = reinterpret_cast<ControllerList *>(context);
|
||||
if (!controllerList) {
|
||||
return;
|
||||
}
|
||||
emit controllerList->controllerEventSignal(controllerId, changeEvent, changeType, QString::fromUtf8(newValue));
|
||||
}
|
36
telldus-gui/Plugins/Controllers/controllerlist.h
Normal file
36
telldus-gui/Plugins/Controllers/controllerlist.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
#ifndef CONTROLLERLIST_H
|
||||
#define CONTROLLERLIST_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QScriptValue>
|
||||
#include <telldus-core.h>
|
||||
|
||||
class ControllerList : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(int length READ rowCount)
|
||||
public:
|
||||
explicit ControllerList(QObject *parent = 0);
|
||||
~ControllerList();
|
||||
|
||||
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
||||
|
||||
signals:
|
||||
void changed();
|
||||
void controllerEventSignal(int controllerId, int changeEvent, int changeType, const QString &newValue);
|
||||
|
||||
public slots:
|
||||
void save();
|
||||
|
||||
private slots:
|
||||
void controllerEventSlot(int controllerId, int changeEvent, int changeType, const QString &newValue);
|
||||
|
||||
private:
|
||||
static void WINAPI controllerEvent( int controllerId, int changeEvent, int changeType, const char *newValue, int callbackId, void *context);
|
||||
class PrivateData;
|
||||
PrivateData *d;
|
||||
|
||||
};
|
||||
|
||||
#endif // CONTROLLERLIST_H
|
30
telldus-gui/Plugins/Controllers/controllersplugin.cpp
Normal file
30
telldus-gui/Plugins/Controllers/controllersplugin.cpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include "controllersplugin.h"
|
||||
#include "controllerlist.h"
|
||||
#include "controller.h"
|
||||
#include <QScriptEngine>
|
||||
#include <QtDeclarative>
|
||||
|
||||
|
||||
ControllersPlugin::ControllersPlugin ( QObject * parent )
|
||||
:QScriptExtensionPlugin( parent )
|
||||
{
|
||||
}
|
||||
|
||||
ControllersPlugin::~ControllersPlugin() {
|
||||
}
|
||||
|
||||
void ControllersPlugin::initialize ( const QString & key, QScriptEngine * engine ) {
|
||||
if (key == "com.telldus.controllers") {
|
||||
qmlRegisterType<Controller>("Telldus", 1, 0, "Controller");
|
||||
|
||||
QScriptValue qml = engine->globalObject().property("com").property("telldus").property("controllers");
|
||||
QScriptValue list = engine->newQObject(new ControllerList(), QScriptEngine::ScriptOwnership);
|
||||
qml.setProperty("list", list);
|
||||
}
|
||||
}
|
||||
|
||||
QStringList ControllersPlugin::keys () const {
|
||||
return QStringList() << "com.telldus.controllers";
|
||||
}
|
||||
|
||||
Q_EXPORT_PLUGIN2(ControllersInterface, ControllersPlugin)
|
16
telldus-gui/Plugins/Controllers/controllersplugin.h
Normal file
16
telldus-gui/Plugins/Controllers/controllersplugin.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef CONTROLLERSPLUGIN_H
|
||||
#define CONTROLLERSPLUGIN_H
|
||||
|
||||
#include <QScriptExtensionPlugin>
|
||||
|
||||
class ControllersPlugin : public QScriptExtensionPlugin {
|
||||
public:
|
||||
ControllersPlugin ( QObject * parent = 0 );
|
||||
~ControllersPlugin ();
|
||||
|
||||
virtual void initialize ( const QString & key, QScriptEngine * engine );
|
||||
virtual QStringList keys () const;
|
||||
};
|
||||
|
||||
|
||||
#endif // CONTROLLERSPLUGIN_H
|
BIN
telldus-gui/Plugins/Controllers/header_bg.png
Normal file
BIN
telldus-gui/Plugins/Controllers/header_bg.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 526 B |
BIN
telldus-gui/Plugins/Controllers/icon.png
Normal file
BIN
telldus-gui/Plugins/Controllers/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 754 B |
30
telldus-gui/Plugins/Controllers/main.qml
Normal file
30
telldus-gui/Plugins/Controllers/main.qml
Normal file
|
@ -0,0 +1,30 @@
|
|||
import QtQuick 1.1
|
||||
|
||||
//import QtDesktop 0.1
|
||||
|
||||
Item {
|
||||
width: 500 //Minimum width
|
||||
|
||||
Column {
|
||||
spacing: 1
|
||||
anchors.fill: parent
|
||||
|
||||
BorderImage {
|
||||
id: header
|
||||
source: "header_bg.png"
|
||||
width: parent.width; height: 40
|
||||
border.left: 5; border.top: 5
|
||||
border.right: 5; border.bottom: 5
|
||||
|
||||
HeaderTitle {
|
||||
text: "Controllers"
|
||||
anchors.left: parent.left
|
||||
anchors.leftMargin: 15
|
||||
}
|
||||
}
|
||||
Repeater {
|
||||
model: controllerModel
|
||||
delegate: ControllerView {}
|
||||
}
|
||||
}
|
||||
}
|
2
telldus-gui/Plugins/Controllers/qmldir
Normal file
2
telldus-gui/Plugins/Controllers/qmldir
Normal file
|
@ -0,0 +1,2 @@
|
|||
HeaderTitle 1.0 HeaderTitle.qml
|
||||
ControllerView 1.0 ControllerView.qml
|
BIN
telldus-gui/Plugins/Controllers/row_bg.png
Normal file
BIN
telldus-gui/Plugins/Controllers/row_bg.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 346 B |
BIN
telldus-gui/Plugins/Controllers/tellstick.png
Normal file
BIN
telldus-gui/Plugins/Controllers/tellstick.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
BIN
telldus-gui/Plugins/Controllers/tellstick_duo.png
Normal file
BIN
telldus-gui/Plugins/Controllers/tellstick_duo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
|
@ -69,3 +69,11 @@ void QMLView::setProperty( const QString & name, const QScriptValue &value ) {
|
|||
context->setContextProperty(name, value.toVariant());
|
||||
}
|
||||
}
|
||||
|
||||
void QMLView::sizeRootObjectToView(bool enable) {
|
||||
if (enable) {
|
||||
this->setResizeMode(QDeclarativeView::SizeRootObjectToView);
|
||||
} else {
|
||||
this->setResizeMode(QDeclarativeView::SizeViewToRootObject);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ signals:
|
|||
public slots:
|
||||
void load(const QString &url);
|
||||
void setProperty( const QString & name, const QScriptValue &value );
|
||||
void sizeRootObjectToView(bool enable);
|
||||
|
||||
private:
|
||||
class PrivateData;
|
||||
|
|
|
@ -136,6 +136,26 @@ QVariant TelldusCoreObject::sensorValue(const QString &protocol, const QString &
|
|||
return retval;
|
||||
}
|
||||
|
||||
QVariant TelldusCoreObject::controller() const {
|
||||
const int DATA_LENGTH = 255;
|
||||
char name[DATA_LENGTH];
|
||||
int controllerId = 0, available = 0, controllerType = 0;
|
||||
|
||||
if (tdController(&controllerId, &controllerType, name, DATA_LENGTH, &available) != TELLSTICK_SUCCESS) {
|
||||
qDebug() << "Return null";
|
||||
return 0;
|
||||
}
|
||||
|
||||
QVariantMap retval;
|
||||
|
||||
retval["id"] = controllerId;
|
||||
retval["type"] = controllerType;
|
||||
retval["name"] = name;
|
||||
retval["available"] = available;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
void TelldusCoreObject::triggerError(int deviceId, int errorId) {
|
||||
char *errorString = tdGetErrorString( errorId );
|
||||
QString message = QString::fromUtf8( errorString );
|
||||
|
|
|
@ -32,6 +32,8 @@ public slots:
|
|||
QVariant sensor() const;
|
||||
QVariant sensorValue(const QString &protocol, const QString &model, int id, int dataType) const;
|
||||
|
||||
QVariant controller() const;
|
||||
|
||||
int turnOn( int deviceId );
|
||||
int turnOff( int deviceId );
|
||||
int up( int deviceId );
|
||||
|
|
|
@ -87,6 +87,12 @@ QScriptValue ConfigurationDialog::addPage( const QString &name, const QString &f
|
|||
QWidget *widget = loader.load(&file, this);
|
||||
file.close();
|
||||
|
||||
return this->addPage(name, widget, icon);
|
||||
}
|
||||
|
||||
QScriptValue ConfigurationDialog::addPage(const QString &name, QWidget *widget, const QString &icon) {
|
||||
QDir dir = this->baseDir();
|
||||
|
||||
int index = d->stackedLayout->addWidget(widget);
|
||||
|
||||
QListWidgetItem *item = new QListWidgetItem(QIcon(dir.filePath(icon)), name, d->listWidget);
|
||||
|
|
|
@ -20,6 +20,7 @@ signals:
|
|||
|
||||
public slots:
|
||||
QScriptValue addPage( const QString &name, const QString &file, const QString &icon );
|
||||
QScriptValue addPage( const QString &name, QWidget *widget, const QString &icon);
|
||||
void open();
|
||||
void valueChanged();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue