const int SUCCESS_RESPONSE = 0;
std::shared_ptr<OCResource> curResource;
+std::mutex curResourceLock;
class Garage
{
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("")
{
// 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;
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: ";
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)
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;
// 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;
}
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,
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-
return 0;
}
+