diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2021-04-19 22:04:19 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2021-04-19 22:04:19 -0700 |
commit | d1d3be228d270355c9d48ede69a6e3570d3710b7 (patch) | |
tree | 62b36176047fe17f6dd845e531a1575f30b44a3b /src | |
parent | d45729113d11116d1645d26e7606560aecbb8b11 (diff) | |
download | traccar-server-d1d3be228d270355c9d48ede69a6e3570d3710b7.tar.gz traccar-server-d1d3be228d270355c9d48ede69a6e3570d3710b7.tar.bz2 traccar-server-d1d3be228d270355c9d48ede69a6e3570d3710b7.zip |
Implement iStartek commands
Diffstat (limited to 'src')
3 files changed, 84 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/protocol/StartekProtocol.java b/src/main/java/org/traccar/protocol/StartekProtocol.java index e22973243..230dc3118 100644 --- a/src/main/java/org/traccar/protocol/StartekProtocol.java +++ b/src/main/java/org/traccar/protocol/StartekProtocol.java @@ -21,16 +21,22 @@ import io.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; +import org.traccar.model.Command; public class StartekProtocol extends BaseProtocol { public StartekProtocol() { + setSupportedDataCommands( + Command.TYPE_OUTPUT_CONTROL, + Command.TYPE_ENGINE_STOP, + Command.TYPE_ENGINE_RESUME); addServer(new TrackerServer(false, getName()) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast(new LineBasedFrameDecoder(1024)); pipeline.addLast(new StringEncoder()); pipeline.addLast(new StringDecoder()); + pipeline.addLast(new StartekProtocolEncoder(StartekProtocol.this)); pipeline.addLast(new StartekProtocolDecoder(StartekProtocol.this)); } }); diff --git a/src/main/java/org/traccar/protocol/StartekProtocolEncoder.java b/src/main/java/org/traccar/protocol/StartekProtocolEncoder.java new file mode 100644 index 000000000..a96502f11 --- /dev/null +++ b/src/main/java/org/traccar/protocol/StartekProtocolEncoder.java @@ -0,0 +1,54 @@ +/* + * Copyright 2021 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.channel.Channel; +import org.traccar.Protocol; +import org.traccar.StringProtocolEncoder; +import org.traccar.helper.Checksum; +import org.traccar.model.Command; + +public class StartekProtocolEncoder extends StringProtocolEncoder { + + public StartekProtocolEncoder(Protocol protocol) { + super(protocol); + } + + @Override + protected String formatCommand(Command command, String format, String... keys) { + String uniqueId = getUniqueId(command.getDeviceId()); + String payload = super.formatCommand(command, format, keys); + int length = uniqueId.length() + 1 + payload.length(); + String sentence = "$$:" + length + "," + uniqueId + "," + payload; + return sentence + Checksum.sum(sentence) + "\r\n"; + } + + @Override + protected Object encodeCommand(Channel channel, Command command) { + + switch (command.getType()) { + case Command.TYPE_OUTPUT_CONTROL: + return formatCommand(command, "900,%s,%s", Command.KEY_INDEX, Command.KEY_DATA); + case Command.TYPE_ENGINE_STOP: + return formatCommand(command, "900,1,1"); + case Command.TYPE_ENGINE_RESUME: + return formatCommand(command, "900,1,0"); + default: + return null; + } + } + +} diff --git a/src/test/java/org/traccar/protocol/StartekProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/StartekProtocolEncoderTest.java new file mode 100644 index 000000000..f23cfb227 --- /dev/null +++ b/src/test/java/org/traccar/protocol/StartekProtocolEncoderTest.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 StartekProtocolEncoderTest extends ProtocolTest { + + @Test + public void testEncodeEngineStop() { + + var encoder = new StartekProtocolEncoder(null); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_ENGINE_STOP); + + assertEquals("$$:23,123456789012345,900,1,19E\r\n", encoder.encodeCommand(null, command)); + + } + +} |