#include <rda/RDAService.h>
#include <rda/Config.h>
static rdaRDAService* rda = 0;
static rdaDeviceHandle* device = 0;
static rdaRequest* request = 0;
static void printValue(double value, double timestamp)
{
time_t sec = (time_t)(timestamp/1000);
printf("%4.1f %s", value, ctime(&sec));
}
static void printValueData(const rdaData& data)
{
try
{
double value = data.extractDouble("value");
double timestamp = data.extractDouble("timestamp");
printValue(value, timestamp);
}
catch(...)
{
data.print();
}
}
static void printError(const rdaException& ex)
{
printf("%s: %s\n", ex.getType(), ex.getMessage());
}
static void doGet(const char* prop)
{
try
{
rdaData* value = device->get(prop);
printValueData(*value);
delete value;
}
catch(const rdaException& ex)
{
printError(ex);
}
}
static void doSet(const char* prop, double value)
{
try
{
rdaData data;
data.insert("value", value);
device->set(prop, data);
}
catch(const rdaException& ex)
{
printError(ex);
}
}
class SubscriptionReportHandler : public rdaReplyHandler
{
public:
virtual void handleReply(const rdaRequest& rq, const rdaData& value)
{
printf("%s/%s = ",
rq.getDeviceHandle()->getDeviceName(),
rq.getPropertyName());
printValueData(value);
}
virtual void handleError(const rdaRequest& rq, const rdaException& ex)
{
printf("%s/%s - error reported\n",
rq.getDeviceHandle()->getDeviceName(),
rq.getPropertyName());
printError(ex);
}
virtual void disconnected(const rdaRequest& rq)
{
printf("%s/%s - disconnected from data source\n",
rq.getDeviceHandle()->getDeviceName(),
rq.getPropertyName());
}
virtual void reconnected(const rdaRequest& rq)
{
printf("%s/%s - reconnected to data source\n",
rq.getDeviceHandle()->getDeviceName(),
rq.getPropertyName());
}
virtual void cancelled(const rdaRequest& rq)
{
printf("%s/%s - subscription cancelled\n",
rq.getDeviceHandle()->getDeviceName(),
rq.getPropertyName());
}
};
static SubscriptionReportHandler replyHandler;
static void doMon(const char* prop)
{
try
{
if (request != 0) device->monitorOff(request);
request = device->monitorOn(prop, false, &replyHandler);
}
catch(const rdaException& ex)
{
printError(ex);
}
}
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("Usage: %s <server name>\n", argv[0]);
return 1;
}
rdaConfig::setProperty("cmw.rda.testServer", argv[1]);
try
{
rda = rdaRDAService::init();
while (true)
{
char command[100];
char ss1[21];
char ss2[21];
char ss3[21];
printf("=> ");
fgets(command, 100, stdin);
if (*command == '\n')
{
if (request != 0) device->monitorOff(request);
continue;
}
int count = sscanf(command, "%20s %20s %20s", ss1, ss2, ss3);
if (count == 1 && !strcmp(ss1, "device"))
{
if (device == 0) printf("Device not defined\n");
else printf("%s\n", device->getDeviceName());
}
else if (count == 2 && !strcmp(ss1, "device"))
{
try { device = rda->getDeviceHandle(ss2); }
catch(...) { printf("Device not found\n"); }
}
else if (count == 2 && !strcmp(ss1, "get"))
{
doGet(ss2);
}
else if (count == 3 && !strcmp(ss1, "set"))
{
double value = 0;
int n = sscanf(ss3, "%lf", &value);
if (n == 0) printf("Invalid value\n");
else doSet(ss2, value);
}
else if (count == 2 && !strcmp(ss1, "mon"))
{
doMon(ss2);
}
else if (count == 1 && !strcmp(ss1, "quit"))
{
return 0;
}
else printf("Invalid command\n");
}
}
catch(const rdaInternalException& ex)
{
printf("RDA internal exception: %s\n", ex.getMessage());
return 1;
}
catch(const rdaInternalError& ex)
{
printf("RDA internal error [%s:%d]: %s\n",
ex.getFile(), ex.getLine(), ex.getMessage());
return 1;
}
catch(...)
{
printf("Cannot proceed: unknown exception\n");
return 1;
}
return 0;
}