Differences

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

Link to this comparison view

en:local:remoteapi1504:codesamples:update_item [20/05/2014 11:49]
en:local:remoteapi1504:codesamples:update_item [20/05/2014 11:49] (current)
Line 1: Line 1:
 +{{indexmenu_n>​4}}
 +====== Creating, editing and deleting items ======
  
 +Let's see how standard operations with items such as creating, editing, and deleting are performed in Wialon. As an example we'll take a unit. Manipulations with items of other types are similar to ones described below except editing which differs according to item type.
 +
 +//:!: 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**//​.
 +
 +===== Creating unit =====
 +
 +Let's create a unit with name "​ips_unit"​ and device type "​Wialon IPS". To do this, we'll need the request [[../​apiref/​core/​create_unit|core/​create_unit]]:​
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=core/​create_unit&​
 + params={
 + "​creatorId":​50935,​
 + "​name":"​ips_unit",​
 + "​hwTypeId":​96266,​
 + "​dataFlags":​1
 + }&​sid=<​your_sid>​
 +</​code>​
 +
 +Response:
 +<code javascript>​
 +{
 +    "​item":​ {
 +        "​nm":​ "​ips_unit",​
 +        "​cls":​ 2,
 +        "​id":​ 652886,
 +        "​uacl":​ -1
 +    },
 +    "​flags":​ 1
 +}
 +</​code>​
 +
 +To check that the unit has indeed been created, perform a [[../​apiref/​core/​search_items|search]] by name:
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=core/​search_items&​
 + params={
 + "​spec":​{
 + "​itemsType":"​avl_unit",​
 + "​propName":"​sys_name",​
 + "​propValueMask":"​ips_unit",​
 + "​sortType":"​sys_name"​
 + },
 + "​force":​1,​
 + "​flags":​1,​
 + "​from":​0,​
 + "​to":​0
 + }&​sid=<​your_sid>​
 +</​code>​
 +
 +Response:
 +<code javascript>​
 +{
 +    "​searchSpec":​ {
 +        "​itemsType":​ "​avl_unit",​
 +        "​propName":​ "​sys_name",​
 +        "​propValueMask":​ "​ips_unit",​
 +        "​sortType":​ "​sys_name",​
 +        "​propType":​ ""​
 +    },
 +    "​dataFlags":​ 1,
 +    "​totalItemsCount":​ 1,
 +    "​indexFrom":​ 0,
 +    "​indexTo":​ 0,
 +    "​items":​ [{
 +        "​nm":​ "​ips_unit",​
 +        "​cls":​ 2,
 +        "​id":​ 652886,
 +        "​uacl":​ -1
 +    }]
 +}
 +</​code>​
 +
 +The response is not empty, so we can conclude that the unit has been created successfully.
 +
 +===== Editing unit =====
 +
 +Now we are going to edit the value of the mileage counter of the new unit. To watch the changes, let's first make the request about the initial value of this counter. To see counter values in the resulting JSON, use the flag 0x00002000:
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=core/​search_item&​
 + params={
 + "​id":​652886,​
 + "​flags":​8193
 + }&​sid=<​your_sid>​
 +</​code>​
 +Response:
 +<code javascript>​
 +{
 +    "​item":​ {
 +        "​nm":​ "​ips_unit",​
 +        "​cls":​ 2,
 +        "​id":​ 652886,
 +        "​cfl":​ 16,
 +        "​cnm":​ 0,
 +        "​cneh":​ 0,
 +        "​cnkb":​ 0,
 +        "​uacl":​ -1
 +    },
 +    "​flags":​ 8193
 +}
 +</​code>​
 +Mileage counter value is placed in the field //cnm// and it is 0.
 +
 +To change the value of the counter, use the request [[../​apiref/​unit/​update_mileage_counter|unit/​update_mileage_counter]]:​
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=unit/​update_mileage_counter&​
 + params={
 + "​itemId":​652886,​
 + "​newValue":​456
 + }&​sid=<​your_sid>​
 +</​code>​
 +Response:
 +<code javascript>​
 +{
 +    "​cnm":​ 456
 +}
 +</​code>​
 +
 +Now let's check the result of editing using a search request: ​
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=core/​search_item&​
 + params={
 + "​id":​652886,​
 + "​flags":​8193
 + }&​sid=<​your_sid>​
 +</​code>​
 +Response:
 +<code javascript>​
 +{
 +    "​item":​ {
 +        "​nm":​ "​ips_unit",​
 +        "​cls":​ 2,
 +        "​id":​ 652886,
 +        "​cfl":​ 16,
 +        "​cnm":​ 456,
 +        "​cneh":​ 0,
 +        "​cnkb":​ 0,
 +        "​uacl":​ -1
 +    },
 +    "​flags":​ 8193
 +}
 +</​code>​
 +We see that the value in //cnm// field has changed and it is 456 now, so the editing operation has been successful.
 +
 +===== Deleting unit =====
 +You can delete an item using the request [[../​apiref/​item/​delete_item|item/​delete_item]]:​
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=item/​delete_item&​
 + params={
 + "​itemId":​652886
 + }&​sid=<​your_sid>​
 +</​code>​
 +Response:
 +<code javascript>​
 +{}
 +</​code>​
 +
 +Make a search by ID to make sure that that the operation has been successful:
 +<​code>​
 +https://​hst-api.wialon.com/​wialon/​ajax.html?​svc=core/​search_item&​
 + params={
 + "​id":​652886,​
 + "​flags":​1
 + }&​sid=<​your_sid>​
 +</​code>​
 +Result:
 +<code javascript>​
 +{
 +    "​error":​ 7
 +}
 +</​code>​
Follow us on Facebook Gurtam Wialon Twitter Gurtam Wialon info@gurtam.com   |   Copyright © 2002-2024 Gurtam