diff options
Diffstat (limited to 'src/org/traccar/protocol/RuptelaProtocolEncoder.java')
-rw-r--r-- | src/org/traccar/protocol/RuptelaProtocolEncoder.java | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/src/org/traccar/protocol/RuptelaProtocolEncoder.java b/src/org/traccar/protocol/RuptelaProtocolEncoder.java index 0abfa18ce..420e2cb3d 100644 --- a/src/org/traccar/protocol/RuptelaProtocolEncoder.java +++ b/src/org/traccar/protocol/RuptelaProtocolEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 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. @@ -26,13 +26,13 @@ import java.nio.charset.StandardCharsets; public class RuptelaProtocolEncoder extends BaseProtocolEncoder { - private ChannelBuffer encodeContent(String content) { + private ChannelBuffer encodeContent(int type, ChannelBuffer content) { ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); - buf.writeShort(1 + content.length()); - buf.writeByte(RuptelaProtocolDecoder.MSG_SMS_VIA_GPRS); - buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII)); + buf.writeShort(1 + content.readableBytes()); + buf.writeByte(type); + buf.writeBytes(content); buf.writeShort(Checksum.crc16(Checksum.CRC16_KERMIT, buf.toByteBuffer(2, buf.writerIndex() - 2))); return buf; @@ -41,9 +41,31 @@ public class RuptelaProtocolEncoder extends BaseProtocolEncoder { @Override protected Object encodeCommand(Command command) { + ChannelBuffer content = ChannelBuffers.dynamicBuffer(); + switch (command.getType()) { case Command.TYPE_CUSTOM: - return encodeContent(command.getString(Command.KEY_DATA)); + content.writeBytes(command.getString(Command.KEY_DATA).getBytes(StandardCharsets.US_ASCII)); + return encodeContent(RuptelaProtocolDecoder.MSG_SMS_VIA_GPRS, content); + case Command.TYPE_CONFIGURATION: + content.writeBytes((command.getString(Command.KEY_DATA) + "\r\n").getBytes(StandardCharsets.US_ASCII)); + return encodeContent(RuptelaProtocolDecoder.MSG_DEVICE_CONFIGURATION, content); + case Command.TYPE_GET_VERSION: + return encodeContent(RuptelaProtocolDecoder.MSG_DEVICE_VERSION, content); + case Command.TYPE_FIRMWARE_UPDATE: + content.writeBytes("|FU_STRT*\r\n".getBytes(StandardCharsets.US_ASCII)); + return encodeContent(RuptelaProtocolDecoder.MSG_FIRMWARE_UPDATE, content); + case Command.TYPE_OUTPUT_CONTROL: + content.writeInt(command.getInteger(Command.KEY_INDEX)); + content.writeInt(Integer.parseInt(command.getString(Command.KEY_DATA))); + return encodeContent(RuptelaProtocolDecoder.MSG_SET_IO, content); + case Command.TYPE_SET_CONNECTION: + String c = command.getString(Command.KEY_SERVER) + "," + command.getInteger(Command.KEY_PORT) + ",TCP"; + content.writeBytes(c.getBytes(StandardCharsets.US_ASCII)); + return encodeContent(RuptelaProtocolDecoder.MSG_SET_CONNECTION, content); + case Command.TYPE_SET_ODOMETER: + content.writeInt(Integer.parseInt(command.getString(Command.KEY_DATA))); + return encodeContent(RuptelaProtocolDecoder.MSG_SET_ODOMETER, content); default: Log.warning(new UnsupportedOperationException(command.getType())); break; |