From f778b61390e3580d49959eaa3c3ffb899d20417b Mon Sep 17 00:00:00 2001 From: NateZ7 <33479378+NateZ7@users.noreply.github.com> Date: Tue, 30 Jan 2018 14:01:45 +0200 Subject: Added status arg --- src/org/traccar/WebDataHandler.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src/org/traccar/WebDataHandler.java') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index c64dcc81b..401216276 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -80,6 +80,7 @@ public class WebDataHandler extends BaseDataHandler { String request = url .replace("{name}", device.getName()) .replace("{uniqueId}", device.getUniqueId()) + .replace("{status}", device.getStatus()) .replace("{deviceId}", String.valueOf(position.getDeviceId())) .replace("{protocol}", String.valueOf(position.getProtocol())) .replace("{deviceTime}", String.valueOf(position.getDeviceTime().getTime())) -- cgit v1.2.3 From 377a2234ef8c25c54c22acf9db36d102347e0ad4 Mon Sep 17 00:00:00 2001 From: nativbarak Date: Sun, 4 Feb 2018 15:31:10 +0200 Subject: added POST option to forward url --- src/org/traccar/BasePipelineFactory.java | 2 +- src/org/traccar/WebDataHandler.java | 70 +++++++++++++++++++++++++++++--- test/org/traccar/WebDataHandlerTest.java | 2 +- 3 files changed, 66 insertions(+), 8 deletions(-) (limited to 'src/org/traccar/WebDataHandler.java') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index aa188d209..e8034779d 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -238,7 +238,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { } if (Context.getConfig().getBoolean("forward.enable")) { - pipeline.addLast("webHandler", new WebDataHandler(Context.getConfig().getString("forward.url"))); + pipeline.addLast("webHandler", new WebDataHandler(Context.getConfig().getString("forward.url"), Context.getConfig().getBoolean("forward.json"))); } if (commandResultEventHandler != null) { diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index 401216276..10fac2e41 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -16,6 +16,7 @@ package org.traccar; import com.fasterxml.jackson.core.JsonProcessingException; +import com.ning.http.client.AsyncHttpClient; import org.traccar.helper.Checksum; import org.traccar.helper.Log; import org.traccar.model.Device; @@ -24,17 +25,16 @@ import org.traccar.model.Position; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.Calendar; -import java.util.Formatter; -import java.util.Locale; -import java.util.TimeZone; +import java.util.*; public class WebDataHandler extends BaseDataHandler { private final String url; + private final Boolean json; - public WebDataHandler(String url) { + public WebDataHandler(String url, Boolean json) { this.url = url; + this.json = json; } private static String formatSentence(Position position) { @@ -121,10 +121,68 @@ public class WebDataHandler extends BaseDataHandler { @Override protected Position handlePosition(Position position) { + if(json){ + AsyncHttpClient.BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); + requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name()); - Context.getAsyncHttpClient().prepareGet(formatRequest(position)).execute(); + requestBuilder.addHeader("Content-Type", getContentType()); + setContent(position, requestBuilder); + requestBuilder.execute(); + }else { + Context.getAsyncHttpClient().prepareGet(formatRequest(position)).execute(); + } return position; } + protected String getContentType() { + return "application/json; charset=utf-8"; + } + + protected void setContent(Position position, AsyncHttpClient.BoundRequestBuilder requestBuilder) { + requestBuilder.setBody(prepareJsonPayload(position)); + } + + protected String prepareJsonPayload(Position position) { + + Map data = new HashMap<>(); + Device device = Context.getIdentityManager().getById(position.getDeviceId()); + + data.put("name", device.getName()); + data.put("uniqueId", device.getUniqueId()); + data.put("status", device.getStatus()); + data.put("deviceId", String.valueOf(position.getDeviceId())); + data.put("protocol", String.valueOf(position.getProtocol())); + data.put("deviceTime", String.valueOf(position.getDeviceTime().getTime())); + data.put("fixTime", String.valueOf(position.getFixTime().getTime())); + data.put("valid", String.valueOf(position.getValid())); + data.put("latitude", String.valueOf(position.getLatitude())); + data.put("longitude", String.valueOf(position.getLongitude())); + data.put("speed", String.valueOf(position.getSpeed())); + data.put("course", String.valueOf(position.getCourse())); + data.put("statusCode", calculateStatus(position)); + + if (position.getAddress() != null) { + try { + data.put("address", URLEncoder.encode(position.getAddress(), StandardCharsets.UTF_8.name())); + } catch (UnsupportedEncodingException error) { + Log.warning(error); + } + } + + try { + String attributes = Context.getObjectMapper().writeValueAsString(position.getAttributes()); + + data.put("attributes", URLEncoder.encode(attributes, StandardCharsets.UTF_8.name())); + } catch (UnsupportedEncodingException | JsonProcessingException error) { + Log.warning(error); + } + + try { + return Context.getObjectMapper().writeValueAsString(data); + } catch (JsonProcessingException e) { + Log.warning(e); + return null; + } + } } diff --git a/test/org/traccar/WebDataHandlerTest.java b/test/org/traccar/WebDataHandlerTest.java index 70b098b0f..70c744b3a 100644 --- a/test/org/traccar/WebDataHandlerTest.java +++ b/test/org/traccar/WebDataHandlerTest.java @@ -12,7 +12,7 @@ public class WebDataHandlerTest extends ProtocolTest { Position p = position("2016-01-01 01:02:03.000", true, 20, 30); - WebDataHandler handler = new WebDataHandler("http://localhost/?fixTime={fixTime}&gprmc={gprmc}&name={name}"); + WebDataHandler handler = new WebDataHandler("http://localhost/?fixTime={fixTime}&gprmc={gprmc}&name={name}", false); assertEquals( "http://localhost/?fixTime=1451610123000&gprmc=$GPRMC,010203.000,A,2000.0000,N,03000.0000,E,0.00,0.00,010116,,*05&name=test", -- cgit v1.2.3 From fc3106e742beba6aeaebb66083380c6b9e1082d0 Mon Sep 17 00:00:00 2001 From: nativbarak Date: Sun, 4 Feb 2018 17:21:51 +0200 Subject: Code refactoring --- src/org/traccar/WebDataHandler.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/org/traccar/WebDataHandler.java') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index 10fac2e41..a4c95aa3c 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -121,7 +121,7 @@ public class WebDataHandler extends BaseDataHandler { @Override protected Position handlePosition(Position position) { - if(json){ + if (json) { AsyncHttpClient.BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name()); @@ -129,7 +129,8 @@ public class WebDataHandler extends BaseDataHandler { setContent(position, requestBuilder); requestBuilder.execute(); - }else { + + } else { Context.getAsyncHttpClient().prepareGet(formatRequest(position)).execute(); } return position; -- cgit v1.2.3 From 6ae8a397c29a3d7a16d9cf481e2141e3e70921fd Mon Sep 17 00:00:00 2001 From: nativbarak Date: Sun, 4 Feb 2018 17:26:05 +0200 Subject: code refactoring --- src/org/traccar/WebDataHandler.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/org/traccar/WebDataHandler.java') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index a4c95aa3c..a30a41208 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -22,10 +22,16 @@ import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Position; +import java.util.HashMap; +import java.util.Map; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.Calendar; +import java.util.Formatter; +import java.util.Locale; +import java.util.TimeZone; + public class WebDataHandler extends BaseDataHandler { @@ -129,7 +135,7 @@ public class WebDataHandler extends BaseDataHandler { setContent(position, requestBuilder); requestBuilder.execute(); - + } else { Context.getAsyncHttpClient().prepareGet(formatRequest(position)).execute(); } -- cgit v1.2.3 From 00b1715aa854f62b2cc90753c05d558cea9dd6ea Mon Sep 17 00:00:00 2001 From: nativbarak Date: Mon, 5 Feb 2018 10:59:55 +0200 Subject: changed POST to use full object --- src/org/traccar/WebDataHandler.java | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) (limited to 'src/org/traccar/WebDataHandler.java') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index a30a41208..e45a47835 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -37,6 +37,8 @@ public class WebDataHandler extends BaseDataHandler { private final String url; private final Boolean json; + private static final String KEY_POSITION = "position"; + private static final String KEY_DEVICE = "device"; public WebDataHandler(String url, Boolean json) { this.url = url; @@ -153,39 +155,20 @@ public class WebDataHandler extends BaseDataHandler { protected String prepareJsonPayload(Position position) { Map data = new HashMap<>(); - Device device = Context.getIdentityManager().getById(position.getDeviceId()); - - data.put("name", device.getName()); - data.put("uniqueId", device.getUniqueId()); - data.put("status", device.getStatus()); - data.put("deviceId", String.valueOf(position.getDeviceId())); - data.put("protocol", String.valueOf(position.getProtocol())); - data.put("deviceTime", String.valueOf(position.getDeviceTime().getTime())); - data.put("fixTime", String.valueOf(position.getFixTime().getTime())); - data.put("valid", String.valueOf(position.getValid())); - data.put("latitude", String.valueOf(position.getLatitude())); - data.put("longitude", String.valueOf(position.getLongitude())); - data.put("speed", String.valueOf(position.getSpeed())); - data.put("course", String.valueOf(position.getCourse())); - data.put("statusCode", calculateStatus(position)); - if (position.getAddress() != null) { - try { - data.put("address", URLEncoder.encode(position.getAddress(), StandardCharsets.UTF_8.name())); - } catch (UnsupportedEncodingException error) { - Log.warning(error); - } + if (position != null) { + data.put(KEY_POSITION, position); } - try { - String attributes = Context.getObjectMapper().writeValueAsString(position.getAttributes()); - - data.put("attributes", URLEncoder.encode(attributes, StandardCharsets.UTF_8.name())); - } catch (UnsupportedEncodingException | JsonProcessingException error) { - Log.warning(error); + if (position.getDeviceId() != 0) { + Device device = Context.getIdentityManager().getById(position.getDeviceId()); + if (device != null) { + data.put(KEY_DEVICE, device); + } } try { + System.out.println(Context.getObjectMapper().writeValueAsString(data).toString()); return Context.getObjectMapper().writeValueAsString(data); } catch (JsonProcessingException e) { Log.warning(e); -- cgit v1.2.3 From ed4a723e518d444bcfd6c310c4ed448b8ee8dedf Mon Sep 17 00:00:00 2001 From: nativbarak Date: Mon, 5 Feb 2018 13:35:19 +0200 Subject: added unit test, fixed spaces, fixed primitive types --- src/org/traccar/WebDataHandler.java | 6 ++---- test/org/traccar/WebDataHandlerTest.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'src/org/traccar/WebDataHandler.java') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index e45a47835..8bfabf183 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -32,15 +32,14 @@ import java.util.Formatter; import java.util.Locale; import java.util.TimeZone; - public class WebDataHandler extends BaseDataHandler { private final String url; - private final Boolean json; + private final boolean json; private static final String KEY_POSITION = "position"; private static final String KEY_DEVICE = "device"; - public WebDataHandler(String url, Boolean json) { + public WebDataHandler(String url, boolean json) { this.url = url; this.json = json; } @@ -168,7 +167,6 @@ public class WebDataHandler extends BaseDataHandler { } try { - System.out.println(Context.getObjectMapper().writeValueAsString(data).toString()); return Context.getObjectMapper().writeValueAsString(data); } catch (JsonProcessingException e) { Log.warning(e); diff --git a/test/org/traccar/WebDataHandlerTest.java b/test/org/traccar/WebDataHandlerTest.java index 70c744b3a..3e79232ec 100644 --- a/test/org/traccar/WebDataHandlerTest.java +++ b/test/org/traccar/WebDataHandlerTest.java @@ -3,6 +3,8 @@ package org.traccar; import org.junit.Test; import org.traccar.model.Position; +import java.text.ParseException; + import static org.junit.Assert.assertEquals; public class WebDataHandlerTest extends ProtocolTest { @@ -20,4 +22,17 @@ public class WebDataHandlerTest extends ProtocolTest { } + @Test + public void testBuilderRequest() throws ParseException { + + Position p = position("2016-01-01 01:02:03.000", true, 20, 30); + + WebDataHandler handler = new WebDataHandler("http://localhost/", true); + + assertEquals( + "{\"position\":{\"id\":0,\"attributes\":{},\"deviceId\":0,\"type\":null,\"protocol\":null,\"serverTime\":null,\"deviceTime\":1451610123000,\"fixTime\":1451610123000,\"outdated\":false,\"valid\":true,\"latitude\":20.0,\"longitude\":30.0,\"altitude\":0.0,\"speed\":0.0,\"course\":0.0,\"address\":null,\"accuracy\":0.0,\"network\":null}}", + handler.prepareJsonPayload(p)); + + } + } -- cgit v1.2.3 From 4969ecd1d16324f488f01f1f0873e101c1609302 Mon Sep 17 00:00:00 2001 From: nativbarak Date: Tue, 6 Feb 2018 10:28:21 +0200 Subject: removed unneccesary methods --- src/org/traccar/WebDataHandler.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'src/org/traccar/WebDataHandler.java') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index 8bfabf183..5a0aab3e4 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -132,9 +132,9 @@ public class WebDataHandler extends BaseDataHandler { AsyncHttpClient.BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name()); - requestBuilder.addHeader("Content-Type", getContentType()); + requestBuilder.addHeader("Content-Type", "application/json; charset=utf-8"); - setContent(position, requestBuilder); + requestBuilder.setBody(prepareJsonPayload(position)); requestBuilder.execute(); } else { @@ -143,14 +143,6 @@ public class WebDataHandler extends BaseDataHandler { return position; } - protected String getContentType() { - return "application/json; charset=utf-8"; - } - - protected void setContent(Position position, AsyncHttpClient.BoundRequestBuilder requestBuilder) { - requestBuilder.setBody(prepareJsonPayload(position)); - } - protected String prepareJsonPayload(Position position) { Map data = new HashMap<>(); -- cgit v1.2.3 From 3a0a269dca63dacc1199cc738aacdfb2c2ecc7bf Mon Sep 17 00:00:00 2001 From: nativbarak Date: Wed, 7 Feb 2018 09:56:41 +0200 Subject: remove unnecasseray checks --- src/org/traccar/WebDataHandler.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'src/org/traccar/WebDataHandler.java') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index 5a0aab3e4..f7a7222d8 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -146,17 +146,10 @@ public class WebDataHandler extends BaseDataHandler { protected String prepareJsonPayload(Position position) { Map data = new HashMap<>(); + Device device = Context.getIdentityManager().getById(position.getDeviceId()); - if (position != null) { - data.put(KEY_POSITION, position); - } - - if (position.getDeviceId() != 0) { - Device device = Context.getIdentityManager().getById(position.getDeviceId()); - if (device != null) { - data.put(KEY_DEVICE, device); - } - } + data.put(KEY_POSITION, position); + data.put(KEY_DEVICE, device); try { return Context.getObjectMapper().writeValueAsString(data); -- cgit v1.2.3 From 56669ce1a869d7a1e2ebda0ecf1d38174026bab9 Mon Sep 17 00:00:00 2001 From: nativbarak Date: Wed, 7 Feb 2018 11:09:30 +0200 Subject: Fixed correct checks --- src/org/traccar/WebDataHandler.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/org/traccar/WebDataHandler.java') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index f7a7222d8..adc82486c 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -148,8 +148,13 @@ public class WebDataHandler extends BaseDataHandler { Map data = new HashMap<>(); Device device = Context.getIdentityManager().getById(position.getDeviceId()); - data.put(KEY_POSITION, position); - data.put(KEY_DEVICE, device); + if (position != null) { + data.put(KEY_POSITION, position); + } + + if (device != null) { + data.put(KEY_DEVICE, device); + } try { return Context.getObjectMapper().writeValueAsString(data); -- cgit v1.2.3 From 6d7bc2fe5dfc38dbe25c488501a8f6c653928e33 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 9 Feb 2018 12:08:13 +0500 Subject: Remove unnecessary checks --- src/org/traccar/WebDataHandler.java | 6 ++---- src/org/traccar/notification/EventForwarder.java | 10 ++++------ 2 files changed, 6 insertions(+), 10 deletions(-) (limited to 'src/org/traccar/WebDataHandler.java') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index adc82486c..cdd9d0c11 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -148,9 +148,7 @@ public class WebDataHandler extends BaseDataHandler { Map data = new HashMap<>(); Device device = Context.getIdentityManager().getById(position.getDeviceId()); - if (position != null) { - data.put(KEY_POSITION, position); - } + data.put(KEY_POSITION, position); if (device != null) { data.put(KEY_DEVICE, device); diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index d1bdd4aba..8be4c23b0 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -85,11 +85,9 @@ public abstract class EventForwarder { if (position != null) { data.put(KEY_POSITION, position); } - if (event.getDeviceId() != 0) { - Device device = Context.getIdentityManager().getById(event.getDeviceId()); - if (device != null) { - data.put(KEY_DEVICE, device); - } + Device device = Context.getIdentityManager().getById(event.getDeviceId()); + if (device != null) { + data.put(KEY_DEVICE, device); } if (event.getGeofenceId() != 0) { Geofence geofence = Context.getGeofenceManager().getById(event.getGeofenceId()); -- cgit v1.2.3