POST Create Complete Zone – Client examples

/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Create Complete Zone client demonstrates adding a complete Zone which has fences, beacons and actions
 * to an existing application using 'request' node module.
 */
var request = require('request');
 
//JSON for a complete Zone with a beacon and a custom action which is active for a certain time period during the day. 
//Time values have a format of HH:MM and the period value has to be one of {am/pm}
var zoneTimeActiveData =
    {
        "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": {
                "zoneName": "starbucks",
                "minimumRetriggerTime": "00:01",
                "enableCheckOut": false,
                "timeActive": {
                    "from": {
                        /* Time in Hour:Minute format.*/
                        "time": "0:00",
                        "period": "am"
                    },
                    "to": {
                        /* Time in Hour:Minute format.*/
                        "time": "12:00",
                        "period": "pm"
                    }
                },
                "actions": {
                    "customActions": [
                        {
                            "name": "A Custom Application action"
                        }
                    ]
                },
                "beacons": [
                    {
                        /*This is the ID of a beacon registered to a customer's account,
                         * which has to be associated with a Zone.  */
                        "beaconId" : "f4d3c2f3-b2ba-4529-adac-18af1598ee4f",
                        "proximity" : 1,
                        "order": 1
                    }
                ]
            }
        }
    };
//JSON for a complete Zone which is active all day with a circular geofence and a message action.
var zoneActiveAllDayData =
{
    "security": {
        "apiKey" : "c2c8211f-796f-4eda-b6ce-05467b5263a9",    
        "customerApiKey": "86577370-7b91-11e4-bcb7-a0481cdc3311"
    },
    "content": {
        "zone": {
            "zoneName": "starbucks",
            "minimumRetriggerTime": "00:01",
            "enableCheckOut": false,
            "activeAllDay": true,
            "fences": {
                "circles": [
                    {
                        "order": 1
                        "name": "A circular fence",
                        "color": "#000ffff",
                        "radius": 8.00,
                        "center": {
                            "latitude": -37.8159544565362,
                            "longitude": 144.9723565578461
                        }
                    }
                ]
            },
            "actions": {
                "messageActions": [
                    {
                        "name" : "A Message Action",
                        "title" : "Welcome to Bluedot Innovation",
                        "message" : "Welcome to Bluedot Innovation Offices"
                    }
                ]
            }
        }
    }
};
 
var options = {
    uri: 'https://api.bluedotinnovation.com/1/zones',
    method: 'POST',
    json: zoneTimeActiveData
};
 
request(options,
    function (error, response, body) {
        if (error) {
            console.log(error);
        }
        console.log(JSON.stringify(response.body));
    }
);
package com.bluedotinnovation.zone;
 
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.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 Complete Zone client demonstrates adding a complete Zone which has fences, beacons and actions
 * to an existing application using Apache HTTP client and JSON Simple libraries.
 */
 
public class AddCompleteZone extends BDCommon {
     
    private static String bdCustomerApiKey    = "86577370-7b91-11e4-bcb7-a0481cdc3311"; //This key is generated by Bluedot Point Access UI when your account is created.
    private static String bdApplicationApiKey = "9beebb18-ec2e-4f30-8e1c-2286c77007d4"; //This apiKey is generated when you create an application
    private static String bdRestUrl       = "https://api.bluedotinnovation.com/1/zones";
 
