Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:local:remoteapi1504:codesamples:update_datafalags [20/05/2014 11:49]
en:local:remoteapi1504:codesamples:update_datafalags [20/05/2014 11:49] (current)
Line 1: Line 1:
 +{{indexmenu_n>​3}}
 +====== Item events management ======
  
 +Item events management implies 2 steps. Firstly, we choose types of events to be received, and then, events are received and handled.
 +
 +//:!: Attention!//​ To launch this example at Wialon Kit, change **<​nowiki>​https://​hst-api.wialon.com</​nowiki>​** for **<​nowiki>​https://​kit-api.wialon.com</​nowiki>​** in all requests and use user name and password of your account to login to the system or name and password of demo user //**kitdemo kitdemo**//​.
 +
 +
 +===== Adding items to the session =====
 +
 +Watching item events is one of the most important feature of Wialon. To [[../​apiref:​requests:​avl_evts|get events from items]], these items must be added to the session.
 +
 +There are three ways to add items to the session: by type, by list of IDs and by ID.
 +
 +For example, you need to add three units to the session -- Bavarian Tractor, Alabama, and Touareg. Since their IDs are known, we will add these units using the value "​col"​ for the parameter //type//:
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=core/​update_data_flags&​
 + params={
 + "​spec":​[
 + {
 + "​type":"​col",​
 + "​data":​[34868,​22800,​553632],​
 + "​flags":​1,​
 + "​mode":​0 /​* replacing flags */
 + }
 + ]
 + }&​sid=<​your_sid>​
 +</​code>​
 +
 +Since only the base flag is set, we will be able to get events only about renaming of these units. ​
 +
 +//:!: Attention!//​ The value of the parameter //mode// set to 0 means that all previous flags of the item will be replaced by ones in the request.
 +
 +Response:
 +<code javascript>​
 +[{
 +    "​i":​ 34868,
 +    "​d":​ null,
 +    "​f":​ 1
 +}, {
 +    "​i":​ 22800,
 +    "​d":​ null,
 +    "​f":​ 1
 +}, {
 +    "​i":​ 553632,
 +    "​d":​ null,
 +    "​f":​ 1
 +}]
 +</​code>​
 +
 +If we don't need to get events from the unit "​Touareg"​ any more, this unit can be removed from the session. Since we need to operate only with one unit, we set the value "​id"​ for the parameter //type// in the request:
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=core/​update_data_flags&​
 + params={
 + "​spec":​[{
 + "​type":"​id",​
 + "​data":​553632,​
 + "​flags":​1,​
 + "​mode":​2 /​* removing flags */
 + }]
 + }&​sid=<​your_sid>​
 +</​code>​
 +
 +//:!: Attention!//​ For complete removal of a unit from the session, set the parameter //flags// as 1. The point is we can stop getting any messages from a unit only if we make a request for removal of the base flag. For example, if while adding a unit we specify flag 3 and while removing we specify flag 2, then flag 1 still remains after removal. So, we won't be able to get events for which flag 2 is responsible,​ but we'll still be able to get events of flag 1.
 +
 +Response:
 +<code javascript>​
 +[{
 +    "​i":​ 553632,
 +    "​d":​ null,
 +    "​f":​ 0
 +}]
 +</​code>​
 +
 +The value "​0"​ of the field //f// indicates that the unit was successfully removed from the session. ​
 +
 +Let's imagine that a unit ("​Bavarian Tractor",​ for instance) has been added to the session and now we need to handle not only renaming event but changes in counters values as well. It is not necessary to remove unit from session and then add it to the session again with new flags. You can simply add new flags to existing ones:
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=core/​update_data_flags&​
 + params={
 + "​spec":​[{
 + "​type":"​id",​
 + "​data":​34868,​
 + "​flags":​8192,​
 + "​mode":​1 /​* adding flags */
 + }]
 + }&​sid=<​your_sid>​
 +</​code>​
 +
 +Response:
 +<code javascript>​
 +[{
 +    "​i":​ 34868,
 +    "​d":​ {
 +        "​cfl":​ 1298,
 +        "​cnm":​ 45414,
 +        "​cneh":​ 0,
 +        "​cnkb":​ 43602
 +    },
 +    "​f":​ 8193
 +}]
 +</​code>​
 +
 +In the request we specified flag 8192, and in the result we see that the value of //f// is 8193. It means that 8192 has been added to previous value of //f// which is 1. 
 +
 +If you need to get events from all units of specified type, set value "​type"​ to the parameter //type//:
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=core/​update_data_flags&​
 + params={
 + "​spec":​[{
 + "​type":"​type",​
 + "​data":"​user",​
 + "​flags":​1,​
 + "​mode":​0
 + }]
 + }&​sid=<​your_sid>​
 +</​code>​
 +
 +===== Getting events =====
 +
 +Let's see how the system sends and receives events about renaming of a unit. 
 +
 +**1.** Create a new unit in [[http://​wialonb3.gurtam.com/​|Wialon Hosting]], for example:
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=core/​create_unit&​
 + params={
 + "​creatorId":​50935,​
 + "​name":"​unit",​
 + "​hwTypeId":​96266,​
 + "​dataFlags":​1
 + }&​sid=<​your_sid>​
 +</​code>​
 +Response:
 +<code javascript>​
 +{
 +    "​item":​ {
 +        "​nm":​ "​unit",​
 +        "​cls":​ 2,
 +        "​id":​ 656513,
 +        "​uacl":​ -1
 +    },
 +    "​flags":​ 1
 +}
 +</​code>​
 +
 +**2.** Login to the system as it was described in the chapter [[login]].
 +
 +**3.** Make the request [[../​apiref/​requests/​avl_evts|avl_evts]]. No events will be shown.
 +<​code>​
 +https://​hst-api.wialon.com/​avl_evts?​sid=<​your_sid>​
 +</​code>​
 +Response:
 +<code javascript>​
 +{
 +    "​tm":​ 1358780392,
 +    "​events":​ []
 +}
 +</​code>​
 +
 +**4.** Add unit to the session as it was described before, for example:
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=core/​update_data_flags&​
 + params={
 + "​spec":​[{
 + "​type":"​id",​
 + "​data":​656513,​
 + "​flags":​1,​
 + "​mode":​0}]
 + }&​sid=<​your_sid>​
 +</​code>​
 +Response:
 +<code javascript>​
 +[{
 +    "​i":​ 656513,
 +    "​d":​ {
 +        "​nm":​ "​unit",​
 +        "​cls":​ 2,
 +        "​id":​ 656513,
 +        "​uacl":​ -1
 +    },
 +    "​f":​ 1
 +}]
 +</​code>​
 +
 +**5.** Rename the unit, for example:
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=item/​update_name&​
 + params={
 + "​itemId":​656513,​
 + "​name":"​unit1"​
 + }&​sid=<​your_sid>​
 +</​code>​
 +Response:
 +<code javascript>​
 +{
 +    "​nm":​ "​unit1"​
 +}
 +</​code>​
 +
 +**6.** Make the request [[../​apiref/​requests/​avl_evts|avl_evts]] again. ​
 +<​code>​
 +https://​hst-api.wialon.com/​avl_evts?​sid=<​your_sid>​
 +</​code>​
 +Response:
 +<code javascript>​
 +{
 +    "​tm":​ 1358780632,
 +    "​events":​ [{
 +        "​i":​ 656513,
 +        "​t":​ "​u",​
 +        "​d":​ {
 +            "​nm":​ "​unit1"​
 +        }
 +    }]
 +}
 +</​code>​
 +
 +The field "​events"​ contains information about renaming event.
Follow us on Facebook Gurtam Wialon Twitter Gurtam Wialon info@gurtam.com   |   Copyright © 2002-2024 Gurtam