POST Action with Condition – Client examples

/**
 * @author Bluedot Innovation
 * Copyright (c) 2016 Bluedot Innovation. All rights reserved.
 * Create Action with Condition client demonstrates adding the different types of conditions to an action of an existing zone
 * using the 'request' node module.
 */
var request = require('request');
var percentageCrossedData =
{
    "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",
            "actions": {
                "customActions": [
                    {
                        "name" "A Custom Action with Percentage Crossed Condition",
                        "conditions": {
                            "percentageCrossed": [
                                {
                                    "percentage": 45,
                                    /* Time in Hour:Minute format.*/
                                    "timeoutPeriod""00: 01"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
};
var sequentialData =
{
    "security": {
        "apiKey" "c2c8211f-796f-4eda-b6ce-05467b5263a9",
        "customerApiKey""86577370-7b91-11e4-bcb7-a0481cdc3311"
    },
    "content": {
        "zone": {
            "zoneId""722c991a-3ebb-4143-b441-5b0c0cf680a0",
            "actions": {
                "customActions": [
                    {
                        "name""A Custom Action with a Sequential Percentage Crossed Condition",
                        "conditions": {
                            "percentageCrossed": [
                                {
                                    "percentage": 45,
                                    /* Time in Hour:Minute format.*/
                                    "timeoutPeriod""00: 01",
                                    "sequential"true
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
};
var dateRangeData =
{
    "security": {
        "apiKey" "c2c8211f-796f-4eda-b6ce-05467b5263a9",  
        "customerApiKey""86577370-7b91-11e4-bcb7-a0481cdc3311"
    },
    "content": {
        "zone": {
            "zoneId""722c991a-3ebb-4143-b441-5b0c0cf680a0",
            "actions": {
                "customActions": [
                    {
                        "name""A Custom Action with a Date Condition",
                        "conditions": {
                            "dateRange": [
                                {
                                    "start""01/10/2016",
                                    "end""01/12/2017"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
};
var timeActiveData =
{
    "security": {
        "apiKey" "c2c8211f-796f-4eda-b6ce-05467b5263a9",
        "customerApiKey""86577370-7b91-11e4-bcb7-a0481cdc3311"
    },
    "content": {
        "zone": {
            "zoneId""722c991a-3ebb-4143-b441-5b0c0cf680a0",
            "actions": {
                "customActions": [
                    {
                        "name""A Custom Action with a Time Condition",
                        "conditions": {
                            "timeActive": [
                               {
                                   "from": {
                                       /* Time in Hour:Minute format.*/
                                       "time""10:00",
                                       "period""am"
                                   },
                                   "to": {
                                       /* Time in Hour:Minute format.*/
                                       "time""02:00",
                                       "period""pm"
                                   }
                               }
                           ]
                        }
                    }
                ]
            }
        }
    }
};
var bearingData =
{
    "security": {
        "apiKey" "c2c8211f-796f-4eda-b6ce-05467b5263a9",   
        "customerApiKey""86577370-7b91-11e4-bcb7-a0481cdc3311"
    },
    "content": {
        "zone": {
            "zoneId""722c991a-3ebb-4143-b441-5b0c0cf680a0",
            "actions": {
                "customActions": [
                    {
                        "name""A Custom Action with a Bearing Condition",
                        "conditions": {
                            "bearing": [
                                {
                                    "fromAngle": 80,
                                    "toAngle": 200
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
};
var speedData =
{
    "security": {
        "apiKey" "c2c8211f-796f-4eda-b6ce-05467b5263a9",   
        "customerApiKey""86577370-7b91-11e4-bcb7-a0481cdc3311"
    },
    "content": {
        "zone": {
            "zoneId""722c991a-3ebb-4143-b441-5b0c0cf680a0",
            "actions": {
                "customActions": [
                    {
                        "name""A Custom Action with a Speed Condition",
                        "conditions": {
                            "speed": [
                                {
                                    "minimumSpeed": 10,
                                    "maximumSpeed": 30
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
};
var options = {
    method: 'POST',
    json: sequentialData
};
request(options,
    function (error, response, body) {
        if (error) {
            console.log(error);
        }
        console.log(JSON.stringify(response.body));
    }
);
package com.bluedotinnovation.action;
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 Action with conditions client demonstrates adding an action with different types of conditions to an existing zone
 * using JSON simple and Apache HTTP client libraries.
 */
public class AddActionWithConditions 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/actions";
    
    /**
     * @param args
     * @throws IOException
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    public static void main(String[] args) throws  IOException, ParseException, KeyManagementException, NoSuchAlgorithmException {
        
        CloseableHttpClient httpRestClient = HttpClients.custom().setSSLSocketFactory(getSSLContextFactory()).build();
        
        HttpPost postRequest = new HttpPost(bdRestUrl);
  
        JSONParser parser    = new JSONParser();
        JSONObject bdMessageActionWithCondtionsJSONObject = (JSONObject) parser.parse(getJsonActionWithSequentialCondition()); //action with sequential condition json
                                
        postRequest.addHeader("content-type""application/json");
        postRequest.setEntity(new StringEntity(bdMessageActionWithCondtionsJSONObject.toJSONString(), Charset.defaultCharset()));
     
        HttpResponse response = httpRestClient.execute(postRequest);
                        
        if (response.getStatusLine().getStatusCode() == 200) {
            System.out.println("Message action and conditions were added to your zone 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);
        }          
    }
    /* Example of Custom Action with Percentage Crossed condition. */
    private static String getJsonActionWithPercentageCrossedCondition() {
        
        String customActionWithPercentageCrossedCondtionJson =
             "{" +
                "\"security\": {" +
                    "\"apiKey\":" "\"" + bdApplicationApiKey +"\"," +
                    "\"customerApiKey\":" +"\"" + bdCustomerApiKey + "\""+
                "}," +
                "\"content\": {" +
                    "\"zone\": {" +
                        "\"zoneId\":""\"" + bdZoneId +"\"," +
                        "\"actions\": {" +
                        "\"customActions\": [" +
                            "{" +
                                "\"name\": \"A Custom Action with Percentage Crossed Condition\"," +
                                "\"conditions\": {" +
                                   "\"percentageCrossed\": [" +
                                      "{" +
                                          "\"percentage\": 90," +
                                          "\"timeoutPeriod\": \"00:15\"" +
                                       "}" +
                                  "]" +
                               "}" +
                            "}" +
                        "]" +
                      "}" +
                    "}" +
                "}" +
            "}";
        return customActionWithPercentageCrossedCondtionJson;
    }
    
    /* Example of Custom Action with Sequential condition. */
    private static String getJsonActionWithSequentialCondition() {
        
        String customActionWithSequentialCondtionJson =
             "{" +
                "\"security\": {" +
                    "\"apiKey\":" "\"" + bdApplicationApiKey +"\"," +
                    "\"customerApiKey\":" +"\"" + bdCustomerApiKey + "\""+
                "}," +
                "\"content\": {" +
                    "\"zone\": {" +
                        "\"zoneId\":""\"" + bdZoneId +"\"," +
                        "\"actions\": {" +
                        "\"customActions\": [" +
                            "{" +
                                "\"name\": \"A Custom Action with a Sequential Percentage Crossed Condition\"," +
                                "\"conditions\": {" +
                                   "\"percentageCrossed\": [" +
                                      "{" +
                                          "\"percentage\": 90," +
                                          "\"timeoutPeriod\": \"00:15\"," +
                                          "\"sequential\": true" +
                                       "}" +
                                  "]" +
                               "}" +
                            "}" +
                        "]" +
                      "}" +
                    "}" +
                "}" +
            "}";
        return customActionWithSequentialCondtionJson;
    }
    
    /* Example of Custom Action with Date Range condition. */
    private static String getJsonActionWithDateRangeCondition() {
        
        String customActionWithDateRangeCondtionJson =
             "{" +
                "\"security\": {" +
                    "\"apiKey\":" "\"" + bdApplicationApiKey +"\"," +
                    "\"customerApiKey\":" +"\"" + bdCustomerApiKey + "\""+
                "}," +
                "\"content\": {" +
                    "\"zone\": {" +
                        "\"zoneId\":""\"" + bdZoneId +"\"," +
                        "\"actions\": {" +
                        "\"customActions\": [" +
                            "{" +
                                "\"name\": \"A Custom Action with a Date Condition\"," +
                                "\"conditions\": {" +
                                "\"dateRange\": [" +
                                    "{" +
                                        "\"start\": \"10/11/2016\"," +
                                        "\"end\": \"26/12/2017\"" +
                                    "}" +
                                "]" +
                               "}" +
                            "}" +
                        "]" +
                      "}" +
                    "}" +
                "}" +
            "}";
        return customActionWithDateRangeCondtionJson;
    }
    
    /* Example of Custom Action with Time Active condition. */
    private static String getJsonActionWithTimeActiveCondition() {
        
        String customActionWithTimeActiveCondtionJson =
             "{" +
                "\"security\": {" +
                    "\"apiKey\":" "\"" + bdApplicationApiKey +"\"," +
                    "\"customerApiKey\":" +"\"" + bdCustomerApiKey + "\""+
                "}," +
                "\"content\": {" +
                    "\"zone\": {" +
                        "\"zoneId\":""\"" + bdZoneId +"\"," +
                        "\"actions\": {" +
                        "\"customActions\": [" +
                            "{" +
                                "\"name\": \"A Custom Action with a Time Condition\"," +
                                "\"conditions\": {" +
                                    "\"timeActive\": [{" +
                                        "\"from\": {" +
                                            "\"time\": \"06:01\"," +
                                            "\"period\": \"am\" " +
                                        "}," +
                                        "\"to\": {" +
                                            "\"time\": \"11:00\"," +
                                            "\"period\": \"pm\" " +
                                        "}" +
                                    "}]" +
                               "}" +
                            "}" +
                        "]" +
                      "}" +
                    "}" +
                "}" +
            "}";
        return customActionWithTimeActiveCondtionJson;
    }
    
    /* Example of Custom Action with Bearing condition. */
    private static String getJsonActionWithBearingCondition() {
        
        String customActionWithBearingCondtionJson =
             "{" +
                "\"security\": {" +
                    "\"apiKey\":" "\"" + bdApplicationApiKey +"\"," +
                    "\"customerApiKey\":" +"\"" + bdCustomerApiKey + "\""+
                "}," +
                "\"content\": {" +
                    "\"zone\": {" +
                        "\"zoneId\":""\"" + bdZoneId +"\"," +
                        "\"actions\": {" +
                        "\"customActions\": [" +
                            "{" +
                                "\"name\": \"A Custom Action with a Time Condition\"," +
                                "\"conditions\": {" +
                                "\"bearing\": [" +
                                    "{" +
                                        "\"fromAngle\": 0," +
                                        "\"toAngle\": 90" +
                                    "}" +
                                "]" +
                               "}" +
                            "}" +
                        "]" +
                      "}" +
                    "}" +
                "}" +
            "}";
        return customActionWithBearingCondtionJson;
    }
    
    /* Example of Custom Action with Speed condition. */
    private static String getJsonActionWithSpeedCondition() {
        
        String customActionWithSpeedCondtionJson =
             "{" +
                "\"security\": {" +
                    "\"apiKey\":" "\"" + bdApplicationApiKey +"\"," +
                    "\"customerApiKey\":" +"\"" + bdCustomerApiKey + "\""+
                "}," +
                "\"content\": {" +
                    "\"zone\": {" +
                        "\"zoneId\":""\"" + bdZoneId +"\"," +
                        "\"actions\": {" +
                        "\"customActions\": [" +
                            "{" +
                                "\"name\": \"A Custom Action with a Time Condition\"," +
                                "\"conditions\": {" +
                                "\"speed\": [" +
                                    "{" +
                                        "\"minmumSpeed\": 10," +
                                        "\"maximumSpeed\": 20" +
                                    "}" +
                                "]" +
                               "}" +
                            "}" +
                        "]" +
                      "}" +
                    "}" +
                "}" +
            "}";
        return customActionWithSpeedCondtionJson;
    }
}
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 Action with conditions client demonstrates adding an action with different types of conditions to an existing zone
 * using .net http web api libray
 */
namespace BluedotPublicApiClient.actionclient
{
    public class CreateActionWithCondition
    {
        private static String bdCustomerApiKey    = "a6598740-75f5-11e4-86ca-a0481cdc3311"//This key is generated by Bluedot Point Access UI when your account is created.
        private static String bdApplicationApiKey = "5a9b1b78-3dd7-4f4d-8608-82408f3baf4c"//This apiKey is generated when you create an application
        private static String bdZoneId            = "b80e50eb-b9f9-4ed5-966e-4b2e39cc0549"//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/actions";
        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);
            }
        }
        public void addPercenatgeCrossedCondition()
        {
            postToService(getJsonActionWithPercentageCrossedCondition());
        }
        public void addSequentialCondition()
        {
            postToService(getJsonActionWithSequentialCondition());
        }
        public void addDateRangeCondition()
        {
            postToService(getJsonActionWithDateRangeCondition());
        }
        public void addTimeActiveCondition()
        {
            postToService(getJsonActionWithTimeActiveCondition());
        }
        public void addBearingCondition()
        {
            postToService(getJsonActionWithBearingCondition());
        }
        public void addSpeedCondition()
        {
            postToService(getJsonActionWithSpeedCondition());
        }
        /* Example of Custom Action with Percentage Crossed condition. */
        private static String getJsonActionWithPercentageCrossedCondition()
        {
            String customActionWithPercentageCrossedCondtionJson =
                 "{" +
                    "\"security\": {" +
                        "\"apiKey\":" "\"" + bdApplicationApiKey + "\"," +
                        "\"customerApiKey\":" "\"" + bdCustomerApiKey + "\"" +
                    "}," +
                    "\"content\": {" +
                        "\"zone\": {" +
                            "\"zoneId\":" "\"" + bdZoneId + "\"," +
                            "\"actions\": {" +
                            "\"customActions\": [" +
                                "{" +
                                    "\"name\": \"A Custom Action with Percentage Crossed Condition\"," +
                                    "\"conditions\": {" +
                                       "\"percentageCrossed\": [" +
                                          "{" +
                                              "\"percentage\": 90," +
                                              "\"timeoutPeriod\": \"00:15\"" +
                                           "}" +
                                      "]" +
                                   "}" +
                                "}" +
                            "]" +
                          "}" +
                        "}" +
                    "}" +
                "}";
            return customActionWithPercentageCrossedCondtionJson;
        }
        /* Example of Custom Action with Sequential condition. */
        private static String getJsonActionWithSequentialCondition()
        {
            String customActionWithSequentialCondtionJson =
                 "{" +
                    "\"security\": {" +
                        "\"apiKey\":" "\"" + bdApplicationApiKey + "\"," +
                        "\"customerApiKey\":" "\"" + bdCustomerApiKey + "\"" +
                    "}," +
                    "\"content\": {" +
                        "\"zone\": {" +
                            "\"zoneId\":" "\"" + bdZoneId + "\"," +
                            "\"actions\": {" +
                            "\"customActions\": [" +
                                "{" +
                                    "\"name\": \"A Custom Action with a Sequential Percentage Crossed Condition\"," +
                                    "\"conditions\": {" +
                                       "\"percentageCrossed\": [" +
                                          "{" +
                                              "\"percentage\": 90," +
                                              "\"timeoutPeriod\": \"00:15\"," +
                                              "\"sequential\": true" +
                                           "}" +
                                      "]" +
                                   "}" +
                                "}" +
                            "]" +
                          "}" +
                        "}" +
                    "}" +
                "}";
            return customActionWithSequentialCondtionJson;
        }
        /* Example of Custom Action with Date Range condition. */
        private static String getJsonActionWithDateRangeCondition()
        {
            String customActionWithDateRangeCondtionJson =
                 "{" +
                    "\"security\": {" +
                        "\"apiKey\":" "\"" + bdApplicationApiKey + "\"," +
                        "\"customerApiKey\":" "\"" + bdCustomerApiKey + "\"" +
                    "}," +
                    "\"content\": {" +
                        "\"zone\": {" +
                            "\"zoneId\":" "\"" + bdZoneId + "\"," +
                            "\"actions\": {" +
                            "\"customActions\": [" +
                                "{" +
                                    "\"name\": \"A Custom Action with a Date Condition\"," +
                                    "\"conditions\": {" +
                                    "\"dateRange\": [" +
                                        "{" +
                                            "\"start\": \"10/11/2016\"," +
                                            "\"end\": \"26/12/2017\"" +
                                        "}" +
                                    "]" +
                                   "}" +
                                "}" +
                            "]" +
                          "}" +
                        "}" +
                    "}" +
                "}";
            return customActionWithDateRangeCondtionJson;
        }
        /* Example of Custom Action with Time Active condition. */
        private static String getJsonActionWithTimeActiveCondition()
        {
            String customActionWithTimeActiveCondtionJson =
                 "{" +
                    "\"security\": {" +
                        "\"apiKey\":" "\"" + bdApplicationApiKey + "\"," +
                        "\"customerApiKey\":" "\"" + bdCustomerApiKey + "\"" +
                    "}," +
                    "\"content\": {" +
                        "\"zone\": {" +
                            "\"zoneId\":" "\"" + bdZoneId + "\"," +
                            "\"actions\": {" +
                            "\"customActions\": [" +
                                "{" +
                                    "\"name\": \"A Custom Action with a Time Condition\"," +
                                    "\"conditions\": {" +
                                        "\"timeActive\": [{" +
                                            "\"from\": {" +
                                                "\"time\": \"06:01\"," +
                                                "\"period\": \"am\" " +
                                            "}," +
                                            "\"to\": {" +
                                                "\"time\": \"11:00\"," +
                                                "\"period\": \"pm\" " +
                                            "}" +
                                        "}]" +
                                   "}" +
                                "}" +
                            "]" +
                          "}" +
                        "}" +
                    "}" +
                "}";
            return customActionWithTimeActiveCondtionJson;
        }
        /* Example of Custom Action with Bearing condition. */
        private static String getJsonActionWithBearingCondition()
        {
            String customActionWithBearingCondtionJson =
                 "{" +
                    "\"security\": {" +
                        "\"apiKey\":" "\"" + bdApplicationApiKey + "\"," +
                        "\"customerApiKey\":" "\"" + bdCustomerApiKey + "\"" +
                    "}," +
                    "\"content\": {" +
                        "\"zone\": {" +
                            "\"zoneId\":" "\"" + bdZoneId + "\"," +
                            "\"actions\": {" +
                            "\"customActions\": [" +
                                "{" +
                                    "\"name\": \"A Custom Action with a Time Condition\"," +
                                    "\"conditions\": {" +
                                    "\"bearing\": [" +
                                        "{" +
                                            "\"fromAngle\": 0," +
                                            "\"toAngle\": 90" +
                                        "}" +
                                    "]" +
                                   "}" +
                                "}" +
                            "]" +
                          "}" +
                        "}" +
                    "}" +
                "}";
            return customActionWithBearingCondtionJson;
        }
        /* Example of Custom Action with Speed condition. */
        private static String getJsonActionWithSpeedCondition()
        {
            String customActionWithSpeedCondtionJson =
                 "{" +
                    "\"security\": {" +
                        "\"apiKey\":" "\"" + bdApplicationApiKey + "\"," +
                        "\"customerApiKey\":" "\"" + bdCustomerApiKey + "\"" +
                    "}," +
                    "\"content\": {" +
                        "\"zone\": {" +
                            "\"zoneId\":" "\"" + bdZoneId + "\"," +
                            "\"actions\": {" +
                            "\"customActions\": [" +
                                "{" +
                                    "\"name\": \"A Custom Action with a Time Condition\"," +
                                    "\"conditions\": {" +
                                    "\"speed\": [" +
                                        "{" +
                                            "\"minmumSpeed\": 10," +
                                            "\"maximumSpeed\": 20" +
                                        "}" +
                                    "]" +
                                   "}" +
                                "}" +
                            "]" +
                          "}" +
                        "}" +
                    "}" +
                "}";
            return customActionWithSpeedCondtionJson;
        }
    }
}
Created by Bluedot DevOps on May 3, 2018