    public static void main(String[] args) throws ParseException, IOException, KeyManagementException, NoSuchAlgorithmException {
         
        CloseableHttpClient httpRestClient = HttpClients.custom().setSSLSocketFactory(getSSLContextFactory()).build();
         
        JSONParser parser    = new JSONParser();
        JSONObject bdZoneJSONObject = (JSONObject) parser.parse(getJsonCompleteZoneActiveAllDay()); //JSON of Zone which is active during 06:00 AM - 11:00 PM
 
        HttpPost postRequest = new HttpPost(bdRestUrl);
        postRequest.addHeader("content-type", "application/json");
        postRequest.setEntity(new StringEntity(bdZoneJSONObject.toJSONString(), Charset.defaultCharset()));
      
        HttpResponse response = httpRestClient.execute(postRequest);
                         
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("Zone was added successfully");
            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);
        }          
 
    }
 
    /*Return JSON for a complete Zone with a beacon and a custom action which is active for a certain time period during the day.
     * Time values have a format of HH:MM and the period value has to be one of {am/pm}*/
    private static String getJsonCompleteZoneTimeActive() {
        return "{" +
             "\"security\": {" +
                "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\"" +
            "}," +
           "\"content\": {" +
                 "\"zone\": {" +
                    "\"zoneName\": \"First Test Bluedot Zone"\"," +
                    "\"minimumRetriggerTime\": \"11:11\"," +
                    "\"enableCheckOut\": true," +
                    "\"timeActive\" : {" +
                        "\"from\": {" +
                            "\"time\": \"06:00\"," +
                            "\"period\": \"am\"" +
                        "}," +
                        "\"to\": {" +
                            "\"time\": \"11:00\"," +
                            "\"period\": \"pm\"" +
                        "}" +
                    "}," +
                    "\"actions\": {" +
                        "\"customActions\": [" +
                            "{" +
                                "\"name\": \"A Custom Application action\"" +
                            "}" +
                        "]" +
                    "}," +
                    "\"beacons\": [" +
                        "{" +
                           "\"beaconId\":  \"b22ea927-f757-4a8f-8ba7-2960a4b688b2\"," +
                           "\"proximity\": 1," +
                           "\"order\": 1" +
                        "}"+
                    "]"+
                "}" +
            "}" +
        "}";
    }
     
    /*Return JSON for a complete Zone which is active all day with a circular geofence and a message action.*/
    private static String getJsonCompleteZoneActiveAllDay() {
        return "{" +
            "\"security\": {" +
                   "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                   "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\"" +
           "}," +
           "\"content\": {" +
               "\"zone\": {" +
                   "\"zoneName\": \"First Test Bluedot Zone-Updated\"," +
                   "\"minimumRetriggerTime\": \"11:11\"," +
                   "\"enableCheckOut\": true," +
                   "\"activeAllDay\": true," +
                   "\"fences\": {" +
                       "\"circles\": [" +
                           "{"+
                               "\"name\": \"Circle-1\","+
                               "\"color\": \"#000ffff\","+
                               "\"radius\": \"30.330384237225\","+
                               "\"center\": {"+
                                   "\"latitude\": -37.818780,"+
                                   "\"longitude\": 144.980734 "+
                               "},"+
                               "\"order\": 2" +
                           "}" +
                       "]" +
                   "},"+
                   "\"actions\": {" +
                       "\"messageActions\": [" +
                           "{" +
                               "\"name\": \"Welcome to MCG\"," +
                               "\"title\": \"MCG Welcome Message\"," +
                               "\"message\": \"Welcome to MCG\"" +
                           "}" +
                       "]" +
                   "}" +
               "}" +
           "}" +
       "}";
    }
}
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.Web.Script.Serialization;
using System.Security.Cryptography.X509Certificates;
 
/**
* @author Bluedot Innovation
* Copyright (c) 2016 Bluedot Innovation. All rights reserved.
* Add Complete Zone client demonstrates adding a complete Zone which has fences, beacons and actions
*/
namespace BluedotPublicApiClient.zoneclient
{
 
    public class CreateCompleteZone
    {
        private static String bdCustomerApiKey      = "76e1ae30-c616-11e5-a7c0-b8ca3a6b879d"; //This key is generated by Bluedot Point Access UI when your account is created
        private static String bdApplicationApiKey   = "dee11930-ebff-11e5-8e27-bc305bf60831"; //This key is generated by Bluedot Access UI when you register
        private static String bdRestUrl             = "https://api.bluedotinnovation.com/1/zones";
 
