From 015116991389643865bcdc35ea6e247eb6cb3ba1 Mon Sep 17 00:00:00 2001 From: michaelloliveira Date: Wed, 30 May 2018 19:48:33 -0300 Subject: Svias Protocol Encode e Decoder --- src/org/traccar/protocol/SviasProtocol.java | 66 +++++++ src/org/traccar/protocol/SviasProtocolDecoder.java | 205 +++++++++++++++++++++ src/org/traccar/protocol/SviasProtocolEncoder.java | 50 +++++ 3 files changed, 321 insertions(+) create mode 100644 src/org/traccar/protocol/SviasProtocol.java create mode 100644 src/org/traccar/protocol/SviasProtocolDecoder.java create mode 100644 src/org/traccar/protocol/SviasProtocolEncoder.java (limited to 'src') diff --git a/src/org/traccar/protocol/SviasProtocol.java b/src/org/traccar/protocol/SviasProtocol.java new file mode 100644 index 000000000..b58872baf --- /dev/null +++ b/src/org/traccar/protocol/SviasProtocol.java @@ -0,0 +1,66 @@ +/* + * 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.string.StringDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; +import org.traccar.model.Command; + +import java.util.List; +import org.jboss.netty.bootstrap.ConnectionlessBootstrap; + +public class SviasProtocol extends BaseProtocol { + + public SviasProtocol() { + super("svias"); + setSupportedDataCommands( + Command.TYPE_CUSTOM, + Command.TYPE_POSITION_SINGLE, + Command.TYPE_SET_ODOMETER, + Command.TYPE_ENGINE_STOP, + Command.TYPE_ENGINE_RESUME, + Command.TYPE_ALARM_ARM, + Command.TYPE_ALARM_DISARM, + Command.TYPE_ALARM_REMOVE); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectEncoder", new SviasProtocolEncoder()); + pipeline.addLast("objectDecoder", new SviasProtocolDecoder(SviasProtocol.this)); + } + }); + serverList.add(new TrackerServer(new ConnectionlessBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectEncoder", new SviasProtocolEncoder()); + pipeline.addLast("objectDecoder", new SviasProtocolDecoder(SviasProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/SviasProtocolDecoder.java b/src/org/traccar/protocol/SviasProtocolDecoder.java new file mode 100644 index 000000000..4ec758eb0 --- /dev/null +++ b/src/org/traccar/protocol/SviasProtocolDecoder.java @@ -0,0 +1,205 @@ +/* + * 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.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.model.Position; +import java.text.ParseException; + +import java.net.SocketAddress; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.TimeZone; +import org.traccar.helper.Log; +import org.traccar.helper.UnitsConverter; + +public class SviasProtocolDecoder extends BaseProtocolDecoder { + + public SviasProtocolDecoder(SviasProtocol protocol) { + super(protocol); + } + + private void sendResponse(Channel channel, String prefix) { + if (channel != null) { + channel.write(prefix); + } + } + + private String zeros(String texto, Integer valor) { + String aux = ""; + for (int i = 0; i < valor; i++) { + aux += '0'; + } + return aux + texto; + } + + public String decimalToBinary(int valor) { + + String bin = Integer.toString(valor, 2); + return bin; + } + + private double convert2decimal(long v) { + float a = (float) v / 1.0E7F; + int b = (int) (v / 10000000L); + float c = a - b; + float d = (float) (c * 1.6666666666666667D); + int e = (int) (v / 10000000L); + float f = d + e; + + return Double.valueOf(f); + + } + + private Position decodePosition(Channel channel, SocketAddress remoteAddress, String substring) + throws ParseException { + int index = 0; + + String[] values = substring.split(","); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, values[3]); + if (deviceSession == null) { + return null; + } + + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.set(Position.KEY_VERSION_HW, values[index++].replace("[", "")); + + String swOrAlt = values[index++]; + position.set(Position.KEY_VERSION_HW, swOrAlt); + position.setAltitude(Double.parseDouble(swOrAlt)); + + position.set(Position.KEY_INDEX, Integer.valueOf(values[index++])); + + String imei = values[index++]; + position.set(Position.KEY_TYPE, values[index++]); + + DateFormat dateFormat = new SimpleDateFormat("ddMMyyHHmmss"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + + String date = values[index++]; + String time = values[index++]; + + position.setTime(dateFormat.parse(zeros(date, 6 - date.trim().length()) + + zeros(time, 6 - time.trim().length()))); + + position.setLatitude(convert2decimal(Long.valueOf(values[index++]))); + position.setLongitude(convert2decimal(Long.valueOf(values[index++]))); + + position.setSpeed(UnitsConverter.knotsFromKph(Integer.valueOf(values[index++]) / 100)); + position.setCourse(Integer.valueOf(values[index++]) / 100); + + position.set(Position.KEY_ODOMETER, Integer.valueOf(values[index++])); + + String input = decimalToBinary(Integer.valueOf(values[index++])); + String output = decimalToBinary(Integer.valueOf(values[index++])); + + /** inputs */ + String in = new StringBuilder(zeros(input, 8 - input.length())).reverse().toString(); + + /** outputs */ + String out = new StringBuilder(zeros(output, 8 - output.length())).reverse().toString(); + + if (in.substring(0, 1).equals("1")) { + position.set(Position.KEY_ALARM, Position.ALARM_SOS); + } + + position.set(Position.PREFIX_IN + 1, in.substring(1, 2).equals("1")); + position.set(Position.PREFIX_IN + 2, in.substring(2, 3).equals("1")); + position.set(Position.PREFIX_IN + 3, in.substring(3, 4).equals("1")); + position.set(Position.KEY_IGNITION, in.substring(4, 5).equals("1")); + + if (in.substring(7, 8).equals("1")) { + position.set(Position.KEY_ALARM, Position.ALARM_POWER_CUT); + } + + position.setValid(out.substring(0, 1).equals("1")); + + if (out.substring(1, 2).equals("1")) { + position.set(Position.KEY_ALARM, Position.ALARM_JAMMING); + } + + position.set(Position.PREFIX_OUT + 1, out.substring(2, 3).equals("1")); + position.set(Position.PREFIX_OUT + 2, out.substring(3, 4).equals("1")); + position.set(Position.PREFIX_OUT + 3, out.substring(4, 5).equals("1")); + + position.set(Position.KEY_POWER, Integer.valueOf(values[index++]) / 100); + + position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(values[index++])); + + position.set(Position.KEY_RSSI, Integer.valueOf(values[index++])); + + if (values.length == 22) { + String driverUniqueId = values[index++]; + if (!driverUniqueId.isEmpty()) { + position.set(Position.KEY_DRIVER_UNIQUE_ID, driverUniqueId); + } + } + + String status = decimalToBinary(Integer.parseInt(values[index++])); + String st = new StringBuilder(zeros(status, 8 - status.length())).reverse().toString(); + + position.set(Position.ALARM_CORNERING, st.substring(0, 1).equals("1")); + position.set(Position.ALARM_GEOFENCE_ENTER, st.substring(1, 2).equals("1")); + position.set(Position.ALARM_FALL_DOWN, st.substring(3, 4).equals("1")); + position.set(Position.ALARM_OVERSPEED, st.substring(4, 5).equals("1")); + position.set("connectedPrimaryServer", st.substring(5, 6).equals("1")); + position.set("connectedSecundaryServer", st.substring(6, 7).equals("1")); + + return position; + } + + @Override + protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) + throws Exception { + + String sentence = (String) msg; + + if (sentence.contains(":")) { + + Log.info(sentence); + + String[] values = sentence.substring(1).split(":"); + + String imei = values[1]; + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + + if (deviceSession != null) { + sendResponse(channel, "@"); + } + + } else { + + Position position = decodePosition(channel, remoteAddress, + sentence.substring(sentence.indexOf('[', 1) + 1)); + + if (position != null) { + sendResponse(channel, "@"); + return position; + } + } + + return null; + + } + +} diff --git a/src/org/traccar/protocol/SviasProtocolEncoder.java b/src/org/traccar/protocol/SviasProtocolEncoder.java new file mode 100644 index 000000000..518268cab --- /dev/null +++ b/src/org/traccar/protocol/SviasProtocolEncoder.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 Andrey Kunitsyn (andrey@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.traccar.StringProtocolEncoder; +import org.traccar.helper.Log; +import org.traccar.model.Command; + +public class SviasProtocolEncoder extends StringProtocolEncoder { + + @Override + protected Object encodeCommand(Command command) { + switch (command.getType()) { + case Command.TYPE_CUSTOM: + return formatCommand(command, "{%s}", Command.KEY_DATA); + case Command.TYPE_POSITION_SINGLE: + return formatCommand(command, "AT+STR=1*"); + case Command.TYPE_SET_ODOMETER: + return formatCommand(command, "AT+ODT={%s}*", Command.KEY_DATA); + case Command.TYPE_ENGINE_STOP: + return formatCommand(command, "AT+OUT=1,1*"); + case Command.TYPE_ENGINE_RESUME: + return formatCommand(command, "AT+OUT=1,0*"); + case Command.TYPE_ALARM_ARM: + return formatCommand(command, "AT+OUT=2,1*"); + case Command.TYPE_ALARM_DISARM: + return formatCommand(command, "AT+OUT=2,0*"); + case Command.TYPE_ALARM_REMOVE: + return formatCommand(command, "AT+PNC=600*"); + default: + Log.warning(new UnsupportedOperationException(command.getType())); + break; + } + return null; + } +} -- cgit v1.2.3 From 453be4c44ddbc8cd1db3a2cfd7b1fd37cee7a8fc Mon Sep 17 00:00:00 2001 From: michaelloliveira Date: Wed, 30 May 2018 20:54:22 -0300 Subject: Update SviasProtocolDecoder.java As required by the developer. --- src/org/traccar/protocol/SviasProtocolDecoder.java | 87 +++++----------------- 1 file changed, 17 insertions(+), 70 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/SviasProtocolDecoder.java b/src/org/traccar/protocol/SviasProtocolDecoder.java index 4ec758eb0..caff6339e 100644 --- a/src/org/traccar/protocol/SviasProtocolDecoder.java +++ b/src/org/traccar/protocol/SviasProtocolDecoder.java @@ -25,7 +25,6 @@ import java.net.SocketAddress; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.TimeZone; -import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; public class SviasProtocolDecoder extends BaseProtocolDecoder { @@ -40,21 +39,7 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { } } - private String zeros(String texto, Integer valor) { - String aux = ""; - for (int i = 0; i < valor; i++) { - aux += '0'; - } - return aux + texto; - } - - public String decimalToBinary(int valor) { - - String bin = Integer.toString(valor, 2); - return bin; - } - - private double convert2decimal(long v) { + private double convertCoordinates(long v) { float a = (float) v / 1.0E7F; int b = (int) (v / 10000000L); float c = a - b; @@ -81,11 +66,8 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { position.setProtocol(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); - position.set(Position.KEY_VERSION_HW, values[index++].replace("[", "")); - - String swOrAlt = values[index++]; - position.set(Position.KEY_VERSION_HW, swOrAlt); - position.setAltitude(Double.parseDouble(swOrAlt)); + String versionHw = values[index++].replaceAll("[^0-9]", ""); + String versionSw = values[index++]; position.set(Position.KEY_INDEX, Integer.valueOf(values[index++])); @@ -95,51 +77,31 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { DateFormat dateFormat = new SimpleDateFormat("ddMMyyHHmmss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - String date = values[index++]; - String time = values[index++]; + String date = String.format("%06d", Integer.parseInt(values[index++])); + String time = String.format("%06d", Integer.parseInt(values[index++])); - position.setTime(dateFormat.parse(zeros(date, 6 - date.trim().length()) - + zeros(time, 6 - time.trim().length()))); + position.setTime(dateFormat.parse(date + time)); - position.setLatitude(convert2decimal(Long.valueOf(values[index++]))); - position.setLongitude(convert2decimal(Long.valueOf(values[index++]))); + position.setLatitude(convertCoordinates(Long.valueOf(values[index++]))); + position.setLongitude(convertCoordinates(Long.valueOf(values[index++]))); position.setSpeed(UnitsConverter.knotsFromKph(Integer.valueOf(values[index++]) / 100)); position.setCourse(Integer.valueOf(values[index++]) / 100); position.set(Position.KEY_ODOMETER, Integer.valueOf(values[index++])); - String input = decimalToBinary(Integer.valueOf(values[index++])); - String output = decimalToBinary(Integer.valueOf(values[index++])); + String input = new StringBuilder(String.format("%08d", + Integer.parseInt(values[index++]))).reverse().toString(); - /** inputs */ - String in = new StringBuilder(zeros(input, 8 - input.length())).reverse().toString(); + String output = new StringBuilder(String.format("%08d", + Integer.parseInt(values[index++]))).reverse().toString(); - /** outputs */ - String out = new StringBuilder(zeros(output, 8 - output.length())).reverse().toString(); - - if (in.substring(0, 1).equals("1")) { + if (input.substring(0).equals("1")) { position.set(Position.KEY_ALARM, Position.ALARM_SOS); } + position.set(Position.KEY_IGNITION, input.substring(4).equals("1")); - position.set(Position.PREFIX_IN + 1, in.substring(1, 2).equals("1")); - position.set(Position.PREFIX_IN + 2, in.substring(2, 3).equals("1")); - position.set(Position.PREFIX_IN + 3, in.substring(3, 4).equals("1")); - position.set(Position.KEY_IGNITION, in.substring(4, 5).equals("1")); - - if (in.substring(7, 8).equals("1")) { - position.set(Position.KEY_ALARM, Position.ALARM_POWER_CUT); - } - - position.setValid(out.substring(0, 1).equals("1")); - - if (out.substring(1, 2).equals("1")) { - position.set(Position.KEY_ALARM, Position.ALARM_JAMMING); - } - - position.set(Position.PREFIX_OUT + 1, out.substring(2, 3).equals("1")); - position.set(Position.PREFIX_OUT + 2, out.substring(3, 4).equals("1")); - position.set(Position.PREFIX_OUT + 3, out.substring(4, 5).equals("1")); + position.setValid(output.substring(0).equals("1")); position.set(Position.KEY_POWER, Integer.valueOf(values[index++]) / 100); @@ -154,16 +116,6 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { } } - String status = decimalToBinary(Integer.parseInt(values[index++])); - String st = new StringBuilder(zeros(status, 8 - status.length())).reverse().toString(); - - position.set(Position.ALARM_CORNERING, st.substring(0, 1).equals("1")); - position.set(Position.ALARM_GEOFENCE_ENTER, st.substring(1, 2).equals("1")); - position.set(Position.ALARM_FALL_DOWN, st.substring(3, 4).equals("1")); - position.set(Position.ALARM_OVERSPEED, st.substring(4, 5).equals("1")); - position.set("connectedPrimaryServer", st.substring(5, 6).equals("1")); - position.set("connectedSecundaryServer", st.substring(6, 7).equals("1")); - return position; } @@ -175,13 +127,9 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { if (sentence.contains(":")) { - Log.info(sentence); - String[] values = sentence.substring(1).split(":"); - String imei = values[1]; - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, values[1]); if (deviceSession != null) { sendResponse(channel, "@"); @@ -189,8 +137,7 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { } else { - Position position = decodePosition(channel, remoteAddress, - sentence.substring(sentence.indexOf('[', 1) + 1)); + Position position = decodePosition(channel, remoteAddress, sentence.substring(1)); if (position != null) { sendResponse(channel, "@"); -- cgit v1.2.3 From 111f5bb7a70d3d75b7ddc5cab673bcfdf09c1f67 Mon Sep 17 00:00:00 2001 From: michaelloliveira Date: Sun, 3 Jun 2018 12:54:13 -0300 Subject: Reformatter Code Svias --- src/org/traccar/protocol/SviasProtocolDecoder.java | 86 +++++++--------------- 1 file changed, 27 insertions(+), 59 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/SviasProtocolDecoder.java b/src/org/traccar/protocol/SviasProtocolDecoder.java index caff6339e..cca21467a 100644 --- a/src/org/traccar/protocol/SviasProtocolDecoder.java +++ b/src/org/traccar/protocol/SviasProtocolDecoder.java @@ -29,31 +29,23 @@ import org.traccar.helper.UnitsConverter; public class SviasProtocolDecoder extends BaseProtocolDecoder { + public static final String MSG_KEEPALIVE = "@"; + public SviasProtocolDecoder(SviasProtocol protocol) { super(protocol); } - private void sendResponse(Channel channel, String prefix) { - if (channel != null) { - channel.write(prefix); - } - } - private double convertCoordinates(long v) { - float a = (float) v / 1.0E7F; - int b = (int) (v / 10000000L); - float c = a - b; - float d = (float) (c * 1.6666666666666667D); - int e = (int) (v / 10000000L); - float f = d + e; - - return Double.valueOf(f); + return Double.valueOf(((float) ((((float) v / 1.0E7F) + - ((int) (v / 10000000L))) * 1.6666666666666667D)) + ((int) (v / 10000000L))); + } + private String toBin(String v) { + return Integer.toString(Integer.parseInt(v), 2); } private Position decodePosition(Channel channel, SocketAddress remoteAddress, String substring) throws ParseException { - int index = 0; String[] values = substring.split(","); @@ -66,57 +58,40 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { position.setProtocol(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); - String versionHw = values[index++].replaceAll("[^0-9]", ""); - String versionSw = values[index++]; - - position.set(Position.KEY_INDEX, Integer.valueOf(values[index++])); + position.set(Position.KEY_INDEX, Integer.valueOf(values[2])); - String imei = values[index++]; - position.set(Position.KEY_TYPE, values[index++]); + position.set(Position.KEY_TYPE, values[4]); DateFormat dateFormat = new SimpleDateFormat("ddMMyyHHmmss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - String date = String.format("%06d", Integer.parseInt(values[index++])); - String time = String.format("%06d", Integer.parseInt(values[index++])); + String date = String.format("%06d", Integer.parseInt(values[5])); + String time = String.format("%06d", Integer.parseInt(values[6])); position.setTime(dateFormat.parse(date + time)); - position.setLatitude(convertCoordinates(Long.valueOf(values[index++]))); - position.setLongitude(convertCoordinates(Long.valueOf(values[index++]))); + position.setLatitude(convertCoordinates(Long.valueOf(values[7]))); + position.setLongitude(convertCoordinates(Long.valueOf(values[8]))); - position.setSpeed(UnitsConverter.knotsFromKph(Integer.valueOf(values[index++]) / 100)); - position.setCourse(Integer.valueOf(values[index++]) / 100); + position.setSpeed(UnitsConverter.knotsFromKph(Integer.valueOf(values[9]) / 100)); + position.setCourse(Integer.valueOf(values[10]) / 100); - position.set(Position.KEY_ODOMETER, Integer.valueOf(values[index++])); + position.set(Position.KEY_ODOMETER, Integer.valueOf(values[11])); String input = new StringBuilder(String.format("%08d", - Integer.parseInt(values[index++]))).reverse().toString(); + Integer.parseInt(toBin(values[12])))).reverse().toString(); String output = new StringBuilder(String.format("%08d", - Integer.parseInt(values[index++]))).reverse().toString(); - - if (input.substring(0).equals("1")) { - position.set(Position.KEY_ALARM, Position.ALARM_SOS); - } - position.set(Position.KEY_IGNITION, input.substring(4).equals("1")); - - position.setValid(output.substring(0).equals("1")); + Integer.parseInt(toBin(values[13])))).reverse().toString(); - position.set(Position.KEY_POWER, Integer.valueOf(values[index++]) / 100); + position.set(Position.KEY_ALARM, (input.substring(0, 1).equals("1") ? Position.ALARM_SOS : null)); - position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(values[index++])); + position.set(Position.KEY_IGNITION, input.substring(4, 5).equals("1")); - position.set(Position.KEY_RSSI, Integer.valueOf(values[index++])); - - if (values.length == 22) { - String driverUniqueId = values[index++]; - if (!driverUniqueId.isEmpty()) { - position.set(Position.KEY_DRIVER_UNIQUE_ID, driverUniqueId); - } - } + position.setValid(output.substring(0, 1).equals("1")); return position; + } @Override @@ -125,26 +100,19 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { String sentence = (String) msg; - if (sentence.contains(":")) { - - String[] values = sentence.substring(1).split(":"); - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, values[1]); - - if (deviceSession != null) { - sendResponse(channel, "@"); - } - - } else { + if (!sentence.contains(":")) { Position position = decodePosition(channel, remoteAddress, sentence.substring(1)); if (position != null) { - sendResponse(channel, "@"); return position; } } + if (channel != null) { + channel.write(MSG_KEEPALIVE); + } + return null; } -- cgit v1.2.3 From 36a253b19764613e03b538a2b1a48eaa6408f4d5 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 4 Jun 2018 16:36:49 +1200 Subject: Implement Wialon Retranslator protocol --- setup/default.xml | 1 + .../traccar/protocol/RetranslatorFrameDecoder.java | 38 +++++++ src/org/traccar/protocol/RetranslatorProtocol.java | 42 ++++++++ .../protocol/RetranslatorProtocolDecoder.java | 119 +++++++++++++++++++++ .../protocol/RetranslatorProtocolDecoderTest.java | 21 ++++ 5 files changed, 221 insertions(+) create mode 100644 src/org/traccar/protocol/RetranslatorFrameDecoder.java create mode 100644 src/org/traccar/protocol/RetranslatorProtocol.java create mode 100644 src/org/traccar/protocol/RetranslatorProtocolDecoder.java create mode 100644 test/org/traccar/protocol/RetranslatorProtocolDecoderTest.java (limited to 'src') diff --git a/setup/default.xml b/setup/default.xml index badc03086..66c72a877 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -242,5 +242,6 @@ 5164 5165 5166 + 5167 diff --git a/src/org/traccar/protocol/RetranslatorFrameDecoder.java b/src/org/traccar/protocol/RetranslatorFrameDecoder.java new file mode 100644 index 000000000..1836fb26e --- /dev/null +++ b/src/org/traccar/protocol/RetranslatorFrameDecoder.java @@ -0,0 +1,38 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; + +public class RetranslatorFrameDecoder extends FrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + + int length = 4 + ChannelBuffers.swapInt(buf.getInt(buf.readerIndex())); + if (buf.readableBytes() >= length) { + return buf.readBytes(length); + } else { + return null; + } + } + +} diff --git a/src/org/traccar/protocol/RetranslatorProtocol.java b/src/org/traccar/protocol/RetranslatorProtocol.java new file mode 100644 index 000000000..0421390b2 --- /dev/null +++ b/src/org/traccar/protocol/RetranslatorProtocol.java @@ -0,0 +1,42 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class RetranslatorProtocol extends BaseProtocol { + + public RetranslatorProtocol() { + super("retranslator"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new RetranslatorFrameDecoder()); + pipeline.addLast("objectDecoder", new RetranslatorProtocolDecoder(RetranslatorProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/RetranslatorProtocolDecoder.java b/src/org/traccar/protocol/RetranslatorProtocolDecoder.java new file mode 100644 index 000000000..756b0bcaf --- /dev/null +++ b/src/org/traccar/protocol/RetranslatorProtocolDecoder.java @@ -0,0 +1,119 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; +import java.util.Date; + +public class RetranslatorProtocolDecoder extends BaseProtocolDecoder { + + public RetranslatorProtocolDecoder(RetranslatorProtocol protocol) { + super(protocol); + } + + private double readDouble(ChannelBuffer buf) { + byte[] bytes = new byte[8]; + buf.readBytes(bytes); + return ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, bytes).readDouble(); + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + if (channel != null) { + channel.write(ChannelBuffers.wrappedBuffer(new byte[]{0x11})); + } + + ChannelBuffer buf = (ChannelBuffer) msg; + + buf.readUnsignedInt(); // length + + int idLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex(); + String id = buf.readBytes(idLength).toString(StandardCharsets.US_ASCII); + buf.readByte(); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + position.setTime(new Date(buf.readUnsignedInt() * 1000)); + + buf.readUnsignedInt(); // bit flags + + while (buf.readable()) { + + buf.readUnsignedShort(); // block type + int blockEnd = buf.readInt() + buf.readerIndex(); + buf.readUnsignedByte(); // security attribute + int dataType = buf.readUnsignedByte(); + + int nameLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex(); + String name = buf.readBytes(nameLength).toString(StandardCharsets.US_ASCII); + buf.readByte(); + + if (name.equals("posinfo")) { + position.setValid(true); + position.setLongitude(readDouble(buf)); + position.setLatitude(readDouble(buf)); + position.setAltitude(readDouble(buf)); + position.setSpeed(buf.readShort()); + position.setCourse(buf.readShort()); + position.set(Position.KEY_SATELLITES, buf.readByte()); + } else { + switch (dataType) { + case 1: + int len = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex(); + position.set(name, buf.readBytes(len).toString(StandardCharsets.US_ASCII)); + buf.readByte(); + break; + case 3: + position.set(name, buf.readInt()); + break; + case 4: + position.set(name, readDouble(buf)); + break; + case 5: + position.set(name, buf.readLong()); + break; + default: + break; + } + } + + buf.readerIndex(blockEnd); + + } + + if (position.getLatitude() == 0 && position.getLongitude() == 0) { + getLastLocation(position, position.getDeviceTime()); + } + + return position; + } + +} diff --git a/test/org/traccar/protocol/RetranslatorProtocolDecoderTest.java b/test/org/traccar/protocol/RetranslatorProtocolDecoderTest.java new file mode 100644 index 000000000..680570763 --- /dev/null +++ b/test/org/traccar/protocol/RetranslatorProtocolDecoderTest.java @@ -0,0 +1,21 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class RetranslatorProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + RetranslatorProtocolDecoder decoder = new RetranslatorProtocolDecoder(new RetranslatorProtocol()); + + verifyPosition(decoder, binary( + "74000000333533393736303133343435343835004B0BFB70000000030BBB000000270102706F73696E666F00A027AFDF5D9848403AC7253383DD4B400000000000805A40003601460B0BBB0000001200047077725F657874002B8716D9CE973B400BBB00000011010361766C5F696E707574730000000001")); + + verifyPosition(decoder, binary( + "1f010000333533353439303930303934373330005b129b5f000000010bbb000000270102706f73696e666f004e3be14e5ec356c0e0e92f6201282c400000000000000000000000130d0bbb0000000a00036d636300000002c00bbb0000000a00036d6e6300000000010bbb0000000a00036c616300000001490bbb0000000e000363656c6c5f696400000056590bbb0000000a000361636300000000000bbb000000100003646174615f6d6f6465000000000e0bbb0000001200036770735f7265616c5f757000000000000bbb0000000d000373657269616c000000089b0bbb0000001000016d73675f747970650030783232000bbb000000110004637573746f6d0000000000000033400bbb0000001200046d696c65616765000000000000000000")); + + } + +} -- cgit v1.2.3 From 733e620bea7d6e14d43c8472a32d7db9594494e5 Mon Sep 17 00:00:00 2001 From: michaelloliveira Date: Mon, 4 Jun 2018 11:26:52 -0300 Subject: Modified structure for using Patterns. --- src/org/traccar/protocol/SviasProtocolDecoder.java | 134 +++++++++++++-------- 1 file changed, 82 insertions(+), 52 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/SviasProtocolDecoder.java b/src/org/traccar/protocol/SviasProtocolDecoder.java index cca21467a..ef631bf64 100644 --- a/src/org/traccar/protocol/SviasProtocolDecoder.java +++ b/src/org/traccar/protocol/SviasProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2017 Anton Tananaev (anton@traccar.org) + * 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. @@ -17,104 +17,134 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.DeviceSession; -import org.traccar.model.Position; -import java.text.ParseException; +import org.traccar.helper.PatternBuilder; import java.net.SocketAddress; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.TimeZone; +import java.util.regex.Pattern; +import org.traccar.DeviceSession; +import org.traccar.helper.Parser; import org.traccar.helper.UnitsConverter; +import org.traccar.model.Position; public class SviasProtocolDecoder extends BaseProtocolDecoder { - public static final String MSG_KEEPALIVE = "@"; + private static final String MSG_KEEPALIVE = "@"; public SviasProtocolDecoder(SviasProtocol protocol) { super(protocol); } + private static final Pattern PATTERN = new PatternBuilder() + .text("[") // delimiter init + .number("(dddd),") // version hardware + .number("(dddd),") // version software + .number("(d+),") // counter + .number("(d+),") // imei + .any() // model or hourmeter + .number("(d+),") // date (yyyymmdd) + .number("(d+),") // time (hhmmss) + .number("(-?d+),") // longitude + .number("(-?d+),") // latitude + .number("(d+),") // speed + .number("(d+),") // course + .number("(d+),") // odometer + .number("(d+),") // input + .number("(d+),") // output / status + .number("(d+),") // flag pack input 1 + .number("(d+),") // flag pack input 2 + .number("(d+),") // main power voltage + .number("(d+),") // percentual power internal battery + .number("(d+),") // RSSID + .any() + .compile(); + private double convertCoordinates(long v) { return Double.valueOf(((float) ((((float) v / 1.0E7F) - ((int) (v / 10000000L))) * 1.6666666666666667D)) + ((int) (v / 10000000L))); } - private String toBin(String v) { - return Integer.toString(Integer.parseInt(v), 2); - } + @Override + protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) + throws Exception { - private Position decodePosition(Channel channel, SocketAddress remoteAddress, String substring) - throws ParseException { + String sentence = (String) msg; - String[] values = substring.split(","); + if (!sentence.contains(":")) { - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, values[3]); - if (deviceSession == null) { - return null; - } + Parser parser = new Parser(PATTERN, (String) sentence); + if (!parser.matches()) { + return null; + } - Position position = new Position(); - position.setProtocol(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); + Position position = new Position(getProtocolName()); - position.set(Position.KEY_INDEX, Integer.valueOf(values[2])); + String versionHard = parser.next(); + String versionSoft = parser.next(); + String counterInternal = parser.next(); - position.set(Position.KEY_TYPE, values[4]); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } - DateFormat dateFormat = new SimpleDateFormat("ddMMyyHHmmss"); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + position.setDeviceId(deviceSession.getDeviceId()); - String date = String.format("%06d", Integer.parseInt(values[5])); - String time = String.format("%06d", Integer.parseInt(values[6])); + DateFormat dateFormat = new SimpleDateFormat("ddMMyyHHmmss"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - position.setTime(dateFormat.parse(date + time)); + String date = String.format("%06d", parser.nextInt()); + String time = String.format("%06d", parser.nextInt()); - position.setLatitude(convertCoordinates(Long.valueOf(values[7]))); - position.setLongitude(convertCoordinates(Long.valueOf(values[8]))); + position.setTime(dateFormat.parse(date + time)); - position.setSpeed(UnitsConverter.knotsFromKph(Integer.valueOf(values[9]) / 100)); - position.setCourse(Integer.valueOf(values[10]) / 100); + position.setLatitude(convertCoordinates(parser.nextLong())); + position.setLongitude(convertCoordinates(parser.nextLong())); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextInt() / 100)); + position.setCourse(parser.nextInt() / 100); + position.setAltitude(0); - position.set(Position.KEY_ODOMETER, Integer.valueOf(values[11])); + position.set(Position.KEY_ODOMETER, parser.nextInt()); - String input = new StringBuilder(String.format("%08d", - Integer.parseInt(toBin(values[12])))).reverse().toString(); + String input = new StringBuilder(String.format("%08d", + Integer.parseInt(Integer.toString(parser.nextInt(), 2)))).reverse().toString(); - String output = new StringBuilder(String.format("%08d", - Integer.parseInt(toBin(values[13])))).reverse().toString(); + String output = new StringBuilder(String.format("%08d", + Integer.parseInt(Integer.toString(parser.nextInt(), 2)))).reverse().toString(); - position.set(Position.KEY_ALARM, (input.substring(0, 1).equals("1") ? Position.ALARM_SOS : null)); + position.set(Position.KEY_ALARM, (input.substring(0, 1).equals("1") + ? Position.ALARM_SOS : null)); - position.set(Position.KEY_IGNITION, input.substring(4, 5).equals("1")); + position.set(Position.KEY_IGNITION, input.substring(4, 5).equals("1")); - position.setValid(output.substring(0, 1).equals("1")); + position.setValid(output.substring(0, 1).equals("1")); - return position; + String pck1 = parser.next(); + String pck2 = parser.next(); - } + position.set(Position.KEY_POWER, parser.nextInt() / 1000); - @Override - protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) - throws Exception { + position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt()); - String sentence = (String) msg; + position.set(Position.KEY_RSSI, parser.nextInt()); - if (!sentence.contains(":")) { + if (channel != null) { + channel.write(MSG_KEEPALIVE); + } - Position position = decodePosition(channel, remoteAddress, sentence.substring(1)); + return position; - if (position != null) { - return position; + } else { + //send keepalive for message check + if (channel != null) { + channel.write(MSG_KEEPALIVE); } - } - if (channel != null) { - channel.write(MSG_KEEPALIVE); + return null; } - return null; - } } -- cgit v1.2.3 From 504277e7a77bdf27b1bed31f585ef01dace1a3a5 Mon Sep 17 00:00:00 2001 From: michaelloliveira Date: Mon, 4 Jun 2018 11:29:20 -0300 Subject: Modified structure for using Patterns. --- src/org/traccar/protocol/SviasProtocol.java | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/SviasProtocol.java b/src/org/traccar/protocol/SviasProtocol.java index b58872baf..8a45f43cb 100644 --- a/src/org/traccar/protocol/SviasProtocol.java +++ b/src/org/traccar/protocol/SviasProtocol.java @@ -21,10 +21,9 @@ 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 org.traccar.model.Command; import java.util.List; -import org.jboss.netty.bootstrap.ConnectionlessBootstrap; +import org.traccar.model.Command; public class SviasProtocol extends BaseProtocol { @@ -41,7 +40,7 @@ public class SviasProtocol extends BaseProtocol { Command.TYPE_ALARM_REMOVE); } - @Override + @Override public void initTrackerServers(List serverList) { serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { @Override @@ -52,15 +51,6 @@ public class SviasProtocol extends BaseProtocol { pipeline.addLast("objectDecoder", new SviasProtocolDecoder(SviasProtocol.this)); } }); - serverList.add(new TrackerServer(new ConnectionlessBootstrap(), getName()) { - @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("stringEncoder", new StringEncoder()); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("objectEncoder", new SviasProtocolEncoder()); - pipeline.addLast("objectDecoder", new SviasProtocolDecoder(SviasProtocol.this)); - } - }); } } -- cgit v1.2.3 From 7d83f65d3b2f9f57f3c1a4fc1288272571662f55 Mon Sep 17 00:00:00 2001 From: michaelloliveira Date: Tue, 5 Jun 2018 15:46:23 -0300 Subject: Optimized! --- src/org/traccar/protocol/SviasProtocolDecoder.java | 48 ++++++++-------------- 1 file changed, 16 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/SviasProtocolDecoder.java b/src/org/traccar/protocol/SviasProtocolDecoder.java index ef631bf64..bc459de25 100644 --- a/src/org/traccar/protocol/SviasProtocolDecoder.java +++ b/src/org/traccar/protocol/SviasProtocolDecoder.java @@ -31,19 +31,15 @@ import org.traccar.model.Position; public class SviasProtocolDecoder extends BaseProtocolDecoder { - private static final String MSG_KEEPALIVE = "@"; - public SviasProtocolDecoder(SviasProtocol protocol) { super(protocol); } private static final Pattern PATTERN = new PatternBuilder() - .text("[") // delimiter init - .number("(dddd),") // version hardware - .number("(dddd),") // version software - .number("(d+),") // counter - .number("(d+),") // imei - .any() // model or hourmeter + .text("[") // delimiter init + .any() + .number("(dddddddd),") // imei + .any() .number("(d+),") // date (yyyymmdd) .number("(d+),") // time (hhmmss) .number("(-?d+),") // longitude @@ -53,9 +49,8 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { .number("(d+),") // odometer .number("(d+),") // input .number("(d+),") // output / status - .number("(d+),") // flag pack input 1 - .number("(d+),") // flag pack input 2 - .number("(d+),") // main power voltage + .any() + .number("(ddddd),") // main power voltage .number("(d+),") // percentual power internal battery .number("(d+),") // RSSID .any() @@ -71,6 +66,7 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { throws Exception { String sentence = (String) msg; + Object result = null; if (!sentence.contains(":")) { @@ -81,10 +77,6 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(getProtocolName()); - String versionHard = parser.next(); - String versionSoft = parser.next(); - String counterInternal = parser.next(); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); if (deviceSession == null) { return null; @@ -102,8 +94,8 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { position.setLatitude(convertCoordinates(parser.nextLong())); position.setLongitude(convertCoordinates(parser.nextLong())); - position.setSpeed(UnitsConverter.knotsFromKph(parser.nextInt() / 100)); - position.setCourse(parser.nextInt() / 100); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0) / 100)); + position.setCourse(parser.nextDouble(0) / 100); position.setAltitude(0); position.set(Position.KEY_ODOMETER, parser.nextInt()); @@ -121,30 +113,22 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { position.setValid(output.substring(0, 1).equals("1")); - String pck1 = parser.next(); - String pck2 = parser.next(); - - position.set(Position.KEY_POWER, parser.nextInt() / 1000); + position.set(Position.KEY_POWER, parser.nextDouble(0) / 1000); position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt()); position.set(Position.KEY_RSSI, parser.nextInt()); - if (channel != null) { - channel.write(MSG_KEEPALIVE); - } - - return position; + result = position; - } else { - //send keepalive for message check - if (channel != null) { - channel.write(MSG_KEEPALIVE); - } + } - return null; + if (channel != null) { + channel.write("@"); } + return result; + } } -- cgit v1.2.3 From 6c68a7531289c4166b7e8ea58fc1ffdbe4893ed4 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 1 Jun 2018 05:14:08 +1200 Subject: Fix GPS103 course decoding --- src/org/traccar/protocol/Gps103ProtocolDecoder.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/Gps103ProtocolDecoder.java b/src/org/traccar/protocol/Gps103ProtocolDecoder.java index 9883079d1..9a27dd9d2 100644 --- a/src/org/traccar/protocol/Gps103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gps103ProtocolDecoder.java @@ -66,13 +66,13 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { .expression("([EW]),").optional() .number("(d+)(dd.d+),") // longitude (dddmm.mmmm) .expression("([EW])?,").optional() - .number("(d+.?d*)?,").optional() // speed - .number("(d+.?d*)?,").optional() // course - .number("(d+.?d*)?,").optional() // altitude - .number("([01])?,").optional() // ignition - .number("([01])?,").optional() // door - .number("(?:(d+.d+)%)?,").optional() // fuel 1 - .number("(?:(d+.d+)%)?,").optional() // fuel 2 + .number("(d+.?d*)?").optional() // speed + .number(",(d+.?d*)?").optional() // course + .number(",(d+.?d*)?").optional() // altitude + .number(",([01])?").optional() // ignition + .number(",([01])?").optional() // door + .number(",(?:(d+.d+)%)?").optional() // fuel 1 + .number(",(?:(d+.d+)%)?").optional() // fuel 2 .number("(-?d+)?") // temperature .groupEnd() .any() -- cgit v1.2.3 From acf93dbe1b5c03df4f8f82ffb7fb0cace767a416 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 8 Jun 2018 14:05:41 +1200 Subject: Clean up Svias decoder --- setup/default.xml | 1 + src/org/traccar/protocol/SviasProtocol.java | 4 +- src/org/traccar/protocol/SviasProtocolDecoder.java | 125 ++++++++------------- src/org/traccar/protocol/SviasProtocolEncoder.java | 5 +- .../traccar/protocol/SviasProtocolDecoderTest.java | 8 +- 5 files changed, 61 insertions(+), 82 deletions(-) (limited to 'src') diff --git a/setup/default.xml b/setup/default.xml index 66c72a877..ee80fb4df 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -243,5 +243,6 @@ 5165 5166 5167 + 5168 diff --git a/src/org/traccar/protocol/SviasProtocol.java b/src/org/traccar/protocol/SviasProtocol.java index 8a45f43cb..badf3d091 100644 --- a/src/org/traccar/protocol/SviasProtocol.java +++ b/src/org/traccar/protocol/SviasProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; +import org.traccar.CharacterDelimiterFrameDecoder; import org.traccar.TrackerServer; import java.util.List; @@ -45,6 +46,7 @@ public class SviasProtocol extends BaseProtocol { serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new CharacterDelimiterFrameDecoder(1024, "]")); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("objectEncoder", new SviasProtocolEncoder()); diff --git a/src/org/traccar/protocol/SviasProtocolDecoder.java b/src/org/traccar/protocol/SviasProtocolDecoder.java index bc459de25..9375038ed 100644 --- a/src/org/traccar/protocol/SviasProtocolDecoder.java +++ b/src/org/traccar/protocol/SviasProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,12 +17,10 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.helper.BitUtil; import org.traccar.helper.PatternBuilder; import java.net.SocketAddress; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.TimeZone; import java.util.regex.Pattern; import org.traccar.DeviceSession; import org.traccar.helper.Parser; @@ -36,99 +34,70 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() - .text("[") // delimiter init - .any() - .number("(dddddddd),") // imei - .any() - .number("(d+),") // date (yyyymmdd) - .number("(d+),") // time (hhmmss) - .number("(-?d+),") // longitude - .number("(-?d+),") // latitude - .number("(d+),") // speed - .number("(d+),") // course - .number("(d+),") // odometer - .number("(d+),") // input - .number("(d+),") // output / status - .any() - .number("(ddddd),") // main power voltage - .number("(d+),") // percentual power internal battery - .number("(d+),") // RSSID + .text("[") // delimiter + .number("d{4},") // hardware version + .number("d{4},") // software version + .number("d+,") // index + .number("(d+),") // imei + .number("d+,") // hour meter + .number("(d+)(dd)(dd),") // date (dmmyy) + .number("(d+)(dd)(dd),") // time (hmmss) + .number("(-?)(d+)(dd)(d{5}),") // latitude + .number("(-?)(d+)(dd)(d{5}),") // longitude + .number("(d+),") // speed + .number("(d+),") // course + .number("(d+),") // odometer + .number("(d+),") // input + .number("(d+),") // output / status + .number("(d),") + .number("(d),") + .number("(d+),") // power + .number("(d+),") // battery level + .number("(d+),") // rssi .any() .compile(); - private double convertCoordinates(long v) { - return Double.valueOf(((float) ((((float) v / 1.0E7F) - - ((int) (v / 10000000L))) * 1.6666666666666667D)) + ((int) (v / 10000000L))); - } - @Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - String sentence = (String) msg; - Object result = null; - - if (!sentence.contains(":")) { - - Parser parser = new Parser(PATTERN, (String) sentence); - if (!parser.matches()) { - return null; - } - - Position position = new Position(getProtocolName()); - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { - return null; - } - - position.setDeviceId(deviceSession.getDeviceId()); - - DateFormat dateFormat = new SimpleDateFormat("ddMMyyHHmmss"); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - - String date = String.format("%06d", parser.nextInt()); - String time = String.format("%06d", parser.nextInt()); - - position.setTime(dateFormat.parse(date + time)); - - position.setLatitude(convertCoordinates(parser.nextLong())); - position.setLongitude(convertCoordinates(parser.nextLong())); - position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0) / 100)); - position.setCourse(parser.nextDouble(0) / 100); - position.setAltitude(0); - - position.set(Position.KEY_ODOMETER, parser.nextInt()); - - String input = new StringBuilder(String.format("%08d", - Integer.parseInt(Integer.toString(parser.nextInt(), 2)))).reverse().toString(); - - String output = new StringBuilder(String.format("%08d", - Integer.parseInt(Integer.toString(parser.nextInt(), 2)))).reverse().toString(); + Parser parser = new Parser(PATTERN, (String) msg); + if (!parser.matches()) { + return null; + } - position.set(Position.KEY_ALARM, (input.substring(0, 1).equals("1") - ? Position.ALARM_SOS : null)); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } - position.set(Position.KEY_IGNITION, input.substring(4, 5).equals("1")); + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); - position.setValid(output.substring(0, 1).equals("1")); + position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_MIN)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_MIN)); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble() * 0.01)); + position.setCourse(parser.nextDouble() * 0.01); - position.set(Position.KEY_POWER, parser.nextDouble(0) / 1000); + position.set(Position.KEY_ODOMETER, parser.nextInt() * 100); - position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt()); + int input = parser.nextInt(); + int output = parser.nextInt(); - position.set(Position.KEY_RSSI, parser.nextInt()); + position.set(Position.KEY_ALARM, BitUtil.check(input, 0) ? Position.ALARM_SOS : null); + position.set(Position.KEY_IGNITION, BitUtil.check(input, 4)); + position.setValid(BitUtil.check(output, 0)); - result = position; - - } + position.set(Position.KEY_POWER, parser.nextInt() * 0.001); + position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt()); + position.set(Position.KEY_RSSI, parser.nextInt()); if (channel != null) { channel.write("@"); } - return result; - + return position; } } diff --git a/src/org/traccar/protocol/SviasProtocolEncoder.java b/src/org/traccar/protocol/SviasProtocolEncoder.java index 518268cab..c26ee2032 100644 --- a/src/org/traccar/protocol/SviasProtocolEncoder.java +++ b/src/org/traccar/protocol/SviasProtocolEncoder.java @@ -1,6 +1,6 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org) + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,4 +47,5 @@ public class SviasProtocolEncoder extends StringProtocolEncoder { } return null; } + } diff --git a/test/org/traccar/protocol/SviasProtocolDecoderTest.java b/test/org/traccar/protocol/SviasProtocolDecoderTest.java index 064ab16f1..3bf355a15 100644 --- a/test/org/traccar/protocol/SviasProtocolDecoderTest.java +++ b/test/org/traccar/protocol/SviasProtocolDecoderTest.java @@ -11,7 +11,13 @@ public class SviasProtocolDecoderTest extends ProtocolTest { SviasProtocolDecoder decoder = new SviasProtocolDecoder(new SviasProtocol()); verifyPosition(decoder, text( - "[7061,3041,57,20324277,710,40618,141342,-93155840,-371754060,0,20469,0,16,1,0,0,11323,100,9,,32,4695]")); + "[7061,3041,57,20324277,710,40618,141342,-93155840,-371754060,0,20469,0,16,1,0,0,11323,100,9,,32,4695")); + + verifyPosition(decoder, text( + "[7041,3041,8629,20856286,710,60618,201027,-92268040,-371346250,7994,31844,38271,16,1,0,0,13416,100,0,0,5089")); + + verifyPosition(decoder, text( + "[7051,3041,15270,30179873,710,70618,40335,-94679080,-360604930,0,35454,23148,0,1,0,0,12542,100,13,32,4971")); } -- cgit v1.2.3 From 37868cf0b5be5b99e48b28d1aafacdc74c2aff9e Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 8 Jun 2018 10:00:28 +0500 Subject: Rename all tables to avoid using reserved words --- schema/changelog-4.0.xml | 56 +++++++++++++++++++++++++++++++ schema/changelog-master.xml | 1 + setup/default.xml | 16 ++++----- src/org/traccar/database/DataManager.java | 5 +-- 4 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 schema/changelog-4.0.xml (limited to 'src') diff --git a/schema/changelog-4.0.xml b/schema/changelog-4.0.xml new file mode 100644 index 000000000..04e9fe0c4 --- /dev/null +++ b/schema/changelog-4.0.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/schema/changelog-master.xml b/schema/changelog-master.xml index 3e7944238..e8fbc0c4b 100644 --- a/schema/changelog-master.xml +++ b/schema/changelog-master.xml @@ -18,4 +18,5 @@ + diff --git a/setup/default.xml b/setup/default.xml index ee80fb4df..c427217f7 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -41,36 +41,36 @@ ./schema/changelog-master.xml - SELECT * FROM users + SELECT * FROM tc_users WHERE email = :email OR login = :email - SELECT * FROM positions WHERE deviceId = :deviceId AND fixTime BETWEEN :from AND :to ORDER BY fixTime + SELECT * FROM tc_positions WHERE deviceId = :deviceId AND fixTime BETWEEN :from AND :to ORDER BY fixTime - SELECT positions.* FROM positions INNER JOIN devices ON positions.id = devices.positionid; + SELECT tc_positions.* FROM tc_positions INNER JOIN tc_devices ON tc_positions.id = tc_devices.positionid; - UPDATE devices SET positionId = :id WHERE id = :deviceId + UPDATE tc_devices SET positionId = :id WHERE id = :deviceId - SELECT * FROM events WHERE deviceId = :deviceId AND serverTime BETWEEN :from AND :to ORDER BY serverTime + SELECT * FROM tc_events WHERE deviceId = :deviceId AND serverTime BETWEEN :from AND :to ORDER BY serverTime - DELETE FROM positions WHERE serverTime < :serverTime AND id NOT IN (SELECT positionId FROM devices WHERE positionId IS NOT NULL) + DELETE FROM tc_positions WHERE serverTime < :serverTime AND id NOT IN (SELECT positionId FROM tc_devices WHERE positionId IS NOT NULL) - DELETE FROM events WHERE serverTime < :serverTime + DELETE FROM tc_events WHERE serverTime < :serverTime - SELECT * FROM statistics WHERE captureTime BETWEEN :from AND :to ORDER BY captureTime + SELECT * FROM tc_statistics WHERE captureTime BETWEEN :from AND :to ORDER BY captureTime diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index ae5ec2d5f..06dd26b17 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -273,11 +273,12 @@ public class DataManager { if (propertyName.equals("ManagedUser")) { propertyName = "User"; } - return Introspector.decapitalize(owner.getSimpleName()) + "_" + Introspector.decapitalize(propertyName); + return "tc_" + Introspector.decapitalize(owner.getSimpleName()) + + "_" + Introspector.decapitalize(propertyName); } private static String getObjectsTableName(Class clazz) { - String result = Introspector.decapitalize(clazz.getSimpleName()); + String result = "tc_" + Introspector.decapitalize(clazz.getSimpleName()); // Add "s" ending if object name is not plural already if (!result.endsWith("s")) { result += "s"; -- cgit v1.2.3 From ba67624e3c4d18f313aa0167d7276a58bdbac445 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 9 Jun 2018 00:14:16 +1200 Subject: Move response to the top --- src/org/traccar/protocol/SviasProtocolDecoder.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/SviasProtocolDecoder.java b/src/org/traccar/protocol/SviasProtocolDecoder.java index 9375038ed..c2ea33ae9 100644 --- a/src/org/traccar/protocol/SviasProtocolDecoder.java +++ b/src/org/traccar/protocol/SviasProtocolDecoder.java @@ -61,6 +61,10 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + if (channel != null) { + channel.write("@"); + } + Parser parser = new Parser(PATTERN, (String) msg); if (!parser.matches()) { return null; @@ -93,10 +97,6 @@ public class SviasProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt()); position.set(Position.KEY_RSSI, parser.nextInt()); - if (channel != null) { - channel.write("@"); - } - return position; } -- cgit v1.2.3 From f56db9b1d895c027d983696db7daed900ca881ab Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 9 Jun 2018 22:22:01 +1200 Subject: Implement eSeal CT-C protocol --- setup/default.xml | 1 + src/org/traccar/model/Position.java | 2 + src/org/traccar/protocol/EsealProtocol.java | 47 +++++++ src/org/traccar/protocol/EsealProtocolDecoder.java | 154 +++++++++++++++++++++ .../traccar/protocol/EsealProtocolDecoderTest.java | 33 +++++ 5 files changed, 237 insertions(+) create mode 100644 src/org/traccar/protocol/EsealProtocol.java create mode 100644 src/org/traccar/protocol/EsealProtocolDecoder.java create mode 100644 test/org/traccar/protocol/EsealProtocolDecoderTest.java (limited to 'src') diff --git a/setup/default.xml b/setup/default.xml index c427217f7..15d169f63 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -244,5 +244,6 @@ 5166 5167 5168 + 5169 diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index b0fe7a79b..4b327cbd2 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -112,6 +112,8 @@ public class Position extends Message { public static final String ALARM_POWER_OFF = "powerOff"; public static final String ALARM_POWER_ON = "powerOn"; public static final String ALARM_DOOR = "door"; + public static final String ALARM_LOCK = "lock"; + public static final String ALARM_UNLOCK = "unlock"; public static final String ALARM_GEOFENCE = "geofence"; public static final String ALARM_GEOFENCE_ENTER = "geofenceEnter"; public static final String ALARM_GEOFENCE_EXIT = "geofenceExit"; diff --git a/src/org/traccar/protocol/EsealProtocol.java b/src/org/traccar/protocol/EsealProtocol.java new file mode 100644 index 000000000..534e3d0c4 --- /dev/null +++ b/src/org/traccar/protocol/EsealProtocol.java @@ -0,0 +1,47 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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 EsealProtocol extends BaseProtocol { + + public EsealProtocol() { + super("eseal"); + } + + @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 EsealProtocolDecoder(EsealProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/EsealProtocolDecoder.java b/src/org/traccar/protocol/EsealProtocolDecoder.java new file mode 100644 index 000000000..67b45b33f --- /dev/null +++ b/src/org/traccar/protocol/EsealProtocolDecoder.java @@ -0,0 +1,154 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.Context; +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 EsealProtocolDecoder extends BaseProtocolDecoder { + + private String config; + + public EsealProtocolDecoder(EsealProtocol protocol) { + super(protocol); + config = Context.getConfig().getString(getProtocolName() + ".config"); + } + + private static final Pattern PATTERN = new PatternBuilder() + .text("##S,") + .expression("[^,]+,") // device type + .number("(d+),") // device id + .number("d+,") // customer id + .expression("[^,]+,") // firmware version + .expression("([^,]+),") // type + .number("(d+),") // index + .number("(dddd)-(dd)-(dd),") // date + .number("(dd):(dd):(dd),") // time + .number("d+,") // interval + .expression("([AV]),") // validity + .number("(d+.d+)([NS]) ") // latitude + .number("(d+.d+)([EW]),") // longitude + .number("(d+),") // course + .number("(d+),") // speed + .expression("([^,]+),") // door + .number("(d+.d+),") // acceleration + .expression("([^,]+),") // nfc + .number("(d+.d+),") // battery + .number("(-?d+),") // rssi + .text("E##") + .compile(); + + private void sendResponse(Channel channel, String prefix, String type, String payload) { + if (channel != null) { + channel.write(prefix + type + "," + payload + ",E##\r\n"); + } + } + + private String decodeAlarm(String type) { + switch (type) { + case "Event-Door": + return Position.ALARM_DOOR; + case "Event-Shock": + return Position.ALARM_SHOCK; + case "Event-Drop": + return Position.ALARM_FALL_DOWN; + case "Event-Lock": + return Position.ALARM_LOCK; + case "Event-RC-Unlock": + return Position.ALARM_UNLOCK; + default: + return null; + } + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + String sentence = (String) msg; + Parser parser = new Parser(PATTERN, sentence); + if (!parser.matches()) { + return null; + } + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + String type = parser.next(); + String prefix = sentence.substring(0, sentence.indexOf(type)); + int index = parser.nextInt(); + + position.set(Position.KEY_INDEX, index); + position.set(Position.KEY_ALARM, decodeAlarm(type)); + + switch (type) { + case "Startup": + sendResponse(channel, prefix, type + " ACK", index + "," + config); + break; + case "Normal": + case "Termination": + case "Event-Door": + case "Event-Shock": + case "Event-Drop": + case "Event-Lock": + case "Event-RC-Unlock": + sendResponse(channel, prefix, type + " ACK", String.valueOf(index)); + break; + default: + break; + } + + position.setTime(parser.nextDateTime()); + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setCourse(parser.nextInt()); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextInt())); + + switch (parser.next()) { + case "Open": + position.set(Position.KEY_DOOR, true); + break; + case "Close": + position.set(Position.KEY_DOOR, false); + break; + default: + break; + } + + position.set(Position.KEY_ACCELERATION, parser.nextDouble()); + position.set("nfc", parser.next()); + position.set(Position.KEY_BATTERY, parser.nextDouble()); + position.set(Position.KEY_RSSI, parser.nextInt()); + + return position; + } + +} diff --git a/test/org/traccar/protocol/EsealProtocolDecoderTest.java b/test/org/traccar/protocol/EsealProtocolDecoderTest.java new file mode 100644 index 000000000..c14652dd1 --- /dev/null +++ b/test/org/traccar/protocol/EsealProtocolDecoderTest.java @@ -0,0 +1,33 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class EsealProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + EsealProtocolDecoder decoder = new EsealProtocolDecoder(new EsealProtocol()); + + verifyPosition(decoder, text( + "##S,eSeal,1000821,256,3.0.6,Normal,34,2017-08-31,08:14:40,15,A,25.708828N 100.372870W,10,0,Close,0.71,0:0:3:0,3.8,-73,E##")); + + verifyPosition(decoder, text( + "##S,eSeal,1000821,256,3.0.6,Startup,1,2017-08-31,02:01:19,3,V,0.000000N 0.000000E,0,0,Close,3.25,0:0:5:0,3.8,-93,E##")); + + verifyNull(decoder, text( + "##S,eSeal,1000821,256,3.0.6,Startup OK,1,180,30,30,16,1,E##")); + + verifyNull(decoder, text( + "##S,eSeal,1000821,256,3.0.6,Startup OK,1,180,30,30,16,1,E##")); + + verifyPosition(decoder, text( + "##S,eSeal,1000898,256,3.0.6,Normal,6,2017-09-06,23:48:39,3,V,0.000000N 0.000000E,0,0,Close,1.0,0:0:3:0,4.0,-81,E##")); + + verifyNull(decoder, text( + "##S,eSeal,1000898,256,3.0.6,RC-NFC DEL ACK,,E##")); + + } + +} -- cgit v1.2.3 From dff539dd8c556c727ce1262941ba2e80bb82a9fa Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 10 Jun 2018 10:37:12 +1200 Subject: Support eSeal button normal --- src/org/traccar/protocol/EsealProtocolDecoder.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/org/traccar/protocol/EsealProtocolDecoder.java b/src/org/traccar/protocol/EsealProtocolDecoder.java index 67b45b33f..90f360d66 100644 --- a/src/org/traccar/protocol/EsealProtocolDecoder.java +++ b/src/org/traccar/protocol/EsealProtocolDecoder.java @@ -113,6 +113,7 @@ public class EsealProtocolDecoder extends BaseProtocolDecoder { sendResponse(channel, prefix, type + " ACK", index + "," + config); break; case "Normal": + case "Button-Normal": case "Termination": case "Event-Door": case "Event-Shock": -- cgit v1.2.3 From 0cd2e4acd1a1a1000eb8e1e4a82ecb47809bd106 Mon Sep 17 00:00:00 2001 From: Jake Stride Date: Sun, 10 Jun 2018 07:16:47 +0800 Subject: Update WebDataHandler.java Added accuracy as an additional parameter that can be passed to url forward --- src/org/traccar/WebDataHandler.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java index 79221de9a..763493437 100644 --- a/src/org/traccar/WebDataHandler.java +++ b/src/org/traccar/WebDataHandler.java @@ -98,6 +98,7 @@ public class WebDataHandler extends BaseDataHandler { .replace("{altitude}", String.valueOf(position.getAltitude())) .replace("{speed}", String.valueOf(position.getSpeed())) .replace("{course}", String.valueOf(position.getCourse())) + .replace("{accuracy}", String.valueOf(position.getAccuracy())) .replace("{statusCode}", calculateStatus(position)); if (position.getAddress() != null) { -- cgit v1.2.3 From 77f5a12b9c50cdf317528a8e1569c3cf47b251f4 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 10 Jun 2018 22:30:19 +1200 Subject: Change password config key --- src/org/traccar/protocol/AquilaProtocolDecoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/org/traccar/protocol/AquilaProtocolDecoder.java b/src/org/traccar/protocol/AquilaProtocolDecoder.java index 9963ead34..3821e13ca 100644 --- a/src/org/traccar/protocol/AquilaProtocolDecoder.java +++ b/src/org/traccar/protocol/AquilaProtocolDecoder.java @@ -301,7 +301,7 @@ public class AquilaProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_ALARM, Position.ALARM_SOS); if (channel != null) { String password = Context.getIdentityManager().lookupAttributeString( - position.getDeviceId(), getProtocolName() + ".language", "aquila123", true); + position.getDeviceId(), getProtocolName() + ".password", "aquila123", true); channel.write("#set$" + id + "@" + password + "#EMR_MODE:0*"); } } -- cgit v1.2.3 From 9cbd8fafc312996b9b5dae68b6c3ecf68d8facf0 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 11 Jun 2018 05:49:35 +1200 Subject: Implement Atlanta OBD protocol --- src/org/traccar/protocol/L100FrameDecoder.java | 24 +++-- src/org/traccar/protocol/L100ProtocolDecoder.java | 109 ++++++++++++++++++++- .../org/traccar/protocol/L100FrameDecoderTest.java | 4 + .../traccar/protocol/L100ProtocolDecoderTest.java | 6 ++ 4 files changed, 134 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/L100FrameDecoder.java b/src/org/traccar/protocol/L100FrameDecoder.java index 98c2f9768..c8a520772 100644 --- a/src/org/traccar/protocol/L100FrameDecoder.java +++ b/src/org/traccar/protocol/L100FrameDecoder.java @@ -26,22 +26,32 @@ public class L100FrameDecoder extends FrameDecoder { protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - if (buf.readableBytes() < 80) { + if (buf.readableBytes() < 10) { return null; } - int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x02); - if (index == -1) { - index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x04); + int header = buf.getByte(buf.readerIndex()); + boolean obd = header == 'L' || header == 'H'; + + int index; + if (obd) { + index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + } else { + index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x02); if (index == -1) { - return null; + index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x04); + if (index == -1) { + return null; + } } } index += 2; // checksum - if (buf.readableBytes() >= index - buf.readerIndex()) { - buf.skipBytes(2); // header + if (buf.writerIndex() >= index) { + if (!obd) { + buf.skipBytes(2); // header + } ChannelBuffer frame = buf.readBytes(index - buf.readerIndex() - 2); buf.skipBytes(2); // footer return frame; diff --git a/src/org/traccar/protocol/L100ProtocolDecoder.java b/src/org/traccar/protocol/L100ProtocolDecoder.java index fe2c298e1..5842b35b6 100644 --- a/src/org/traccar/protocol/L100ProtocolDecoder.java +++ b/src/org/traccar/protocol/L100ProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -66,23 +67,59 @@ public class L100ProtocolDecoder extends BaseProtocolDecoder { .text("ATL") .compile(); + private static final Pattern PATTERN_OBD = new PatternBuilder() + .expression("[LH],") // archive + .text("ATL,") + .number("(d{15}),") // imei + .number("(d+),") // type + .number("(d+),") // index + .groupBegin() + .number("(dd)(dd)(dd),") // time (hhmmss) + .number("(dd)(dd)(dd),") // date (ddmmyy) + .expression("([AV]),") // validity + .number("(d+.d+);([NS]),") // latitude + .number("(d+.d+);([EW]),") // longitude + .number("(d+),") // speed + .number("(d+),") // course + .number("(d+.d+),") // odometer + .number("(d+.d+),") // battery + .number("(d+),") // rssi + .number("(d+),") // mcc + .number("(d+),") // mnc + .number("(d+),") // lac + .number("(x+),") // cid + .number("#(d)(d)(d)(d),") // status + .number("(d),") // overspeed + .text("ATL,") + .groupEnd("?") + .compile(); + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { String sentence = (String) msg; + if (sentence.startsWith("L") || sentence.startsWith("H")) { + return decodeObd(channel, remoteAddress, sentence); + } else { + return decodeNormal(channel, remoteAddress, sentence); + } + } + + private Object decodeNormal(Channel channel, SocketAddress remoteAddress, String sentence) { + Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; } - Position position = new Position(getProtocolName()); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); if (deviceSession == null) { return null; } + + Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() @@ -112,4 +149,72 @@ public class L100ProtocolDecoder extends BaseProtocolDecoder { return position; } + private Object decodeObd(Channel channel, SocketAddress remoteAddress, String sentence) { + + Parser parser = new Parser(PATTERN_OBD, sentence); + if (!parser.matches()) { + return null; + } + + String imei = parser.next(); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { + return null; + } + + int type = parser.nextInt(); + int index = parser.nextInt(); + + if (type == 1) { + if (channel != null) { + String response = "@" + imei + ",00," + index + ","; + response += "*" + (char) Checksum.xor(response); + channel.write(response, remoteAddress); + } + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.setTime(parser.nextDateTime(Parser.DateTimeFormat.HMS_DMY)); + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setSpeed(parser.nextInt()); + position.setCourse(parser.nextInt()); + + position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000); + position.set(Position.KEY_BATTERY, parser.nextDouble()); + + int rssi = parser.nextInt(); + position.setNetwork(new Network(CellTower.from( + parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextHexInt(), rssi))); + + position.set(Position.KEY_IGNITION, parser.nextInt() == 1); + parser.next(); // reserved + + switch (parser.nextInt()) { + case 0: + position.set(Position.KEY_ALARM, Position.ALARM_BRAKING); + break; + case 2: + position.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION); + break; + case 1: + position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); + break; + default: + break; + } + + position.set(Position.KEY_CHARGE, parser.nextInt() == 1); + + if (parser.nextInt() == 1) { + position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED); + } + + return position; + } + } diff --git a/test/org/traccar/protocol/L100FrameDecoderTest.java b/test/org/traccar/protocol/L100FrameDecoderTest.java index 7cb385d3e..a94f2eb0d 100644 --- a/test/org/traccar/protocol/L100FrameDecoderTest.java +++ b/test/org/traccar/protocol/L100FrameDecoderTest.java @@ -10,6 +10,10 @@ public class L100FrameDecoderTest extends ProtocolTest { L100FrameDecoder decoder = new L100FrameDecoder(); + verifyFrame( + binary("4c2c41544c2c3836363739353033303437373935322c30312c303033352c"), + decoder.decode(null, null, binary("4c2c41544c2c3836363739353033303437373935322c30312c303033352c2a28"))); + verifyFrame( binary("41544c3335363839353033373533333734352c244750524d432c3131313731392e3030302c412c323833382e303034352c4e2c30373731332e333730372c452c302e30302c2c3132303831302c2c2c412a3735242c2330313130303131313030313031302c4e2e432c4e2e432c4e2e432c31323334352e36372c33312e342c342e322c32312c4d43432c4d4e432c4c41432c43656c6c494441544c"), decoder.decode(null, null, binary("200141544c3335363839353033373533333734352c244750524d432c3131313731392e3030302c412c323833382e303034352c4e2c30373731332e333730372c452c302e30302c2c3132303831302c2c2c412a3735242c2330313130303131313030313031302c4e2e432c4e2e432c4e2e432c31323334352e36372c33312e342c342e322c32312c4d43432c4d4e432c4c41432c43656c6c494441544c027a"))); diff --git a/test/org/traccar/protocol/L100ProtocolDecoderTest.java b/test/org/traccar/protocol/L100ProtocolDecoderTest.java index 1e48ecef5..65a5a1afe 100644 --- a/test/org/traccar/protocol/L100ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/L100ProtocolDecoderTest.java @@ -10,6 +10,12 @@ public class L100ProtocolDecoderTest extends ProtocolTest { L100ProtocolDecoder decoder = new L100ProtocolDecoder(new L100Protocol()); + verifyPosition(decoder, text( + "H,ATL,866795030478513,02,0981,054448,230318,A,28.633486;N,77.222595;E,0,154,1.14,4.2,18,404,4,88,ad7b,#1031,0,ATL,")); + + verifyNull(decoder, text( + "L,ATL,866795030477952,01,0035,")); + verifyPosition(decoder, text( "ATL861693039769518,$GPRMC,074930.000,A,2838.0112,N,07713.3602,E,0000,223.36,290518,,,A*7E,#01111011000100,0.012689,0,0,2.572415,0,4.015,22,404,4,88,3ad5,0,0.01,1.4_800F_VTS3D3_gen_peri_myn,,internet,00000000,ATL")); -- cgit v1.2.3 From cac3ddae39938a0d25c9b2a1f7fa87ecb9dd2b77 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 11 Jun 2018 21:27:36 +1200 Subject: Implement eSeal commands --- src/org/traccar/protocol/EsealProtocol.java | 6 +++ src/org/traccar/protocol/EsealProtocolEncoder.java | 45 ++++++++++++++++++++++ .../traccar/protocol/EsealProtocolEncoderTest.java | 24 ++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/org/traccar/protocol/EsealProtocolEncoder.java create mode 100644 test/org/traccar/protocol/EsealProtocolEncoderTest.java (limited to 'src') diff --git a/src/org/traccar/protocol/EsealProtocol.java b/src/org/traccar/protocol/EsealProtocol.java index 534e3d0c4..c778021ec 100644 --- a/src/org/traccar/protocol/EsealProtocol.java +++ b/src/org/traccar/protocol/EsealProtocol.java @@ -22,6 +22,7 @@ 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 org.traccar.model.Command; import java.util.List; @@ -29,6 +30,10 @@ public class EsealProtocol extends BaseProtocol { public EsealProtocol() { super("eseal"); + setSupportedDataCommands( + Command.TYPE_CUSTOM, + Command.TYPE_ALARM_ARM, + Command.TYPE_ALARM_DISARM); } @Override @@ -39,6 +44,7 @@ public class EsealProtocol extends BaseProtocol { pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectEncoder", new EsealProtocolEncoder()); pipeline.addLast("objectDecoder", new EsealProtocolDecoder(EsealProtocol.this)); } }); diff --git a/src/org/traccar/protocol/EsealProtocolEncoder.java b/src/org/traccar/protocol/EsealProtocolEncoder.java new file mode 100644 index 000000000..886bb80b1 --- /dev/null +++ b/src/org/traccar/protocol/EsealProtocolEncoder.java @@ -0,0 +1,45 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.traccar.StringProtocolEncoder; +import org.traccar.helper.Log; +import org.traccar.model.Command; + +public class EsealProtocolEncoder extends StringProtocolEncoder { + + @Override + protected Object encodeCommand(Command command) { + + switch (command.getType()) { + case Command.TYPE_CUSTOM: + return formatCommand( + command, "##S,eSeal,{%s},256,3.0.8,{%s},E##", Command.KEY_UNIQUE_ID, Command.KEY_DATA); + case Command.TYPE_ALARM_ARM: + return formatCommand( + command, "##S,eSeal,{%s},256,3.0.8,RC-Power Control,Power OFF,E##", Command.KEY_UNIQUE_ID); + case Command.TYPE_ALARM_DISARM: + return formatCommand( + command, "##S,eSeal,{%s},256,3.0.8,RC-Unlock,E##", Command.KEY_UNIQUE_ID); + default: + Log.warning(new UnsupportedOperationException(command.getType())); + break; + } + + return null; + } + +} diff --git a/test/org/traccar/protocol/EsealProtocolEncoderTest.java b/test/org/traccar/protocol/EsealProtocolEncoderTest.java new file mode 100644 index 000000000..16f00d69b --- /dev/null +++ b/test/org/traccar/protocol/EsealProtocolEncoderTest.java @@ -0,0 +1,24 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; +import org.traccar.model.Command; + +import static org.junit.Assert.assertEquals; + +public class EsealProtocolEncoderTest extends ProtocolTest { + + @Test + public void testEncode() throws Exception { + + EsealProtocolEncoder encoder = new EsealProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_ALARM_DISARM); + + assertEquals("##S,eSeal,123456789012345,256,3.0.8,RC-Unlock,E##", encoder.encodeCommand(command)); + + } + +} -- cgit v1.2.3 From 86ad45c63c9a9e9bee59a8cc621416e1396f9c89 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 11 Jun 2018 22:00:45 +1200 Subject: Implement Freematics UDP protocol --- setup/default.xml | 1 + src/org/traccar/protocol/FreematicsProtocol.java | 43 +++++++++ .../protocol/FreematicsProtocolDecoder.java | 103 +++++++++++++++++++++ .../protocol/FreematicsProtocolDecoderTest.java | 18 ++++ 4 files changed, 165 insertions(+) create mode 100644 src/org/traccar/protocol/FreematicsProtocol.java create mode 100644 src/org/traccar/protocol/FreematicsProtocolDecoder.java create mode 100644 test/org/traccar/protocol/FreematicsProtocolDecoderTest.java (limited to 'src') diff --git a/setup/default.xml b/setup/default.xml index 15d169f63..1ced20f38 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -245,5 +245,6 @@ 5167 5168 5169 + 5170 diff --git a/src/org/traccar/protocol/FreematicsProtocol.java b/src/org/traccar/protocol/FreematicsProtocol.java new file mode 100644 index 000000000..551c7e619 --- /dev/null +++ b/src/org/traccar/protocol/FreematicsProtocol.java @@ -0,0 +1,43 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.ConnectionlessBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class FreematicsProtocol extends BaseProtocol { + + public FreematicsProtocol() { + super("freematics"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ConnectionlessBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new FreematicsProtocolDecoder(FreematicsProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/FreematicsProtocolDecoder.java b/src/org/traccar/protocol/FreematicsProtocolDecoder.java new file mode 100644 index 000000000..66e451d6d --- /dev/null +++ b/src/org/traccar/protocol/FreematicsProtocolDecoder.java @@ -0,0 +1,103 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.DateBuilder; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.util.Date; + +public class FreematicsProtocolDecoder extends BaseProtocolDecoder { + + public FreematicsProtocolDecoder(FreematicsProtocol protocol) { + super(protocol); + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + String sentence = (String) msg; + int startIndex = sentence.indexOf('#'); + int endIndex = sentence.indexOf('*'); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, sentence.substring(0, startIndex)); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + position.setValid(true); + + DateBuilder dateBuilder = new DateBuilder(new Date()); + + for (String pair : sentence.substring(startIndex + 1, endIndex).split(",")) { + String[] data = pair.split("="); + int key = Integer.parseInt(data[0], 16); + String value = data[1]; + switch (key) { + case 0x11: + value = ("000000" + value).substring(value.length()); + dateBuilder.setDateReverse( + Integer.parseInt(value.substring(0, 2)), + Integer.parseInt(value.substring(2, 4)), + Integer.parseInt(value.substring(4))); + break; + case 0x10: + value = ("00000000" + value).substring(value.length()); + dateBuilder.setTime( + Integer.parseInt(value.substring(0, 2)), + Integer.parseInt(value.substring(2, 4)), + Integer.parseInt(value.substring(4, 6)), + Integer.parseInt(value.substring(6)) * 10); + break; + case 0xA: + position.setLatitude(Double.parseDouble(value)); + break; + case 0xB: + position.setLongitude(Double.parseDouble(value)); + break; + case 0xC: + position.setAltitude(Integer.parseInt(value)); + break; + case 0xD: + position.setLatitude(UnitsConverter.knotsFromKph(Integer.parseInt(value))); + break; + case 0xE: + position.setCourse(Integer.parseInt(value)); + break; + case 0xF: + position.set(Position.KEY_SATELLITES, Integer.parseInt(value)); + break; + default: + position.set(data[0], value); + break; + } + + } + + position.setTime(dateBuilder.getDate()); + + return position; + } + +} diff --git a/test/org/traccar/protocol/FreematicsProtocolDecoderTest.java b/test/org/traccar/protocol/FreematicsProtocolDecoderTest.java new file mode 100644 index 000000000..f629f68df --- /dev/null +++ b/test/org/traccar/protocol/FreematicsProtocolDecoderTest.java @@ -0,0 +1,18 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class FreematicsProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + FreematicsProtocolDecoder decoder = new FreematicsProtocolDecoder(new FreematicsProtocol()); + + verifyPosition(decoder, text( + "1#0=68338,10D=79,30=1010,105=199,10C=4375,104=56,111=62,20=0;-1;95,10=6454200,A=-32.727482,B=150.150301,C=159,D=0,F=5,24=1250*7A")); + + } + +} -- cgit v1.2.3