bool m_state;
std::string m_name;
- bool m_lightState;
- int m_lightPower;
+ std::vector<bool> m_lightStates;
+ std::vector<int> m_lightPowers;
OCRepresentation m_lightRep;
Garage() : m_state(false), m_name("")
Garage myGarage;
-// callback handler on PUT request
-void onPut(const OCRepresentation& rep, const int eCode)
+void printRepresentation(const OCRepresentation& rep)
{
- if(eCode == SUCCESS_RESPONSE)
- {
- std::cout << "PUT request was successful" << std::endl;
-
rep.getValue("state", myGarage.m_state);
rep.getValue("name", myGarage.m_name);
rep.getValue("light", myGarage.m_lightRep);
- myGarage.m_lightRep.getValue("state", myGarage.m_lightState);
- myGarage.m_lightRep.getValue("power", myGarage.m_lightPower);
+ myGarage.m_lightRep.getValue("states", myGarage.m_lightStates);
+ myGarage.m_lightRep.getValue("powers", myGarage.m_lightPowers);
std::cout << "\tstate: " << myGarage.m_state << std::endl;
std::cout << "\tname: " << myGarage.m_name << std::endl;
- std::cout << "\tlightRep: state: " << myGarage.m_lightState << std::endl;
- std::cout << "\tlightRep: power: " << myGarage.m_lightPower << std::endl;
+ std::cout << "\tlightRep: states: ";
+
+ int first = 1;
+ for(auto state: myGarage.m_lightStates)
+ {
+ if(first)
+ {
+ std::cout << state;
+ first = 0;
+ }
+ else
+ {
+ std::cout << "," << state;
+ }
+ }
+
+ std::cout << std::endl;
+ std::cout << "\tlightRep: powers: ";
+ first = 1;
+ for(auto power: myGarage.m_lightPowers)
+ {
+ if(first)
+ {
+ std::cout << power;
+ first = 0;
+ }
+ else
+ {
+ std::cout << "," << power;
+ }
+ }
+ std::cout << std::endl;
+
+}
+// callback handler on PUT request
+void onPut(const OCRepresentation& rep, const int eCode)
+{
+ if(eCode == SUCCESS_RESPONSE)
+ {
+ std::cout << "PUT request was successful" << std::endl;
+
+ printRepresentation(rep);
}
else
{
std::cout << "GET request was successful" << std::endl;
std::cout << "Resource URI: " << rep.getUri() << std::endl;
- rep.getValue("state", myGarage.m_state);
- rep.getValue("name", myGarage.m_name);
- rep.getValue("light", myGarage.m_lightRep);
-
- myGarage.m_lightRep.getValue("state", myGarage.m_lightState);
- myGarage.m_lightRep.getValue("power", myGarage.m_lightPower);
-
- std::cout << "\tstate: " << myGarage.m_state << std::endl;
- std::cout << "\tname: " << myGarage.m_name << std::endl;
- std::cout << "\tlightRep: state: " << myGarage.m_lightState << std::endl;
- std::cout << "\tlightRep: power: " << myGarage.m_lightPower << std::endl;
+ printRepresentation(rep);
putLightRepresentation(curResource);
}
OCRepresentation m_garageRep;
ObservationIds m_interestedObservers;
- // Light representation with in GarageResource
+ // array of lights representation with in GarageResource
OCRepresentation m_lightRep;
- bool m_lightState;
- int m_lightPower;
+ std::vector<bool> m_lightStates;
+ std::vector<int> m_lightPowers;
public:
/// Constructor
m_garageRep.setValue("state", m_state);
m_garageRep.setValue("name", m_name);
- m_lightState = true;
- m_lightPower = 10;
- m_lightRep.setValue("state", m_lightState);
- m_lightRep.setValue("power", m_lightPower);
+ for(int i = 0; i <= 9; i++)
+ {
+ m_lightStates.push_back(i % 2 == 0);
+ m_lightPowers.push_back(i);
+ }
+
+ m_lightRep.setValue("states", m_lightStates);
+ m_lightRep.setValue("powers", m_lightPowers);
// Storing another representation within a representation
m_garageRep.setValue("light", m_lightRep);
#include <vector>
#include <map>
#include <memory>
+#include <iterator>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
}
};
- struct AttributeNull {};
-
- // Forward declaration
- struct AttributeDataValue;
-
- typedef std::map<std::string, AttributeDataValue> AttributeMapValue;
- typedef std::vector<AttributeDataValue> AttributeValueVector;
-
typedef boost::variant<
- AttributeNull,
+ // TODO NULL value to be implmented.
int,
+ double,
bool,
std::string,
- AttributeMapValue,
- OCRepresentation,
- AttributeValueVector> AttributeValue;
+ std::vector<int>,
+ std::vector<double>,
+ std::vector<bool>,
+ std::vector<std::string>,
+ OCRepresentation> AttributeValue;
struct AttributeDataValue
{
AttributeValue data;
};
+ template <typename T>
+ inline std::string getJSONFromVector(std::vector<T> v)
+ {
+ std::ostringstream json;
+
+ json << "\"[";
+ if(v.size() != 0)
+ {
+ std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(json, ","));
+ json << v.back();
+ }
+ json << "]\"";
+
+ return json.str();
+ }
+
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 double d) const
+ {
+ return std::to_string(d);
+ }
+
std::string operator() (const std::string& str) const
{
std::ostringstream json;
}
}
- std::string operator() (const AttributeMapValue& objValue) const
+ std::string operator() (const std::vector<int> numbers) const
{
- // TODO Not Implemented
- return std::string();
- std::ostringstream json;
+ return getJSONFromVector(numbers);
+ }
+
+ std::string operator() (const std::vector<double> numbers) const
+ {
+ return getJSONFromVector(numbers);
}
- std::string operator() (const AttributeValueVector& values) const
+ std::string operator() (const std::vector<bool> bools) const
{
- // TODO Not Implemented
- return std::string();
std::ostringstream json;
+ int first = 1;
+
+ json << "\"[";
+ for(auto b: bools)
+ {
+ if(first)
+ {
+ b ? json << "true" : json << "false";
+ first = 0;
+ }
+ else
+ {
+ b ? json << ",true" : json << ",false";
+ }
+ }
+ json << "]\"";
+
+ return json.str();
+ }
+
+ std::string operator() (const std::vector<std::string> strings) const
+ {
+ return getJSONFromVector(strings);
}
std::string operator() (const OCRepresentation& rep) const
};
+ inline void split(std::string input, char delimiter, std::vector<std::string>& tokens)
+ {
+ std::stringstream ss(input);
+ std::string item;
+
+ while(std::getline(ss, item, delimiter))
+ {
+ tokens.push_back(item);
+ }
+ }
+
class ParseVisitor : public boost::static_visitor<void>
{
public:
{
}
- void operator() (AttributeNull& nl) const
+ void operator() (int& i) const
{
- // TODO Not Implemented
+ i = std::stoi(m_str);
}
- void operator() (int& i) const
+ void operator() (double& d) const
{
- i = std::stoi(m_str);
+ d = std::stod(m_str);
}
void operator() (std::string& str) const
b = m_str.compare("true") == 0;
}
- void operator() (AttributeMapValue& objValue) const
+ void operator() (std::vector<int>& numbers) const
{
- // TODO Not Implemented
+ numbers.clear();
+
+ if(m_str.length() >= 2)
+ {
+ std::string str = m_str.substr(1, m_str.length()-2);
+
+ std::vector<std::string> tokens;
+ split(str, ',', tokens);
+
+ for(auto s: tokens)
+ {
+ numbers.push_back(std::stoi(s));
+ }
+ }
+ else
+ {
+ // TODO Logging
+ std::cout << "Array type should have atleast []\n";
+ }
+
+ }
+
+ void operator() (std::vector<double>& numbers) const
+ {
+ numbers.clear();
+
+ if(m_str.length() >= 2)
+ {
+ std::string str = m_str.substr(1, m_str.length()-2);
+ std::vector<std::string> tokens;
+ split(str, ',', tokens);
+
+ for(auto s: tokens)
+ {
+ numbers.push_back(std::stod(s));
+ }
+ }
+ else
+ {
+ // TODO Logging
+ std::cout << "Array type should have atleast []\n";
+ }
+ }
+
+ void operator() (std::vector<bool>& bools) const
+ {
+ bools.clear();
+
+ if(m_str.length() >= 2)
+ {
+ std::string str = m_str.substr(1, m_str.length()-2);
+
+ std::vector<std::string> tokens;
+ split(str, ',', tokens);
+
+ for(auto s: tokens)
+ {
+ bools.push_back(s.compare("true") == 0);
+ }
+ }
+ else
+ {
+ // TODO Logging
+ std::cout << "Array type should have atleast []\n";
+ }
+
}
- void operator() (AttributeValueVector& values) const
+ void operator() (std::vector<std::string>& strings) const
{
- // TODO Not Implemented
+ strings.clear();
+
+ if(m_str.length() >= 2)
+ {
+ std::string str = m_str.substr(1, m_str.length()-2);
+
+ std::vector<std::string> tokens;
+ split(str, ',', tokens);
+
+ for(auto s: tokens)
+ {
+ strings.push_back(s);
+ }
+ }
+ else
+ {
+ // TODO Logging
+ std::cout << "Array type should have atleast []\n";
+ }
}
void operator() (OCRepresentation& rep) const