        public void createCompleteZoneWithTimeActive()
        {
            postToService(getJsonCompleteZoneTimeActive());
        }
 
        public void createCompleteZoneActiveAllDay()
        {
            postToService(getJsonCompleteZoneActiveAllDay());
        }
 
        private void postToService(String json)
        {
            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 jsonContent = new StringContent(json);
            jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 
            HttpResponseMessage serverResponse = httpRestClient.PostAsync(new Uri(bdRestUrl), jsonContent).Result;
            if (serverResponse.IsSuccessStatusCode)
            {
                var result = serverResponse.Content.ReadAsStringAsync().Result;
                Console.WriteLine("{0}", result);
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)serverResponse.StatusCode, serverResponse.Content.ReadAsStringAsync().Result);
            }
        }
 
        /*Return JSON for a complete Zone with a beacon and a custom action which is active for a certain time period during the day.
         * Time values have a format of HH:MM and the period value has to be one of {am/pm}*/
        private static String getJsonCompleteZoneTimeActive()
        {
            return "{" +
                    "\"security\": {" +
                            "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                            "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\"" +
                "}," +
                "\"content\": {" +
                        "\"zone\": {" +
                        "\"zoneName\": \"First Test Bluedot Zone"\"," +
                        "\"minimumRetriggerTime\": \"11:11\"," +
                        "\"enableCheckOut\": true," +
                        "\"timeActive\" : {" +
                            "\"from\": {" +
                                "\"time\": \"06:00\"," +
                                "\"period\": \"am\"" +
                            "}," +
                            "\"to\": {" +
                                "\"time\": \"11:00\"," +
                                "\"period\": \"pm\"" +
                            "}" +
                        "}," +
                        "\"actions\": {" +
                            "\"customActions\": [" +
                                "{" +
                                    "\"name\": \"A Custom Application action\"" +
                                "}" +
                            "]" +
                        "}," +
                        "\"beacons\": [" +
                            "{" +
                               "\"beaconId\":  \"b22ea927-f757-4a8f-8ba7-2960a4b688b2\"," +
                               "\"proximity\": 1," +
                               "\"order\": 1" +
                            "}" +
                        "]" +
                    "}" +
                "}" +
            "}";
        }
 
        /*Return JSON for a complete Zone which is active all day with a circular geofence and a message action.*/
        private static String getJsonCompleteZoneActiveAllDay()
        {
            return "{" +
                "\"security\": {" +
                            "\"apiKey\":" + "\"" + bdApplicationApiKey + "\"," +
                            "\"customerApiKey\":" + "\"" + bdCustomerApiKey + "\"" +
                "}," +
                "\"content\": {" +
                    "\"zone\": {" +
                        "\"zoneName\": \"First Test Bluedot Zone"\"," +
                        "\"minimumRetriggerTime\": \"11:11\"," +
                        "\"enableCheckOut\": true," +
                        "\"activeAllDay\": true," +
                        "\"fences\": {" +
                           "\"circles\": [" +
                               "{" +
                                   "\"order\": 1," +
                                   "\"name\": \"Circle-1\"," +
                                   "\"color\": \"#000ffff\"," +
                                   "\"radius\": \"30.330384237225\"," +
                                   "\"center\": {" +
                                       "\"latitude\": -37.818780," +
                                       "\"longitude\": 144.980734 " +
                                   "}" +
                               "}" +
                           "]" +
                       "}," +
                       "\"actions\": {" +
                           "\"messageActions\": [" +
                               "{" +
                                   "\"name\": \"Welcome to MCG\"," +
                                   "\"title\": \"MCG Welcome Message\"," +
                                   "\"message\": \"Welcome to MCG\"" +
                               "}" +
                           "]" +
                       "}" +
                    "}" +
                "}" +
            "}";
        }
    }
}
Created by Bluedot DevOps on March 3, 2018

Start the discussion