Skip to content

Instantly share code, notes, and snippets.

@normanmaurer
Created October 14, 2011 11:45
Show Gist options
  • Save normanmaurer/1286885 to your computer and use it in GitHub Desktop.
Save normanmaurer/1286885 to your computer and use it in GitHub Desktop.
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