From 5193bd866734b6fea1bf96b5d975c52f09cbec2e Mon Sep 17 00:00:00 2001 From: namo Date: Fri, 13 Oct 2017 16:03:25 +0300 Subject: initial flespi integtation: pulling messages from flespi channels and updating devices' position --- src/org/traccar/Context.java | 21 +++++ src/org/traccar/Main.java | 6 ++ src/org/traccar/flespi/ChannelPullTask.java | 15 ++++ src/org/traccar/flespi/FlespiClient.java | 128 ++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 src/org/traccar/flespi/ChannelPullTask.java create mode 100644 src/org/traccar/flespi/FlespiClient.java (limited to 'src/org') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 3b24c6460..e582c5256 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -21,6 +21,9 @@ import com.ning.http.client.AsyncHttpClient; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Properties; import org.apache.velocity.app.VelocityEngine; @@ -43,6 +46,7 @@ import org.traccar.database.StatisticsManager; import org.traccar.database.UsersManager; import org.traccar.events.MotionEventHandler; import org.traccar.events.OverspeedEventHandler; +import org.traccar.flespi.FlespiClient; import org.traccar.geocoder.BingMapsGeocoder; import org.traccar.geocoder.FactualGeocoder; import org.traccar.geocoder.GeocodeFarmGeocoder; @@ -263,6 +267,12 @@ public final class Context { config.getDouble("event.motion.speedThreshold", 0.01)); } + private final static List flespiClients = new ArrayList<>(); + + public static List getFlespiClients() { + return flespiClients; + } + public static void init(String[] arguments) throws Exception { config = new Config(); @@ -411,6 +421,17 @@ public final class Context { smppClient = new SmppClient(); } + if (config.getBoolean("flespi.enable")) { + String uri = config.getString("flespi.url"); + String token = config.getString("flespi.token"); + String channelIds = config.getString("flespi.channel.ids"); + List ids = Arrays.asList(channelIds.split(",")); + if (uri != null && token != null) { + for (String channelId : ids) { + flespiClients.add(new FlespiClient(uri, token, channelId)); + } + } + } } public static void init(IdentityManager testIdentityManager) { diff --git a/src/org/traccar/Main.java b/src/org/traccar/Main.java index 1e2db2693..0d7c2ecf0 100644 --- a/src/org/traccar/Main.java +++ b/src/org/traccar/Main.java @@ -15,9 +15,11 @@ */ package org.traccar; +import org.traccar.flespi.FlespiClient; import org.traccar.helper.Log; import java.sql.SQLException; +import java.util.List; import java.util.Timer; import java.util.TimerTask; import java.util.Locale; @@ -56,6 +58,10 @@ public final class Main { public void run() { Log.info("Shutting down server..."); + List flespiClients = Context.getFlespiClients(); + for (FlespiClient flespiClient : flespiClients) { + flespiClient.stopPullTask(); + } if (Context.getWebServer() != null) { Context.getWebServer().stop(); } diff --git a/src/org/traccar/flespi/ChannelPullTask.java b/src/org/traccar/flespi/ChannelPullTask.java new file mode 100644 index 000000000..5e5525ec9 --- /dev/null +++ b/src/org/traccar/flespi/ChannelPullTask.java @@ -0,0 +1,15 @@ +package org.traccar.flespi; + +public class ChannelPullTask implements Runnable { + + private final FlespiClient flespiClient; + + protected ChannelPullTask(FlespiClient flespiClient) { + this.flespiClient = flespiClient; + } + + @Override + public void run() { + flespiClient.channelPull(); + } +} diff --git a/src/org/traccar/flespi/FlespiClient.java b/src/org/traccar/flespi/FlespiClient.java new file mode 100644 index 000000000..2c98f36b3 --- /dev/null +++ b/src/org/traccar/flespi/FlespiClient.java @@ -0,0 +1,128 @@ +package org.traccar.flespi; + + +import com.ning.http.client.AsyncCompletionHandler; +import com.ning.http.client.Response; +import org.traccar.Context; +import org.traccar.helper.Log; +import org.traccar.model.Device; +import org.traccar.model.Position; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonNumber; +import javax.json.JsonReader; +import javax.json.JsonArray; +import java.util.Date; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +public class FlespiClient { + private final String url; + private final String token; + private final String channel_id; + private final ScheduledExecutorService pullChannelExecutor = Executors.newScheduledThreadPool(5); + private ScheduledFuture pullTask; + private Integer pullDelay = 5; + private int nextKey; + + public FlespiClient(String url, String token, String channelId) { + this.channel_id = channelId; + this.url = url + "/gw/channels/" + channelId + "/messages?data={\"limit_count\":1000000," + + "\"limit_size\":100000000,\"delete\":true,\"timeout\":25,\"curr_key\":%d}"; + this.token = "FlespiToken " + token; + + schedulePull(); + } + + private void schedulePull() { + pullTask = pullChannelExecutor.scheduleAtFixedRate(new ChannelPullTask(this), 1, pullDelay, TimeUnit.SECONDS); + } + + public void stopPullTask() { + if (pullTask != null) { + pullTask.cancel(false); + } + } + + protected synchronized void channelPull() { + Context.getAsyncHttpClient().prepareGet(String.format(this.url, nextKey)) + .addHeader("Authorization", this.token) + .execute(new AsyncCompletionHandler() { + @Override + public Object onCompleted(Response response) throws Exception { + try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { + JsonObject object = reader.readObject(); + JsonArray result = object.getJsonArray("result"); + nextKey = object.getInt("next_key", nextKey); + Log.debug(String.format("channelPull next_key=%d msgs_count=%d", nextKey, result.size())); + for (int i = 0; i < result.size(); i++) { + Position position = decodePosition(result.getJsonObject(i)); + if (position != null && position.getLatitude() != 0 && position.getLongitude() != 0) { + Context.getConnectionManager().updateDevice(position.getDeviceId(), + Device.STATUS_ONLINE, new Date()); + Context.getDeviceManager().updateLatestPosition(position); + } + } + JsonArray errors = object.getJsonArray("errors"); + if (errors != null) { + for (int i = 0; i < errors.size(); i++) { + JsonObject error = errors.getJsonObject(i); + Log.warning("Error in flespi channel: " + error.toString()); + } + if (result == null || result.size() == 0) { + stopPullTask(); + } + } + } + return null; + } + + @Override + public void onThrowable(Throwable t) { + t.printStackTrace(); + } + }); + } + + private Position decodePosition(JsonObject msg) { + Device device = null; + try { + device = Context.getIdentityManager().getByUniqueId(msg.getString("ident")); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + if (device == null) { + return null; + } + Position position = new Position(); + position.setDeviceId(device.getId()); + + + position.setProtocol("flespi"); + + position.setTime(new Date((long) msg.getJsonNumber("timestamp").doubleValue() * 1000)); + JsonNumber lat = msg.getJsonNumber("position.latitude"); + JsonNumber lon = msg.getJsonNumber("position.longitude"); + position.setLatitude((lat != null && lon != null) ? lat.doubleValue() : 0); + position.setLongitude((lat != null && lon != null) ? lon.doubleValue() : 0); + + JsonNumber speed = msg.getJsonNumber("position.speed"); + position.setSpeed(speed != null ? speed.doubleValue() : 0); + + JsonNumber course = msg.getJsonNumber("position.direction"); + position.setCourse(course != null ? course.doubleValue() : 0); + + JsonNumber altitude = msg.getJsonNumber("position.altitude"); + position.setAltitude(altitude != null ? altitude.doubleValue() : 0); + + int satellites = msg.getInt("position.satellites", 0); + position.setValid(lat != null && lon != null && satellites >= 3); + position.set(Position.KEY_SATELLITES, satellites); + + return position; + } +} -- cgit v1.2.3 From da4fce0da7be92b381c6cdc1069486bc83a2974d Mon Sep 17 00:00:00 2001 From: namo Date: Thu, 19 Oct 2017 17:59:25 +0300 Subject: Revert "initial flespi integtation: pulling messages from flespi channels and updating devices' position" This reverts commit 5193bd866734b6fea1bf96b5d975c52f09cbec2e. --- checkstyle.xml | 4 +- setup/default.xml | 6 -- src/org/traccar/Context.java | 21 ----- src/org/traccar/Main.java | 6 -- src/org/traccar/flespi/ChannelPullTask.java | 15 ---- src/org/traccar/flespi/FlespiClient.java | 128 ---------------------------- 6 files changed, 1 insertion(+), 179 deletions(-) delete mode 100644 src/org/traccar/flespi/ChannelPullTask.java delete mode 100644 src/org/traccar/flespi/FlespiClient.java (limited to 'src/org') diff --git a/checkstyle.xml b/checkstyle.xml index dc038f551..9d0314b06 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -67,9 +67,7 @@ - - - + diff --git a/setup/default.xml b/setup/default.xml index 0a7b421d7..95b69e216 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -218,10 +218,4 @@ 5147 5148 - - true - https://flespi.io - - - diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index e582c5256..3b24c6460 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -21,9 +21,6 @@ import com.ning.http.client.AsyncHttpClient; import java.net.InetAddress; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import java.util.Properties; import org.apache.velocity.app.VelocityEngine; @@ -46,7 +43,6 @@ import org.traccar.database.StatisticsManager; import org.traccar.database.UsersManager; import org.traccar.events.MotionEventHandler; import org.traccar.events.OverspeedEventHandler; -import org.traccar.flespi.FlespiClient; import org.traccar.geocoder.BingMapsGeocoder; import org.traccar.geocoder.FactualGeocoder; import org.traccar.geocoder.GeocodeFarmGeocoder; @@ -267,12 +263,6 @@ public final class Context { config.getDouble("event.motion.speedThreshold", 0.01)); } - private final static List flespiClients = new ArrayList<>(); - - public static List getFlespiClients() { - return flespiClients; - } - public static void init(String[] arguments) throws Exception { config = new Config(); @@ -421,17 +411,6 @@ public final class Context { smppClient = new SmppClient(); } - if (config.getBoolean("flespi.enable")) { - String uri = config.getString("flespi.url"); - String token = config.getString("flespi.token"); - String channelIds = config.getString("flespi.channel.ids"); - List ids = Arrays.asList(channelIds.split(",")); - if (uri != null && token != null) { - for (String channelId : ids) { - flespiClients.add(new FlespiClient(uri, token, channelId)); - } - } - } } public static void init(IdentityManager testIdentityManager) { diff --git a/src/org/traccar/Main.java b/src/org/traccar/Main.java index 0d7c2ecf0..1e2db2693 100644 --- a/src/org/traccar/Main.java +++ b/src/org/traccar/Main.java @@ -15,11 +15,9 @@ */ package org.traccar; -import org.traccar.flespi.FlespiClient; import org.traccar.helper.Log; import java.sql.SQLException; -import java.util.List; import java.util.Timer; import java.util.TimerTask; import java.util.Locale; @@ -58,10 +56,6 @@ public final class Main { public void run() { Log.info("Shutting down server..."); - List flespiClients = Context.getFlespiClients(); - for (FlespiClient flespiClient : flespiClients) { - flespiClient.stopPullTask(); - } if (Context.getWebServer() != null) { Context.getWebServer().stop(); } diff --git a/src/org/traccar/flespi/ChannelPullTask.java b/src/org/traccar/flespi/ChannelPullTask.java deleted file mode 100644 index 5e5525ec9..000000000 --- a/src/org/traccar/flespi/ChannelPullTask.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.traccar.flespi; - -public class ChannelPullTask implements Runnable { - - private final FlespiClient flespiClient; - - protected ChannelPullTask(FlespiClient flespiClient) { - this.flespiClient = flespiClient; - } - - @Override - public void run() { - flespiClient.channelPull(); - } -} diff --git a/src/org/traccar/flespi/FlespiClient.java b/src/org/traccar/flespi/FlespiClient.java deleted file mode 100644 index 2c98f36b3..000000000 --- a/src/org/traccar/flespi/FlespiClient.java +++ /dev/null @@ -1,128 +0,0 @@ -package org.traccar.flespi; - - -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; -import org.traccar.Context; -import org.traccar.helper.Log; -import org.traccar.model.Device; -import org.traccar.model.Position; - -import javax.json.Json; -import javax.json.JsonObject; -import javax.json.JsonNumber; -import javax.json.JsonReader; -import javax.json.JsonArray; -import java.util.Date; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - -public class FlespiClient { - private final String url; - private final String token; - private final String channel_id; - private final ScheduledExecutorService pullChannelExecutor = Executors.newScheduledThreadPool(5); - private ScheduledFuture pullTask; - private Integer pullDelay = 5; - private int nextKey; - - public FlespiClient(String url, String token, String channelId) { - this.channel_id = channelId; - this.url = url + "/gw/channels/" + channelId + "/messages?data={\"limit_count\":1000000," - + "\"limit_size\":100000000,\"delete\":true,\"timeout\":25,\"curr_key\":%d}"; - this.token = "FlespiToken " + token; - - schedulePull(); - } - - private void schedulePull() { - pullTask = pullChannelExecutor.scheduleAtFixedRate(new ChannelPullTask(this), 1, pullDelay, TimeUnit.SECONDS); - } - - public void stopPullTask() { - if (pullTask != null) { - pullTask.cancel(false); - } - } - - protected synchronized void channelPull() { - Context.getAsyncHttpClient().prepareGet(String.format(this.url, nextKey)) - .addHeader("Authorization", this.token) - .execute(new AsyncCompletionHandler() { - @Override - public Object onCompleted(Response response) throws Exception { - try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { - JsonObject object = reader.readObject(); - JsonArray result = object.getJsonArray("result"); - nextKey = object.getInt("next_key", nextKey); - Log.debug(String.format("channelPull next_key=%d msgs_count=%d", nextKey, result.size())); - for (int i = 0; i < result.size(); i++) { - Position position = decodePosition(result.getJsonObject(i)); - if (position != null && position.getLatitude() != 0 && position.getLongitude() != 0) { - Context.getConnectionManager().updateDevice(position.getDeviceId(), - Device.STATUS_ONLINE, new Date()); - Context.getDeviceManager().updateLatestPosition(position); - } - } - JsonArray errors = object.getJsonArray("errors"); - if (errors != null) { - for (int i = 0; i < errors.size(); i++) { - JsonObject error = errors.getJsonObject(i); - Log.warning("Error in flespi channel: " + error.toString()); - } - if (result == null || result.size() == 0) { - stopPullTask(); - } - } - } - return null; - } - - @Override - public void onThrowable(Throwable t) { - t.printStackTrace(); - } - }); - } - - private Position decodePosition(JsonObject msg) { - Device device = null; - try { - device = Context.getIdentityManager().getByUniqueId(msg.getString("ident")); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - if (device == null) { - return null; - } - Position position = new Position(); - position.setDeviceId(device.getId()); - - - position.setProtocol("flespi"); - - position.setTime(new Date((long) msg.getJsonNumber("timestamp").doubleValue() * 1000)); - JsonNumber lat = msg.getJsonNumber("position.latitude"); - JsonNumber lon = msg.getJsonNumber("position.longitude"); - position.setLatitude((lat != null && lon != null) ? lat.doubleValue() : 0); - position.setLongitude((lat != null && lon != null) ? lon.doubleValue() : 0); - - JsonNumber speed = msg.getJsonNumber("position.speed"); - position.setSpeed(speed != null ? speed.doubleValue() : 0); - - JsonNumber course = msg.getJsonNumber("position.direction"); - position.setCourse(course != null ? course.doubleValue() : 0); - - JsonNumber altitude = msg.getJsonNumber("position.altitude"); - position.setAltitude(altitude != null ? altitude.doubleValue() : 0); - - int satellites = msg.getInt("position.satellites", 0); - position.setValid(lat != null && lon != null && satellites >= 3); - position.set(Position.KEY_SATELLITES, satellites); - - return position; - } -} -- cgit v1.2.3 From fb82a8167b99f706cb25d828bf1d72424ba98a75 Mon Sep 17 00:00:00 2001 From: namo Date: Thu, 19 Oct 2017 18:00:30 +0300 Subject: flespi integration: listening messages --- setup/default.xml | 1 + src/org/traccar/BaseProtocolDecoder.java | 9 ++ src/org/traccar/ExtendedObjectDecoder.java | 13 +++ src/org/traccar/protocol/FlespiProtocol.java | 49 +++++++++ .../traccar/protocol/FlespiProtocolDecoder.java | 121 +++++++++++++++++++++ 5 files changed, 193 insertions(+) create mode 100644 src/org/traccar/protocol/FlespiProtocol.java create mode 100644 src/org/traccar/protocol/FlespiProtocolDecoder.java (limited to 'src/org') diff --git a/setup/default.xml b/setup/default.xml index 95b69e216..79455d846 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -217,5 +217,6 @@ 5146 5147 5148 + 5149 diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 2d6286bf8..b0fb72225 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -29,6 +29,8 @@ import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.Set; +import java.util.Iterator; import java.sql.SQLException; public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { @@ -205,6 +207,13 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { if (!positions.isEmpty()) { position = (Position) positions.iterator().next(); } + } else if (decodedMessage instanceof Map) { + Set deviceIds = ((HashMap) decodedMessage).keySet(); + Iterator deviceIdsIterator = deviceIds.iterator(); + while (deviceIdsIterator.hasNext()) { + Context.getConnectionManager().updateDevice( + deviceIdsIterator.next(), Device.STATUS_ONLINE, new Date()); + } } } if (position != null) { diff --git a/src/org/traccar/ExtendedObjectDecoder.java b/src/org/traccar/ExtendedObjectDecoder.java index 268e6f688..a9b3a1053 100644 --- a/src/org/traccar/ExtendedObjectDecoder.java +++ b/src/org/traccar/ExtendedObjectDecoder.java @@ -29,6 +29,9 @@ import javax.xml.bind.DatatypeConverter; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; public abstract class ExtendedObjectDecoder implements ChannelUpstreamHandler { @@ -69,6 +72,16 @@ public abstract class ExtendedObjectDecoder implements ChannelUpstreamHandler { saveOriginal(o, originalMessage); Channels.fireMessageReceived(ctx, o, e.getRemoteAddress()); } + } else if (decodedMessage instanceof Map) { + Iterator it = ((Map) decodedMessage).entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = (Map.Entry) it.next(); + List positions = (List) pair.getValue(); + for (Position position : positions) { + saveOriginal(position, originalMessage); + Channels.fireMessageReceived(ctx, position, e.getRemoteAddress()); + } + } } else { saveOriginal(decodedMessage, originalMessage); Channels.fireMessageReceived(ctx, decodedMessage, e.getRemoteAddress()); diff --git a/src/org/traccar/protocol/FlespiProtocol.java b/src/org/traccar/protocol/FlespiProtocol.java new file mode 100644 index 000000000..1164dac2d --- /dev/null +++ b/src/org/traccar/protocol/FlespiProtocol.java @@ -0,0 +1,49 @@ +/* + * Copyright 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.http.HttpChunkAggregator; +import org.jboss.netty.handler.codec.http.HttpRequestDecoder; +import org.jboss.netty.handler.codec.http.HttpResponseEncoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; +import org.traccar.model.Command; + +import java.util.List; + +public class FlespiProtocol extends BaseProtocol { + + public FlespiProtocol() { + super("flespi"); + setSupportedDataCommands( + Command.TYPE_CUSTOM); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("httpEncoder", new HttpResponseEncoder()); + pipeline.addLast("httpDecoder", new HttpRequestDecoder()); + pipeline.addLast("httpAggregator", new HttpChunkAggregator(Integer.MAX_VALUE)); + pipeline.addLast("objectDecoder", new FlespiProtocolDecoder(FlespiProtocol.this)); + } + }); + } +} diff --git a/src/org/traccar/protocol/FlespiProtocolDecoder.java b/src/org/traccar/protocol/FlespiProtocolDecoder.java new file mode 100644 index 000000000..6d5d68382 --- /dev/null +++ b/src/org/traccar/protocol/FlespiProtocolDecoder.java @@ -0,0 +1,121 @@ +/* + * Copyright 2013 - 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.handler.codec.http.DefaultHttpResponse; +import org.jboss.netty.handler.codec.http.HttpRequest; +import org.jboss.netty.handler.codec.http.HttpResponse; +import org.jboss.netty.handler.codec.http.HttpResponseStatus; +import org.jboss.netty.handler.codec.http.HttpVersion; +import org.jboss.netty.handler.codec.http.HttpHeaders; +import org.jboss.netty.util.CharsetUtil; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.Log; +import org.traccar.model.Position; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonNumber; +import javax.json.JsonObject; +import java.io.StringReader; +import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.LinkedList; +import java.util.Map; +import java.util.Date; + +public class FlespiProtocolDecoder extends BaseProtocolDecoder { + + public FlespiProtocolDecoder(FlespiProtocol protocol) { + super(protocol); + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + HttpRequest request = (HttpRequest) msg; + JsonArray result = Json.createReader(new StringReader(request.getContent().toString(StandardCharsets.UTF_8))) + .readArray(); + Map unitsPositions = new HashMap<>(); + Log.debug(String.format("messages received msgs_count=%d", result.size())); + for (int i = 0; i < result.size(); i++) { + JsonObject message = result.getJsonObject(i); + String ident = message.getString("ident"); + if (ident == null) { + continue; + } + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident); + if (deviceSession == null) { + continue; + } + if (unitsPositions.get(deviceSession.getDeviceId()) == null) { + unitsPositions.put(deviceSession.getDeviceId(), new LinkedList()); + } + Position position = new Position(); + position.setDeviceId(deviceSession.getDeviceId()); + decodePosition(message, position); + unitsPositions.get(deviceSession.getDeviceId()).add(position); + } + + sendResponse(channel, HttpResponseStatus.OK); + return unitsPositions; + } + + private void sendResponse(Channel channel, HttpResponseStatus status) { + if (channel != null) { + HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status); + response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, 10); + response.setContent(ChannelBuffers.copiedBuffer("Hello namo", CharsetUtil.US_ASCII)); + channel.write(response); + } + } + + private void decodePosition(JsonObject msg, Position position) { + position.setProtocol("flespi"); + + position.setTime(new Date((long) msg.getJsonNumber("timestamp").doubleValue() * 1000)); + JsonNumber lat = msg.getJsonNumber("position.latitude"); + JsonNumber lon = msg.getJsonNumber("position.longitude"); + position.setLatitude((lat != null && lon != null) ? lat.doubleValue() : 0); + position.setLongitude((lat != null && lon != null) ? lon.doubleValue() : 0); + + JsonNumber speed = msg.getJsonNumber("position.speed"); + position.setSpeed(speed != null ? speed.doubleValue() : 0); + if (position.getSpeed() == 111) { + position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); + } + + JsonNumber course = msg.getJsonNumber("position.direction"); + position.setCourse(course != null ? course.doubleValue() : 0); + + JsonNumber altitude = msg.getJsonNumber("position.altitude"); + position.setAltitude(altitude != null ? altitude.doubleValue() : 0); + + int satellites = msg.getInt("position.satellites", 0); + position.setValid(position.getLatitude() != 0 && position.getLongitude() != 0 && satellites >= 3); + position.set(Position.KEY_SATELLITES, satellites); + + if (msg.getBoolean("alarm.event.trigger", false)) { + position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); + } + } +} -- cgit v1.2.3 From 8483681361a386d0159422ed089ed3360161811c Mon Sep 17 00:00:00 2001 From: namo Date: Fri, 20 Oct 2017 09:30:36 +0300 Subject: Revert "flespi integration: listening messages" This reverts commit fb82a8167b99f706cb25d828bf1d72424ba98a75. --- setup/default.xml | 1 - src/org/traccar/BaseProtocolDecoder.java | 9 -- src/org/traccar/ExtendedObjectDecoder.java | 13 --- src/org/traccar/protocol/FlespiProtocol.java | 49 --------- .../traccar/protocol/FlespiProtocolDecoder.java | 121 --------------------- 5 files changed, 193 deletions(-) delete mode 100644 src/org/traccar/protocol/FlespiProtocol.java delete mode 100644 src/org/traccar/protocol/FlespiProtocolDecoder.java (limited to 'src/org') diff --git a/setup/default.xml b/setup/default.xml index 79455d846..95b69e216 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -217,6 +217,5 @@ 5146 5147 5148 - 5149 diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index b0fb72225..2d6286bf8 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -29,8 +29,6 @@ import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Map; -import java.util.Set; -import java.util.Iterator; import java.sql.SQLException; public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { @@ -207,13 +205,6 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { if (!positions.isEmpty()) { position = (Position) positions.iterator().next(); } - } else if (decodedMessage instanceof Map) { - Set deviceIds = ((HashMap) decodedMessage).keySet(); - Iterator deviceIdsIterator = deviceIds.iterator(); - while (deviceIdsIterator.hasNext()) { - Context.getConnectionManager().updateDevice( - deviceIdsIterator.next(), Device.STATUS_ONLINE, new Date()); - } } } if (position != null) { diff --git a/src/org/traccar/ExtendedObjectDecoder.java b/src/org/traccar/ExtendedObjectDecoder.java index a9b3a1053..268e6f688 100644 --- a/src/org/traccar/ExtendedObjectDecoder.java +++ b/src/org/traccar/ExtendedObjectDecoder.java @@ -29,9 +29,6 @@ import javax.xml.bind.DatatypeConverter; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Map; public abstract class ExtendedObjectDecoder implements ChannelUpstreamHandler { @@ -72,16 +69,6 @@ public abstract class ExtendedObjectDecoder implements ChannelUpstreamHandler { saveOriginal(o, originalMessage); Channels.fireMessageReceived(ctx, o, e.getRemoteAddress()); } - } else if (decodedMessage instanceof Map) { - Iterator it = ((Map) decodedMessage).entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry) it.next(); - List positions = (List) pair.getValue(); - for (Position position : positions) { - saveOriginal(position, originalMessage); - Channels.fireMessageReceived(ctx, position, e.getRemoteAddress()); - } - } } else { saveOriginal(decodedMessage, originalMessage); Channels.fireMessageReceived(ctx, decodedMessage, e.getRemoteAddress()); diff --git a/src/org/traccar/protocol/FlespiProtocol.java b/src/org/traccar/protocol/FlespiProtocol.java deleted file mode 100644 index 1164dac2d..000000000 --- a/src/org/traccar/protocol/FlespiProtocol.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.protocol; - -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.http.HttpChunkAggregator; -import org.jboss.netty.handler.codec.http.HttpRequestDecoder; -import org.jboss.netty.handler.codec.http.HttpResponseEncoder; -import org.traccar.BaseProtocol; -import org.traccar.TrackerServer; -import org.traccar.model.Command; - -import java.util.List; - -public class FlespiProtocol extends BaseProtocol { - - public FlespiProtocol() { - super("flespi"); - setSupportedDataCommands( - Command.TYPE_CUSTOM); - } - - @Override - public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { - @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("httpEncoder", new HttpResponseEncoder()); - pipeline.addLast("httpDecoder", new HttpRequestDecoder()); - pipeline.addLast("httpAggregator", new HttpChunkAggregator(Integer.MAX_VALUE)); - pipeline.addLast("objectDecoder", new FlespiProtocolDecoder(FlespiProtocol.this)); - } - }); - } -} diff --git a/src/org/traccar/protocol/FlespiProtocolDecoder.java b/src/org/traccar/protocol/FlespiProtocolDecoder.java deleted file mode 100644 index 6d5d68382..000000000 --- a/src/org/traccar/protocol/FlespiProtocolDecoder.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright 2013 - 2017 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.protocol; - -import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.handler.codec.http.DefaultHttpResponse; -import org.jboss.netty.handler.codec.http.HttpRequest; -import org.jboss.netty.handler.codec.http.HttpResponse; -import org.jboss.netty.handler.codec.http.HttpResponseStatus; -import org.jboss.netty.handler.codec.http.HttpVersion; -import org.jboss.netty.handler.codec.http.HttpHeaders; -import org.jboss.netty.util.CharsetUtil; -import org.traccar.BaseProtocolDecoder; -import org.traccar.DeviceSession; -import org.traccar.helper.Log; -import org.traccar.model.Position; - -import javax.json.Json; -import javax.json.JsonArray; -import javax.json.JsonNumber; -import javax.json.JsonObject; -import java.io.StringReader; -import java.net.SocketAddress; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.List; -import java.util.LinkedList; -import java.util.Map; -import java.util.Date; - -public class FlespiProtocolDecoder extends BaseProtocolDecoder { - - public FlespiProtocolDecoder(FlespiProtocol protocol) { - super(protocol); - } - - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - - HttpRequest request = (HttpRequest) msg; - JsonArray result = Json.createReader(new StringReader(request.getContent().toString(StandardCharsets.UTF_8))) - .readArray(); - Map unitsPositions = new HashMap<>(); - Log.debug(String.format("messages received msgs_count=%d", result.size())); - for (int i = 0; i < result.size(); i++) { - JsonObject message = result.getJsonObject(i); - String ident = message.getString("ident"); - if (ident == null) { - continue; - } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident); - if (deviceSession == null) { - continue; - } - if (unitsPositions.get(deviceSession.getDeviceId()) == null) { - unitsPositions.put(deviceSession.getDeviceId(), new LinkedList()); - } - Position position = new Position(); - position.setDeviceId(deviceSession.getDeviceId()); - decodePosition(message, position); - unitsPositions.get(deviceSession.getDeviceId()).add(position); - } - - sendResponse(channel, HttpResponseStatus.OK); - return unitsPositions; - } - - private void sendResponse(Channel channel, HttpResponseStatus status) { - if (channel != null) { - HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status); - response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, 10); - response.setContent(ChannelBuffers.copiedBuffer("Hello namo", CharsetUtil.US_ASCII)); - channel.write(response); - } - } - - private void decodePosition(JsonObject msg, Position position) { - position.setProtocol("flespi"); - - position.setTime(new Date((long) msg.getJsonNumber("timestamp").doubleValue() * 1000)); - JsonNumber lat = msg.getJsonNumber("position.latitude"); - JsonNumber lon = msg.getJsonNumber("position.longitude"); - position.setLatitude((lat != null && lon != null) ? lat.doubleValue() : 0); - position.setLongitude((lat != null && lon != null) ? lon.doubleValue() : 0); - - JsonNumber speed = msg.getJsonNumber("position.speed"); - position.setSpeed(speed != null ? speed.doubleValue() : 0); - if (position.getSpeed() == 111) { - position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); - } - - JsonNumber course = msg.getJsonNumber("position.direction"); - position.setCourse(course != null ? course.doubleValue() : 0); - - JsonNumber altitude = msg.getJsonNumber("position.altitude"); - position.setAltitude(altitude != null ? altitude.doubleValue() : 0); - - int satellites = msg.getInt("position.satellites", 0); - position.setValid(position.getLatitude() != 0 && position.getLongitude() != 0 && satellites >= 3); - position.set(Position.KEY_SATELLITES, satellites); - - if (msg.getBoolean("alarm.event.trigger", false)) { - position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); - } - } -} -- cgit v1.2.3 From 563776604e5acfcde8f83ab779406caa8a069ef4 Mon Sep 17 00:00:00 2001 From: namo Date: Fri, 20 Oct 2017 09:42:01 +0300 Subject: flespi integration: listening messages (rev.2) --- setup/default.xml | 1 + src/org/traccar/protocol/FlespiProtocol.java | 46 ++++++++ .../traccar/protocol/FlespiProtocolDecoder.java | 116 +++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 src/org/traccar/protocol/FlespiProtocol.java create mode 100644 src/org/traccar/protocol/FlespiProtocolDecoder.java (limited to 'src/org') diff --git a/setup/default.xml b/setup/default.xml index 95b69e216..79455d846 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -217,5 +217,6 @@ 5146 5147 5148 + 5149 diff --git a/src/org/traccar/protocol/FlespiProtocol.java b/src/org/traccar/protocol/FlespiProtocol.java new file mode 100644 index 000000000..285fcd9f1 --- /dev/null +++ b/src/org/traccar/protocol/FlespiProtocol.java @@ -0,0 +1,46 @@ +/* + * Copyright 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.http.HttpChunkAggregator; +import org.jboss.netty.handler.codec.http.HttpRequestDecoder; +import org.jboss.netty.handler.codec.http.HttpResponseEncoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class FlespiProtocol extends BaseProtocol { + + public FlespiProtocol() { + super("flespi"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("httpEncoder", new HttpResponseEncoder()); + pipeline.addLast("httpDecoder", new HttpRequestDecoder()); + pipeline.addLast("httpAggregator", new HttpChunkAggregator(Integer.MAX_VALUE)); + pipeline.addLast("objectDecoder", new FlespiProtocolDecoder(FlespiProtocol.this)); + } + }); + } +} diff --git a/src/org/traccar/protocol/FlespiProtocolDecoder.java b/src/org/traccar/protocol/FlespiProtocolDecoder.java new file mode 100644 index 000000000..a98529b19 --- /dev/null +++ b/src/org/traccar/protocol/FlespiProtocolDecoder.java @@ -0,0 +1,116 @@ +/* + * Copyright 2013 - 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.handler.codec.http.DefaultHttpResponse; +import org.jboss.netty.handler.codec.http.HttpRequest; +import org.jboss.netty.handler.codec.http.HttpResponse; +import org.jboss.netty.handler.codec.http.HttpResponseStatus; +import org.jboss.netty.handler.codec.http.HttpVersion; +import org.jboss.netty.handler.codec.http.HttpHeaders; +import org.jboss.netty.util.CharsetUtil; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.Log; +import org.traccar.model.Position; + +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonNumber; +import javax.json.JsonObject; +import java.io.StringReader; +import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.LinkedList; +import java.util.Date; + +public class FlespiProtocolDecoder extends BaseProtocolDecoder { + + public FlespiProtocolDecoder(FlespiProtocol protocol) { + super(protocol); + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + HttpRequest request = (HttpRequest) msg; + JsonArray result = Json.createReader(new StringReader(request.getContent().toString(StandardCharsets.UTF_8))) + .readArray(); + List positions = new LinkedList<>(); + Log.debug(String.format("messages received msgs_count=%d", result.size())); + for (int i = 0; i < result.size(); i++) { + JsonObject message = result.getJsonObject(i); + String ident = message.getString("ident"); + if (ident == null) { + continue; + } + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident); + if (deviceSession == null) { + continue; + } + Position position = new Position(); + position.setDeviceId(deviceSession.getDeviceId()); + decodePosition(message, position); + positions.add(position); + } + + sendResponse(channel, HttpResponseStatus.OK); + return positions; + } + + private void sendResponse(Channel channel, HttpResponseStatus status) { + if (channel != null) { + HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status); + response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, 10); + response.setContent(ChannelBuffers.copiedBuffer("Hello namo", CharsetUtil.US_ASCII)); + channel.write(response); + } + } + + private void decodePosition(JsonObject msg, Position position) { + position.setProtocol("flespi"); + + position.setTime(new Date((long) msg.getJsonNumber("timestamp").doubleValue() * 1000)); + JsonNumber lat = msg.getJsonNumber("position.latitude"); + JsonNumber lon = msg.getJsonNumber("position.longitude"); + position.setLatitude((lat != null && lon != null) ? lat.doubleValue() : 0); + position.setLongitude((lat != null && lon != null) ? lon.doubleValue() : 0); + + JsonNumber speed = msg.getJsonNumber("position.speed"); + position.setSpeed(speed != null ? speed.doubleValue() : 0); + if (position.getSpeed() == 111) { + position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); + } + + JsonNumber course = msg.getJsonNumber("position.direction"); + position.setCourse(course != null ? course.doubleValue() : 0); + + JsonNumber altitude = msg.getJsonNumber("position.altitude"); + position.setAltitude(altitude != null ? altitude.doubleValue() : 0); + + int satellites = msg.getInt("position.satellites", 0); + position.setValid(position.getLatitude() != 0 && position.getLongitude() != 0 && satellites >= 3); + position.set(Position.KEY_SATELLITES, satellites); + + if (msg.getBoolean("alarm.event.trigger", false)) { + position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); + } + } +} -- cgit v1.2.3 From f20b926497a4d61b6fefcdba57c0ebfb23ef5fc2 Mon Sep 17 00:00:00 2001 From: namo Date: Fri, 20 Oct 2017 10:48:16 +0300 Subject: flespi integration: listening messages (rev.3) --- src/org/traccar/protocol/FlespiProtocol.java | 2 +- .../traccar/protocol/FlespiProtocolDecoder.java | 41 ++++++++++------------ 2 files changed, 19 insertions(+), 24 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/FlespiProtocol.java b/src/org/traccar/protocol/FlespiProtocol.java index 285fcd9f1..d22bd7ae0 100644 --- a/src/org/traccar/protocol/FlespiProtocol.java +++ b/src/org/traccar/protocol/FlespiProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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. diff --git a/src/org/traccar/protocol/FlespiProtocolDecoder.java b/src/org/traccar/protocol/FlespiProtocolDecoder.java index a98529b19..1940673c3 100644 --- a/src/org/traccar/protocol/FlespiProtocolDecoder.java +++ b/src/org/traccar/protocol/FlespiProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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. @@ -15,7 +15,6 @@ */ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.jboss.netty.handler.codec.http.DefaultHttpResponse; import org.jboss.netty.handler.codec.http.HttpRequest; @@ -23,10 +22,8 @@ import org.jboss.netty.handler.codec.http.HttpResponse; import org.jboss.netty.handler.codec.http.HttpResponseStatus; import org.jboss.netty.handler.codec.http.HttpVersion; import org.jboss.netty.handler.codec.http.HttpHeaders; -import org.jboss.netty.util.CharsetUtil; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; -import org.traccar.helper.Log; import org.traccar.model.Position; import javax.json.Json; @@ -42,8 +39,11 @@ import java.util.Date; public class FlespiProtocolDecoder extends BaseProtocolDecoder { + private final String protocolName; + public FlespiProtocolDecoder(FlespiProtocol protocol) { super(protocol); + protocolName = protocol.getName(); } @Override @@ -54,7 +54,6 @@ public class FlespiProtocolDecoder extends BaseProtocolDecoder { JsonArray result = Json.createReader(new StringReader(request.getContent().toString(StandardCharsets.UTF_8))) .readArray(); List positions = new LinkedList<>(); - Log.debug(String.format("messages received msgs_count=%d", result.size())); for (int i = 0; i < result.size(); i++) { JsonObject message = result.getJsonObject(i); String ident = message.getString("ident"); @@ -78,38 +77,34 @@ public class FlespiProtocolDecoder extends BaseProtocolDecoder { private void sendResponse(Channel channel, HttpResponseStatus status) { if (channel != null) { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status); - response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, 10); - response.setContent(ChannelBuffers.copiedBuffer("Hello namo", CharsetUtil.US_ASCII)); + response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, 0); channel.write(response); } } - private void decodePosition(JsonObject msg, Position position) { - position.setProtocol("flespi"); + private void decodePosition(JsonObject object, Position position) { + position.setProtocol(protocolName); - position.setTime(new Date((long) msg.getJsonNumber("timestamp").doubleValue() * 1000)); - JsonNumber lat = msg.getJsonNumber("position.latitude"); - JsonNumber lon = msg.getJsonNumber("position.longitude"); - position.setLatitude((lat != null && lon != null) ? lat.doubleValue() : 0); - position.setLongitude((lat != null && lon != null) ? lon.doubleValue() : 0); + position.setTime(new Date((long) object.getJsonNumber("timestamp").doubleValue() * 1000)); + JsonNumber lat = object.getJsonNumber("position.latitude"); + JsonNumber lon = object.getJsonNumber("position.longitude"); + position.setLatitude(lat != null ? lat.doubleValue() : 0); + position.setLongitude(lon != null ? lon.doubleValue() : 0); - JsonNumber speed = msg.getJsonNumber("position.speed"); + JsonNumber speed = object.getJsonNumber("position.speed"); position.setSpeed(speed != null ? speed.doubleValue() : 0); - if (position.getSpeed() == 111) { - position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); - } - JsonNumber course = msg.getJsonNumber("position.direction"); + JsonNumber course = object.getJsonNumber("position.direction"); position.setCourse(course != null ? course.doubleValue() : 0); - JsonNumber altitude = msg.getJsonNumber("position.altitude"); + JsonNumber altitude = object.getJsonNumber("position.altitude"); position.setAltitude(altitude != null ? altitude.doubleValue() : 0); - int satellites = msg.getInt("position.satellites", 0); - position.setValid(position.getLatitude() != 0 && position.getLongitude() != 0 && satellites >= 3); + int satellites = object.getInt("position.satellites", 0); + position.setValid(object.getBoolean("position.valid", true)); position.set(Position.KEY_SATELLITES, satellites); - if (msg.getBoolean("alarm.event.trigger", false)) { + if (object.getBoolean("alarm.event.trigger", false)) { position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); } } -- cgit v1.2.3 From cf1f0c95cb2f948abfffc5cb207926c916e636c0 Mon Sep 17 00:00:00 2001 From: namo Date: Fri, 20 Oct 2017 13:52:15 +0300 Subject: flespi integration: listening messages (rev.4) --- .../traccar/protocol/FlespiProtocolDecoder.java | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/FlespiProtocolDecoder.java b/src/org/traccar/protocol/FlespiProtocolDecoder.java index 1940673c3..3a12cd7b8 100644 --- a/src/org/traccar/protocol/FlespiProtocolDecoder.java +++ b/src/org/traccar/protocol/FlespiProtocolDecoder.java @@ -88,21 +88,25 @@ public class FlespiProtocolDecoder extends BaseProtocolDecoder { position.setTime(new Date((long) object.getJsonNumber("timestamp").doubleValue() * 1000)); JsonNumber lat = object.getJsonNumber("position.latitude"); JsonNumber lon = object.getJsonNumber("position.longitude"); - position.setLatitude(lat != null ? lat.doubleValue() : 0); - position.setLongitude(lon != null ? lon.doubleValue() : 0); - + if (lat != null && lon != null) { + position.setLatitude(lat.doubleValue()); + position.setLongitude(lon.doubleValue()); + } JsonNumber speed = object.getJsonNumber("position.speed"); - position.setSpeed(speed != null ? speed.doubleValue() : 0); - + if (speed != null) { + position.setSpeed(speed.doubleValue()); + } JsonNumber course = object.getJsonNumber("position.direction"); - position.setCourse(course != null ? course.doubleValue() : 0); - + if (course != null) { + position.setCourse(course.doubleValue()); + } JsonNumber altitude = object.getJsonNumber("position.altitude"); - position.setAltitude(altitude != null ? altitude.doubleValue() : 0); + if (altitude != null) { + position.setAltitude(altitude.doubleValue()); + } - int satellites = object.getInt("position.satellites", 0); position.setValid(object.getBoolean("position.valid", true)); - position.set(Position.KEY_SATELLITES, satellites); + position.set(Position.KEY_SATELLITES, object.getInt("position.satellites", 0)); if (object.getBoolean("alarm.event.trigger", false)) { position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); -- cgit v1.2.3 From ad8ee1702e02e02533e9f20e06af960b2cf3c647 Mon Sep 17 00:00:00 2001 From: namo Date: Fri, 20 Oct 2017 18:50:16 +0300 Subject: flespi integration: listening messages (rev.5): unit test added --- .../traccar/protocol/FlespiProtocolDecoder.java | 5 +++-- test/org/traccar/ProtocolTest.java | 14 ++++++++----- .../protocol/FlespiProtocolDecoderTest.java | 24 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 test/org/traccar/protocol/FlespiProtocolDecoderTest.java (limited to 'src/org') diff --git a/src/org/traccar/protocol/FlespiProtocolDecoder.java b/src/org/traccar/protocol/FlespiProtocolDecoder.java index 3a12cd7b8..69a977577 100644 --- a/src/org/traccar/protocol/FlespiProtocolDecoder.java +++ b/src/org/traccar/protocol/FlespiProtocolDecoder.java @@ -30,6 +30,7 @@ import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonNumber; import javax.json.JsonObject; +import javax.json.JsonString; import java.io.StringReader; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; @@ -56,11 +57,11 @@ public class FlespiProtocolDecoder extends BaseProtocolDecoder { List positions = new LinkedList<>(); for (int i = 0; i < result.size(); i++) { JsonObject message = result.getJsonObject(i); - String ident = message.getString("ident"); + JsonString ident = message.getJsonString("ident"); if (ident == null) { continue; } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident.getString()); if (deviceSession == null) { continue; } diff --git a/test/org/traccar/ProtocolTest.java b/test/org/traccar/ProtocolTest.java index 1daefabd6..daff777a6 100644 --- a/test/org/traccar/ProtocolTest.java +++ b/test/org/traccar/ProtocolTest.java @@ -98,6 +98,10 @@ public class ProtocolTest extends BaseTest { verifyDecodedPosition(decoder.decode(null, null, object), false, true, null); } + protected void verifyAttributesList(BaseProtocolDecoder decoder, Object object) throws Exception { + verifyDecodedList(decoder.decode(null, null, object), false, true, null); + } + protected void verifyPosition(BaseProtocolDecoder decoder, Object object) throws Exception { verifyDecodedPosition(decoder.decode(null, null, object), true, false, null); } @@ -107,25 +111,25 @@ public class ProtocolTest extends BaseTest { } protected void verifyPositions(BaseProtocolDecoder decoder, Object object) throws Exception { - verifyDecodedList(decoder.decode(null, null, object), true, null); + verifyDecodedList(decoder.decode(null, null, object), true, false, null); } protected void verifyPositions(BaseProtocolDecoder decoder, boolean checkLocation, Object object) throws Exception { - verifyDecodedList(decoder.decode(null, null, object), checkLocation, null); + verifyDecodedList(decoder.decode(null, null, object), checkLocation, false, null); } protected void verifyPositions(BaseProtocolDecoder decoder, Object object, Position position) throws Exception { - verifyDecodedList(decoder.decode(null, null, object), true, position); + verifyDecodedList(decoder.decode(null, null, object), true, false, position); } - private void verifyDecodedList(Object decodedObject, boolean checkLocation, Position expected) { + private void verifyDecodedList(Object decodedObject, boolean checkLocation, boolean checkAttributes, Position expected) { Assert.assertNotNull("list is null", decodedObject); Assert.assertTrue("not a list", decodedObject instanceof List); Assert.assertFalse("list is empty", ((List) decodedObject).isEmpty()); for (Object item : (List) decodedObject) { - verifyDecodedPosition(item, checkLocation, false, expected); + verifyDecodedPosition(item, checkLocation, checkAttributes, expected); } } diff --git a/test/org/traccar/protocol/FlespiProtocolDecoderTest.java b/test/org/traccar/protocol/FlespiProtocolDecoderTest.java new file mode 100644 index 000000000..badeaa76e --- /dev/null +++ b/test/org/traccar/protocol/FlespiProtocolDecoderTest.java @@ -0,0 +1,24 @@ +package org.traccar.protocol; + +import org.jboss.netty.handler.codec.http.HttpMethod; +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class FlespiProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + FlespiProtocolDecoder decoder = new FlespiProtocolDecoder(new FlespiProtocol()); + + verifyPositions(decoder, request(HttpMethod.POST, "/", + buffer("[{\"position.speed\":0,\"position.latitude\":53.90573,\"time.valid.status\":true,\"timestamp\":1506956075,\"position.satellites\":10,\"message.buffered.status\":false,\"business.mode.status\":true,\"gps.status\":true,\"position.longitude\":27.455848,\"position.direction\":0,\"ident\":\"605630\"},{\"siren.status\":false,\"business.mode.status\":true,\"position.satellites\":8,\"timestamp\":1506695785,\"led.status\":false,\"position.latitude\":53.905569,\"position.longitude\":27.455986,\"position.speed\":0,\"gradual.stop.status\":false,\"position.direction\":262.643854,\"hardware.version.enum\":223,\"vehicle.mileage\":160,\"message.buffered.status\":false,\"blinkers.status\":false,\"ident\":\"605630\",\"position.altitude\":233.48,\"immobilizer.status\":false}]"))); + + verifyPositions(decoder, request(HttpMethod.POST, "/", + buffer("[{\"geofence.inside.status\":false,\"position.valid\":false,\"ain#4\":0,\"rs232.sensor.value#1\":0,\"position.direction\":0,\"rs232.sensor.value#0\":0,\"position.speed\":0,\"position.latitude\":0,\"refrigerator.sensor.temperature#1\":62.5,\"gnss.antenna.cut.status\":true,\"din#4\":3,\"ain#3\":0,\"refrigerator.sensor.temperature#3\":71.4,\"position.altitude\":0,\"din#2\":3,\"shock.event.trigger\":false,\"alarm.mode.status\":false,\"ibutton.event.connect\":false,\"refrigerator.sensor.temperature#4\":66.7,\"internal.battery.voltage.limit.lower.status\":false,\"ain#2\":0,\"gsm.signal.level\":0,\"refrigerator.connection.status\":0,\"position.satellites\":0,\"din#1\":3,\"external.powersource.voltage.range.outside.status\":false,\"refrigerator.sensor.temperature#2\":68.2,\"incline.event.trigger\":false,\"alarm.event.trigger\":false,\"movement.status\":true,\"refrigerator.sensor.temperature#6\":68.9,\"ident\":\"50\",\"timestamp\":946684840,\"engine.ignition.status\":true,\"gsm.sim.status\":true,\"record.seqnum\":8165,\"external.powersource.voltage\":15.298,\"gnss.enum\":\"glonass\",\"position.longitude\":0,\"battery.voltage\":4.088,\"refrigerator.sensor.temperature#5\":71.3,\"din#3\":3,\"ain#1\":0,\"internal.bus.supply.voltage.range.outside.status\":false},{\"geofence.inside.status\":false,\"position.valid\":true,\"ain#4\":0,\"rs232.sensor.value#1\":0,\"position.direction\":0,\"rs232.sensor.value#0\":0,\"position.speed\":0,\"position.latitude\":57.986744,\"refrigerator.sensor.temperature#1\":74.1,\"gnss.antenna.cut.status\":false,\"din#4\":3,\"ain#3\":0,\"position.hdop\":21.1,\"refrigerator.sensor.temperature#3\":71.4,\"position.altitude\":219,\"din#2\":3,\"shock.event.trigger\":false,\"alarm.mode.status\":false,\"ibutton.event.connect\":false,\"refrigerator.sensor.temperature#4\":70.5,\"internal.battery.voltage.limit.lower.status\":false,\"ain#2\":0,\"gsm.signal.level\":0,\"refrigerator.connection.status\":0,\"position.satellites\":5,\"din#1\":3,\"external.powersource.voltage.range.outside.status\":false,\"refrigerator.sensor.temperature#2\":71.3,\"incline.event.trigger\":false,\"alarm.event.trigger\":false,\"movement.status\":true,\"refrigerator.sensor.temperature#6\":69.3,\"ident\":\"50\",\"timestamp\":1392272112,\"engine.ignition.status\":true,\"gsm.sim.status\":true,\"record.seqnum\":8174,\"external.powersource.voltage\":15.303,\"gnss.enum\":\"glonass\",\"position.longitude\":56.207576,\"battery.voltage\":3.934,\"refrigerator.sensor.temperature#5\":68.1,\"din#3\":3,\"ain#1\":0,\"internal.bus.supply.voltage.range.outside.status\":false}]"))); + + verifyAttributesList(decoder, request(HttpMethod.POST, "/", + buffer("[{\"ain#1\":1,\"ain#2\":0,\"ain#3\":0,\"ain#4\":0,\"alarm.event.trigger\":true,\"custom.SOS\":1,\"custom.dparam\":3.141593,\"custom.ign\":1,\"custom.iparam\":-55,\"custom.tparam\":\"lorem\",\"din#1\":false,\"din#10\":false,\"din#11\":false,\"din#12\":false,\"din#13\":false,\"din#14\":false,\"din#15\":false,\"din#16\":false,\"din#17\":false,\"din#18\":false,\"din#19\":false,\"din#2\":false,\"din#20\":false,\"din#21\":false,\"din#22\":false,\"din#23\":false,\"din#24\":false,\"din#25\":false,\"din#26\":false,\"din#27\":false,\"din#28\":false,\"din#29\":false,\"din#3\":false,\"din#30\":false,\"din#31\":false,\"din#32\":false,\"din#4\":false,\"din#5\":false,\"din#6\":false,\"din#7\":false,\"din#8\":false,\"din#9\":false,\"dout#1\":false,\"dout#10\":false,\"dout#11\":false,\"dout#12\":false,\"dout#13\":false,\"dout#14\":false,\"dout#15\":false,\"dout#16\":false,\"dout#17\":false,\"dout#18\":false,\"dout#19\":false,\"dout#2\":false,\"dout#20\":false,\"dout#21\":false,\"dout#22\":false,\"dout#23\":false,\"dout#24\":false,\"dout#25\":false,\"dout#26\":false,\"dout#27\":false,\"dout#28\":false,\"dout#29\":false,\"dout#3\":false,\"dout#30\":false,\"dout#31\":false,\"dout#32\":false,\"dout#4\":false,\"dout#5\":false,\"dout#6\":false,\"dout#7\":false,\"dout#8\":false,\"dout#9\":false,\"ident\":\"namo:namo\",\"position.altitude\":300,\"position.direction\":0,\"position.hdop\":1.1,\"position.latitude\":53.90821,\"position.longitude\":27.524165,\"position.satellites\":7,\"position.speed\":0,\"timestamp\":1508508510.013227}]"))); + } + +} -- cgit v1.2.3 From 4b8095a1c91ae2fc9bcd6cfc6e291c7b63af3cd1 Mon Sep 17 00:00:00 2001 From: namo Date: Tue, 24 Oct 2017 12:26:29 +0300 Subject: Revert "flespi integration: listening messages (rev.5): unit test added" This reverts commit ad8ee1702e02e02533e9f20e06af960b2cf3c647. --- .../traccar/protocol/FlespiProtocolDecoder.java | 5 ++--- test/org/traccar/ProtocolTest.java | 14 +++++-------- .../protocol/FlespiProtocolDecoderTest.java | 24 ---------------------- 3 files changed, 7 insertions(+), 36 deletions(-) delete mode 100644 test/org/traccar/protocol/FlespiProtocolDecoderTest.java (limited to 'src/org') diff --git a/src/org/traccar/protocol/FlespiProtocolDecoder.java b/src/org/traccar/protocol/FlespiProtocolDecoder.java index 69a977577..3a12cd7b8 100644 --- a/src/org/traccar/protocol/FlespiProtocolDecoder.java +++ b/src/org/traccar/protocol/FlespiProtocolDecoder.java @@ -30,7 +30,6 @@ import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonNumber; import javax.json.JsonObject; -import javax.json.JsonString; import java.io.StringReader; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; @@ -57,11 +56,11 @@ public class FlespiProtocolDecoder extends BaseProtocolDecoder { List positions = new LinkedList<>(); for (int i = 0; i < result.size(); i++) { JsonObject message = result.getJsonObject(i); - JsonString ident = message.getJsonString("ident"); + String ident = message.getString("ident"); if (ident == null) { continue; } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident.getString()); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident); if (deviceSession == null) { continue; } diff --git a/test/org/traccar/ProtocolTest.java b/test/org/traccar/ProtocolTest.java index daff777a6..1daefabd6 100644 --- a/test/org/traccar/ProtocolTest.java +++ b/test/org/traccar/ProtocolTest.java @@ -98,10 +98,6 @@ public class ProtocolTest extends BaseTest { verifyDecodedPosition(decoder.decode(null, null, object), false, true, null); } - protected void verifyAttributesList(BaseProtocolDecoder decoder, Object object) throws Exception { - verifyDecodedList(decoder.decode(null, null, object), false, true, null); - } - protected void verifyPosition(BaseProtocolDecoder decoder, Object object) throws Exception { verifyDecodedPosition(decoder.decode(null, null, object), true, false, null); } @@ -111,25 +107,25 @@ public class ProtocolTest extends BaseTest { } protected void verifyPositions(BaseProtocolDecoder decoder, Object object) throws Exception { - verifyDecodedList(decoder.decode(null, null, object), true, false, null); + verifyDecodedList(decoder.decode(null, null, object), true, null); } protected void verifyPositions(BaseProtocolDecoder decoder, boolean checkLocation, Object object) throws Exception { - verifyDecodedList(decoder.decode(null, null, object), checkLocation, false, null); + verifyDecodedList(decoder.decode(null, null, object), checkLocation, null); } protected void verifyPositions(BaseProtocolDecoder decoder, Object object, Position position) throws Exception { - verifyDecodedList(decoder.decode(null, null, object), true, false, position); + verifyDecodedList(decoder.decode(null, null, object), true, position); } - private void verifyDecodedList(Object decodedObject, boolean checkLocation, boolean checkAttributes, Position expected) { + private void verifyDecodedList(Object decodedObject, boolean checkLocation, Position expected) { Assert.assertNotNull("list is null", decodedObject); Assert.assertTrue("not a list", decodedObject instanceof List); Assert.assertFalse("list is empty", ((List) decodedObject).isEmpty()); for (Object item : (List) decodedObject) { - verifyDecodedPosition(item, checkLocation, checkAttributes, expected); + verifyDecodedPosition(item, checkLocation, false, expected); } } diff --git a/test/org/traccar/protocol/FlespiProtocolDecoderTest.java b/test/org/traccar/protocol/FlespiProtocolDecoderTest.java deleted file mode 100644 index badeaa76e..000000000 --- a/test/org/traccar/protocol/FlespiProtocolDecoderTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.traccar.protocol; - -import org.jboss.netty.handler.codec.http.HttpMethod; -import org.junit.Test; -import org.traccar.ProtocolTest; - -public class FlespiProtocolDecoderTest extends ProtocolTest { - - @Test - public void testDecode() throws Exception { - - FlespiProtocolDecoder decoder = new FlespiProtocolDecoder(new FlespiProtocol()); - - verifyPositions(decoder, request(HttpMethod.POST, "/", - buffer("[{\"position.speed\":0,\"position.latitude\":53.90573,\"time.valid.status\":true,\"timestamp\":1506956075,\"position.satellites\":10,\"message.buffered.status\":false,\"business.mode.status\":true,\"gps.status\":true,\"position.longitude\":27.455848,\"position.direction\":0,\"ident\":\"605630\"},{\"siren.status\":false,\"business.mode.status\":true,\"position.satellites\":8,\"timestamp\":1506695785,\"led.status\":false,\"position.latitude\":53.905569,\"position.longitude\":27.455986,\"position.speed\":0,\"gradual.stop.status\":false,\"position.direction\":262.643854,\"hardware.version.enum\":223,\"vehicle.mileage\":160,\"message.buffered.status\":false,\"blinkers.status\":false,\"ident\":\"605630\",\"position.altitude\":233.48,\"immobilizer.status\":false}]"))); - - verifyPositions(decoder, request(HttpMethod.POST, "/", - buffer("[{\"geofence.inside.status\":false,\"position.valid\":false,\"ain#4\":0,\"rs232.sensor.value#1\":0,\"position.direction\":0,\"rs232.sensor.value#0\":0,\"position.speed\":0,\"position.latitude\":0,\"refrigerator.sensor.temperature#1\":62.5,\"gnss.antenna.cut.status\":true,\"din#4\":3,\"ain#3\":0,\"refrigerator.sensor.temperature#3\":71.4,\"position.altitude\":0,\"din#2\":3,\"shock.event.trigger\":false,\"alarm.mode.status\":false,\"ibutton.event.connect\":false,\"refrigerator.sensor.temperature#4\":66.7,\"internal.battery.voltage.limit.lower.status\":false,\"ain#2\":0,\"gsm.signal.level\":0,\"refrigerator.connection.status\":0,\"position.satellites\":0,\"din#1\":3,\"external.powersource.voltage.range.outside.status\":false,\"refrigerator.sensor.temperature#2\":68.2,\"incline.event.trigger\":false,\"alarm.event.trigger\":false,\"movement.status\":true,\"refrigerator.sensor.temperature#6\":68.9,\"ident\":\"50\",\"timestamp\":946684840,\"engine.ignition.status\":true,\"gsm.sim.status\":true,\"record.seqnum\":8165,\"external.powersource.voltage\":15.298,\"gnss.enum\":\"glonass\",\"position.longitude\":0,\"battery.voltage\":4.088,\"refrigerator.sensor.temperature#5\":71.3,\"din#3\":3,\"ain#1\":0,\"internal.bus.supply.voltage.range.outside.status\":false},{\"geofence.inside.status\":false,\"position.valid\":true,\"ain#4\":0,\"rs232.sensor.value#1\":0,\"position.direction\":0,\"rs232.sensor.value#0\":0,\"position.speed\":0,\"position.latitude\":57.986744,\"refrigerator.sensor.temperature#1\":74.1,\"gnss.antenna.cut.status\":false,\"din#4\":3,\"ain#3\":0,\"position.hdop\":21.1,\"refrigerator.sensor.temperature#3\":71.4,\"position.altitude\":219,\"din#2\":3,\"shock.event.trigger\":false,\"alarm.mode.status\":false,\"ibutton.event.connect\":false,\"refrigerator.sensor.temperature#4\":70.5,\"internal.battery.voltage.limit.lower.status\":false,\"ain#2\":0,\"gsm.signal.level\":0,\"refrigerator.connection.status\":0,\"position.satellites\":5,\"din#1\":3,\"external.powersource.voltage.range.outside.status\":false,\"refrigerator.sensor.temperature#2\":71.3,\"incline.event.trigger\":false,\"alarm.event.trigger\":false,\"movement.status\":true,\"refrigerator.sensor.temperature#6\":69.3,\"ident\":\"50\",\"timestamp\":1392272112,\"engine.ignition.status\":true,\"gsm.sim.status\":true,\"record.seqnum\":8174,\"external.powersource.voltage\":15.303,\"gnss.enum\":\"glonass\",\"position.longitude\":56.207576,\"battery.voltage\":3.934,\"refrigerator.sensor.temperature#5\":68.1,\"din#3\":3,\"ain#1\":0,\"internal.bus.supply.voltage.range.outside.status\":false}]"))); - - verifyAttributesList(decoder, request(HttpMethod.POST, "/", - buffer("[{\"ain#1\":1,\"ain#2\":0,\"ain#3\":0,\"ain#4\":0,\"alarm.event.trigger\":true,\"custom.SOS\":1,\"custom.dparam\":3.141593,\"custom.ign\":1,\"custom.iparam\":-55,\"custom.tparam\":\"lorem\",\"din#1\":false,\"din#10\":false,\"din#11\":false,\"din#12\":false,\"din#13\":false,\"din#14\":false,\"din#15\":false,\"din#16\":false,\"din#17\":false,\"din#18\":false,\"din#19\":false,\"din#2\":false,\"din#20\":false,\"din#21\":false,\"din#22\":false,\"din#23\":false,\"din#24\":false,\"din#25\":false,\"din#26\":false,\"din#27\":false,\"din#28\":false,\"din#29\":false,\"din#3\":false,\"din#30\":false,\"din#31\":false,\"din#32\":false,\"din#4\":false,\"din#5\":false,\"din#6\":false,\"din#7\":false,\"din#8\":false,\"din#9\":false,\"dout#1\":false,\"dout#10\":false,\"dout#11\":false,\"dout#12\":false,\"dout#13\":false,\"dout#14\":false,\"dout#15\":false,\"dout#16\":false,\"dout#17\":false,\"dout#18\":false,\"dout#19\":false,\"dout#2\":false,\"dout#20\":false,\"dout#21\":false,\"dout#22\":false,\"dout#23\":false,\"dout#24\":false,\"dout#25\":false,\"dout#26\":false,\"dout#27\":false,\"dout#28\":false,\"dout#29\":false,\"dout#3\":false,\"dout#30\":false,\"dout#31\":false,\"dout#32\":false,\"dout#4\":false,\"dout#5\":false,\"dout#6\":false,\"dout#7\":false,\"dout#8\":false,\"dout#9\":false,\"ident\":\"namo:namo\",\"position.altitude\":300,\"position.direction\":0,\"position.hdop\":1.1,\"position.latitude\":53.90821,\"position.longitude\":27.524165,\"position.satellites\":7,\"position.speed\":0,\"timestamp\":1508508510.013227}]"))); - } - -} -- cgit v1.2.3 From 2ef987e1352c9d9483e062ff82fa83d0511a0972 Mon Sep 17 00:00:00 2001 From: namo Date: Tue, 24 Oct 2017 12:43:00 +0300 Subject: flespi integration: listening messages (rev.6): unit test added, getLastLocation added if position is absent --- .../traccar/protocol/FlespiProtocolDecoder.java | 10 ++++++--- .../protocol/FlespiProtocolDecoderTest.java | 24 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/org/traccar/protocol/FlespiProtocolDecoderTest.java (limited to 'src/org') diff --git a/src/org/traccar/protocol/FlespiProtocolDecoder.java b/src/org/traccar/protocol/FlespiProtocolDecoder.java index 3a12cd7b8..2e57c1dcf 100644 --- a/src/org/traccar/protocol/FlespiProtocolDecoder.java +++ b/src/org/traccar/protocol/FlespiProtocolDecoder.java @@ -30,6 +30,7 @@ import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonNumber; import javax.json.JsonObject; +import javax.json.JsonString; import java.io.StringReader; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; @@ -56,11 +57,11 @@ public class FlespiProtocolDecoder extends BaseProtocolDecoder { List positions = new LinkedList<>(); for (int i = 0; i < result.size(); i++) { JsonObject message = result.getJsonObject(i); - String ident = message.getString("ident"); + JsonString ident = message.getJsonString("ident"); if (ident == null) { continue; } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident.getString()); if (deviceSession == null) { continue; } @@ -85,12 +86,15 @@ public class FlespiProtocolDecoder extends BaseProtocolDecoder { private void decodePosition(JsonObject object, Position position) { position.setProtocol(protocolName); - position.setTime(new Date((long) object.getJsonNumber("timestamp").doubleValue() * 1000)); + Date deviceTime = new Date((long) object.getJsonNumber("timestamp").doubleValue() * 1000); + position.setTime(deviceTime); JsonNumber lat = object.getJsonNumber("position.latitude"); JsonNumber lon = object.getJsonNumber("position.longitude"); if (lat != null && lon != null) { position.setLatitude(lat.doubleValue()); position.setLongitude(lon.doubleValue()); + } else { + getLastLocation(position, deviceTime); } JsonNumber speed = object.getJsonNumber("position.speed"); if (speed != null) { diff --git a/test/org/traccar/protocol/FlespiProtocolDecoderTest.java b/test/org/traccar/protocol/FlespiProtocolDecoderTest.java new file mode 100644 index 000000000..b23da024a --- /dev/null +++ b/test/org/traccar/protocol/FlespiProtocolDecoderTest.java @@ -0,0 +1,24 @@ +package org.traccar.protocol; + +import org.jboss.netty.handler.codec.http.HttpMethod; +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class FlespiProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + FlespiProtocolDecoder decoder = new FlespiProtocolDecoder(new FlespiProtocol()); + + verifyPositions(decoder, request(HttpMethod.POST, "/", + buffer("[{\"position.speed\":0,\"position.latitude\":53.90573,\"time.valid.status\":true,\"timestamp\":1506956075,\"position.satellites\":10,\"message.buffered.status\":false,\"business.mode.status\":true,\"gps.status\":true,\"position.longitude\":27.455848,\"position.direction\":0,\"ident\":\"605630\"},{\"siren.status\":false,\"business.mode.status\":true,\"position.satellites\":8,\"timestamp\":1506695785,\"led.status\":false,\"position.latitude\":53.905569,\"position.longitude\":27.455986,\"position.speed\":0,\"gradual.stop.status\":false,\"position.direction\":262.643854,\"hardware.version.enum\":223,\"vehicle.mileage\":160,\"message.buffered.status\":false,\"blinkers.status\":false,\"ident\":\"605630\",\"position.altitude\":233.48,\"immobilizer.status\":false}]"))); + + verifyPositions(decoder, request(HttpMethod.POST, "/", + buffer("[{\"geofence.inside.status\":false,\"position.valid\":false,\"ain#4\":0,\"rs232.sensor.value#1\":0,\"position.direction\":0,\"rs232.sensor.value#0\":0,\"position.speed\":0,\"position.latitude\":0,\"refrigerator.sensor.temperature#1\":62.5,\"gnss.antenna.cut.status\":true,\"din#4\":3,\"ain#3\":0,\"refrigerator.sensor.temperature#3\":71.4,\"position.altitude\":0,\"din#2\":3,\"shock.event.trigger\":false,\"alarm.mode.status\":false,\"ibutton.event.connect\":false,\"refrigerator.sensor.temperature#4\":66.7,\"internal.battery.voltage.limit.lower.status\":false,\"ain#2\":0,\"gsm.signal.level\":0,\"refrigerator.connection.status\":0,\"position.satellites\":0,\"din#1\":3,\"external.powersource.voltage.range.outside.status\":false,\"refrigerator.sensor.temperature#2\":68.2,\"incline.event.trigger\":false,\"alarm.event.trigger\":false,\"movement.status\":true,\"refrigerator.sensor.temperature#6\":68.9,\"ident\":\"50\",\"timestamp\":946684840,\"engine.ignition.status\":true,\"gsm.sim.status\":true,\"record.seqnum\":8165,\"external.powersource.voltage\":15.298,\"gnss.enum\":\"glonass\",\"position.longitude\":0,\"battery.voltage\":4.088,\"refrigerator.sensor.temperature#5\":71.3,\"din#3\":3,\"ain#1\":0,\"internal.bus.supply.voltage.range.outside.status\":false},{\"geofence.inside.status\":false,\"position.valid\":true,\"ain#4\":0,\"rs232.sensor.value#1\":0,\"position.direction\":0,\"rs232.sensor.value#0\":0,\"position.speed\":0,\"position.latitude\":57.986744,\"refrigerator.sensor.temperature#1\":74.1,\"gnss.antenna.cut.status\":false,\"din#4\":3,\"ain#3\":0,\"position.hdop\":21.1,\"refrigerator.sensor.temperature#3\":71.4,\"position.altitude\":219,\"din#2\":3,\"shock.event.trigger\":false,\"alarm.mode.status\":false,\"ibutton.event.connect\":false,\"refrigerator.sensor.temperature#4\":70.5,\"internal.battery.voltage.limit.lower.status\":false,\"ain#2\":0,\"gsm.signal.level\":0,\"refrigerator.connection.status\":0,\"position.satellites\":5,\"din#1\":3,\"external.powersource.voltage.range.outside.status\":false,\"refrigerator.sensor.temperature#2\":71.3,\"incline.event.trigger\":false,\"alarm.event.trigger\":false,\"movement.status\":true,\"refrigerator.sensor.temperature#6\":69.3,\"ident\":\"50\",\"timestamp\":1392272112,\"engine.ignition.status\":true,\"gsm.sim.status\":true,\"record.seqnum\":8174,\"external.powersource.voltage\":15.303,\"gnss.enum\":\"glonass\",\"position.longitude\":56.207576,\"battery.voltage\":3.934,\"refrigerator.sensor.temperature#5\":68.1,\"din#3\":3,\"ain#1\":0,\"internal.bus.supply.voltage.range.outside.status\":false}]"))); + + verifyPositions(decoder, request(HttpMethod.POST, "/", + buffer("[{\"ain#1\":1,\"ain#2\":0,\"ain#3\":0,\"ain#4\":0,\"alarm.event.trigger\":true,\"custom.SOS\":1,\"custom.dparam\":3.141593,\"custom.ign\":1,\"custom.iparam\":-55,\"custom.tparam\":\"lorem\",\"din#1\":false,\"din#10\":false,\"din#11\":false,\"din#12\":false,\"din#13\":false,\"din#14\":false,\"din#15\":false,\"din#16\":false,\"din#17\":false,\"din#18\":false,\"din#19\":false,\"din#2\":false,\"din#20\":false,\"din#21\":false,\"din#22\":false,\"din#23\":false,\"din#24\":false,\"din#25\":false,\"din#26\":false,\"din#27\":false,\"din#28\":false,\"din#29\":false,\"din#3\":false,\"din#30\":false,\"din#31\":false,\"din#32\":false,\"din#4\":false,\"din#5\":false,\"din#6\":false,\"din#7\":false,\"din#8\":false,\"din#9\":false,\"dout#1\":false,\"dout#10\":false,\"dout#11\":false,\"dout#12\":false,\"dout#13\":false,\"dout#14\":false,\"dout#15\":false,\"dout#16\":false,\"dout#17\":false,\"dout#18\":false,\"dout#19\":false,\"dout#2\":false,\"dout#20\":false,\"dout#21\":false,\"dout#22\":false,\"dout#23\":false,\"dout#24\":false,\"dout#25\":false,\"dout#26\":false,\"dout#27\":false,\"dout#28\":false,\"dout#29\":false,\"dout#3\":false,\"dout#30\":false,\"dout#31\":false,\"dout#32\":false,\"dout#4\":false,\"dout#5\":false,\"dout#6\":false,\"dout#7\":false,\"dout#8\":false,\"dout#9\":false,\"ident\":\"namo:namo\",\"position.altitude\":300,\"position.direction\":0,\"position.hdop\":1.1,\"position.latitude\":53.90821,\"position.longitude\":27.524165,\"position.satellites\":7,\"position.speed\":0,\"timestamp\":1508508510.013227}]"))); + } + +} \ No newline at end of file -- cgit v1.2.3 From c72e261e92166c0f775ad2378637ee778e65564b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 25 Oct 2017 06:34:33 +1300 Subject: Fix Meitrack temperature (fix #3604) --- src/org/traccar/protocol/MeitrackProtocolDecoder.java | 8 ++++---- test/org/traccar/protocol/MeitrackProtocolDecoderTest.java | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/org/traccar/protocol/MeitrackProtocolDecoder.java index efc9c24db..a74d5ca62 100644 --- a/src/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -219,13 +219,13 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { if (parser.hasNext()) { for (String temp : parser.next().split("\\|")) { - int index = Integer.valueOf(temp.substring(0, 2), 16); + int index = Integer.parseInt(temp.substring(0, 2), 16); if (protocol >= 3) { - double value = Short.valueOf(temp.substring(2), 16); + double value = (short) Integer.parseInt(temp.substring(2), 16); position.set(Position.PREFIX_TEMP + index, value * 0.01); } else { - double value = Byte.valueOf(temp.substring(2, 4), 16); - value += (value < 0 ? -0.01 : 0.01) * Integer.valueOf(temp.substring(4), 16); + double value = Byte.parseByte(temp.substring(2, 4), 16); + value += (value < 0 ? -0.01 : 0.01) * Integer.parseInt(temp.substring(4), 16); position.set(Position.PREFIX_TEMP + index, value); } } diff --git a/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java b/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java index 07411dbc8..34b2ad579 100644 --- a/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java +++ b/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java @@ -11,6 +11,9 @@ public class MeitrackProtocolDecoderTest extends ProtocolTest { MeitrackProtocolDecoder decoder = new MeitrackProtocolDecoder(new MeitrackProtocol()); + verifyPosition(decoder, buffer( + "$$V177,863835026871173,AAA,35,34.516428,10.470160,170915154043,A,9,12,68,74,0.9,9,1988259,525882,605|2|008C|0007B5A6,0200,0003|0000|0000|01A6|0571,00000001,,3,0000,06FB2E,360,511*74")); + verifyPosition(decoder, buffer( "$$V177,863835026871173,AAA,35,34.516428,10.470160,170915154043,A,9,12,68,74,0.9,9,1988259,525882,605|2|008C|0007B5A6,0200,0003|0000|0000|01A6|0571,00000001,,3,0000,010A92,360,511*74")); -- cgit v1.2.3 From 8e14763c45f2a10ac198d311e770282387cd9070 Mon Sep 17 00:00:00 2001 From: namo Date: Wed, 25 Oct 2017 10:35:17 +0300 Subject: flespi integration: listening messages (rev.7) --- src/org/traccar/protocol/FlespiProtocolDecoder.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/FlespiProtocolDecoder.java b/src/org/traccar/protocol/FlespiProtocolDecoder.java index 2e57c1dcf..799d78ecb 100644 --- a/src/org/traccar/protocol/FlespiProtocolDecoder.java +++ b/src/org/traccar/protocol/FlespiProtocolDecoder.java @@ -40,11 +40,8 @@ import java.util.Date; public class FlespiProtocolDecoder extends BaseProtocolDecoder { - private final String protocolName; - public FlespiProtocolDecoder(FlespiProtocol protocol) { super(protocol); - protocolName = protocol.getName(); } @Override @@ -84,7 +81,7 @@ public class FlespiProtocolDecoder extends BaseProtocolDecoder { } private void decodePosition(JsonObject object, Position position) { - position.setProtocol(protocolName); + position.setProtocol(getProtocolName()); Date deviceTime = new Date((long) object.getJsonNumber("timestamp").doubleValue() * 1000); position.setTime(deviceTime); -- cgit v1.2.3 From fa9d7dbce38370a82d59494dec698b2177a45ec2 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 25 Oct 2017 22:03:19 +1300 Subject: Implement D-WAY protocol --- setup/default.xml | 1 + src/org/traccar/protocol/DwayProtocol.java | 47 +++++++++ src/org/traccar/protocol/DwayProtocolDecoder.java | 109 +++++++++++++++++++++ .../traccar/protocol/DwayProtocolDecoderTest.java | 21 ++++ 4 files changed, 178 insertions(+) create mode 100644 src/org/traccar/protocol/DwayProtocol.java create mode 100644 src/org/traccar/protocol/DwayProtocolDecoder.java create mode 100644 test/org/traccar/protocol/DwayProtocolDecoderTest.java (limited to 'src/org') diff --git a/setup/default.xml b/setup/default.xml index 79455d846..d966cf91c 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -218,5 +218,6 @@ 5147 5148 5149 + 5150 diff --git a/src/org/traccar/protocol/DwayProtocol.java b/src/org/traccar/protocol/DwayProtocol.java new file mode 100644 index 000000000..151d3fe01 --- /dev/null +++ b/src/org/traccar/protocol/DwayProtocol.java @@ -0,0 +1,47 @@ +/* + * Copyright 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class DwayProtocol extends BaseProtocol { + + public DwayProtocol() { + super("dway"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new DwayProtocolDecoder(DwayProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/DwayProtocolDecoder.java b/src/org/traccar/protocol/DwayProtocolDecoder.java new file mode 100644 index 000000000..993aa91b2 --- /dev/null +++ b/src/org/traccar/protocol/DwayProtocolDecoder.java @@ -0,0 +1,109 @@ +/* + * Copyright 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.util.regex.Pattern; + +public class DwayProtocolDecoder extends BaseProtocolDecoder { + + public DwayProtocolDecoder(DwayProtocol protocol) { + super(protocol); + } + + private static final Pattern PATTERN = new PatternBuilder() + .text("AA55,") + .number("d+,") // index + .number("(d+),") // imei + .number("d+,") // type + .number("(dd)(dd)(dd),") // date (yymmdd) + .number("(dd)(dd)(dd),") // time (hhmmss) + .number("(-?d+.d+),") // latitude + .number("(-?d+.d+),") // longitude + .number("(-?d+),") // altitude + .number("(d+.d+),") // speed + .number("(d+),") // course + .number("([01]{4}),") // input + .number("([01]{4}),") // output + .number("([01])([01])([01])([01]),") // flags + .number("(d+),") // battery + .number("(d+),") // adc1 + .number("(d+),") // adc2 + .number("(d+)") // driver + .compile(); + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + String sentence = (String) msg; + if (sentence.startsWith(">H")) { + if (channel != null) { + channel.write(">ALIVE\r\n"); + } + return null; + } + + Parser parser = new Parser(PATTERN, (String) msg); + if (!parser.matches()) { + return null; + } + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } + + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.setTime(parser.nextDateTime()); + position.setLatitude(parser.nextDouble()); + position.setLongitude(parser.nextDouble()); + position.setAltitude(parser.nextDouble(0)); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0))); + position.setCourse(parser.nextDouble(0)); + + position.set(Position.KEY_INPUT, parser.nextBinInt()); + position.set(Position.KEY_OUTPUT, parser.nextBinInt()); + + position.setValid(parser.next().equals("1")); + + position.set(Position.KEY_IGNITION, parser.next().equals("1")); + position.set(Position.KEY_CHARGE, parser.next().equals("1")); + + if (parser.next().equals("1")) { + position.set(Position.KEY_ALARM, Position.ALARM_SHOCK); + } + + position.set(Position.KEY_BATTERY, parser.nextInt() * 0.001); + position.set(Position.PREFIX_ADC + 1, parser.nextInt() * 0.001); + position.set(Position.PREFIX_ADC + 2, parser.nextInt() * 0.001); + position.set(Position.KEY_DRIVER_UNIQUE_ID, parser.next()); + + return position; + } + +} diff --git a/test/org/traccar/protocol/DwayProtocolDecoderTest.java b/test/org/traccar/protocol/DwayProtocolDecoderTest.java new file mode 100644 index 000000000..33a943b29 --- /dev/null +++ b/test/org/traccar/protocol/DwayProtocolDecoderTest.java @@ -0,0 +1,21 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class DwayProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + DwayProtocolDecoder decoder = new DwayProtocolDecoder(new DwayProtocol()); + + verifyPosition(decoder, text( + "AA55,1,123456,1,140101,101132,22.5500,113.6770,75,70.5,320,1100,0011,1110,3950,33000,24000,12345678")); + + verifyNull(decoder, text( + ">H12345678")); + + } + +} -- cgit v1.2.3 From 594992c7a2c200a8b9b398bb58d1e7393c2e006c Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 25 Oct 2017 22:11:00 +1300 Subject: Add missing genx parameters --- src/org/traccar/protocol/GenxProtocolDecoder.java | 19 +++++++++++++++++++ .../org/traccar/protocol/GenxProtocolDecoderTest.java | 5 ++++- 2 files changed, 23 insertions(+), 1 deletion(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/GenxProtocolDecoder.java b/src/org/traccar/protocol/GenxProtocolDecoder.java index 3b716796c..ebf6f2b53 100644 --- a/src/org/traccar/protocol/GenxProtocolDecoder.java +++ b/src/org/traccar/protocol/GenxProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; import org.traccar.DeviceSession; +import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; import java.net.SocketAddress; @@ -68,6 +69,24 @@ public class GenxProtocolDecoder extends BaseProtocolDecoder { case 4: position.setLongitude(Double.parseDouble(values[i])); break; + case 11: + position.set(Position.KEY_IGNITION, values[i].equals("ON")); + break; + case 13: + position.setSpeed(UnitsConverter.knotsFromKph(Integer.parseInt(values[i]))); + break; + case 17: + position.setCourse(Integer.parseInt(values[i])); + break; + case 23: + position.set(Position.KEY_ODOMETER, Double.parseDouble(values[i]) * 1000); + break; + case 27: + position.setAltitude(UnitsConverter.metersFromFeet(Integer.parseInt(values[i]))); + break; + case 46: + position.set(Position.KEY_SATELLITES, Integer.parseInt(values[i])); + break; default: break; } diff --git a/test/org/traccar/protocol/GenxProtocolDecoderTest.java b/test/org/traccar/protocol/GenxProtocolDecoderTest.java index 43d9e7d6e..9c49839c4 100644 --- a/test/org/traccar/protocol/GenxProtocolDecoderTest.java +++ b/test/org/traccar/protocol/GenxProtocolDecoderTest.java @@ -16,11 +16,14 @@ public class GenxProtocolDecoderTest extends ProtocolTest { verifyPosition(decoder, text( "000036004130,08/31/2017 17:24:37,45.47257,-73.65506,3,0,117,1.14,124,ON,1489,0,5,N,0,0.000,-95.0,-1.0,0,0.0000,0.0000,0.000,0,0.00,0.00,0.00,NA,U,UUU,0,-95.0,U")); - decoder.setReportColumns("1,2,3,4"); + decoder.setReportColumns("1,2,3,4,13,17,10,23,27,11,7,8,46,56,59,70,74,75,77,89,90,93,99,107,112,113,114,176,175,178,181,182"); verifyPosition(decoder, text( "000036035855,04/16/2017 21:19:07,45.46485,-73.65424,24,32,61:213,342.51,157,ON,20984,0,12,O,18,0.000,95.0,24.0,1990,64.0894,0.0219,316.009,71,0.00,16.78,5.10,NA,U,UUU,0,-95.0,U")); + verifyPosition(decoder, text( + "000036004129,10/20/2017 00:54:27,43.44638,-79.68616,36,310,6,4954.40,321,ON,35377,0,12,O,13,0.000,85.6,36.0,1573,451.2514,0.0012,5260.953,0,0.00,122.48,33.17,NA,U,UUU,0,-95.0,U")); + } } -- cgit v1.2.3 From 91133eed4a914b985f824ebe20dfc90892584d16 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 25 Oct 2017 22:35:47 +1300 Subject: Implement alternative Totem ACK --- src/org/traccar/protocol/TotemProtocolDecoder.java | 252 +++++++++++---------- 1 file changed, 138 insertions(+), 114 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/TotemProtocolDecoder.java b/src/org/traccar/protocol/TotemProtocolDecoder.java index 3c2dee8ec..bfc1973f8 100644 --- a/src/org/traccar/protocol/TotemProtocolDecoder.java +++ b/src/org/traccar/protocol/TotemProtocolDecoder.java @@ -182,13 +182,136 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { } } + private boolean decode12(Position position, Parser parser, Pattern pattern) { + + if (parser.hasNext()) { + position.set(Position.KEY_ALARM, decodeAlarm(Short.parseShort(parser.next(), 16))); + } + DateBuilder dateBuilder = new DateBuilder(); + int year = 0, month = 0, day = 0; + if (pattern == PATTERN2) { + day = parser.nextInt(0); + month = parser.nextInt(0); + year = parser.nextInt(0); + } + dateBuilder.setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); + + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble(0)); + position.setCourse(parser.nextDouble(0)); + + if (pattern == PATTERN1) { + day = parser.nextInt(0); + month = parser.nextInt(0); + year = parser.nextInt(0); + } + if (year == 0) { + return false; // ignore invalid data + } + dateBuilder.setDate(year, month, day); + position.setTime(dateBuilder.getDate()); + + if (pattern == PATTERN1) { + position.set(Position.KEY_PDOP, parser.nextDouble()); + position.set(Position.KEY_HDOP, parser.nextDouble()); + position.set(Position.KEY_VDOP, parser.nextDouble()); + } else { + position.set(Position.KEY_HDOP, parser.nextDouble()); + } + + position.set(Position.PREFIX_IO + 1, parser.next()); + if (pattern == PATTERN1) { + position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.01); + } else { + position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.1); + } + position.set(Position.KEY_POWER, parser.nextDouble(0)); + position.set(Position.PREFIX_ADC + 1, parser.next()); + + int lac = parser.nextHexInt(0); + int cid = parser.nextHexInt(0); + if (lac != 0 && cid != 0) { + position.setNetwork(new Network(CellTower.fromLacCid(lac, cid))); + } + + position.set(Position.PREFIX_TEMP + 1, parser.next()); + position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); + + return true; + } + + private boolean decode3(Position position, Parser parser) { + + if (parser.hasNext()) { + position.set(Position.KEY_ALARM, decodeAlarm(Short.parseShort(parser.next(), 16))); + } + + position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); + + position.set(Position.PREFIX_IO + 1, parser.next()); + position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.1); + position.set(Position.KEY_POWER, parser.nextDouble(0)); + position.set(Position.PREFIX_ADC + 1, parser.next()); + position.set(Position.PREFIX_ADC + 2, parser.next()); + position.set(Position.PREFIX_TEMP + 1, parser.next()); + position.set(Position.PREFIX_TEMP + 2, parser.next()); + + position.setNetwork(new Network( + CellTower.fromLacCid(parser.nextHexInt(0), parser.nextHexInt(0)))); + + position.setValid(parser.next().equals("A")); + position.set(Position.KEY_SATELLITES, parser.nextInt()); + position.setCourse(parser.nextDouble(0)); + position.setSpeed(parser.nextDouble(0)); + position.set(Position.KEY_PDOP, parser.nextDouble()); + position.set(Position.KEY_ODOMETER, parser.nextInt(0) * 1000); + + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + + return true; + } + + private boolean decode4(Position position, Parser parser) { + + position.set(Position.KEY_STATUS, parser.next()); + + position.setTime(parser.nextDateTime()); + + position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.1); + position.set(Position.KEY_POWER, parser.nextDouble(0)); + + position.set(Position.PREFIX_ADC + 1, parser.next()); + position.set(Position.PREFIX_ADC + 2, parser.next()); + position.set(Position.PREFIX_ADC + 3, parser.next()); + position.set(Position.PREFIX_ADC + 4, parser.next()); + position.set(Position.PREFIX_TEMP + 1, parser.next()); + position.set(Position.PREFIX_TEMP + 2, parser.next()); + + CellTower cellTower = CellTower.fromLacCid(parser.nextHexInt(0), parser.nextHexInt(0)); + position.set(Position.KEY_SATELLITES, parser.nextInt(0)); + cellTower.setSignalStrength(parser.nextInt(0)); + position.setNetwork(new Network(cellTower)); + + position.setCourse(parser.nextDouble(0)); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0))); + position.set(Position.KEY_HDOP, parser.nextDouble(0)); + position.set(Position.KEY_ODOMETER, parser.nextInt(0) * 1000); + + position.setValid(true); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + + return true; + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { String sentence = (String) msg; - - // Determine format Pattern pattern = PATTERN3; if (sentence.indexOf("AA") == 6) { pattern = PATTERN4; @@ -215,123 +338,24 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { } position.setDeviceId(deviceSession.getDeviceId()); + boolean result; if (pattern == PATTERN1 || pattern == PATTERN2) { - if (parser.hasNext()) { - position.set(Position.KEY_ALARM, decodeAlarm(Short.parseShort(parser.next(), 16))); - } - DateBuilder dateBuilder = new DateBuilder(); - int year = 0, month = 0, day = 0; - if (pattern == PATTERN2) { - day = parser.nextInt(0); - month = parser.nextInt(0); - year = parser.nextInt(0); - } - dateBuilder.setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); - - position.setValid(parser.next().equals("A")); - position.setLatitude(parser.nextCoordinate()); - position.setLongitude(parser.nextCoordinate()); - position.setSpeed(parser.nextDouble(0)); - position.setCourse(parser.nextDouble(0)); - - if (pattern == PATTERN1) { - day = parser.nextInt(0); - month = parser.nextInt(0); - year = parser.nextInt(0); - } - if (year == 0) { - return null; // ignore invalid data - } - dateBuilder.setDate(year, month, day); - position.setTime(dateBuilder.getDate()); - - if (pattern == PATTERN1) { - position.set(Position.KEY_PDOP, parser.nextDouble()); - position.set(Position.KEY_HDOP, parser.nextDouble()); - position.set(Position.KEY_VDOP, parser.nextDouble()); - } else { - position.set(Position.KEY_HDOP, parser.nextDouble()); - } - - position.set(Position.PREFIX_IO + 1, parser.next()); - if (pattern == PATTERN1) { - position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.01); - } else { - position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.1); - } - position.set(Position.KEY_POWER, parser.nextDouble(0)); - position.set(Position.PREFIX_ADC + 1, parser.next()); - - int lac = parser.nextHexInt(0); - int cid = parser.nextHexInt(0); - if (lac != 0 && cid != 0) { - position.setNetwork(new Network(CellTower.fromLacCid(lac, cid))); - } - - position.set(Position.PREFIX_TEMP + 1, parser.next()); - position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); - + result = decode12(position, parser, pattern); } else if (pattern == PATTERN3) { - if (parser.hasNext()) { - position.set(Position.KEY_ALARM, decodeAlarm(Short.parseShort(parser.next(), 16))); - } - - position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); - - position.set(Position.PREFIX_IO + 1, parser.next()); - position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.1); - position.set(Position.KEY_POWER, parser.nextDouble(0)); - position.set(Position.PREFIX_ADC + 1, parser.next()); - position.set(Position.PREFIX_ADC + 2, parser.next()); - position.set(Position.PREFIX_TEMP + 1, parser.next()); - position.set(Position.PREFIX_TEMP + 2, parser.next()); - - position.setNetwork(new Network( - CellTower.fromLacCid(parser.nextHexInt(0), parser.nextHexInt(0)))); - - position.setValid(parser.next().equals("A")); - position.set(Position.KEY_SATELLITES, parser.nextInt()); - position.setCourse(parser.nextDouble(0)); - position.setSpeed(parser.nextDouble(0)); - position.set(Position.KEY_PDOP, parser.nextDouble()); - position.set(Position.KEY_ODOMETER, parser.nextInt(0) * 1000); - - position.setLatitude(parser.nextCoordinate()); - position.setLongitude(parser.nextCoordinate()); - - } else if (pattern == PATTERN4) { - position.set(Position.KEY_STATUS, parser.next()); - - position.setTime(parser.nextDateTime()); - - position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.1); - position.set(Position.KEY_POWER, parser.nextDouble(0)); - - position.set(Position.PREFIX_ADC + 1, parser.next()); - position.set(Position.PREFIX_ADC + 2, parser.next()); - position.set(Position.PREFIX_ADC + 3, parser.next()); - position.set(Position.PREFIX_ADC + 4, parser.next()); - position.set(Position.PREFIX_TEMP + 1, parser.next()); - position.set(Position.PREFIX_TEMP + 2, parser.next()); - - CellTower cellTower = CellTower.fromLacCid(parser.nextHexInt(0), parser.nextHexInt(0)); - position.set(Position.KEY_SATELLITES, parser.nextInt(0)); - cellTower.setSignalStrength(parser.nextInt(0)); - position.setNetwork(new Network(cellTower)); - - position.setCourse(parser.nextDouble(0)); - position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0))); - position.set(Position.KEY_HDOP, parser.nextDouble(0)); - position.set(Position.KEY_ODOMETER, parser.nextInt(0) * 1000); - - position.setValid(true); - position.setLatitude(parser.nextCoordinate()); - position.setLongitude(parser.nextCoordinate()); + result = decode3(position, parser); + } else { + result = decode4(position, parser); } + if (channel != null) { - channel.write("ACK OK\r\n"); + if (pattern == PATTERN4) { + channel.write("$$0014AA" + sentence.substring(sentence.length() - 6)); + } else { + channel.write("ACK OK\r\n"); + } } - return position; + + return result ? position : null; } } -- cgit v1.2.3 From 0a17cc5b40a2e01d215e6d39eaee1fa5c8f06aac Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 25 Oct 2017 23:28:35 +1300 Subject: Save Meitrack photos --- .../traccar/protocol/MeitrackProtocolDecoder.java | 55 ++++++++++++++++++---- 1 file changed, 45 insertions(+), 10 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/org/traccar/protocol/MeitrackProtocolDecoder.java index a74d5ca62..9f6e25d89 100644 --- a/src/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -16,6 +16,7 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; @@ -37,6 +38,8 @@ import java.util.regex.Pattern; public class MeitrackProtocolDecoder extends BaseProtocolDecoder { + private ChannelBuffer photo; + public MeitrackProtocolDecoder(MeitrackProtocol protocol) { super(protocol); } @@ -308,6 +311,16 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { return positions; } + private void requestPhotoPacket(Channel channel, String imei, int index) { + if (channel != null) { + String content = "D00,camera_picture.jpg," + index; + int length = 1 + imei.length() + 1 + content.length() + 5; + String response = String.format("@@O%02d,%s,%s*", length, imei, content); + response += Checksum.sum(response) + "\r\n"; + channel.write(response); + } + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -315,20 +328,42 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { ChannelBuffer buf = (ChannelBuffer) msg; int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); + String imei = buf.toString(index + 1, 15, StandardCharsets.US_ASCII); index = buf.indexOf(index + 1, buf.writerIndex(), (byte) ','); - String type = buf.toString(index + 1, 3, StandardCharsets.US_ASCII); + switch (type) { - case "D03": - if (channel != null) { - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); - String imei = Context.getIdentityManager().getById(deviceSession.getDeviceId()).getUniqueId(); - String content = "D00,camera_picture.jpg,0"; - int length = 1 + imei.length() + 1 + content.length() + 5; - String response = String.format("@@O%02d,%s,%s*", length, imei, content); - response += Checksum.sum(response) + "\r\n"; - channel.write(response); + case "D00": + index = buf.indexOf(index + 1 + type.length() + 1, buf.writerIndex(), (byte) ',') + 1; + int endIndex = buf.indexOf(index, buf.writerIndex(), (byte) ','); + int total = Integer.parseInt(buf.toString(index, endIndex - index, StandardCharsets.US_ASCII)); + index = endIndex + 1; + endIndex = buf.indexOf(index, buf.writerIndex(), (byte) ','); + int current = Integer.parseInt(buf.toString(index, endIndex - index, StandardCharsets.US_ASCII)); + + buf.readerIndex(endIndex + 1); + photo.writeBytes(buf.readBytes(buf.readableBytes() - 1 - 2 - 2)); + + if (current == total - 1) { + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(getDeviceSession(channel, remoteAddress, imei).getDeviceId()); + + getLastLocation(position, null); + + position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(imei, photo, "jpg")); + photo = null; + + return position; + } else { + if ((current + 1) % 8 == 0) { + requestPhotoPacket(channel, imei, current + 1); + } + return null; } + case "D03": + photo = ChannelBuffers.dynamicBuffer(); + requestPhotoPacket(channel, imei, 0); return null; case "CCC": return decodeBinaryMessage(channel, remoteAddress, buf); -- cgit v1.2.3 From 80e7a43412a72f0189c11baac102af7a335de09b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 26 Oct 2017 04:44:55 +1300 Subject: Add support for AB and AC types --- src/org/traccar/protocol/TotemProtocolDecoder.java | 4 ++-- test/org/traccar/protocol/TotemProtocolDecoderTest.java | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/TotemProtocolDecoder.java b/src/org/traccar/protocol/TotemProtocolDecoder.java index bfc1973f8..a3e8c9921 100644 --- a/src/org/traccar/protocol/TotemProtocolDecoder.java +++ b/src/org/traccar/protocol/TotemProtocolDecoder.java @@ -131,7 +131,7 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { private static final Pattern PATTERN4 = new PatternBuilder() .text("$$") // header .number("dddd") // length - .text("AA") // type + .expression("A[ABC]") // type .number("(d+)|") // imei .number("(x{8})") // status .number("(dd)(dd)(dd)") // date (yymmdd) @@ -313,7 +313,7 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { String sentence = (String) msg; Pattern pattern = PATTERN3; - if (sentence.indexOf("AA") == 6) { + if (sentence.indexOf("A") == 6) { pattern = PATTERN4; } else if (sentence.contains("$GPRMC")) { pattern = PATTERN1; diff --git a/test/org/traccar/protocol/TotemProtocolDecoderTest.java b/test/org/traccar/protocol/TotemProtocolDecoderTest.java index 40d8d9eba..67c75e08d 100644 --- a/test/org/traccar/protocol/TotemProtocolDecoderTest.java +++ b/test/org/traccar/protocol/TotemProtocolDecoderTest.java @@ -10,6 +10,12 @@ public class TotemProtocolDecoderTest extends ProtocolTest { TotemProtocolDecoder decoder = new TotemProtocolDecoder(new TotemProtocol()); + verifyPosition(decoder, text( + "$$0108AB863835028447675|5004C0001710250234064214059828A058AE121010604000.600000320304.7772N10134.8238E11625B")); + + verifyPosition(decoder, text( + "$$0108AA863835028447675|5004C0001710250234134114057728A058AE112108305100.600000660304.7787N10134.8719E116458")); + verifyPosition(decoder, text( "$$0112AA864244026065291|180018001409160205244011000027BA0E57063100000001.200000002237.8119N11403.5075E05202D")); -- cgit v1.2.3 From dcc7e0c6ed949604af5e62c671e293f582146bc0 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 26 Oct 2017 07:12:37 +1300 Subject: Implement Meitrack CCE format --- .../traccar/protocol/MeitrackProtocolDecoder.java | 102 ++++++++++++++++++++- .../protocol/MeitrackProtocolDecoderTest.java | 3 + 2 files changed, 101 insertions(+), 4 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/org/traccar/protocol/MeitrackProtocolDecoder.java index 9f6e25d89..5b67aebe3 100644 --- a/src/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -112,7 +112,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { } } - private Position decodeRegularMessage(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { + private Position decodeRegular(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { Parser parser = new Parser(PATTERN, buf.toString(StandardCharsets.US_ASCII)); if (!parser.matches()) { @@ -237,7 +237,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { return position; } - private List decodeBinaryMessage(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { + private List decodeBinaryC(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { List positions = new LinkedList<>(); String flag = buf.toString(2, 1, StandardCharsets.US_ASCII); @@ -311,6 +311,98 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { return positions; } + private List decodeBinaryE(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { + List positions = new LinkedList<>(); + + buf.readerIndex(buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',') + 1); + String imei = buf.readBytes(15).toString(StandardCharsets.US_ASCII); + buf.skipBytes(1 + 3 + 1); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { + return null; + } + + buf.readUnsignedInt(); // remaining cache + int count = buf.readUnsignedShort(); + + for (int i = 0; i < count; i++) { + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + buf.readUnsignedShort(); // length + buf.readUnsignedShort(); // index + + int paramCount = buf.readUnsignedByte(); + for (int j = 0; j < paramCount; j++) { + int id = buf.readUnsignedByte(); + switch (id) { + case 0x01: + position.set(Position.KEY_EVENT, buf.readUnsignedByte()); + break; + case 0x05: + position.setValid(buf.readUnsignedByte() > 0); + break; + case 0x06: + position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); + break; + default: + buf.readUnsignedByte(); + break; + } + } + + paramCount = buf.readUnsignedByte(); + for (int j = 0; j < paramCount; j++) { + int id = buf.readUnsignedByte(); + switch (id) { + case 0x08: + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); + break; + case 0x09: + position.setCourse(buf.readUnsignedShort() * 0.1); + break; + case 0x0B: + position.setAltitude(buf.readShort()); + break; + default: + buf.readUnsignedShort(); + break; + } + } + + paramCount = buf.readUnsignedByte(); + for (int j = 0; j < paramCount; j++) { + int id = buf.readUnsignedByte(); + switch (id) { + case 0x02: + position.setLatitude(buf.readInt() * 0.000001); + break; + case 0x03: + position.setLongitude(buf.readInt() * 0.000001); + break; + case 0x04: + position.setTime(new Date((946684800 + buf.readUnsignedInt()) * 1000)); // 2000-01-01 + break; + default: + buf.readUnsignedInt(); + break; + } + } + + paramCount = buf.readUnsignedByte(); + for (int j = 0; j < paramCount; j++) { + buf.readUnsignedByte(); // id + buf.skipBytes(buf.readUnsignedByte()); // value + } + + positions.add(position); + } + + return positions; + } + private void requestPhotoPacket(Channel channel, String imei, int index) { if (channel != null) { String content = "D00,camera_picture.jpg," + index; @@ -366,9 +458,11 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { requestPhotoPacket(channel, imei, 0); return null; case "CCC": - return decodeBinaryMessage(channel, remoteAddress, buf); + return decodeBinaryC(channel, remoteAddress, buf); + case "CCE": + return decodeBinaryE(channel, remoteAddress, buf); default: - return decodeRegularMessage(channel, remoteAddress, buf); + return decodeRegular(channel, remoteAddress, buf); } } diff --git a/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java b/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java index 34b2ad579..4ec2211bc 100644 --- a/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java +++ b/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java @@ -11,6 +11,9 @@ public class MeitrackProtocolDecoderTest extends ProtocolTest { MeitrackProtocolDecoder decoder = new MeitrackProtocolDecoder(new MeitrackProtocol()); + verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "24245f3237382c3836353738393032313434373233322c4343452c5b00000003005000130006012305010608070d15001b0006080000091e010a09000b2e0019a1011af90106025c033300039be60c06044f6678210c6f1806000d48db06001c41000000010e0cf60113002005912b830001ff5000130006012305010608070d15001b0006080000091e010a09000b2e0019a0011af90106025c033300039be60c0604506678210c6f1806000d49db06001c41000000010e0cf60113002005912b830001ff5000130006012305010608070d15001b0006080000091e010a09000b2e0019a1011af90106025c033300039be60c0604516678210c6f1806000d4adb06001c41000000010e0cf60113002005912b830001ff2a37460d0a")); + verifyPosition(decoder, buffer( "$$V177,863835026871173,AAA,35,34.516428,10.470160,170915154043,A,9,12,68,74,0.9,9,1988259,525882,605|2|008C|0007B5A6,0200,0003|0000|0000|01A6|0571,00000001,,3,0000,06FB2E,360,511*74")); -- cgit v1.2.3