'../../include',
])
+ss_resource_bundle_env.PrependUnique(LIBS = ['ResContainerLib'])
-ss_resource_bundle_src = [ Glob(SS_RESOURCE_BUNDLE_DIR + 'src/*.cpp'), Glob('src/*.cpp')]
+ss_resource_bundle_src = [ Glob(SS_RESOURCE_BUNDLE_DIR + 'src/*.cpp')]
SoftSensorBundle = ss_resource_bundle_env.SharedLibrary('SoftSensorBundle', ss_resource_bundle_src)
ss_resource_bundle_env.InstallTarget(SoftSensorBundle, 'libSoftSensorBundle')
'include/'
])
- hue_resource_bundle_env.PrependUnique(LIBS = ['curl'])
+ hue_resource_bundle_env.PrependUnique(LIBS = ['curl', 'ResContainerLib'])
- hue_resource_bundle_src = [ Glob(HUE_RESOURCE_BUNDLE_DIR + 'src/*.cpp'), Glob('src/*.cpp')]
+ hue_resource_bundle_src = [ Glob(HUE_RESOURCE_BUNDLE_DIR + 'src/*.cpp')]
HueBundle = hue_resource_bundle_env.SharedLibrary('HueBundle', hue_resource_bundle_src)
hue_resource_bundle_env.InstallTarget(HueBundle, 'libHueBundle')
-lib_env = conf2.Finish()
+ lib_env = conf2.Finish()
######################################################################
# build resource container unit tests
######################################################################
-#SConscript('unittests/SConscript')
+SConscript('unittests/SConscript')
######################################################################
-# Build Container Test
+# Build Container Sample
######################################################################
-containertest_env = resource_container_env.Clone();
-containertest_env.AppendUnique(LINKFLAGS=["-rdynamic"])
+containersample_env = resource_container_env.Clone();
+containersample_env.AppendUnique(LINKFLAGS=["-rdynamic"])
# Copy test configuration
Command("examples/ResourceContainerConfig.xml","examples/ResourceContainerConfig.xml", Copy("$TARGET", "$SOURCE"))
Ignore("examples/ResourceContainerConfig.xml", "examples/ResourceContainerConfig.xml")
-
containersampleapp_src = [res_container_src , 'src/ContainerSample.cpp']
-containersampleapp = containertest_env.Program('ContainerSample',containersampleapp_src)
+containersampleapp = containersample_env.Program('ContainerSample',containersampleapp_src)
Alias("containersample", containersampleapp)
env.AppendTarget('containersample')
+
+######################################################################
+# Build Container Sample Client
+######################################################################
+containersampleclient_env = resource_container_env.Clone();
+containersampleclient_env.AppendUnique(LINKFLAGS=["-rdynamic"])
+containersampleclient_src = [res_container_src , 'src/ContainerSampleClient.cpp']
+containersampleclientapp = containersample_env.Program('ContainerSampleClient',containersampleclient_src)
+Alias("containersampleclient", containersampleclientapp)
+env.AppendTarget('containersampleclient')
+
+
#include <vector>
#include "NotificationReceiver.h"
+#include "ResourceAttributes.h"
using namespace std;
*
* @todo use type variant mechanism
*/
- virtual string getAttribute(string attributeName);
+ //virtual string getAttribute(string attributeName);
/**
* Execute the logic of bundle to set the value of attribute
*
* @return void
*/
- virtual void setAttribute(string attributeName, string value);
+ //virtual void setAttribute(string attributeName, string value);
/**
* Return the list of attribute names of the resource
*/
void registerObserver(NotificationReceiver *pNotiReceiver);
+ virtual ResourceAttributes& getAttributes();
+
+ virtual void setAttribute(std::string key, ResourceAttributes::Value&&);
+
+ virtual ResourceAttributes::Value getAttribute(const std::string& key);
+
+
public:
string m_bundleId;
string m_name, m_uri, m_resourceType, m_address;
map< string, vector< map< string, string > > > m_mapResourceProperty;
private:
- map< string, string > m_mapAttributes;
+ //map< string, string > m_mapAttributes;
NotificationReceiver *m_pNotiReceiver;
+ ResourceAttributes m_resourceAttributes;
+
};
}
}
*/
virtual ~ProtocolBridgeResource();
- /**
- * Execute the logic of bundle to get the value of attribute
- *
- * @param attributeName - name of attribute to get
- *
- * @return string - return value of the attribute
- *
- * @todo use type variant mechanism
- */
- virtual string getAttribute(string attributeName) = 0;
+ virtual ResourceAttributes& getAttributes()=0;
- /**
- * Execute the logic of bundle to set the value of attribute
- *
- * @param attributeName - name of attribute to set
- *
- * @param value - value of attribute to set
- *
- * @return void
- */
- virtual void setAttribute(string attributeName, string value) = 0;
+ virtual void setAttribute(std::string key, ResourceAttributes::Value&&)=0;
+
+ virtual ResourceAttributes::Value getAttribute(const std::string& key)=0;
};
}
}
*
* @todo use type variant mechanism
*/
- virtual string getAttribute(string attributeName) = 0;
+ virtual ResourceAttributes::Value getAttribute(string attributeName) = 0;
/**
* Execute the logic of bundle to set the value of attribute
*
* @return void
*/
- virtual void setAttribute(string attributeName, string value) = 0;
+ virtual void setAttribute(string attributeName, ResourceAttributes::Value value) = 0;
/**
* Set Input data to update output value of the soft sensor
* @param key name of the attribute to be read
* @return Value of the attribute
*/
- public abstract String getAttribute(String key);
+ public abstract String getAttribute(String key);
/**
* Attribute keys provided through by the bundle resource.
HueLightResource hueLightResource = new HueLightResource(connector,
config.getName(), config.getURI(),
config.getResourceType(), config.getAddress());
+
+ System.out.println("Registration of HueLightresource");
registerResource(hueLightResource);
}
}
public HueLightResource() {
initAttributes();
m_resourceType = "oic.light.control";
+
}
public HueLightResource(HueConnector hueConnector, String name, String uri,
m_name = name;
m_uri = uri;
m_resourceType = resourceType;
- m_address = address;
+ m_address = address;
}
protected void initAttributes() {
}
public String getAttribute(String key) {
- System.out.println("Get attribute called - key: " + key);
+
// map key to hue address
// read from Hue gateway, parse resource representation and return
// attribute
- m_hueConnector.read(m_address);
- return "";
- }
+ //m_hueConnector.read(m_address);
+ return m_attributes.get(key);
+ }
@Override
public String toString() {
HueLight();
HueLight(HueConnector* connector, std::string address);
virtual ~HueLight();
- string getAttribute(string attributeName);
- void setAttribute(string attributeName, string value);
virtual void initAttributes();
+ virtual ResourceAttributes& getAttributes();
+
+ virtual void setAttribute(std::string key, ResourceAttributes::Value&&);
+
+ virtual ResourceAttributes::Value getAttribute(const std::string& key);
private:
std::string m_address;
HueConnector* m_connector;
- };
- }
+ };}
}
#endif /* HUEGATEWAYCLIENT_H_ */
}
-string HueLight::getAttribute(string attributeName)
-{
- return BundleResource::getAttribute(attributeName);
+void HueLight::initAttributes(){
+
+ BundleResource::setAttribute("on-off", false);
+ BundleResource::setAttribute("dim", 0);
+ BundleResource::setAttribute("color", 0);
}
-void HueLight::initAttributes()
-{
- BundleResource::setAttribute("on-off", "false");
- BundleResource::setAttribute("dim", "0");
- BundleResource::setAttribute("color", "0");
- /*m_mapAttributes.insert(std::pair<string, string>("on-off", "false"));
- m_mapAttributes.insert(std::pair<string, string>("dim", "0"));
- m_mapAttributes.insert(std::pair<string, string>("color", "0"));*/
+ResourceAttributes& HueLight::getAttributes(){
+ return BundleResource::getAttributes();
}
-void HueLight::setAttribute(string attributeName, string value)
-{
- cout << "HueLight::setAttribute setting " << attributeName << " to " << value << std::endl;
+ResourceAttributes::Value HueLight::getAttribute(const std::string& key){
+ cout << "HueLight::getAttribute called for " << key << " called" << endl;
+ return BundleResource::getAttribute(key);
+}
+void HueLight::setAttribute(std::string attributeName, ResourceAttributes::Value&& value){
+ cout << "HueLight::setAttribute setting " << attributeName << " to " << value.toString() << std::endl;
if (attributeName == "on-off")
{
- m_connector->transmit(this->m_address + "/state", "{\"on\":" + value + "}");
+ m_connector->transmit(this->m_address + "/state", "{\"on\":" + value.toString() + "}");
}
if (attributeName == "dim")
{
- //m_connector->transmit(this->m_address + "/state", "{\"bri\":" + (value * 2.5) + "}");
- m_connector->transmit(this->m_address + "/state", "{\"bri\":" + value + "}");
+ // needs conversion * 2.5
+ m_connector->transmit(this->m_address + "/state", "{\"bri\":" + value.toString() + "}");
}
if (attributeName == "color")
{
- //m_connector->transmit(this->m_address+ "/state", "{\"hue\":" + (value * 650) + "}");
- m_connector->transmit(this->m_address + "/state", "{\"hue\":" + value + "}");
+ // needs conversion *650
+ m_connector->transmit(this->m_address+ "/state", "{\"hue\":" + value.toString() + "}");
}
+
+ BundleResource::setAttribute(attributeName, std::move(value));
}
void HueSampleBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
std::string bundleId)
{
- std::cout << "HueSampleBundle::activateBundle called" << std::endl;
m_pResourceContainer = resourceContainer;
m_bundleId = bundleId;
void HueSampleBundleActivator::createResource(resourceInfo resourceInfo)
{
- std::cout << "HueSampleBundle::createResource called" << std::endl;
if (resourceInfo.resourceType == "oic.light.control")
{
</resourceInfo>
</resources>
</bundle>
- <bundle>
+ <!--<bundle>
<id>oic.bundle.hueJavaSample</id>
<path>../../../../../../../../service/resource-manipulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/target/hue-0.1-jar-with-dependencies.jar</path>
<libraryPath>.</libraryPath>
<address>http://192.168.0.2/api/newdeveloper/lights/1</address>
</resourceInfo>
</resources>
- </bundle>
+ </bundle>-->
</container>
DiscomfortIndexSensorResource(vector<string> inputAttributes);
~DiscomfortIndexSensorResource();
- string getAttribute(string attributeName);
- void setAttribute(string attributeName, string value);
+ ResourceAttributes::Value getAttribute(string attributeName);
+ void setAttribute(string attributeName, ResourceAttributes::Value value);
virtual void initAttributes();
void setInputAttribute(SensorData input);
BundleResource::setAttribute("temperature", "23");
BundleResource::setAttribute("humidity", "40");
- BundleResource::setAttribute("discomfortIndex", "5");
+ BundleResource::setAttribute("discomfortIndex","5");
}
-string DiscomfortIndexSensorResource::getAttribute(string attributeName)
+ResourceAttributes::Value DiscomfortIndexSensorResource::getAttribute(string attributeName)
{
cout << "DiscomfortIndexSensorResource::getAttribute called !!" << endl;
return BundleResource::getAttribute(attributeName);
}
-void DiscomfortIndexSensorResource::setAttribute(string attributeName, string value)
+void DiscomfortIndexSensorResource::setAttribute(string attributeName, ResourceAttributes::Value value)
{
cout << "DiscomfortIndexSensorResource::setAttribute called !!" << endl;
}
jobjectArray attributes);
virtual ~JavaBundleResource();
- string getAttribute(string attributeName);
- void setAttribute(string attributeName, string value);
- void initAttributes();
+ virtual ResourceAttributes& getAttributes();
+
+ virtual void setAttribute(std::string key, ResourceAttributes::Value&&);
+
+ virtual ResourceAttributes::Value getAttribute(const std::string& key);
+ virtual void initAttributes();
private:
// needs to be a GlobalRef
jobject bundleResource;
using namespace OIC::Service;
-std::map<string, JavaBundleResource*> java_resources;
+std::map< string, JavaBundleResource* > java_resources;
/*
* Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
* Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_registerJavaResource
- (JNIEnv *env, jobject obj, jobject bundleResource, jobjectArray attributes, jstring bundleId, jstring uri, jstring resourceType, jstring res_name){
+(JNIEnv *env, jobject obj, jobject bundleResource, jobjectArray attributes, jstring bundleId, jstring uri, jstring resourceType, jstring res_name)
+{
//return;
//static std::map<jobject, JavaBundleResource > javaBundles;
const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
const char *str_uri = env->GetStringUTFChars(uri, 0);
const char *str_resourceType = env->GetStringUTFChars(resourceType, 0);
const char *str_res_name = env->GetStringUTFChars(res_name, 0);
+
JavaBundleResource *javaBundleResource = new JavaBundleResource(env, obj, bundleResource, str_bundleId, attributes);
ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
container->registerResource(javaBundleResource);
java_resources[str_uri] = javaBundleResource;
+
}
/*
* Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_unregisterJavaResource
- (JNIEnv *env, jobject obj, jobject bundleResource, jstring uri){
+(JNIEnv *env, jobject obj, jobject bundleResource, jstring uri)
+{
const char *str_uri = env->GetStringUTFChars(uri, 0);
- if(java_resources[str_uri] != NULL){
+ if(java_resources[str_uri] != NULL)
+ {
ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
container->unregisterResource(java_resources[str_uri]);
java_resources.erase(str_uri);
* Method: getNumberOfConfiguredResources
* Signature: (Ljava/lang/String;)I
*/
-JNIEXPORT jint JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_getNumberOfConfiguredResources
- (JNIEnv *env, jobject obj, jstring bundleId){
+JNIEXPORT jint JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_getNumberOfConfiguredResources(
+ JNIEnv *env, jobject obj, jstring bundleId)
+{
const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
- vector<resourceInfo> resourceConfig;
+ vector< resourceInfo > resourceConfig;
container->getResourceConfiguration(str_bundleId, &resourceConfig);
return resourceConfig.size();
* Method: getConfiguredResourceParams
* Signature: (Ljava/lang/String;I)[Ljava/lang/String;
*/
-JNIEXPORT jobjectArray JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_getConfiguredResourceParams
- (JNIEnv *env, jobject obj, jstring bundleId, jint resourceId){
+JNIEXPORT jobjectArray JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_getConfiguredResourceParams(
+ JNIEnv *env, jobject obj, jstring bundleId, jint resourceId)
+{
jobjectArray ret;
const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
- vector<resourceInfo> resourceConfig;
+ vector< resourceInfo > resourceConfig;
container->getResourceConfiguration(str_bundleId, &resourceConfig);
resourceInfo conf = resourceConfig[resourceId];
- ret = (jobjectArray) env->NewObjectArray(4,
- env->FindClass("java/lang/String"),
+ ret = (jobjectArray) env->NewObjectArray(4, env->FindClass("java/lang/String"),
env->NewStringUTF(""));
env->SetObjectArrayElement(ret, 0, env->NewStringUTF(conf.name.c_str()));
m_pNotiReceiver = pNotiReceiver;
}
- void BundleResource::setAttribute(string attributeName, string value)
+ std::list< string > BundleResource::getAttributeNames()
{
- this->m_mapAttributes[attributeName] = value;
-
- if (m_pNotiReceiver != NULL && !m_uri.empty())
- {
- m_pNotiReceiver->onNotificationReceived(m_uri);
+ std::list< string > ret;
+ for (ResourceAttributes::iterator it = m_resourceAttributes.begin(); it != m_resourceAttributes.end(); ++it){
+ ret.push_back(it->key());
}
+ return ret;
+ }
+ ResourceAttributes& BundleResource::getAttributes(){
+ return m_resourceAttributes;
}
- std::string BundleResource::getAttribute(string attributeName)
+ void BundleResource::setAttribute(std::string key, ResourceAttributes::Value&& value)
{
- return this->m_mapAttributes[attributeName];
+ cout << "Bundle resource set attribute " << value.toString() << "|" << endl;
+ m_resourceAttributes[key] = value;
}
- std::list< string > BundleResource::getAttributeNames()
- {
- std::list< string > ret;
- for (map< string, string >::iterator it = m_mapAttributes.begin();
- it != m_mapAttributes.end(); ++it)
- {
- ret.push_back(it->first);
- }
- return ret;
+ ResourceAttributes::Value BundleResource::getAttribute(const std::string& key){
+ cout << "Bundle resource get attribute " << m_resourceAttributes.at(key).toString() << "|" << endl;
+ return m_resourceAttributes.at(key);
}
}
--- /dev/null
+//******************************************************************
+//
+// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+// OCClient.cpp : Defines the entry point for the console application.
+//
+#include <string>
+#include <map>
+#include <cstdlib>
+#include <pthread.h>
+#include <mutex>
+#include <condition_variable>
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+using namespace OC;
+
+typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
+
+DiscoveredResourceMap discoveredResources;
+std::shared_ptr<OCResource> curResource;
+static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+std::mutex curResourceLock;
+
+class Light
+{
+public:
+
+ bool m_on_off;
+ int m_color;
+ int m_dim;
+ std::string m_name;
+
+
+ Light() : m_on_off(false), m_color(0), m_dim(0), m_name("")
+ {
+ }
+};
+
+Light mylight;
+
+int observe_count()
+{
+ static int oc = 0;
+ return ++oc;
+}
+
+void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
+ const int& eCode, const int& sequenceNumber)
+{
+ try
+ {
+ if(eCode == OC_STACK_OK)
+ {
+ std::cout << "OBSERVE RESULT:"<<std::endl;
+ std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
+ rep.getValue("on-off", mylight.m_on_off);
+ rep.getValue("color", mylight.m_color);
+ rep.getValue("dim", mylight.m_dim);
+ rep.getValue("name", mylight.m_name);
+
+ std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
+ std::cout << "\tcolor: " << mylight.m_color << std::endl;
+ std::cout << "\tdim: " << mylight.m_dim << std::endl;
+
+ if(observe_count() > 10)
+ {
+ std::cout<<"Cancelling Observe..."<<std::endl;
+ OCStackResult result = curResource->cancelObserve();
+
+ std::cout << "Cancel result: "<< result <<std::endl;
+ sleep(10);
+ std::cout << "DONE"<<std::endl;
+ std::exit(0);
+ }
+ }
+ else
+ {
+ std::cout << "onObserve Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
+ }
+ catch(std::exception& e)
+ {
+ std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
+ }
+
+}
+
+void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+{
+ try
+ {
+ if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
+ {
+ std::cout << "POST request was successful" << std::endl;
+
+ if(rep.hasAttribute("createduri"))
+ {
+ std::cout << "\tUri of the created resource: "
+ << rep.getValue<std::string>("createduri") << std::endl;
+ }
+ else
+ {
+ rep.getValue("on-off", mylight.m_on_off);
+ rep.getValue("color", mylight.m_color);
+ rep.getValue("dim", mylight.m_dim);
+
+ std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
+ std::cout << "\tcolor: " << mylight.m_color << std::endl;
+ std::cout << "\tdim: " << mylight.m_dim << std::endl;
+ }
+
+ if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
+ std::cout << std::endl << "Observe is used." << std::endl << std::endl;
+ else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
+ std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
+
+ curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
+
+ }
+ else
+ {
+ std::cout << "onPost2 Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
+ }
+ catch(std::exception& e)
+ {
+ std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
+ }
+
+}
+
+void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+{
+ try
+ {
+ if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
+ {
+ std::cout << "POST request was successful" << std::endl;
+
+ if(rep.hasAttribute("createduri"))
+ {
+ std::cout << "\tUri of the created resource: "
+ << rep.getValue<std::string>("createduri") << std::endl;
+ }
+ else
+ {
+ rep.getValue("on-off", mylight.m_on_off);
+ rep.getValue("color", mylight.m_color);
+ rep.getValue("dim", mylight.m_dim);
+
+ std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
+ std::cout << "\tcolor: " << mylight.m_color << std::endl;
+ std::cout << "\tdim: " << mylight.m_dim << std::endl;
+ }
+
+ OCRepresentation rep2;
+
+ std::cout << "Posting light representation..." << std::endl;
+
+ mylight.m_on_off = true;
+
+ rep2.setValue("on-off", mylight.m_on_off);
+
+ curResource->post(rep2, QueryParamsMap(), &onPost2);
+ }
+ else
+ {
+ std::cout << "onPost Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
+ }
+ catch(std::exception& e)
+ {
+ std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
+ }
+}
+
+// Local function to put a different state for this resource
+void postLightRepresentation(std::shared_ptr<OCResource> resource)
+{
+ if(resource)
+ {
+ OCRepresentation rep;
+
+ std::cout << "Posting light representation..."<<std::endl;
+
+ mylight.m_on_off = "false";
+
+ rep.setValue("on-off", mylight.m_on_off);
+
+ // Invoke resource's post API with rep, query map and the callback parameter
+ resource->post(rep, QueryParamsMap(), &onPost);
+ }
+}
+
+// callback handler on PUT request
+void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+{
+ try
+ {
+ if(eCode == OC_STACK_OK)
+ {
+ std::cout << "PUT request was successful" << std::endl;
+
+ /*rep.getValue("on-off", mylight.m_on_off);
+ rep.getValue("dim", mylight.m_dim);
+ rep.getValue("color", mylight.m_color);
+
+ std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
+ std::cout << "\tcolor: " << mylight.m_color << std::endl;
+ std::cout << "\tdim: " << mylight.m_dim << std::endl;*/
+
+ //postLightRepresentation(curResource);
+ }
+ else
+ {
+ std::cout << "onPut Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
+ }
+ catch(std::exception& e)
+ {
+ std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
+ }
+}
+
+// Local function to put a different state for this resource
+void putLightRepresentation(std::shared_ptr<OCResource> resource)
+{
+ if(resource)
+ {
+ OCRepresentation rep;
+
+ std::cout << "Putting light representation..."<<std::endl;
+
+ mylight.m_on_off = true;
+
+ std::cout << "Sending request to: " << resource->uri() << std::endl;
+ rep.setValue("on-off", mylight.m_on_off);
+
+ // Invoke resource's put API with rep, query map and the callback parameter
+
+ resource->put(rep, QueryParamsMap(), &onPut);
+ }
+}
+
+// Callback handler on GET request
+void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+{
+ try
+ {
+ if(eCode == OC_STACK_OK)
+ {
+ std::cout << "GET request was successful" << std::endl;
+ std::cout << "Resource URI: " << rep.getUri() << std::endl;
+
+ std::cout << "Payload: " << rep.getPayload() << std::endl;
+
+ rep.getValue("on-off", mylight.m_on_off);
+ rep.getValue("dim", mylight.m_dim);
+ rep.getValue("color", mylight.m_color);
+
+ std::cout << "\ton-off: " << mylight.m_on_off << std::endl;
+ std::cout << "\tcolor: " << mylight.m_color << std::endl;
+ std::cout << "\tdim: " << mylight.m_dim << std::endl;
+
+ putLightRepresentation(curResource);
+ }
+ else
+ {
+ std::cout << "onGET Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
+ }
+ catch(std::exception& e)
+ {
+ std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
+ }
+}
+
+// Local function to get representation of light resource
+void getLightRepresentation(std::shared_ptr<OCResource> resource)
+{
+ if(resource)
+ {
+ std::cout << "Getting Light Representation..."<<std::endl;
+ // Invoke resource's get API with the callback parameter
+
+ QueryParamsMap test;
+ std::cout << "Sending request to: " << resource->uri() << std::endl;
+ resource->get(test, &onGet);
+ }
+}
+
+// Callback to found resources
+void foundResource(std::shared_ptr<OCResource> resource)
+{
+ std::cout << "In foundResource\n";
+ std::string resourceURI;
+ std::string hostAddress;
+ try
+ {
+ {
+ std::lock_guard<std::mutex> lock(curResourceLock);
+ if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
+ {
+ std::cout << "Found resource " << resource->uniqueIdentifier() <<
+ " for the first time on server with ID: "<< resource->sid()<<std::endl;
+ discoveredResources[resource->uniqueIdentifier()] = resource;
+ }
+ else
+ {
+ std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
+ }
+
+ if(curResource)
+ {
+ std::cout << "Found another resource, ignoring"<<std::endl;
+ return;
+ }
+ }
+
+ // Do some operations with resource object.
+ if(resource)
+ {
+ std::cout<<"DISCOVERED Resource:"<<std::endl;
+ // Get the resource URI
+ resourceURI = resource->uri();
+ std::cout << "\tURI of the resource: " << resourceURI << std::endl;
+
+ // Get the resource host address
+ hostAddress = resource->host();
+ std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
+
+ // Get the resource types
+ std::cout << "\tList of resource types: " << std::endl;
+ for(auto &resourceTypes : resource->getResourceTypes())
+ {
+ std::cout << "\t\t" << resourceTypes << std::endl;
+
+ if(resourceTypes == "oic.light.control")
+ {
+ curResource = resource;
+ // Call a local function which will internally invoke get API on the resource pointer
+ getLightRepresentation(resource);
+ }
+ }
+
+ // Get the resource interfaces
+ std::cout << "\tList of resource interfaces: " << std::endl;
+ for(auto &resourceInterfaces : resource->getResourceInterfaces())
+ {
+ std::cout << "\t\t" << resourceInterfaces << std::endl;
+ }
+ }
+ else
+ {
+ // Resource is invalid
+ std::cout << "Resource is invalid" << std::endl;
+ }
+
+ }
+ catch(std::exception& e)
+ {
+ std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
+ }
+}
+
+void printUsage()
+{
+ std::cout << std::endl;
+ std::cout << "---------------------------------------------------------------------\n";
+ std::cout << "Usage : ContainerSampleClient <ObserveType>" << std::endl;
+ std::cout << " ObserveType : 1 - Observe" << std::endl;
+ std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
+ std::cout << "---------------------------------------------------------------------\n\n";
+}
+
+void checkObserverValue(int value)
+{
+ if (value == 1)
+ {
+ OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+ std::cout << "<===Setting ObserveType to Observe===>\n\n";
+ }
+ else if (value == 2)
+ {
+ OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
+ std::cout << "<===Setting ObserveType to ObserveAll===>\n\n";
+ }
+ else
+ {
+ std::cout << "<===Invalid ObserveType selected."
+ <<" Setting ObserveType to Observe===>\n\n";
+ }
+}
+
+static FILE* client_open(const char *path, const char *mode)
+{
+ return fopen("./oic_svr_db_client.json", mode);
+}
+
+int main(int argc, char* argv[]) {
+
+ std::ostringstream requestURI;
+ OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
+ try
+ {
+ printUsage();
+ if (argc == 1)
+ {
+ std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
+ }
+ else if (argc == 2)
+ {
+ checkObserverValue(std::stoi(argv[1]));
+ }
+ else
+ {
+ std::cout << "<===Invalid number of command line arguments===>\n\n";
+ return -1;
+ }
+ }
+ catch(std::exception& )
+ {
+ std::cout << "<===Invalid input arguments===>\n\n";
+ return -1;
+ }
+
+ // Create PlatformConfig object
+ PlatformConfig cfg {
+ OC::ServiceType::InProc,
+ OC::ModeType::Both,
+ "0.0.0.0",
+ 0,
+ OC::QualityOfService::LowQos,
+ &ps
+ };
+
+ OCPlatform::Configure(cfg);
+ try
+ {
+ // makes it so that all boolean values are printed as 'true/false' in this stream
+ std::cout.setf(std::ios::boolalpha);
+ // Find all resources
+ requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
+
+ OCPlatform::findResource("", requestURI.str(),
+ CT_DEFAULT, &foundResource);
+ std::cout<< "Finding Resource... " <<std::endl;
+
+ // Find resource is done twice so that we discover the original resources a second time.
+ // These resources will have the same uniqueidentifier (yet be different objects), so that
+ // we can verify/show the duplicate-checking code in foundResource(above);
+ OCPlatform::findResource("", requestURI.str(),
+ CT_DEFAULT, &foundResource);
+ std::cout<< "Finding Resource for second time..." << std::endl;
+
+ // A condition variable will free the mutex it is given, then do a non-
+ // intensive block until 'notify' is called on it. In this case, since we
+ // don't ever call cv.notify, this should be a non-processor intensive version
+ // of while(true);
+ std::mutex blocker;
+ std::condition_variable cv;
+ std::unique_lock<std::mutex> lock(blocker);
+ cv.wait(lock);
+
+ }catch(OCException& e)
+ {
+ oclog() << "Exception in main: "<<e.what();
+ }
+
+ return 0;
+}
+
+
this->bundleResource = env->NewGlobalRef(bundleResource);
bundleResourceClass = env->GetObjectClass(bundleResource);
+
attributeSetter = env->GetMethodID(bundleResourceClass, "setAttribute",
"(Ljava/lang/String;Ljava/lang/String;)V");
+
attributeGetter = env->GetMethodID(bundleResourceClass, "getAttribute",
- "(Ljava/lang/String;)Ljava/lang/String");
+ "(Ljava/lang/String;)Ljava/lang/String;");
+
}
JavaBundleResource::~JavaBundleResource()
}
-string JavaBundleResource::getAttribute(string attributeName)
+ResourceAttributes& JavaBundleResource::getAttributes(){
+ return BundleResource::getAttributes();
+}
+
+ResourceAttributes::Value JavaBundleResource::getAttribute(const std::string& attributeName)
{
JavaVM* vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
jstring returnString = (jstring) env->CallObjectMethod(bundleResource, attributeGetter,
attrName);
+
const char *js = env->GetStringUTFChars(returnString, NULL);
- std::string value(js);
+ std::string val(js);
+ ResourceAttributes::Value newVal = val;
env->ReleaseStringUTFChars(returnString, js);
- BundleResource::setAttribute(attributeName, value);
- return value;
+ BundleResource::setAttribute(attributeName, newVal.toString());
+ return BundleResource::getAttribute(attributeName);
}
-void JavaBundleResource::setAttribute(string attributeName, string value)
+void JavaBundleResource::setAttribute(std::string attributeName, ResourceAttributes::Value&& value)
{
- BundleResource::setAttribute(attributeName, value);
-
JavaVM* vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
JNIEnv * env;
}
jstring attrName = env->NewStringUTF(attributeName.c_str());
- jstring val = env->NewStringUTF(value.c_str());
+ jstring val = env->NewStringUTF(value.toString().c_str());
+
env->CallObjectMethod(bundleResource, attributeSetter, attrName, val);
+ BundleResource::setAttribute(attributeName, std::move(value));
}
class ResourceContainerTest: public Test
{
public:
+ ~ResourceContainerTest() noexcept(true){
+
+ }
+
+
+ public:
ResourceContainer *m_pResourceContainer;
protected:
class ResourceContainerBundleAPITest: public Test
{
+
+ public:
+ ~ResourceContainerBundleAPITest() noexcept(true){
+
+ }
+
public:
MockRepository mocks;
ResourceObject *m_pResourceObject;
class ResourceContainerImplTest: public Test
{
public:
+ ~ResourceContainerImplTest() noexcept(true){
+
+ }
+
+ public:
MockRepository mocks;
ResourceContainerImpl *m_pResourceContainer;
BundleInfo *m_pBundleInfo;
if env.get('LOGGING'):
env.AppendUnique(CPPDEFINES = ['TB_LOG'])
+
+
+
# Add third party libraries
lib_env = env.Clone()
container_gtest_env.AppendUnique(LIBPATH = [os.environ['JAVA_LIB']])
+container_gtest_env.AppendUnique(CCFLAGS = ['-Wnoexcept'])
+
conf = Configure(container_gtest_env)
if not conf.CheckLib('jvm'):
print '''