Introduction

If you’d like to use BioStar 2 New Local API as a RESTful API for your own customization or other purposes, you can simply reference this article. This article will cover the most basic usage of the BioStar 2 New Local API by C# console application. 

 

This is just a sample application made for those who might need to use the BioStar 2 New Local API. It is a Visual C# console application.

 

Features

This article will cover the functions below:

  1. Login
  2. Search Users
  3. Create Users 
  4. Create Users with Access Group
  5. Retrieve Log Data
  6. Retrieve Log Data with order by

 

Through this article, you can learn how to call an API function to retrieve the log data within the period of time from and to the time of your choice. In addition, you’ll also have to clarify the number of the logs you’d like to retrieve in the request body of the call.

 

Make sure you take a good look at the example code of the C# program built for API calls. You can simply copy & paste the source code to use the same function in your own integration. 

 

You can also see how to call the API functions via Postman, which is a program used for RESTful API calls.

 

Part 1. API Call & Parameters 

 

[POST]: /events/search

[Parameters] 

Name

Type

*M/O

Explanation

 

 

 

limit

integer

O

# of results you want to search

conditions-columnstring    OKeyword for searching
['datetime','device_id.id','door_id.id','event_type_id.code',
'user_group_id.id','user_id.user_id']

Conditions-values

String Array

O

Two DateTime array for event period, the end time must be greater than the start time. *Datetime in UTC in ISO-8601 format (e.g. 2015-06-10T02:14:05.268Z )

Conditions-operator

integer

O

desc: Condition Operator[

EQUAL : 0,

NOT_EQUAL : 1,

CONTAINS : 2,

BETWEEN : 3,

LIKE : 4,

GREATER : 5,

LESS : 6]

  * M – Mandatory, O – Optional

 

Part 2. Request Body & Response Model

[Example Value/Parameters Model]

{

       "Query": {

        "limit": 100,

           "conditions": [

               {

                "column": "datetime",

                "operator": 3,

                "values": [

                       "2021-08-01T15:00:00.000Z",

                       "2021-08-02T14:59:59.000Z"

                ]

               }

           ]

    }

}

*"limit" : 100 indicates that you’d only see 100 logs in the response as a result of the API call. You can set this value accordingly since retrieving a large amount of data can cause delays or server crashes. (Recommended Max. limit: 2,000)

*"column" : DateTime indicates that you’d sort the logs by the DateTime value of the logs. For example, you’ll only see the logs from 2021-08-01T15:00:00.000Z to 2021-08-02T14:59:59.000Z.

*If you change it to "column" : id or other values, you can sort the logs by their id or other values accordingly.

*"column" : "hint" can be used in conditions in order to bring consecutive logs from a certain log with the input hint value. For instance, if you know certain log's hint value(from searching via event/search API) and would like to retrieve next 100 logs, you can set the limit/operator/values value accordingly and retrieve the next 100 logs starting from the log that has the input hint value.

 

[Response: Successful]

