Skip to Content

In the first blog post of this Iot blog post series we’ve prepared the Raspberry Pi for the upcoming blog posts.

In this blog post we’ll create a Java app on your free developer account on the SAP HANA Cloud Platform that will receive the sensor values from the Raspberry Pi.

Once this is up and running, the Java app on the Raspberry Pi will be “a piece of cake” ūüôā

NOTE AND DISCLAIMER

Please be aware that this blog post will not provide you with code that you should use productively. It actually is focusing on getting an app up-and-running for demo purposes.

So take this whole blog post series and its sample code AS IS and with the purpose it was build: to inspire you to think about other IoT (Internet of Things) scenarios you could run on the SAP HANA Cloud Platform and NOT as a best practice for productive environments.

So let’s get started.

Table of Contents

The Business Scenario: Rui’s Fish Import/Export Inc.

In our scenario we’ll assume we are in the Fish business and transport fish from one location to the other. As the fish we are transporting is very valuable we want to be able to track the temperature inside the fish boxes remotely.

For that purpose we have installed inside our truck a Raspberry Pi that can send data to the internet. In addition to that we have connected 4 temperature sensors to the RaspberryPi that we’ll use to determine the temperature inside the fish boxes. Those temperature values will be sent every minute to your free developer account on the SAP HANA Cloud Platform.

Screen Shot 2014-11-12 at 14.20.26.png

The applications we’ll need

Our solution will have in its center an SAP HANA Cloud Platform account. We’ll use an account under the free developer license.

The software solution well build will have three building blocks.

Java app on the Raspberry Pi

Firstly we’ll provide our Raspberry Pi with a Java app. That app will send an http request to a Java app on the SAP HANA Cloud Platform and send sensor data to it.

Java app on SAP HANA Cloud Platform account

Secondly we’ll have a Java app on your account on the SAP HANA Cloud Platform that will be the receiving the data from your Raspberry Pi. That app will also persist the data on the HANA instance of your account.

The Java app will also be able to create a JSON output with the sensor data so it can be consumed by the SAPUI5 application we have.

SAPUI5 app on SAP HANA Cloud Platform

The SAPUI5 app will provide us with a dashboard showing the current temperature of the fish boxes in the truck. That application will consume the JSON output from the Java app on the SAP HANA Cloud Platform account.

Get a free developer account for SAP HANA Cloud Platform

As a developer you can get a free developer account under a developer license. You register on https://hanatrial.ondemand.com and after activating your account via an email you get access to the cockpit of your developer account (https://account.hanatrial.ondemand.com/cockpit).

Setup your development environment

You’ll need to install Eclipse IDE for Java EE Developers as well as the SAP HANA Cloud Platform Tools and the SAP HANA Tools Eclipse plugins.

In a last step you also need to download the Java Web SAP HANA Cloud Platform SDK and unzip it on your drive. I’ve used for this blog post the SDK version 1.65.10.

To prepare your project you should also already add a new server under the Servers view for deployment on your localhost.

All of these steps are drilled-down in detail on a tutorial at the hcp.sap.com website. Just follow the steps there and you are all set for the next steps.

Screen Shot 2014-11-17 at 09.48.06.png

Deploy the Java app on your SAP HANA Cloud Platform account

The Java app on your account will be a Dynamic Web Project. So go ahead and create such a project in your Eclipse.

To do that switch to your Java EE perspective (Window > Open Perspective > Other > Java EE) and create such a project (File > New > Dynamic Web Project). Ensure your screen looks like the screenshot below.

Meaning you have selected the correct Target runtime (Java Web) as well as the right Dynamic web module version (2.5) and the right Configuration (Default Configuration for Java Web).

Screen Shot 2014-11-17 at 09.40.15.png

Getting the libs

Now you need to get a couple of libraries first to use the app. Three of them can be found in the Java Web SDK of the SAP HANA Cloud Platform (1.xx.yy). They are called eclipselink-xxx.jar, javax.persistence-yyy and com.sap.security.core.server.csi_1.0.1.jar. You can find all three files in one of the samples in the SDK folder under samples > persistence-with-jpa > WebContent > WEB-INF > lib. In the SDK version I’ve used (1.65.10) the files are called eclipselink-2.5.1.jar, javax.persistence-2.1.0.jar and com.sap.security.core.server.csi_1.0.1.jar. Copy all three files into the lib folder of your Eclipse project (WebContent > WEB-INF > lib).

The other library you need is the GSON library from Google, which helps us generate JSON output quite elegantly. You can get it here. You also need to copy that jar file into the lib folder of your Eclipse project (WebContent > WEB-INF > lib).

