Skip to Content

In IoT, you don’t want to store all the data from the device into the cloud or manipulate. For that reason, we can do Edge Processing.  SAP has out of the box solutions for doing Edge Processing like the Streaming service,… But you could also add your own logic by creating interceptors!

In this blog, I want to show you how you can create an interceptor and how this can be used to filter the data coming from the device. In my example, I’m going read a property that I’ve defined in the custom properties of the device configuration (in the IoT cockpit). This property will be used to only allow values, from the device, that are higher.

Before I start creating the Interceptor, I add a value in the custom properties of my device that I want to use for filtering:

Let’s start

Download

Start by downloading the Eclipse plugin to create Interceptors

https://help.sap.com/viewer/c4945853cc164aa385973d5938b385ac/Cloud/en-US/80078d73ae1a428386c70794d2069545.html

 

Install

Add the plugin to Eclipse

https://help.sap.com/viewer/c4945853cc164aa385973d5938b385ac/Cloud/en-US/80c309e15e514bd89709ac6b4c3a99a4.html

Create Interceptor

Before we can create an Incterceptor, we need to create a target project.

Create Target project

The Target project will keep the libraries that we need to build the project

Go to File -> New -> Other

Search for the folder “SAP Cloud Platform Internet of Things Service” and select Target.

Give a name to your IoT Target Project:

Open the file that you’ve downloaded in the beginning and go to the gateway folder. Copy the folder plugins and version.json to the lib package in the Target project

Just select “Copy files and folders”

Your project will look like this:

Open the “gateway-target.target” file and set this target as Active Target Platform

 

Create Interceptor

Now, we can create an Interceptor

Give a name for your interceptor

By default, it generates you a template like this with some example code:

I changed the example code in the function “processObject” of the “InterceptorImpl” to the following:

The code does the following:

  • Read the incoming value
  • Get the custom property from the device configuration
  • Compare the values
  • Delete in case the incoming value is too low
try {
	log.info("Interceptor triggered: pointcut " + pointcutName);
	IoTServicesPointcut pointcut = IoTServicesPointcut.valueOf(pointcutName);
	List<WSNParsedMeasure> toBeRemoved = new ArrayList<>();
	Object obj = args[0];

	switch (pointcut) {

	//event which is fired for all sensor data received
	case GATEWAY_PARSED_DATA_DISPATCH:
		
		//list of measures received in this call
		List<WSNParsedMeasure> measures = (List<WSNParsedMeasure>) obj;

		if (measures != null) {
			for (WSNParsedMeasure wsnParsedMeasure : measures) {
				List<Value<?>> valueList = wsnParsedMeasure.getValues();

				for (int i = 0; i < valueList.size(); i++) {
					Value<?> value = (Value<?>) valueList.get(i);
					//get incoming value
					String newValue = "";
					if (value.getInnerMeasure() instanceof String) {
						Value<String> valueString = (Value<String>) value;
						newValue = processMessage((Value<String>) value);
						valueString.setInnerMeasure(newValue);
					} else if (value.getInnerMeasure() instanceof Integer) {
						Value<Integer> valueString = (Value<Integer>) value;
						Integer newValueInt = processMessageInt((Value<Integer>) value);
						valueString.setInnerMeasure(newValueInt);
						newValue = newValueInt.toString();
					}

					//get custom property from the device configuratie
					String minValue = "";	
					//get device check old prop value if same ignore ; else set new alarm value
					IGatewayTopology topologyService = InterceptorActivator.getToplogyService();
					if(topologyService != null){
						Device device = topologyService.getDevice(wsnParsedMeasure.getDeviceAlternateId());
						if(device != null){
							Map<String,String> propMap = device.getProperties();
							log.info("all");
							for (Map.Entry<String, String> entry : propMap.entrySet())
							{
								System.out.println(entry.getKey() + "/" + entry.getValue());
								log.info(entry.getKey() + "/" + entry.getValue());
							}
							if(propMap != null){
								log.info("get min value");
								minValue = propMap.get("minValue");
								log.info("min value:"+minValue);
							}
						}
						
					}
					//compare values and ignore values that are too low
					if (Integer.parseInt(newValue) < Integer.parseInt(minValue)) {
						log.info("Data dropped below filter value");
						toBeRemoved.add(wsnParsedMeasure);
					}
					
				}
			}

			// final filtering of list
			for (WSNParsedMeasure wsnParsedMeasure : toBeRemoved) {
				measures.remove(wsnParsedMeasure);
			}

			log.info("Measurements received");
		}
		break;
	default:
		break;

	}
} catch (Exception e) {
	log.error(e.getMessage());
}

Build

When the code is finished, we can build the Interceptor project. Right click on the Interceptor project -> Build IoT Interceptor project.

This will generate a jar file in the target folder

Deploy

We can deploy our Interceptor in Eclipse to the IoT Gateway but that didn’t work for me. Therefore, I used the IoT Cockpit. In the IoT Cockpit, you can view all the IoT Gateways. I used my own IoT Gateway that’s installed on our own internal server. You can validate this by the type, “Edge”. Because I’m using my own IoT Gateway, I’m able to see the logs of my Interceptor on the server.

You could also do this for Cloud IoT Gateways but then I don’t know how you can view the logs…

Select the IoT Gateway where you want to deploy the Interceptor. Go to Bundle Management and click on the upload icon:

Browse for the generated jar file (which is generated by the build in your project) and click on deploy.

When the deployment is successful, you’ll find your interceptor in the list.

Test

For testing my Interceptor, I used the Paho Client. I used the IP and port of our internal IoT Gateway

tcp://<ip>:61618

The Deviceid:

measures/10

The message:

{“capabilityAlternateId”:”0104″,”sensorAlternateId”:”11″,”measures”:[[19]] }

When I send a value that’s lower than 12, I’ll see the following in the logs on the IoT Gateway but I won’t see the value in the IoT Cockpit

You won’t find a value lower than 12 here:

 

IoT AE in SCP, this is where the cool sh!t happens ! 🙂

 

To report this post you need to login first.

9 Comments

You must be Logged on to comment or reply to a post.

  1. Sergio Guerrero

    Wouter,

    this is a really nice blog with a lot of great details. I like the fact you shared the code and details on how to create the interceptor, the plug in docs and how you were able to upload it to the SCP. If anything, I think it would also be helpful to explain what the message and device parts of the IOT cockpit and how else they can be consumed. here I shared those details a while back https://blogs.sap.com/2016/03/16/my-first-use-of-hcptrial-iot-service/

    otherwise, this blog is very helpful. Thank you for sharing!

     

    (0) 
    1. Iwona Hahn

      Hi Sergio,

      The blog you are referring to in your comment is related to the IoT service for the Neo environment and not as mentioned in this blog post here to the IoT service for the Cloud Foundry environment.

      Best regards,
      Iwona

      (0) 
  2. Iwona Hahn

    Hi Wouter,

    Thank you for this blog post. Can you please correct the headline to “How to do Edge Processing with SAP Cloud Platform Internet of Things – Interceptors” to make it clear, that you use the SDK for the Internet of Things Service (as referred by the links) and not for SAP IoT Application Enablement?

    Thank you and best regards,
    Iwona

    (0) 
  3. Vivek Gupta

    Hi Wouter Lemaire –

    Thanks for excellent block on interceptor …!!

    My Question are below ;

    1. As per my understanding Interceptor install on IoT Gateway Edge,not in IOT Gateway Cloud
    2. I have been confuse with Interceptor and SAP Edge services . Is they are different ? If not than how and where uses these feature ?  Probably if you can explain with example will be great .?

    Regards~

    Vivek Gupta

     

     

     

    (0) 
    1. Wouter Lemaire Post author

      To be honest, I didn’t test it on the Cloud gateway… I just noticed that you can deploy to the cloud gateway…

      SAP Edge Services comes with out of the box interceptors that you can configure. In this blog, I show how you can create your own interceptor like SAP provides in SAP Edge Services..

       

      Kr, Wouter

       

      (0) 
  4. Vivek Gupta

    Hi Wouter Lemaire –

    Big Thanks for reply … !!

    For Question # 1 , i am fine .

    For Question # 2 :-  SAP Edge Services comes with pre-build interceptor where you define rule and filter on sensor  , Where in your block create custom interceptor on edge device ( hardware ) … and than define rule and filter on sensor data . Kindly clarify this ?

     

    Regards~

    Vivek gupta

     

    (1) 

Leave a Reply