#include "OCPlatform.h"
#include "OCApi.h"
+#include "logger.h"
#include <functional>
#include <pthread.h>
+#include <mutex>
+#include <condition_variable>
#include <iostream>
using namespace std;
namespace PH = std::placeholders;
OCResourceHandle resourceHandle;
-
shared_ptr< OCResource > g_resource;
vector< string > lights;
+std::mutex blocker;
+std::condition_variable cv;
bool isReady = false;
}
isReady = true;
+ cv.notify_one();
}
void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
}
}
+string buildActionSetDesc()
+{
+ string actionsetDesc = "";
+ //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10*uri=coap:
+ //10.251.44.228:49858/a/light|power=10|";
+ actionsetDesc = "allbulboff";
+ actionsetDesc.append("*");
+ for (auto iter = lights.begin(); iter != lights.end(); ++iter)
+ {
+ actionsetDesc.append("uri=").append((*iter));
+ actionsetDesc.append("|");
+ actionsetDesc.append("power=");
+ actionsetDesc.append("off");
+ if ((iter + 1) != lights.end())
+ {
+ actionsetDesc.append("*");
+ }
+ }
+ return actionsetDesc;
+}
+
+bool isResourceReady()
+{
+ return isReady;
+}
int main()
{
PlatformConfig config
string resourceTypeName = "a.collection";
OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=a.collection", &foundResource);
- isReady = false;
+ //Non-intensive block util foundResource callback is called by OCPlatform
+ //and onGet gets resource.
+ //isResourceReady takes care of spurious wake-up
+
+ std::unique_lock<std::mutex> lock(blocker);
+ cv.wait(lock, isResourceReady);
while (isRun)
{
usleep(100);
- while (isReady)
- {
- int n;
+ int selectedMenu;
- cout << endl
- << "1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET 3 :: GET ACTIONSET\n";
- cout << "4 :: DELETE ACTIONSET 5 :: Quit\n";
+ cout << endl
+ << "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
+ cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl;
- cin >> n;
- if (n == 1)
- {
- string actionsetDesc;
- //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10*uri=coap://10.251.44.228:49858/a/light|power=10|";
+ cin >> selectedMenu;
+
+ OCRepresentation rep;
+ string actionsetDesc;
+ switch(selectedMenu)
+ {
+ case 0:
+ isRun = false;
+ break;
+ case 1:
+ actionsetDesc = buildActionSetDesc();
- actionsetDesc = "allbulboff";
- actionsetDesc.append("*");
- for (auto iter = lights.begin(); iter != lights.end(); ++iter)
+ if(!actionsetDesc.empty())
{
- actionsetDesc.append("uri=").append((*iter));
- actionsetDesc.append("|");
- actionsetDesc.append("power=");
- actionsetDesc.append("off");
- if ((iter + 1) != lights.end())
- {
- actionsetDesc.append("*");
- }
+ cout << "ActionSet :: " << actionsetDesc << endl;
+ rep.setValue("ActionSet", actionsetDesc);
}
- cout << "ActionSet :: " << actionsetDesc << endl;
-
- OCRepresentation rep;
- rep.setValue("ActionSet", actionsetDesc);
-
if (g_resource)
{
g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
&onPut);
}
- }
- else if (n == 2)
- {
- OCRepresentation rep;
-
+ break;
+ case 2:
rep.setValue("DoAction", std::string("allbulboff"));
-
if (g_resource)
{
g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
- &onPost);
+ &onPost);
}
- }
- else if (n == 3)
- {
- OCRepresentation rep;
-
+ break;
+ case 3:
rep.setValue("GetActionSet", std::string("allbulboff"));
-
if (g_resource)
{
g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
- &onPost);
+ &onPost);
}
- }
- else if (n == 4)
- {
- OCRepresentation rep;
-
+ break;
+ case 4:
rep.setValue("DelActionSet", std::string("allbulboff"));
-
if (g_resource)
{
g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
- &onPut);
+ &onPut);
}
- }
- else if (n == 5)
- {
- isRun = false;
break;
- }
+ default:
+ cout << "Incorrect option" << endl;
+ break;
- fflush(stdin);
}
+ fflush(stdin);
}
}
catch (OCException& e)