Jump to: navigation, search

OM2M/one/Developer

< OM2M‎ | one

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
  • 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.
  • 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);
	}
 
}