Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / examples / garageclient.cpp
index ba8a09a..c64528d 100644 (file)
@@ -32,6 +32,7 @@ using namespace OC;
 
 const int SUCCESS_RESPONSE = 0;
 std::shared_ptr<OCResource> curResource;
+std::mutex curResourceLock;
 
 class Garage
 {
@@ -43,6 +44,7 @@ public:
     std::vector<int> m_lightPowers;
     OCRepresentation m_lightRep;
     std::vector<OCRepresentation> m_reps;
+    std::vector<std::vector<int>> m_hingeStates;
 
     Garage() : m_state(false), m_name("")
     {
@@ -57,7 +59,7 @@ void printRepresentation(const OCRepresentation& rep)
         // Check if attribute "name" exists, and then getValue
         if(rep.hasAttribute("name"))
         {
-            myGarage.m_name = rep.getValue<std::string>("name");
+            myGarage.m_name = rep["name"];
         }
         std::cout << "\tname: " << myGarage.m_name << std::endl;
 
@@ -92,10 +94,10 @@ void printRepresentation(const OCRepresentation& rep)
             std::cout << "\tnullAttribute is not null." << std::endl;
         }
 
-        rep.getValue("light", myGarage.m_lightRep);
+        myGarage.m_lightRep = rep["light"];
 
-        myGarage.m_lightRep.getValue("states", myGarage.m_lightStates);
-        myGarage.m_lightRep.getValue("powers", myGarage.m_lightPowers);
+        myGarage.m_lightStates = myGarage.m_lightRep["states"];
+        myGarage.m_lightPowers = myGarage.m_lightRep["powers"];
 
         std::cout << "\tlightRep: states: ";
 
@@ -131,13 +133,26 @@ void printRepresentation(const OCRepresentation& rep)
         std::cout << std::endl;
 
         // Get vector of representations
-        rep.getValue("reps", myGarage.m_reps);
-        // Client know that server is sending two representations
-        // and has key1 and key2 repsectively
-        std::cout << "\treps[0].key1: " << myGarage.m_reps[0].getValue<int>("key1") << std::endl;
-        std::cout << "\treps[0].key2: " << myGarage.m_reps[1].getValue<int>("key2") << std::endl;
+        myGarage.m_reps = rep["reps"];
+
+        int ct = 0;
+        for(auto& rep : myGarage.m_reps)
+        {
+            for(auto& attribute : rep)
+            {
+                std::cout<< "\treps["<<ct<<"]."<<attribute.attrname()<<":"
+                    << attribute.type()<<" with value " <<attribute.getValueToString() <<std::endl;
+            }
+            ++ct;
+        }
+
+        std::cout << "\tjson: " << rep["json"] << std::endl;
+        myGarage.m_hingeStates = rep["hinges"];
+
+        std::cout<< "\tHinge parameter is type: " << rep["hinges"].type() << " with depth "<<
+            rep["hinges"].depth() << " and a base type of "<< rep["hinges"].base_type()<<std::endl;
+
 
-        std::cout << "\tjson: " << rep.getValue<std::string>("json") << std::endl;
 }
 // callback handler on PUT request
 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
@@ -166,7 +181,7 @@ void putLightRepresentation(std::shared_ptr<OCResource> resource)
 
         myGarage.m_state = true;
 
-        rep.setValue("state", myGarage.m_state);
+        rep["state"] = myGarage.m_state;
 
         // Create QueryParameters Map and add query params (if any)
         QueryParamsMap queryParamsMap;
@@ -211,9 +226,11 @@ void getLightRepresentation(std::shared_ptr<OCResource> resource)
 // Callback to found resources
 void foundResource(std::shared_ptr<OCResource> resource)
 {
+    std::lock_guard<std::mutex> lock(curResourceLock);
     if(curResource)
     {
         std::cout << "Found another resource, ignoring"<<std::endl;
+        return;
     }
 
     std::string resourceURI;
@@ -263,12 +280,14 @@ void foundResource(std::shared_ptr<OCResource> resource)
     }
     catch(std::exception& e)
     {
-        //log(e.what());
+        std::cerr << "Exception in foundResource: "<< e.what()<<std::endl;
     }
 }
 
 int main(int argc, char* argv[]) {
 
+    std::ostringstream requestURI;
+
     // Create PlatformConfig object
     PlatformConfig cfg {
         OC::ServiceType::InProc,
@@ -282,8 +301,11 @@ int main(int argc, char* argv[]) {
     try
     {
         // Find all resources
-        OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.garage",
-                    &foundResource);
+        requestURI << OC_MULTICAST_DISCOVERY_URI << "?rt=core.garage";
+
+        OCPlatform::findResource("", requestURI.str(),
+                OC_ALL, &foundResource);
+
         std::cout<< "Finding Resource... " <<std::endl;
 
         // A condition variable will free the mutex it is given, then do a non-
@@ -303,3 +325,4 @@ int main(int argc, char* argv[]) {
     return 0;
 }
 
+