Skip to main content
Jump to: navigation, search

Difference between revisions of "OM2M/one/Developer"

< OM2M‎ | one
(First draft)
 
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''Before doing this tutorial you have to''' [https://wiki.eclipse.org/OM2M/Clone clone and build OM2M using Eclipse]
+
'''Before doing this tutorial you have to''' [https://wiki.eclipse.org/OM2M/one/Clone clone and build OM2M using Eclipse]
  
 
__TOC__
 
__TOC__
Line 27: Line 27:
 
*Open the build.properties file and update the src attributes as depicted in the following figure.  
 
*Open the build.properties file and update the src attributes as depicted in the following figure.  
 
[[File:Om2m_ipe_configure.png|thumb|center|600px|build-properties build]]
 
[[File:Om2m_ipe_configure.png|thumb|center|600px|build-properties build]]
 +
 +
'''Note:''' At the end of the tutorial, you may face this error: '''gogo: BundleException: Error loading bundle activator.''' when starting the IPE. If this is the case, replacing ''src/main/java'' by the default ''src/'' might do the trick.
  
 
*Select the created plug-in "org.eclipse.om2m.sample.ipe" → right click → configure → convert to maven project.  
 
*Select the created plug-in "org.eclipse.om2m.sample.ipe" → right click → configure → convert to maven project.  
Line 45: Line 47:
 
** Go to the ''modules'' tab and press ''Add'' button.
 
** Go to the ''modules'' tab and press ''Add'' button.
 
** Select the org.eclipse.om2m.sample.ipe plug-in and press ''ok''.
 
** Select the org.eclipse.om2m.sample.ipe plug-in and press ''ok''.
** Build the om2m package and its sub-projects. To do this, select the "om2m.org.eclipse" package → right click → Run as → maven install.  
+
** Build the om2m package and its sub-projects. To do this, select the "org.eclipse.om2m" project → right click → Run as → maven install.  
 
** Check the org.eclipse.om2m.sample.ipe was successfully built. At the buid end, we should get this result.  
 
** Check the org.eclipse.om2m.sample.ipe was successfully built. At the buid end, we should get this result.  
 
[[File:Om2m_ipe_build.png|thumb|center|600px|om2m build]]
 
[[File:Om2m_ipe_build.png|thumb|center|600px|om2m build]]
Line 61: Line 63:
 
== Add the plugin to the OM2M product(s) ==
 
== Add the plugin to the OM2M product(s) ==
  
*The final step consists of adding the created plug-in to one of om2m platform products, i.e the ''gscl'' or the ''nscl'' executable. In the following, we choose to add the org.eclipse.om2m.sample.ipe plug-in to the gscl product:
+
*The final step consists of adding the created plug-in to one of om2m platform products, i.e the ''mn-cse'' or the ''in-cse'' executable. In the following, we choose to add the org.eclipse.om2m.sample.ipe plug-in to the mn-cse product:
* select the org.eclipse.om2m.site.gscl package.  
+
* Select the org.eclipse.om2m.site.mn-cse package.  
* open the om2m.product file.  
+
* Open the om2m.product file.
* Press Add button and Type org.eclipse.om2m.sample.ipe
+
* Go in the "Contents" tab.
* Click on ok button and save.  
+
* Press "Add" button and type: org.eclipse.om2m.sample.ipe
* Build the om2m package and its sub-projects: select the "om2m.org.eclipse" package -> right click -> Run as -> maven install.
+
* Click on "Ok" button and save.  
* To check that the "org.eclipse.om2m.sample.ipe" was successfully added to the gscl product, run the gscl and verify in the console if the "org.eclipse.om2m.sample.ipe" is displayed.
+
* Build the om2m package and its sub-projects: select the "org.eclipse.om2m" package -> right click -> Run as -> maven install.
[[File:Om2m_ipe_run.png|thumb|center|600px|gscl product]]
+
* To check that the "org.eclipse.om2m.sample.ipe" was successfully added to the mn-cse product, run the mn-cse and verify in the console if the "org.eclipse.om2m.sample.ipe" is displayed.
 +
[[File:Om2m_ipe_run.png|thumb|center|600px|mn-cse product]]
 +
 
 
== Add required dependencies ==
 
== Add required dependencies ==
 
=== Add plugin dependencies ===
 
=== Add plugin dependencies ===
Line 85: Line 89:
 
[[File:jars.png|thumb|center|800px|plugin specific jar dependencies]]
 
[[File:jars.png|thumb|center|800px|plugin specific jar dependencies]]
  
= Develop an Interworking Proxy Unit (IPE) plugin =
+
= Develop an Interworking Proxy application Entity (IPE) plugin =
 
== IPE Activator ==
 
== IPE Activator ==
  
 
* The Activator class contains the implementation of the start() and stop() method used to activate and deactivate the IPE plugin.  
 
* The Activator class contains the implementation of the start() and stop() method used to activate and deactivate the IPE plugin.  
* In the start() method, the Activator registers the IPE Controller service on the registry to make it available for the CORE plugin. Then, it starts tracking the CORE SCL service to use it once it the CORE plugin is activated.  
+
* In the start() method, the Activator registers the IPE Controller service on the registry to make it available for the CORE plugin. Then, it starts tracking the CORE CSE service to use it once the CORE plugin is activated.  
* In the Stop() method, the Activator can execute some process to make a clean before deactivation such as stopping threads, handle exceptions, etc.
+
* In the Stop() method, the Activator can execute some process to make a clean before deactivation such as stopping threads, handle exceptions, etc. In our case, we stop the Monitor that have a set of thread that must be handled.
  
 
<source lang="java">
 
<source lang="java">
package org.eclipse.om2m.sample.ipu;
+
package org.eclipse.om2m.sample.ipe;
  
import org.apache.commons.logging.Log;
+
import org.eclipse.om2m.core.service.CseService;
import org.apache.commons.logging.LogFactory;
+
import org.eclipse.om2m.interworking.service.InterworkingService;
import org.eclipse.om2m.core.service.SclService;
+
import org.eclipse.om2m.ipu.service.IpuService;
+
 
import org.osgi.framework.BundleActivator;
 
import org.osgi.framework.BundleActivator;
 
import org.osgi.framework.BundleContext;
 
import org.osgi.framework.BundleContext;
Line 105: Line 107:
  
 
public class Activator implements BundleActivator {
 
public class Activator implements BundleActivator {
private static Log logger = LogFactory.getLog(Activator.class);
+
private ServiceTracker<Object, Object> sclServiceTracker;
+
private static BundleContext context;
 
+
        // Activate the plugin
+
private Monitor monitor;
public void start(BundleContext context) throws Exception {
+
logger.info("IPU started");
+
static BundleContext getContext() {
               
+
return context;
                // Register the IPU Controller service
+
}
logger.info("Register IpuService..");
+
context.registerService(IpuService.class.getName(), new Controller(), null);
+
public void start(BundleContext bundleContext) throws Exception {
logger.info("IpuService is registered.");
+
Activator.context = bundleContext;
 
+
System.out.println("Starting Sample Ipe");
                // Track the CORE SCL service
+
new ServiceTracker<Object, Object>(bundleContext, CseService.class.getName(), null){
sclServiceTracker = new ServiceTracker<Object, Object>(context,
+
SclService.class.getName(), null) {
+
@Override
public void removedService(ServiceReference<Object> reference, Object service) {
+
logger.info("SclService removed");
+
}
+
 
+
 
public Object addingService(ServiceReference<Object> reference) {
 
public Object addingService(ServiceReference<Object> reference) {
logger.info("SclService discovered");
+
final CseService cse = (CseService) this.context.getService(reference);
SclService sclService = (SclService) this.context.getService(reference);
+
if(cse != null){
final Monitor IpuMonitor = new Monitor(sclService);
+
RequestSender.CSE = cse;
new Thread() {
+
new Thread(){
public void run() {
+
public void run() {
try {
+
if (monitor == null){
IpuMonitor.start();
+
monitor = new Monitor(cse);
} catch (Exception e) {
+
monitor.start();
logger.error("IpuMonitor error", e);
+
}
}
+
};
}
+
}.start();
}.start();
+
}
return sclService;
+
return cse;
 
}
 
}
};
+
sclServiceTracker.open();
+
public void removedService(org.osgi.framework.ServiceReference<Object> reference, Object service) {
 +
if(monitor != null){
 +
monitor.stop();
 +
monitor = null;
 +
}
 +
};
 +
 +
}.open();
 +
 +
bundleContext.registerService(InterworkingService.class.getName(), new Controller(), null);
 +
 
}
 
}
       
+
        // Deactivate the plugin
+
public void stop(BundleContext bundleContext) throws Exception {
public void stop(BundleContext context) throws Exception {
+
Activator.context = null;
logger.info("IPU stopped");
+
System.out.println("Stopping Sample Ipe");
 +
if(monitor != null){
 +
monitor.stop();
 +
monitor = null;
 +
}
 
}
 
}
 +
 
}
 
}
 
 
</source>
 
</source>
 
<hr>
 
<hr>
 +
 
== IPE Monitor ==
 
== IPE Monitor ==
  
* The Monitor class creates one sensor application called "MY_SENSOR", and one actuator application called "MY_ACTUATOR". It creates two containers for each application: "DESCRIPTOR" container to store the description and "DATA" container to store the measurements. For each container the right oBIX XML payload is created as contentInstance.
+
* The Monitor class creates one sensor AE called "MY_SENSOR", and one actuator AE called "MY_ACTUATOR". It creates two containers for each applications: "DESCRIPTOR" container to store the description and "DATA" container to store the measurements. For each container the right oBIX XML payload is created as contentInstance.
  
 
* The Monitor starts two threads to listen to each application data as well. Once a new data is detected, the Monitor creates a new contentInstance including the measurement representation.
 
* The Monitor starts two threads to listen to each application data as well. Once a new data is detected, the Monitor creates a new contentInstance including the measurement representation.
 +
 +
* Another method is available to stop the monitor and linked threads.
  
 
<source lang="java">
 
<source lang="java">
package org.eclipse.om2m.sample.ipu;
+
package org.eclipse.om2m.sample.ipe;
  
import org.eclipse.om2m.commons.resource.*;
+
import java.math.BigInteger;
import org.eclipse.om2m.commons.rest.*;
+
 
import org.eclipse.om2m.core.service.SclService;
+
import org.eclipse.om2m.commons.constants.Constants;
 +
import org.eclipse.om2m.commons.constants.MimeMediaType;
 +
import org.eclipse.om2m.commons.constants.ResponseStatusCode;
 +
import org.eclipse.om2m.commons.resource.AE;
 +
import org.eclipse.om2m.commons.resource.Container;
 +
import org.eclipse.om2m.commons.resource.ContentInstance;
 +
import org.eclipse.om2m.commons.resource.ResponsePrimitive;
 +
import org.eclipse.om2m.core.service.CseService;
  
 
public class Monitor {
 
public class Monitor {
static SclService core;
+
 
static String sclId = System.getProperty("org.eclipse.om2m.sclBaseId", "");
+
static CseService CSE;
static String reqEntity = System.getProperty("org.eclipse.om2m.adminRequestingEntity", "");
+
static String CSE_ID = Constants.CSE_ID;
static String ipuId = "sample";
+
static String CSE_NAME = Constants.CSE_NAME;
 +
static String REQUEST_ENTITY = Constants.ADMIN_REQUESTING_ENTITY;
 +
static String ipeId = "sample";
 
static String actuatorId = "MY_ACTUATOR";
 
static String actuatorId = "MY_ACTUATOR";
 
static String sensorId = "MY_SENSOR";
 
static String sensorId = "MY_SENSOR";
 
static boolean actuatorValue = false;
 
static boolean actuatorValue = false;
 
static int sensorValue = 0;
 
static int sensorValue = 0;
       
+
static String DESCRIPTOR = "DESCRIPTOR";
public Monitor(SclService sclService) {
+
static String DATA = "DATA";
core = sclService;
+
 +
private SensorListener sensorListener;
 +
private ActuatorListener actuatorListener;
 +
 +
public Monitor(CseService cseService){
 +
CSE = cseService;
 
}
 
}
 
+
public void start() {
+
public void start(){
                // Create required resources for the Sensor
+
// Create sensor resources
 
createSensorResources();
 
createSensorResources();
                // Listen for the Sensor data
+
// Listen for the sensor data
 
listenToSensor();
 
listenToSensor();
 
+
                // Create required resources for the Actuator
+
// Create required resources for the Actuator
createActuatorResources();
+
createActuatorResource();
                // Listen for the Actuator data
+
// Listen for the Actuator data
 
listenToActuator();
 
listenToActuator();
 
}
 
}
 
+
public void createSensorResources() {
+
public void stop(){
 +
if(sensorListener != null && sensorListener.isAlive()){
 +
sensorListener.stopThread();
 +
}
 +
if(actuatorListener != null && actuatorListener.isAlive()){
 +
actuatorListener.stopThread();
 +
}
 +
}
 +
 +
public void createSensorResources(){
 
String targetId, content;
 
String targetId, content;
               
+
                // Create the MY_SENSOR application
+
targetId = "/" + CSE_ID + "/" + CSE_NAME;
targetId = sclId + "/applications";
+
AE ae = new AE();
ResponseConfirm response = core
+
ae.setRequestReachability(true);
.doRequest(new RequestIndication("CREATE", targetId, reqEntity,
+
ae.setAppID(ipeId);
new Application(sensorId, ipuId)));
+
ae.getPointOfAccess().add(ipeId);
 
+
ResponsePrimitive response = RequestSender.createAE(ae, sensorId);
if (response.getStatusCode().equals(StatusCode.STATUS_CREATED)) {
+
                        // Create the "DESCRIPTOR" container
+
if(response.getResponseStatusCode().equals(ResponseStatusCode.CREATED)){
targetId = sclId + "/applications/" + sensorId + "/containers";
+
targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + sensorId;
core.doRequest(new RequestIndication("CREATE", targetId, reqEntity,
+
Container cnt = new Container();
new Container("DESCRIPTOR")));
+
cnt.setMaxNrOfInstances(BigInteger.valueOf(10));
               
+
// Create the DESCRIPTOR container
                        // Create the "DATA" container
+
RequestSender.createContainer(targetId, DESCRIPTOR, cnt);
core.doRequest(new RequestIndication("CREATE", targetId, reqEntity,
+
new Container("DATA")));
+
// Create the DATA container
 
+
RequestSender.createContainer(targetId, DATA, cnt);
                        // Create the description contentInstance
+
content = Mapper.getSensorDescriptorRep(sclId, sensorId, ipuId);
+
// Create the description contentInstance
targetId = sclId + "/applications/" + sensorId
+
content = ObixUtil.getSensorDescriptorRep(sensorId, ipeId);
+ "/containers/DESCRIPTOR/contentInstances";
+
targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + sensorId + "/" + DESCRIPTOR;
core.doRequest(new RequestIndication("CREATE", targetId, reqEntity,
+
ContentInstance cin = new ContentInstance();
new ContentInstance(content.getBytes())));
+
cin.setContent(content);
                       
+
cin.setContentInfo(MimeMediaType.OBIX);
                        // Create the data contentInstance
+
RequestSender.createContentInstance(targetId, cin);
content = Mapper.getSensorDataRep(sensorValue);
+
targetId = sclId + "/applications/" + sensorId
+
+ "/containers/DATA/contentInstances";
+
core.doRequest(new RequestIndication("CREATE", targetId, reqEntity,
+
new ContentInstance(content.getBytes())));
+
 
}
 
}
 
}
 
}
 
+
public void createActuatorResources() {
+
public void createActuatorResource(){
 
String targetId, content;
 
String targetId, content;
               
+
                // Create the "MY_ACTUATOR" application
+
targetId = "/" + CSE_ID + "/" + CSE_NAME;
targetId = sclId + "/applications";
+
AE ae = new AE();
ResponseConfirm response = core.doRequest(new RequestIndication(
+
ae.setRequestReachability(true);
"CREATE", targetId, reqEntity, new Application(actuatorId,ipuId)));
+
ae.setAppID(ipeId);
 
+
ae.getPointOfAccess().add(ipeId);
if (response.getStatusCode().equals(StatusCode.STATUS_CREATED)) {
+
ResponsePrimitive response = RequestSender.createAE(ae, actuatorId);
                        // Create the "DESCRIPTOR" container
+
targetId = sclId + "/applications/" + actuatorId + "/containers";
+
if(response.getResponseStatusCode().equals(ResponseStatusCode.CREATED)){
core.doRequest(new RequestIndication("CREATE", targetId, reqEntity,
+
targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + actuatorId;
new Container("DESCRIPTOR")));
+
Container cnt = new Container();
 
+
cnt.setMaxNrOfInstances(BigInteger.valueOf(10));
                        // Create the "DATA" container
+
// Create the DESCRIPTOR container
core.doRequest(new RequestIndication("CREATE", targetId, reqEntity,
+
RequestSender.createContainer(targetId, DESCRIPTOR, cnt);
new Container("DATA")));
+
 
+
// Create the DATA container
                        // Create the description contentInstance
+
RequestSender.createContainer(targetId, DATA, cnt);
content = Mapper.getActutaorDescriptorRep(sclId, actuatorId, ipuId);
+
targetId = sclId + "/applications/" + actuatorId
+
// Create the description contentInstance
+ "/containers/DESCRIPTOR/contentInstances";
+
content = ObixUtil.getActuatorDescriptorRep(actuatorId);
core.doRequest(new RequestIndication("CREATE", targetId, reqEntity,
+
targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + actuatorId + "/" + DESCRIPTOR;
content));
+
ContentInstance cin = new ContentInstance();
 
+
cin.setContent(content);
                        // Create the data contentInstance
+
cin.setContentInfo(MimeMediaType.OBIX);
content = Mapper.getActuatorDataRep(actuatorValue);
+
RequestSender.createContentInstance(targetId, cin);
targetId = sclId + "/applications/" + actuatorId
+
+ "/containers/DATA/contentInstances";
+
core.doRequest(new RequestIndication("CREATE", targetId, reqEntity,
+
content));
+
 
}
 
}
 
}
 
}
 +
 +
public void listenToSensor(){
 +
sensorListener = new SensorListener();
 +
sensorListener.start();
 +
}
 +
 +
public void listenToActuator(){
 +
actuatorListener = new ActuatorListener();
 +
actuatorListener.start();
 +
}
 +
  
public void listenToSensor() {
+
private static class SensorListener extends Thread{
new Thread() {
+
public void run() {
+
private boolean running = true;
while (true) {
+
                                        // Simualte a random measurement of the sensor
+
@Override
sensorValue = 10 + (int) (Math.random() * 100);
+
public void run() {
 
+
while(running){
                                        // Create a data contentInstance
+
// Simulate a random measurement of the sensor
String content = Mapper.getSensorDataRep(sensorValue);
+
sensorValue = 10 + (int) (Math.random() * 100);
String targetID = sclId + "/applications/" + sensorId
+
+ "/containers/DATA/contentInstances";
+
// Create the data contentInstance
core.doRequest(new RequestIndication("CREATE", targetID,reqEntity,
+
String content = ObixUtil.getSensorDataRep(sensorValue);
content));
+
String targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + sensorId + "/" + DATA;
 
+
ContentInstance cin = new ContentInstance();
                                        // Wait for 2 seconds then loop
+
cin.setContent(content);
try {
+
cin.setContentInfo(MimeMediaType.OBIX);
Thread.sleep(2000);
+
RequestSender.createContentInstance(targetId, cin);
} catch (InterruptedException e) {
+
e.printStackTrace();
+
try {
}
+
Thread.sleep(2000);
 +
} catch (InterruptedException e){
 +
e.printStackTrace();
 
}
 
}
 
}
 
}
}.start();
+
 +
}
 +
 +
public void stopThread(){
 +
running = false;
 +
}
 +
 
}
 
}
 
+
public void listenToActuator() {
+
private static class ActuatorListener extends Thread{
new Thread() {
+
public void run() {
+
private boolean running = true;
 
+
private boolean memorizedActuatorValue = false;
boolean memorizedActuatorValue = false;
+
while (true) {
+
@Override
                                        // If the Actuator state has changed
+
public void run() {
if (memorizedActuatorValue != actuatorValue) {
+
while(running){
                                                // Memorize the new Actuator state
+
// If the actuator state has changed
memorizedActuatorValue = actuatorValue;
+
if(memorizedActuatorValue != actuatorValue){
 
+
// Memorize the new actuator state
                                                // Create a data contentInstance
+
memorizedActuatorValue = actuatorValue;
String content = Mapper.getActuatorDataRep(actuatorValue);
+
String targetID = sclId + "/applications/" + actuatorId
+
// Create a data contentInstance
+ "/containers/DATA/contentInstances";
+
String content = ObixUtil.getActuatorDataRep(memorizedActuatorValue);
core.doRequest(new RequestIndication("CREATE",targetID, reqEntity,
+
String targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + actuatorId + "/" + DATA;
content));
+
ContentInstance cin = new ContentInstance();
}
+
cin.setContent(content);
                         
+
cin.setContentInfo(MimeMediaType.OBIX);
                                        // Wait for 2 seconds then loop
+
RequestSender.createContentInstance(targetId, cin);
try {
+
}
Thread.sleep(2000);
+
} catch (InterruptedException e) {
+
// Wait for 2 seconds
e.printStackTrace();
+
try{
}
+
Thread.sleep(2000);
 +
} catch (InterruptedException e){
 +
e.printStackTrace();
 
}
 
}
 
}
 
}
}.start();
+
}
 +
 +
public void stopThread(){
 +
running = false;
 +
}
 +
 
}
 
}
 +
 
}
 
}
 
</source>
 
</source>
 
<hr>
 
<hr>
== IPE Mapper ==
+
== IPE oBIX Util ==
 
* Convert the description and the measured data of the device to oBIX XML representation.
 
* Convert the description and the measured data of the device to oBIX XML representation.
  
 
<source lang="java">
 
<source lang="java">
package org.eclipse.om2m.sample.ipu;
+
package org.eclipse.om2m.sample.ipe;
import org.eclipse.om2m.commons.obix.*;
+
 
 +
import org.eclipse.om2m.commons.constants.Constants;
 +
import org.eclipse.om2m.commons.obix.Bool;
 +
import org.eclipse.om2m.commons.obix.Contract;
 +
import org.eclipse.om2m.commons.obix.Int;
 +
import org.eclipse.om2m.commons.obix.Obj;
 +
import org.eclipse.om2m.commons.obix.Op;
 +
import org.eclipse.om2m.commons.obix.Uri;
 
import org.eclipse.om2m.commons.obix.io.ObixEncoder;
 
import org.eclipse.om2m.commons.obix.io.ObixEncoder;
  
public class Mapper {
+
public class ObixUtil {
  
public static String getSensorDescriptorRep(String sclId, String appId, String ipuId) {
+
public static String getSensorDescriptorRep(String appId, String ipeId) {
 +
String prefix = "/" + Constants.CSE_ID + "/" + Constants.CSE_NAME + "/" + appId;
 
Obj obj = new Obj();
 
Obj obj = new Obj();
  
 
Op opGet = new Op();
 
Op opGet = new Op();
 
opGet.setName("GET");
 
opGet.setName("GET");
opGet.setHref(new Uri(sclId + "/applications/" + appId
+
opGet.setHref(new Uri(prefix + "/DATA/la"));
+ "/containers/DATA/contentInstances/latest/content"));
+
 
opGet.setIs(new Contract("retrieve"));
 
opGet.setIs(new Contract("retrieve"));
 
obj.add(opGet);
 
obj.add(opGet);
Line 338: Line 401:
 
Op opGetDirect = new Op();
 
Op opGetDirect = new Op();
 
opGetDirect.setName("GET(Direct)");
 
opGetDirect.setName("GET(Direct)");
opGetDirect.setHref(new Uri(sclId + "/applications/" + appId + "/" + ipuId));
+
opGetDirect.setHref(new Uri(prefix + "?op=get"));
opGetDirect.setIs(new Contract("retrieve"));
+
opGetDirect.setIs(new Contract("execute"));
 
obj.add(opGetDirect);
 
obj.add(opGetDirect);
  
Line 345: Line 408:
 
}
 
}
  
public static String getActutaorDescriptorRep(String sclId, String appId, String ipuId) {
+
public static String getActuatorDescriptorRep(String appId) {
 +
String prefix = "/" + Constants.CSE_ID + "/" + Constants.CSE_NAME + "/" + appId;
 
Obj obj = new Obj();
 
Obj obj = new Obj();
  
 
Op opGet = new Op();
 
Op opGet = new Op();
 
opGet.setName("GET");
 
opGet.setName("GET");
opGet.setHref(new Uri(sclId + "/applications/" + appId
+
opGet.setHref(new Uri(prefix + "/DATA/la"));
+ "/containers/DATA/contentInstances/latest/content"));
+
 
opGet.setIs(new Contract("retrieve"));
 
opGet.setIs(new Contract("retrieve"));
 
obj.add(opGet);
 
obj.add(opGet);
Line 357: Line 420:
 
Op opGetDirect = new Op();
 
Op opGetDirect = new Op();
 
opGetDirect.setName("GET(Direct)");
 
opGetDirect.setName("GET(Direct)");
opGetDirect.setHref(new Uri(sclId + "/applications/" + appId + "/" + ipuId));
+
opGetDirect.setHref(new Uri(prefix + "?op=get"));
opGetDirect.setIs(new Contract("retrieve"));
+
opGetDirect.setIs(new Contract("execute"));
 
obj.add(opGetDirect);
 
obj.add(opGetDirect);
  
 
Op opON = new Op();
 
Op opON = new Op();
 
opON.setName("ON");
 
opON.setName("ON");
opON.setHref(new Uri(sclId + "/applications/" + appId + "/" + ipuId + "/true"));
+
opON.setHref(new Uri(prefix + "?op=true"));
 
opON.setIs(new Contract("execute"));
 
opON.setIs(new Contract("execute"));
 
obj.add(opON);
 
obj.add(opON);
Line 369: Line 432:
 
Op opOFF = new Op();
 
Op opOFF = new Op();
 
opOFF.setName("OFF");
 
opOFF.setName("OFF");
opOFF.setHref(new Uri(sclId + "/applications/" + appId + "/" + ipuId + "/false"));
+
opOFF.setHref(new Uri(prefix + "?op=false"));
 
opOFF.setIs(new Contract("execute"));
 
opOFF.setIs(new Contract("execute"));
 
obj.add(opOFF);
 
obj.add(opOFF);
Line 387: Line 450:
 
return ObixEncoder.toString(obj);
 
return ObixEncoder.toString(obj);
 
}
 
}
 +
 
}
 
}
 
</source>
 
</source>
Line 395: Line 459:
  
 
<source lang="java">
 
<source lang="java">
package org.eclipse.om2m.sample.ipu;
+
package org.eclipse.om2m.sample.ipe;
  
import org.eclipse.om2m.commons.resource.StatusCode;
+
import org.eclipse.om2m.commons.constants.ResponseStatusCode;
import org.eclipse.om2m.commons.rest.*;
+
import org.eclipse.om2m.commons.resource.RequestPrimitive;
import org.eclipse.om2m.ipu.service.IpuService;
+
import org.eclipse.om2m.commons.resource.ResponsePrimitive;
 +
import org.eclipse.om2m.interworking.service.InterworkingService;
  
public class Controller implements IpuService {
+
public class Controller implements InterworkingService{
  
public ResponseConfirm doExecute(RequestIndication requestIndication) {
+
@Override
String[] parts = requestIndication.getTargetID().split("/");
+
public ResponsePrimitive doExecute(RequestPrimitive request) {
String appId = parts[2];
+
String[] parts = request.getTo().split("/");
String value = parts[4];
+
String appId = parts[3];
 
+
ResponsePrimitive response = new ResponsePrimitive(request);
if (appId.equals(Monitor.actuatorId)) {
+
Monitor.actuatorValue = Boolean.parseBoolean(value);
+
if(request.getQueryStrings().containsKey("op")){
return new ResponseConfirm(StatusCode.STATUS_OK);
+
String valueOp = request.getQueryStrings().get("op").get(0);
 +
 +
switch(valueOp){
 +
case "get":
 +
if(appId.equals(Monitor.sensorId)){
 +
response.setContent(ObixUtil.getSensorDataRep(Monitor.sensorValue));
 +
response.setResponseStatusCode(ResponseStatusCode.OK);
 +
} else if (appId.equals(Monitor.actuatorId)){
 +
response.setContent(ObixUtil.getActuatorDataRep(Monitor.actuatorValue));
 +
response.setResponseStatusCode(ResponseStatusCode.OK);
 +
} else {
 +
response.setResponseStatusCode(ResponseStatusCode.BAD_REQUEST);
 +
}
 +
return response;
 +
case "true": case "false":
 +
if(appId.equals(Monitor.actuatorId)){
 +
Monitor.actuatorValue = Boolean.parseBoolean(valueOp);
 +
response.setResponseStatusCode(ResponseStatusCode.OK);
 +
} else {
 +
response.setResponseStatusCode(ResponseStatusCode.BAD_REQUEST);
 +
}
 +
return response;
 +
default:
 +
response.setResponseStatusCode(ResponseStatusCode.BAD_REQUEST);
 +
}
 
} else {
 
} else {
return new ResponseConfirm(StatusCode.STATUS_NOT_FOUND, appId + " not found");
+
response.setResponseStatusCode(ResponseStatusCode.BAD_REQUEST);
 
}
 
}
 +
return response;
 
}
 
}
  
public ResponseConfirm doRetrieve(RequestIndication requestIndication) {
+
@Override
String[] parts = requestIndication.getTargetID().split("/");
+
public String getAPOCPath() {
String appId = parts[2];
+
return Monitor.ipeId;
String content;
+
}
 +
 +
}
 +
</source>
  
if (appId.equals(Monitor.sensorId)) {
 
content = Mapper.getSensorDataRep(Monitor.sensorValue);
 
return new ResponseConfirm(StatusCode.STATUS_OK, content);
 
} else if (appId.equals(Monitor.actuatorId)) {
 
content = Mapper.getActuatorDataRep(Monitor.actuatorValue);
 
return new ResponseConfirm(StatusCode.STATUS_OK, content);
 
} else {
 
return new ResponseConfirm(StatusCode.STATUS_NOT_FOUND, appId + " not found");
 
}
 
  
}
+
== IPE Util: RequestSender ==
  
public ResponseConfirm doUpdate(RequestIndication requestIndication) {
+
* This class simplify the interaction with the CseService providing the response of specific requests directly. You can take inspiration of this class if you want to create your own request utility/factory.
return new ResponseConfirm(StatusCode.STATUS_NOT_IMPLEMENTED,
+
requestIndication.getMethod() + " not Implemented");
+
}
+
  
public ResponseConfirm doDelete(RequestIndication requestIndication) {
+
<source lang="java">
return new ResponseConfirm(StatusCode.STATUS_NOT_IMPLEMENTED,
+
package org.eclipse.om2m.sample.ipe;
requestIndication.getMethod() + " not Implemented");
+
}
+
  
public ResponseConfirm doCreate(RequestIndication requestIndication) {
+
import java.math.BigInteger;
return new ResponseConfirm(StatusCode.STATUS_NOT_IMPLEMENTED,
+
 
requestIndication.getMethod() + " not Implemented");
+
import org.eclipse.om2m.commons.constants.Constants;
 +
import org.eclipse.om2m.commons.constants.MimeMediaType;
 +
import org.eclipse.om2m.commons.constants.Operation;
 +
import org.eclipse.om2m.commons.constants.ResourceType;
 +
import org.eclipse.om2m.commons.resource.AE;
 +
import org.eclipse.om2m.commons.resource.Container;
 +
import org.eclipse.om2m.commons.resource.ContentInstance;
 +
import org.eclipse.om2m.commons.resource.RequestPrimitive;
 +
import org.eclipse.om2m.commons.resource.Resource;
 +
import org.eclipse.om2m.commons.resource.ResponsePrimitive;
 +
import org.eclipse.om2m.core.service.CseService;
 +
 
 +
public class RequestSender {
 +
 +
public static CseService CSE;
 +
 +
/**
 +
* Private constructor to avoid creation of this object
 +
*/
 +
private RequestSender(){}
 +
 +
public static ResponsePrimitive createResource(String targetId, String name, Resource resource, int resourceType){
 +
RequestPrimitive request = new RequestPrimitive();
 +
request.setFrom(Constants.ADMIN_REQUESTING_ENTITY);
 +
request.setTargetId(targetId);
 +
request.setResourceType(BigInteger.valueOf(resourceType));
 +
request.setRequestContentType(MimeMediaType.OBJ);
 +
request.setReturnContentType(MimeMediaType.OBJ);
 +
request.setContent(resource);
 +
request.setName(name);
 +
request.setOperation(Operation.CREATE);
 +
return CSE.doRequest(request);
 +
}
 +
 +
public static ResponsePrimitive createAE(AE resource, String name){
 +
return createResource("/" + Constants.CSE_ID, name, resource, ResourceType.AE);
 +
}
 +
 +
public static ResponsePrimitive createContainer(String targetId, String name, Container resource){
 +
return createResource(targetId, name, resource, ResourceType.CONTAINER);
 +
}
 +
 +
public static ResponsePrimitive createContentInstance(String targetId, String name, ContentInstance resource){
 +
return createResource(targetId, name, resource, ResourceType.CONTENT_INSTANCE);
 +
}
 +
 +
public static ResponsePrimitive createContentInstance(String targetId, ContentInstance resource){
 +
return createContentInstance(targetId, null, resource);
 
}
 
}
  
public String getAPOCPath(){
+
public static ResponsePrimitive getRequest(String targetId){
return Monitor.ipuId;
+
RequestPrimitive request = new RequestPrimitive();
 +
request.setFrom(Constants.ADMIN_REQUESTING_ENTITY);
 +
request.setTargetId(targetId);
 +
request.setReturnContentType(MimeMediaType.OBJ);
 +
request.setOperation(Operation.RETRIEVE);
 +
request.setRequestContentType(MimeMediaType.OBJ);
 +
return CSE.doRequest(request);
 
}
 
}
 +
 
}
 
}
 
</source>
 
</source>

Latest revision as of 12:13, 14 September 2017

Before doing this tutorial you have to clone and build OM2M using Eclipse

Add a new plug-in to OM2M

Clone and import OM2M

  • Clone OM2M using the Eclipse IDE.
  • Import all nested projects into your workspace.

Create a new plug-in project

  • Create a new plug-in project called org.eclipse.om2m.sample.ipe via File → New → Other → Plug-in Project.
  • Enter the data as depicted in the following screenshots.
create a plug-in project: step 1
create a plug-in project: step 2
  • Uncheck the Create a plug-in using one of the templates checkbook and press the Finish button.
create a plug-in project: step 3
  • As result the following project is created.
sample ipe plug-in

Convert the plugin into maven project

  • Open the build.properties file and update the src attributes as depicted in the following figure.
build-properties build

Note: At the end of the tutorial, you may face this error: gogo: BundleException: Error loading bundle activator. when starting the IPE. If this is the case, replacing src/main/java by the default src/ might do the trick.

  • Select the created plug-in "org.eclipse.om2m.sample.ipe" → right click → configure → convert to maven project.
  • Enter the data as illustrated in the following and press the Finish button.
maven conversion: POM edition
  • Once the plug-in is converted, open the pom.xml file to edit the parent filed.
  • Click on "select Parent" icon and enter "org.eclipse.om2m" in the "Enter groupId, artifactId or sh1 prefix or pattern" field.
  • Select "org.om2m.eclipse" and press ok.
maven conversion: Parent edition
  • We end this step by updating the plug-in project. For this, select the "org.eclipse.om2m.sample.ipe" project → maven → update project.

Add the plugin as a maven module to the OM2M parent project

  • In this part, we will add the created plug-in as a module to the om2m platform:
    • Open the pom file of the org.eclipse.om2m package.
    • Go to the modules tab and press Add button.
    • Select the org.eclipse.om2m.sample.ipe plug-in and press ok.
    • Build the om2m package and its sub-projects. To do this, select the "org.eclipse.om2m" project → right click → Run as → maven install.
    • Check the org.eclipse.om2m.sample.ipe was successfully built. At the buid end, we should get this result.
om2m build
  • Remark: To keep the same display pattern for all the platform' plugins, you can add description and name tags to the org.eclipse.om2m.sample.ipe' pom.xml file.
<project>
......
  <name>org.eclipse.om2m :: sample ipe</name>
  <description>org.eclipse.om2m :: sample ipe</description>
.....
</project>

Add the plugin to the OM2M product(s)

  • The final step consists of adding the created plug-in to one of om2m platform products, i.e the mn-cse or the in-cse executable. In the following, we choose to add the org.eclipse.om2m.sample.ipe plug-in to the mn-cse product:
  • Select the org.eclipse.om2m.site.mn-cse package.
  • Open the om2m.product file.
  • Go in the "Contents" tab.
  • Press "Add" button and type: org.eclipse.om2m.sample.ipe
  • Click on "Ok" button and save.
  • Build the om2m package and its sub-projects: select the "org.eclipse.om2m" package -> right click -> Run as -> maven install.
  • To check that the "org.eclipse.om2m.sample.ipe" was successfully added to the mn-cse product, run the mn-cse and verify in the console if the "org.eclipse.om2m.sample.ipe" is displayed.
mn-cse product

Add required dependencies

Add plugin dependencies

  • Open the manifest file of the created plugin and select Dependencies tab.
  • Add the dependencies depicted in the following figure.

Important! You need to import om2m nested projects into your workspace to find them in the list below.

plugin dependencies

Add jar dependencies (Only if required)

If you want to add specific jar libraries to your plugin, you can follow these steps:

  • Create a "libs" folder on your plugin project.
  • Put required specific jar on the "libs" folder.
  • Open the manifest file of the created plugin and select "runtime" tab.
  • Go to the "classpath" tab and click on the "Add" button.
  • Select the required jar and click on "ok" button.
plugin specific jar dependencies

Develop an Interworking Proxy application Entity (IPE) plugin

IPE Activator

  • The Activator class contains the implementation of the start() and stop() method used to activate and deactivate the IPE plugin.
  • In the start() method, the Activator registers the IPE Controller service on the registry to make it available for the CORE plugin. Then, it starts tracking the CORE CSE service to use it once the CORE plugin is activated.
  • In the Stop() method, the Activator can execute some process to make a clean before deactivation such as stopping threads, handle exceptions, etc. In our case, we stop the Monitor that have a set of thread that must be handled.
package org.eclipse.om2m.sample.ipe;
 
import org.eclipse.om2m.core.service.CseService;
import org.eclipse.om2m.interworking.service.InterworkingService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
 
public class Activator implements BundleActivator {
 
	private static BundleContext context;
 
	private Monitor monitor;
 
	static BundleContext getContext() {
		return context;
	}
 
	public void start(BundleContext bundleContext) throws Exception {
		Activator.context = bundleContext;
		System.out.println("Starting Sample Ipe");
		new ServiceTracker<Object, Object>(bundleContext, CseService.class.getName(), null){
 
			@Override
			public Object addingService(ServiceReference<Object> reference) {
				final CseService cse = (CseService) this.context.getService(reference);
				if(cse != null){
					RequestSender.CSE = cse;
					new Thread(){
						public void run() {
							if (monitor == null){
								monitor = new Monitor(cse);
								monitor.start();								
							}
						};
					}.start();
				}
				return cse;	
			}
 
			public void removedService(org.osgi.framework.ServiceReference<Object> reference, Object service) {
				if(monitor != null){
					monitor.stop();
					monitor = null;
				}		
			};
 
		}.open();
 
		bundleContext.registerService(InterworkingService.class.getName(), new Controller(), null);
 
	}
 
	public void stop(BundleContext bundleContext) throws Exception {
		Activator.context = null;
		System.out.println("Stopping Sample Ipe");
		if(monitor != null){
			monitor.stop();
			monitor = null;
		}
	}
 
}

IPE Monitor

  • The Monitor class creates one sensor AE called "MY_SENSOR", and one actuator AE called "MY_ACTUATOR". It creates two containers for each applications: "DESCRIPTOR" container to store the description and "DATA" container to store the measurements. For each container the right oBIX XML payload is created as contentInstance.
  • The Monitor starts two threads to listen to each application data as well. Once a new data is detected, the Monitor creates a new contentInstance including the measurement representation.
  • Another method is available to stop the monitor and linked threads.
package org.eclipse.om2m.sample.ipe;
 
import java.math.BigInteger;
 
import org.eclipse.om2m.commons.constants.Constants;
import org.eclipse.om2m.commons.constants.MimeMediaType;
import org.eclipse.om2m.commons.constants.ResponseStatusCode;
import org.eclipse.om2m.commons.resource.AE;
import org.eclipse.om2m.commons.resource.Container;
import org.eclipse.om2m.commons.resource.ContentInstance;
import org.eclipse.om2m.commons.resource.ResponsePrimitive;
import org.eclipse.om2m.core.service.CseService;
 
public class Monitor {
 
	static CseService CSE;
	static String CSE_ID = Constants.CSE_ID;
	static String CSE_NAME = Constants.CSE_NAME;
	static String REQUEST_ENTITY = Constants.ADMIN_REQUESTING_ENTITY;
	static String ipeId = "sample";
	static String actuatorId = "MY_ACTUATOR";
	static String sensorId = "MY_SENSOR";
	static boolean actuatorValue = false;
	static int sensorValue = 0;
	static String DESCRIPTOR = "DESCRIPTOR";
	static String DATA = "DATA";
 
	private SensorListener sensorListener;
	private ActuatorListener actuatorListener;
 
	public Monitor(CseService cseService){
		CSE = cseService;
	}
 
	public void start(){
		// Create sensor resources
		createSensorResources();
		// Listen for the sensor data
		listenToSensor();
 
		// Create required resources for the Actuator
		createActuatorResource();
		// Listen for the Actuator data
		listenToActuator();
	}
 
	public void stop(){
		if(sensorListener != null && sensorListener.isAlive()){
			sensorListener.stopThread();
		}
		if(actuatorListener != null && actuatorListener.isAlive()){
			actuatorListener.stopThread();
		}
	}
 
	public void createSensorResources(){
		String targetId, content;
 
		targetId = "/" + CSE_ID + "/" + CSE_NAME;
		AE ae = new AE();
		ae.setRequestReachability(true);
		ae.setAppID(ipeId);
		ae.getPointOfAccess().add(ipeId);
		ResponsePrimitive response = RequestSender.createAE(ae, sensorId);
 
		if(response.getResponseStatusCode().equals(ResponseStatusCode.CREATED)){
			targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + sensorId;
			Container cnt = new Container();
			cnt.setMaxNrOfInstances(BigInteger.valueOf(10));
			// Create the DESCRIPTOR container
			RequestSender.createContainer(targetId, DESCRIPTOR, cnt);
 
			// Create the DATA container
			RequestSender.createContainer(targetId, DATA, cnt);
 
			// Create the description contentInstance
			content = ObixUtil.getSensorDescriptorRep(sensorId, ipeId);
			targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + sensorId + "/" + DESCRIPTOR;
			ContentInstance cin = new ContentInstance();
			cin.setContent(content);
			cin.setContentInfo(MimeMediaType.OBIX);
			RequestSender.createContentInstance(targetId, cin);
		}
	}
 
	public void createActuatorResource(){
		String targetId, content;
 
		targetId = "/" + CSE_ID + "/" + CSE_NAME;
		AE ae = new AE();
		ae.setRequestReachability(true);
		ae.setAppID(ipeId);
		ae.getPointOfAccess().add(ipeId);
		ResponsePrimitive response = RequestSender.createAE(ae, actuatorId);
 
		if(response.getResponseStatusCode().equals(ResponseStatusCode.CREATED)){
			targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + actuatorId;
			Container cnt = new Container();
			cnt.setMaxNrOfInstances(BigInteger.valueOf(10));
			// Create the DESCRIPTOR container
			RequestSender.createContainer(targetId, DESCRIPTOR, cnt);
 
			// Create the DATA container
			RequestSender.createContainer(targetId, DATA, cnt);
 
			// Create the description contentInstance
			content = ObixUtil.getActuatorDescriptorRep(actuatorId);
			targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + actuatorId + "/" + DESCRIPTOR;
			ContentInstance cin = new ContentInstance();
			cin.setContent(content);
			cin.setContentInfo(MimeMediaType.OBIX);
			RequestSender.createContentInstance(targetId, cin);
		}
	}
 
	public void listenToSensor(){
		sensorListener = new SensorListener();
		sensorListener.start();
	}
 
	public void listenToActuator(){
		actuatorListener = new ActuatorListener();
		actuatorListener.start();
	}
 
 
	private static class SensorListener extends Thread{
 
		private boolean running = true;
 
		@Override
		public void run() {
			while(running){
				// Simulate a random measurement of the sensor
				sensorValue = 10 + (int) (Math.random() * 100);
 
				// Create the data contentInstance
				String content = ObixUtil.getSensorDataRep(sensorValue);
				String targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + sensorId + "/" + DATA;
				ContentInstance cin = new ContentInstance();
				cin.setContent(content);
				cin.setContentInfo(MimeMediaType.OBIX);
				RequestSender.createContentInstance(targetId, cin);
 
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e){
					e.printStackTrace();
				}
			}
 
		}
 
		public void stopThread(){
			running = false;
		}
 
	}
 
	private static class ActuatorListener extends Thread{
 
		private boolean running = true;
		private boolean memorizedActuatorValue = false;
 
		@Override
		public void run() {
			while(running){
				// If the actuator state has changed
				if(memorizedActuatorValue != actuatorValue){
					// Memorize the new actuator state
					memorizedActuatorValue = actuatorValue;
 
					// Create a data contentInstance
					String content = ObixUtil.getActuatorDataRep(memorizedActuatorValue);
					String targetId = "/" + CSE_ID + "/" + CSE_NAME + "/" + actuatorId + "/" + DATA;
					ContentInstance cin = new ContentInstance();
					cin.setContent(content);
					cin.setContentInfo(MimeMediaType.OBIX);
					RequestSender.createContentInstance(targetId, cin);
				}
 
				// Wait for 2 seconds
				try{
					Thread.sleep(2000);
				} catch (InterruptedException e){
					e.printStackTrace();
				}
			}
		}
 
		public void stopThread(){
			running = false;
		}
 
	}
 
}

IPE oBIX Util

  • Convert the description and the measured data of the device to oBIX XML representation.
package org.eclipse.om2m.sample.ipe;
 
import org.eclipse.om2m.commons.constants.Constants;
import org.eclipse.om2m.commons.obix.Bool;
import org.eclipse.om2m.commons.obix.Contract;
import org.eclipse.om2m.commons.obix.Int;
import org.eclipse.om2m.commons.obix.Obj;
import org.eclipse.om2m.commons.obix.Op;
import org.eclipse.om2m.commons.obix.Uri;
import org.eclipse.om2m.commons.obix.io.ObixEncoder;
 
public class ObixUtil {
 
	public static String getSensorDescriptorRep(String appId, String ipeId) {
		String prefix = "/" + Constants.CSE_ID + "/" + Constants.CSE_NAME + "/" + appId;
		Obj obj = new Obj();
 
		Op opGet = new Op();
		opGet.setName("GET");
		opGet.setHref(new Uri(prefix + "/DATA/la"));
		opGet.setIs(new Contract("retrieve"));
		obj.add(opGet);
 
		Op opGetDirect = new Op();
		opGetDirect.setName("GET(Direct)");
		opGetDirect.setHref(new Uri(prefix + "?op=get"));
		opGetDirect.setIs(new Contract("execute"));
		obj.add(opGetDirect);
 
		return ObixEncoder.toString(obj);
	}
 
	public static String getActuatorDescriptorRep(String appId) {
		String prefix = "/" + Constants.CSE_ID + "/" + Constants.CSE_NAME + "/" + appId;
		Obj obj = new Obj();
 
		Op opGet = new Op();
		opGet.setName("GET");
		opGet.setHref(new Uri(prefix + "/DATA/la"));
		opGet.setIs(new Contract("retrieve"));
		obj.add(opGet);
 
		Op opGetDirect = new Op();
		opGetDirect.setName("GET(Direct)");
		opGetDirect.setHref(new Uri(prefix + "?op=get"));
		opGetDirect.setIs(new Contract("execute"));
		obj.add(opGetDirect);
 
		Op opON = new Op();
		opON.setName("ON");
		opON.setHref(new Uri(prefix + "?op=true"));
		opON.setIs(new Contract("execute"));
		obj.add(opON);
 
		Op opOFF = new Op();
		opOFF.setName("OFF");
		opOFF.setHref(new Uri(prefix + "?op=false"));
		opOFF.setIs(new Contract("execute"));
		obj.add(opOFF);
 
		return ObixEncoder.toString(obj);
	}
 
	public static String getActuatorDataRep(boolean value) {
		Obj obj = new Obj();
		obj.add(new Bool("data", value));
		return ObixEncoder.toString(obj);
	}
 
	public static String getSensorDataRep(int value) {
		Obj obj = new Obj();
		obj.add(new Int("data", value));
		return ObixEncoder.toString(obj);
	}
 
}

IPE Controller

  • Execute received requests from OM2M to the specific technologies.
package org.eclipse.om2m.sample.ipe;
 
import org.eclipse.om2m.commons.constants.ResponseStatusCode;
import org.eclipse.om2m.commons.resource.RequestPrimitive;
import org.eclipse.om2m.commons.resource.ResponsePrimitive;
import org.eclipse.om2m.interworking.service.InterworkingService;
 
public class Controller implements InterworkingService{
 
	@Override
	public ResponsePrimitive doExecute(RequestPrimitive request) {
		String[] parts = request.getTo().split("/");
		String appId = parts[3];
		ResponsePrimitive response = new ResponsePrimitive(request);
 
		if(request.getQueryStrings().containsKey("op")){
			String valueOp = request.getQueryStrings().get("op").get(0);
 
			switch(valueOp){
			case "get":
				if(appId.equals(Monitor.sensorId)){
					response.setContent(ObixUtil.getSensorDataRep(Monitor.sensorValue));
					response.setResponseStatusCode(ResponseStatusCode.OK);
				} else if (appId.equals(Monitor.actuatorId)){
					response.setContent(ObixUtil.getActuatorDataRep(Monitor.actuatorValue));
					response.setResponseStatusCode(ResponseStatusCode.OK);
				} else {
					response.setResponseStatusCode(ResponseStatusCode.BAD_REQUEST);
				}
				return response;
			case "true": case "false":
				if(appId.equals(Monitor.actuatorId)){
					Monitor.actuatorValue = Boolean.parseBoolean(valueOp);
					response.setResponseStatusCode(ResponseStatusCode.OK);
				} else {
					response.setResponseStatusCode(ResponseStatusCode.BAD_REQUEST);
				}
				return response;
			default:
				response.setResponseStatusCode(ResponseStatusCode.BAD_REQUEST);
			}
		} else {
			response.setResponseStatusCode(ResponseStatusCode.BAD_REQUEST);
		}
		return response;
	}
 
	@Override
	public String getAPOCPath() {
		return Monitor.ipeId;
	}
 
}


IPE Util: RequestSender

  • This class simplify the interaction with the CseService providing the response of specific requests directly. You can take inspiration of this class if you want to create your own request utility/factory.
package org.eclipse.om2m.sample.ipe;
 
import java.math.BigInteger;
 
import org.eclipse.om2m.commons.constants.Constants;
import org.eclipse.om2m.commons.constants.MimeMediaType;
import org.eclipse.om2m.commons.constants.Operation;
import org.eclipse.om2m.commons.constants.ResourceType;
import org.eclipse.om2m.commons.resource.AE;
import org.eclipse.om2m.commons.resource.Container;
import org.eclipse.om2m.commons.resource.ContentInstance;
import org.eclipse.om2m.commons.resource.RequestPrimitive;
import org.eclipse.om2m.commons.resource.Resource;
import org.eclipse.om2m.commons.resource.ResponsePrimitive;
import org.eclipse.om2m.core.service.CseService;
 
public class RequestSender {
 
	public static CseService CSE;
 
	/**
	 * Private constructor to avoid creation of this object
	 */
	private RequestSender(){}
 
	public static ResponsePrimitive createResource(String targetId, String name, Resource resource, int resourceType){
		RequestPrimitive request = new RequestPrimitive();
		request.setFrom(Constants.ADMIN_REQUESTING_ENTITY);
		request.setTargetId(targetId);
		request.setResourceType(BigInteger.valueOf(resourceType));
		request.setRequestContentType(MimeMediaType.OBJ);
		request.setReturnContentType(MimeMediaType.OBJ);
		request.setContent(resource);
		request.setName(name);
		request.setOperation(Operation.CREATE);
		return CSE.doRequest(request);
	}
 
	public static ResponsePrimitive createAE(AE resource, String name){
		return createResource("/" + Constants.CSE_ID, name, resource, ResourceType.AE);
	}
 
	public static ResponsePrimitive createContainer(String targetId, String name, Container resource){
		return createResource(targetId, name, resource, ResourceType.CONTAINER);
	}
 
	public static ResponsePrimitive createContentInstance(String targetId, String name, ContentInstance resource){
		return createResource(targetId, name, resource, ResourceType.CONTENT_INSTANCE);
	}
 
	public static ResponsePrimitive createContentInstance(String targetId, ContentInstance resource){
		return createContentInstance(targetId, null, resource);
	}
 
	public static ResponsePrimitive getRequest(String targetId){
		RequestPrimitive request = new RequestPrimitive();
		request.setFrom(Constants.ADMIN_REQUESTING_ENTITY);
		request.setTargetId(targetId);
		request.setReturnContentType(MimeMediaType.OBJ);
		request.setOperation(Operation.RETRIEVE);
		request.setRequestContentType(MimeMediaType.OBJ);
		return CSE.doRequest(request);
	}
 
}

Back to the top