{

       "EventCollection": {

           "rows": [

               {

                "id": "133",

                "server_datetime": "2021-08-02T13:08:27.00Z",

                "datetime": "2021-08-02T04:08:30.00Z",

                "index": "346",

                "user_group_id": {

                    "id": "0",

                    "name": ""

                   },

                "device_id": {

                    "id": "939271697",

                    "name": "BioStation A2 939271697 (192.168.13.132)"

                },

                "event_type_id": {

                    "code": "20736"

                },

                "door_id": [

                    {

                        "id": "4",

                        "name": "Door1"

                    }

                ],

                "is_dst": "0",

                "timezone": {

                    "half": "0",

                    "hour": "9",

                    "negative": "0"

                },

                   "user_update_by_device": "false",

                "hint": "162787731009392716970000000346",

                "user_id": {

                    "photo_exists": "false"

                }

               },

.

*hint: an index value in each table(monthly log) in the AC database. Starting from the 1st log data in the table, the number increase by 1 for each new log data. 


*With 'hint', you can comprehend that certain logs come after/before some logs since you can compare them.


"server_datetime": "2021-08-02T13:08:27.00Z",  => Means Timestamp when Biostar get logs from Device

 "datetime": "2021-08-02T04:08:30.00Z",  => Means Timestamp when event occurs on device in UTC

 

Part 3. Console Retrieve Logs Example

 

[Retrieve Logs Method Source Code] 

static async void GetLogTask()

        {

            if(sessionID == null)

            {

                Console.WriteLine("You must log in first!");

                return;

            }

 

            CookieContainer cookieContainer = new CookieContainer();

 

            HttpClientHandler handler = new HttpClientHandler();

            handler.CookieContainer = cookieContainer;

 

            HttpClient httpClient = new HttpClient(handler);

 

               HttpClient client = new HttpClient(handler);

            httpClient.DefaultRequestHeaders.Add("bs-session-id", sessionID);

            cookieContainer.Add(new Uri("https://127.0.0.1"), new Cookie("bs-session-id", sessionID));

            string resourceAddress = "https://127.0.0.1/api/events/search";

            Console.WriteLine("How many results do you want to see?");

            string limit = Console.ReadLine();

            string startTime = "1970-01-01T00:00:00Z";

            string endTime = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");

 

            DateTime dtLatestLogTime = new DateTime(1970, 1, 1);

 

            JavaScriptSerializer serializer = new JavaScriptSerializer();

                

            string payload2 = "{ \"Query\": { \"limit\": " + limit + ", \"conditions\": [  { \"column\": \"datetime\",     \"operator\": 3, \"values\": [  \"" + startTime + "\", \"" + endTime + "\" ] } ] }}";

            Console.WriteLine(payload2);

 

            StringContent sc = new StringContent(payload2, Encoding.UTF8, "application/json"); 

            HttpResponseMessage httpResponse = httpClient.PostAsync(resourceAddress, sc).Result;

 

            if (httpResponse.IsSuccessStatusCode == true)

               {

                Console.WriteLine("Succeeded to retrieve log from " + startTime + " to " + endTime);

                string httpResponseBody = await httpResponse.Content.ReadAsStringAsync();

                //Console.WriteLine(httpResponseBody);

                dynamic obj = JsonConvert.DeserializeObject(httpResponseBody);

                Console.WriteLine("************** LOG("+limit+") : **************");

                Console.WriteLine("** LogID ********* DateTime *********** DeviceID ******************** DeviceName ********************* EventCode *****");

                for (int i = 0; i < Int16.Parse(limit); i++)

                {

                       Console.WriteLine(obj.EventCollection.rows[i].id + "        " + obj.EventCollection.rows[i].datetime + "        " + obj.EventCollection.rows[i].device_id.id + "        " + obj.EventCollection.rows[i].device_id.name + "        " + obj.EventCollection.rows[i].event_type_id.code);

                }

                System.Threading.Thread.Sleep(1000);

            }

            else

            {

                Console.WriteLine("Log Retrieval Failed from " + startTime + " to " + endTime);

                   Console.WriteLine(httpResponse.ToString());

            }

            

        }

 

[After Retrieve Logs Successfully: only managed to see a few values of the logs] 

 

 

 

 

Part 4. Retrieve Logs via Postman

 

[Request Example: Headers] 

*You must use the ‘be-session-id’ value from the response header of the Login API call to authenticate API use for other API calls.

텍스트, 스크린샷, 모니터, 화면이(가) 표시된 사진

자동 생성된 설명

[Request Example] 

 

 

[Response Example: body] 

텍스트이(가) 표시된 사진

자동 생성된 설명

 



FAQ

1. How can we know the event type?

You can call GET api/event_types as follows.


https://bs2api.biostar2.com/#d54843e3-6ce2-4ddc-94f4-0da88334c304

GETView Event Types

https://127.0.0.1/api/event_types?is_break_glass=false&setting_alert=false&setting_all=true

This API is used for listing event types recorded on BioStar 2 database.