Introduction

This tutorial will walk you through how to write a program that can import data about your environments into the Flare server.

The Flare sever comes with an import.js script that you can run on the command line on the server. It reads some Flare objects from a JSON file and imports them directly into the database. See the server page for instructions on getting the server up and running and importing data server-side.

This tutorial will walk you through writing a script that can do the same thing client-side. It will read the same JSON file and call the API.

Using the buttons at the top of the column to the right, you can see the raw API calls and JSON objects, or follow along with code samples in each of the supported languages.

To import your own data, you can:

  • modify the JSON file with a description of your own environments
  • modify one of the import scripts to connect to your own data source
  • write your own script in the language of your choice that calls the API

See the environments page for a detailed description of the Flare object model, and other ways of importing data into the server.

See the import script included with each sample code project for a complete solution.

Dependencies

Start by importing these dependencies.

Data source

open the model.json file

Open the JSON file, which contains a hierarchical outline of Flare objects. Iterate over the environments array, and call the importEnvironment function for each environment.

Instead of importing from JSON, you could connect to your own data source and modify the script as needed.

Environments

POST /environments

{
    "name": "New York",
    "description": "Cisco New York, One Penn Plaza",
    "geofence": {
        "latitude": 40.751267,
        "longitude": -73.99229,
        "radius": 500
    },
    "perimeter": {
        "origin": {
            "x": -1,
            "y": -1
        },
        "size": {
            "height": 100,
            "width": 100
        }
    },
    "angle": 29,
    "data": {
        "uuid": "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"
    }
}

This function will create a new environment object from the source object by copying one property at a time. This assumes that the source object is already in the format of a Flare object, so this is a trivial operation (we could just duplicate the source object or use it directly). But the sample code breaks it down this way to validate the data, and to give an example that you can modify if you are using a different data source.

The code then creates a new environment by calling the API. The response includes the ID of the new environment, which is generated by the database. Then we iterate over all the zones and devices in the environment, and create them using the new environment ID.

Zones

POST /environments/{environment_id}/zones

{
    "name": "Open Space",
    "description": "The open space near the elevators",
    "perimeter": {
        "origin": {
            "x": 5,
            "y": 5
        },
        "size": {
            "height": 10,
            "width": 10
        }
    },
    "data": {
        "major": 2
    },
}

This function will create a new zone object from the source object by copying one property at a time, as above. Modify as necessary for your data source.

The code then creates a new zone by calling the API. The response includes the ID of the new zone. Then we iterate over all the things in the zone, and create them using the environment ID and the new zone ID.

Things

POST /environments/{environment_id}/zones/{zone_id}/things

{
    "name": "Southwest",
    "description": "Flare 1.3",
    "position": {
        "x": 1,
        "y": 1
    },
    "data": {
        "minor": 3
    }
}

This function will create a new thing object from the source object by copying one property at a time, as above. Modify as necessary for your data source.

The code then creates a new thing by calling the API.

Devices

POST /environments/{environment_id}/devices

{
    "name": "My iPhone",
    "description": "iPhone 6 (32 GB)",
    "position": {
        "x": 3,
        "y": 1
    },
    "data": {
        "color": "red",
        "angle": 90,
        "mac": "69:6b:a6:11:9a:3e"
    }
}

This function will create a new device object from the source object by copying one property at a time, as above. Modify as necessary for your data source.

The code then creates a new device by calling the API.