Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
WouterLemaire
Active Contributor
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/80078d73ae1a428386c70794d20...

 

Install


Add the plugin to Eclipse

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

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 ! 🙂

 
10 Comments
Labels in this area