Added functions tdSensor() and tdSensorValue() to the client library. The function tdSensor() caches the result and only returns one sensor per call
This commit is contained in:
parent
ad1b182a71
commit
8eaaf6f4a6
4 changed files with 72 additions and 1 deletions
|
@ -32,7 +32,8 @@ public:
|
||||||
Socket eventSocket;
|
Socket eventSocket;
|
||||||
RawDeviceEventList rawDeviceEventList;
|
RawDeviceEventList rawDeviceEventList;
|
||||||
SensorEventList sensorEventList;
|
SensorEventList sensorEventList;
|
||||||
bool running;
|
bool running, sensorCached;
|
||||||
|
std::wstring sensorCache;
|
||||||
TelldusCore::Mutex mutex;
|
TelldusCore::Mutex mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -44,6 +45,7 @@ Client::Client()
|
||||||
d = new PrivateData;
|
d = new PrivateData;
|
||||||
d->lastCallbackId = 0;
|
d->lastCallbackId = 0;
|
||||||
d->running = true;
|
d->running = true;
|
||||||
|
d->sensorCached = false;
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,3 +321,40 @@ bool Client::unregisterCallback( int callbackId ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Client::getSensor(char *protocol, int protocolLen, char *model, int modelLen, int *sensorId, int *dataTypes) {
|
||||||
|
if (!d->sensorCached) {
|
||||||
|
Message msg(L"tdSensor");
|
||||||
|
std::wstring response = Client::getWStringFromService(msg);
|
||||||
|
int count = Message::takeInt(&response);
|
||||||
|
d->sensorCached = true;
|
||||||
|
d->sensorCache = L"";
|
||||||
|
if (count > 0) {
|
||||||
|
d->sensorCache = response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (d->sensorCache == L"") {
|
||||||
|
d->sensorCached = false;
|
||||||
|
return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::wstring p = Message::takeString(&d->sensorCache);
|
||||||
|
std::wstring m = Message::takeString(&d->sensorCache);
|
||||||
|
int id = Message::takeInt(&d->sensorCache);
|
||||||
|
int dt = Message::takeInt(&d->sensorCache);
|
||||||
|
|
||||||
|
if (protocol && protocolLen) {
|
||||||
|
strncpy(protocol, TelldusCore::wideToString(p).c_str(), protocolLen);
|
||||||
|
}
|
||||||
|
if (model && modelLen) {
|
||||||
|
strncpy(model, TelldusCore::wideToString(m).c_str(), modelLen);
|
||||||
|
}
|
||||||
|
if (sensorId) {
|
||||||
|
(*sensorId) = id;
|
||||||
|
}
|
||||||
|
if (dataTypes) {
|
||||||
|
(*dataTypes) = dt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TELLSTICK_SUCCESS;
|
||||||
|
}
|
||||||
|
|
|
@ -25,6 +25,8 @@ namespace TelldusCore {
|
||||||
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);
|
||||||
|
|
||||||
static bool getBoolFromService(const Message &msg);
|
static bool getBoolFromService(const Message &msg);
|
||||||
static int getIntegerFromService(const Message &msg);
|
static int getIntegerFromService(const Message &msg);
|
||||||
static std::wstring getWStringFromService(const Message &msg);
|
static std::wstring getWStringFromService(const Message &msg);
|
||||||
|
|
|
@ -534,5 +534,32 @@ void WINAPI tdDisconnectTellStickController(int vid, int pid, const char *serial
|
||||||
Client::getWStringFromService(msg);
|
Client::getWStringFromService(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int WINAPI tdSensor(char *protocol, int protocolLen, char *model, int modelLen, int *id, int *dataTypes) {
|
||||||
|
Client *client = Client::getInstance();
|
||||||
|
return client->getSensor(protocol, protocolLen, model, modelLen, id, dataTypes);
|
||||||
|
}
|
||||||
|
|
||||||
|
int WINAPI tdSensorValue(const char *protocol, const char *model, int id, int dataType, char *value, int len, int *timestamp) {
|
||||||
|
Message msg(L"tdSensorValue");
|
||||||
|
msg.addArgument(protocol);
|
||||||
|
msg.addArgument(model);
|
||||||
|
msg.addArgument(id);
|
||||||
|
msg.addArgument(dataType);
|
||||||
|
std::wstring retval = Client::getWStringFromService(msg);
|
||||||
|
if (retval.length() == 0) {
|
||||||
|
return TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::wstring v = Message::takeString(&retval);
|
||||||
|
int t = Message::takeInt(&retval);
|
||||||
|
if (value && len) {
|
||||||
|
strncpy(value, TelldusCore::wideToString(v).c_str(), len);
|
||||||
|
}
|
||||||
|
if (timestamp) {
|
||||||
|
(*timestamp) = t;
|
||||||
|
}
|
||||||
|
return TELLSTICK_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*\@}*/
|
/*\@}*/
|
||||||
|
|
|
@ -83,6 +83,9 @@ extern "C" {
|
||||||
TELLSTICK_API void WINAPI tdConnectTellStickController(int vid, int pid, const char *serial);
|
TELLSTICK_API void WINAPI tdConnectTellStickController(int vid, int pid, const char *serial);
|
||||||
TELLSTICK_API void WINAPI tdDisconnectTellStickController(int vid, int pid, const char *serial);
|
TELLSTICK_API void WINAPI tdDisconnectTellStickController(int vid, int pid, const char *serial);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue