Rename the class Json to avoid symbol conflicts with Jsoncpp
[platform/core/context/context-provider.git] / src / sensor / SensorProvider.cpp
index 32f0584..6bf6ee5 100644 (file)
 
 using namespace ctx;
 
+std::map<std::string, SensorProvider*> SensorProvider::__providerMap;
+
 SensorProvider::SensorProvider(const char *subject) :
        ContextProvider(subject),
        sensorLogger(NULL)
 {
+       __providerMap[subject] = this;
 }
 
 SensorProvider::~SensorProvider()
 {
+       __providerMap.erase(getSubject());
        delete sensorLogger;
 }
 
-int SensorProvider::subscribe(Json option, Json *requestResult)
+int SensorProvider::subscribe(CtxJson1 option, CtxJson1 *requestResult)
 {
        return ERR_NONE;
 }
 
-int SensorProvider::unsubscribe(Json option)
+int SensorProvider::unsubscribe(CtxJson1 option)
 {
        return ERR_NONE;
 }
 
-int SensorProvider::read(Json option, Json *requestResult)
+int SensorProvider::read(CtxJson1 option, CtxJson1 *requestResult)
 {
        int endTime = static_cast<int>(time(NULL)) + 1;
        int startTime = endTime - DEFAULT_QUERY_PERIOD - 1;
@@ -75,6 +79,8 @@ int SensorProvider::read(Json option, Json *requestResult)
        Querier *querier = getQuerier(option);
        IF_FAIL_RETURN(querier, ERR_OPERATION_FAILED);
 
+       sensorLogger->flushCache(true);
+
        if (interval == 0)
                ret = querier->queryRaw(startTime, endTime);
        else if (interval > 0)
@@ -88,12 +94,13 @@ int SensorProvider::read(Json option, Json *requestResult)
        return ret;
 }
 
-int SensorProvider::write(Json data, Json *requestResult)
+int SensorProvider::write(CtxJson1 data, CtxJson1 *requestResult)
 {
        IF_FAIL_RETURN(sensorLogger, ERR_OPERATION_FAILED);
 
        std::string operation;
        std::string pkgId;
+       CtxJson1 option;
        int retentionPeriod = DEFAULT_RETENTION;
 
        _J("Data", data);
@@ -101,22 +108,33 @@ int SensorProvider::write(Json data, Json *requestResult)
        IF_FAIL_RETURN(data.get(NULL, KEY_OPERATION, &operation), ERR_INVALID_PARAMETER);
        IF_FAIL_RETURN(data.get(NULL, KEY_CLIENT_PKG_ID, &pkgId), ERR_INVALID_PARAMETER);
 
-       if (data.get(NULL, KEY_RETENTION, &retentionPeriod))
+       data.get(NULL, KEY_OPTION, &option);
+
+       if (option.get(NULL, KEY_RETENTION, &retentionPeriod)) {
                retentionPeriod *= SECONDS_PER_HOUR;
+               option.remove(NULL, KEY_RETENTION);
+       }
 
-       /* TODO: remove the operation & pkg id from the json */
+       IF_FAIL_RETURN(verifyOption(option), ERR_INVALID_PARAMETER);
 
        if (operation == VAL_START)
-               return __addClient(pkgId, retentionPeriod, data);
+               return __addClient(pkgId, retentionPeriod, option);
        else if (operation == VAL_STOP)
                return __removeClient(pkgId);
 
        return ERR_NOT_SUPPORTED;
 }
 
-int SensorProvider::__addClient(std::string pkgId, int retentionPeriod, Json option)
+bool SensorProvider::verifyOption(CtxJson1 option)
+{
+       std::list<std::string> keys;
+       option.getKeys(&keys);
+       return keys.size() == 0;
+}
+
+int SensorProvider::__addClient(std::string pkgId, int retentionPeriod, CtxJson1 option)
 {
-       Json tmp;
+       CtxJson1 tmp;
        int ret;
 
        /* Validate the retention period */
@@ -139,7 +157,7 @@ int SensorProvider::__addClient(std::string pkgId, int retentionPeriod, Json opt
 
 int SensorProvider::__removeClient(std::string pkgId)
 {
-       std::vector<Json> options;
+       std::vector<CtxJson1> options;
        int ret;
 
        /* Remove the app's request first */
@@ -147,11 +165,23 @@ int SensorProvider::__removeClient(std::string pkgId)
 
        /* Check if there is no client anymore */
        ret = __clientInfo.get(getSubject(), options);
-       IF_FAIL_RETURN(ret != ERR_NONE, ERR_NONE);
-       IF_FAIL_RETURN(ret == ERR_NO_DATA, ERR_OPERATION_FAILED);
 
-       /* Stop listening */
-       sensorLogger->stop();
+       if (ret == ERR_NONE) {
+               /* Still, one or more clients exist */
+               /* If necessary, the logger restarts its logging logic with updated parameters */
+               sensorLogger->start();
+               return ERR_NONE;
 
-       return ERR_NONE;
+       } else if (ret == ERR_NO_DATA) {
+               /* No client */
+               sensorLogger->stop();
+               return ERR_NONE;
+       }
+
+       return ERR_OPERATION_FAILED;
+}
+
+void SensorProvider::removeClient(std::string subject, std::string pkgId)
+{
+       __providerMap[subject]->__removeClient(pkgId);
 }