Using Predix Mobile Services to Create a Cross-Platform App

by Igor RazhnovApril 7, 2016
Learn how to create an app targeted at multiple platforms with a single code base—by employing Apache Cordova and Predix Mobile services.

This tutorial introduces developers to mobile services available on GE’s Predix platform. The scenario for the article includes preparing a simple cross-platform application based on the Cordova technology, as well as interacting with Predix Mobile services. We also want to see if there are any restrictions for using Predix Mobile services in a hybrid application.

 

Prerequisites

using-predix-mobile-services-in-a-hybrid-app

To follow our examples, you need:

  • Node.js
  • the Cordova CLI
  • the Cloud Foundry CLI
  • the Cloud Foundry UAA CLI
  • the Predix Mobile CLI

You can find more information in the official Predix docs.

 

Preparation

After you install Cordova, create a project and add platforms you want to test. We used AngularJS to write a sample JavaScript application.

In our case, it is not necessary to deploy the application to Predix: the application will be located on a mobile device. For sure, you can deploy your application to the PaaS and use it via a browser.

At the same time, you need to take care of Predix Mobile services that your application will communicate with. So, we will focus on how to prepare Predix to serve your application requests.

The implementation of the JavaScript code for using Predix Mobile services is the same as you need in a RESTful application. You need to make POST or GET requests. Find the sample code in this GitHub repo.

 

Using Predix Mobile services

To make a valid request and get data from Predix services, you need to create a service instance and then bind it to your application. To show what services are available for your application, run the following command.

cf marketplace

As a result, you will get a list similar to the one displayed on the screenshot below.

using-predix-mobile-services

With the cf create-service service_name plan my_instance command, create a service instance for your application.

cf create-service log-stash-1 free myapp-log-service

The next step is to bind the new service to your application using the cf bind-service your_app_name service_instance_name command.

cf bind-service testhw myapp-log-service

To update your environment and put the changes into effect, run the following command.

cf restage app_name

Use the command bellow to show your configuration information about the included services.

cf env app_name

For example, the VCAP_SERVICES environment variable is shown on the image below.

using-predix-mobile-services-configuration

The next step is to set up access to the Predix services for making your requests.

 

Accessing Predix services

For this step, we use Predix tutorials as a guideline. You can find the details in this tutorial (“Exploring Application Security: Create a Predix Service and set up a UAA ClientId”). Here is the section that you need.

setting-up-access-to-predix-services

Our goal is to consecutively execute tasks from the menu below.

predix-starter-kit-menu

We’ll try to explain why this is necessary. Working with Predix Starter Kit, you create users who have access to the services you bind to an application. In the Update Client menu, you paste authority-token to add new authorities for accessing Predix services to your client.

predix-access-credentials

Without this manipulation, a user can’t get data from the service even with a valid token. You can input a queue of tokens for several services. Mark them with quotes and separate with spaces.

using-predix-mobile-services-in-a-cross-platform-app-uaa-client

Now, you are ready to do a request to fill an asset. Use the Predix form to make a test request.

using-predix-mobile-services-in-a-cross-platform-app

The sample Predix documentation has a mistake: the locomotive endpoint in the URL should be plural—locomotives.

Note: The body of the POST request should be wrapped in square brackets.

We didn’t find any obstacles for creating cross-platform applications using Predix Mobile services during our experiment. However, when working with Predix, you do need to care about managing Predix services and binding them to your application.

 

Further reading

 


This tutorial was written by Igor Razhnov, edited Victoria Fedzkovich and Alex Khizhniak.