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-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.

*"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 its id or other values accordingly.

 

[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"

                }

               },

.

 

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] 

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

자동 생성된 설명