import java.util.List;
import java.util.Vector;
-public class BaseActivator implements BundleActivator
-{
- private String bundleId;
-
- public BaseActivator(String bundleId){
- this.bundleId = bundleId;
- }
-
- static{
- try{
- System.loadLibrary("ResContainerLib");
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
-
- public void activateBundle(){
-
+public class BaseActivator implements BundleActivator {
+ private String bundleId;
+
+ public BaseActivator(String bundleId) {
+ this.bundleId = bundleId;
+ }
+
+ static {
+ try {
+ System.loadLibrary("ResContainerLib");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
}
-
- public void deactivateBundle(){
-
- }
-
- public void registerResource(BundleResource resource){
- registerJavaResource(resource, resource.getAttributeKeys(), bundleId, resource.getURI(), resource.getResourceType(), resource.getName());
+
+ public void activateBundle() {
+
}
-
- public List<ResourceConfig> getConfiguredBundleResources(){
- int configuredResources = getNumberOfConfiguredResources(bundleId);
-
- Vector<ResourceConfig> configs = new Vector<ResourceConfig>();
-
- for(int i = 0; i < configuredResources; i++){
- String[] resourceParams = getConfiguredResourceParams(bundleId, i);
- ResourceConfig config = new ResourceConfig(resourceParams);
- configs.add(config);
-
- }
- return configs;
+
+ public void deactivateBundle() {
+
+ }
+
+ public void registerResource(BundleResource resource) {
+ registerJavaResource(resource, resource.getAttributeKeys(), bundleId,
+ resource.getURI(), resource.getResourceType(),
+ resource.getName());
+ }
+
+ public List<ResourceConfig> getConfiguredBundleResources() {
+ int configuredResources = getNumberOfConfiguredResources(bundleId);
+
+ Vector<ResourceConfig> configs = new Vector<ResourceConfig>();
+
+ for (int i = 0; i < configuredResources; i++) {
+ String[] resourceParams = getConfiguredResourceParams(bundleId, i);
+ ResourceConfig config = new ResourceConfig(resourceParams);
+ configs.add(config);
+
+ }
+ return configs;
}
-
- public void unregisterResource(BundleResource resource){
-
+
+ public void unregisterResource(BundleResource resource) {
+
}
-
- public native void registerJavaResource(BundleResource resource, String[] attributes, String bundleId, String uri, String resourceType, String name);
-
+
+ public native void registerJavaResource(BundleResource resource,
+ String[] attributes, String bundleId, String uri,
+ String resourceType, String name);
+
public native void unregisterJavaResource(BundleResource resource);
-
+
public native int getNumberOfConfiguredResources(String bundleId);
-
- public native String[] getConfiguredResourceParams(String bundleId, int resId);
-
-}
+ public native String[] getConfiguredResourceParams(String bundleId,
+ int resId);
+
+}
package org.iotivity.resourcecontainer.bundle.api;
-
import java.util.List;
-public interface BundleActivator
-{
+public interface BundleActivator {
public void activateBundle();
+
public void deactivateBundle();
+
public void registerResource(BundleResource resource);
+
public void unregisterResource(BundleResource resource);
+
public List<ResourceConfig> getConfiguredBundleResources();
}
package org.iotivity.resourcecontainer.bundle.api;
-
import java.util.HashMap;
import java.util.Set;
-public abstract class BundleResource
-{
- protected String m_name, m_uri, m_resourceType, m_address;
-
- protected HashMap<String, String> m_attributes;
-
- protected abstract void initAttributes();
- public abstract void setAttribute(String key, String value);
- public abstract String getAttribute(String key);
- public String[] getAttributeKeys(){
- Set<String> keys = m_attributes.keySet();
- return keys.toArray(new String[keys.size()]);
- }
-
- public void setURI(String uri){
- this.m_uri = uri;
- }
-
- public String getURI(){
- return m_uri;
- }
-
- public void setResourceType(String resourceType){
- this.m_resourceType = resourceType;
- }
-
- public String getResourceType(){
- return m_resourceType;
- }
-
- public void setAddress(String address){
- this.m_address = address;
- }
-
- public String getAddress(){
- return m_address;
- }
-
- public void setName(String name){
- this.m_name = name;
- }
-
- public String getName(){
- return m_name;
- }
+public abstract class BundleResource {
+ protected String m_name, m_uri, m_resourceType, m_address;
+
+ protected HashMap<String, String> m_attributes;
+
+ protected abstract void initAttributes();
+
+ public abstract void setAttribute(String key, String value);
+
+ public abstract String getAttribute(String key);
+
+ public String[] getAttributeKeys() {
+ Set<String> keys = m_attributes.keySet();
+ return keys.toArray(new String[keys.size()]);
+ }
+
+ public void setURI(String uri) {
+ this.m_uri = uri;
+ }
+
+ public String getURI() {
+ return m_uri;
+ }
+
+ public void setResourceType(String resourceType) {
+ this.m_resourceType = resourceType;
+ }
+
+ public String getResourceType() {
+ return m_resourceType;
+ }
+
+ public void setAddress(String address) {
+ this.m_address = address;
+ }
+
+ public String getAddress() {
+ return m_address;
+ }
+
+ public void setName(String name) {
+ this.m_name = name;
+ }
+
+ public String getName() {
+ return m_name;
+ }
}
package org.iotivity.resourcecontainer.bundle.api;
-
public interface ProtocolBridgeConnector {
- public void connect();
+ public void connect();
+
public void disconnect();
}
package org.iotivity.resourcecontainer.bundle.api;
-
public class ResourceConfig {
- private String m_name, m_uri, m_resourceType, m_address;
-
- public ResourceConfig(){
-
- }
-
- public ResourceConfig(String[] params){
- m_name = params[0];
- m_uri = params[1];
- m_resourceType = params[2];
- m_address = params[3];
- }
-
- public String getM_name() {
- return m_name;
- }
-
- public void setM_name(String m_name) {
- this.m_name = m_name;
- }
-
- public String getM_uri() {
- return m_uri;
- }
-
- public void setM_uri(String m_uri) {
- this.m_uri = m_uri;
- }
-
- public String getM_resourceType() {
- return m_resourceType;
- }
-
- public void setM_resourceType(String m_resourceType) {
- this.m_resourceType = m_resourceType;
- }
-
- public String getM_address() {
- return m_address;
- }
-
- public void setM_address(String m_address) {
- this.m_address = m_address;
- }
-
- @Override
- public String toString() {
- return "ResourceConfig [m_name=" + m_name + ", m_uri=" + m_uri
- + ", m_resourceType=" + m_resourceType + ", m_address="
- + m_address + "]";
- }
-
-
+ private String m_name, m_uri, m_resourceType, m_address;
+
+ public ResourceConfig() {
+
+ }
+
+ public ResourceConfig(String[] params) {
+ m_name = params[0];
+ m_uri = params[1];
+ m_resourceType = params[2];
+ m_address = params[3];
+ }
+
+ public String getM_name() {
+ return m_name;
+ }
+
+ public void setM_name(String m_name) {
+ this.m_name = m_name;
+ }
+
+ public String getM_uri() {
+ return m_uri;
+ }
+
+ public void setM_uri(String m_uri) {
+ this.m_uri = m_uri;
+ }
+
+ public String getM_resourceType() {
+ return m_resourceType;
+ }
+
+ public void setM_resourceType(String m_resourceType) {
+ this.m_resourceType = m_resourceType;
+ }
+
+ public String getM_address() {
+ return m_address;
+ }
+
+ public void setM_address(String m_address) {
+ this.m_address = m_address;
+ }
+
+ @Override
+ public String toString() {
+ return "ResourceConfig [m_name=" + m_name + ", m_uri=" + m_uri
+ + ", m_resourceType=" + m_resourceType + ", m_address="
+ + m_address + "]";
+ }
+
}
import org.iotivity.resourcecontainer.bundle.api.BaseActivator;
import org.iotivity.resourcecontainer.bundle.api.ResourceConfig;
-public class HueBundleActivator extends BaseActivator
-{
- private HueConnector connector;
-
- public HueBundleActivator(String bundleId){
- super(bundleId);
- }
-
- public void activateBundle(){
- super.activateBundle();
- connector = new HueConnector();
- List<ResourceConfig> resourceConfig = getConfiguredBundleResources();
-
- for(ResourceConfig config : resourceConfig){
-
- HueLightResource hueLightResource = new HueLightResource(connector, config.getM_name(), config.getM_uri(), config.getM_resourceType(), config.getM_address());
- registerResource(hueLightResource);
- }
+public class HueBundleActivator extends BaseActivator {
+ private HueConnector connector;
+
+ public HueBundleActivator(String bundleId) {
+ super(bundleId);
+ }
+
+ public void activateBundle() {
+ super.activateBundle();
+ connector = new HueConnector();
+ List<ResourceConfig> resourceConfig = getConfiguredBundleResources();
+
+ for (ResourceConfig config : resourceConfig) {
+
+ HueLightResource hueLightResource = new HueLightResource(connector,
+ config.getM_name(), config.getM_uri(),
+ config.getM_resourceType(), config.getM_address());
+ registerResource(hueLightResource);
+ }
+ }
+
+ public void deactivateBundle() {
+ System.out.println("Deactivate bundle called.");
}
-
- public void deactivateBundle(){
- System.out.println("Deactivate bundle called.");
- }
-
+
// test call
- public static void main(String[] args){
- HueBundleActivator activator = new HueBundleActivator("oic.hue.bundle");
+ public static void main(String[] args) {
+ HueBundleActivator activator = new HueBundleActivator("oic.hue.bundle");
}
}
import org.apache.http.util.EntityUtils;
import org.iotivity.resourcecontainer.bundle.api.ProtocolBridgeConnector;
-public class HueConnector implements ProtocolBridgeConnector{
- public void connect(){
-
- }
-
- public void disconnect(){
-
- }
-
- public void transmit(String target, String payload){
- try {
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpPost httpPost = new HttpPost(target);
- //httpPost.setHeader("content-type","application/json");
- StringEntity stringEntity = new StringEntity(payload, ContentType.create("application/json", "UTF-8"));
- httpPost.setEntity(stringEntity);
-
- CloseableHttpResponse response1;
- response1 = httpclient.execute(httpPost);
- // The underlying HTTP connection is still held by the response object
- // to allow the response content to be streamed directly from the network socket.
- // In order to ensure correct deallocation of system resources
- // the user MUST call CloseableHttpResponse#close() from a finally clause.
- // Please note that if response content is not fully consumed the underlying
- // connection cannot be safely re-used and will be shut down and discarded
- // by the connection manager.
- try {
- HttpEntity entity1 = response1.getEntity();
- // do something useful with the response body
- // and ensure it is fully consumed
- EntityUtils.consume(entity1);
- } finally {
- response1.close();
- }
-
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- public String read(String target){
- return "";
- }
+public class HueConnector implements ProtocolBridgeConnector {
+ public void connect() {
+
+ }
+
+ public void disconnect() {
+
+ }
+
+ public void transmit(String target, String payload) {
+ try {
+ CloseableHttpClient httpclient = HttpClients.createDefault();
+ HttpPost httpPost = new HttpPost(target);
+ // httpPost.setHeader("content-type","application/json");
+ StringEntity stringEntity = new StringEntity(payload,
+ ContentType.create("application/json", "UTF-8"));
+ httpPost.setEntity(stringEntity);
+
+ CloseableHttpResponse response1;
+ response1 = httpclient.execute(httpPost);
+ // The underlying HTTP connection is still held by the response
+ // object
+ // to allow the response content to be streamed directly from the
+ // network socket.
+ // In order to ensure correct deallocation of system resources
+ // the user MUST call CloseableHttpResponse#close() from a finally
+ // clause.
+ // Please note that if response content is not fully consumed the
+ // underlying
+ // connection cannot be safely re-used and will be shut down and
+ // discarded
+ // by the connection manager.
+ try {
+ HttpEntity entity1 = response1.getEntity();
+ // do something useful with the response body
+ // and ensure it is fully consumed
+ EntityUtils.consume(entity1);
+ } finally {
+ response1.close();
+ }
+
+ } catch (ClientProtocolException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public String read(String target) {
+ return "";
+ }
}
/**
* This class maps a Philips Hue light to OIC light resource
+ *
* @author iotivity
*/
-public class HueLightResource extends BundleResource
-{
- private HueConnector m_hueConnector;
-
- public HueLightResource(){
- initAttributes();
- m_resourceType = "oic.light.control";
- }
-
- public HueLightResource(HueConnector hueConnector, String name, String uri, String resourceType, String address){
- this();
- this.m_hueConnector = hueConnector;
- m_name = name;
- m_uri = uri;
- m_resourceType = resourceType;
- m_address = address;
- }
-
- protected void initAttributes(){
- m_attributes = new HashMap<String, String>();
- m_attributes.put("on-off", "true");
- m_attributes.put("color", "0");
- m_attributes.put("dim", "0");
- }
-
- public void setAttribute(String key, String value){
- System.out.println("Set attribute called - key: " + key + ", value: " + value + " transmitting now.");
-
- if("on-off".equals(value)){
- m_hueConnector.transmit(m_address + "/state", "{\"on\":" + value + "}");
- }
+public class HueLightResource extends BundleResource {
+ private HueConnector m_hueConnector;
- if("dim".equals(value)){
- m_hueConnector.transmit(m_address + "/state", "{\"bri\":" + value + "}");
- }
+ public HueLightResource() {
+ initAttributes();
+ m_resourceType = "oic.light.control";
+ }
- if("color".equals(value)){
- m_hueConnector.transmit(m_address + "/state", "{\"hue\":" + value + "}");
- }
- m_attributes.put(key, value);
- }
-
- 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 "";
- }
+ public HueLightResource(HueConnector hueConnector, String name, String uri,
+ String resourceType, String address) {
+ this();
+ this.m_hueConnector = hueConnector;
+ m_name = name;
+ m_uri = uri;
+ m_resourceType = resourceType;
+ m_address = address;
+ }
+
+ protected void initAttributes() {
+ m_attributes = new HashMap<String, String>();
+ m_attributes.put("on-off", "true");
+ m_attributes.put("color", "0");
+ m_attributes.put("dim", "0");
+ }
+
+ public void setAttribute(String key, String value) {
+ System.out.println("Set attribute called - key: " + key + ", value: "
+ + value + " transmitting now.");
+
+ if ("on-off".equals(value)) {
+ m_hueConnector.transmit(m_address + "/state", "{\"on\":" + value
+ + "}");
+ }
+
+ if ("dim".equals(value)) {
+ m_hueConnector.transmit(m_address + "/state", "{\"bri\":" + value
+ + "}");
+ }
+
+ if ("color".equals(value)) {
+ m_hueConnector.transmit(m_address + "/state", "{\"hue\":" + value
+ + "}");
+ }
+ m_attributes.put(key, value);
+ }
+
+ 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 "";
+ }
+
+ @Override
+ public String toString() {
+ return "HueLightResource [m_hueConnector=" + m_hueConnector
+ + ", m_name=" + m_name + ", m_uri=" + m_uri
+ + ", m_resourceType=" + m_resourceType + ", m_address="
+ + m_address + ", m_attributes=" + m_attributes + "]";
+ }
- @Override
- public String toString() {
- return "HueLightResource [m_hueConnector=" + m_hueConnector
- + ", m_name=" + m_name + ", m_uri=" + m_uri
- + ", m_resourceType=" + m_resourceType + ", m_address="
- + m_address + ", m_attributes=" + m_attributes + "]";
- }
-
-
}