const int SUCCESS_RESPONSE = 0;
std::shared_ptr<OCResource> curResource;
+std::mutex curResourceLock;
+
+static void printUsage()
+{
+ std::cout<<"Usage: garageclient <0|1> \n";
+ std::cout<<"ConnectivityType: Default IP\n";
+ std::cout<<"ConnectivityType 0: IP \n";
+}
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;
+
+ OCConnectivityType connectivityType = CT_ADAPTER_IP;
+
+ if(argc == 2)
+ {
+ try
+ {
+ std::size_t inputValLen;
+ int optionSelected = std::stoi(argv[1], &inputValLen);
+
+ if(inputValLen == strlen(argv[1]))
+ {
+ if(optionSelected == 0)
+ {
+ std::cout << "Using IP."<< std::endl;
+ connectivityType = CT_ADAPTER_IP;
+ }
+ else
+ {
+ std::cout << "Invalid connectivity type selected. Using default IP"
+ << std::endl;
+ }
+ }
+ else
+ {
+ std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
+ }
+ }
+ catch(std::exception& e)
+ {
+ std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
+ }
+ }
+ else
+ {
+ printUsage();
+ std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
+ }
+
// 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_RSRVD_WELL_KNOWN_URI << "?rt=core.garage";
+
+ OCPlatform::findResource("", requestURI.str(),
+ connectivityType, &foundResource);
+
std::cout<< "Finding Resource... " <<std::endl;
// A condition variable will free the mutex it is given, then do a non-
return 0;
}
+