Created
October 14, 2011 11:45
-
-
Save normanmaurer/1286885 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Gps103ProtocolEncoder extends OneToOneEncoder { | |
private static final Charset CHARSET = CharsetUtil.US_ASCII; | |
private static final byte[] TURN_ON_COMAND = ",K".getBytes(CHARSET); | |
private static final byte[] TURN_OFF_COMAND = ",J".getBytes(CHARSET); | |
private static final byte[] IMEI_PREFIX = "**,imei:".getBytes(CHARSET); | |
@Override | |
protected Object encode(ChannelHandlerContext ctx, Channel channel, | |
Object msg) throws Exception { | |
if (!(msg instanceof OutgoingPacket)) { | |
return msg; | |
} | |
// prefix + imei + command + optional parameters | |
ChannelBuffer buffer = ChannelBuffers.buffer(35); | |
buffer.writeBytes(IMEI_PREFIX); | |
Device device = ChannelAttributes.device.get(channel); | |
buffer.writeBytes(CHARSET.encode(device.getImei())); | |
if (msg == LightControl.TURN_ON) { | |
buffer.writeBytes(TURN_ON_COMAND); | |
} else if (msg == LightControl.TURN_OFF) { | |
buffer.writeBytes(TURN_OFF_COMAND); | |
} | |
return buffer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment