4 Replies Latest reply on May 22, 2015 12:47 PM by Marcos Vainer Loeff

    Current video about how's to work or set the PI web API 2015.

    jose_dai

      Hi,

       

      I'm a new user for WEB API, so I'm looking for a video where explain how to start to work or set  web API or some code examples where I can get data from PI point.

       

      Regards.

        • Re: Current video about how's to work or set the PI web API 2015.
          Eugene Lee

          We currently do not yet have a video on PI Web API in the Learning Channel. I will recommend these resources instead.

           

          Getting started with PI Web API

          Builders's Café Webinar Series: Preview of PI Web API

           

          What language are you going to use PI Web API with?

          • Re: Current video about how's to work or set the PI web API 2015.
            jose_dai

            Eugene, We'll use Java code. The project is the following. The user will create an application in java for getting information from Pi points(current values or archive values) using web api and put this information into the private cloud for analysis.

             

            Do you have some examples using java code for getting the current and archive values?

             

            Thanks in advance.

              • Re: Current video about how's to work or set the PI web API 2015.
                Marcos Vainer Loeff

                Hello Jose,

                 

                I have written some Java code to communicate with older version of PI Web API. Although probably it needs to be updated in order to work properly, I guess you will find it pretty useful. Before you start to code, you will need to download:

                 

                • Apache HttpComponents project libraries à this project is responsible for creating and maintaining a toolset of low level Java components focused on HTTP and associated protocols. Click here to go to their web site and download their libraries.
                • Json simple à it is a simple Java toolkit for JSON. You can use JSON.simple to encode or decode JSON text. Click here to download.

                 

                Using Eclipse, create a Java project with two files: Application.java and PIWebAPIWrapper.java. The source code from both files are below:

                 

                PIWebAPIWrapper.java

                 

                import java.io.IOException;
                import java.io.UnsupportedEncodingException;
                import java.net.URLEncoder;
                import java.util.ArrayList;
                import java.util.List;
                import org.apache.http.*;
                import org.apache.http.client.*;
                import org.apache.http.client.methods.HttpGet;
                import org.apache.http.client.methods.HttpPost;
                import org.apache.http.entity.StringEntity;
                import org.apache.http.impl.client.HttpClientBuilder;
                import org.apache.http.util.EntityUtils;
                import org.json.simple.JSONArray;
                import org.json.simple.JSONObject;
                import org.json.simple.parser.JSONParser;
                
                
                public class PIWebAPIWrapper {
                  private static String base_url = "https://osi-serv.osibr.com/piwebapi/";
                
                
                  public static List<String> GetCitiesName() {
                  List<String> items = new ArrayList<String>();
                  String url = base_url
                  + "elements/E07dtyl5PO4EmFExFPTz6FbghNYw-7Q65Uav8IhHRRw_WQTUFSQy1QSTIwMTRcQUZQQVJUTkVSQ09VUlNFV0VBVEhFUlxDSVRJRVM/elements";
                  JSONArray citiesItems = (JSONArray) MakeGetRequest(url).get("Items");
                  for (int i = 0; i < citiesItems.size(); i++) {
                  JSONObject myCity = (JSONObject) citiesItems.get(i);
                  items.add(myCity.get("Name").toString());
                  }
                  return items;
                  }
                
                
                  public static List<String> GetAttributesName() {
                  List<String> items = new ArrayList<String>();
                  String url = base_url
                  + "elementtemplates/T07dtyl5PO4EmFExFPTz6FbgSnYaLt6aAkqQMOBOaLJPKQTUFSQy1QSTIwMTRcQUZQQVJUTkVSQ09VUlNFV0VBVEhFUlxFTEVNRU5UVEVNUExBVEVTW0NJVElFUyBURU1QTEFURV0/attributetemplates";
                  JSONArray citiesItems = (JSONArray) MakeGetRequest(url).get("Items");
                  for (int i = 0; i < citiesItems.size(); i++) {
                  JSONObject myCity = (JSONObject) citiesItems.get(i);
                  items.add(myCity.get("Name").toString());
                  }
                  return items;
                  }
                
                
                  public static int SendValue(String cityName, String attributeName,
                  String value) {
                  String attributePath = "\\\\MARC-PI2014\\AFPartnerCourseWeather\\Cities\\"
                  + cityName + "|" + attributeName;
                  String url = null;
                  try {
                  url = base_url + "attributes?path="
                  + URLEncoder.encode(attributePath, "UTF-8");
                  } catch (UnsupportedEncodingException e) {
                  e.printStackTrace();
                  }
                  JSONObject linkItems = (JSONObject) MakeGetRequest(url).get("Links");
                  String url_to_send = linkItems.get("Value").toString();
                  try {
                  return MakePostRequest(url_to_send, value);
                  } catch (IOException e) {
                
                
                  e.printStackTrace();
                  return -1;
                  }
                  }
                
                
                
                
                  private static JSONObject MakeGetRequest(String url) {
                  JSONObject myJSONObject = null;
                  try {
                  HttpClient client = HttpClientBuilder.create().build();
                  HttpGet request = new HttpGet(url);
                  HttpResponse response = client.execute(request);
                  HttpEntity entity = response.getEntity();
                  String responseString = EntityUtils.toString(entity);
                
                
                  JSONParser myJSONParser = new JSONParser();
                  myJSONObject = (JSONObject) myJSONParser.parse(responseString);
                
                
                  } catch (Exception ex) {
                
                
                  }
                  return myJSONObject;
                  }
                
                
                  private static int MakePostRequest(String url, String value)
                  throws ClientProtocolException, IOException {
                  HttpClient client = HttpClientBuilder.create().build();
                  HttpPost post = new HttpPost(url);
                  post.setHeader("Content-Type", "application/json");
                  post.setEntity(new StringEntity("{\"Value\":" + value + "}"));
                  HttpResponse response = client.execute(post);
                  int responseCode = response.getStatusLine().getStatusCode();
                  return responseCode;
                  }
                }
                
                
                
                
                

                 

                 

                 

                Application.java

                 

                import java.util.List;
                
                
                public class Application {
                  public static void main(String[] args) 
                  {
                  List<String> citiesNames = PIWebAPIWrapper.GetCitiesName();
                    List<String> attributesNames = PIWebAPIWrapper.GetAttributesName();
                    String value = "1320";
                    int statusCode = PIWebAPIWrapper.SendValue(citiesNames.get(0), attributesNames.get(1), value);
                  
                     if (statusCode == 202)
                     {
                      System.out.println("Success!");
                     }
                     else
                     {
                      System.out.println("Error");
                     }
                  }
                }
                

                 

                 

                On this example, I am updating a value from an attribute by using the POST method. If I receive a 202 status code back, it means that the operation was done successfully. Receiving other value means that there was an error running this operation.

                 

                Hope this helps you to get started!