Setup project facets

We’ll use JPA in our project hence we need to activate the corresponding project facet in our project. Simply right-click on your project in the Eclipse project explorer and select Build path > Configure Build Path. After that click on the Project Facets entry on the left side of the window and activate on the right the JPA project facet in version 2.0.

Once you click on it you’ll also notice at the bottom of the window a link called “Further configuration available”. Click on that link and configure the facet as shown in the screenshot below.

Screen Shot 2014-11-21 at 15.59.16.png

Add the source code

We’ll need in total four Java classes. One is the servlet, the other one a helper class for the persistence and two other classes containing the data model of our app.

The two persistence classes

Click on the folder of your project that is called Java Resources, right-click on it and select New > Class. In the Package field enter the value org.persistence and in the filed Name you need to enter Measurement. Once entered, click on Finish.

Now do the same for the class Sensor. In the Package field enter also the value org.persistence and in the filed Name you need to enter Sensor. Once entered, click on Finish.

Substitute the generated code with the following code.

Code for Measurement.java file

package org.persistence;

import static javax.persistence.GenerationType.AUTO;

import java.io.Serializable;

import java.sql.Timestamp;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.NamedQueries;

import javax.persistence.NamedQuery;

@Entity

@NamedQueries({

@NamedQuery(name = “AllMeasurements”, query = “select m from Measurement m”),

@NamedQuery(name = “LastSensorReading”, query = “select m from Measurement m where m.sensorId = :paramSensorId and m.storedAt =¬† (SELECT MAX(r.storedAt) from Measurement r where r.sensorId = :paramSensorId)”),

@NamedQuery(name = “LastReadingsFromSensor”, query = “select p from Measurement p where p.sensorId = :paramSensorId order by p.storedAt DESC”) })

public class Measurement implements Serializable {

private static final long serialVersionUID = 1L;

public Measurement() {}

@Id

@GeneratedValue(strategy = AUTO)

private Long id;

private String unit;

private Timestamp storedAt;

private Double value;

private long sensorId;

public Long getId() {return id;}

public void setId(Long id) {this.id = id;}

public String getUnit() {return unit;}

public void setUnit(String unit) {this.unit = unit;}

public Timestamp getStoredAt() {return storedAt;}

public void setStoredAt(Timestamp dateStored) {this.storedAt = dateStored;}

public Double getValue() {return value;}

public void setValue(Double sensorValue) {this.value = sensorValue;}

public static long getSerialversionuid() {return serialVersionUID;}

public long getSensorId() {return sensorId;}

public void setSensorId(long param) {this.sensorId = param;}

}

Code for Sensor.java file

package org.persistence;

import java.io.Serializable;

import javax.persistence.*;

import org.persistence.Measurement;

import java.util.Collection;

import static javax.persistence.GenerationType.AUTO;

@Entity

@NamedQueries({ @NamedQuery(name = “GetListOfSensors”, query = “select s from Sensor s”) })

public class Sensor implements Serializable {

private static final long serialVersionUID = 1L;

public Sensor() { }

@Id

@GeneratedValue(strategy = AUTO)

private long id;

private String device;

private String type;

private String description;

private Measurement lastMeasurement;

@OneToMany

private Collection<Measurement> measurements;

public Measurement getLastMeasurement() {return lastMeasurement;}

public void setLastMeasurement(Measurement lastMeasurement) {this.lastMeasurement = lastMeasurement;}

public long getId() {return id;}

public void setId(long id) {this.id = id;}

public String getDevice() {return device;}

public void setDevice(String param) {this.device = param;}

public String getType() {return type;}

public void setType(String param) {this.type = param;}

public String getDescription() {return description;}

public void setDescription(String param) {this.description = param;}

public Collection<Measurement> getMeasurement() {return measurements;}

public void setMeasurement(Collection<Measurement> param) {this.measurements = param;}

}

Finally you need to update the persistence.xml file so that you add a few new lines to register the two persistence classes to it.

Open the persistence.xml file (Java Resources > src > META-INF) by right-clicking on the persistence.xml file and click on Open With > Text Editor.

Between the persistence-unit tag you need to have 6 additional lines added which are marked bold below.

Be aware to only add the 6 new lines, because the file contains the name of your project in the persistence-unit under the name property.

So your persistence.xml file looks like this.

<?xml version=”1.0″ encoding=”UTF-8″?>

<persistence version=”2.0″ xmlns=”http://java.sun.com/xml/ns/persistence” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd“>

