Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Devices Through API #753

Closed
WalrusHat opened this issue Jun 30, 2016 · 18 comments
Closed

Updating Devices Through API #753

WalrusHat opened this issue Jun 30, 2016 · 18 comments

Comments

@WalrusHat
Copy link

Hey Vitalidze!

quick issue! Im trying to update my devices through AJAX and Javascript. When I update through the web UI it sends off this header:

7|0|17|http://MyServer:8082/traccar/|48ACB3167486C154BB2793684679C460|org.traccar.web.client.model.DataService|updateDevice|org.traccar.web.shared.model.Device/1329600652|chinese device|org.traccar.web.shared.model.Group/3338357041|Test|90EE90|DEDEDE|F3F500|FF0000|org.traccar.web.shared.model.DeviceIconMode/752598484|org.traccar.web.shared.model.DeviceIconType/3813480130|java.util.ArrayList/4159755760|irisE|4109137594|1|2|3|4|1|5|5|0|0|6|7|0|0|E|8|0|9|10|11|10|12|13|0|1|14|0|E|0|0|15|0|60|16|0|0|0|0|15|0|1|1|1|0|0|300|17|0|

So I have been sending POST request to: "traccar/rest/updateDevice" and I get back a "200 ok" response but nothing is updated. Any help?

@vitalidze
Copy link
Owner

This request is working using the GWT RPC protocol. It transfers data in special compact format. You should not be using such format when you are working with the RESTful API. You should post your messages in JSON instead. Find examples here: http://traccar.litvak.su/features/rest-api.html

@WalrusHat
Copy link
Author

So im doing that currently and I'm getting a "200 ok" response from the server but nothing is changing...

This is what im getting back:
requestheader

@vitalidze
Copy link
Owner

It looks correct. What do you get in response to this request? Which fields are not updated?

@WalrusHat
Copy link
Author

Thats the thing. I didn't get any response at all other then the "200 ok" I am trying to update the device name

@WalrusHat
Copy link
Author

where is says "name" : iris

@vitalidze
Copy link
Owner

I see. What do you have on the "Response" tab?

@WalrusHat
Copy link
Author

it didn't return anything at all

@vitalidze
Copy link
Owner

Well, I am out of ideas. It should return you the updated device profile in JSON format.

I will try to reproduce it myself later.

@WalrusHat
Copy link
Author

Thanks! : this is the response tab that I got.. Thanks for the help!
responseheader

@vitalidze vitalidze added the bug label Jul 1, 2016
@WalrusHat
Copy link
Author

any luck?

@vitalidze
Copy link
Owner

It does not work for me either. I have found a bug in the code. Will try to fix it in nearest future.

vitalidze added a commit that referenced this issue Jul 4, 2016
@vitalidze
Copy link
Owner

vitalidze commented Jul 4, 2016

I have fixed issue in code. Also I have a workaround for current version. The root cause is incorrect deserialisation of iconType field, which format is a bit unexpected for the server. So, to make it proper you have to remove the object notation from that field and just use 'string' notation for this enumeration. You can find all possible values here: https://github.com/vitalidze/traccar-web/blob/dev/src/main/java/org/traccar/web/shared/model/DeviceIconType.java

Here is an example request I have been sending to my demo site, which works:

$ curl -v --cookie "JSESSIONID=XXXXXXXX" -X POST --data "[ {
  \"id\" : 1607,
  \"uniqueId\" : \"vittest1\",
  \"name\" : \"testXXX\",
  \"timeout\" : 300,
  \"idleSpeedThreshold\" : 0.0,
  \"minIdleTime\" : 60,
  \"iconType\" : \"DEFAULT\",
  \"maintenances\" : [ ],
  \"sensors\" : [ ],
  \"iconMode\" : \"ICON\",
  \"iconArrowMovingColor\" : \"90EE90\",
  \"iconArrowPausedColor\" : \"F3F500\",
  \"iconArrowStoppedColor\" : \"FF0000\",
  \"iconArrowOfflineColor\" : \"DEDEDE\",
  \"iconRotation\" : false,
  \"iconArrowRadius\" : 10.0,
  \"showName\" : true,
  \"showProtocol\" : true,
  \"showOdometer\" : true                                                                                                                                                                      <....
