POST Create Fence – Client examples

/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Create fence client demonstrates adding fences of various shapes to an existing zone using 'request' node module.
 * Circular
 * Bounding Box
 * Polygonal
 * Geoline
 */
var request = require('request');
 
var circularFenceData =
{
    "security": {
        /* The apiKey is generated when you create an application. */
        "apiKey" : "c2c8211f-796f-4eda-b6ce-05467b5263a9",
        /* This key is generated by Bluedot Point Access UI when your account is created. It is also available
         * on the PointAccess interface in the Edit Profile section. */      
        "customerApiKey": "86577370-7b91-11e4-bcb7-a0481cdc3311"
    },
    "content": {
        "zone": {
            /* The zoneId is the id of the zone being updated. This can be fetched by calling GET Zones API */
            "zoneId": "722c991a-3ebb-4143-b441-5b0c0cf680a0",
            "fences": {
                "circles": [
                    {
                        "order": 1,
                        "name": "A circleur fence",
                        "color": "#000ffff",
                        "radius": 8.00,
                        "center": {
                            "latitude": -37.8159544565362,
                            "longitude": 144.9723565578461
                        }
                    }
                ]
            }
        }
    }
};
 
var rectangularFenceData =
{
    "security": {
        /* The apiKey is generated when you create an application. */
        "apiKey" : "c2c8211f-796f-4eda-b6ce-05467b5263a9",
        /* This key is generated by Bluedot Point Access UI when your account is created. It is also available
         * on the PointAccess interface in the Edit Profile section. */      
        "customerApiKey": "86577370-7b91-11e4-bcb7-a0481cdc3311"
    },
    "content": {
        "zone": {
            /* The zoneId is the id of the zone being updated. This can be fetched by calling GET Zones API */
            "zoneId": "722c991a-3ebb-4143-b441-5b0c0cf680a0",
            "fences": {
                "rectangles": [
                    {
                        "order": 2,
                        "name": "A bounding box with north east and south west",
                        "color": "#3559e",
                        "northEast": {
                            "latitude": -37.81758175613945,
                            "longitude": 144.9731397628784
                        },
                        "southWest": {
                            "latitude": -37.8159544565362,
                            "longitude": 144.9723565578461                                       
                        }
                    }
            }
        }
    }
};
 
var polygonalFenceData =
{
    "security": {
        /* The apiKey is generated when you create an application. */
        "apiKey" : "c2c8211f-796f-4eda-b6ce-05467b5263a9",
        /* This key is generated by Bluedot Point Access UI when your account is created. It is also available
         * on the PointAccess interface in the Edit Profile section. */      
        "customerApiKey": "86577370-7b91-11e4-bcb7-a0481cdc3311"
    },
    "content": {
        "zone": {
            /* The zoneId is the id of the zone being updated. This can be fetched by calling GET Zones API */
            "zoneId": "722c991a-3ebb-4143-b441-5b0c0cf680a0",
            "fences": {
                "polygons": [
                    {
                        "order": 3,
                        "name": "A Polygonal Fence",
                        "color": "#000ffff",
                        "vertices": [
                            {
                                "longitude" : 144.9786221981049,
                                "latitude" : -37.81464920072108
                            },
                            {
                                "longitude" : 144.9755859375,
                                "latitude" : -37.81670030664056
                            },
                            {
                                "longitude" : 144.9807143211365,
                                "latitude" : -37.81650536927249
                            }
                        ]
                    }
                ]
            }
        }
    }
};
 
var geolineFenceData =
    {
        "security": {
            /* The apiKey is generated when you create an application. */
            "apiKey" : "c2c8211f-796f-4eda-b6ce-05467b5263a9",
            /* This key is generated by Bluedot Point Access UI when your account is created. It is also available
             * on the PointAccess interface in the Edit Profile section. */      
            "customerApiKey": "86577370-7b91-11e4-bcb7-a0481cdc3311"
        },
        "content": {
            "zone": {
                /* The zoneId is the id of the zone being updated. This can be fetched by calling GET Zones API */
                "zoneId": "722c991a-3ebb-4143-b441-5b0c0cf680a0",
                "fences": {
                    "polylines": [
                        {
                            "order": 4,
                            "name": "PolyLine Around MCG",
                            "color": "#000ffff",
                            "vertices": [
                                 {
                                     "latitude" : -37.818717,
                                     "longitude" : 144.983085
                                 },
                                 {
                                    "latitude" : -37.819540,
                                    "longitude" : 144.982125
                                 },
                                 {
                                    "latitude" : -37.820298,
                                    "longitude" : 144.985178
                                 },
                                 {
                                    "latitude" : -37.820468,
                                    "longitude" : 144.984228
                                 },
                                 {
                                     "latitude" : -37.818768,
                                     "longitude" : 144.984330
                                 },
                                 {
                                     "latitude" : -37.819476,
                                     "longitude" : 144.985033
                                 },
                                 {
                                    "latitude" : -37.820527,
                                    "longitude" : 144.982978
                                 },
                                 {
                                    "latitude" : -37.818887,
                                    "longitude" : 144.982587
                                 }
                            ]
                        }
                    ]
                }
            }
        }
    };
 
 
var options = {
    uri: 'https://api.bluedotinnovation.com/1/fences',
    method: 'POST',
    json: geolineFenceData
};
 
request(options,
    function (error, response) {
        if (error) {
            console.log(error);
        }
        console.log(JSON.stringify(response.body));
    }
);
package com.bluedotinnovation.fence;
 
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
 
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
 
import com.bluedotinnovation.common.BDCommon;
 
/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Add fence client demonstrates adding fences of various shapes to an existing zone using Apache HTTP client and JSON Simple libraries.
 * The different types of geofences are:
 *      - Circular
 *      - Bounding Box
 *      - Polygonal
 *      - Geoline
 */
public class AddFence extends BDCommon {
 
    private static String bdCustomerApiKey    = "ca4c8d11-6942-11e4-ba4b-a0481cdc3311"; //This key is generated by Bluedot Point Access UI when your account is created.
    private static String bdApplicationApiKey = "f5223f4c-a0f3-47be-ba59-080b6290a9d4"; //This apiKey is generated when you create an application
    private static String bdZoneId            = "dfafb7fd-d2f7-42ec-afd5-d5c7851c0396"; //This is the id of the zone being updated. This can be fetched by calling GET Zones API
    private static String bdRestUrl           = "https://api.bluedotinnovation.com/1/fences";
     
    /**
     * @param args
     * @throws ClientProtocolException
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     * @throws ParseException
     */
    public static void main(String[] args) throws ClientProtocolException, IOException, KeyManagementException, NoSuchAlgorithmException, ParseException {
         
        CloseableHttpClient httpRestClient = HttpClients.custom().setSSLSocketFactory(getSSLContextFactory()).build();
 
        JSONParser parser    = new JSONParser();
        JSONObject bdPolygonalFenceJSONObject = (JSONObject) parser.parse(getJsonPolygonalFence()); //add a polygonal fence
 
        HttpPost postRequest = new HttpPost(bdRestUrl);                    
        postRequest.addHeader("content-type", "application/json");
        postRequest.setEntity(new StringEntity(bdPolygonalFenceJSONObject.toJSONString(), Charset.defaultCharset()));
 
        HttpResponse response = httpRestClient.execute(postRequest);
 
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("Fence was successfully created");
            InputStream inputStream = response.getEntity().getContent();
            byte[] bytes            = readStream(inputStream);
            String resultString     = new String(bytes); //json result
            JSONObject jsonResult   = (JSONObject)  parser.parse(resultString);
            System.out.println(jsonResult);
        } else {
            InputStream inputStream = response.getEntity().getContent();
            byte[] bytes            = readStream(inputStream);
            String resultString     = new String(bytes); //json error result
            System.out.println(resultString);
        }             
    }
     
    /*Circular fence requires a centerpoint and radius*/
    private static String getJsonCircularFence() {     
        String circularFenceJson = 
            "{" +
               "\"security\": {" +
               "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
               "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
            "}," +
            "\"content\": {" +
                "\"zone\": {" +
                    "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                    "\"fences\": {" +
                        "\"circles\": [" +
                            "{"+
                                "\"order\": 1," +
                                "\"name\": \"Circle-1\","+
                                "\"color\": \"#000ffff\","+
                                "\"radius\": \"30.330384237225\","+
                                "\"center\": {"+
                                    "\"latitude\": -37.818780,"+
                                    "\"longitude\": 144.980734 "+
                                "}"+
                            "}" +
                        "]" +
                    "}"+
                "}"+
            "}"+
         "}";    
        return circularFenceJson;
    }
     
    /*Bounding box requires north east and south west points*/
    private static String getJsonBoundingBox() {
        String boundingBoxFenceJson =
                "{" +
                   "\"security\": {" +
                   "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                   "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                "}," +
                "\"content\": {" +
                    "\"zone\": {" +
                       "\"zoneId\":" + "\"" + bdZoneId + "\"," + 
                     "\"fences\": {" +
                        "\"rectangles\": [" +
                            "{"+
                                "\"order\": 2," +
                                "\"name\": \"Bounding Box-1\"," +
                                "\"color\": \"#3559e\"," +
                                "\"northEast\": {" +
                                    "\"latitude\": -37.81544591805361," +
                                    "\"longitude\": 144.9786114692688"+
                                "}," +
                                "\"southWest\": {" +
                                    "\"latitude\": -37.81758175613945," +
                                    "\"longitude\": 144.9731397628784" +
                                "}" +
                            "}" +
                        "]" +
                    "}"+
                "}"+
             "}" +
           "}";                          
        return boundingBoxFenceJson;
    }
     
    /*Polygonal fence requires a series points in lat/long*/
    private static String getJsonPolygonalFence() {
        String polygonalFenceJson =
                       "{" +
                           "\"security\": {" +
                              "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                              "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                        "}," +
                        "\"content\": {" +
                            "\"zone\": {" +
                                "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                                "\"fences\": {" +
                                "\"polygons\": [" +
                                    "{" +
                                        "\"order\": 3," +
                                        "\"name\": \"Polygon-1\"," +
                                        "\"color\": \"#000ffff\"," +
                                        "\"vertices\": [" +
                                             "{" +
                                                 "\"latitude\": -37.818717," +
                                                 "\"longitude\": 144.983085" +
                                             "},"+
                                             "{" +
                                                "\"latitude\": -37.819540," +
                                                "\"longitude\": 144.982125" +
                                             "}," +
                                             "{"  +
                                                "\"latitude\": -37.820298," +
                                                "\"longitude\": 144.985178" +
                                             "}," +
                                             "{" +
                                                "\"latitude\": -37.820468," +
                                                "\"longitude\": 144.984228" +
                                             "}," +
                                             "{" +
                                                "\"latitude\": -37.818768," +
                                                "\"longitude\": 144.984330" +
                                             "}," +
                                             "{" +
                                                "\"latitude\": -37.819476," +
                                                "\"longitude\": 144.985033" +
                                             "}," +
                                             "{" +
                                                "\"latitude\": -37.820527," +
                                                "\"longitude\": 144.982978" +
                                             "}," +
                                             "{"  +
                                                "\"latitude\": -37.818887," +
                                                "\"longitude\": 144.982587" +
                                             "}" +
                                        "]" +
                                    "}" +
                                "]" +
                            "}" +
                        "}"+
                     "}" +
                "}";                         
        return polygonalFenceJson;
    }
     
    /*Geoline fence requires a series points in lat/long*/
    private static String getJsonGeoline() {
        String geolineJson =
                       "{" +
                           "\"security\": {" +
                              "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                              "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                        "}," +
                        "\"content\": {" +
                            "\"zone\": {" +
                                "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                                "\"fences\": {" +
                                "\"polylines\": [" +
                                    "{" +
                                        "\"order\": 4," +
                                        "\"name\": \"Geoline-1\"," +
                                        "\"color\": \"#000ffff\"," +
                                        "\"vertices\": [" +
                                             "{" +
                                                 "\"latitude\": -37.818717," +
                                                 "\"longitude\": 144.983085" +
                                             "},"+
                                             "{"  +
                                                "\"latitude\": -37.818887," +
                                                "\"longitude\": 144.982587" +
                                             "}" +
                                        "]" +
                                    "}" +
                                "]" +
                            "}" +
                        "}"+
                     "}" +
                "}";                         
        return geolineJson;
    }
}
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
 
