[Iotcon] startCaching(), stopCaching(), res.cachedRepresentation
authorPiotr Kosko <p.kosko@samsung.com>
Tue, 16 Feb 2016 12:41:45 +0000 (13:41 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Thu, 18 Feb 2016 07:06:32 +0000 (16:06 +0900)
[Verification] Code compiles, functionalities tested in console.

Change-Id: I6ebf9a650b661c5c3dcc9857d96f9a58fe8edc36
Signed-off-by: Piotr Kosko <p.kosko@samsung.com>
src/iotcon/iotcon_api.js
src/iotcon/iotcon_instance.cc

index a97da630178bc3b48f04a257cda78957054e53e7..7548ac050288a23ab0a8ffd1cc310a5795b57ba8 100644 (file)
@@ -817,6 +817,8 @@ RemoteResource.prototype.startCaching = function() {
 
   if (native.isFailure(result)) {
     throw native.getErrorObject(result);
+  } else {
+    manageId(this, native.getResultObject(result));
   }
 };
 
@@ -827,6 +829,8 @@ RemoteResource.prototype.stopCaching = function() {
 
   if (native.isFailure(result)) {
     throw native.getErrorObject(result);
+  } else {
+    manageId(this, native.getResultObject(result));
   }
 };
 
index 5f18e9ba0603ceb2e02ae18fca934bf010583df2..8634735aa7aead67c433d72893aab267a101f963 100644 (file)
@@ -486,7 +486,27 @@ common::TizenResult IotconInstance::ResponseSend(const picojson::object& args) {
 
 common::TizenResult IotconInstance::RemoteResourceGetCachedRepresentation(const picojson::object& args) {
   ScopeLogger();
-  return common::UnknownError("Not implemented");
+
+  FoundRemoteInfoPtr ptr;
+  auto res = IotconUtils::RemoteResourceFromJson(args, &ptr);
+  if (!res) {
+    LogAndReturnTizenError(res, ("Failed to build resource using json data"));
+  }
+  iotcon_representation_h representation = nullptr;
+  res = IotconUtils::ConvertIotconError(
+      iotcon_remote_resource_get_cached_representation(ptr->handle, &representation));
+  if (!res) {
+    LogAndReturnTizenError(res, ("Gathering cached representation failed"));
+  }
+  if (representation) {
+    picojson::value repr_json{picojson::object{}};
+    res = IotconUtils::RepresentationToJson(representation, &repr_json.get<picojson::object>());
+    if (!res) {
+      LogAndReturnTizenError(res, ("RepresentationToJson() failed"));
+    }
+    return common::TizenSuccess{repr_json};
+  }
+  return common::UnknownError("Failed to gather cached representation");
 }
 
 common::TizenResult IotconInstance::RemoteResourceMethodGet(const picojson::object& args,
@@ -523,14 +543,41 @@ common::TizenResult IotconInstance::RemoteResourceUnsetStateChangeListener(const
   return common::UnknownError("Not implemented");
 }
 
+static void RepresentationChangedCallback(iotcon_remote_resource_h resource,
+                                          iotcon_representation_h representation,
+                                          void *user_data) {
+  LoggerD("Entered");
+  //TODO probably should be handled
+}
+
 common::TizenResult IotconInstance::RemoteResourceStartCaching(const picojson::object& args) {
   ScopeLogger();
-  return common::UnknownError("Not implemented");
+  FoundRemoteInfoPtr ptr;
+  auto result = IotconUtils::RemoteResourceFromJson(args, &ptr);
+  if (!result) {
+    LogAndReturnTizenError(result, ("Failed to create remote resource handle"));
+  }
+  result = IotconUtils::ConvertIotconError(
+      iotcon_remote_resource_start_caching(ptr->handle, RepresentationChangedCallback, nullptr));
+  if (!result) {
+    return result;
+  }
+  return common::TizenSuccess{IotconClientManager::GetInstance().StoreRemoteResource(ptr)};
 }
 
 common::TizenResult IotconInstance::RemoteResourceStopCaching(const picojson::object& args) {
   ScopeLogger();
-  return common::UnknownError("Not implemented");
+  FoundRemoteInfoPtr ptr;
+  auto result = IotconUtils::RemoteResourceFromJson(args, &ptr);
+  if (!result) {
+    LogAndReturnTizenError(result, ("Failed to create remote resource handle"));
+  }
+  result = IotconUtils::ConvertIotconError(
+      iotcon_remote_resource_stop_caching(ptr->handle));
+  if (!result) {
+    return result;
+  }
+  return common::TizenSuccess{IotconClientManager::GetInstance().RemoveRemoteResource(ptr)};
 }
 
 common::TizenResult IotconInstance::RemoteResourceSetConnectionChangeListener(const picojson::object& args) {