-
Notifications
You must be signed in to change notification settings - Fork 1
Tracking your assets with NileDB
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!
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.
Once you have Docker Compose installed, you will create a NileDB Core instance following next steps:
- Create a folder:
# mkdir tracking-your-assets
# cd tracking-your-assets
- 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
- 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 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");
- 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
}
- 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
}
}
The command returns the new created Agent identification number. This identification number will be used in the Tracker App later.
- 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:
- 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
- Install APK:
Open the downloaded app-debug.apk file and install it into your Android device.
- 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.
Then, you can configure the different parameters:
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.
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.
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.
Finally, you can check that the tracker app is sending locations with the following GraphQL query:
{
CarrierAgentList {
id
firstName
lastName
locations: CarrierAgentLocationListViaCarrierAgent {
timestamp
location
}
}
}
Now, you can integrate this locations with your Web Application..... or you can copy and paste into 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! ;-)