<persistence-unit name=”iotscenario”>

        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

        <class>org.persistence.Measurement</class>

        <class>org.persistence.Sensor</class>

        <properties>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† <property name=”eclipselink.ddl-generation” value=”create-tables”/>

        </properties>

</persistence-unit>

</persistence>

The SensorsServlet

The SensorsServlet is the servlet that will receive the data and also take care to create corresponding JSON output.

Create a servlet called SensorsServlet. To do that click on the folder of your project that is called Java Resources, right-click on it and select New > Servlet. As Java package enter the value com.sap.iot.sensors and as Class name you enter SensorsServlet. Once entered click on Finish.

Now just replace the code you have there with the code for SensorsServlet that I’ve provided as an additional file to this blog. Just unzip the file and copy-and-paste it.

Please be sure to adapt the init() method inside the SensorsServlet so that

emf = Persistence.createEntityManagerFactory(“iotscenario“, properties);

has the same name like your project. It needs to be the same like defined in the persistence-unit entry of your persistence.xml file (under <persistence-unit name=”iotscenario“>).

The DataHelper class

This class will help us with some methods to persist data.

Create a classe called DataHelper. To do that click on the folder of your project that is called Java Resources, right-click on it and select New > Class. As Java package enter the value com.sap.iot.sensors and as Class name you enter DataHelper. Once entered click on Finish.

Now just replace the code you have there with the code for DataHelper.java (see at the bottom of this post).

Adapt the web.xml

At the end we’ll need to modify the web.xml file for the servlet mapping of the Servlet. Instead of

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://java.sun.com/xml/ns/javaee” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=”WebApp_ID” version=”2.5″>

<display-name>iotscenario</display-name>

<resource-ref>

<res-ref-name>jdbc/DefaultDB</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

</resource-ref>

<servlet>

<description></description>

<display-name>SensorsServlet</display-name>

<servlet-name>SensorsServlet</servlet-name>

<servlet-class>com.sap.iot.sensors.SensorsServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SensorsServlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

we need to change the url-pattern and add a new resource into the web.xml for the jdbc connection so that the part of the servlet-mapping looks like this

  <resource-ref>

    <res-ref-name>jdbc/DefaultDB</res-ref-name>

    <res-type>javax.sql.DataSource</res-type>

  </resource-ref>

<servlet>

<description></description>

<display-name>SensorsServlet</display-name>

<servlet-name>SensorsServlet</servlet-name>

<servlet-class>com.sap.iot.sensors.SensorsServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SensorsServlet</servlet-name>

  <url-pattern>/</url-pattern>

</servlet-mapping>

The resulting project

At the end your project structure should look like this:

Screen Shot 2014-11-17 at 13.13.31.png

Deploy the app

Now deploy the app to your free developer account. Just follow the instructions in the tutorial HCP and Java – Hello World on the hcp.sap.com website if you don’t know how to do that. Check the step called “Deploy, Run and Test the Application in the Cloud” in that tutorial and adapt the instructions there to your project here.

Test the app

If you did everything right you should now be able to add a sensor to the database. Let’s test this.

When calling your app for the first time you’ll get an empty JSON output with an opening and closing curly bracket.

{
}

Adding a sensor

To add a sensor to your database you should call the servlet this way:

https://<name of your application><your account name>.hanatrial.ondemand.com/<name of your project>/?action=addsensor&type=temperature&device=Truck 1- RaspPi&description=RaspberryPi CPU

If your account is called p1234567890trial, the project is called iotscenario and you defined the name fishimport as the name of your app when you’ve deployed it to your account the link looks like this:

https://fishimportp1234567890trial.hanatrial.ondemand.com/iotscenario/?action=addsensor&type=temperature&device=Truck 1- RaspPi&description=RaspberryPi CPU

