commit
bdd95b8d78
52 changed files with 1524 additions and 737 deletions
|
@ -11,83 +11,61 @@
|
||||||
|
|
||||||
using namespace TelldusCore;
|
using namespace TelldusCore;
|
||||||
|
|
||||||
TDDeviceEventDispatcher::TDDeviceEventDispatcher(CallbackStruct<TDDeviceEvent> *data, int id, int m, const std::string &strD, TelldusCore::EventRef cbDone)
|
TDEventDispatcher::TDEventDispatcher(EventDataRef cbd, CallbackStruct *cb, EventRef cbDone)
|
||||||
:Thread(), d(data), deviceId(id), method(m), strData(strD), doneRunning(false), callbackExecuted(cbDone)
|
:Thread(), doneRunning(false), callbackData(cbd), callback(cb), callbackExecuted(cbDone)
|
||||||
{
|
{
|
||||||
this->startAndLock(&d->mutex);
|
this->startAndLock(&callback->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
TDDeviceEventDispatcher::~TDDeviceEventDispatcher() {
|
TDEventDispatcher::~TDEventDispatcher() {
|
||||||
this->wait();
|
this->wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TDDeviceEventDispatcher::done() const {
|
bool TDEventDispatcher::done() const {
|
||||||
return doneRunning;
|
return doneRunning;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TDDeviceEventDispatcher::run() {
|
void TDEventDispatcher::run() {
|
||||||
char *str = wrapStdString(strData);
|
this->fireEvent();
|
||||||
d->event(deviceId, method, strData.c_str(), d->id, d->context);
|
|
||||||
doneRunning = true;
|
doneRunning = true;
|
||||||
callbackExecuted->signal();
|
callbackExecuted->signal();
|
||||||
}
|
}
|
||||||
|
|
||||||
TDDeviceChangeEventDispatcher::TDDeviceChangeEventDispatcher(CallbackStruct<TDDeviceChangeEvent> *data, int id, int event, int type, TelldusCore::EventRef cbDone)
|
void TDEventDispatcher::fireEvent() {
|
||||||
:Thread(), d(data), deviceId(id), changeEvent(event), changeType(type), doneRunning(false), callbackExecuted(cbDone)
|
if (callback->type == CallbackStruct::DeviceEvent) {
|
||||||
{
|
DeviceEventCallbackData *data = dynamic_cast<DeviceEventCallbackData *>(callbackData.get());
|
||||||
this->startAndLock(&d->mutex);
|
if (!data) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
((TDDeviceEvent)callback->event)(data->deviceId, data->deviceState, data->deviceStateValue.c_str(), callback->id, callback->context);
|
||||||
|
|
||||||
TDDeviceChangeEventDispatcher::~TDDeviceChangeEventDispatcher() {
|
} else if (callback->type == CallbackStruct::DeviceChangeEvent) {
|
||||||
this->wait();
|
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 {
|
} else if (callback->type == CallbackStruct::RawDeviceEvent) {
|
||||||
return doneRunning;
|
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() {
|
} else if (callback->type == CallbackStruct::SensorEvent) {
|
||||||
d->event(deviceId, changeEvent, changeType, d->id, d->context);
|
SensorEventCallbackData *data = dynamic_cast<SensorEventCallbackData *>(callbackData.get());
|
||||||
doneRunning = true;
|
if (!data) {
|
||||||
callbackExecuted->signal();
|
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)
|
} else if (callback->type == CallbackStruct::ControllerEvent) {
|
||||||
:Thread(), d(data), controllerId(id), strData(strD), doneRunning(false), callbackExecuted(cbDone)
|
ControllerEventCallbackData *data = dynamic_cast<ControllerEventCallbackData *>(callbackData.get());
|
||||||
{
|
if (!data) {
|
||||||
this->startAndLock(&d->mutex);
|
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"
|
#include "telldus-core.h"
|
||||||
|
|
||||||
namespace TelldusCore {
|
namespace TelldusCore {
|
||||||
template <typename T> struct CallbackStruct {
|
|
||||||
|
/*template <typename T> struct CallbackStruct {
|
||||||
T event;
|
T event;
|
||||||
int id;
|
int id;
|
||||||
void *context;
|
void *context;
|
||||||
TelldusCore::Mutex mutex;
|
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:
|
public:
|
||||||
TDDeviceEventDispatcher(CallbackStruct<TDDeviceEvent> *data, int deviceId, int method, const std::string &strData, TelldusCore::EventRef cbDone);
|
DeviceEventCallbackData() : CallbackData(CallbackStruct::DeviceEvent) {}
|
||||||
virtual ~TDDeviceEventDispatcher();
|
int deviceId;
|
||||||
bool done() const;
|
int deviceState;
|
||||||
protected:
|
std::string deviceStateValue;
|
||||||
virtual void run();
|
|
||||||
bool doneRunning;
|
|
||||||
private:
|
|
||||||
CallbackStruct<TDDeviceEvent> *d;
|
|
||||||
int deviceId, method;
|
|
||||||
std::string strData;
|
|
||||||
TelldusCore::EventRef callbackExecuted;
|
|
||||||
};
|
};
|
||||||
class TDDeviceChangeEventDispatcher : public Thread {
|
class DeviceChangeEventCallbackData : public CallbackData {
|
||||||
public:
|
public:
|
||||||
TDDeviceChangeEventDispatcher(CallbackStruct<TDDeviceChangeEvent> *data, int deviceId, int changeEvent, int changeType, TelldusCore::EventRef cbDone);
|
DeviceChangeEventCallbackData() : CallbackData(CallbackStruct::DeviceChangeEvent) {}
|
||||||
virtual ~TDDeviceChangeEventDispatcher();
|
int deviceId;
|
||||||
bool done() const;
|
int changeEvent;
|
||||||
protected:
|
int changeType;
|
||||||
virtual void run();
|
|
||||||
bool doneRunning;
|
|
||||||
private:
|
|
||||||
CallbackStruct<TDDeviceChangeEvent> *d;
|
|
||||||
int deviceId, changeEvent, changeType;
|
|
||||||
TelldusCore::EventRef callbackExecuted;
|
|
||||||
};
|
};
|
||||||
class TDRawDeviceEventDispatcher : public Thread {
|
|
||||||
|
class RawDeviceEventCallbackData : public CallbackData {
|
||||||
public:
|
public:
|
||||||
TDRawDeviceEventDispatcher( CallbackStruct<TDRawDeviceEvent> *data, const std::string &strData, int controllerId, TelldusCore::EventRef cbDone);
|
RawDeviceEventCallbackData() : CallbackData(CallbackStruct::RawDeviceEvent) {}
|
||||||
virtual ~TDRawDeviceEventDispatcher();
|
std::string data;
|
||||||
bool done() const;
|
|
||||||
protected:
|
|
||||||
virtual void run();
|
|
||||||
bool doneRunning;
|
|
||||||
private:
|
|
||||||
CallbackStruct<TDRawDeviceEvent> *d;
|
|
||||||
int controllerId;
|
int controllerId;
|
||||||
std::string strData;
|
|
||||||
TelldusCore::EventRef callbackExecuted;
|
|
||||||
};
|
};
|
||||||
class TDSensorEventDispatcher : public Thread {
|
|
||||||
|
class SensorEventCallbackData : public CallbackData {
|
||||||
public:
|
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);
|
SensorEventCallbackData() : CallbackData(CallbackStruct::SensorEvent) {}
|
||||||
virtual ~TDSensorEventDispatcher();
|
|
||||||
bool done() const;
|
|
||||||
protected:
|
|
||||||
virtual void run();
|
|
||||||
bool doneRunning;
|
|
||||||
private:
|
|
||||||
CallbackStruct<TDSensorEvent> *d;
|
|
||||||
std::string protocol;
|
std::string protocol;
|
||||||
std::string model;
|
std::string model;
|
||||||
int sensorId;
|
int id;
|
||||||
int dataType;
|
int dataType;
|
||||||
std::string value;
|
std::string value;
|
||||||
int timestamp;
|
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;
|
using namespace TelldusCore;
|
||||||
|
|
||||||
|
typedef std::list<CallbackStruct *> CallbackList;
|
||||||
|
|
||||||
class CallbackMainDispatcher::PrivateData {
|
class CallbackMainDispatcher::PrivateData {
|
||||||
public:
|
public:
|
||||||
EventHandler eventHandler;
|
EventHandler eventHandler;
|
||||||
EventRef stopEvent, generalCallbackEvent, janitor;
|
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;
|
Mutex mutex;
|
||||||
|
std::list<std::tr1::shared_ptr<TelldusCore::TDEventDispatcher> > eventThreadList;
|
||||||
|
|
||||||
|
CallbackList callbackList;
|
||||||
|
|
||||||
|
int lastCallbackId;
|
||||||
};
|
};
|
||||||
|
|
||||||
CallbackMainDispatcher::CallbackMainDispatcher()
|
CallbackMainDispatcher::CallbackMainDispatcher()
|
||||||
|
@ -33,6 +35,8 @@ CallbackMainDispatcher::CallbackMainDispatcher()
|
||||||
d->stopEvent = d->eventHandler.addEvent();
|
d->stopEvent = d->eventHandler.addEvent();
|
||||||
d->generalCallbackEvent = d->eventHandler.addEvent();
|
d->generalCallbackEvent = d->eventHandler.addEvent();
|
||||||
d->janitor = d->eventHandler.addEvent(); //Used for cleanups
|
d->janitor = d->eventHandler.addEvent(); //Used for cleanups
|
||||||
|
|
||||||
|
d->lastCallbackId = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CallbackMainDispatcher::~CallbackMainDispatcher(void){
|
CallbackMainDispatcher::~CallbackMainDispatcher(void){
|
||||||
|
@ -48,6 +52,42 @@ EventRef CallbackMainDispatcher::retrieveCallbackEvent(){
|
||||||
return d->generalCallbackEvent;
|
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(){
|
void CallbackMainDispatcher::run(){
|
||||||
|
|
||||||
while(!d->stopEvent->isSignaled()){
|
while(!d->stopEvent->isSignaled()){
|
||||||
|
@ -58,36 +98,16 @@ void CallbackMainDispatcher::run(){
|
||||||
if(d->generalCallbackEvent->isSignaled()){
|
if(d->generalCallbackEvent->isSignaled()){
|
||||||
EventDataRef eventData = d->generalCallbackEvent->takeSignal();
|
EventDataRef eventData = d->generalCallbackEvent->takeSignal();
|
||||||
|
|
||||||
DeviceEventCallbackData *decd = dynamic_cast<DeviceEventCallbackData*>(eventData.get());
|
CallbackData *cbd = dynamic_cast<CallbackData *>(eventData.get());
|
||||||
if(decd){
|
if (!cbd) {
|
||||||
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
TelldusCore::MutexLocker locker(&d->mutex);
|
||||||
DeviceChangeEventCallbackData *dcecd = dynamic_cast<DeviceChangeEventCallbackData*>(eventData.get());
|
for(CallbackList::iterator callback_it = d->callbackList.begin(); callback_it != d->callbackList.end(); ++callback_it) {
|
||||||
if(dcecd){
|
if ( (*callback_it)->type == cbd->type ) {
|
||||||
std::tr1::shared_ptr<TDDeviceChangeEventDispatcher> ptr(new TDDeviceChangeEventDispatcher(dcecd->data, dcecd->deviceId, dcecd->eventDeviceChanges, dcecd->eventChangeType, d->janitor));
|
std::tr1::shared_ptr<TelldusCore::TDEventDispatcher> ptr(new TelldusCore::TDEventDispatcher(eventData, *callback_it, d->janitor));
|
||||||
MutexLocker locker(&d->mutex);
|
d->eventThreadList.push_back(ptr);
|
||||||
d->deviceChangeEventThreadList.push_back(ptr);
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (d->janitor->isSignaled()) {
|
if (d->janitor->isSignaled()) {
|
||||||
|
@ -107,52 +127,10 @@ void CallbackMainDispatcher::cleanupCallbacks() {
|
||||||
do {
|
do {
|
||||||
again = false;
|
again = false;
|
||||||
MutexLocker locker(&d->mutex);
|
MutexLocker locker(&d->mutex);
|
||||||
std::list<std::tr1::shared_ptr<TDDeviceEventDispatcher> >::iterator it = d->deviceEventThreadList.begin();
|
std::list<std::tr1::shared_ptr<TDEventDispatcher> >::iterator it = d->eventThreadList.begin();
|
||||||
for (;it != d->deviceEventThreadList.end(); ++it) {
|
for (;it != d->eventThreadList.end(); ++it) {
|
||||||
if ((*it)->done()) {
|
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;
|
again = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,39 +16,6 @@
|
||||||
#include "EventHandler.h"
|
#include "EventHandler.h"
|
||||||
|
|
||||||
namespace TelldusCore {
|
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
|
class CallbackMainDispatcher : public Thread
|
||||||
{
|
{
|
||||||
|
@ -58,6 +25,9 @@ namespace TelldusCore {
|
||||||
|
|
||||||
EventRef retrieveCallbackEvent();
|
EventRef retrieveCallbackEvent();
|
||||||
|
|
||||||
|
int registerCallback( TelldusCore::CallbackStruct::CallbackType type, void *eventFunction, void *context );
|
||||||
|
bool unregisterCallback( int callbackId );
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
|
|
|
@ -9,25 +9,11 @@
|
||||||
|
|
||||||
using namespace TelldusCore;
|
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 {
|
class Client::PrivateData {
|
||||||
public:
|
public:
|
||||||
int lastCallbackId;
|
|
||||||
DeviceEventList deviceEventList;
|
|
||||||
DeviceChangeList deviceChangeEventList;
|
|
||||||
Socket eventSocket;
|
Socket eventSocket;
|
||||||
RawDeviceEventList rawDeviceEventList;
|
bool running, sensorCached, controllerCached;
|
||||||
SensorEventList sensorEventList;
|
std::wstring sensorCache, controllerCache;
|
||||||
bool running, sensorCached;
|
|
||||||
std::wstring sensorCache;
|
|
||||||
TelldusCore::Mutex mutex;
|
TelldusCore::Mutex mutex;
|
||||||
CallbackMainDispatcher callbackMainDispatcher;
|
CallbackMainDispatcher callbackMainDispatcher;
|
||||||
|
|
||||||
|
@ -39,9 +25,9 @@ Client::Client()
|
||||||
: Thread()
|
: Thread()
|
||||||
{
|
{
|
||||||
d = new PrivateData;
|
d = new PrivateData;
|
||||||
d->lastCallbackId = 0;
|
|
||||||
d->running = true;
|
d->running = true;
|
||||||
d->sensorCached = false;
|
d->sensorCached = false;
|
||||||
|
d->controllerCached = false;
|
||||||
d->callbackMainDispatcher.start();
|
d->callbackMainDispatcher.start();
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
@ -69,63 +55,6 @@ Client *Client::getInstance() {
|
||||||
return Client::instance;
|
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) {
|
bool Client::getBoolFromService(const Message &msg) {
|
||||||
return getIntegerFromService(msg) == TELLSTICK_SUCCESS;
|
return getIntegerFromService(msg) == TELLSTICK_SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -143,48 +72,8 @@ std::wstring Client::getWStringFromService(const Message &msg) {
|
||||||
return Message::takeString(&response);
|
return Message::takeString(&response);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Client::registerDeviceEvent( TDDeviceEvent eventFunction, void *context ) {
|
int Client::registerEvent( CallbackStruct::CallbackType type, void *eventFunction, void *context ) {
|
||||||
TelldusCore::MutexLocker locker(&d->mutex);
|
return d->callbackMainDispatcher.registerCallback(type, eventFunction, context );
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::run(){
|
void Client::run(){
|
||||||
|
@ -208,32 +97,44 @@ void Client::run(){
|
||||||
//a message arrived
|
//a message arrived
|
||||||
std::wstring type = Message::takeString(&clientMessage);
|
std::wstring type = Message::takeString(&clientMessage);
|
||||||
if(type == L"TDDeviceChangeEvent"){
|
if(type == L"TDDeviceChangeEvent"){
|
||||||
int deviceId = Message::takeInt(&clientMessage);
|
DeviceChangeEventCallbackData *data = new DeviceChangeEventCallbackData();
|
||||||
int eventDeviceChanges = Message::takeInt(&clientMessage);
|
data->deviceId = Message::takeInt(&clientMessage);
|
||||||
int eventChangeType = Message::takeInt(&clientMessage);
|
data->changeEvent = Message::takeInt(&clientMessage);
|
||||||
callbackDeviceChangeEvent(deviceId, eventDeviceChanges, eventChangeType);
|
data->changeType = Message::takeInt(&clientMessage);
|
||||||
}
|
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data);
|
||||||
else if(type == L"TDDeviceEvent"){
|
|
||||||
int deviceId = Message::takeInt(&clientMessage);
|
} else if(type == L"TDDeviceEvent"){
|
||||||
int eventState = Message::takeInt(&clientMessage);
|
DeviceEventCallbackData *data = new DeviceEventCallbackData();
|
||||||
std::wstring eventValue = Message::takeString(&clientMessage);
|
data->deviceId = Message::takeInt(&clientMessage);
|
||||||
callbackDeviceEvent(deviceId, eventState, eventValue);
|
data->deviceState = Message::takeInt(&clientMessage);
|
||||||
}
|
data->deviceStateValue = TelldusCore::wideToString(Message::takeString(&clientMessage));
|
||||||
else if(type == L"TDRawDeviceEvent"){
|
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data);
|
||||||
std::wstring command = Message::takeString(&clientMessage);
|
|
||||||
int controllerId = Message::takeInt(&clientMessage);
|
} else if(type == L"TDRawDeviceEvent"){
|
||||||
callbackRawEvent(command, controllerId);
|
RawDeviceEventCallbackData *data = new RawDeviceEventCallbackData();
|
||||||
}
|
data->data = TelldusCore::wideToString(Message::takeString(&clientMessage));
|
||||||
else if(type == L"TDSensorEvent"){
|
data->controllerId = Message::takeInt(&clientMessage);
|
||||||
std::wstring protocol = Message::takeString(&clientMessage);
|
d->callbackMainDispatcher.retrieveCallbackEvent()->signal(data);
|
||||||
std::wstring model = Message::takeString(&clientMessage);
|
|
||||||
int sensorId = Message::takeInt(&clientMessage);
|
} else if(type == L"TDSensorEvent"){
|
||||||
int dataType = Message::takeInt(&clientMessage);
|
SensorEventCallbackData *data = new SensorEventCallbackData();
|
||||||
std::wstring value = Message::takeString(&clientMessage);
|
data->protocol = TelldusCore::wideToString(Message::takeString(&clientMessage));
|
||||||
int timestamp = Message::takeInt(&clientMessage);
|
data->model = TelldusCore::wideToString(Message::takeString(&clientMessage));
|
||||||
callbackSensorEvent(protocol, model, sensorId, dataType, value, timestamp);
|
data->id = Message::takeInt(&clientMessage);
|
||||||
}
|
data->dataType = Message::takeInt(&clientMessage);
|
||||||
else{
|
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
|
clientMessage = L""; //cleanup, if message contained garbage/unhandled data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -285,85 +186,7 @@ void Client::stopThread(){
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Client::unregisterCallback( int callbackId ) {
|
bool Client::unregisterCallback( int callbackId ) {
|
||||||
DeviceEventList newDEList;
|
return d->callbackMainDispatcher.unregisterCallback(callbackId);
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Client::getSensor(char *protocol, int protocolLen, char *model, int modelLen, int *sensorId, int *dataTypes) {
|
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;
|
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 "Message.h"
|
||||||
#include "telldus-core.h"
|
#include "telldus-core.h"
|
||||||
#include "Thread.h"
|
#include "Thread.h"
|
||||||
|
#include "CallbackDispatcher.h"
|
||||||
|
|
||||||
namespace TelldusCore {
|
namespace TelldusCore {
|
||||||
class Client : public Thread
|
class Client : public Thread
|
||||||
|
@ -14,18 +15,12 @@ namespace TelldusCore {
|
||||||
static Client *getInstance();
|
static Client *getInstance();
|
||||||
static void close();
|
static void close();
|
||||||
|
|
||||||
void callbackDeviceEvent(int deviceId, int deviceState, const std::wstring &deviceStateValue);
|
int registerEvent(CallbackStruct::CallbackType type, void *eventFunction, void *context );
|
||||||
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 );
|
|
||||||
void stopThread(void);
|
void stopThread(void);
|
||||||
bool unregisterCallback( int callbackId );
|
bool unregisterCallback( int callbackId );
|
||||||
|
|
||||||
int getSensor(char *protocol, int protocolLen, char *model, int modelLen, int *id, int *dataTypes);
|
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 bool getBoolFromService(const Message &msg);
|
||||||
static int getIntegerFromService(const Message &msg);
|
static int getIntegerFromService(const Message &msg);
|
||||||
|
|
|
@ -53,3 +53,8 @@ EXPORTS
|
||||||
tdSensor @38
|
tdSensor @38
|
||||||
tdSensorValue @39
|
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.
|
* Added in version 2.0.0.
|
||||||
**/
|
**/
|
||||||
int WINAPI tdRegisterDeviceEvent( TDDeviceEvent eventFunction, void *context ) {
|
int WINAPI tdRegisterDeviceEvent( TDDeviceEvent eventFunction, void *context ) {
|
||||||
Client *client = Client::getInstance();
|
eventFunction; Client *client = Client::getInstance();
|
||||||
return client->registerDeviceEvent( eventFunction, context );
|
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 ) {
|
int WINAPI tdRegisterRawDeviceEvent( TDRawDeviceEvent eventFunction, void *context ) {
|
||||||
Client *client = Client::getInstance();
|
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) {
|
int WINAPI tdRegisterDeviceChangeEvent( TDDeviceChangeEvent eventFunction, void *context) {
|
||||||
Client *client = Client::getInstance();
|
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) {
|
int WINAPI tdRegisterSensorEvent( TDSensorEvent eventFunction, void *context) {
|
||||||
Client *client = Client::getInstance();
|
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;
|
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 *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 *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 *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
|
#ifndef __cplusplus
|
||||||
#define bool char
|
#define bool char
|
||||||
|
@ -42,6 +43,7 @@ extern "C" {
|
||||||
TELLSTICK_API int WINAPI tdRegisterDeviceChangeEvent( TDDeviceChangeEvent eventFunction, void *context);
|
TELLSTICK_API int WINAPI tdRegisterDeviceChangeEvent( TDDeviceChangeEvent eventFunction, void *context);
|
||||||
TELLSTICK_API int WINAPI tdRegisterRawDeviceEvent( TDRawDeviceEvent 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 tdRegisterSensorEvent( TDSensorEvent eventFunction, void *context );
|
||||||
|
TELLSTICK_API int WINAPI tdRegisterControllerEvent( TDControllerEvent eventFunction, void *context);
|
||||||
TELLSTICK_API int WINAPI tdUnregisterCallback( int callbackId );
|
TELLSTICK_API int WINAPI tdUnregisterCallback( int callbackId );
|
||||||
TELLSTICK_API void WINAPI tdClose(void);
|
TELLSTICK_API void WINAPI tdClose(void);
|
||||||
TELLSTICK_API void WINAPI tdReleaseString(char *string);
|
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 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 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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -125,6 +132,11 @@ extern "C" {
|
||||||
#define TELLSTICK_TYPE_GROUP 2
|
#define TELLSTICK_TYPE_GROUP 2
|
||||||
#define TELLSTICK_TYPE_SCENE 3
|
#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
|
//Device changes
|
||||||
#define TELLSTICK_DEVICE_ADDED 1
|
#define TELLSTICK_DEVICE_ADDED 1
|
||||||
#define TELLSTICK_DEVICE_CHANGED 2
|
#define TELLSTICK_DEVICE_CHANGED 2
|
||||||
|
@ -136,5 +148,7 @@ extern "C" {
|
||||||
#define TELLSTICK_CHANGE_PROTOCOL 2
|
#define TELLSTICK_CHANGE_PROTOCOL 2
|
||||||
#define TELLSTICK_CHANGE_MODEL 3
|
#define TELLSTICK_CHANGE_MODEL 3
|
||||||
#define TELLSTICK_CHANGE_METHOD 4
|
#define TELLSTICK_CHANGE_METHOD 4
|
||||||
|
#define TELLSTICK_CHANGE_AVAILABLE 5
|
||||||
|
#define TELLSTICK_CHANGE_FIRMWARE 6
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -5,6 +5,9 @@
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#ifndef COMMON_H
|
||||||
|
#define COMMON_H
|
||||||
|
|
||||||
#ifdef _WINDOWS
|
#ifdef _WINDOWS
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <ole2.h>
|
#include <ole2.h>
|
||||||
|
@ -75,3 +78,5 @@ inline char *wrapStdString( const std::string &string) {
|
||||||
inline char *wrapStdWstring( const std::wstring &wstring) {
|
inline char *wrapStdWstring( const std::wstring &wstring) {
|
||||||
return wrapStdString(TelldusCore::wideToString(wstring));
|
return wrapStdString(TelldusCore::wideToString(wstring));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif //COMMON_H
|
||||||
|
|
|
@ -10,13 +10,14 @@ public:
|
||||||
TelldusCore::EventRef event, deviceUpdateEvent;
|
TelldusCore::EventRef event, deviceUpdateEvent;
|
||||||
bool done;
|
bool done;
|
||||||
DeviceManager *deviceManager;
|
DeviceManager *deviceManager;
|
||||||
|
ControllerManager *controllerManager;
|
||||||
};
|
};
|
||||||
|
|
||||||
ClientCommunicationHandler::ClientCommunicationHandler(){
|
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()
|
:Thread()
|
||||||
{
|
{
|
||||||
d = new PrivateData;
|
d = new PrivateData;
|
||||||
|
@ -25,7 +26,7 @@ ClientCommunicationHandler::ClientCommunicationHandler(TelldusCore::Socket *clie
|
||||||
d->done = false;
|
d->done = false;
|
||||||
d->deviceManager = deviceManager;
|
d->deviceManager = deviceManager;
|
||||||
d->deviceUpdateEvent = deviceUpdateEvent;
|
d->deviceUpdateEvent = deviceUpdateEvent;
|
||||||
|
d->controllerManager = controllerManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientCommunicationHandler::~ClientCommunicationHandler(void)
|
ClientCommunicationHandler::~ClientCommunicationHandler(void)
|
||||||
|
@ -230,8 +231,25 @@ void ClientCommunicationHandler::parseMessage(const std::wstring &clientMessage,
|
||||||
int dataType = TelldusCore::Message::takeInt(&msg);
|
int dataType = TelldusCore::Message::takeInt(&msg);
|
||||||
(*wstringReturn) = d->deviceManager->getSensorValue(protocol, model, id, dataType);
|
(*wstringReturn) = d->deviceManager->getSensorValue(protocol, model, id, dataType);
|
||||||
|
|
||||||
}
|
} else if (function == L"tdController") {
|
||||||
else{
|
(*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;
|
(*intReturn) = TELLSTICK_ERROR_UNKNOWN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,13 @@ class ClientCommunicationHandler : public TelldusCore::Thread
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ClientCommunicationHandler();
|
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);
|
~ClientCommunicationHandler(void);
|
||||||
|
|
||||||
bool isDone();
|
bool isDone();
|
||||||
|
|
|
@ -1,17 +1,20 @@
|
||||||
#include "Controller.h"
|
#include "Controller.h"
|
||||||
#include "Protocol.h"
|
#include "Protocol.h"
|
||||||
#include <stdio.h> //TODO DEBUG
|
#include "EventUpdateManager.h"
|
||||||
|
#include "Strings.h"
|
||||||
|
|
||||||
class Controller::PrivateData {
|
class Controller::PrivateData {
|
||||||
public:
|
public:
|
||||||
TelldusCore::Event *event;
|
TelldusCore::EventRef event, updateEvent;
|
||||||
int id;
|
int id, firmwareVersion;
|
||||||
};
|
};
|
||||||
|
|
||||||
Controller::Controller(int id, TelldusCore::Event *event){
|
Controller::Controller(int id, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent){
|
||||||
d = new PrivateData;
|
d = new PrivateData;
|
||||||
d->event = event;
|
d->event = event;
|
||||||
|
d->updateEvent = updateEvent;
|
||||||
d->id = id;
|
d->id = id;
|
||||||
|
d->firmwareVersion = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Controller::~Controller(){
|
Controller::~Controller(){
|
||||||
|
@ -33,3 +36,18 @@ void Controller::decodePublishData(const std::string &data) const {
|
||||||
this->publishData(*msgIt);
|
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:
|
public:
|
||||||
virtual ~Controller();
|
virtual ~Controller();
|
||||||
|
|
||||||
virtual int firmwareVersion() = 0;
|
virtual int firmwareVersion() const;
|
||||||
virtual int send( const std::string &message ) = 0;
|
virtual int send( const std::string &message ) = 0;
|
||||||
virtual int reset() = 0;
|
virtual int reset() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Controller(int id, TelldusCore::Event *event);
|
Controller(int id, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent);
|
||||||
void publishData(const std::string &data) const;
|
void publishData(const std::string &data) const;
|
||||||
void decodePublishData(const std::string &data) const;
|
void decodePublishData(const std::string &data) const;
|
||||||
|
void setFirmwareVersion(int version);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class PrivateData;
|
class PrivateData;
|
||||||
|
|
|
@ -3,31 +3,47 @@
|
||||||
#include "Mutex.h"
|
#include "Mutex.h"
|
||||||
#include "TellStick.h"
|
#include "TellStick.h"
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
|
#include "Message.h"
|
||||||
|
#include "Strings.h"
|
||||||
|
#include "Settings.h"
|
||||||
|
#include "EventUpdateManager.h"
|
||||||
#include "../client/telldus-core.h"
|
#include "../client/telldus-core.h"
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <stdio.h>
|
#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 {
|
class ControllerManager::PrivateData {
|
||||||
public:
|
public:
|
||||||
int lastControllerId;
|
int lastControllerId;
|
||||||
|
Settings settings;
|
||||||
ControllerMap controllers;
|
ControllerMap controllers;
|
||||||
TelldusCore::Event *event;
|
TelldusCore::EventRef event, updateEvent;
|
||||||
TelldusCore::Mutex mutex;
|
TelldusCore::Mutex mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
ControllerManager::ControllerManager(TelldusCore::Event *event){
|
ControllerManager::ControllerManager(TelldusCore::EventRef event, TelldusCore::EventRef updateEvent){
|
||||||
d = new PrivateData;
|
d = new PrivateData;
|
||||||
d->lastControllerId = 0;
|
d->lastControllerId = 0;
|
||||||
d->event = event;
|
d->event = event;
|
||||||
|
d->updateEvent = updateEvent;
|
||||||
|
this->loadStoredControllers();
|
||||||
this->loadControllers();
|
this->loadControllers();
|
||||||
}
|
}
|
||||||
|
|
||||||
ControllerManager::~ControllerManager() {
|
ControllerManager::~ControllerManager() {
|
||||||
for (ControllerMap::iterator it = d->controllers.begin(); it != d->controllers.end(); ++it) {
|
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;
|
delete d;
|
||||||
}
|
}
|
||||||
|
@ -41,8 +57,9 @@ void ControllerManager::deviceInsertedOrRemoved(int vid, int pid, const std::str
|
||||||
TelldusCore::MutexLocker locker(&d->mutex);
|
TelldusCore::MutexLocker locker(&d->mutex);
|
||||||
while(d->controllers.size()) {
|
while(d->controllers.size()) {
|
||||||
ControllerMap::iterator it = d->controllers.begin();
|
ControllerMap::iterator it = d->controllers.begin();
|
||||||
delete it->second;
|
delete it->second.controller;
|
||||||
d->controllers.erase(it);
|
it->second.controller = 0;
|
||||||
|
signalControllerEvent(it->first, TELLSTICK_DEVICE_STATE_CHANGED, TELLSTICK_CHANGE_AVAILABLE, L"0");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -58,11 +75,11 @@ void ControllerManager::deviceInsertedOrRemoved(int vid, int pid, const std::str
|
||||||
} else {
|
} else {
|
||||||
//Autodetect which has been disconnected
|
//Autodetect which has been disconnected
|
||||||
TelldusCore::MutexLocker locker(&d->mutex);
|
TelldusCore::MutexLocker locker(&d->mutex);
|
||||||
bool again = true;
|
|
||||||
while(again) {
|
|
||||||
again = false;
|
|
||||||
for(ControllerMap::iterator it = d->controllers.begin(); it != d->controllers.end(); ++it) {
|
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) {
|
if (!tellstick) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -78,11 +95,9 @@ void ControllerManager::deviceInsertedOrRemoved(int vid, int pid, const std::str
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
d->controllers.erase(it);
|
it->second.controller = 0;
|
||||||
delete tellstick;
|
delete tellstick;
|
||||||
again=true;
|
signalControllerEvent(it->first, TELLSTICK_DEVICE_STATE_CHANGED, TELLSTICK_CHANGE_AVAILABLE, L"0");
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,10 +108,16 @@ Controller *ControllerManager::getBestControllerById(int id) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
ControllerMap::const_iterator it = d->controllers.find(id);
|
ControllerMap::const_iterator it = d->controllers.find(id);
|
||||||
if (it != d->controllers.end()) {
|
if (it != d->controllers.end() && it->second.controller) {
|
||||||
return it->second;
|
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;
|
bool found = false;
|
||||||
ControllerMap::const_iterator cit = d->controllers.begin();
|
ControllerMap::const_iterator cit = d->controllers.begin();
|
||||||
for(; cit != d->controllers.end(); ++cit) {
|
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) {
|
if (!tellstick) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -125,14 +149,59 @@ void ControllerManager::loadControllers() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int controllerId = d->lastControllerId-1;
|
int type = TELLSTICK_CONTROLLER_TELLSTICK;
|
||||||
TellStick *controller = new TellStick(controllerId, d->event, *it);
|
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()) {
|
if (!controller->isOpen()) {
|
||||||
delete controller;
|
delete controller;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
d->lastControllerId = controllerId;
|
d->controllers[controllerId].controller = controller;
|
||||||
d->controllers[d->lastControllerId] = 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);
|
TelldusCore::MutexLocker locker(&d->mutex);
|
||||||
for(ControllerMap::iterator it = d->controllers.begin(); it != d->controllers.end(); ++it) {
|
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) {
|
if (tellstick) {
|
||||||
tellStickControllers.push_back(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
|
deviceInsertedOrRemoved(tellstick->vid(), tellstick->pid(), tellstick->serial(), false); //remove from list and delete
|
||||||
return success;
|
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
|
#ifndef CONTROLLERMANAGER_H
|
||||||
#define CONTROLLERMANAGER_H
|
#define CONTROLLERMANAGER_H
|
||||||
|
|
||||||
|
#include "Event.h"
|
||||||
class Controller;
|
class Controller;
|
||||||
namespace TelldusCore {
|
|
||||||
class Event;
|
|
||||||
}
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class ControllerManager {
|
class ControllerManager {
|
||||||
public:
|
public:
|
||||||
ControllerManager(TelldusCore::Event *event);
|
ControllerManager(TelldusCore::EventRef event, TelldusCore::EventRef updateEvent);
|
||||||
~ControllerManager(void);
|
~ControllerManager(void);
|
||||||
|
|
||||||
void deviceInsertedOrRemoved(int vid, int pid, const std::string &serial, bool inserted);
|
void deviceInsertedOrRemoved(int vid, int pid, const std::string &serial, bool inserted);
|
||||||
|
|
||||||
Controller *getBestControllerById(int id);
|
Controller *getBestControllerById(int id);
|
||||||
void loadControllers();
|
void loadControllers();
|
||||||
|
void loadStoredControllers();
|
||||||
void queryControllerStatus();
|
void queryControllerStatus();
|
||||||
int resetController(Controller *controller);
|
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:
|
private:
|
||||||
|
void signalControllerEvent(int controllerId, int changeEvent, int changeType, const std::wstring &newValue);
|
||||||
class PrivateData;
|
class PrivateData;
|
||||||
PrivateData *d;
|
PrivateData *d;
|
||||||
};
|
};
|
||||||
|
|
|
@ -47,13 +47,13 @@ DeviceManager::~DeviceManager(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeviceManager::fillDevices(){
|
void DeviceManager::fillDevices(){
|
||||||
int numberOfDevices = d->set.getNumberOfDevices();
|
int numberOfDevices = d->set.getNumberOfNodes(Settings::Device);
|
||||||
TelldusCore::MutexLocker deviceListLocker(&d->lock);
|
TelldusCore::MutexLocker deviceListLocker(&d->lock);
|
||||||
|
|
||||||
for (int i = 0; i < numberOfDevices; ++i) {
|
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] = 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]->setModel(d->set.getModel(id));
|
||||||
d->devices[id]->setProtocolName(d->set.getProtocol(id));
|
d->devices[id]->setProtocolName(d->set.getProtocol(id));
|
||||||
d->devices[id]->setPreferredControllerId(d->set.getPreferredControllerId(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);
|
DeviceMap::iterator it = d->devices.find(deviceId);
|
||||||
if (it != d->devices.end()) {
|
if (it != d->devices.end()) {
|
||||||
TelldusCore::MutexLocker deviceLocker(it->second);
|
TelldusCore::MutexLocker deviceLocker(it->second);
|
||||||
int ret = d->set.setName(deviceId, name);
|
int ret = d->set.setName(Settings::Device, deviceId, name);
|
||||||
if (ret != TELLSTICK_SUCCESS) {
|
if (ret != TELLSTICK_SUCCESS) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -330,7 +330,7 @@ int DeviceManager::getNumberOfDevices(){
|
||||||
|
|
||||||
int DeviceManager::addDevice(){
|
int DeviceManager::addDevice(){
|
||||||
|
|
||||||
int id = d->set.addDevice();
|
int id = d->set.addNode(Settings::Device);
|
||||||
if(id < 0){
|
if(id < 0){
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -344,7 +344,7 @@ int DeviceManager::addDevice(){
|
||||||
}
|
}
|
||||||
|
|
||||||
int DeviceManager::getDeviceId(int deviceIndex) {
|
int DeviceManager::getDeviceId(int deviceIndex) {
|
||||||
return d->set.getDeviceId(deviceIndex);
|
return d->set.getNodeId(Settings::Device, deviceIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
int DeviceManager::getDeviceType(int deviceId){
|
int DeviceManager::getDeviceType(int deviceId){
|
||||||
|
@ -573,7 +573,7 @@ int DeviceManager::removeDevice(int deviceId){
|
||||||
|
|
||||||
Device *device = 0;
|
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) {
|
if (ret != TELLSTICK_SUCCESS) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,6 +105,13 @@ void EventUpdateManager::sendMessageToClients(EventUpdateData *data){
|
||||||
msg.addArgument(data->value);
|
msg.addArgument(data->value);
|
||||||
msg.addArgument(data->timestamp);
|
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);
|
(*it)->write(msg);
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,21 @@
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
|
|
||||||
|
TelldusCore::Mutex Settings::mutex;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get the name of the device
|
* 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);
|
TelldusCore::MutexLocker locker(&mutex);
|
||||||
return getStringSetting(intDeviceId, L"name", false);
|
return getStringSetting(type, intNodeId, L"name", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set the name of the device
|
* 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);
|
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 {
|
std::wstring Settings::getProtocol(int intDeviceId) const {
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
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){
|
int Settings::setProtocol(int intDeviceId, const std::wstring &strVendor){
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
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 {
|
std::wstring Settings::getModel(int intDeviceId) const {
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
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){
|
int Settings::setModel(int intDeviceId, const std::wstring &strModel){
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
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){
|
int Settings::setDeviceParameter(int intDeviceId, const std::wstring &strName, const std::wstring &strValue){
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
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 {
|
std::wstring Settings::getDeviceParameter(int intDeviceId, const std::wstring &strName) const {
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
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){
|
int Settings::setPreferredControllerId(int intDeviceId, int value){
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
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) {
|
int Settings::getPreferredControllerId(int intDeviceId) {
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
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
|
#ifndef _CONFUSE
|
||||||
|
|
||||||
bool Settings::setDeviceState( int intDeviceId, int intDeviceState, const std::wstring &strDeviceStateValue ) {
|
bool Settings::setDeviceState( int intDeviceId, int intDeviceState, const std::wstring &strDeviceStateValue ) {
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
TelldusCore::MutexLocker locker(&mutex);
|
||||||
bool retval = setIntSetting( intDeviceId, L"state", intDeviceState, true );
|
bool retval = setIntSetting( Settings::Device, intDeviceId, L"state", intDeviceState, true );
|
||||||
setStringSetting( intDeviceId, L"stateValue", strDeviceStateValue, true );
|
setStringSetting( Settings::Device, intDeviceId, L"stateValue", strDeviceStateValue, true );
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Settings::getDeviceState( int intDeviceId ) const {
|
int Settings::getDeviceState( int intDeviceId ) const {
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
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 {
|
std::wstring Settings::getDeviceStateValue( int intDeviceId ) const {
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
TelldusCore::MutexLocker locker(&mutex);
|
||||||
return getStringSetting( intDeviceId, L"stateValue", true );
|
return getStringSetting( Settings::Device, intDeviceId, L"stateValue", true );
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -6,13 +6,15 @@
|
||||||
|
|
||||||
class Settings {
|
class Settings {
|
||||||
public:
|
public:
|
||||||
|
enum Node { Device, Controller };
|
||||||
|
|
||||||
Settings(void);
|
Settings(void);
|
||||||
virtual ~Settings(void);
|
virtual ~Settings(void);
|
||||||
|
|
||||||
std::wstring getSetting(const std::wstring &strName) const;
|
std::wstring getSetting(const std::wstring &strName) const;
|
||||||
int getNumberOfDevices(void) const;
|
int getNumberOfNodes(Node type) const;
|
||||||
std::wstring getName(int intDeviceId) const;
|
std::wstring getName(Node type, int intNodeId) const;
|
||||||
int setName(int intDeviceId, const std::wstring &strNewName);
|
int setName(Node type, int intDeviceId, const std::wstring &strNewName);
|
||||||
std::wstring getProtocol(int intDeviceId) const;
|
std::wstring getProtocol(int intDeviceId) const;
|
||||||
int setProtocol(int intDeviceId, const std::wstring &strVendor);
|
int setProtocol(int intDeviceId, const std::wstring &strVendor);
|
||||||
std::wstring getModel(int intDeviceId) const;
|
std::wstring getModel(int intDeviceId) const;
|
||||||
|
@ -25,22 +27,28 @@ public:
|
||||||
int getPreferredControllerId(int intDeviceId);
|
int getPreferredControllerId(int intDeviceId);
|
||||||
int setPreferredControllerId(int intDeviceId, int value);
|
int setPreferredControllerId(int intDeviceId, int value);
|
||||||
|
|
||||||
int addDevice();
|
int addNode(Node type);
|
||||||
int getDeviceId(int intDeviceIndex) const;
|
int getNodeId(Node type, int intDeviceIndex) const;
|
||||||
int removeDevice(int intDeviceId);
|
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:
|
protected:
|
||||||
std::wstring getStringSetting(int intDeviceId, const std::wstring &name, bool parameter) const;
|
std::wstring getStringSetting(Node type, int intNodeId, const std::wstring &name, bool parameter) const;
|
||||||
int setStringSetting(int intDeviceId, const std::wstring &name, const std::wstring &value, bool parameter);
|
int setStringSetting(Node type, int intDeviceId, const std::wstring &name, const std::wstring &value, bool parameter);
|
||||||
int getIntSetting(int intDeviceId, const std::wstring &name, bool parameter) const;
|
int getIntSetting(Node type, int intDeviceId, const std::wstring &name, bool parameter) const;
|
||||||
int setIntSetting(int intDeviceId, const std::wstring &name, int value, bool parameter);
|
int setIntSetting(Node type, int intDeviceId, const std::wstring &name, int value, bool parameter);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int getNextDeviceId() const;
|
int getNextNodeId(Node type) const;
|
||||||
|
std::string getNodeString(Node type) const;
|
||||||
|
|
||||||
class PrivateData;
|
class PrivateData;
|
||||||
PrivateData *d;
|
PrivateData *d;
|
||||||
mutable TelldusCore::Mutex mutex;
|
static TelldusCore::Mutex mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -76,86 +76,107 @@ std::wstring Settings::getSetting(const std::wstring &strName) const {
|
||||||
/*
|
/*
|
||||||
* Return the number of stored devices
|
* Return the number of stored devices
|
||||||
*/
|
*/
|
||||||
int Settings::getNumberOfDevices(void) const {
|
int Settings::getNumberOfNodes(Node node) const {
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
TelldusCore::MutexLocker locker(&mutex);
|
||||||
if (d->cfg > 0) {
|
if (d->cfg > 0) {
|
||||||
|
if (node == Device) {
|
||||||
return cfg_size(d->cfg, "device");
|
return cfg_size(d->cfg, "device");
|
||||||
|
} else if (node == Controller) {
|
||||||
|
return cfg_size(d->cfg, "controller");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Settings::getDeviceId(int intDeviceIndex) const {
|
int Settings::getNodeId(Node type, int intDeviceIndex) const {
|
||||||
if (intDeviceIndex >= getNumberOfDevices()) { //Out of bounds
|
if (intDeviceIndex >= getNumberOfNodes(type)) { //Out of bounds
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
TelldusCore::MutexLocker locker(&mutex);
|
||||||
cfg_t *cfg_device = cfg_getnsec(d->cfg, "device", intDeviceIndex);
|
cfg_t *cfg_node;
|
||||||
int id = cfg_getint(cfg_device, "id");
|
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;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Add a new device
|
* Add a new node
|
||||||
*/
|
*/
|
||||||
int Settings::addDevice(){
|
int Settings::addNode(Node type){
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
TelldusCore::MutexLocker locker(&mutex);
|
||||||
int intDeviceId = getNextDeviceId();
|
int intNodeId = getNextNodeId(type);
|
||||||
|
|
||||||
FILE *fp = fopen(CONFIG_FILE, "w");
|
FILE *fp = fopen(CONFIG_FILE, "w");
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
||||||
}
|
}
|
||||||
cfg_print(d->cfg, fp); //Print the config-file
|
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);
|
fclose(fp);
|
||||||
|
|
||||||
//Re-read config-file
|
//Re-read config-file
|
||||||
cfg_free(d->cfg);
|
cfg_free(d->cfg);
|
||||||
readConfig(&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
|
//Private, no locks needed
|
||||||
int intDeviceId = 0;
|
int intNodeId = 0;
|
||||||
cfg_t *cfg_device;
|
cfg_t *cfg_node;
|
||||||
for (int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
std::string strType;
|
||||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
if (type == Device) {
|
||||||
if (cfg_getint(cfg_device, "id") >= intDeviceId) {
|
strType = "device";
|
||||||
intDeviceId = cfg_getint(cfg_device, "id");
|
} 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++;
|
intNodeId++;
|
||||||
return intDeviceId;
|
return intNodeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Remove a device
|
* Remove a device
|
||||||
*/
|
*/
|
||||||
int Settings::removeDevice(int intDeviceId){
|
int Settings::removeNode(Node type, int intNodeId){
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
TelldusCore::MutexLocker locker(&mutex);
|
||||||
FILE *fp = fopen(CONFIG_FILE, "w");
|
FILE *fp = fopen(CONFIG_FILE, "w");
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string strType = getNodeString(type);
|
||||||
|
|
||||||
// Print all opts
|
// Print all opts
|
||||||
for(int i = 0; d->cfg->opts[i].name; i++) {
|
for(int i = 0; d->cfg->opts[i].name; i++) {
|
||||||
|
|
||||||
// Check if it isn't a device section
|
// 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);
|
cfg_opt_print(&d->cfg->opts[i], fp);
|
||||||
} else {
|
} else {
|
||||||
// Print all sections except the one to remove
|
// Print all sections except the one to remove
|
||||||
cfg_t *cfg_device;
|
cfg_t *cfg_node;
|
||||||
for (int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
|
||||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
cfg_node = cfg_getnsec(d->cfg, strType.c_str(), i);
|
||||||
if (cfg_getint(cfg_device, "id") != intDeviceId) { //This isn't the one to skip
|
if (cfg_getint(cfg_node, "id") != intNodeId) { //This isn't the one to skip
|
||||||
fprintf(fp, "device {\n");
|
fprintf(fp, "%s {\n", strType.c_str());
|
||||||
cfg_print_indent(cfg_device, fp, 1);
|
cfg_print_indent(cfg_node, fp, 1);
|
||||||
fprintf(fp, "}\n");
|
fprintf(fp, "}\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -244,15 +265,17 @@ std::wstring Settings::getDeviceStateValue( int intDeviceId ) const {
|
||||||
return L"";
|
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
|
//already locked
|
||||||
if (d->cfg == 0) {
|
if (d->cfg == 0) {
|
||||||
return L"";
|
return L"";
|
||||||
}
|
}
|
||||||
|
std::string strType = getNodeString(type);
|
||||||
|
|
||||||
cfg_t *cfg_device;
|
cfg_t *cfg_device;
|
||||||
for (int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
|
||||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
cfg_device = cfg_getnsec(d->cfg, strType.c_str(), i);
|
||||||
if (cfg_getint(cfg_device, "id") == intDeviceId) {
|
if (cfg_getint(cfg_device, "id") == intNodeId) {
|
||||||
if (parameter) {
|
if (parameter) {
|
||||||
cfg_device = cfg_getsec(cfg_device, "parameters");
|
cfg_device = cfg_getsec(cfg_device, "parameters");
|
||||||
}
|
}
|
||||||
|
@ -267,14 +290,15 @@ std::wstring Settings::getStringSetting(int intDeviceId, const std::wstring &nam
|
||||||
return L"";
|
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
|
//already locked
|
||||||
if (d->cfg == 0) {
|
if (d->cfg == 0) {
|
||||||
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
||||||
}
|
}
|
||||||
|
std::string strType = getNodeString(type);
|
||||||
cfg_t *cfg_device;
|
cfg_t *cfg_device;
|
||||||
for (int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
|
||||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
cfg_device = cfg_getnsec(d->cfg, strType.c_str(), i);
|
||||||
if (cfg_getint(cfg_device, "id") == intDeviceId) {
|
if (cfg_getint(cfg_device, "id") == intDeviceId) {
|
||||||
std::string newValue = TelldusCore::wideToString(value);
|
std::string newValue = TelldusCore::wideToString(value);
|
||||||
if (parameter) {
|
if (parameter) {
|
||||||
|
@ -295,32 +319,34 @@ int Settings::setStringSetting(int intDeviceId, const std::wstring &name, const
|
||||||
return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
|
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
|
//already locked
|
||||||
if (d->cfg == 0) {
|
if (d->cfg == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
cfg_t *cfg_device;
|
std::string strType = getNodeString(type);
|
||||||
for(int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
cfg_t *cfg_node;
|
||||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
for(int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
|
||||||
if (cfg_getint(cfg_device, "id") == intDeviceId) {
|
cfg_node = cfg_getnsec(d->cfg, strType.c_str(), i);
|
||||||
|
if (cfg_getint(cfg_node, "id") == intDeviceId) {
|
||||||
if (parameter) {
|
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;
|
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
|
//already locked
|
||||||
if (d->cfg == 0) {
|
if (d->cfg == 0) {
|
||||||
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
return TELLSTICK_ERROR_PERMISSION_DENIED;
|
||||||
}
|
}
|
||||||
|
std::string strType = getNodeString(type);
|
||||||
cfg_t *cfg_device;
|
cfg_t *cfg_device;
|
||||||
for (int i = 0; i < cfg_size(d->cfg, "device"); ++i) {
|
for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
|
||||||
cfg_device = cfg_getnsec(d->cfg, "device", i);
|
cfg_device = cfg_getnsec(d->cfg, strType.c_str(), i);
|
||||||
if (cfg_getint(cfg_device, "id") == intDeviceId) {
|
if (cfg_getint(cfg_device, "id") == intDeviceId) {
|
||||||
if (parameter) {
|
if (parameter) {
|
||||||
cfg_t *cfg_parameters = cfg_getsec(cfg_device, "parameters");
|
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.
|
//All the const_cast keywords is to remove the compiler warnings generated by the C++-compiler.
|
||||||
cfg_opt_t controller_opts[] = {
|
cfg_opt_t controller_opts[] = {
|
||||||
CFG_INT(const_cast<char *>("id"), -1, CFGF_NONE),
|
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[] = {
|
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 *>("deviceNode"), const_cast<char *>("/dev/tellstick"), CFGF_NONE),
|
||||||
CFG_STR(const_cast<char *>("ignoreControllerConfirmation"), const_cast<char *>("false"), 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 *>("device"), device_opts, CFGF_MULTI),
|
||||||
|
CFG_SEC(const_cast<char *>("controller"), controller_opts, CFGF_MULTI),
|
||||||
CFG_END()
|
CFG_END()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -60,22 +60,26 @@ std::wstring Settings::getSetting(const std::wstring &strName) const {
|
||||||
/*
|
/*
|
||||||
* Return the number of stored devices
|
* 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 );
|
CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName );
|
||||||
if (!cfarray) return 0;
|
if (!cfarray) return 0;
|
||||||
CFIndex size = CFArrayGetCount( cfarray );
|
CFIndex size = CFArrayGetCount( cfarray );
|
||||||
int devices = 0;
|
int nodes = 0;
|
||||||
for (CFIndex k = 0; k < size; ++k) {
|
for (CFIndex k = 0; k < size; ++k) {
|
||||||
CFStringRef key = (CFStringRef) CFArrayGetValueAtIndex(cfarray, k);
|
CFStringRef key = (CFStringRef) CFArrayGetValueAtIndex(cfarray, k);
|
||||||
if (CFStringHasPrefix( key, CFSTR("devices.") ) &&
|
if (!CFStringHasSuffix( key, CFSTR(".name") )) {
|
||||||
CFStringHasSuffix( key, CFSTR(".name") ) ) {
|
continue;
|
||||||
devices++;
|
}
|
||||||
|
if (type == Device && CFStringHasPrefix( key, CFSTR("devices.") )) {
|
||||||
|
++nodes;
|
||||||
|
} else if (type == Controller && CFStringHasPrefix( key, CFSTR("controllers.") )) {
|
||||||
|
++nodes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return devices;
|
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 );
|
CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName );
|
||||||
if (!cfarray) return 0;
|
if (!cfarray) return 0;
|
||||||
CFIndex size = CFArrayGetCount( cfarray );
|
CFIndex size = CFArrayGetCount( cfarray );
|
||||||
|
@ -83,10 +87,19 @@ int Settings::getDeviceId(int intDeviceIndex) const {
|
||||||
int id = 0;
|
int id = 0;
|
||||||
for (CFIndex k = 0; k < size; ++k) {
|
for (CFIndex k = 0; k < size; ++k) {
|
||||||
CFStringRef key = (CFStringRef) CFArrayGetValueAtIndex(cfarray, k);
|
CFStringRef key = (CFStringRef) CFArrayGetValueAtIndex(cfarray, k);
|
||||||
if (CFStringHasPrefix( key, CFSTR("devices.") ) &&
|
if (!CFStringHasSuffix( key, CFSTR(".name") )) {
|
||||||
CFStringHasSuffix( key, CFSTR(".name") ) ) {
|
CFRelease( key );
|
||||||
|
continue;
|
||||||
if (index == intDeviceIndex) {
|
}
|
||||||
|
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(".") );
|
CFArrayRef split = CFStringCreateArrayBySeparatingStrings( 0, key, CFSTR(".") );
|
||||||
if ( !split || CFArrayGetCount( split ) != 3 ) continue;
|
if ( !split || CFArrayGetCount( split ) != 3 ) continue;
|
||||||
|
|
||||||
|
@ -117,29 +130,30 @@ int Settings::getDeviceId(int intDeviceIndex) const {
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
CFRelease( key );
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Add a new node
|
||||||
|
*/
|
||||||
|
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;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Add a new device
|
* Get next available node id
|
||||||
*/
|
*/
|
||||||
int Settings::addDevice() {
|
int Settings::getNextNodeId(Node type) const {
|
||||||
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 );
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Get next available device id
|
|
||||||
*/
|
|
||||||
int Settings::getNextDeviceId() const {
|
|
||||||
int id = 0, max = 0;
|
int id = 0, max = 0;
|
||||||
int numberOfDevices = getNumberOfDevices();
|
int numberOfNodes = getNumberOfNodes(type);
|
||||||
for( int i = 0; i < numberOfDevices; i++) {
|
for( int i = 0; i < numberOfNodes; i++) {
|
||||||
id = getDeviceId( i );
|
id = getNodeId( type, i );
|
||||||
if (id > max) {
|
if (id > max) {
|
||||||
max = id;
|
max = id;
|
||||||
}
|
}
|
||||||
|
@ -151,9 +165,9 @@ int Settings::getNextDeviceId() const {
|
||||||
/*
|
/*
|
||||||
* Remove a device
|
* Remove a device
|
||||||
*/
|
*/
|
||||||
int Settings::removeDevice(int intDeviceId){
|
int Settings::removeNode(Node type, int intNodeId){
|
||||||
int ret = TELLSTICK_ERROR_DEVICE_NOT_FOUND;
|
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 );
|
CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName );
|
||||||
if (!cfarray) {
|
if (!cfarray) {
|
||||||
|
@ -172,15 +186,15 @@ int Settings::removeDevice(int intDeviceId){
|
||||||
return ret;
|
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));
|
std::string name(TelldusCore::wideToString(wname));
|
||||||
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
||||||
|
|
||||||
CFStringRef key;
|
CFStringRef key;
|
||||||
if (parameter) {
|
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 {
|
} 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;
|
CFStringRef value;
|
||||||
|
@ -209,7 +223,7 @@ std::wstring Settings::getStringSetting(int intDeviceId, const std::wstring &wna
|
||||||
return retval;
|
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 name(TelldusCore::wideToString(wname));
|
||||||
std::string value(TelldusCore::wideToString(wvalue));
|
std::string value(TelldusCore::wideToString(wvalue));
|
||||||
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
||||||
|
@ -217,9 +231,9 @@ int Settings::setStringSetting(int intDeviceId, const std::wstring &wname, const
|
||||||
|
|
||||||
CFStringRef key;
|
CFStringRef key;
|
||||||
if (parameter) {
|
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 {
|
} 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 );
|
CFPreferencesSetValue( key, cfvalue, d->app_ID, d->userName, d->hostName );
|
||||||
|
@ -227,7 +241,7 @@ int Settings::setStringSetting(int intDeviceId, const std::wstring &wname, const
|
||||||
return TELLSTICK_SUCCESS;
|
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;
|
int retval = 0;
|
||||||
std::string name(TelldusCore::wideToString(wname));
|
std::string name(TelldusCore::wideToString(wname));
|
||||||
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
||||||
|
@ -235,9 +249,9 @@ int Settings::getIntSetting(int intDeviceId, const std::wstring &wname, bool par
|
||||||
|
|
||||||
CFStringRef key;
|
CFStringRef key;
|
||||||
if (parameter) {
|
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 {
|
} 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);
|
cfvalue = (CFNumberRef)CFPreferencesCopyValue(key, d->app_ID, d->userName, d->hostName);
|
||||||
|
@ -258,16 +272,16 @@ int Settings::getIntSetting(int intDeviceId, const std::wstring &wname, bool par
|
||||||
return retval;
|
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));
|
std::string name(TelldusCore::wideToString(wname));
|
||||||
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
|
||||||
CFNumberRef cfvalue = CFNumberCreate(NULL, kCFNumberIntType, &value);
|
CFNumberRef cfvalue = CFNumberCreate(NULL, kCFNumberIntType, &value);
|
||||||
|
|
||||||
CFStringRef key;
|
CFStringRef key;
|
||||||
if (parameter) {
|
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 {
|
} 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 );
|
CFPreferencesSetValue( key, cfvalue, d->app_ID, d->userName, d->hostName );
|
||||||
|
|
|
@ -15,16 +15,24 @@ const int intMaxRegValueLength = 1000;
|
||||||
class Settings::PrivateData {
|
class Settings::PrivateData {
|
||||||
public:
|
public:
|
||||||
HKEY rootKey;
|
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
|
* Constructor
|
||||||
*/
|
*/
|
||||||
Settings::Settings(void) {
|
Settings::Settings(void) {
|
||||||
d = new PrivateData();
|
d = new PrivateData();
|
||||||
d->strRegPathDevice = L"SOFTWARE\\Telldus\\Devices\\";
|
|
||||||
d->strRegPath = L"SOFTWARE\\Telldus\\";
|
d->strRegPath = L"SOFTWARE\\Telldus\\";
|
||||||
d->rootKey = HKEY_LOCAL_MACHINE;
|
d->rootKey = HKEY_LOCAL_MACHINE;
|
||||||
}
|
}
|
||||||
|
@ -39,13 +47,13 @@ Settings::~Settings(void) {
|
||||||
/*
|
/*
|
||||||
* Return the number of stored devices
|
* Return the number of stored devices
|
||||||
*/
|
*/
|
||||||
int Settings::getNumberOfDevices(void) const {
|
int Settings::getNumberOfNodes(Node type) const {
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
TelldusCore::MutexLocker locker(&mutex);
|
||||||
|
|
||||||
int intNumberOfDevices = 0;
|
int intNumberOfNodes = 0;
|
||||||
HKEY hk;
|
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){
|
if(lnExists == ERROR_SUCCESS){
|
||||||
|
|
||||||
|
@ -53,27 +61,27 @@ int Settings::getNumberOfDevices(void) const {
|
||||||
DWORD dNumSubKeys;
|
DWORD dNumSubKeys;
|
||||||
RegQueryInfoKey(hk, NULL, NULL, NULL, &dNumSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
|
RegQueryInfoKey(hk, NULL, NULL, NULL, &dNumSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
|
||||||
|
|
||||||
intNumberOfDevices = (int)dNumSubKeys;
|
intNumberOfNodes = (int)dNumSubKeys;
|
||||||
|
|
||||||
RegCloseKey(hk);
|
RegCloseKey(hk);
|
||||||
}
|
}
|
||||||
return intNumberOfDevices;
|
return intNumberOfNodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Settings::getDeviceId(int intDeviceIndex) const {
|
int Settings::getNodeId(Node type, int intNodeIndex) const {
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
TelldusCore::MutexLocker locker(&mutex);
|
||||||
|
|
||||||
int intReturn = -1;
|
int intReturn = -1;
|
||||||
HKEY hk;
|
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){
|
if(lnExists == ERROR_SUCCESS){
|
||||||
|
|
||||||
wchar_t* Buff = new wchar_t[intMaxRegValueLength];
|
wchar_t* Buff = new wchar_t[intMaxRegValueLength];
|
||||||
DWORD size = 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);
|
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);
|
TelldusCore::MutexLocker locker(&mutex);
|
||||||
|
|
||||||
int intDeviceId = -1;
|
int intNodeId = -1;
|
||||||
HKEY hk;
|
HKEY hk;
|
||||||
|
|
||||||
DWORD dwDisp;
|
DWORD dwDisp;
|
||||||
intDeviceId = getNextDeviceId();
|
intNodeId = getNextNodeId(type);
|
||||||
|
|
||||||
std::wstring strCompleteRegPath = d->strRegPathDevice;
|
std::wstring strCompleteRegPath = d->getNodePath(type);
|
||||||
strCompleteRegPath.append(TelldusCore::intToWstring(intDeviceId));
|
strCompleteRegPath.append(TelldusCore::intToWstring(intNodeId));
|
||||||
|
|
||||||
if (RegCreateKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hk, &dwDisp)) {
|
if (RegCreateKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hk, &dwDisp)) {
|
||||||
//fail
|
//fail
|
||||||
intDeviceId = -1;
|
intNodeId = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
RegCloseKey(hk);
|
RegCloseKey(hk);
|
||||||
return intDeviceId;
|
return intNodeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get next available device id
|
* Get next available device id
|
||||||
*/
|
*/
|
||||||
int Settings::getNextDeviceId() const {
|
int Settings::getNextNodeId(Node type) const {
|
||||||
//Private, no locks needed
|
//Private, no locks needed
|
||||||
int intReturn = -1;
|
int intReturn = -1;
|
||||||
HKEY hk;
|
HKEY hk;
|
||||||
DWORD dwDisp;
|
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){
|
if(lnExists == ERROR_SUCCESS){
|
||||||
|
|
||||||
DWORD dwLength;
|
DWORD dwLength = sizeof(DWORD);
|
||||||
DWORD nResult(0);
|
DWORD nResult(0);
|
||||||
|
|
||||||
long lngStatus = RegQueryValueEx(hk, L"LastUsedId", NULL, NULL, reinterpret_cast<LPBYTE>(&nResult), &dwLength);
|
long lngStatus = RegQueryValueEx(hk, L"LastUsedId", NULL, NULL, reinterpret_cast<LPBYTE>(&nResult), &dwLength);
|
||||||
|
@ -141,11 +149,11 @@ int Settings::getNextDeviceId() const {
|
||||||
/*
|
/*
|
||||||
* Remove a device
|
* Remove a device
|
||||||
*/
|
*/
|
||||||
int Settings::removeDevice(int intDeviceId) {
|
int Settings::removeNode(Node type, int intNodeId) {
|
||||||
TelldusCore::MutexLocker locker(&mutex);
|
TelldusCore::MutexLocker locker(&mutex);
|
||||||
|
|
||||||
std::wstring strCompleteRegPath = d->strRegPathDevice;
|
std::wstring strCompleteRegPath = d->getNodePath(type);
|
||||||
strCompleteRegPath.append(TelldusCore::intToWstring(intDeviceId));
|
strCompleteRegPath.append(TelldusCore::intToWstring(intNodeId));
|
||||||
|
|
||||||
long lngSuccess = RegDeleteKey(d->rootKey, strCompleteRegPath.c_str());
|
long lngSuccess = RegDeleteKey(d->rootKey, strCompleteRegPath.c_str());
|
||||||
|
|
||||||
|
@ -184,12 +192,12 @@ std::wstring Settings::getSetting(const std::wstring &strName) const{
|
||||||
return strReturn;
|
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;
|
std::wstring strReturn;
|
||||||
HKEY hk;
|
HKEY hk;
|
||||||
|
|
||||||
std::wstring strCompleteRegPath = d->strRegPathDevice;
|
std::wstring strCompleteRegPath = d->getNodePath(type);
|
||||||
strCompleteRegPath.append(TelldusCore::intToWstring(intDeviceId));
|
strCompleteRegPath.append(TelldusCore::intToWstring(intNodeId));
|
||||||
long lnExists = RegOpenKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, KEY_QUERY_VALUE, &hk);
|
long lnExists = RegOpenKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, KEY_QUERY_VALUE, &hk);
|
||||||
|
|
||||||
if(lnExists == ERROR_SUCCESS){
|
if(lnExists == ERROR_SUCCESS){
|
||||||
|
@ -212,14 +220,14 @@ std::wstring Settings::getStringSetting(int intDeviceId, const std::wstring &nam
|
||||||
return strReturn;
|
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;
|
HKEY hk;
|
||||||
int ret = TELLSTICK_SUCCESS;
|
int ret = TELLSTICK_SUCCESS;
|
||||||
|
|
||||||
std::wstring bla = TelldusCore::intToWstring(intDeviceId);
|
std::wstring strNodeId = TelldusCore::intToWstring(intNodeId);
|
||||||
std::wstring strCompleteRegPath = d->strRegPathDevice;
|
std::wstring strCompleteRegPath = d->getNodePath(type);
|
||||||
strCompleteRegPath.append(bla);
|
strCompleteRegPath.append(strNodeId);
|
||||||
long lnExists = RegOpenKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, KEY_WRITE, &hk);
|
long lnExists = RegOpenKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, KEY_WRITE, &hk);
|
||||||
|
|
||||||
if (lnExists == ERROR_SUCCESS){
|
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;
|
int intReturn = 0;
|
||||||
|
|
||||||
std::wstring strSetting = getStringSetting(intDeviceId, name, parameter);
|
std::wstring strSetting = getStringSetting(type, intNodeId, name, parameter);
|
||||||
if (strSetting.length()) {
|
if (strSetting.length()) {
|
||||||
intReturn = (int)strSetting[0];
|
intReturn = (int)strSetting[0]; //TODO: do real conversion instead
|
||||||
}
|
}
|
||||||
|
|
||||||
return intReturn;
|
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;
|
int intReturn = TELLSTICK_ERROR_UNKNOWN;
|
||||||
HKEY hk;
|
HKEY hk;
|
||||||
|
|
||||||
std::wstring strCompleteRegPath = d->strRegPathDevice;
|
std::wstring strCompleteRegPath = d->getNodePath(type);
|
||||||
strCompleteRegPath.append(TelldusCore::intToWstring(intDeviceId));
|
strCompleteRegPath.append(TelldusCore::intToWstring(intNodeId));
|
||||||
long lnExists = RegOpenKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, KEY_WRITE, &hk);
|
long lnExists = RegOpenKeyEx(d->rootKey, strCompleteRegPath.c_str(), 0, KEY_WRITE, &hk);
|
||||||
if (lnExists == ERROR_SUCCESS) {
|
if (lnExists == ERROR_SUCCESS) {
|
||||||
DWORD dwVal = value;
|
DWORD dwVal = value;
|
||||||
|
|
|
@ -24,10 +24,9 @@ public:
|
||||||
|
|
||||||
class TellStick : public Controller, public TelldusCore::Thread {
|
class TellStick : public Controller, public TelldusCore::Thread {
|
||||||
public:
|
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 ~TellStick();
|
||||||
|
|
||||||
virtual int firmwareVersion();
|
|
||||||
virtual int pid() const;
|
virtual int pid() const;
|
||||||
virtual int vid() const;
|
virtual int vid() const;
|
||||||
virtual std::string serial() const;
|
virtual std::string serial() const;
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
class TellStick::PrivateData {
|
class TellStick::PrivateData {
|
||||||
public:
|
public:
|
||||||
bool open, running, ignoreControllerConfirmation;
|
bool open, running, ignoreControllerConfirmation;
|
||||||
int vid, pid, fwVersion;
|
int vid, pid;
|
||||||
std::string serial, message;
|
std::string serial, message;
|
||||||
FT_HANDLE ftHandle;
|
FT_HANDLE ftHandle;
|
||||||
TelldusCore::Mutex mutex;
|
TelldusCore::Mutex mutex;
|
||||||
|
@ -40,8 +40,8 @@ public:
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
TellStick::TellStick(int controllerId, TelldusCore::Event *event, const TellStickDescriptor &td )
|
TellStick::TellStick(int controllerId, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent, const TellStickDescriptor &td )
|
||||||
:Controller(controllerId, event)
|
:Controller(controllerId, event, updateEvent)
|
||||||
{
|
{
|
||||||
d = new PrivateData;
|
d = new PrivateData;
|
||||||
#ifdef _WINDOWS
|
#ifdef _WINDOWS
|
||||||
|
@ -54,7 +54,6 @@ TellStick::TellStick(int controllerId, TelldusCore::Event *event, const TellStic
|
||||||
d->running = false;
|
d->running = false;
|
||||||
d->vid = td.vid;
|
d->vid = td.vid;
|
||||||
d->pid = td.pid;
|
d->pid = td.pid;
|
||||||
d->fwVersion = 0;
|
|
||||||
d->serial = td.serial;
|
d->serial = td.serial;
|
||||||
Settings set;
|
Settings set;
|
||||||
d->ignoreControllerConfirmation = set.getSetting(L"ignoreControllerConfirmation")==L"true";
|
d->ignoreControllerConfirmation = set.getSetting(L"ignoreControllerConfirmation")==L"true";
|
||||||
|
@ -109,10 +108,6 @@ void TellStick::setBaud( int baud ) {
|
||||||
FT_SetBaudRate(d->ftHandle, baud);
|
FT_SetBaudRate(d->ftHandle, baud);
|
||||||
}
|
}
|
||||||
|
|
||||||
int TellStick::firmwareVersion() {
|
|
||||||
return d->fwVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
int TellStick::pid() const {
|
int TellStick::pid() const {
|
||||||
return d->pid;
|
return d->pid;
|
||||||
}
|
}
|
||||||
|
@ -148,7 +143,7 @@ void TellStick::processData( const std::string &data ) {
|
||||||
continue;
|
continue;
|
||||||
} else if (data[i] == 10) { // \n found
|
} else if (data[i] == 10) { // \n found
|
||||||
if (d->message.substr(0,2).compare("+V") == 0) {
|
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) {
|
} else if (d->message.substr(0,2).compare("+R") == 0) {
|
||||||
this->publishData(d->message.substr(2));
|
this->publishData(d->message.substr(2));
|
||||||
} else if(d->message.substr(0,2).compare("+W") == 0) {
|
} else if(d->message.substr(0,2).compare("+W") == 0) {
|
||||||
|
|
|
@ -34,7 +34,7 @@ typedef int DWORD;
|
||||||
class TellStick::PrivateData {
|
class TellStick::PrivateData {
|
||||||
public:
|
public:
|
||||||
bool open, ignoreControllerConfirmation;
|
bool open, ignoreControllerConfirmation;
|
||||||
int vid, pid, fwVersion;
|
int vid, pid;
|
||||||
std::string serial, message;
|
std::string serial, message;
|
||||||
ftdi_context ftHandle;
|
ftdi_context ftHandle;
|
||||||
EVENT_HANDLE eh;
|
EVENT_HANDLE eh;
|
||||||
|
@ -42,14 +42,13 @@ public:
|
||||||
TelldusCore::Mutex mutex;
|
TelldusCore::Mutex mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
TellStick::TellStick(int controllerId, TelldusCore::Event *event, const TellStickDescriptor &td )
|
TellStick::TellStick(int controllerId, TelldusCore::EventRef event, TelldusCore::EventRef updateEvent, const TellStickDescriptor &td )
|
||||||
:Controller(controllerId, event)
|
:Controller(controllerId, event, updateEvent)
|
||||||
{
|
{
|
||||||
d = new PrivateData;
|
d = new PrivateData;
|
||||||
d->open = false;
|
d->open = false;
|
||||||
d->vid = td.vid;
|
d->vid = td.vid;
|
||||||
d->pid = td.pid;
|
d->pid = td.pid;
|
||||||
d->fwVersion = 0;
|
|
||||||
d->serial = td.serial;
|
d->serial = td.serial;
|
||||||
d->running = false;
|
d->running = false;
|
||||||
|
|
||||||
|
@ -96,10 +95,6 @@ TellStick::~TellStick() {
|
||||||
delete d;
|
delete d;
|
||||||
}
|
}
|
||||||
|
|
||||||
int TellStick::firmwareVersion() {
|
|
||||||
return d->fwVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
int TellStick::pid() const {
|
int TellStick::pid() const {
|
||||||
return d->pid;
|
return d->pid;
|
||||||
}
|
}
|
||||||
|
@ -135,7 +130,7 @@ void TellStick::processData( const std::string &data ) {
|
||||||
continue;
|
continue;
|
||||||
} else if (data[i] == 10) { // \n found
|
} else if (data[i] == 10) { // \n found
|
||||||
if (d->message.substr(0,2).compare("+V") == 0) {
|
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) {
|
} else if (d->message.substr(0,2).compare("+R") == 0) {
|
||||||
this->publishData(d->message.substr(2));
|
this->publishData(d->message.substr(2));
|
||||||
} else if(d->message.substr(0,2).compare("+W") == 0) {
|
} 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.setInterval(60); //Once every minute
|
||||||
supervisor.start();
|
supervisor.start();
|
||||||
|
|
||||||
ControllerManager controllerManager(dataEvent.get());
|
|
||||||
EventUpdateManager eventUpdateManager;
|
EventUpdateManager eventUpdateManager;
|
||||||
TelldusCore::EventRef deviceUpdateEvent = eventUpdateManager.retrieveUpdateEvent();
|
TelldusCore::EventRef deviceUpdateEvent = eventUpdateManager.retrieveUpdateEvent();
|
||||||
eventUpdateManager.start();
|
eventUpdateManager.start();
|
||||||
|
ControllerManager controllerManager(dataEvent, deviceUpdateEvent);
|
||||||
DeviceManager deviceManager(&controllerManager, deviceUpdateEvent);
|
DeviceManager deviceManager(&controllerManager, deviceUpdateEvent);
|
||||||
|
|
||||||
ConnectionListener clientListener(L"TelldusClient", clientEvent);
|
ConnectionListener clientListener(L"TelldusClient", clientEvent);
|
||||||
|
@ -91,7 +91,7 @@ void TelldusMain::start(void) {
|
||||||
TelldusCore::EventDataRef eventDataRef = clientEvent->takeSignal();
|
TelldusCore::EventDataRef eventDataRef = clientEvent->takeSignal();
|
||||||
ConnectionListenerEventData *data = reinterpret_cast<ConnectionListenerEventData*>(eventDataRef.get());
|
ConnectionListenerEventData *data = reinterpret_cast<ConnectionListenerEventData*>(eventDataRef.get());
|
||||||
if (data) {
|
if (data) {
|
||||||
ClientCommunicationHandler *clientCommunication = new ClientCommunicationHandler(data->socket, handlerEvent, &deviceManager, deviceUpdateEvent);
|
ClientCommunicationHandler *clientCommunication = new ClientCommunicationHandler(data->socket, handlerEvent, &deviceManager, deviceUpdateEvent, &controllerManager);
|
||||||
clientCommunication->start();
|
clientCommunication->start();
|
||||||
clientCommunicationHandlerList.push_back(clientCommunication);
|
clientCommunicationHandlerList.push_back(clientCommunication);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,11 +8,12 @@ IF (BUILD_LIBTELLDUS-GUI)
|
||||||
SET(BUILD_PLUGIN_SYSTRAY TRUE CACHE BOOL "Build plugin 'Systray'")
|
SET(BUILD_PLUGIN_SYSTRAY TRUE CACHE BOOL "Build plugin 'Systray'")
|
||||||
ENDIF (BUILD_LIBTELLDUS-GUI)
|
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_DBUS FALSE CACHE BOOL "Build plugin 'DBus'")
|
||||||
SET(BUILD_PLUGIN_LIVE FALSE CACHE BOOL "Build plugin 'Telldus Live!'")
|
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_SCHEDULERGUISIMPLE FALSE CACHE BOOL "Build plugin 'Simple Scheduler GUI'")
|
||||||
SET(BUILD_PLUGIN_SENSORS TRUE CACHE BOOL "Build plugin 'Sensors'")
|
SET(BUILD_PLUGIN_SENSORS TRUE CACHE BOOL "Build plugin 'Sensors'")
|
||||||
|
SET(BUILD_PLUGIN_XPL FALSE CACHE BOOL "Build plugin 'xPL'")
|
||||||
|
|
||||||
ADD_SUBDIRECTORY(telldus)
|
ADD_SUBDIRECTORY(telldus)
|
||||||
|
|
||||||
|
@ -28,6 +29,10 @@ IF(BUILD_PLUGIN_SYSTRAY)
|
||||||
ADD_SUBDIRECTORY(Systray)
|
ADD_SUBDIRECTORY(Systray)
|
||||||
ENDIF(BUILD_PLUGIN_SYSTRAY)
|
ENDIF(BUILD_PLUGIN_SYSTRAY)
|
||||||
|
|
||||||
|
IF(BUILD_PLUGIN_CONTROLLERS)
|
||||||
|
ADD_SUBDIRECTORY(Controllers)
|
||||||
|
ENDIF()
|
||||||
|
|
||||||
IF(BUILD_PLUGIN_DBUS)
|
IF(BUILD_PLUGIN_DBUS)
|
||||||
ADD_SUBDIRECTORY(DBus)
|
ADD_SUBDIRECTORY(DBus)
|
||||||
ENDIF(BUILD_PLUGIN_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());
|
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:
|
public slots:
|
||||||
void load(const QString &url);
|
void load(const QString &url);
|
||||||
void setProperty( const QString & name, const QScriptValue &value );
|
void setProperty( const QString & name, const QScriptValue &value );
|
||||||
|
void sizeRootObjectToView(bool enable);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class PrivateData;
|
class PrivateData;
|
||||||
|
|
|
@ -136,6 +136,26 @@ QVariant TelldusCoreObject::sensorValue(const QString &protocol, const QString &
|
||||||
return retval;
|
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) {
|
void TelldusCoreObject::triggerError(int deviceId, int errorId) {
|
||||||
char *errorString = tdGetErrorString( errorId );
|
char *errorString = tdGetErrorString( errorId );
|
||||||
QString message = QString::fromUtf8( errorString );
|
QString message = QString::fromUtf8( errorString );
|
||||||
|
|
|
@ -32,6 +32,8 @@ public slots:
|
||||||
QVariant sensor() const;
|
QVariant sensor() const;
|
||||||
QVariant sensorValue(const QString &protocol, const QString &model, int id, int dataType) const;
|
QVariant sensorValue(const QString &protocol, const QString &model, int id, int dataType) const;
|
||||||
|
|
||||||
|
QVariant controller() const;
|
||||||
|
|
||||||
int turnOn( int deviceId );
|
int turnOn( int deviceId );
|
||||||
int turnOff( int deviceId );
|
int turnOff( int deviceId );
|
||||||
int up( 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);
|
QWidget *widget = loader.load(&file, this);
|
||||||
file.close();
|
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);
|
int index = d->stackedLayout->addWidget(widget);
|
||||||
|
|
||||||
QListWidgetItem *item = new QListWidgetItem(QIcon(dir.filePath(icon)), name, d->listWidget);
|
QListWidgetItem *item = new QListWidgetItem(QIcon(dir.filePath(icon)), name, d->listWidget);
|
||||||
|
|
|
@ -20,6 +20,7 @@ signals:
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
QScriptValue addPage( const QString &name, const QString &file, const QString &icon );
|
QScriptValue addPage( const QString &name, const QString &file, const QString &icon );
|
||||||
|
QScriptValue addPage( const QString &name, QWidget *widget, const QString &icon);
|
||||||
void open();
|
void open();
|
||||||
void valueChanged();
|
void valueChanged();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue