From d5bd395f19ef5ed6732b32a0e8d0eb5e6a25c33e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 18 Feb 2017 16:28:17 +1300 Subject: Implement Cellocator output control --- src/org/traccar/protocol/CellocatorProtocol.java | 7 ++- .../protocol/CellocatorProtocolEncoder.java | 72 ++++++++++++++++++++++ test/org/traccar/FilterHandlerTest.java | 10 +-- test/org/traccar/ProtocolTest.java | 2 - .../protocol/CellocatorProtocolEncoderTest.java | 26 ++++++++ 5 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 src/org/traccar/protocol/CellocatorProtocolEncoder.java create mode 100644 test/org/traccar/protocol/CellocatorProtocolEncoderTest.java diff --git a/src/org/traccar/protocol/CellocatorProtocol.java b/src/org/traccar/protocol/CellocatorProtocol.java index bfaf03692..7c8510204 100644 --- a/src/org/traccar/protocol/CellocatorProtocol.java +++ b/src/org/traccar/protocol/CellocatorProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 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. @@ -20,6 +20,7 @@ import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.ChannelPipeline; import org.traccar.BaseProtocol; import org.traccar.TrackerServer; +import org.traccar.model.Command; import java.nio.ByteOrder; import java.util.List; @@ -28,6 +29,8 @@ public class CellocatorProtocol extends BaseProtocol { public CellocatorProtocol() { super("cellocator"); + setSupportedCommands( + Command.TYPE_OUTPUT_CONTROL); } @Override @@ -36,6 +39,7 @@ public class CellocatorProtocol extends BaseProtocol { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("frameDecoder", new CellocatorFrameDecoder()); + pipeline.addLast("objectEncoder", new CellocatorProtocolEncoder()); pipeline.addLast("objectDecoder", new CellocatorProtocolDecoder(CellocatorProtocol.this)); } }; @@ -45,6 +49,7 @@ public class CellocatorProtocol extends BaseProtocol { server = new TrackerServer(new ConnectionlessBootstrap(), getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("objectEncoder", new CellocatorProtocolEncoder()); pipeline.addLast("objectDecoder", new CellocatorProtocolDecoder(CellocatorProtocol.this)); } }; diff --git a/src/org/traccar/protocol/CellocatorProtocolEncoder.java b/src/org/traccar/protocol/CellocatorProtocolEncoder.java new file mode 100644 index 000000000..bb143d349 --- /dev/null +++ b/src/org/traccar/protocol/CellocatorProtocolEncoder.java @@ -0,0 +1,72 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.traccar.BaseProtocolEncoder; +import org.traccar.helper.Log; +import org.traccar.model.Command; + +import java.nio.ByteOrder; + +public class CellocatorProtocolEncoder extends BaseProtocolEncoder { + + private ChannelBuffer encodeContent(long deviceId, int command, int data1, int data2) { + + ChannelBuffer buf = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + buf.writeByte('M'); + buf.writeByte('C'); + buf.writeByte('G'); + buf.writeByte('P'); + buf.writeByte(0); + buf.writeInt(Integer.parseInt(getUniqueId(deviceId))); + buf.writeByte(0); // command numerator + buf.writeInt(0); // authentication code + buf.writeByte(command); + buf.writeByte(command); + buf.writeByte(data1); + buf.writeByte(data1); + buf.writeByte(data2); + buf.writeByte(data2); + buf.writeInt(0); // command specific data + + byte checksum = 0; + for (int i = 4; i < buf.writerIndex(); i++) { + checksum += buf.getByte(i); + } + buf.writeByte(checksum); + + return buf; + } + + @Override + protected Object encodeCommand(Command command) { + + switch (command.getType()) { + case Command.TYPE_OUTPUT_CONTROL: + int data = Integer.parseInt(command.getString(Command.KEY_DATA)) << 4 + + command.getInteger(Command.KEY_INDEX); + return encodeContent(command.getDeviceId(), 0x03, data, 0); + default: + Log.warning(new UnsupportedOperationException(command.getType())); + break; + } + + return null; + } + +} diff --git a/test/org/traccar/FilterHandlerTest.java b/test/org/traccar/FilterHandlerTest.java index 11b4163a1..452592048 100644 --- a/test/org/traccar/FilterHandlerTest.java +++ b/test/org/traccar/FilterHandlerTest.java @@ -1,14 +1,14 @@ package org.traccar; -import java.util.Date; import org.junit.After; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import org.junit.Before; import org.junit.Test; -import org.traccar.database.IdentityManager; import org.traccar.model.Position; -import org.traccar.model.Device; + +import java.util.Date; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; public class FilterHandlerTest extends BaseTest { diff --git a/test/org/traccar/ProtocolTest.java b/test/org/traccar/ProtocolTest.java index 955e0b788..d4543a06d 100644 --- a/test/org/traccar/ProtocolTest.java +++ b/test/org/traccar/ProtocolTest.java @@ -6,10 +6,8 @@ import org.jboss.netty.handler.codec.http.DefaultHttpRequest; import org.jboss.netty.handler.codec.http.HttpMethod; import org.jboss.netty.handler.codec.http.HttpVersion; import org.junit.Assert; -import org.traccar.database.IdentityManager; import org.traccar.model.CellTower; import org.traccar.model.Command; -import org.traccar.model.Device; import org.traccar.model.Position; import javax.xml.bind.DatatypeConverter; diff --git a/test/org/traccar/protocol/CellocatorProtocolEncoderTest.java b/test/org/traccar/protocol/CellocatorProtocolEncoderTest.java new file mode 100644 index 000000000..89850fb5f --- /dev/null +++ b/test/org/traccar/protocol/CellocatorProtocolEncoderTest.java @@ -0,0 +1,26 @@ +package org.traccar.protocol; + +import org.junit.Ignore; +import org.junit.Test; +import org.traccar.ProtocolTest; +import org.traccar.model.Command; + +public class CellocatorProtocolEncoderTest extends ProtocolTest { + + @Ignore + @Test + public void testEncode() throws Exception { + + CellocatorProtocolEncoder encoder = new CellocatorProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_OUTPUT_CONTROL); + command.set(Command.KEY_INDEX, 0); + command.set(Command.KEY_DATA, "1"); + + verifyCommand(encoder, command, binary("4D434750000000000000000000000303101000000000000026")); + + } + +} -- cgit v1.2.3