Just enter the link and press return. After that call your app again (in this example the link to the app is https://fishimportp1234567890trial.hanatrial.ondemand.com/iotscenario), and instead of getting empty curly brackets you should see something similar like this:

{

“sensor0”:

{“id”:1,”device”:”Truck 1 – RaspPi”,”type”:”temperature”,”description”:”RaspberryPi CPU”,”measurements”:[]}

}

You can add as many sensors as you want. Most importantly each of them as a unique id. That id is used to assign measurements to it. That’s what we’ll do in the next step.

Adding a sensor value (measurement)

Now that we have a sensor added to the database we can also store the sensor values related to it.

To add a sensor value (or measurement how the object is called in the database) you need to call the servlet with different parameters. Taking the example from above where we’ve created a sensor with the id 1, the link to create a corresponding measurement looks like this:

https://fishimportp1234567890trial.hanatrial.ondemand.com/iotscenario/?action=addsensorvalue&sensorid=1&unit=Celsius&sensorvalue=16&sensorvaluemultiplier=0.1&sensorvaluecalibration=0

Again type-in the link and execute it. Afterwards call the servlet again without any parameters and you’ll get an output similar to this one:

{

“id”:1,”device”:”Truck 1 – RaspPi”,”type”:”temperature”,”description”:”RaspberryPi CPU”,

“lastMeasurement”:

{“id”:1,”unit”:”Celsius”,”storedAt”:”Nov 10, 2014 9:53:13 AM”,”value”:-1.6,”sensorId”:1},

“measurements”:

[

{“id”:1,”unit”:”Celsius”,”storedAt”:”Nov 10, 2014 9:53:13 AM”,”value”:-1.1,”sensorId”:1}

]

}

The output provides you now with the last measured sensor value (lastMeasurement) as well as a list of all stored measurements (measurements). Although it should provide you with ALL measurements you have assigned to the sensor there is a limit that I’ve added for performance reasons.

If you check the code of the servlet there is a variable inside SensorsServlet called MAX_NUMBER_SENSOR_READINGS. In the code I’ve provided this is set to 10. Of course you can modify this number and adapt it to your needs.

Summary

We are now set to send sensor values to our app in the cloud which also provides a JSON output of that data that we can consume in an UI.

Screen+Shot+2014-11-12+at+14.20.26.png

In the next blog post we’ll create the sending part on the RaspberryPi so that it can send it’s temperature to our app in the cloud.

Looking forward for your feedback.

Best,

Rui

—–

DataHelper.java

package com.sap.iot.sensors;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.persistence.Measurement;
import org.persistence.Sensor;

public class DataHelper {
	private EntityManagerFactory emf;

	public DataHelper(EntityManagerFactory emf) {
		this.emf = emf;
	}

	/*
	 * Persists a measured sensor value (measurement)
	 * 
	 * @param measurement The measured sensor value
	 */
	public boolean addMeasurement(Measurement measurement) {
		boolean result = false;

		EntityManager em = emf.createEntityManager();
		// System.out.println("Trying to commit sensor data for sensor " +
		// measurement.getSensorDescription());
		try {
			if (measurement != null && measurement.getValue() != null) {
				em.getTransaction().begin();
				em.persist(measurement);
				em.getTransaction().commit();
			}
		} catch (Exception e) {
			System.out.println("ERROR: persisting measurement didn't work " + e.getMessage());
		} finally {
			em.close();
		}

		return result;
	}

	/*
	 * Persists a new sensor
	 * 
	 * @param sensor The sensor object to be added
	 */
	public boolean addSensor(Sensor sensor) {
		boolean result = false;
		if (sensor != null) {
			EntityManager em = emf.createEntityManager();
			try {

				em.getTransaction().begin();
				em.persist(sensor);
				em.getTransaction().commit();
				result = true;

			} catch (Exception e) {
				System.out.println("ERROR: persisting sensor didn't work " + e.getMessage());
				result = false;
			}
			em.close();

		}

		return result;
	}

	/*
	 * Provides a list of a defined number of sensor readings for a specific
	 * sensor. The method will provide the newest sensor readings (measurements)
	 * first
	 * 
	 * @param sensorId The sensor id of the sensor that you wish to get the
	 * measured values from
	 * 
	 * @param numberOfReadings The maximum number of readings you'll get back
	 */
	@SuppressWarnings("unchecked")
	public List<Measurement> getLastSensorReadings(long sensorId, int numberOfReadings) {
		List<Measurement> result = null;

		EntityManager em = emf.createEntityManager();
		try {

			Query q = em.createNamedQuery("LastReadingsFromSensor");
			q.setParameter("paramSensorId", sensorId);
			// To not affect performance we just retrieve the first 20 result
			// sets
			q.setMaxResults(numberOfReadings);
			result = q.getResultList();

			Collections.sort(result, new Comparator<Measurement>() {
				public int compare(Measurement m1, Measurement m2) {
					return m1.getStoredAt().compareTo(m2.getStoredAt());
				}
			});

		} catch (Exception e) {

		}

		em.close();
		return result;
	}

	/*
	 * Provides a list of ALL sensor readings. To avoid too many data the output
	 * is restricted to a maximum of 500 entries
	 */
	@SuppressWarnings("unchecked")
	public List<Measurement> getAllSensorReadings() {
		List<Measurement> result = null;

		EntityManager em = emf.createEntityManager();
		try {
			Query q = em.createNamedQuery("AllMeasurements");
			q.setMaxResults(500);
			result = q.getResultList();

		} catch (Exception e) {

		}

		em.close();
		return result;
	}

	/*
	 * Provides the last measured sensor value for a sensor
	 * 
	 * @param sensorId The sensor id of the sensor that you wish to get the
	 * measured value from
	 */
	public Measurement getLastSensorReading(long sensorId) {
		Measurement result = null;

		EntityManager em = emf.createEntityManager();
		try {
			Query q = em.createNamedQuery("LastSensorReading");
			q.setParameter("paramSensorId", sensorId);
			result = (Measurement) q.getSingleResult();

		} catch (Exception e) {

		}

		em.close();
		return result;
	}

	/*
	 * Provides a list of all sensors
	 */
	@SuppressWarnings("unchecked")
	public List<Sensor> getListOfSensors() {
		List<Sensor> result = null;

		EntityManager em = emf.createEntityManager();
		try {
			Query q = em.createNamedQuery("GetListOfSensors");
			result = q.getResultList();
		} catch (Exception e) {

		}

		em.close();
		return result;
	}

}

SensorsServlet.java

package com.sap.iot.sensors;

 

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.sql.Timestamp;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;

 

import org.eclipse.persistence.config.PersistenceUnitProperties;

import org.persistence.Measurement;

import org.persistence.Sensor;

 

import com.google.gson.Gson;

import com.sap.security.core.server.csi.IXSSEncoder;

import com.sap.security.core.server.csi.XSSEncoder;

 

/**

* Servlet implementation class SensorsServlet

*/

public class SensorsServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

 

    private DataSource ds;

    private EntityManagerFactory emf;

 

    // Number of sensor readings that should be sent as response

    private static final int MAX_NUMBER_SENSOR_READINGS = 10;

 

    /**

    * @see HttpServlet#HttpServlet()

    */

    public SensorsServlet() {

        super();

 

    }

 

    /**

    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse

    *      response)

    */

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String action = encodeText(request.getParameter("action"));

 

        DataHelper dataHelper = new DataHelper(emf);

 

        // Add a sensor

        if (action != null && action.equalsIgnoreCase("addsensor")) {

            Sensor sensor = extractSensorData(request);

            dataHelper.addSensor(sensor);

        }

 

        // Add a sensor value

        if (action != null && action.equalsIgnoreCase("addsensorvalue")) {

            Measurement measurement = extractMeasurementData(request);

            dataHelper.addMeasurement(measurement);

        }

 

        // Provide a JSON output of all sensor values (measurements)

        if (action != null && action.equalsIgnoreCase("showallmeasurements")) {

            List<Measurement> sensorMeasurements = dataHelper.getAllSensorReadings();

            outputJsonForAllMeasurements(response, sensorMeasurements);

        }

 

        // If no action parameter is provided simply print out the sensor data

        // as JSON

        if (action == null) {

            List<Sensor> sensors = dataHelper.getListOfSensors();

            // Step into each sensor and add the related measurements to it

            for (int i = 0; i < sensors.size(); i++) {

                Sensor sensor = sensors.get(i);

 

                List<Measurement> sensorMeasurements = dataHelper.getLastSensorReadings(sensor.getId(), MAX_NUMBER_SENSOR_READINGS);

                sensor.setMeasurement(sensorMeasurements);

 

                Measurement sensorLastMeasurement = dataHelper.getLastSensorReading(sensor.getId());

                sensor.setLastMeasurement(sensorLastMeasurement);

 

                sensors.set(i, sensor);

            }

            outputJsonAllData(response, sensors);

        }

    }

 

    /*

    * Creates a JSON output of all sensor values (measurements)

    *

    * @param response The HTTP-response object

    *

    * @param sensorMeasurements The list of sensors values (measurements)

    */

    private void outputJsonForAllMeasurements(HttpServletResponse response, List<Measurement> sensorMeasurements) {

        Gson gson = new Gson();

        try {

            response.getWriter().println("{");

            for (int i = 0; i < sensorMeasurements.size(); i++) {

                response.getWriter().println(gson.toJson(sensorMeasurements.get(i)));

 

                if (i != sensorMeasurements.size() - 1) {

                    response.getWriter().println(",");

                }

            }

 

            response.getWriter().println("}");

        } catch (IOException e) {

            e.printStackTrace();

        }

 

    }

 

    /*

    * Creates a JSON output out of the provided list of sensors with their

    * corresponding sensor values (measurements)

    *

    * @param response The HTTP-response object

    *

    * @param sensors The list of sensors with their respective measurements

    */

    private void outputJsonAllData(HttpServletResponse response, List<Sensor> sensors) {

 

        Gson gson = new Gson();

        try {

            response.getWriter().println("{");

            for (int i = 0; i < sensors.size(); i++) {

                response.getWriter().println('"' + "sensor" + i + '"' + ":");

                response.getWriter().print(gson.toJson(sensors.get(i)));

 

                if (i != sensors.size() - 1) {

                    response.getWriter().println(",");

                }

            }

 

            response.getWriter().println("}");

        } catch (IOException e) {

            e.printStackTrace();

        }

 

    }

 

    /*

    * Extracts a Sensor object out of the parameters provided in the

    * HTTP-request

    *

    * @param request The HTTP-request object

    *

    * @return The derived Sensor object

    */

    private Sensor extractSensorData(HttpServletRequest request) {

        Sensor sensor = new Sensor();

        String idString = encodeText(request.getParameter("id"));

        if (idString != null && idString.length() > 0) {

            sensor.setId(Long.parseLong(idString));

        }

        sensor.setType(encodeText(request.getParameter("type")));

        sensor.setDevice(encodeText(request.getParameter("device")));

        sensor.setDescription(encodeText(request.getParameter("description")));

        return sensor;

    }

 

    /*

    * Extracts a Measurement object (sensor values) out of the parameters

    * provided in the HTTP-request
    *
    * @param request The HTTP-request object
    *
    * @return The derived Measurement object
    */
    private Measurement extractMeasurementData(HttpServletRequest request) {
        Measurement measurement = new Measurement();

        // Get sensorId
        String sensorIdString = encodeText(request.getParameter("sensorid"));
        if (sensorIdString != null && sensorIdString.length() > 0) {
            measurement.setSensorId(Long.parseLong(sensorIdString));
        }
        // Get unit of measured value
        measurement.setUnit(encodeText(request.getParameter("unit")));

        // Get measured value and calculate the value to be stored
        String sensorValue = encodeText(request.getParameter("sensorvalue"));
        String sensorValueMultiplier = encodeText(request.getParameter("sensorvaluemultiplier"));
        String sensorValueCalibration = encodeText(request.getParameter("sensorvaluecalibration"));

        if (sensorValueCalibration != null && sensorValueCalibration.length() > 0 && sensorValue != null && sensorValueMultiplier != null && sensorValueMultiplier.length() > 0
                && sensorValue.length() > 0) {
            measurement.setStoredAt(new Timestamp(new Date().getTime()));
            Double valueDouble = Double.parseDouble(sensorValue);
            Double multiplierDouble = Double.parseDouble(sensorValueMultiplier);
            Double valueGap = Double.parseDouble(sensorValueCalibration);
            Double value = (valueDouble * multiplierDouble) + valueGap;
            measurement.setValue(value);
        }
        return measurement;
    }

    /*
    * Encodes a text to avoid cross-site-scripting vulnerability
    *
    * @param request The text to be encoded
    *
    * @return The encoded String
    */
    private String encodeText(String text) {
        String result = null;
        IXSSEncoder xssEncoder = XSSEncoder.getInstance();
        try {
            result = (String) xssEncoder.encodeURL(text);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
    *      response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    /** {@inheritDoc} */
    @SuppressWarnings({ "rawtypes", "unchecked" })

    @Override
    public void init() throws ServletException {
        try {
            InitialContext ctx = new InitialContext();
            ds = (DataSource) ctx.lookup("java:comp/env/jdbc/DefaultDB");
            Map properties = new HashMap();
            properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, ds);

            emf = Persistence.createEntityManagerFactory("practice", properties);
        } catch (NamingException e) {
            throw new ServletException(e);
        }
    }


    /** {@inheritDoc} */

    @Override
    public void destroy() {
        emf.close();
    }

}
To report this post you need to login first.

