OCRepresentation is now provides simpler interface to setValue and getValue from the applications.
Applications doesn't have to go through attributeMaps in OCRepresentation.
OCRepresentation now supports serialization/deserialization for primitive attributes (numbers, booleans and strings) so application doesn't have to deal with strings.
Change-Id: I09d323fc624d87c0b2a3c9b510935a6d5907a11b
std::atomic<bool> LightResource::shutdown_flag(false);
std::thread LightResource::observe_thread;
-void LightResource::setRepresentation(AttributeMap& attributeMap)
+void LightResource::setRepresentation(const OCRepresentation& rep)
{
cout << "\t\t\t" << "Received representation: " << endl;
- cout << "\t\t\t\t" << "power: " << attributeMap["power"][0] << endl;
- cout << "\t\t\t\t" << "state: " << attributeMap["state"][0] << endl;
- m_state = attributeMap["state"][0].compare("true") == 0;
- m_power = std::stoi(attributeMap["power"][0]);
+ rep.getValue("state", m_state);
+ rep.getValue("power", m_power);
+
+ cout << "\t\t\t\t" << "power: " << m_power << endl;
+ cout << "\t\t\t\t" << "state: " << m_state << endl;
}
-void LightResource::getRepresentation(AttributeMap& attributeMap) const
+OCRepresentation LightResource::getRepresentation(void)
{
- attributeMap["state"] = { (m_state ? "true" : "false") };
- attributeMap["power"] = { to_string(m_power) };
+ m_rep.setValue("state", m_state);
+ m_rep.setValue("power", m_power);
+ return m_rep;
}
void LightResource::addType(const OC::OCPlatform& platform, const std::string& type) const
// ...do any processing of the query here...
// Get a representation of the resource and send it back as a response:
- AttributeMap attribute_map;
-
- getRepresentation(attribute_map);
-
response->setErrorCode(200);
- response->setResourceRepresentation(attribute_map);
+ response->setResourceRepresentation(getRepresentation());
}
void LightResource::handle_put_request(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
const auto query_params_map = request->getQueryParameters();
// ...do something with the query parameters (if there were any)...
- auto attribute_map = request->getAttributeRepresentation();
+ auto rep = request->getResourceRepresentation();
- setRepresentation(attribute_map);
- getRepresentation(attribute_map); // in case we changed something
+ setRepresentation(rep);
if(!response)
return;
response->setErrorCode(200);
- response->setResourceRepresentation(attribute_map);
+ response->setResourceRepresentation(getRepresentation());
}
void LightResource::handle_post_request(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
{
// ...demo-code...
- response->setErrorCode(200);
-
- auto attribute_map = request->getAttributeRepresentation();
- getRepresentation(attribute_map);
- response->setResourceRepresentation(attribute_map);
}
void LightResource::handle_delete_request(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
{
// ...demo-code...
- response->setErrorCode(200);
-
- auto attribute_map = request->getAttributeRepresentation();
- getRepresentation(attribute_map);
- response->setResourceRepresentation(attribute_map);
}
// Set up observation in a separate thread:
public:
bool m_state; // off or on?
int m_power; // power level
+ OCRepresentation m_rep;
private:
atomic<bool> m_observation; // are we under observation?
}
private:
- inline std::string make_URI(const unsigned int resource_number) const
+ inline std::string make_URI(const unsigned int resource_number)
{
- return std::string("/a/light") + "_" + std::to_string(resource_number);
+ std::string uri = std::string("/a/light") + "_" + std::to_string(resource_number);
+ m_rep.setUri(uri);
+ return uri;
}
public:
void unregisterResource(OC::OCPlatform& platform);
OCResourceHandle getHandle() const { return m_resourceHandle; }
- void setRepresentation(AttributeMap& attributeMap);
- void getRepresentation(AttributeMap& attributeMap) const;
+ void setRepresentation(const OCRepresentation& rep);
+ OCRepresentation getRepresentation(void);
void addType(const OC::OCPlatform& platform, const std::string& type) const;
void addInterface(const OC::OCPlatform& platform, const std::string& interface) const;
using namespace OC;
std::shared_ptr<OCResource> curResource;
-static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
OCPlatform* platformPtr;
// Callback to found resources
void foundResource(std::shared_ptr<OCResource> resource)
{
-
if(curResource)
{
std::cout << "Found another resource, ignoring"<<std::endl;
}
}
-void PrintUsage()
-{
- std::cout << std::endl;
- std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
- std::cout << " ObserveType : 1 - Observe" << std::endl;
- std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
-}
-
-
int main(int argc, char* argv[]) {
- if (argc == 1)
- {
- OBSERVE_TYPE_TO_USE = ObserveType::Observe;
- }
- else if (argc == 2)
- {
- int value = atoi(argv[1]);
- if (value == 1)
- OBSERVE_TYPE_TO_USE = ObserveType::Observe;
- else if (value == 2)
- OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
- else
- OBSERVE_TYPE_TO_USE = ObserveType::Observe;
- }
- else
- {
- PrintUsage();
- return -1;
- }
// Create PlatformConfig object
PlatformConfig cfg {
return m_resourceHandle;
}
- void setRepresentation(OCRepresentation& light)
- {
- AttributeMap attributeMap = light.getAttributeMap();
-
- if(attributeMap.find("state") != attributeMap.end() && attributeMap.find("power") != attributeMap.end())
- {
- cout << "\t\t\t" << "Received representation: " << endl;
- cout << "\t\t\t\t" << "power: " << attributeMap["power"][0] << endl;
- cout << "\t\t\t\t" << "state: " << attributeMap["state"][0] << endl;
-
- m_state = attributeMap["state"][0].compare("true") == 0;
- m_power= std::stoi(attributeMap["power"][0]);
- }
- }
-
- OCRepresentation getRepresentation()
- {
- OCRepresentation light;
-
- light.setUri(m_lightUri);
-
- std::vector<std::string> interfaces;
- //interfaces.push_back(m_lightInterface);
-
- light.setResourceInterfaces(interfaces);
-
- std::vector<std::string> types;
- //types.push_back(m_lightType);
-
- light.setResourceTypes(types);
-
- AttributeMap attributeMap;
- AttributeValues stateVal;
- if(m_state)
- {
- stateVal.push_back("true");
- }
- else
- {
- stateVal.push_back("false");
- }
-
- AttributeValues powerVal;
- powerVal.push_back(to_string(m_power));
-
- attributeMap["state"] = stateVal;
- attributeMap["power"] = powerVal;
-
- light.setAttributeMap(attributeMap);
-
- return light;
- }
-
void addType(const OC::OCPlatform& platform, const std::string& type) const
{
OCStackResult result = platform.bindTypeToResource(m_resourceHandle, type);
cout << "Binding TypeName to Resource was unsuccessful\n";
}
}
+
};
// Create the instance of the resource class (in this case instance of class 'LightResource').
LightResource myLightResource;
-// This is just a sample implementation of entity handler.
-// Entity handler can be implemented in several ways by the manufacturer
void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
{
cout << "\tIn Server CPP entity handler:\n";
-
- if(request)
- {
- // Get the request type and request flag
- std::string requestType = request->getRequestType();
- RequestHandlerFlag requestFlag = request->getRequestHandlerFlag();
-
- if(requestFlag == RequestHandlerFlag::InitFlag)
- {
- cout << "\t\trequestFlag : Init\n";
-
- // entity handler to perform resource initialization operations
- }
- else if(requestFlag == RequestHandlerFlag::RequestFlag)
- {
- cout << "\t\trequestFlag : Request\n";
-
- // If the request type is GET
- if(requestType == "GET")
- {
- cout << "\t\t\trequestType : GET\n";
-
- // Check for query params (if any)
- QueryParamsMap queryParamsMap = request->getQueryParameters();
-
- cout << "\t\t\tquery params: \n";
- for(QueryParamsMap::iterator it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
- {
- cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
- }
-
- // Process query params and do required operations ..
-
- // Get the representation of this resource at this point and send it as response
- // AttributeMap attributeMap;
- OCRepresentation rep;
- rep = myLightResource.getRepresentation();
-
- if(response)
- {
- // TODO Error Code
- response->setErrorCode(200);
-
- auto findRes = queryParamsMap.find("if");
-
- if(findRes != queryParamsMap.end())
- {
- response->setResourceRepresentation(rep, findRes->second);
- }
- else
- {
- response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
- }
- }
- }
- else if(requestType == "PUT")
- {
- cout << "\t\t\trequestType : PUT\n";
-
- // Check for query params (if any)
- QueryParamsMap queryParamsMap = request->getQueryParameters();
-
- cout << "\t\t\tquery params: \n";
- for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
- {
- cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
- }
-
- // Get the representation from the request
- OCRepresentation rep = request->getResourceRepresentation();
-
- myLightResource.setRepresentation(rep);
-
- // Do related operations related to PUT request
- rep = myLightResource.getRepresentation();
-
- if(response)
- {
- // TODO Error Code
- response->setErrorCode(200);
-
- auto findRes = queryParamsMap.find("if");
-
- if(findRes != queryParamsMap.end())
- {
- response->setResourceRepresentation(rep, findRes->second);
- }
- else
- {
- response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
- }
- }
-
- }
- else if(requestType == "POST")
- {
- // POST request operations
- }
- else if(requestType == "DELETE")
- {
- // DELETE request operations
- }
- }
- else if(requestFlag == RequestHandlerFlag::ObserverFlag)
- {
- // OBSERVE flag operations
- }
- }
- else
- {
- std::cout << "Request invalid" << std::endl;
- }
}
int main()
if(eCode == SUCCESS_RESPONSE)
{
std::cout << "GET request was successful" << std::endl;
-
- AttributeMap attributeMap = rep.getAttributeMap();
- std::cout << "Resource URI: " << rep.getUri() << std::endl;
-
- for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
- {
- std::cout << "\tAttribute name: "<< it->first << " value: ";
- for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
- {
- std::cout <<"\t"<< *valueItr << " ";
- }
-
- std::cout << std::endl;
- }
+ std::cout << "\tResource URI: " << rep.getUri() << std::endl;
std::vector<OCRepresentation> children = rep.getChildren();
for(auto oit = children.begin(); oit != children.end(); ++oit)
{
- std::cout << "Child Resource URI: " << oit->getUri() << std::endl;
-
- attributeMap = oit->getAttributeMap();
-
- for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
+ std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
+ if(oit->getUri().find("light") != std::string::npos)
{
- std::cout << "\tAttribute name: "<< it->first << " value: ";
- for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
- {
- std::cout <<"\t"<< *valueItr << " ";
- }
-
- std::cout << std::endl;
+ bool state = false;
+ int color = 0;
+ oit->getValue("state", state);
+ oit->getValue("color", color);
+
+ std::cout << "\t\tstate:" << state << std::endl;
+ std::cout << "\t\tcolor:" << color << std::endl;
+ }
+ else if(oit->getUri().find("fan") != std::string::npos)
+ {
+ bool state = false;
+ int speed = 0;
+ oit->getValue("state", state);
+ oit->getValue("speed", speed);
+
+ std::cout << "\t\tstate:" << state << std::endl;
+ std::cout << "\t\tspeed:" << speed << std::endl;
}
}
{
OCRepresentation rep;
std::cout << "Putting room representation..."<<std::endl;
- // Create AttributeMap
- AttributeMap attributeMap;
- // Add the attribute name and values in the attribute map
- AttributeValues stateVal;
- stateVal.push_back("true");
-
- AttributeValues powerVal;
- powerVal.push_back("8");
- attributeMap["state"] = stateVal;
- attributeMap["speed"] = powerVal;
-
- // Create QueryParameters Map and add query params (if any)
- QueryParamsMap qp;
- rep.setAttributeMap(attributeMap);
+ bool state = true;
+ int speed = 10;
+ rep.setValue("state", state);
+ rep.setValue("speed", speed);
// Invoke resource's pit API with attribute map, query map and the callback parameter
- resource->put("core.room", BATCH_INTERFACE, rep, qp, &onPut);
+ resource->put("core.room", BATCH_INTERFACE, rep, QueryParamsMap(), &onPut);
}
}
{
std::cout << "PUT request was successful" << std::endl;
- AttributeMap attributeMap = rep.getAttributeMap();
-
- for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
- {
- std::cout << "\tAttribute name: "<< it->first << " value: ";
- for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
- {
- std::cout <<"\t"<< *valueItr << " ";
- }
-
- std::cout << std::endl;
- }
+ std::cout << "\tResource URI: " << rep.getUri() << std::endl;
std::vector<OCRepresentation> children = rep.getChildren();
for(auto oit = children.begin(); oit != children.end(); ++oit)
{
- attributeMap = oit->getAttributeMap();
-
- for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
+ std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
+ if(oit->getUri().find("light") != std::string::npos)
{
- std::cout << "\tAttribute name: "<< it->first << " value: ";
- for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
- {
- std::cout <<"\t"<< *valueItr << " ";
- }
-
- std::cout << std::endl;
+ bool state = false;
+ int color = 0;
+ oit->getValue("state", state);
+ oit->getValue("color", color);
+
+ std::cout << "\t\tstate:" << state << std::endl;
+ std::cout << "\t\tcolor:" << color << std::endl;
}
+ else if(oit->getUri().find("fan") != std::string::npos)
+ {
+ bool state = false;
+ int speed = 0;
+ oit->getValue("state", state);
+ oit->getValue("speed", speed);
+
+ std::cout << "\t\tstate:" << state << std::endl;
+ std::cout << "\t\tspeed:" << speed << std::endl;
+ }
+
}
}
// Room members
std::string m_roomUri;
- std::string m_roomType;
- std::string m_roomInterface1;
- std::string m_roomInterface2;
- std::string m_roomInterface3;
+ std::vector<std::string> m_roomTypes;
+ std::vector<std::string> m_roomInterfaces;
OCResourceHandle m_roomHandle;
+ OCRepresentation m_roomRep;
// light members
bool m_lightState;
int m_lightColor;
- std::string m_lightInterface;
std::string m_lightUri;
- std::string m_lightType;
+ std::vector<std::string> m_lightTypes;
+ std::vector<std::string> m_lightInterfaces;
OCResourceHandle m_lightHandle;
+ OCRepresentation m_lightRep;
// fan members
bool m_fanState;
int m_fanSpeed;
- std::string m_fanInterface;
std::string m_fanUri;
- std::string m_fanType;
+ std::vector<std::string> m_fanTypes;
+ std::vector<std::string> m_fanInterfaces;
OCResourceHandle m_fanHandle;
+ OCRepresentation m_fanRep;
public:
/// Constructor
RoomResource(): m_lightState(false), m_lightColor(0), m_fanState(false), m_fanSpeed(0)
{
- m_roomUri = "/a/room"; // URI of the resource
- m_roomType = "core.room"; // resource type name. In this case, it is light
- m_roomInterface1 = DEFAULT_INTERFACE; // resource interface.
- m_roomInterface2 = BATCH_INTERFACE; // resource interface.
- m_roomInterface3 = LINK_INTERFACE; // resource interface.
-
m_lightUri = "/a/light"; // URI of the resource
- m_lightType = "core.light"; // resource type name. In this case, it is light
- m_lightInterface = DEFAULT_INTERFACE; // resource interface.
+ m_lightTypes.push_back("core.light"); // resource type name. In this case, it is light
+ m_lightInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
+
+ m_lightRep.setUri(m_lightUri);
+ m_lightRep.setResourceTypes(m_lightTypes);
+ m_lightRep.setResourceInterfaces(m_lightInterfaces);
+ m_lightRep.setValue("state", m_lightState);
+ m_lightRep.setValue("color", m_lightColor);
m_fanUri = "/a/fan"; // URI of the resource
- m_fanType = "core.fan"; // resource type name. In this case, it is light
- m_fanInterface = DEFAULT_INTERFACE; // resource interface.
+ m_fanTypes.push_back("core.fan"); // resource type name. In this case, it is light
+ m_fanInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
+
+ m_fanRep.setUri(m_fanUri);
+ m_fanRep.setResourceTypes(m_fanTypes);
+ m_fanRep.setResourceInterfaces(m_fanInterfaces);
+ m_fanRep.setValue("state", m_fanState);
+ m_fanRep.setValue("speed", m_fanSpeed);
+
+ m_roomUri = "/a/room"; // URI of the resource
+ m_roomTypes.push_back("core.room"); // resource type name. In this case, it is light
+ m_roomInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
+ m_roomInterfaces.push_back(BATCH_INTERFACE); // resource interface.
+ m_roomInterfaces.push_back(LINK_INTERFACE); // resource interface.
+ m_roomRep.setUri(m_roomUri);
+ m_roomRep.setResourceTypes(m_roomTypes);
+ m_roomRep.setResourceInterfaces(m_roomInterfaces);
}
/// This function internally calls registerResource API.
{
// This will internally create and register the resource.
OCStackResult result = platform.registerResource(
- m_roomHandle, m_roomUri, m_roomType,
- m_roomInterface1, NULL, //entityHandlerRoom,
+ m_roomHandle, m_roomUri, m_roomTypes[0],
+ m_roomInterfaces[0], NULL, //entityHandlerRoom,
OC_DISCOVERABLE | OC_OBSERVABLE
);
cout << "Resource creation (room) was unsuccessful\n";
}
- result = platform.bindInterfaceToResource(m_roomHandle, m_roomInterface2);
+ result = platform.bindInterfaceToResource(m_roomHandle, m_roomInterfaces[1]);
if (OC_STACK_OK != result)
{
cout << "Binding TypeName to Resource was unsuccessful\n";
}
- result = platform.bindInterfaceToResource(m_roomHandle, m_roomInterface3);
+ result = platform.bindInterfaceToResource(m_roomHandle, m_roomInterfaces[2]);
if (OC_STACK_OK != result)
{
cout << "Binding TypeName to Resource was unsuccessful\n";
}
result = platform.registerResource(
- m_lightHandle, m_lightUri, m_lightType,
- m_lightInterface, entityHandlerLight,
+ m_lightHandle, m_lightUri, m_lightTypes[0],
+ m_lightInterfaces[0], entityHandlerLight,
OC_DISCOVERABLE | OC_OBSERVABLE
);
}
result = platform.registerResource(
- m_fanHandle, m_fanUri, m_fanType,
- m_fanInterface, entityHandlerFan,
+ m_fanHandle, m_fanUri, m_fanTypes[0],
+ m_fanInterfaces[0], entityHandlerFan,
OC_DISCOVERABLE | OC_OBSERVABLE
);
}
- void setRoomRepresentation(OCRepresentation& rep)
+ void setLightRepresentation(OCRepresentation& rep)
{
- setLightRepresentation(rep);
- setFanRepresentation(rep);
- }
+ bool tempState = false;
+ int tempColor = 0;
- void setLightRepresentation(OCRepresentation& light)
- {
- AttributeMap attributeMap = light.getAttributeMap();
-
- if(attributeMap.find("state") != attributeMap.end() && attributeMap.find("color") != attributeMap.end())
+ // If both entries exist
+ if(rep.getValue("state", tempState) && rep.getValue("color", tempColor))
{
- m_lightState = attributeMap["state"][0].compare("true") == 0;
- m_lightColor= std::stoi(attributeMap["color"][0]);
+ m_lightState = tempState;
+ m_lightColor= tempColor;
+
+ cout << "\t\t\t\t" << "state: " << m_lightState << endl;
+ cout << "\t\t\t\t" << "color: " << m_lightColor << endl;
}
}
- void setFanRepresentation(OCRepresentation& fan)
+ void setFanRepresentation(OCRepresentation& rep)
{
- AttributeMap attributeMap = fan.getAttributeMap();
+ bool tempState = false;
+ int tempSpeed = 0;
- if(attributeMap.find("state") != attributeMap.end() && attributeMap.find("speed") != attributeMap.end())
+ // If both entries exist
+ if(rep.getValue("state", tempState) && rep.getValue("speed", tempSpeed))
{
- m_fanState = attributeMap["state"][0].compare("true") == 0;
- m_fanSpeed = std::stoi(attributeMap["speed"][0]);
+ m_fanState = tempState;
+ m_fanSpeed = tempSpeed;
+
+ cout << "\t\t\t\t" << "state: " << m_fanState << endl;
+ cout << "\t\t\t\t" << "speed: " << m_fanSpeed << endl;
}
}
- OCRepresentation getLightRepresentation() const
+ OCRepresentation getLightRepresentation()
{
- OCRepresentation light;
-
- light.setUri(m_lightUri);
-
- std::vector<std::string> interfaces;
- interfaces.push_back(m_lightInterface);
-
- light.setResourceInterfaces(interfaces);
-
- std::vector<std::string> types;
- types.push_back(m_lightType);
-
- light.setResourceTypes(types);
-
- AttributeMap attributeMap;
- AttributeValues stateVal;
- if(m_lightState)
- {
- stateVal.push_back("true");
- }
- else
- {
- stateVal.push_back("false");
- }
+ m_lightRep.setValue("state", m_lightState);
+ m_lightRep.setValue("color", m_lightColor);
- AttributeValues colorVal;
- colorVal.push_back(to_string(m_lightColor));
-
- attributeMap["state"] = stateVal;
- attributeMap["color"] = colorVal;
-
- light.setAttributeMap(attributeMap);
-
- return light;
+ return m_lightRep;
}
- OCRepresentation getFanRepresentation() const
+ OCRepresentation getFanRepresentation()
{
- OCRepresentation fan;
- fan.setUri(m_fanUri);
-
- std::vector<std::string> interfaces;
- interfaces.push_back(m_fanInterface);
-
- fan.setResourceInterfaces(interfaces);
-
- std::vector<std::string> types;
- types.push_back(m_fanType);
-
- fan.setResourceTypes(types);
-
- AttributeMap attributeMap;
- AttributeValues stateVal;
- if(m_fanState)
- {
- stateVal.push_back("true");
- }
- else
- {
- stateVal.push_back("false");
- }
-
- AttributeValues speedVal;
- speedVal.push_back(to_string(m_fanSpeed));
-
- attributeMap["state"] = stateVal;
- attributeMap["speed"] = speedVal;
-
- fan.setAttributeMap(attributeMap);
-
- return fan;
+ m_fanRep.setValue("state", m_fanState);
+ m_fanRep.setValue("speed", m_fanSpeed);
+ return m_fanRep;
}
- OCRepresentation getRoomRepresentation(void) const
+ OCRepresentation getRoomRepresentation(void)
{
- OCRepresentation room;
-
- room.setUri(m_roomUri);
-
- std::vector<std::string> interfaces;
- interfaces.push_back(m_roomInterface1);
- interfaces.push_back(m_roomInterface2);
- interfaces.push_back(m_roomInterface3);
-
- room.setResourceInterfaces(interfaces);
-
- std::vector<std::string> types;
- types.push_back(m_roomType);
-
- room.setResourceTypes(types);
-
std::vector<OCRepresentation> children;
OCRepresentation light = getLightRepresentation();
OCRepresentation fan = getFanRepresentation();
children.push_back(fan);
- room.setChildren(children);
+
+ m_roomRep.setChildren(children);
- return room;
+ return m_roomRep;
}
};
{
cout << "\t\t\trequestType : PUT\n";
- // Check for query params (if any)
- QueryParamsMap queryParamsMap = request->getQueryParameters();
-
- cout << "\t\t\tquery params: \n";
- for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
- {
- cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
- }
-
- // Get the representation from the request
- OCRepresentation rep = request->getResourceRepresentation();
-
- myRoomResource.setRoomRepresentation(rep);
-
- // Do related operations related to PUT request
- rep = myRoomResource.getRoomRepresentation();
+ entityHandlerLight(request, response);
+ entityHandlerFan(request, response);
if(response)
{
- // TODO Error Code
- response->setErrorCode(200);
-
- auto findRes = queryParamsMap.find("if");
-
- if(findRes != queryParamsMap.end())
- {
- response->setResourceRepresentation(rep, findRes->second);
- }
- else
- {
- response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
- }
+ response->setResourceRepresentation(myRoomResource.getRoomRepresentation());
}
-
}
else if(requestType == "POST")
{
{
cout << "\t\t\trequestType : GET\n";
- // Check for query params (if any)
- QueryParamsMap queryParamsMap = request->getQueryParameters();
-
- cout << "\t\t\tquery params: \n";
- for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
- {
- cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
- }
-
- OCRepresentation rep = myRoomResource.getLightRepresentation();
-
if(response)
{
// TODO Error Code
response->setErrorCode(200);
-
- auto findRes = queryParamsMap.find("if");
-
- if(findRes != queryParamsMap.end())
- {
- response->setResourceRepresentation(rep, findRes->second);
- }
- else
- {
- response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
- }
+ response->setResourceRepresentation(myRoomResource.getLightRepresentation());
}
}
{
cout << "\t\t\trequestType : PUT\n";
- // Check for query params (if any)
- QueryParamsMap queryParamsMap = request->getQueryParameters();
-
- cout << "\t\t\tquery params: \n";
- for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
- {
- cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
- }
-
- // Get the representation from the request
OCRepresentation rep = request->getResourceRepresentation();
- myRoomResource.setLightRepresentation(rep);
-
// Do related operations related to PUT request
- rep = myRoomResource.getLightRepresentation();
+ myRoomResource.setLightRepresentation(rep);
if(response)
{
// TODO Error Code
response->setErrorCode(200);
-
- auto findRes = queryParamsMap.find("if");
-
- if(findRes != queryParamsMap.end())
- {
- response->setResourceRepresentation(rep, findRes->second);
- }
- else
- {
- response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
- }
+ response->setResourceRepresentation(myRoomResource.getLightRepresentation());
}
}
{
cout << "\t\t\trequestType : GET\n";
- // Check for query params (if any)
- QueryParamsMap queryParamsMap = request->getQueryParameters();
-
- cout << "\t\t\tquery params: \n";
- for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
- {
- cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
- }
-
- OCRepresentation rep = myRoomResource.getFanRepresentation();
-
if(response)
{
// TODO Error Code
response->setErrorCode(200);
- auto findRes = queryParamsMap.find("if");
-
- if(findRes != queryParamsMap.end())
- {
- response->setResourceRepresentation(rep, findRes->second);
- }
- else
- {
- response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
- }
+ response->setResourceRepresentation(myRoomResource.getFanRepresentation());
}
}
{
cout << "\t\t\trequestType : PUT\n";
- // Check for query params (if any)
- QueryParamsMap queryParamsMap = request->getQueryParameters();
-
- cout << "\t\t\tquery params: \n";
- for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
- {
- cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
- }
-
- // Get the representation from the request
OCRepresentation rep = request->getResourceRepresentation();
- myRoomResource.setFanRepresentation(rep);
-
// Do related operations related to PUT request
- rep = myRoomResource.getFanRepresentation();
+ myRoomResource.setFanRepresentation(rep);
if(response)
{
// TODO Error Code
response->setErrorCode(200);
-
- auto findRes = queryParamsMap.find("if");
-
- if(findRes != queryParamsMap.end())
- {
- response->setResourceRepresentation(rep, findRes->second);
- }
- else
- {
- response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
- }
+ response->setResourceRepresentation(myRoomResource.getFanRepresentation());
}
-
}
else if(requestType == "POST")
{
std::shared_ptr<OCResource> curResource;
static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+class Light
+{
+public:
+
+ bool m_state;
+ int m_power;
+ std::string m_name;
+
+ Light() : m_state(false), m_power(0), m_name("")
+ {
+ }
+};
+
+Light mylight;
+
int observe_count()
{
static int oc = 0;
{
if(eCode == SUCCESS_RESPONSE)
{
- AttributeMap attributeMap = rep.getAttributeMap();
-
std::cout << "OBSERVE RESULT:"<<std::endl;
std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
- for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
- {
- std::cout << "\tAttribute name: "<< it->first << " value: ";
- for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
- {
- std::cout <<"\t"<< *valueItr << " ";
- }
- std::cout << std::endl;
- }
-
+ rep.getValue("state", mylight.m_state);
+ rep.getValue("power", mylight.m_power);
+ rep.getValue("name", mylight.m_name);
+
+ std::cout << "\tstate: " << mylight.m_state << std::endl;
+ std::cout << "\tpower: " << mylight.m_power << std::endl;
+ std::cout << "\tname: " << mylight.m_name << std::endl;
+
if(observe_count() > 30)
{
std::cout<<"Cancelling Observe..."<<std::endl;
{
std::cout << "PUT request was successful" << std::endl;
- AttributeMap attributeMap = rep.getAttributeMap();
-
- for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
- {
- std::cout << "\tAttribute name: "<< it->first << " value: ";
- for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
- {
- std::cout <<"\t"<< *valueItr << " ";
- }
-
- std::cout << std::endl;
- }
-
- std::vector<OCRepresentation> children = rep.getChildren();
-
- for(auto oit = children.begin(); oit != children.end(); ++oit)
- {
- attributeMap = oit->getAttributeMap();
-
- for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
- {
- std::cout << "\tAttribute name: "<< it->first << " value: ";
- for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
- {
- std::cout <<"\t"<< *valueItr << " ";
- }
+ rep.getValue("state", mylight.m_state);
+ rep.getValue("power", mylight.m_power);
+ rep.getValue("name", mylight.m_name);
- std::cout << std::endl;
- }
- }
+ std::cout << "\tstate: " << mylight.m_state << std::endl;
+ std::cout << "\tpower: " << mylight.m_power << std::endl;
+ std::cout << "\tname: " << mylight.m_name << std::endl;
if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
std::cout << endl << "Observe is used." << endl << endl;
else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
std::cout << endl << "ObserveAll is used." << endl << endl;
- QueryParamsMap test;
-
- curResource->observe(OBSERVE_TYPE_TO_USE, test, &onObserve);
+ curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
}
else
if(resource)
{
OCRepresentation rep;
-
+
std::cout << "Putting light representation..."<<std::endl;
- // Create AttributeMap
- AttributeMap attributeMap;
- // Add the attribute name and values in the attribute map
- AttributeValues stateVal;
- stateVal.push_back("true");
- AttributeValues powerVal;
- powerVal.push_back("10");
+ mylight.m_state = true;
+ mylight.m_power = 15;
- attributeMap["state"] = stateVal;
- attributeMap["power"] = powerVal;
+ rep.setValue("state", mylight.m_state);
+ rep.setValue("power", mylight.m_power);
// Create QueryParameters Map and add query params (if any)
QueryParamsMap queryParamsMap;
- rep.setAttributeMap(attributeMap);
-
- // Invoke resource's pit API with attribute map, query map and the callback parameter
+ // Invoke resource's pit API with rep, query map and the callback parameter
resource->put(rep, queryParamsMap, &onPut);
}
}
if(eCode == SUCCESS_RESPONSE)
{
std::cout << "GET request was successful" << std::endl;
-
- AttributeMap attributeMap = rep.getAttributeMap();
-
std::cout << "Resource URI: " << rep.getUri() << std::endl;
- for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
- {
- std::cout << "\tAttribute name: "<< it->first << " value: ";
- for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
- {
- std::cout <<"\t"<< *valueItr << " ";
- }
-
- std::cout << std::endl;
- }
-
- std::vector<OCRepresentation> children = rep.getChildren();
-
- for(auto oit = children.begin(); oit != children.end(); ++oit)
- {
- std::cout << "Child Resource URI: " << oit->getUri() << std::endl;
-
- attributeMap = oit->getAttributeMap();
-
- for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
- {
- std::cout << "\tAttribute name: "<< it->first << " value: ";
- for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
- {
- std::cout <<"\t"<< *valueItr << " ";
- }
+ rep.getValue("state", mylight.m_state);
+ rep.getValue("power", mylight.m_power);
+ rep.getValue("name", mylight.m_name);
- std::cout << std::endl;
- }
- }
+ std::cout << "\tstate: " << mylight.m_state << std::endl;
+ std::cout << "\tpower: " << mylight.m_power << std::endl;
+ std::cout << "\tname: " << mylight.m_name << std::endl;
putLightRepresentation(curResource);
}
// Callback to found resources
void foundResource(std::shared_ptr<OCResource> resource)
{
-
if(curResource)
{
std::cout << "Found another resource, ignoring"<<std::endl;
hostAddress = resource->host();
std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
- // Get the resource types
+ // Get the resource types
std::cout << "\tList of resource types: " << std::endl;
for(auto &resourceTypes : resource->getResourceTypes())
{
std::cout << "\t\t" << resourceTypes << std::endl;
}
-
+
// Get the resource interfaces
std::cout << "\tList of resource interfaces: " << std::endl;
for(auto &resourceInterfaces : resource->getResourceInterfaces())
{
std::cout << "\t\t" << resourceInterfaces << std::endl;
- }
+ }
if(resourceURI == "/a/light")
{
class ClientWorker
{
private:
+ bool m_isFoo;
+ int m_barCount;
void putResourceInfo(const OCRepresentation rep, const OCRepresentation rep2, const int eCode)
{
std::cout << "In PutResourceInfo" << std::endl;
- AttributeMap requestedPut = rep.getAttributeMap();
- AttributeMap attrMap = rep2.getAttributeMap();
-
std::cout <<"Clientside Put response to get was: "<<std::endl;
std::cout <<"ErrorCode: "<<eCode <<std::endl;
{
std::cout<<"Successful Put. Attributes sent were: "<<std::endl;
- for(const auto& attr : requestedPut)
- {
- std::cout << "\tName: "<< attr.first << " value: ";
- for(const auto& val : attr.second)
- {
- std::cout << val << " ";
- }
+ rep.getValue("isFoo", m_isFoo);
+ rep.getValue("barCount", m_barCount);
- std::cout << std::endl;
- }
+ std::cout << "\tisFoo: "<< m_isFoo << std::endl;
+ std::cout << "\tbarCount: "<< m_barCount << std::endl;
std::cout<<"Actual New values are: "<<std::endl;
- for(const auto& attr : attrMap)
- {
- std::cout << "\tName: "<< attr.first << " value: ";
- for(const auto& val : attr.second)
- {
- std::cout << val << " ";
- }
+ rep.getValue("isFoo", m_isFoo);
+ rep.getValue("barCount", m_barCount);
+
+ std::cout << "\tisFoo: "<< m_isFoo << std::endl;
+ std::cout << "\tbarCount: "<< m_barCount << std::endl;
- std::cout << std::endl;
- }
m_cv.notify_all();
}
}
void getResourceInfo(const OCRepresentation rep, const int eCode)
{
std::cout << "In getResourceInfo" << std::endl;
- AttributeMap attrMap = rep.getAttributeMap();
std::cout<<"Clientside response to get was: "<<std::endl;
std::cout<<"Error Code: "<<eCode<<std::endl;
{
std::cout <<"Successful Get. Attributes are: "<<std::endl;
- for(const auto& attr : attrMap)
- {
- std::cout << "\tName: "<< attr.first << " value: ";
- for(const auto& val : attr.second)
- {
- std::cout << val << " ";
- }
+ rep.getValue("isFoo", m_isFoo);
+ rep.getValue("barCount", m_barCount);
- std::cout << std::endl;
- }
+ std::cout << "\tisFoo: "<< m_isFoo << std::endl;
+ std::cout << "\tbarCount: "<< m_barCount << std::endl;
std::cout << "Doing a put on q/foo" <<std::endl;
OCRepresentation rep2(rep);
- AttributeMap attrMap2 = rep2.getAttributeMap();
-
- attrMap2["isFoo"].push_back("false");
- attrMap2["barCount"].push_back("211");
+ m_isFoo = false;
+ m_barCount = 211;
- rep2.setAttributeMap(attrMap2);
+ rep2.setValue("isFoo", m_isFoo);
+ rep2.setValue("barCount", m_barCount);
m_resource->put(rep2, QueryParamsMap(), PutCallback(std::bind(&ClientWorker::putResourceInfo, this, rep2, std::placeholders::_1, std::placeholders::_2)));
}
}
std::cout<<"Doing a get on q/foo."<<std::endl;
- QueryParamsMap test;
- resource->get(test, GetCallback(std::bind(&ClientWorker::getResourceInfo, this, std::placeholders::_1, std::placeholders::_2)));
+
+ resource->get(QueryParamsMap(), GetCallback(std::bind(&ClientWorker::getResourceInfo, this, std::placeholders::_1, std::placeholders::_2)));
}
}
bool m_isFoo;
int m_barCount;
OCResourceHandle m_resourceHandle;
+ OCRepresentation m_rep;
- FooResource(): m_isFoo(true), m_barCount (0){}
+ FooResource(): m_isFoo(true), m_barCount (0)
+ {
+ m_rep.setUri("/q/foo");
+ m_rep.setValue("isFoo", m_isFoo);
+ m_rep.setValue("barCount", m_barCount);
+ }
bool createResource(OCPlatform& platform)
{
return true;
}
- void getRepresentation(OCRepresentation& rep)
+ OCRepresentation get()
{
- AttributeMap attributeMap;
-
- AttributeValues isFooVal;
- isFooVal.push_back(m_isFoo ? "true" : "false");
+ m_rep.setValue("isFoo", m_isFoo);
+ m_rep.setValue("barCount", m_barCount);
- AttributeValues barCt;
- barCt.push_back(to_string(m_barCount));
-
- attributeMap["isFoo"] = isFooVal;
- attributeMap["barCount"] = barCt;
-
- rep.setAttributeMap(attributeMap);
+ return m_rep;
}
- void setRepresentation(OCRepresentation& rep)
+ void put(OCRepresentation& rep)
{
- AttributeMap attributeMap(rep.getAttributeMap());
-
- try
- {
- auto fooVector = attributeMap.at("isFoo");
- m_isFoo = fooVector[0] == "true";
- auto barVector = attributeMap.at("barCount");
- m_barCount = std::stoi(barVector[0]);
- rep.setAttributeMap(attributeMap);
- }
- catch(std::out_of_range& e)
- {
- std::cerr << "setRepresentation(): unable to look up values in attributeMap: " << e.what() << '\n';
- }
+ rep.getValue("isFoo", m_isFoo);
+ rep.getValue("barCount", m_barCount);
}
void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
{
std::cout<<"\t\t\trequestType : GET"<<std::endl;
- OCRepresentation rep;
- getRepresentation(rep);
-
if(response)
{
response->setErrorCode(200);
- response->setResourceRepresentation(rep, "");
+ response->setResourceRepresentation(get(), "");
}
}
else if (request->getRequestType() == "PUT")
std::cout<<"\t\t\trequestType : PUT"<<std::endl;
OCRepresentation rep = request->getResourceRepresentation();
- setRepresentation(rep);
+ put(rep);
if(response)
{
response->setErrorCode(200);
- OCRepresentation rep;
- getRepresentation(rep);
- response->setResourceRepresentation(rep, "");
+ response->setResourceRepresentation(get(), "");
}
}
else
{
public:
/// Access this property from a TB client
+ std::string m_name;
bool m_state;
int m_power;
std::string m_lightUri;
OCResourceHandle m_resourceHandle;
+ OCRepresentation m_lightRep;
public:
/// Constructor
- LightResource(): m_state(false), m_power(0), m_lightUri("/a/light") {}
+ LightResource(): m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") {
+ // Initialize representation
+ m_lightRep.setUri(m_lightUri);
+
+ m_lightRep.setValue("state", m_state);
+ m_lightRep.setValue("power", m_power);
+ m_lightRep.setValue("name", m_name);
+ }
/* Note that this does not need to be a member function: for classes you do not have
access to, you can accomplish this with a free function: */
return m_resourceHandle;
}
- void setRepresentation(OCRepresentation& light)
+ // Puts representation.
+ // Gets values from the representation and
+ // updates the internal state
+ void put(OCRepresentation& rep)
{
- AttributeMap attributeMap = light.getAttributeMap();
-
- if(attributeMap.find("state") != attributeMap.end() && attributeMap.find("power") != attributeMap.end())
- {
- cout << "\t\t\t" << "Received representation: " << endl;
- cout << "\t\t\t\t" << "power: " << attributeMap["power"][0] << endl;
- cout << "\t\t\t\t" << "state: " << attributeMap["state"][0] << endl;
-
- m_state = attributeMap["state"][0].compare("true") == 0;
- m_power= std::stoi(attributeMap["power"][0]);
- }
- }
-
- OCRepresentation getRepresentation()
- {
- OCRepresentation light;
-
- light.setUri(m_lightUri);
-
- std::vector<std::string> interfaces;
- //interfaces.push_back(m_lightInterface);
-
- light.setResourceInterfaces(interfaces);
-
- std::vector<std::string> types;
- //types.push_back(m_lightType);
-
- light.setResourceTypes(types);
+ try {
+ if (rep.getValue("state", m_state))
+ {
+ cout << "\t\t\t\t" << "state: " << m_state << endl;
+ }
+ else
+ {
+ cout << "\t\t\t\t" << "state not found in the representation" << endl;
+ }
- AttributeMap attributeMap;
- AttributeValues stateVal;
- if(m_state)
- {
- stateVal.push_back("true");
+ if (rep.getValue("power", m_power))
+ {
+ cout << "\t\t\t\t" << "power: " << m_power << endl;
+ }
+ else
+ {
+ cout << "\t\t\t\t" << "power not found in the representation" << endl;
+ }
}
- else
+ catch (exception& e)
{
- stateVal.push_back("false");
+ cout << e.what() << endl;
}
- AttributeValues powerVal;
- powerVal.push_back(to_string(m_power));
-
- attributeMap["state"] = stateVal;
- attributeMap["power"] = powerVal;
+ }
- light.setAttributeMap(attributeMap);
+ // gets the updated representation.
+ // Updates the representation with latest internal state before
+ // sending out.
+ OCRepresentation get()
+ {
+ m_lightRep.setValue("state", m_state);
+ m_lightRep.setValue("power", m_power);
- return light;
+ return m_lightRep;
}
void addType(const OC::OCPlatform& platform, const std::string& type) const
};
// Create the instance of the resource class (in this case instance of class 'LightResource').
-LightResource myLightResource;
+LightResource myLight;
// ChangeLightRepresentaion is an observation function,
// which notifies any changes to the resource to stack
// we call notifyObservors
//
// For demostration we are changing the power value and notifying.
- myLightResource.m_power += 10;
+ myLight.m_power += 10;
- cout << "\nPower updated to : " << myLightResource.m_power << endl;
- cout << "Notifying observers with resource handle: " << myLightResource.getHandle() << endl;
+ cout << "\nPower updated to : " << myLight.m_power << endl;
+ cout << "Notifying observers with resource handle: " << myLight.getHandle() << endl;
- OCStackResult result = OCPlatform::notifyObservers(myLightResource.getHandle());
+ OCStackResult result = OCPlatform::notifyObservers(myLight.getHandle());
if(OC_STACK_NO_OBSERVERS == result)
{
{
cout << "\t\t\trequestType : GET\n";
- // Check for query params (if any)
- QueryParamsMap queryParamsMap = request->getQueryParameters();
-
- cout << "\t\t\tquery params: \n";
- for(QueryParamsMap::iterator it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
- {
- cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
- }
-
- // Process query params and do required operations ..
-
- // Get the representation of this resource at this point and send it as response
- // AttributeMap attributeMap;
- OCRepresentation rep;
- rep = myLightResource.getRepresentation();
-
if(response)
{
// TODO Error Code
response->setErrorCode(200);
- auto findRes = queryParamsMap.find("if");
-
- if(findRes != queryParamsMap.end())
- {
- response->setResourceRepresentation(rep, findRes->second);
- }
- else
- {
- response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
- }
+ response->setResourceRepresentation(myLight.get());
}
}
else if(requestType == "PUT")
{
cout << "\t\t\trequestType : PUT\n";
- // Check for query params (if any)
- QueryParamsMap queryParamsMap = request->getQueryParameters();
-
- cout << "\t\t\tquery params: \n";
- for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
- {
- cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
- }
-
- // Get the representation from the request
OCRepresentation rep = request->getResourceRepresentation();
- myLightResource.setRepresentation(rep);
-
// Do related operations related to PUT request
- rep = myLightResource.getRepresentation();
+
+ // Update the lightResource
+ myLight.put(rep);
if(response)
{
// TODO Error Code
response->setErrorCode(200);
- auto findRes = queryParamsMap.find("if");
-
- if(findRes != queryParamsMap.end())
- {
- response->setResourceRepresentation(rep, findRes->second);
- }
- else
- {
- response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
- }
+ response->setResourceRepresentation(myLight.get());
}
}
OCPlatform platform(cfg);
// Invoke createResource function of class light.
+ myLight.createResource(platform);
- myLightResource.createResource(platform);
- myLightResource.addType(platform, std::string("core.brightlight"));
- myLightResource.addInterface(platform, std::string("oc.mi.ll"));
+ myLight.addType(platform, std::string("core.brightlight"));
+ myLight.addInterface(platform, std::string("oc.mi.ll"));
// Perform app tasks
while(true)
{
//log(e.what());
}
-
// No explicit call to stop the platform.
// When OCPlatform destructor is invoked, internally we do platform cleanup
}
#include <vector>
#include <map>
#include <memory>
+
+#include <boost/variant.hpp>
+
#include "ocstack.h"
namespace OC
ObserveAll
};
- // TODO: To find the complete JSon data structure and modify map value type
- // Typedef for attribute values and attribute map.
- typedef std::vector<std::string> AttributeValues;
- typedef std::map<std::string, AttributeValues> AttributeMap;
+ typedef std::map<std::string, std::string> AttributeMap;
+
+ struct AttributeNull {};
+
+ // Forward declaration
+ struct AttributeDataValue;
+
+ typedef std::map<std::string, AttributeDataValue> AttributeMapValue;
+ typedef std::vector<AttributeDataValue> AttributeValueVector;
+
+ typedef boost::variant<
+ AttributeNull,
+ int,
+ bool,
+ std::string,
+ AttributeMapValue,
+ AttributeValueVector> AttributeValue;
+
+ struct AttributeDataValue
+ {
+ AttributeValue data;
+ };
+
+ class ComposeVisitor : public boost::static_visitor<std::string>
+ {
+ public:
+ std::string operator() (const AttributeNull& nl) const
+ {
+ // TODO Not Implemented
+ return std::string();
+ }
+
+ // TODO different int sizes
+ std::string operator() (const int i) const
+ {
+ return std::to_string(i);
+ }
+
+ std::string operator() (const std::string& str) const
+ {
+ return str;
+ }
+
+ std::string operator() (const bool b) const
+ {
+ if(b)
+ {
+ return "true";
+ }
+ else
+ {
+ return "false";
+ }
+ }
+
+ std::string operator() (const AttributeMapValue& objValue) const
+ {
+ // TODO Not Implemented
+ return std::string();
+ std::ostringstream json;
+ }
+
+ std::string operator() (const AttributeValueVector& values) const
+ {
+ // TODO Not Implemented
+ return std::string();
+ std::ostringstream json;
+ }
+ };
+
+ class ParseVisitor : public boost::static_visitor<void>
+ {
+ public:
+
+ ParseVisitor(std::string str): m_str(str)
+ {
+ }
+
+ void operator() (AttributeNull& nl) const
+ {
+ // TODO Not Implemented
+ }
+
+ void operator() (int& i) const
+ {
+ i = std::stoi(m_str);
+ }
+
+ void operator() (std::string& str) const
+ {
+ str = m_str;
+ }
+
+ void operator() (bool& b) const
+ {
+ b = m_str.compare("true") == 0;
+ }
+
+ void operator() (AttributeMapValue& objValue) const
+ {
+ // TODO Not Implemented
+ }
+
+ void operator() (AttributeValueVector& values) const
+ {
+ // TODO Not Implemented
+ }
+
+ private:
+ std::string m_str;
+ };
+
+ typedef std::map<std::string, AttributeValue> AttributeMap1;
+
+ std::string getJSON(const AttributeValue& v);
+ void parseJSON(AttributeValue& v, std::string str);
// Typedef for query parameter map
typedef std::map<std::string, std::string> QueryParamsMap;
AttributeMap m_attributeMap;
std::vector<std::string> m_resourceTypes;
std::vector<std::string> m_resourceInterfaces;
- bool m_observable; // TODO : do we need this here???
int errorCode;
std::vector<OCRepresentation> m_children;
return m_uri;
}
+ template <typename T>
+ void setValue(const std::string& str, const T& val)
+ {
+ m_attributeMap[str] = getJSON(val);
+ }
+
+ template <typename T>
+ bool getValue(const std::string& str, T& val) const
+ {
+ auto x = m_attributeMap.find(str);
+
+ if(m_attributeMap.end() != x)
+ {
+ AttributeValue v = val;
+ parseJSON(v, x->second);
+ val = boost::get<T>(v);
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
void setUri(std::string uri)
{
m_uri = uri;
return m_attributeMap;
}
- void setAttributeMap(AttributeMap map)
+ void setAttributeMap(const AttributeMap map)
{
m_attributeMap = map;
}
/**
* Provides the entire resource attribute representation
- * @return std::map AttributeMap reference containing the name value pairs representing the resource's attributes
+ * @return OCRepresentation reference containing the name value pairs representing the resource's attributes
*/
- const AttributeMap& getAttributeRepresentation() const {return m_attributeMap;}
const OCRepresentation& getResourceRepresentation() const {return m_representation;}
private:
std::string m_requestType;
QueryParamsMap m_queryParameters;
RequestHandlerFlag m_requestHandlerFlag;
- AttributeMap m_attributeMap;
OCRepresentation m_representation;
public:
// This function will not be exposed in future
void setPayload(const std::string& requestPayload)
{
+ AttributeMap attributeMap;
// TODO: The following JSON Parse implementation should be seperated into utitilites
// and used wherever required.
// e.g. parse(std::string& payload, Attributemap& attributeMap)
-
+
std::stringstream requestStream;
requestStream << requestPayload;
boost::property_tree::ptree root;
std::string name = item.first.data();
std::string value = item.second.data();
- AttributeValues values;
- values.push_back(value);
-
- m_attributeMap[name] = values;
+ attributeMap[name] = value;
}
-
- m_representation.setAttributeMap(m_attributeMap);
+
+ m_representation.setAttributeMap(attributeMap);
}
// TODO: This is not a public API for app developers.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-/// @file OCResourceResponse.h
+/// @file OCResourceResponse.h
-/// @brief This file contains the declaration of classes and its members related to
+/// @brief This file contains the declaration of classes and its members related to
/// ResourceResponse.
#ifndef __OCRESOURCERESPONSE_H
typedef std::shared_ptr<OCResourceResponse> Ptr;
/**
- * Default destructor
+ * Default destructor
*/
OCResourceResponse() {}
/**
- * Virtual destructor
+ * Virtual destructor
*/
virtual ~OCResourceResponse(void) {}
void setErrorCode(const int eCode) { m_errorCode = eCode; }
/**
- * API to set the entire resource attribute representation (BATCH)
+ * API to set the entire resource attribute representation
* @param attributeMap reference containing the name value pairs representing the resource's attributes
+ * @param interface specifies the interface
*/
- void setResourceRepresentation(OCRepresentation& rep, std::string interface) {
+ void setResourceRepresentation(OCRepresentation& rep, std::string interface) {
if(!interface.compare(LINK_INTERFACE))
{
setResourceRepresentationLL(rep);
}
/**
+ * API to set the entire resource attribute representation
+ * @param attributeMap rvalue reference containing the name value pairs representing the resource's attributes
+ * @param interface specifies the interface
+ */
+ void setResourceRepresentation(OCRepresentation&& rep, std::string interface) {
+ setResourceRepresentation(rep, interface);
+ }
+
+ /**
+ * API to set the entire resource attribute representation
+ * @param attributeMap reference containing the name value pairs representing the resource's attributes
+ */
+ void setResourceRepresentation(OCRepresentation& rep) {
+ // Call the default
+ setResourceRepresentationDefault(rep);
+ }
+
+ /**
+ * API to set the entire resource attribute representation
+ * @param attributeMap rvalue reference containing the name value pairs representing the resource's attributes
+ */
+ void setResourceRepresentation(OCRepresentation&& rep) {
+ // Call the above function
+ setResourceRepresentation(rep);
+ }
+
+ /**
* API to set the entire resource attribute representation (Linked List Interface))
* @param attributeMap reference containing the name value pairs representing the resource's attributes
*/
- void setResourceRepresentationLL(OCRepresentation& rep) {
+ void setResourceRepresentationLL(OCRepresentation& rep) {
// Default Set
// Parent
payload << "{";
- payload << "\"href\":";
+ payload << "\"href\":";
payload << "\"" ;
payload << rep.getUri();
payload << "\"" ;
// Children stuff
std::vector<OCRepresentation> children = rep.getChildren();
-
+
for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
{
- payload << ",{\"href\":";
-
+ payload << ",{\"href\":";
+
payload << "\"" ;
payload << oitr->getUri();
payload << "\"" ;
* API to set the entire resource attribute representation (Default))
* @param attributeMap reference containing the name value pairs representing the resource's attributes
*/
- void setResourceRepresentationDefault(OCRepresentation& rep) {
+ void setResourceRepresentationDefault(OCRepresentation& rep) {
// Default Set
// Parent
payload << "{";
- payload << "\"href\":";
+ payload << "\"href\":";
payload << "\"" ;
payload << rep.getUri();
payload << "\"" ;
{
payload << ',';
}
- payload << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
+ payload << "\""<<itr->first<<"\":\""<< itr->second <<"\"";
}
payload << "}}";
// Children stuff
std::vector<OCRepresentation> children = rep.getChildren();
-
+
for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
{
- payload << ",{\"href\":";
+ payload << ",{\"href\":";
payload << "\"" ;
payload << oitr->getUri();
* API to set the entire resource attribute representation (BATCH)
* @param attributeMap reference containing the name value pairs representing the resource's attributes
*/
- void setResourceRepresentationBatch(OCRepresentation& rep) {
+ void setResourceRepresentationBatch(OCRepresentation& rep) {
ostringstream payload;
// Parent
payload << "{";
- payload << "\"href\":";
+ payload << "\"href\":";
payload << "\"" ;
payload << rep.getUri();
payload << "\"" ;
payload << "}";
std::vector<OCRepresentation> children = rep.getChildren();
-
+
for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
{
payload << ',';
payload << "{";
-
- payload << "\"href\":";
-
+
+ payload << "\"href\":";
+
payload << "\"" ;
payload << oitr->getUri();
payload << "\"" ;
{
payload << ',';
}
- payload << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
+ payload << "\""<<itr->first<<"\":\""<< itr->second<<"\"";
}
payload << "}}";
m_payload = payload.str();
}
-
- /** TODO remove this once after above function stabilize.
- * API to set the entire resource attribute representation
- * @param attributeMap reference containing the name value pairs representing the resource's attributes
- */
- void setResourceRepresentation(AttributeMap& attributes) {
-
- // TODO To be refactored
- ostringstream payload;
-
- payload << "{";
-
- // TODO fix this (do this programmatically)
- payload << "\"href\":\"/a/room\"";
-
- payload << ",\"rep\":{";
-
- for(AttributeMap::const_iterator itr = attributes.begin(); itr!= attributes.end(); ++ itr)
- {
- if(itr != attributes.begin())
- {
- payload << ',';
- }
- // cout << itr->first << ":" <, itr->second.front() << endl;
- payload << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
-
- }
-
- payload << "}}";
-
- m_payload = payload.str();
- }
-
private:
std::string m_payload;
int m_errorCode;
// TODO only stack should have visibility and apps should not
public:
- /**
- * Get error code
- */
- int getErrorCode() const;
-
/**
- * Get the resource attribute representation
+ * Get error code
*/
- AttributeMap& getResourceRepresentation() const;
+ int getErrorCode() const;
- // TODO This should go away & just use getResourceRepresentation
+ // TODO This should go away & just use getResourceRepresentation
std::string getPayload()
{
return m_payload;
{
std::string name = item.first.data();
std::string value = item.second.data();
- AttributeValues values;
- values.push_back(value);
- attrs[name] = values;
+ attrs[name] = value;
}
if (isRoot)
{
payload << ',';
}
- payload << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
+ payload << "\""<<itr->first<<"\":\""<< itr->second <<"\"";
}
payload << "}}";
return payload.str();
}
OCStackResult InProcClientWrapper::SetResourceAttributes(const std::string& host,
- const std::string& uri, const OCRepresentation& attributes,
+ const std::string& uri, const OCRepresentation& rep,
const QueryParamsMap& queryParams, PutCallback& callback)
{
OCStackResult result;
OCDoHandle handle;
result = OCDoResource(&handle, OC_REST_PUT,
os.str().c_str(), nullptr,
- assembleSetResourcePayload(attributes).c_str(),
+ assembleSetResourcePayload(rep).c_str(),
static_cast<OCQualityOfService>(m_cfg.QoS),
&cbdata);
}
#include <uri.h> // libcoap
#include <option.h> // libcoap
}
+
+#include <OCApi.h>
+
namespace OC{
+
+ std::string getJSON(const AttributeValue& v)
+ {
+ return boost::apply_visitor(ComposeVisitor(), v);
+ }
+
+ void parseJSON(AttributeValue& v, std::string str)
+ {
+ boost::apply_visitor(ParseVisitor(str), v);
+ }
+
// Helper function to escape special character.
std::string escapeString(const std::string& value)
{