Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / resource-encapsulation / examples / linux / SampleResourceServer.cpp
old mode 100755 (executable)
new mode 100644 (file)
index f7c946c..e118781
@@ -29,25 +29,39 @@ using namespace OIC::Service;
 
 constexpr int DEFALUT_VALUE = 0;
 
+constexpr int REQUEST_TEMP = 1;
+constexpr int REQUEST_LIGHT = 2;
+
+constexpr int PRESENCE_ON = 1;
+constexpr int PRESENCE_OFF = 2;
+
 constexpr int DEFALUT_SERVER = 1;
 constexpr int CUSTOM_SERVER = 2;
 constexpr int STOP = 3;
 
 constexpr int INCREASE_TEMPERATURE = 1;
 constexpr int DECREASE_TEMPERATURE = 2;
-constexpr int STOP_SENSOR = 3;
+constexpr int STOP_TEMPERATURE_SENSOR = 3;
+
+constexpr int INCREASE_BRIGHTNESS = 1;
+constexpr int DECREASE_BRIGHTNESS = 2;
+constexpr int STOP_LIGHT_SENSOR = 3;
 
 constexpr int CORRECT_INPUT = 1;
 constexpr int INCORRECT_INPUT = 2;
 constexpr int QUIT = 3;
 
-std::string resourceUri = "/a/TempSensor";
-std::string resourceType = "core.TemperatureSensor";
+
+std::string resourceType = "oic.r.temperaturesensor";
 std::string resourceInterface = "oic.if.";
-std::string attributeKey = "Temperature";
+std::string resourceUri;
+std::string attributeKey;
+int isPresenceOn = PRESENCE_ON;
 
 RCSResourceObject::Ptr server;
 
+int processUserInput();
+
 enum class Control{
     INCREASE,
     DECREASE
@@ -65,11 +79,29 @@ void displayMenu()
               << std::endl;
 }
 
+void displayResourceTypeMenu()
+{
+    std::cout << "========================================================" << std::endl;
+    std::cout << "Select Resource Type" << std::endl;
+    std::cout << "1. Temperature" << std::endl;
+    std::cout << "2. Light" << std::endl;
+    std::cout << "========================================================" << std::endl;
+}
+
 void displayControlTemperatureMenu()
 {
     std::cout << "========================================================" << std::endl;
-    std::cout << "1. Increase Temperature by 10 degree" << std::endl;
-    std::cout << "2. Decrease Temperature by 10 degree" << std::endl;
+    std::cout << "1. Increase Temperature by 1 degree" << std::endl;
+    std::cout << "2. Decrease Temperature by 1 degree" << std::endl;
+    std::cout << "3. Stop the Sensor" << std::endl;
+    std::cout << "========================================================" << std::endl;
+}
+
+void displayControlLightMenu()
+{
+    std::cout << "========================================================" << std::endl;
+    std::cout << "1. Increase Brightness by 1 stage" << std::endl;
+    std::cout << "2. Decrease Brightness by 1 stage" << std::endl;
     std::cout << "3. Stop the Sensor" << std::endl;
     std::cout << "========================================================" << std::endl;
 }
@@ -87,8 +119,7 @@ void printAttribute(const RCSResourceAttributes& attrs)
 RCSGetResponse requestHandlerForGet(const RCSRequest& request,
         RCSResourceAttributes& attrs)
 {
-    std::cout << "Recieved a Get request from Client" << std::endl;
-
+    std::cout << "Received a Get request from Client" << std::endl;
     RCSResourceObject::LockGuard lock(*server);
     RCSResourceAttributes attributes = server->getAttributes();
 
@@ -102,7 +133,7 @@ RCSGetResponse requestHandlerForGet(const RCSRequest& request,
 RCSSetResponse requestHandlerForSet(const RCSRequest& request,
         RCSResourceAttributes& attrs)
 {
-    std::cout << "Recieved a Set request from Client" << std::endl;
+    std::cout << "Received a Set request from Client" << std::endl;
 
     std::cout << "\n\nSending response to Client : " << std::endl;
     RCSResourceObject::LockGuard lock(*server);
@@ -112,8 +143,8 @@ RCSSetResponse requestHandlerForSet(const RCSRequest& request,
 
 void createResource()
 {
-    server = RCSResourceObject::Builder(resourceUri, resourceType,
-                             resourceInterface).setDiscoverable(true).setObservable(true).build();
+    server = RCSResourceObject::Builder(resourceUri, resourceType,resourceInterface).
+            setDiscoverable(true).setObservable(true).build();
 }
 
 void initServer()
@@ -122,7 +153,7 @@ void initServer()
     {
         createResource();
     }
-    catch (const PlatformException& e)
+    catch (const RCSPlatformException& e)
     {
         std::cout << "Exception in initServer : " << e.what() << std::endl;
     }
@@ -132,22 +163,22 @@ void initServer()
     server->setAttribute(attributeKey, DEFALUT_VALUE);
 }
 
-void changeTemperature(Control control)
+void changeAttribute(Control control)
 {
     RCSResourceObject::LockGuard lock(server);
     if(Control::INCREASE == control)
     {
         server->getAttributes()[attributeKey] =
-                server->getAttribute<int>(attributeKey) + 10;
-        std::cout << "\nTemperature increased by 10 degree" << std::endl;
+                server->getAttribute<int>(attributeKey) + 1;
+        std::cout << attributeKey << " increased." << std::endl;
     }
     else if(Control::DECREASE == control)
     {
         server->getAttributes()[attributeKey] =
-                        server->getAttribute<int>(attributeKey) - 10;
-        std::cout << "\nTemperature Decreased by 10 degree" << std::endl;
+                        server->getAttribute<int>(attributeKey) - 1;
+        std::cout << attributeKey << " Decreased." << std::endl;
     }
-    std::cout << "\nCurrent Temperature : "
+    std::cout << "\nCurrent " << attributeKey << ": "
             << server->getAttributeValue(attributeKey).get<int>() << std::endl;
 }
 
@@ -164,17 +195,71 @@ int processUserInput()
     return userInput;
 }
 
+int selectPresenceMenu()
+{
+    std::cout << "========================================================" << std::endl;
+    std::cout << "1. Presence On" << std::endl;
+    std::cout << "2. Presence Off" << std::endl;
+    std::cout << "========================================================" << std::endl;
+
+    switch(processUserInput())
+        {
+        case PRESENCE_ON:
+            isPresenceOn = PRESENCE_ON;
+            startPresence(3);
+            return CORRECT_INPUT;
+        case PRESENCE_OFF:
+            isPresenceOn = PRESENCE_OFF;
+            return CORRECT_INPUT;
+        default :
+            std::cout << "Invalid input, please try again" << std::endl;
+            return INCORRECT_INPUT;
+        }
+}
+
+int selectResourceTypeMenu()
+{
+    displayResourceTypeMenu();
+
+    switch(processUserInput())
+    {
+        case REQUEST_TEMP:
+            resourceUri = "/a/TempSensor";
+            resourceType = "oic.r.temperaturesensor";
+            attributeKey = "Temperature";
+            return CORRECT_INPUT;
+        case REQUEST_LIGHT:
+            resourceUri = "/a/light";
+            resourceType = "oic.r.light";
+            attributeKey = "Brightness";
+            return CORRECT_INPUT;
+        default :
+            std::cout << "Invalid input, please try again" << std::endl;
+            return INCORRECT_INPUT;
+    }
+}
+
 int selectServerMenu()
 {
     switch (processUserInput())
     {
         case DEFALUT_SERVER: // Creation of Resource & Auto control for all requests from Client.
+            while(true)
+            {
+                int ret = selectResourceTypeMenu();
+                if(ret == CORRECT_INPUT) break;
+            }
             initServer();
             return CORRECT_INPUT;
 
         case CUSTOM_SERVER:
             // Creation of Resource & setting get and set handler for handling get and
             // set request from client in application.
+            while(true)
+            {
+                int ret = selectResourceTypeMenu();
+                if(ret == CORRECT_INPUT) break;
+            }
             initServer();
 
             server->setGetRequestHandler(requestHandlerForGet);
@@ -191,17 +276,19 @@ int selectServerMenu()
 
 int selectControlTemperatureMenu()
 {
-   switch (processUserInput())
-   {
+    displayControlTemperatureMenu();
+
+    switch (processUserInput())
+    {
        case INCREASE_TEMPERATURE:
-           changeTemperature(Control::INCREASE);
+           changeAttribute(Control::INCREASE);
            return CORRECT_INPUT;
 
        case DECREASE_TEMPERATURE:
-           changeTemperature(Control::DECREASE);
+           changeAttribute(Control::DECREASE);
            return CORRECT_INPUT;
 
-       case STOP_SENSOR:
+       case STOP_TEMPERATURE_SENSOR:
            return QUIT;
 
        default:
@@ -210,12 +297,40 @@ int selectControlTemperatureMenu()
    }
 }
 
+int selectControlLightMenu()
+{
+    displayControlLightMenu();
+
+    switch (processUserInput())
+    {
+        case INCREASE_BRIGHTNESS:
+            changeAttribute(Control::INCREASE);
+            return CORRECT_INPUT;
+
+        case DECREASE_BRIGHTNESS:
+            changeAttribute(Control::DECREASE);
+            return CORRECT_INPUT;
+
+        case STOP_LIGHT_SENSOR:
+            return QUIT;
+
+        default:
+            std::cout << "Invalid input. Please try again." << std::endl;
+            return INCORRECT_INPUT;
+    }
+}
+
 void process()
 {
     while(true)
     {
-        displayMenu();
+        int ret = selectPresenceMenu();
+        if(ret == CORRECT_INPUT) break;
+    }
 
+    while(true)
+    {
+        displayMenu();
         int ret = selectServerMenu();
 
         if(ret == QUIT) return;
@@ -224,27 +339,37 @@ void process()
 
     while(true)
     {
-        displayControlTemperatureMenu();
-
-        if (selectControlTemperatureMenu() == QUIT) return;
+        if(resourceType == "oic.r.temperaturesensor")
+        {
+            int ret = selectControlTemperatureMenu();
+            if (ret == QUIT) return;
+            if (ret == INCORRECT_INPUT) continue;
+        }
+        else if(resourceType == "oic.r.light")
+        {
+            int ret = selectControlLightMenu();
+            if (ret == QUIT) return;
+            if (ret == INCORRECT_INPUT) continue;
+        }
     }
 }
 
 int main(void)
 {
-    startPresence(3);
-
     try
     {
         process();
         server = NULL;
+
+        if(isPresenceOn == PRESENCE_ON)
+        {
+            stopPresence();
+        }
     }
     catch (const std::exception& e)
     {
         std::cout << "main exception  : " << e.what() << std::endl;
     }
-
     std::cout << "Stopping the Server" << std::endl;
 }
 
-