55 Comments

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

  1. Rui Nogueira Post author

    Hi all, I’ve just added the code for the SensorsServlet and DataHelper as additional files to this blog post. Makes it easier for you to copy-and-paste the code.

    Best,

    Rui

    (1) 
    1. Daniel Heidinger

       

      Hello,

      Where are the files mentioned in this blog post? (zip?)
      The SensorsServlet and the DataHelper or copy and paste the code.

      Please make it available again. Thanks!

      (1) 
      1. Rui Nogueira Post author

        Hi Gregor, you are absolutely right.

        There is no need for a Java app, but you can also create a HANA-native app for this purpose with less effort.

        Haven’t had a chance looking into your “SAP HANA Cloud Trial Authentication Proxy for HANA XS Services”.

        In case you try out with your project please share. Thanks a lot!

        Best,

        Rui

        (0) 
      2. Hendrik Neumann

        Cool idea! If you could give me an extra long weekend next, I’d be up for the challenge ūüėČ

        (and now I’m even more convinced that we’ll be seeing some HCP-XS-Auth-Magic at sitfra)

        Cheers & Have fun Gregor!

        Hendrik

        (0) 
  2. P. Lans

    That’s a serious amount of Java. And Java is not really my thing

    But it all looks pretty comprehansible so I’ll just try and learn ūüôā

    A nice one for the weekend

    Regards,

    Peter

    (0) 
  3. Fouad Fahim

    Hello, Rui–

    I’m getting an issue with the project, there is warning that prevents the deployment of the application in HCP, here is the warning:

    Description Resource Path Location Type No connection specified for project. No database-specific validation will be performed

    It’s related to a missing connection in the Setup project facets where an entry of the connection is missing.

    Do you have any input on this?

    Cheers,

    Fouad

    (0) 
          1. Zaharia Nicu

            I have a similar issue: JPA Problem: No connection specified for project. No database-specific validation will be performed. I am using EclipseLink 2.2.x

            (0) 
    1. Rui Nogueira Post author

      Hi Basar, was hoping that others will jump-in and do that :-).

      I haven’t done much with nodeJS, but you should be able to use the same principles also on nodeJS.

      Best,

      Rui

      (0) 
  4. David Szatmari

    Hi Rui!

    Could I make a calculation view on those tables that were generated from the persistence units? I cannot even find them on the trial cloud account… I have loads of temperature readings and I would like to sum them (lets say by day) in a claculation view. I would call that calculation view in the app.

    Great post and thanks,

    David.

    (0) 
    1. Rui Nogueira Post author

      Hi Andrey, yes. It is possible by editing a property in the persistence.xml file. Just set the DDL generation type from “Create Tables” to “Drop and Create Tables” and deploy your application again to your account. After that the tables of the app are all deleted and created again.

      Screen Shot 2015-07-08 at 17.17.17.png

      If you want to keep your data after deploying the app, you need to switch the property back to “Create Tables”.

      Hope this helps.

      Best,

      Rui

      (0) 
  5. Arush Saxena

    Hi Rui,

    Excellent blog. I was trying to write procedures and create views on the sensor readings dumped to the cloud. However, I get the following error (Insufficient Privileges Execute on Repository_Rest) when I open the content section in SAP Hana Administration console on eclipse. It seems I don’t have the required package privileges. Could you please suggest a solution to this issue.Error_i849681.PNG

    Thanks and Regards

    (0) 
    1. Anton Levin

      Hi Arush,

      it is a shared HANA instance on trial.You are not allowed to see the Contents. You may try to create a HANA XS Trial instance for your HCP account on trial and then re-bind the ‘iotscenario’ java app to work with the created HANA XS instance. With that you will have your own package under Content directory, create XS apps and also use SAP HANA Web Based Development workbench for development.

      Regards,

      Anton

      P.S. After re-bind the data source for your java app, you will have to re-start an app and send some data first.

      Update. Necessary steps on how to create a XS Trial instance and do the re-binding of the data source can be found in blog post #3 by the following link Using the SAP HCP IoT Services

      (0) 
      1. Arush Saxena

        Thank you so much Anton.

        That really helps a lot. I created a HANA XS trial instance and changed the binding. I am able to see the content section and I have the required access privileges I need to create procedures and views.

        Regards

        (0) 
  6. Andrey Savvin

    Hi Rui and Aaron,

    Thank you for your support.

    I was able to connect “DS18B20″ sensor on raspberry pi over  the node-red.

    node-red1.JPG

    node-red2.JPG


    Sending home room temperature  over Internet of Things Services (BETA)

    using HTTP API data services POST as per instructions provided

    hana1.JPG

    Screen below shows exercise with HANA XS application. As per Aaron pdf “Connecting a UI5 GUI using WebIDE”

    hana2.JPG

    Next idea is build fiori application to get access to the data from temperature sensor.

    With regards,

    Andrey

    (0) 
  7. Niels Noort

    I Rui,

    Great post and I tried going through the steps. i do get errors in the eclipse environment. the classes dont seem to be found. I have included a picture.

    I did probably download newer versions of the jre and eclipse environment, could that result in these errors.

    Regards, Niels.pict_errors.JPG

    Figured it out. In Eclips the org.persistance package was not recognised correctly. Fixed now, works like a dream.

    (0) 
    1. Anton Levin

      Hi Eric, do you use the same setup: HCP Runtime and JPA? What is the persistence version specified in your persistence.xml?

      Regards,

      Anton

      (0) 
      1. Eric Rogge

        It seems to be 2.1

        <?xml version=”1.0″ encoding=”UTF-8″?>

        <persistence version=”2.1″ xmlns=”http://xmlns.jcp.org/xml/ns/persistence” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd“>

          <persistence-unit name=”EricsTemperatureApp” transaction-type=”RESOURCE_LOCAL”>

          <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

          <class>org.persistence.Measurement</class>

                <class>org.persistence.Sensor</class>

          <properties>

          <property name=”eclipselink.ddl-generation” value=”create-tables”/>

          </properties>

          </persistence-unit>

        </persistence>

        My suspicion is that my web.xml config is not right. See below:

        <?xml version=”1.0″ encoding=”UTF-8″?>

        <web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instancexmlns=”http://java.sun.com/xml/ns/javaee” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=”WebApp_ID” version=”2.5″>

          <display-name>EricsTemperatureApp</display-name>

          <resource-ref>

            <res-ref-name>jdbc/DefaultDB</res-ref-name>

            <res-type>javax.sql.DataSource</res-type>

          </resource-ref>

          <servlet>

            <description></description>

            <display-name>SensorsServlet</display-name>

            <servlet-name>SensorsServlet</servlet-name>

            <servlet-class>com.sap.iot.sensors.SensorsServlet</servlet-class>

          </servlet>

          <servlet-mapping>

            <servlet-name>SensorsServlet</servlet-name>

          <url-pattern>/</url-pattern>

          </servlet-mapping>

        </web-app>

        Any help gratefully accepted.

        Best,
        Eric

        (0) 
        1. Eric Rogge

          FYI, I’m very much a newbie to Eclipse, SAP’s environment and XML config files. Used to be a C coder.

          Here are my Java resource files

          Screen Shot 2015-10-30 at 8.28.35 AM.png

          Best,

          Eric

          (0) 
  8. Dietmar Steinbichler

    Awesome tutorial Rui!

    Just one comment to maybe help others with the same issue.

    After deploying the app I got message:

    HTTP Status 500 – while trying to invoke the method java.util.List.size() of a null object loaded from local variable ‘sensors’

    It seems the named queries were not registered successfully. To fix this I had to add the persistency classes to the persistency.xml:

    <?xml version=“1.0” encoding=“UTF-8”?>

    <persistence version=“2.0” xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd>

        <persistence-unit name=“iotscenario”>

            <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

            <class>org.persistence.Measurement</class>

            <class>org.persistence.Sensor</class>

            <properties>

                <property name=“eclipselink.ddl-generation” value=“create-tables”/>

            </properties>

        </persistence-unit>

    </persistence>


    Cheers,

    Dietmar

    (0) 
  9. Rakesh Kumar Bhure

    Hi,

    I got servlet deployed on HCP. I am able to run it and see {}. When I am trying to add sensor, it is throwing an exception. Could you please help let me know what could be the issue here.

    HCP_excp_1.PNG

    HCP_excp_2.PNG

    HCP_excp_3.PNG

    (0) 
  10. Moya Watson

    Hi all —
    we’re aware that the required class/code files referenced in this blog have been lost during the SAP Community migration. ¬†Unfortunately Rui is out of the office at the moment but is aware of the issue and plans to get to it as soon as possible! ¬†Stay tuned and apologies for the inconvenience!
    -m

    (0) 
  11. Moya Watson

    Hi all – we’ve restored DataHelper.java. ¬†If there are still other missing files we’ll need to wait for Rui to return. ¬†Hopefully this helps get further!
    thanks

    (0) 
  12. Simon Deutschl

    Hi all together,

    Are there any updates on the missing¬†SensorsServlet file? I’d like to use it for my IoT project. When does¬†Rui Nogueira upload it?

    BR
    Simon

    (0) 
  13. Rui Nogueira Post author

    Hey everybody, due to a great community member (thanks Olivia Carline!) I was able to add the missing Java servlet SensorsServlet.

    Best,
    Rui

    (0) 

Leave a Reply