From 24f96678d940b44182abfb9a8d51f0f2abdb14bc Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 3 Jun 2019 22:20:31 -0700 Subject: Rename Sigfox HTTP protocol --- setup/default.xml | 2 +- .../org/traccar/protocol/SigfoxHttpProtocol.java | 39 +++++++++ .../protocol/SigfoxHttpProtocolDecoder.java | 92 ++++++++++++++++++++++ .../java/org/traccar/protocol/SigfoxProtocol.java | 39 --------- .../traccar/protocol/SigfoxProtocolDecoder.java | 92 ---------------------- .../protocol/SigfoxHttpProtocolDecoderTest.java | 19 +++++ .../protocol/SigfoxProtocolDecoderTest.java | 19 ----- 7 files changed, 151 insertions(+), 151 deletions(-) create mode 100644 src/main/java/org/traccar/protocol/SigfoxHttpProtocol.java create mode 100644 src/main/java/org/traccar/protocol/SigfoxHttpProtocolDecoder.java delete mode 100644 src/main/java/org/traccar/protocol/SigfoxProtocol.java delete mode 100644 src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java create mode 100644 src/test/java/org/traccar/protocol/SigfoxHttpProtocolDecoderTest.java delete mode 100644 src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java diff --git a/setup/default.xml b/setup/default.xml index f2063c5ed..32000432b 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -230,7 +230,7 @@ 5151 5152 5153 - 5154 + 5154 5155 5156 5157 diff --git a/src/main/java/org/traccar/protocol/SigfoxHttpProtocol.java b/src/main/java/org/traccar/protocol/SigfoxHttpProtocol.java new file mode 100644 index 000000000..587b8fa68 --- /dev/null +++ b/src/main/java/org/traccar/protocol/SigfoxHttpProtocol.java @@ -0,0 +1,39 @@ +/* + * Copyright 2017 - 2019 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 io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpRequestDecoder; +import io.netty.handler.codec.http.HttpResponseEncoder; +import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; +import org.traccar.TrackerServer; + +public class SigfoxHttpProtocol extends BaseProtocol { + + public SigfoxHttpProtocol() { + addServer(new TrackerServer(false, getName()) { + @Override + protected void addProtocolHandlers(PipelineBuilder pipeline) { + pipeline.addLast(new HttpResponseEncoder()); + pipeline.addLast(new HttpRequestDecoder()); + pipeline.addLast(new HttpObjectAggregator(65535)); + pipeline.addLast(new SigfoxHttpProtocolDecoder(SigfoxHttpProtocol.this)); + } + }); + } + +} diff --git a/src/main/java/org/traccar/protocol/SigfoxHttpProtocolDecoder.java b/src/main/java/org/traccar/protocol/SigfoxHttpProtocolDecoder.java new file mode 100644 index 000000000..e50336d7c --- /dev/null +++ b/src/main/java/org/traccar/protocol/SigfoxHttpProtocolDecoder.java @@ -0,0 +1,92 @@ +/* + * Copyright 2017 - 2019 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 io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.HttpResponseStatus; +import org.traccar.BaseHttpProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.Protocol; +import org.traccar.helper.DataConverter; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.Position; + +import javax.json.Json; +import javax.json.JsonObject; +import java.io.StringReader; +import java.net.SocketAddress; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.Date; + +public class SigfoxHttpProtocolDecoder extends BaseHttpProtocolDecoder { + + public SigfoxHttpProtocolDecoder(Protocol protocol) { + super(protocol); + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + FullHttpRequest request = (FullHttpRequest) msg; + JsonObject json = Json.createReader(new StringReader(URLDecoder.decode( + request.content().toString(StandardCharsets.UTF_8).split("=")[0], "UTF-8"))).readObject(); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, json.getString("device")); + if (deviceSession == null) { + sendResponse(channel, HttpResponseStatus.BAD_REQUEST); + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.setTime(new Date(json.getInt("time") * 1000L)); + + ByteBuf buf = Unpooled.wrappedBuffer(DataConverter.parseHex(json.getString("data"))); + try { + int type = buf.readUnsignedByte() >> 4; + if (type == 0) { + + position.setValid(true); + position.setLatitude(buf.readIntLE() * 0.0000001); + position.setLongitude(buf.readIntLE() * 0.0000001); + position.setCourse(buf.readUnsignedByte() * 2); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); + + position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.025); + + } else { + + getLastLocation(position, position.getDeviceTime()); + + } + } finally { + buf.release(); + } + + position.set(Position.KEY_RSSI, json.getJsonNumber("rssi").doubleValue()); + position.set(Position.KEY_INDEX, json.getInt("seqNumber")); + + sendResponse(channel, HttpResponseStatus.OK); + return position; + } + +} diff --git a/src/main/java/org/traccar/protocol/SigfoxProtocol.java b/src/main/java/org/traccar/protocol/SigfoxProtocol.java deleted file mode 100644 index e2f2cbe1f..000000000 --- a/src/main/java/org/traccar/protocol/SigfoxProtocol.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2017 - 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 io.netty.handler.codec.http.HttpObjectAggregator; -import io.netty.handler.codec.http.HttpRequestDecoder; -import io.netty.handler.codec.http.HttpResponseEncoder; -import org.traccar.BaseProtocol; -import org.traccar.PipelineBuilder; -import org.traccar.TrackerServer; - -public class SigfoxProtocol extends BaseProtocol { - - public SigfoxProtocol() { - addServer(new TrackerServer(false, getName()) { - @Override - protected void addProtocolHandlers(PipelineBuilder pipeline) { - pipeline.addLast(new HttpResponseEncoder()); - pipeline.addLast(new HttpRequestDecoder()); - pipeline.addLast(new HttpObjectAggregator(65535)); - pipeline.addLast(new SigfoxProtocolDecoder(SigfoxProtocol.this)); - } - }); - } - -} diff --git a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java b/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java deleted file mode 100644 index d7836b35d..000000000 --- a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2017 - 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 io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.channel.Channel; -import io.netty.handler.codec.http.FullHttpRequest; -import io.netty.handler.codec.http.HttpResponseStatus; -import org.traccar.BaseHttpProtocolDecoder; -import org.traccar.DeviceSession; -import org.traccar.Protocol; -import org.traccar.helper.DataConverter; -import org.traccar.helper.UnitsConverter; -import org.traccar.model.Position; - -import javax.json.Json; -import javax.json.JsonObject; -import java.io.StringReader; -import java.net.SocketAddress; -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; -import java.util.Date; - -public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder { - - public SigfoxProtocolDecoder(Protocol protocol) { - super(protocol); - } - - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - - FullHttpRequest request = (FullHttpRequest) msg; - JsonObject json = Json.createReader(new StringReader(URLDecoder.decode( - request.content().toString(StandardCharsets.UTF_8).split("=")[0], "UTF-8"))).readObject(); - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, json.getString("device")); - if (deviceSession == null) { - sendResponse(channel, HttpResponseStatus.BAD_REQUEST); - return null; - } - - Position position = new Position(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - - position.setTime(new Date(json.getInt("time") * 1000L)); - - ByteBuf buf = Unpooled.wrappedBuffer(DataConverter.parseHex(json.getString("data"))); - try { - int type = buf.readUnsignedByte() >> 4; - if (type == 0) { - - position.setValid(true); - position.setLatitude(buf.readIntLE() * 0.0000001); - position.setLongitude(buf.readIntLE() * 0.0000001); - position.setCourse(buf.readUnsignedByte() * 2); - position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); - - position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.025); - - } else { - - getLastLocation(position, position.getDeviceTime()); - - } - } finally { - buf.release(); - } - - position.set(Position.KEY_RSSI, json.getJsonNumber("rssi").doubleValue()); - position.set(Position.KEY_INDEX, json.getInt("seqNumber")); - - sendResponse(channel, HttpResponseStatus.OK); - return position; - } - -} diff --git a/src/test/java/org/traccar/protocol/SigfoxHttpProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/SigfoxHttpProtocolDecoderTest.java new file mode 100644 index 000000000..7acc64477 --- /dev/null +++ b/src/test/java/org/traccar/protocol/SigfoxHttpProtocolDecoderTest.java @@ -0,0 +1,19 @@ +package org.traccar.protocol; + +import io.netty.handler.codec.http.HttpMethod; +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class SigfoxHttpProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + SigfoxHttpProtocolDecoder decoder = new SigfoxHttpProtocolDecoder(null); + + verifyPosition(decoder, request(HttpMethod.POST, "/", + buffer("%7B++%22device%22%3A%222BF839%22%2C++%22time%22%3A1510605882%2C++%22duplicate%22%3Afalse%2C++%22snr%22%3A45.61%2C++%22station%22%3A%2235A9%22%2C++%22data%22%3A%2200bd6475e907398e562d01b9%22%2C++%22avgSnr%22%3A45.16%2C++%22lat%22%3A-38.0%2C++%22lng%22%3A145.0%2C++%22rssi%22%3A-98.00%2C++%22seqNumber%22%3A228+%7D="))); + + } + +} diff --git a/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java deleted file mode 100644 index 48adb0ccb..000000000 --- a/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.traccar.protocol; - -import io.netty.handler.codec.http.HttpMethod; -import org.junit.Test; -import org.traccar.ProtocolTest; - -public class SigfoxProtocolDecoderTest extends ProtocolTest { - - @Test - public void testDecode() throws Exception { - - SigfoxProtocolDecoder decoder = new SigfoxProtocolDecoder(null); - - verifyPosition(decoder, request(HttpMethod.POST, "/", - buffer("%7B++%22device%22%3A%222BF839%22%2C++%22time%22%3A1510605882%2C++%22duplicate%22%3Afalse%2C++%22snr%22%3A45.61%2C++%22station%22%3A%2235A9%22%2C++%22data%22%3A%2200bd6475e907398e562d01b9%22%2C++%22avgSnr%22%3A45.16%2C++%22lat%22%3A-38.0%2C++%22lng%22%3A145.0%2C++%22rssi%22%3A-98.00%2C++%22seqNumber%22%3A228+%7D="))); - - } - -} -- cgit v1.2.3