Skip to content

Tracking your assets with NileDB

Paco Hernández edited this page May 3, 2018 · 16 revisions

As you can imagine, NileDB (https://niledb.com) can be applied to a wide variety of different use cases. GraphQL services automatically generated on top of PostgreSQL is a very powerful pattern.

At the end of this article, you will be able to track your assets using NileDB Tracker App for Android and NileDB Core.

Let's go!

Install dependencies

First of all, you should have Docker & Docker Compose installed. If this is not the case, you can follow the instructions in https://docs.docker.com/install/ and https://docs.docker.com/compose/install/ to install both.

Install NileDB Core

Once you have Docker Compose installed, you will create a NileDB Core instance following next steps:

  1. Create a folder:
# mkdir tracking-your-assets
# cd tracking-your-assets
  1. Add "docker-compose.yml" file with the following contents:
version: "3.1"

services:
  db:
    container_name: db
    environment:
      POSTGRES_PASSWORD: postgres
    image: timescale/timescaledb-postgis:latest-pg10
    restart: always
    ports:
      - 5433:5432

  core:
    container_name: core
    depends_on:
      - db
    environment:
      - JAVA_OPTS="-Xms384m -Xmx384m"
    image: niledb/core
    links:
      - db
    ports:
      - 80:80
      - 1883:1883
    restart: always
  1. Install NileDB Core:
# sudo docker-compose up

This will create two containers, one with PostgreSQL database and the other with NileDB GraphQL server.

Create Database and re-generate GraphQL Service

  1. Create database schema:

Now, you will create a database schema in order to store you assets locations.

In our example, we will assume that we have a company that has several agents delivering packages. We want to track our agents.

# sudo docker exec -it db psql -U postgres niledb

niledb=# CREATE TABLE "CarrierAgent" (
	"id" serial PRIMARY KEY,
	"firstName" text NOT NULL,
	"lastName" text NOT NULL
);

niledb=# CREATE TABLE "CarrierAgentLocation" (
	"id" serial PRIMARY KEY,
	"carrierAgent" int NOT NULL,
	"timestamp" timestamp NOT NULL DEFAULT now(),
	"location" point
);

niledb=# CREATE UNIQUE INDEX ON "CarrierAgentLocation"
	("carrierAgent", "timestamp");

niledb=# ALTER TABLE "CarrierAgentLocation" ADD CONSTRAINT "carrierAgent" 
	FOREIGN KEY ("carrierAgent") REFERENCES "CarrierAgent"("id");
  1. Re-generate GraphQL services:

Now, with our database schema created, we can re-generate the GraphQL services. Go to http://localhost/graphql in your browser and execute the following command:

mutation {
  reloadGraphQLSchema
}

Reload GraphQL Schema

  1. Create a carrier agent:

Let's create an agent to track its locations. Execute the following command:

mutation {
  CarrierAgentCreate(
    entity: {
      firstName: "Paco"
      lastName: "Hernández"
    }
  ) {
    id
  }
}

Create Carrier Agent

The command returns the new created Agent identification number. This identification number will be used in the Tracker App later.

Install NileDB Tracker App for Android

  1. Enable Unknown Sources to install NileDB Tracker from GitHub:

Enable Unknown sources in your Android device under Settings | Security in order to allow installation of apps from other sources:

Create Carrier Agent

  1. Download APK:

Browse to https://github.com/NileDB/com.niledb.tracker.android/releases and download app-debug.apk into your Android device.

This is the APK (Android Package) link: https://github.com/NileDB/com.niledb.tracker.android/releases/download/0.1.1/app-debug.apk

  1. Install APK:

Open the downloaded app-debug.apk file and install it into your Android device.

Install NileDB Tracker Android App Install NileDB Tracker Android App

  1. Configure NileDB Tracker to send locations to your NileDB server:

The first time you open the app, you must allow the app to access your location.

Allow location access

Then, you can configure the different parameters:

Configuration

Let's configure additional parameters first (you can leave default values here):

  • Entity name: CarrierAgentLocation
  • Agent identification parameter name: carrierAgent
  • Location parameter name: location
  • ...
  • Enable debugging (sound): You can enable this parameter if you want to hear a beep each time the app sends a location to the server.
  • Enable debugging (vibrate): You can enable this parameter if you want the device to vibrate each time the app sends a location to the server.

Configuration

Then, you could adjust communication parameters:

  • Communication protocol: GraphQL | MQTT
  • GraphQL endpoint: Adjust this URL to your environment. This is the same endpoint you used previously.
  • MQTT endpoint: If you have selected GraphQL, this endpoint is not used.
  • Minimum distance: This is the minimum distance the agent has to move in order to send a new location to the server.
  • Minimum time interval: This is the minimum time interval has to elapse to send a new location to the server.

Configuration

Now, you can activate the service:

  • Agent identification: The identification of the agent we created earlier.
  • Service Activated: Be sure to activate the service in order to send locations to the server.

Configuration

Finally, you can check that the tracker app is sending locations with the following GraphQL query:

{
  CarrierAgentList {
    id
    firstName
    lastName
    
    locations: CarrierAgentLocationListViaCarrierAgent {
      timestamp
      location
    }
  }
}

Locations

Now, you can integrate this locations with your Web Application..... or you can copy and paste into Google Maps....

Google Maps

That's all folks.

As you can see, it is very easy to integrate NileDB to meet your specific requirements.

Thank you very much! ;-)