Here is a sample microservice that communicates with a Predix Asset service instance and pushes data to Predix Mobile service instance. The data gets synced on devices running a Predix Mobile app. This sample is not meant for production use.
It is assumed that you have running instances of Predix Mobile service and Predix Asset service, and you have installed the Predix Mobile pm command line tool. To get started, follow this documentation:
- [Get Started with the Mobile Service and Mobile SDK] (https://www.predix.io/docs#rae4EfJ6)
- [Running the Predix Mobile Sample App] (https://www.predix.io/docs#EGUzWwcC)
- [Creating a Mobile Hello World Web App] (https://www.predix.io/docs#DrBWuHkl)
When running on a development system, use the configuration found in the config/default.json directory. When pushing to Cloud Foundry, use the manifest.yml.
Here is a simple configuration document:
{
"PORT": 8086,
"ASSET": {
"PA_PREDIX_ZONE_ID" : "XXXXX-86a5-45da-9053-6651c00d1a99", //Asset zone/tenant id
"PA_URL_ASSET" : "https://XXXXX.run.aws-usw02-pr.ice.predix.io/locomotives", //query url for Asset
"PA_URL_UAA" : "https://XXXX-uaa.run.aws-usw02-pr.ice.predix.io/oauth/token", // UAA url for Asset service
"PA_USERNAME" : "XXXX", //UAA user who is allowed to access Asset service
"PA_PASSWORD" : "********",
"DATA_REFRESH_TIME" : 5 //used to refresh data
},
"MOBILE_UP": {
"PM_EP_URL" : "https://XXXX.run.aws-usw02-pr.ice.predix.io/", // Mobile service `api_gateway_short_route`
"PM_BUCKET" : "pm", // bucket name used in cache.
"PM_USERNAME" : "XXXX@ge.com", // NOTE: This user should be granted the "pm.admin" scope in UAA. This scope is granted by default to any account created with the pm-add-service-account.sh or pm-add-developer.sh scripts of the PM-CLI. This permission can also be manually granted using UAAC, via a command of this format: uaac member add "pm.admin" "XXX@ge.com"
"PM_PASSWORD" : "********",
"PM_UAA_URL" : "https://XXXX-uaa.run.aws-usw02-pr.ice.predix.io/oauth/token", // UAA url for Mobile services
"WEB-APP-NAME" : "pa-basic-app", //App name that can access the Asset data generated by this microservice: name and version will be used to create a channel on which this data will be assigned.
"WEB-APP-VERSION" : "0.0.1"
}
}
Checkout this repository, open the folder in a terminal window, and execute:
npm install
Run either of these commands:
node index.js
OR
node index.js | ./node_modules/.bin/bunyan # outputs nice looking logs in terminal window
The command will:
- Get oauth-token for Asset service.
- Get oauth-token for Mobile service.
- Fetch data from Asset service every 5 seconds.
- Create (update) a Mobile service document (
docid = PM_USERNAME+'_predix_asset_'+PA_PREDIX_ZONE_ID) and assign it to a channel, based on WEB-APP-NAME & WEB-APP-VERSION. - Push this document into Mobile service.
- Start an Express server {TODO: accept commands to make changes in asset service data}
Note: This example service uses random asset filters to fetch different data and pushes this data to Mobile service. In real world applications, sync-processor can poll DB/ASSET service and compare results with old results before pushing to Mobile service OR DB/ASSET service will send a notification (postgress/mysql triggers) to sync-processor of data changes and then sync-processor can push those relevant changes to Mobile service.