* Hostname was NOT found in DNS cache
*   Trying 188.166.50.6...
* Connected to d.traccar.litvak.su (188.166.50.6) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
* Server certificate: d.traccar.litvak.su
* Server certificate: StartCom Class 1 DV Server CA
* Server certificate: StartCom Certification Authority
> POST /traccar/rest/updateDevice HTTP/1.1
> User-Agent: curl/7.37.1
> Host: d.traccar.litvak.su
> Accept: */*
> Cookie: JSESSIONID=XXXXXX
> Content-Length: 508
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 508 out of 508 bytes
< HTTP/1.1 200 OK
* Server nginx/1.2.1 is not blacklisted
< Server: nginx/1.2.1
< Date: Mon, 04 Jul 2016 07:13:20 GMT
< Content-Type: application/json; charset=UTF-8
< Content-Length: 756
< Connection: keep-alive
<
{
  "id" : 1607,
  "uniqueId" : "vittest1",
  "name" : "testXXX",
  "timeout" : 300,
  "idleSpeedThreshold" : 0.0,
  "minIdleTime" : 60,
  "iconType" : {
    "OFFLINE" : {
      "width" : 21,
      "height" : 25,
      "urls" : [ "img/marker-white.png", "img/marker-green.png" ]
    },
    "LATEST" : {
      "width" : 21,
      "height" : 25,
      "urls" : [ "img/marker.png", "img/marker-green.png" ]
    }
  },
  "maintenances" : [ ],
  "sensors" : [ ],
  "iconMode" : "ICON",
  "iconArrowMovingColor" : "90EE90",
  "iconArrowPausedColor" : "F3F500",
  "iconArrowStoppedColor" : "FF0000",
  "iconArrowOfflineColor" : "DEDEDE",
  "iconRotation" : false,
  "iconArrowRadius" : 10.0,
  "showName" : true,
  "showProtocol" : true,
  "showOdometer" : true
* Connection #0 to host d.traccar.litvak.su left intact
}%

Please note the format of iconType field in request:

  "iconType" : "DEFAULT"

And in response:

  "iconType" : {
    "OFFLINE" : {
      "width" : 21,
      "height" : 25,
      "urls" : [ "img/marker-white.png", "img/marker-green.png" ]
    },
    "LATEST" : {
      "width" : 21,
      "height" : 25,
      "urls" : [ "img/marker.png", "img/marker-green.png" ]
    }
  }

This format of request will work in both new and current versions. In newer version there will be a new field called type containing name of the enumeration element. Thus, in newer version additional format of request will work:

  "iconType" : {
    "type" : "DEFAULT",
    "OFFLINE" : {
      "width" : 21,
      "height" : 25,
      "urls" : [ "img/marker-white.png", "img/marker-green.png" ]
    },
    "LATEST" : {
      "width" : 21,
      "height" : 25,
      "urls" : [ "img/marker.png", "img/marker-green.png" ]
    }
  }

All responses will contain the type field too.

Hope this helps.

@WalrusHat
Copy link
Author

Awesome! Let me give it a shot!

@WalrusHat
Copy link
Author

Im getting 500 errors when i send iconType: "DEFAULT" and I get a 200 with the same errors when I send iconType: "Default"

@vitalidze
Copy link
Owner

You should check wrapper.log for error messages. Does it work for you with cURL? As been said my example works on the demo server with curl, I just copied and pasted text from terminal.

@WalrusHat
Copy link
Author

wrapper.log is just throwing empty email field warnings. I haven't tried tried cURL.... I'm posting with ajax and javascript.

@vitalidze
Copy link
Owner

Then you should have some text in reply for the 500 error. I guess your request is incorrect.

@vitalidze
Copy link
Owner

Fixed, written news, updated latest and demo versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants