*
******************************************************************/
-#include "PrimitiveResource.h"
#include "RCSResourceObject.h"
#include "OCPlatform.h"
-#include "OCApi.h"
-using namespace OC;
using namespace OC::OCPlatform;
using namespace OIC::Service;
-constexpr int DEFALUT_VALUE = 0;
+struct CloseApp{};
-constexpr int REQUEST_TEMP = 1;
-constexpr int REQUEST_LIGHT = 2;
-
-constexpr int PRESENCE_ON = 1;
-constexpr int PRESENCE_OFF = 2;
+constexpr int RESOURCE_TEMP = 1;
+constexpr int RESOURCE_LIGHT = 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_TEMPERATURE_SENSOR = 3;
+constexpr int INCREASE = 1;
+constexpr int DECREASE = 2;
-constexpr int INCREASE_BRIGHTNESS = 1;
-constexpr int DECREASE_BRIGHTNESS = 2;
-constexpr int STOP_LIGHT_SENSOR = 3;
+const std::string BASELINE_INTERFACE = "oic.if.baseline";
+const std::string ACTUATOR_INTERFACE = "oic.if.a";
+const std::string SENSOR_INTERFACE = "oic.if.s";
+const std::string CUSTOM_INTERFACE = "test.custom";
-constexpr int CORRECT_INPUT = 1;
-constexpr int INCORRECT_INPUT = 2;
-constexpr int QUIT = 3;
+typedef void (*DisplayControlMenuFunc)();
+typedef std::function<void()> Run;
+Run g_currentRun;
-std::string resourceType = "oic.r.temperaturesensor";
-std::string resourceInterface = "oic.if.";
-std::string resourceUri;
-std::string attributeKey;
-int isPresenceOn = PRESENCE_ON;
+bool g_isPresenceStarted = false;
-RCSResourceObject::Ptr server;
+RCSResourceObject::Ptr g_resource;
-int processUserInput();
+int processUserInput(int min, int max)
+{
+ assert(min <= max);
-enum class Control{
- INCREASE,
- DECREASE
-};
+ int input;
-void displayMenu()
-{
- std::cout << "====================================================================="
- << std::endl;
- std::cout << " 1 - Creation of Resource [Auto control for requests]" << std::endl;
- std::cout << " 2 - Creation of Resource [Developer control for Get and Set requests]"
- << std::endl;
- std::cout << " 3 - Quit" << std::endl;
- std::cout << "====================================================================="
- << std::endl;
-}
+ std::cin >> input;
-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;
+ if (!std::cin.fail())
+ {
+ if(input == max + 1) throw CloseApp();
+ if(min <= input && input <= max) return input;
+ }
+
+ std::cin.clear();
+ std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
+
+ throw std::runtime_error("Invalid Input, please try again");
}
void displayControlTemperatureMenu()
{
- std::cout << "========================================================" << 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;
+ std::cout << "========================================================\n";
+ std::cout << INCREASE << ". Increase Temperature by 1 degree \n";
+ std::cout << DECREASE << ". Decrease Temperature by 1 degree \n";
+ std::cout << DECREASE + 1 << ". Quit \n";
+ std::cout << "========================================================\n";
}
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;
+ std::cout << "========================================================\n";
+ std::cout << INCREASE << ". Increase Brightness by 1 stage \n";
+ std::cout << DECREASE << ". Decrease Brightness by 1 stage \n";
+ std::cout << DECREASE + 1 << ". Quit \n";
+ std::cout << "========================================================\n";
}
-void printAttribute(const RCSResourceAttributes& attrs)
+void printAttributes(const RCSResourceAttributes& attrs)
{
for(const auto& attr : attrs)
{
}
}
-//hander for get request (if developer choose second option for resource Creation)
-RCSGetResponse requestHandlerForGet(const RCSRequest& request,
- RCSResourceAttributes& attrs)
+RCSGetResponse requestHandlerForGet(const RCSRequest&, RCSResourceAttributes& attrs)
{
std::cout << "Received a Get request from Client" << std::endl;
- RCSResourceObject::LockGuard lock(*server);
- RCSResourceAttributes attributes = server->getAttributes();
+ printAttributes(attrs);
- std::cout << "\nSending response to Client : " << std::endl;
- printAttribute(attributes);
+ {
+ RCSResourceObject::LockGuard lock(g_resource);
+ std::cout << "\nSending response to Client : " << std::endl;
+ printAttributes(g_resource->getAttributes());
+ }
return RCSGetResponse::defaultAction();
}
-//hander for set request (if developer choose second option for resource Creation)
-RCSSetResponse requestHandlerForSet(const RCSRequest& request,
- RCSResourceAttributes& attrs)
+RCSSetResponse requestHandlerForSet(const RCSRequest&, RCSResourceAttributes& attrs)
{
std::cout << "Received a Set request from Client" << std::endl;
+ printAttributes(attrs);
- std::cout << "\n\nSending response to Client : " << std::endl;
- RCSResourceObject::LockGuard lock(*server);
- printAttribute(attrs);
return RCSSetResponse::defaultAction();
}
-void createResource()
+void initServer(const std::string& resourceUri, const std::string& resourceType,
+ const std::string& attrKey)
{
- server = RCSResourceObject::Builder(resourceUri, resourceType,resourceInterface).
- setDiscoverable(true).setObservable(true).build();
+ g_resource = RCSResourceObject::Builder(resourceUri, resourceType, ACTUATOR_INTERFACE)
+ .addInterface(CUSTOM_INTERFACE)
+ .addInterface(SENSOR_INTERFACE)
+ .setDefaultInterface(BASELINE_INTERFACE)
+ .setDiscoverable(true)
+ .setObservable(true)
+ .build();
+
+ g_resource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
+ g_resource->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
+ g_resource->setAttribute(attrKey, 0);
}
-void initServer()
+void updateAttribute(const std::string& attrKey, int control)
{
- try
- {
- createResource();
- }
- catch (const RCSPlatformException& e)
+ const int diff = control == INCREASE ? 1 : - 1;
+
{
- std::cout << "Exception in initServer : " << e.what() << std::endl;
+ RCSResourceObject::LockGuard lock(g_resource);
+ auto& attrs = g_resource->getAttributes();
+ attrs[attrKey] = attrs[attrKey].get<int>() + diff;
}
- server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
- server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
- server->setAttribute(attributeKey, DEFALUT_VALUE);
-}
-
-void changeAttribute(Control control)
-{
- RCSResourceObject::LockGuard lock(server);
- if(Control::INCREASE == control)
+ if(control == INCREASE)
{
- server->getAttributes()[attributeKey] =
- server->getAttribute<int>(attributeKey) + 1;
- std::cout << attributeKey << " increased." << std::endl;
+ std::cout << attrKey << " increased." << std::endl;
}
- else if(Control::DECREASE == control)
+ else
{
- server->getAttributes()[attributeKey] =
- server->getAttribute<int>(attributeKey) - 1;
- std::cout << attributeKey << " Decreased." << std::endl;
+ std::cout << attrKey << " decreased." << std::endl;
}
- std::cout << "\nCurrent " << attributeKey << ": "
- << server->getAttributeValue(attributeKey).get<int>() << std::endl;
+ std::cout << "\nCurrent " << attrKey << ": "
+ << g_resource->getAttributeValue(attrKey).get<int>() << std::endl;
}
-int processUserInput()
+void runResourceControl(DisplayControlMenuFunc displayMenuFunc, const std::string& attrKey)
{
- int userInput;
- std::cin >> userInput;
- if (std::cin.fail())
- {
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- return -1;
- }
- return userInput;
+ displayMenuFunc();
+ updateAttribute(attrKey, processUserInput(INCREASE, DECREASE));
}
-int selectPresenceMenu()
+void runResourceTypeSelection(int resourceMode)
{
- std::cout << "========================================================" << std::endl;
- std::cout << "1. Presence On" << std::endl;
- std::cout << "2. Presence Off" << std::endl;
- std::cout << "========================================================" << std::endl;
+ std::cout << "========================================================\n";
+ std::cout << "Select Resource Type \n";
+ std::cout << RESOURCE_TEMP << ". Temperature \n";
+ std::cout << RESOURCE_LIGHT << ". Light \n";
+ std::cout << RESOURCE_LIGHT + 1 << ". Quit \n";
+ std::cout << "========================================================\n";
+
+ int resourceType = processUserInput(RESOURCE_TEMP, RESOURCE_LIGHT);
+ DisplayControlMenuFunc displayMenuFunc;
+ std::string attrKey;
+
+ switch (resourceType)
+ {
+ case RESOURCE_TEMP:
+ attrKey = "Temperature";
+ initServer("/a/TempSensor", "oic.r.temperaturesensor", attrKey);
- 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;
- }
-}
+ displayMenuFunc = displayControlTemperatureMenu;
+ break;
-int selectResourceTypeMenu()
-{
- displayResourceTypeMenu();
+ case RESOURCE_LIGHT:
+ attrKey = "Brightness";
+ initServer("/a/light", "oic.r.light", attrKey);
- 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;
+ displayMenuFunc = displayControlLightMenu;
+ break;
}
-}
-int selectServerMenu()
-{
- switch (processUserInput())
+ if (resourceMode == CUSTOM_SERVER)
{
- 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);
- server->setSetRequestHandler(requestHandlerForSet);
- return CORRECT_INPUT;
- case STOP :
- return QUIT;
-
- default :
- std::cout << "Invalid input, please try again" << std::endl;
- return INCORRECT_INPUT;
+ g_resource->setGetRequestHandler(requestHandlerForGet);
+ g_resource->setSetRequestHandler(requestHandlerForSet);
}
+
+ g_currentRun = std::bind(runResourceControl, displayMenuFunc, std::move(attrKey));
}
-int selectControlTemperatureMenu()
+void runResourceModeSelection()
{
- displayControlTemperatureMenu();
-
- switch (processUserInput())
- {
- case INCREASE_TEMPERATURE:
- changeAttribute(Control::INCREASE);
- return CORRECT_INPUT;
-
- case DECREASE_TEMPERATURE:
- changeAttribute(Control::DECREASE);
- return CORRECT_INPUT;
-
- case STOP_TEMPERATURE_SENSOR:
- return QUIT;
-
- default:
- std::cout << "Invalid input. Please try again." << std::endl;
- return INCORRECT_INPUT;
- }
+ std::cout << "======================================================== \n";
+ std::cout << DEFALUT_SERVER << ". Creation of Simple Resource Without Handlers \n";
+ std::cout << CUSTOM_SERVER << ". Creation of Resource With Set and Get Handlers \n";
+ std::cout << CUSTOM_SERVER + 1 << ". Quit \n";
+ std::cout << "======================================================== \n";
+
+ g_currentRun = std::bind(runResourceTypeSelection,
+ processUserInput(DEFALUT_SERVER, CUSTOM_SERVER));
}
-int selectControlLightMenu()
+void runPresenceSelection()
{
- displayControlLightMenu();
-
- switch (processUserInput())
- {
- case INCREASE_BRIGHTNESS:
- changeAttribute(Control::INCREASE);
- return CORRECT_INPUT;
+ constexpr int PRESENCE_ON = 1;
+ constexpr int PRESENCE_OFF = 2;
- case DECREASE_BRIGHTNESS:
- changeAttribute(Control::DECREASE);
- return CORRECT_INPUT;
+ std::cout << "========================================================\n";
+ std::cout << PRESENCE_ON << ". Presence On \n";
+ std::cout << PRESENCE_OFF << ". Presence Off \n";
+ std::cout << PRESENCE_OFF + 1 << ". Quit \n";
+ std::cout << "========================================================\n";
- case STOP_LIGHT_SENSOR:
- return QUIT;
-
- default:
- std::cout << "Invalid input. Please try again." << std::endl;
- return INCORRECT_INPUT;
- }
-}
-
-void process()
-{
- while(true)
+ if (processUserInput(PRESENCE_ON, PRESENCE_OFF) == PRESENCE_ON)
{
- int ret = selectPresenceMenu();
- if(ret == CORRECT_INPUT) break;
+ g_isPresenceStarted = true;
+ startPresence(3);
}
- while(true)
- {
- displayMenu();
- int ret = selectServerMenu();
+ g_currentRun = runResourceModeSelection;
+}
- if(ret == QUIT) return;
- if(ret == CORRECT_INPUT) break;
- }
+int main(void)
+{
+ g_currentRun = runPresenceSelection;
while(true)
{
- if(resourceType == "oic.r.temperaturesensor")
+ try
{
- int ret = selectControlTemperatureMenu();
- if (ret == QUIT) return;
- if (ret == INCORRECT_INPUT) continue;
+ g_currentRun();
}
- else if(resourceType == "oic.r.light")
+ catch(const std::exception& e)
{
- int ret = selectControlLightMenu();
- if (ret == QUIT) return;
- if (ret == INCORRECT_INPUT) continue;
+ std::cout << e.what() << std::endl;
}
- }
-}
-
-int main(void)
-{
- try
- {
- process();
- server = NULL;
-
- if(isPresenceOn == PRESENCE_ON)
+ catch(const CloseApp&)
{
- stopPresence();
+ break;
}
}
- catch (const std::exception& e)
+ std::cout << "Stopping the server" << std::endl;
+
+ g_resource.reset();
+
+ if(g_isPresenceStarted)
{
- std::cout << "main exception : " << e.what() << std::endl;
+ stopPresence();
}
- std::cout << "Stopping the Server" << std::endl;
}