/**
* @author Bluedot Innovation
* Copyright (c) 2016 Bluedot Innovation. All rights reserved.
* Add fence client demonstrates adding fences of various shapes to an existing zone using net http web api library
* Circular fence
* Bounding Box
* Polygonal
* Geoline
*/
 
namespace BluedotPublicApiClient.fenceclient
{
    public class BDAddFenceClient
    {
        private static String bdApplicationApiKey = "a46fc46a-63ac-4c0c-8a9c-3c9aafd88e46"; //This apiKey is generated when you create an application
        private static String bdCustomerApiKey = "944ab370-7a0b-11e4-828c-a0481cdc3311"; //This key is generated by Bluedot Point Access UI when your account is created
        private static String bdZoneId = "cc6f9dd1-3d69-454d-abdd-58176cbf67dc"; //This is the id of the zone being updated. This can be fetched by calling zones/getAll API
        private static String bdRestUrl = "https://api.bluedotinnovation.com/1/fences";
 
        public void addFence()
        {
            WebRequestHandler handler = new WebRequestHandler();
            X509Certificate2 certificate = new X509Certificate2();
            handler.ClientCertificates.Add(certificate);
            HttpClient httpRestClient = new HttpClient(handler);
 
            //specify to use TLS 1.2 as default connection
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
 
            httpRestClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
 
            HttpContent jsonFenceContent = new StringContent(getJsonPolygonalFence());
            jsonFenceContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 
            HttpResponseMessage serverResponse   = httpRestClient.PostAsync(new Uri(bdRestUrl), jsonFenceContent).Result;
            if (serverResponse.IsSuccessStatusCode)
            {
                var result = serverResponse.Content.ReadAsStringAsync().Result;
 
                Console.WriteLine("{0}", result.ToString());
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)serverResponse.StatusCode, serverResponse.Content.ReadAsStringAsync().Result);
            }
        }
 
        /*Circular fence requires a centerpoint and radius*/
        private static String getJsonCircularFence()
        {
            String circularFenceJson =
            "{" +
                "\"security\": {" +
                 "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                 "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
             "}," +
             "\"content\": {" +
                 "\"zone\": {" +
                     "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                     "\"fences\": {" +
                         "\"circles\": [" +
                             "{" +
                                 "\"order\": 1," +
                                 "\"name\": \"Fence-0\"," +
                                 "\"color\": \"#b3a0d\"," +
                                 "\"radius\": 12.855266312171226," +
                                 "\"center\": {" +
                                     "\"latitude\": \"-37.81868567196429\"," +
                                     "\"longitude\": \"144.98012825841897\" " +
                                 "}" +
                             "}" +
                         "]" +
                     "}" +
                 "}" +
             "}" +
           "}";
           return circularFenceJson;
        }
 
        /*Bounding box requires north east and south west points*/
        private static String getJsonBoundingBox()
        {
            String boundingBoxFenceJson =
                    "{" +
                       "\"security\": {" +
                            "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                            "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                    "}," +
                    "\"content\": {" +
                        "\"zone\": {" +
                            "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                         "\"fences\": {" +
                            "\"rectangles\": [" +
                                "{" +
                                    "\"order\": 2," +
                                    "\"name\": \"Bounding Box-1\"," +
                                    "\"color\": \"#3dcb69\"," +
                                    "\"northEast\": {" +
                                        "\"latitude\": -37.81864541435753," +
                                        "\"longitude\": 144.98078003525734" +
                                    "}," +
                                    "\"southWest\": {" +
                                        "\"latitude\": -37.81885941545376," +
                                        "\"longitude\": 144.98049572110176" +
                                    "}" +
                                "}" +
                            "]" +
                        "}" +
                    "}" +
                 "}" +
               "}";
            return boundingBoxFenceJson;
        }
 
        /*Polygonal fence requires a series points in lat/long*/
        private static String getJsonPolygonalFence()
        {
            String polygonalFenceJson =
                           "{" +
                               "\"security\": {" +
                                   "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                                   "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                            "}," +
                            "\"content\": {" +
                                "\"zone\": {" +
                                    "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                                    "\"fences\": {" +
                                    "\"polygons\": [" +
                                        "{" +
                                            "\"order\": 3," +
                                            "\"name\": \"Polygon-1\"," +
                                            "\"color\": \"#a0d7be\"," +
                                            "\"vertices\": [" +
                                                 "{" +
                                                     "\"latitude\": -37.81527640444762," +
                                                     "\"longitude\": 144.975049495697" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.80735968553275," +
                                                    "\"longitude\": 144.9712514877319" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.80581692842606," +
                                                    "\"longitude\": 144.9556946754456" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.80500315345196," +
                                                    "\"longitude\": 144.9490320682526" +
                                                 "}," +
                                                  "{" +
                                                    "\"latitude\": -37.80698671424123," +
                                                    "\"longitude\": 144.9468326568604" +
                                                 "}," +
                                                    "{" +
                                                    "\"latitude\": -37.80927537202523," +
                                                    "\"longitude\": 144.9441075325012" +
                                                 "}," +
                                                  
                                                   "{" +
                                                    "\"latitude\": -37.812004715604," +
                                                    "\"longitude\": 144.9460601806641" +
                                                 "}," +
                                                  "{" +
                                                    "\"latitude\": -37.81137748408285," +
                                                    "\"longitude\": 144.9477124214172" +
                                                 "},"
                                                 +
                                                  "{" +
                                                    "\"latitude\": -37.81346258449963," +
                                                    "\"longitude\": 144.949836730957" +
                                                 "},"
                                                    +
                                                  "{" +
                                                    "\"latitude\": -37.82136169906169," +
                                                    "\"longitude\": 144.9547076225281" +
                                                 "}" +
                                            "]" +
                                        "}" +
                                    "]" +
                                "}" +
                            "}" +
                         "}" +
                    "}";
            return polygonalFenceJson;
        }
 
        /*Geoline fence requires a series points in lat/long*/
        private static String getJsonGeolineFence()
        {
            String geolineFenceJson =
                           "{" +
                               "\"security\": {" +
                                   "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                                   "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\" " +
                            "}," +
                            "\"content\": {" +
                                "\"zone\": {" +
                                    "\"zoneId\":" + "\"" + bdZoneId + "\"," +
                                    "\"fences\": {" +
                                    "\"polylines\": [" +
                                        "{" +
                                            "\"order\": 4," +
                                            "\"name\": \"Geoline-1\"," +
                                            "\"color\": \"#a0d7be\"," +
                                            "\"vertices\": [" +
                                                 "{" +
                                                     "\"latitude\": -37.81527640444762," +
                                                     "\"longitude\": 144.975049495697" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.80735968553275," +
                                                    "\"longitude\": 144.9712514877319" +
                                                 "}," +
                                                 "{" +
                                                    "\"latitude\": -37.80581692842606," +
                                                    "\"longitude\": 144.9556946754456" +
                                                 "}," +
                                                  "{" +
                                                    "\"latitude\": -37.82136169906169," +
                                                    "\"longitude\": 144.9547076225281" +
                                                 "}" +
                                            "]" +
                                        "}" +
                                    "]" +
                                "}" +
                            "}" +
                         "}" +
                    "}";
            return geolineFenceJson;
        }
    }
}
Created by Bluedot DevOps on January 31, 2018

Start the discussion